OrderPay.php 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879
  1. <?php
  2. namespace app\admin\controller;
  3. use app\admin\BaseController;
  4. use app\admin\model\ReportCode;use think\App;
  5. use think\facade\Db;use think\queue\command\Retry;
  6. class OrderPay extends BaseController{
  7. public function __construct(App $app) {parent::__construct($app);}
  8. //新建资金认领数据
  9. public function create(){
  10. $tradNo = isset($this->post['tradNo'])&&$this->post['tradNo']!=""?trim($this->post['tradNo']):"";
  11. if($tradNo==""){
  12. return error_show(1004,"参数 tradNo 不能为空");
  13. }
  14. $orderArr = isset($this->post['orderArr'])&&!empty($this->post['orderArr'])?$this->post['orderArr']:[];
  15. if(empty($orderArr)){
  16. return error_show(1004,"参数 orderArr 不能为空");
  17. }
  18. Db::startTrans();
  19. try{
  20. $logNo=makeNo("TRC");
  21. $trade =Db::name("trade")->where(["tradNo"=>$tradNo,"is_del"=>0])->lock(true)->findOrEmpty();
  22. if(empty($trade)){
  23. Db::rollback();
  24. return error_show(1004,"未找到资金信息");
  25. }
  26. $total_fee =array_sum(array_column($orderArr,"trad_fee"));
  27. if ($trade['balance']<$total_fee){
  28. Db::rollback();
  29. return error_show(1004,"资金余额不足核销");
  30. }
  31. $assoc=[];
  32. foreach ($orderArr as $value){
  33. if(!isset($value['sequenceNo'])||$value['sequenceNo']==""){
  34. Db::rollback();
  35. return error_show(1004,"销售单编号不能为空");
  36. }
  37. if(!isset($value['trad_fee'])||$value['trad_fee']==""){
  38. Db::rollback();
  39. return error_show(1004,"销售单核销金额不能为空");
  40. }
  41. $qrd =Db::name("qrd_info")->where("sequenceNo","=",$value['sequenceNo'])->field("id,customerNo,status,pay_fee,wpay_fee,pay_status,totalPrice")->findOrEmpty();
  42. if(empty($qrd)){
  43. Db::rollback();
  44. return error_show(1004,"销售单信息未找到");
  45. }
  46. if($qrd['wpay_fee']<$value['trad_fee']){
  47. Db::rollback();
  48. return error_show(1004,"销售单未付款金额不足核销金额");
  49. }
  50. $updt=[
  51. "pay_fee"=>$qrd['pay_fee']+$value['trad_fee'],
  52. "wpay_fee"=>$qrd['wpay_fee']-$value['trad_fee'],
  53. "pay_status"=>2,
  54. "status"=>1,
  55. "updatetime"=>date("Y-m-d H:i:s")
  56. ];
  57. $qrdup = Db::name("qrd_info")->where($qrd)->update($updt);
  58. if($qrdup==false){
  59. Db::rollback();
  60. return error_show(1004,"销售单核销金额失败");
  61. }
  62. $temp=[
  63. "assocNo"=>makeNo("AS"),
  64. "apply_id"=>$this->uid,
  65. "apply_name"=>$this->uname,
  66. "type"=>2,
  67. "orderCode"=>$value['sequenceNo'],
  68. "customerNo"=>$qrd['customerNo'],
  69. "viceCode"=>$logNo,
  70. "order_total"=>$qrd['totalPrice'],
  71. "vice_total"=>$total_fee,
  72. "cancel_fee"=>$value['trad_fee'],
  73. "status"=>1,
  74. "addtime"=>date("Y-m-d H:i:s"),
  75. "updatetime"=>date("Y-m-d H:i:s")
  76. ];
  77. $assoc[]=$temp;
  78. $report=ReportCode::where(["qrdNo"=>$value['sequenceNo']])->find();
  79. if($report)$report->setField("logNo",$logNo);
  80. if($report)$report->setField("tradNo",$tradNo);
  81. }
  82. $asscin= Db::name("assoc")->insertAll($assoc);
  83. if($asscin!=count($assoc)){
  84. Db::rollback();
  85. return error_show(1004,"销售单核销金额关联失败");
  86. }
  87. $update=[
  88. "balance"=>$trade['balance']-$total_fee,
  89. "used_fee"=>$trade['used_fee']+$total_fee,
  90. "status"=>($trade['balance']-$total_fee)==0 ? 2:1,
  91. "updatetime"=>date("Y-m-d H:i:s"),
  92. ];
  93. $tradup=Db::name("trade")->where($trade)->update($update);
  94. if($tradup){
  95. $create = [
  96. "logNo"=>$logNo,
  97. "tradNo"=>$tradNo,
  98. "apply_id"=>$this->uid,
  99. "apply_name"=>$this->uname,
  100. "trade_time"=>$trade['trade_time'],
  101. "total_fee"=>$total_fee,
  102. "status"=>1,
  103. "addtime"=>date("Y-m-d H:i:s"),
  104. "updatetime"=>date("Y-m-d H:i:s")
  105. ];
  106. $tradchild =Db::name("trade_pool")->insert($create);
  107. if($tradchild){
  108. Db::commit();
  109. return app_show(0,"资金认领成功",["logNo"=>$logNo]);
  110. }
  111. }
  112. Db::rollback();
  113. return error_show(1004,"资金认领失败");
  114. }catch (\Exception $e){
  115. Db::rollback();
  116. return error_show(1004,$e->getMessage());
  117. }
  118. }
  119. // 1待审批2审批通过3审批驳回4退款5解除认领
  120. public function status(){
  121. $logNo = isset($this->post['logNo'])&&$this->post['logNo']!=""?trim($this->post['logNo']):"";
  122. if($logNo==""){
  123. return error_show(1004,"参数logNo不能为空");
  124. }
  125. $status=isset($this->post['status'])&&$this->post['status']!=""?intval($this->post['status']):"";
  126. if($status==""){
  127. return error_show(1004,"参数 status 不能为空");
  128. }
  129. $remark=isset($this->post['remark'])&&$this->post['remark']!=""?trim($this->post['remark']):"";
  130. if($remark==""){
  131. return error_show(1004,"参数 remark 不能为空");
  132. }
  133. Db::startTrans();
  134. try {
  135. $logingo= Db::name("trade_pool")->where(["logNo"=>$logNo,"is_del"=>0])->find();
  136. if($logingo==false){
  137. Db::rollback();
  138. return error_show(1004,"资金认领信息未找到");
  139. }
  140. $up=["status"=>$status,"updatetime"=>date("Y-m-d H:i:s"),"remark"=>$remark];
  141. $logup = Db::name("trade_pool")->where($logingo)->update($up);
  142. if($logup){
  143. if($status==2){
  144. $qrdArr=Db::name("assoc")->where(["viceCode"=>$logNo,"status"=>1,"is_del"=>0])->select()->toArray();
  145. if(!empty($qrdArr)){
  146. foreach ($qrdArr as $value){
  147. $qrd =Db::name("qrd_info")->where(["sequenceNo"=>$value['orderCode']])->field("id,status,pay_fee,apay_fee,wpay_fee,pay_status,totalPrice")->findOrEmpty();
  148. if(empty($qrd)){
  149. Db::rollback();
  150. return error_show(1005,"未找到销售单数据");
  151. }
  152. if($qrd['pay_fee']<$value['cancel_fee']){
  153. Db::rollback();
  154. return error_show(1005,"销售单待付金额不足");
  155. }
  156. $update =[
  157. "apay_fee"=>$qrd['apay_fee']+$value['cancel_fee'],
  158. "pay_fee"=>$qrd['pay_fee']-$value['cancel_fee'],
  159. "paytime"=>date("Y-m-d H:i:s"),
  160. "pay_status"=>$qrd['wpay_fee']==0 && ($qrd['pay_fee']-$value['cancel_fee'])==0 ? 3:2,
  161. "updatetime"=>date("Y-m-d H:i:s")
  162. ];
  163. $qrdup =Db::name("qrd_info")->where($qrd)->update($update);
  164. if($qrdup==false){
  165. Db::rollback();
  166. return error_show(1005,"销售单更新失败");
  167. }
  168. $asscup =[
  169. "status"=>2,
  170. "assoc_time"=>date("Y-m-d H:i:s"),
  171. "updatetime"=>date("Y-m-d H:i:s")
  172. ];
  173. $assc=Db::name("assoc")->where($value)->update($asscup);
  174. if($assc==false){
  175. Db::rollback();
  176. return error_show(1005,"销售单更新失败");
  177. }
  178. }
  179. }
  180. }
  181. if($status==3){
  182. $qrdArr=Db::name("assoc")->where(["viceCode"=>$logNo,"status"=>1,"is_del"=>0])->select()->toArray();
  183. if(!empty($qrdArr)){
  184. foreach ($qrdArr as $value){
  185. $qrd =Db::name("qrd_info")->where(["sequenceNo"=>$value['orderCode']])->field("id,status,pay_fee,apay_fee,wpay_fee,pay_status,totalPrice")->findOrEmpty();
  186. if(empty($qrd)){
  187. Db::rollback();
  188. return error_show(1005,"未找到销售单数据");
  189. }
  190. if($qrd['pay_fee']<$value['cancel_fee']){
  191. Db::rollback();
  192. return error_show(1005,"销售单待付金额不足");
  193. }
  194. $update =[
  195. "wpay_fee"=>$qrd['wpay_fee']+$value['cancel_fee'],
  196. "pay_fee"=>$qrd['pay_fee']-$value['cancel_fee'],
  197. "pay_status"=>$qrd['apay_fee']==0 && ($qrd['pay_fee']-$value['cancel_fee'])==0 ? 1:2,
  198. "status"=>$qrd['apay_fee']==0 && ($qrd['pay_fee']-$value['cancel_fee'])==0 ?0:1,
  199. "updatetime"=>date("Y-m-d H:i:s")
  200. ];
  201. $qrdup =Db::name("qrd_info")->where($qrd)->update($update);
  202. if($qrdup==false){
  203. Db::rollback();
  204. return error_show(1005,"销售单更新失败");
  205. }
  206. $asscup =[
  207. "status"=>3,
  208. "updatetime"=>date("Y-m-d H:i:s")
  209. ];
  210. $assc=Db::name("assoc")->where($value)->update($asscup);
  211. if($assc==false){
  212. Db::rollback();
  213. return error_show(1005,"销售单更新失败");
  214. }
  215. $report=ReportCode::where(["qrdNo"=>$value['orderCode']])->find();
  216. if($report)$report->rmField("logNo",$logNo);
  217. }
  218. }
  219. $trade =Db::name("trade")->where(["tradNo"=>$logingo['tradNo'],"is_del"=>0])->findOrEmpty();
  220. if($trade==false){
  221. Db::rollback();
  222. return error_show(1005,"资金信息未找到");
  223. }
  224. if($trade['used_fee']<$logingo['total_fee']){
  225. Db::rollback();
  226. return error_show(1005,"资金信息已认领金额不足");
  227. }
  228. $tradup =[
  229. "used_fee"=>$trade['used_fee']-$logingo['total_fee'],
  230. "balance"=>$trade['balance']+$logingo['total_fee'],
  231. "status"=>($trade['used_fee']-$logingo['total_fee'])==0 ? 0 :1,
  232. "updatetime"=>date("Y-m-d H:i:s")
  233. ];
  234. $updaT=Db::name("trade")->where($trade)->update($tradup);
  235. if($updaT==false){
  236. Db::rollback();
  237. return error_show(1005,"资金信息更新失败");
  238. }
  239. }
  240. Db::commit();
  241. return app_show(0,"审核成功");
  242. }
  243. Db::rollback();
  244. return error_show(1004,"审核失败");
  245. }catch (\Exception $e){
  246. Db::rollback();
  247. return error_show(1004,$e->getMessage());
  248. }
  249. }
  250. //资金信息列表
  251. public function list(){
  252. $condition=[];
  253. $page=isset($this->post['page'])&&$this->post['page']!="" ? intval($this->post['page']) :1;
  254. $size=isset($this->post['size'])&&$this->post['size']!="" ? intval($this->post['size']) :15;
  255. $name =isset($this->post['name'])&&$this->post['name']!=""? trim($this->post['name']):"";
  256. $bank =isset($this->post['bank'])&&$this->post['bank']!=""? trim($this->post['bank']):"";
  257. $start =isset($this->post['start'])&&$this->post['start']!=""? trim($this->post['start']):"";
  258. $end =isset($this->post['end'])&&$this->post['end']!=""? trim($this->post['end']):"";
  259. $status =isset($this->post['status'])&&$this->post['status']!==""? intval($this->post['status']):"";
  260. $tradNo =isset($this->post['tradNo'])&&$this->post['tradNo']!=""? trim($this->post['tradNo']):"";
  261. if($tradNo!==""){
  262. $condition[]=["a.tradNo","like","%$tradNo%"];
  263. }
  264. if($bank!=""){
  265. $condition[]=["trade_bank","like","%$bank%"];
  266. }
  267. if($name!=""){
  268. $condition[]=["trade_out","like","%$name%"];
  269. }
  270. if($start!=""){
  271. $condition[]=["trade_time",">=",$start." 00:00:00"];
  272. }
  273. if($end!=""){
  274. $condition[]=["trade_time","<=",$end." 23:59:59"];
  275. }
  276. if($status!==""){
  277. $condition[]=["status","=",$status];
  278. }
  279. $count=Db::name("trade")->where($condition)->count();
  280. $total=ceil($count/$size);
  281. $page=$page>$total? intval($total):$page;
  282. $list =Db::name("trade")->where($condition)->page($page,$size)->order("addtime desc")->select()->toArray();
  283. foreach ($list as &$value){
  284. $data = Db::name("trade_pool")->alias("a")
  285. ->leftJoin("assoc b","a.logNo=b.viceCode")
  286. ->leftJoin("qrd_info c","b.orderCode=c.sequenceNo")
  287. ->where(["a.tradNo"=>$value['tradNo'],"a.is_del"=>0])
  288. ->order("a.addtime desc")
  289. ->field("a.*,b.orderCode,c.qrdSource,goodNo,goodName,qrdType,c.ownerName,c.ownerid,c.department,c.poCode")
  290. ->select()->toArray();
  291. $value['child']=$data;
  292. }
  293. return app_show(0,"获取成功",["list"=>$list,"count"=>$count]);
  294. }
  295. // 认领资金列表明细
  296. public function tradeList(){
  297. $page=isset($this->post['page'])&&$this->post['page']!="" ? intval($this->post['page']) :1;
  298. $size=isset($this->post['size'])&&$this->post['size']!="" ? intval($this->post['size']) :15;
  299. $condition =[["a.is_del","=",0],["b.is_del","=",0]];
  300. $name =isset($this->post['name'])&&$this->post['name']!=""? trim($this->post['name']):"";
  301. $bank =isset($this->post['bank'])&&$this->post['bank']!=""? trim($this->post['bank']):"";
  302. $start =isset($this->post['start'])&&$this->post['start']!=""? trim($this->post['start']):"";
  303. $end =isset($this->post['end'])&&$this->post['end']!=""? trim($this->post['end']):"";
  304. $status =isset($this->post['status'])&&$this->post['status']!==""? intval($this->post['status']):"";
  305. $apply_id =isset($this->post['apply_id'])&&$this->post['apply_id']!=""? intval($this->post['apply_id']):"";
  306. $apply_name =isset($this->post['apply_name'])&&$this->post['apply_name']!=""? trim($this->post['apply_name']):"";
  307. $tradNo =isset($this->post['tradNo'])&&$this->post['tradNo']!=""? trim($this->post['tradNo']):"";
  308. $orderCode =isset($this->post['orderCode'])&&$this->post['orderCode']!=''?trim($this->post['orderCode']):"";
  309. if($tradNo!==""){
  310. $condition[]=["a.tradNo","like","%$tradNo%"];
  311. }
  312. if($orderCode!==""){
  313. $condition[]=["d.orderCode","like","%$orderCode%"];
  314. }
  315. $logNo =isset($this->post['logNo'])&&$this->post['logNo']!=""? trim($this->post['logNo']):"";
  316. if($logNo!==""){
  317. $condition[]=["a.logNo","like","%$logNo%"];
  318. }
  319. if($bank!=""){
  320. $condition[]=["b.trade_bank","like","%$bank%"];
  321. }
  322. if($name!=""){
  323. $condition[]=["b.trade_out","like","%$name%"];
  324. }
  325. if($start!=""){
  326. $condition[]=["a.add_time",">=",$start." 00:00:00"];
  327. }
  328. if($end!=""){
  329. $condition[]=["a.add_time","<=",$end." 23:59:59"];
  330. }
  331. if($status!==""){
  332. $condition[]=["a.status","=",$status];
  333. }
  334. if($apply_id!==""){
  335. $condition[]=["a.apply_id","=",$apply_id];
  336. }
  337. if($apply_name!==""){
  338. $condition[]=["a.apply_name","like","%$apply_name%"];
  339. }
  340. $count=Db::name("trade_pool")->alias("a")
  341. ->leftJoin("trade b","a.tradNo=b.tradNo")
  342. ->leftJoin("assoc d","a.logNo=d.viceCode")
  343. ->leftJoin("qrd_info c","d.orderCode=c.sequenceNo")
  344. ->where($condition)->count();
  345. $total=ceil($count/$size);
  346. $page=$page>$total? intval($total):$page;
  347. $list =Db::name("trade_pool")->alias("a")
  348. ->leftJoin("trade b","a.tradNo=b.tradNo")
  349. ->leftJoin("assoc d","a.logNo=d.viceCode")
  350. ->leftJoin("qrd_info c","d.orderCode=c.sequenceNo")
  351. ->where($condition)
  352. ->page($page,$size)
  353. ->field("a.*,b.trade_bank,b.trade_account,b.trade_out,b.total_fee as btotal_fee,b.used_fee,b.balance,
  354. d.orderCode,c.qrdSource,c.goodNo,c.goodName,c.qrdType,c.ownerName,c.ownerid,c.department,c.poCode,c.platName")
  355. ->order("a.addtime desc")->select();
  356. return app_show(0,"获取成功",["list"=>$list,"count"=>$count]);
  357. }
  358. // 获取资金下面的认领信息
  359. public function tradeQuery(){
  360. $tradNo =isset($this->post['tradNo'])&&$this->post['tradNo']!=""? trim($this->post['tradNo']):"";
  361. $status =isset($this->post['status'])&&$this->post['status']!=""? intval($this->post['status']):"";
  362. $apply_id =isset($this->post['apply_id'])&&$this->post['apply_id']!=""? intval($this->post['apply_id']):"";
  363. $apply_name =isset($this->post['apply_name'])&&$this->post['apply_name']!=""? trim($this->post['apply_name']):"";
  364. $condition=[["is_del","=",0]];
  365. if($apply_id!==""){
  366. $condition[]=["apply_id","=",$apply_id];
  367. }
  368. if($apply_name!==""){
  369. $condition[]=["apply_name","like","%$apply_name%"];
  370. }
  371. if($status!==""){
  372. $condition[]=["status","=",$status];
  373. }
  374. if($tradNo!==""){
  375. $condition[]=["tradNo","like","%$tradNo%"];
  376. }
  377. $list =Db::name("trade_pool")->where($condition)->order("addtime desc")->select();
  378. return app_show(0,"获取成功",$list);
  379. }
  380. //资金详情
  381. public function tradeInfo(){
  382. $tradNo =isset($this->post['tradNo'])&&$this->post['tradNo']!=""? trim($this->post['tradNo']):"";
  383. if($tradNo==""){
  384. return error_show(1004,"参数 tradNo 不能为空");
  385. }
  386. $tradinfo = Db::name("trade")->where(["tradNo"=>$tradNo,"is_del"=>0])->find();
  387. if($tradinfo==false){
  388. return error_show(1004,"资金信息未找到");
  389. }
  390. return app_show(0,"获取成功",$tradinfo);
  391. }
  392. //资金认领详情
  393. public function logInfo(){
  394. $logNo =isset($this->post['logNo'])&&$this->post['logNo']!=""? trim($this->post['logNo']):"";
  395. if($logNo==""){
  396. return error_show(1004,"参数 logNo 不能为空");
  397. }
  398. $tradinfo = Db::name("trade_pool")->where(["logNo"=>$logNo,"is_del"=>0])->find();
  399. if($tradinfo==false){
  400. return error_show(1004,"资金信息未找到");
  401. }
  402. $trade = Db::name("trade")->where(["tradNo"=>$tradinfo['tradNo'],"is_del"=>0])->find();
  403. $tradinfo['trade_out']= $trade['trade_out']??"";
  404. $tradinfo['trade_in']= $trade['trade_in']??"";
  405. $tradinfo['trade_bank']= $trade['trade_bank']??"";
  406. $tradinfo['customerNo']= $trade['customerNo']??"";
  407. $tradinfo['companyNo']= $trade['companyNo']??"";
  408. $tradinfo['total_fee']= $trade['total_fee']??"";
  409. $tradinfo['balance']= $trade['balance']??"";
  410. $tradinfo['used_fee']= $trade['used_fee']??"";
  411. $orderinfo = Db::name("assoc")->alias("a")->leftJoin("qrd_info c","a.orderCode=c.sequenceNo")
  412. ->where(["a.viceCode"=>$logNo,"a.is_del"=>0,"a.status"=>[1,2]])
  413. ->order("a.addtime desc")
  414. ->field("c.*")
  415. ->findOrEmpty();
  416. $tradinfo['orderinfo']=$orderinfo;
  417. return app_show(0,"获取成功",$tradinfo);
  418. }
  419. //认领资金退回或退款 type 1 退款 2 解除资金认领
  420. public function ReturnPay(){
  421. $logNo = isset($this->post['logNo'])&&$this->post['logNo']!="" ? trim($this->post['logNo']):"";
  422. if($logNo==''){
  423. return error_show(1004,"参数 logNo 不能为空");
  424. }
  425. $type = isset($this->post['type'])&&$this->post['type']!="" ? intval($this->post['type']):"";
  426. if($type==""){
  427. return error_show(1004,"参数 type 不能为空");
  428. }
  429. $loginfo = Db::name("trade_pool")->where(["logNo"=>$logNo,"is_del"=>0])->find();
  430. if($loginfo==false){
  431. return error_show(1004,"认领资金信息未找到");
  432. }
  433. if($loginfo['status']!=2)return error_show(1004,"认领资金信息审核未通过");
  434. $tradinfo = Db::name("trade")->where(["tradNo"=>$loginfo['tradNo'],"is_del"=>0])->find();
  435. if($tradinfo==false){
  436. return error_show(1004,"资金信息未找到");
  437. }
  438. $reason = isset($this->post['return_reason'])&&$this->post['return_reason']!="" ? trim($this->post['return_reason']):"";
  439. $remark = isset($this->post['remark'])&&$this->post['remark']!="" ? trim($this->post['remark']):"";
  440. $returnCode =makeNo("RTA");
  441. $data=[
  442. "returnCode"=>$returnCode,
  443. "logNo"=>$logNo,
  444. "tradNo"=>$loginfo['tradNo'],
  445. "type"=>$type,
  446. "apply_id"=>$this->uid,
  447. "apply_name"=>$this->uname,
  448. "return_reason"=>$reason,
  449. "remark"=>$remark,
  450. "addtime"=>date("Y-m-d H:i:s"),
  451. "updatetime"=>date("Y-m-d H:i:s")
  452. ];
  453. $in = Db::name("trade_return")->insert($data);
  454. if($in){
  455. return app_show(0,"申请新建成功");
  456. }else{
  457. return error_show(1004,"申请新建失败");
  458. }
  459. }
  460. /**退款列表
  461. * //page size tradNo logNo apply_id apply_name type status returnCode
  462. * @return \think\response\Json|void
  463. * @throws \think\db\exception\DataNotFoundException
  464. * @throws \think\db\exception\DbException
  465. * @throws \think\db\exception\ModelNotFoundException
  466. */
  467. public function returnList(){
  468. $page=isset($this->post['page'])&&$this->post['page']!='' ? intval($this->post['page']):1;
  469. $size=isset($this->post['size'])&&$this->post['size']!='' ? intval($this->post['size']):15;
  470. $tradNo =isset($this->post['tradNo'])&&$this->post['tradNo']!=''?trim($this->post['tradNo']):"";
  471. $logNo =isset($this->post['logNo'])&&$this->post['logNo']!=''?trim($this->post['logNo']):"";
  472. $apply_id =isset($this->post['apply_id'])&&$this->post['apply_id']!=''?intval($this->post['apply_id']):"";
  473. $apply_name =isset($this->post['apply_name'])&&$this->post['apply_name']!=''?trim($this->post['apply_name']):"";
  474. $type =isset($this->post['type'])&&$this->post['type']!=''?intval($this->post['type']):"";
  475. $status =isset($this->post['status'])&&$this->post['status']!==''?intval($this->post['status']):"";
  476. $returnCode =isset($this->post['returnCode'])&&$this->post['returnCode']!=''?trim($this->post['returnCode']):"";
  477. $orderCode =isset($this->post['orderCode'])&&$this->post['orderCode']!=''?trim($this->post['orderCode']):"";
  478. $condition=[["a.is_del","=",0]];
  479. if($tradNo!=''){
  480. $condition[]=["a.tradNo","like","%$tradNo%"];
  481. }
  482. if($orderCode!=''){
  483. $condition[]=["b.orderCode","like","%$orderCode%"];
  484. }
  485. if($logNo!=''){
  486. $condition[]=["a.logNo","like","%$logNo%"];
  487. }
  488. if($apply_id!=''){
  489. $condition[]=["a.apply_id","=",$apply_id];
  490. }
  491. if($apply_name!=''){
  492. $condition[]=["a.apply_name","like","%$apply_name%"];
  493. }
  494. if($returnCode!=''){
  495. $condition[]=["a.returnCode","like","%$returnCode%"];
  496. }
  497. if($type!=''){
  498. $condition[]=["a.type","=",$type];
  499. }
  500. if($status!=''){
  501. $condition[]=["a.status","=",$status];
  502. }
  503. $count =Db::name("trade_return")->alias("a")
  504. ->leftJoin("assoc b","a.logNo=b.viceCode")
  505. ->leftJoin("qrd_info c","c.sequenceNo=b.orderCode")
  506. ->where($condition)->count();
  507. $total=ceil($count/$size);
  508. $page = $page>$total ? intval($total):$page;
  509. $list=Db::name("trade_return")->alias("a")->leftJoin("assoc b","a.logNo=b.viceCode")
  510. ->leftJoin("qrd_info c","c.sequenceNo=b.orderCode")
  511. ->field("a.*,b.orderCode,c.qrdSource,goodNo,goodName,qrdType,c.ownerName,c.ownerid,c.department,c.poCode,c.platName")
  512. ->where($condition)->page($page,$size)->order("a.addtime desc")->select();
  513. return app_show(0,"获取成功",["list"=>$list,"count"=>$count]);
  514. }
  515. //退款状态审核 0 待审核 1 财务审核 2 财务驳回
  516. public function returnStatus(){
  517. $returnCode=isset($this->post['returnCode'])&&$this->post['returnCode']!=''?trim($this->post['returnCode']):"";
  518. if($returnCode==''){
  519. return error_show(1004,"参数 returnCode 不能为空");
  520. }
  521. $returninfo =Db::name("trade_return")->where(["returnCode"=>$returnCode,"is_del"=>0])->find();
  522. if($returninfo==false){
  523. return error_show(1004,"退款申请数据未找到");
  524. }
  525. $status=isset($this->post['status']) &&$this->post['status']!=''?intval($this->post['status']) : '';
  526. if($status==''){
  527. return error_show(1004,"参数 status 不能为空");
  528. }
  529. $return_img = isset($this->post['return_img'])&&$this->post['return_img']!=''?trim($this->post['return_img']):"";
  530. $remark = isset($this->post['remark'])&&$this->post['remark']!=''?trim($this->post['remark']):"";
  531. $loginfo = Db::name("trade_pool")->where(["logNo"=>$returninfo['logNo'],"is_del"=>0])->find();
  532. if($loginfo==false){
  533. return error_show(1004,"认领资金信息未找到");
  534. }
  535. if($loginfo['status']!=2)return error_show(1004,"认领资金信息审核未通过");
  536. Db::startTrans();
  537. try{
  538. $update=["status"=>$status,"return_img"=>$return_img,"remark"=>$remark,"updatetime"=>date("Y-m-d H:i:s")];
  539. $up =Db::name("trade_return")->where($returninfo)->update($update);
  540. if($up){
  541. if($status==1){
  542. $qrdArr=Db::name("assoc")->where(["viceCode"=>$returninfo['logNo'],"status"=>1,"is_del"=>0])->select()
  543. ->toArray();
  544. if(!empty($qrdArr)){
  545. foreach ($qrdArr as $value){
  546. $qrd =Db::name("qrd_info")->where(["sequenceNo"=>$value['orderCode']])->field("id,status,pay_fee,apay_fee,wpay_fee,pay_status,totalPrice")->findOrEmpty();
  547. if(empty($qrd)){
  548. Db::rollback();
  549. return error_show(1005,"未找到销售单数据");
  550. }
  551. if($qrd['pay_fee']<$value['cancel_fee']){
  552. Db::rollback();
  553. return error_show(1005,"销售单待付金额不足");
  554. }
  555. $update =[
  556. "wpay_fee"=>$qrd['wpay_fee']+$value['cancel_fee'],
  557. "apay_fee"=>$qrd['apay_fee']-$value['cancel_fee'],
  558. "pay_status"=>($qrd['apay_fee']-$value['cancel_fee'])==0 && $qrd['pay_fee']==0 ? 1:2,
  559. "status"=>$qrd['pay_fee']==0 && ($qrd['apay_fee']-$value['cancel_fee'])==0 ?0:1,
  560. "updatetime"=>date("Y-m-d H:i:s")
  561. ];
  562. $qrdup =Db::name("qrd_info")->where($qrd)->update($update);
  563. if($qrdup==false){
  564. Db::rollback();
  565. return error_show(1005,"销售单更新失败");
  566. }
  567. $asscup =[
  568. "status"=>3,
  569. "updatetime"=>date("Y-m-d H:i:s")
  570. ];
  571. $assc=Db::name("assoc")->where($value)->update($asscup);
  572. if($assc==false){
  573. Db::rollback();
  574. return error_show(1005,"销售单更新失败");
  575. }
  576. $report=ReportCode::where(["qrdNo"=>$value['orderCode']])->find();
  577. if($report)$report->setField("returnTrad",$returnCode);
  578. }
  579. }
  580. $logup =["status"=>$returninfo['type']==1?5:4,"updatetime"=>date("Y-m-d H:i:s")];
  581. $upde =Db::name("trade_pool")->where($loginfo)->update($logup);
  582. if($upde==false){
  583. Db::rollback();
  584. return error_show(1005,"资金认领信息更新失败");
  585. }
  586. if($returninfo['type']==1){
  587. $trade =Db::name("trade")->where(["tradNo"=>$returninfo['tradNo'],"is_del"=>0])->find();
  588. if($trade==false){
  589. Db::rollback();
  590. return error_show(1005,"资金信息未找到");
  591. }
  592. if($trade['used_fee']<$loginfo['total_fee']){
  593. Db::rollback();
  594. return error_show(1005,"资金信息已认领金额不足");
  595. }
  596. $tradup =[
  597. "used_fee"=>$trade['used_fee']-$loginfo['total_fee'],
  598. "balance"=>$trade['balance']+$loginfo['total_fee'],
  599. "status"=>($trade['used_fee']-$loginfo['total_fee'])==0 ? 0 :1,
  600. "updatetime"=>date("Y-m-d H:i:s")
  601. ];
  602. $updaT=Db::name("trade")->where($trade)->update($tradup);
  603. if($updaT==false){
  604. Db::rollback();
  605. return error_show(1005,"资金信息更新失败");
  606. }
  607. }
  608. }
  609. Db::commit();
  610. return app_show(0,"退款申请审核成功");
  611. }
  612. Db::rollback();
  613. return error_show(1004,'审核状态失败');
  614. }catch (\Exception $e){
  615. Db::rollback();
  616. return error_show(1004,$e->getMessage());
  617. }
  618. }
  619. //退款申请详情
  620. public function returnInfo(){
  621. $returnCode=isset($this->post['returnCode'])&&$this->post['returnCode']!=''?trim($this->post['returnCode']):"";
  622. if($returnCode==''){
  623. return error_show(1004,"参数 returnCode 不能为空");
  624. }
  625. $returninfo =Db::name("trade_return")->where(["returnCode"=>$returnCode,"is_del"=>0])->find();
  626. if($returninfo==false){
  627. return error_show(1004,"退款申请数据未找到");
  628. }
  629. $trade = Db::name("trade")->where(["tradNo"=>$returninfo['tradNo'],"is_del"=>0])->find();
  630. $returninfo['trade_out']= $trade['trade_out']??"";
  631. $returninfo['trade_in']= $trade['trade_in']??"";
  632. $returninfo['trade_bank']= $trade['trade_bank']??"";
  633. $returninfo['trade_time']= $trade['trade_time']??"";
  634. $returninfo['customerNo']= $trade['customerNo']??"";
  635. $returninfo['companyNo']= $trade['companyNo']??"";
  636. $returninfo['total_fee']= $trade['total_fee']??"";
  637. $returninfo['balance']= $trade['balance']??"";
  638. $returninfo['used_fee']= $trade['used_fee']??"";
  639. $pool =Db::name("trade_pool")->where(["logNo"=>$returninfo['logNo'],"is_del"=>0])->find();
  640. $returninfo['log_total_fee']= $pool['total_fee']??"";
  641. $returninfo['log_apply_id']= $pool['apply_id']??"";
  642. $returninfo['log_apply_name']= $pool['apply_name']??"";
  643. $orderinfo = Db::name("assoc")->alias("a")->leftJoin("qrd_info c","a.orderCode=c.sequenceNo")
  644. ->where(["a.viceCode"=>$returninfo['logNo'],"a.is_del"=>0,"a.status"=>[1,2]])
  645. ->order("a.addtime desc")
  646. ->field("c.*")
  647. ->findOrEmpty();
  648. $returninfo['orderinfo'] = $orderinfo;
  649. return app_show(0,"获取成功",$returninfo);
  650. }
  651. /**
  652. * 显示创建资源表单页.
  653. *
  654. * @return \think\Response
  655. */
  656. public function importTrade()
  657. {
  658. $files = $this->request->file("excel");
  659. $data = upload($files,$files->getOriginalExtension());
  660. if($data['code']!=0){
  661. return error_show(1004,$data['msg']);
  662. }
  663. $trade = $data['data'];
  664. $list = [];
  665. foreach ( $trade as $key => $value) {
  666. if (!isset($value[0]) || $value[0] == '') {
  667. //return ['code' => 1003, "msg" => '交易时间不能为空'];
  668. return error_show( 1003, '第'.($key+1).'行收款方公司编号不能为空!');
  669. }
  670. if (!isset($value[4]) || $value[4] == '') {
  671. // return ['code' => 1003, "msg" => '交易行名不能为空'];
  672. return error_show( 1003, '第'.($key+1).'行收入金额不能为空!');
  673. }
  674. if (!isset($value[8]) || $value[8] == '') {
  675. // return ['code' => 1003, "msg" => '对方账户不能为空'];
  676. return error_show( 1003, '第'.($key+1).'行对方账户不能为空!');
  677. }
  678. if (!isset($value[9]) || $value[9] == '') {
  679. // return ['code' => 1003, "msg" => '对方户名不能为空'];
  680. return error_show( 1003, '第'.($key+1).'行对方户名不能为空!');
  681. }
  682. if ($key == 0) {
  683. if ($value[0] != '收款方公司编号') {
  684. // return ['code' => 1003, "msg" => '模板第一列为必须为交易时间!'];
  685. return error_show( 1003, '模板第一列为必须为收款方公司编号!');
  686. }
  687. if ($value[4] != '收入金额') {
  688. // return ['code' => 1003, "msg" => '模板第二列为必须为收入金额!'];
  689. return error_show( 1003, '模板第五列为必须为收入金额!');
  690. }
  691. if ($value[6] != '交易行名') {
  692. //return ['code' => 1003, "msg" => '模板第五列为必须为交易行名!'];
  693. return error_show( 1003, '模板第七列为必须为交易行名!');
  694. }
  695. if ($value[8] != '对方账号') {
  696. // return ['code' => 1003, "msg" => '模板第七列为必须为对方账号!'];
  697. return error_show( 1003, '模板第九列为必须为对方账号!');
  698. }
  699. if ($value[9] != '对方户名') {
  700. // return ['code' => 1003, "msg" => '模板第八列为必须为对方户名!'];
  701. return error_show( 1003, '模板第十列为必须为对方户名!');
  702. }
  703. continue;
  704. }
  705. $company =Db::name("company_info")->where(["companyNo"=>$value[0]])->findOrEmpty();
  706. if(empty($company)){
  707. return error_show( 1003, "业务公司编号不存在{$value[0]}");
  708. }
  709. $total_fee = $value[4];
  710. $type = 0;
  711. $time = explode(" ", $value[3]);
  712. $list[$key]["tradeTime"] = date("Y-m-d", strtotime($time[0])) . " " . (isset($time[1]) ? $time[1] : "00:00:00");
  713. $list[$key]["trade_fee"] = str_replace(",","",$total_fee);
  714. $list[$key]["trade_bank"] = $value[6];
  715. $list[$key]["trade_account"] = $value[8];
  716. $list[$key]["trade_out"] = $value[9];
  717. $list[$key]["trade_in"] = $value[1]??$company['company_name'];
  718. $list[$key]["trade_type"] = $type;
  719. $list[$key]["trade_used"] = isset($value[10]) ? $value[10] : "";
  720. $list[$key]["trade_remark"] = '';
  721. $list[$key]["companyNo"] = $value[0];
  722. $list[$key]["trade_in_account"] = $value[2]??"";
  723. }
  724. if(empty($list)){
  725. return error_show( 1003, '导入数据不能为空!');
  726. }
  727. Db::startTrans();
  728. try{
  729. $tra=[];
  730. foreach ($list as $value) {
  731. $temp = [];
  732. $temp["tradNo"] = makeStr('S');
  733. $temp['trade_time'] = $value['tradeTime'];
  734. $temp['total_fee'] = $value['trade_fee'];
  735. $temp['trade_bank'] = $value['trade_bank'];
  736. $temp['trade_account'] = $value['trade_account'];
  737. $temp['trade_type'] =$value['trade_type'];
  738. $temp['trade_out'] = $value['trade_out'];
  739. $temp['trade_in'] = $value['trade_in'];
  740. $temp['trade_in_account'] = $value['trade_in_account'];
  741. $temp['companyNo'] = $value['companyNo'];
  742. $temp['trade_used'] = $value['trade_used'];
  743. $temp['trade_remark'] = $value['trade_remark'];
  744. $temp['balance'] =$value['trade_fee'];
  745. $temp['addtime'] = date("Y-m-d H:i:s");
  746. $temp['updatetime'] = date("Y-m-d H:i:s");
  747. $tra[]=$temp;
  748. }
  749. $list = Db::name('trade')->insertAll($tra);
  750. if($list==count($tra)){
  751. Db::commit();
  752. return app_show(0, "资金导入成功");
  753. }else{
  754. Db::rollback();
  755. return app_show(1004, "资金导入失败");
  756. }
  757. }catch (\Exception $e){
  758. Db::rollback();
  759. return app_show(1004, $e->getMessage());
  760. }
  761. }
  762. /**
  763. * @return \think\response\Json|void
  764. */
  765. public function importTradeByArr()
  766. {
  767. $trade =isset($this->post['data']) &&!empty($this->post['data'])? $this->post['data']:[];
  768. if(!is_array($trade) || empty($trade) ) return error_show( 1003,"参数 data 不能为空");
  769. $list = [];
  770. foreach ( $trade as $key => $value) {
  771. if (!isset($value[0]) || $value[0] == '') {
  772. //return ['code' => 1003, "msg" => '交易时间不能为空'];
  773. return error_show( 1003, '第'.($key+1).'行收款方公司编号不能为空!');
  774. }
  775. if (!isset($value[4]) || $value[4] == '') {
  776. // return ['code' => 1003, "msg" => '交易行名不能为空'];
  777. return error_show( 1003, '第'.($key+1).'行收入金额不能为空!');
  778. }
  779. if (!isset($value[8]) || $value[8] == '') {
  780. // return ['code' => 1003, "msg" => '对方账户不能为空'];
  781. return error_show( 1003, '第'.($key+1).'行对方账户不能为空!');
  782. }
  783. if (!isset($value[9]) || $value[9] == '') {
  784. // return ['code' => 1003, "msg" => '对方户名不能为空'];
  785. return error_show( 1003, '第'.($key+1).'行对方户名不能为空!');
  786. }
  787. // if ($key == 0) {
  788. // if ($value[0] != '收款方公司编号') {
  789. // // return ['code' => 1003, "msg" => '模板第一列为必须为交易时间!'];
  790. // return error_show( 1003, '模板第一列为必须为收款方公司编号!');
  791. // }
  792. // if ($value[4] != '收入金额') {
  793. // // return ['code' => 1003, "msg" => '模板第二列为必须为收入金额!'];
  794. // return error_show( 1003, '模板第五列为必须为收入金额!');
  795. // }
  796. //
  797. // if ($value[6] != '交易行名') {
  798. // //return ['code' => 1003, "msg" => '模板第五列为必须为交易行名!'];
  799. // return error_show( 1003, '模板第七列为必须为交易行名!');
  800. // }
  801. // if ($value[8] != '对方账号') {
  802. // // return ['code' => 1003, "msg" => '模板第七列为必须为对方账号!'];
  803. // return error_show( 1003, '模板第九列为必须为对方账号!');
  804. // }
  805. // if ($value[9] != '对方户名') {
  806. // // return ['code' => 1003, "msg" => '模板第八列为必须为对方户名!'];
  807. // return error_show( 1003, '模板第十列为必须为对方户名!');
  808. // }
  809. // continue;
  810. // }
  811. $company =Db::name("company_info")->where(["companyNo"=>$value[0]])->findOrEmpty();
  812. if(empty($company)){
  813. return error_show( 1003, "业务公司编号不存在{$value[0]}");
  814. }
  815. $total_fee = $value[4];
  816. $type = 0;
  817. $time = explode(" ", $value[3]);
  818. $list[$key]["tradeTime"] = date("Y-m-d", strtotime($time[0])) . " " . (isset($time[1]) ? $time[1] : "00:00:00");
  819. $list[$key]["trade_fee"] = str_replace(",","",$total_fee);
  820. $list[$key]["trade_bank"] = $value[6];
  821. $list[$key]["trade_account"] = $value[8];
  822. $list[$key]["trade_out"] = $value[9];
  823. $list[$key]["trade_in"] = $value[1]??$company['company_name'];
  824. $list[$key]["trade_type"] = $type;
  825. $list[$key]["trade_used"] = isset($value[10]) ? $value[10] : "";
  826. $list[$key]["trade_remark"] = '';
  827. $list[$key]["companyNo"] = $value[0];
  828. $list[$key]["trade_in_account"] = $value[2]??"";
  829. }
  830. if(empty($list)){
  831. return error_show( 1003, '导入数据不能为空!');
  832. }
  833. Db::startTrans();
  834. try{
  835. $tra=[];
  836. foreach ($list as $value) {
  837. $temp = [];
  838. $temp["tradNo"] = makeStr('S');
  839. $temp['trade_time'] = $value['tradeTime'];
  840. $temp['total_fee'] = $value['trade_fee'];
  841. $temp['trade_bank'] = $value['trade_bank'];
  842. $temp['trade_account'] = $value['trade_account'];
  843. $temp['trade_type'] =$value['trade_type'];
  844. $temp['trade_out'] = $value['trade_out'];
  845. $temp['trade_in'] = $value['trade_in'];
  846. $temp['trade_in_account'] = $value['trade_in_account'];
  847. $temp['companyNo'] = $value['companyNo'];
  848. $temp['trade_used'] = $value['trade_used'];
  849. $temp['trade_remark'] = $value['trade_remark'];
  850. $temp['balance'] =$value['trade_fee'];
  851. $temp['addtime'] = date("Y-m-d H:i:s");
  852. $temp['updatetime'] = date("Y-m-d H:i:s");
  853. $tra[]=$temp;
  854. }
  855. $list = Db::name('trade')->insertAll($tra);
  856. if($list==count($tra)){
  857. Db::commit();
  858. return app_show(0, "资金导入成功");
  859. }else{
  860. Db::rollback();
  861. return app_show(1004, "资金导入失败");
  862. }
  863. }catch (\Exception $e){
  864. Db::rollback();
  865. return app_show(1004, $e->getMessage());
  866. }
  867. }
  868. }