Trade.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  1. <?php
  2. namespace app\cxinv\controller;
  3. use app\cxinv\model\Assoc;
  4. use app\cxinv\model\ComonOrder;
  5. use app\cxinv\model\QrdInfo;
  6. use app\cxinv\model\TradePool;
  7. use app\cxinv\model\TradeReturn;
  8. use app\user\model\Business;
  9. use think\App;
  10. use think\facade\Validate;
  11. use think\helper\Str;
  12. class Trade extends Base{
  13. public function __construct(App $app) {
  14. parent::__construct($app);
  15. $this->model = new \app\cxinv\model\Trade();
  16. }
  17. public function PoolCreate(){
  18. $params = $this->request->param(["tradNo"=>"","orderArr"=>[]],"post","trim");
  19. $valid =Validate::rule(["tradNo|交易流水编号"=>"require|max:255","orderArr|关联订单信息"=>"require|array"]);
  20. if(!$valid->check($params)) return error($valid->getError());
  21. $trade = $this->model->where(["tradNo"=>$params['tradNo'],"is_del"=>0])->findOrEmpty();
  22. if($trade->isEmpty()) return error("交易流水信息不存在");
  23. $orderValid = Validate::rule([
  24. "sequenceNo|关联订单编号"=>"require|max:255",
  25. "trad_fee|关联订单金额"=>"require|float|min:0.01"]);
  26. $assoc=[];
  27. $log_total = 0;
  28. $logInfo=[];
  29. $qrdList = QrdInfo::where('sequenceNo','in',array_column($params['orderArr'],'sequenceNo'))
  30. ->column("id,sequenceNo,platform_type,wpay_fee,pay_fee,is_comon,pay_status,totalPrice,customerNo","sequenceNo");
  31. foreach ($params['orderArr'] as $key=>$order){
  32. if(!$orderValid->check($order)) return error($orderValid->getError());
  33. if(!isset($qrdList[$order['sequenceNo']])) return error("关联订单信息不存在");
  34. if($qrdList[$order['sequenceNo']]['wpay_fee']<$order['trad_fee']) return error("销售单{$order['sequenceNo']}未付款金额不足核销金额");
  35. if($qrdList[$order['sequenceNo']]['is_comon']==1) return error("{$order['sequenceNo']} 通用订单不可认领资金");
  36. $qrdList[$order['sequenceNo']]['wpay_fee']-=$order['trad_fee'];
  37. $log_total+=$order['trad_fee'];
  38. $temp=[
  39. 'logNo'=>makeNo("TRC",str_pad($key+1,4,'0',STR_PAD_LEFT)),
  40. 'tradNo'=>$params['tradNo'],
  41. 'platform_type'=>$qrdList[$order['sequenceNo']]['platform_type']??0,
  42. 'companyNo'=>$trade['companyNo'],
  43. 'customerNo'=>$qrdList[$order['sequenceNo']]['customerNo'],
  44. 'apply_id'=>$this->uid,
  45. 'apply_name'=>$this->uname,
  46. 'trade_time'=>$trade['trade_time'],
  47. 'total_fee'=>$order['trad_fee'],
  48. 'status'=>1,
  49. ];
  50. $assoc[]=[
  51. "assocNo"=>makeNo("AS",str_pad($key+1,4,"0",STR_PAD_LEFT)),
  52. "apply_id"=>$this->uid,
  53. "apply_name"=>$this->uname,
  54. "type"=>2,
  55. "orderCode"=>$order['sequenceNo'],
  56. "customerNo"=>$qrdList[$order['sequenceNo']]['customerNo'],
  57. "viceCode"=>$temp['logNo'],
  58. "order_total"=>$qrdList[$order['sequenceNo']]['totalPrice'],
  59. "vice_total"=>$order['trad_fee'],
  60. "cancel_fee"=>$order['trad_fee'],
  61. "status"=>1,
  62. ];
  63. $logInfo[]=$temp;
  64. }
  65. if($trade->balance<$log_total) return error("交易流水余额不足");
  66. $this->model->startTrans();
  67. try{
  68. $trade->balance-=$log_total;
  69. $trade->used_fee+=$log_total;
  70. $trade->status= $trade->balance==0?3:2;
  71. $trup=$trade->save();
  72. if(!$trup) throw new \Exception("资金流水更新失败");
  73. $pool = (new \app\cxinv\model\TradePool)->saveAll($logInfo);
  74. if($pool->isEmpty()) throw new \Exception("资金认领记录失败");
  75. $assoc_res = (new \app\cxinv\model\Assoc)->saveAll($assoc);
  76. if($assoc_res->isEmpty()) throw new \Exception("资金关联记录失败");
  77. foreach ($params['orderArr'] as $key=>$qrd){
  78. $qrdInfo = QrdInfo::where(["sequenceNo"=>$qrd['sequenceNo'],"is_del"=>0])->find();
  79. if($qrdInfo->isEmpty()) throw new \Exception("关联订单{$qrd['sequenceNo']}信息不存在");
  80. if($qrdInfo->wpay_fee<$qrd['trad_fee']) throw new \Exception("关联订单{$qrd['sequenceNo']}未付款金额不足核销金额");
  81. $qrdInfo->pay_fee+=$qrd['trad_fee'];
  82. $qrdInfo->wpay_fee-=$qrd['trad_fee'];
  83. $qrdInfo->pay_status= $qrdInfo->apay_fee==0?1:2;
  84. $qrdInfo->status=1;
  85. $sav= $qrdInfo->save();
  86. if(!$sav) throw new \Exception("关联订单更新失败");
  87. }
  88. $this->model->commit();
  89. }catch (\Exception $e){
  90. $this->model->rollback();
  91. return error($e->getMessage());
  92. }
  93. return success("资金认领成功");
  94. }
  95. // 1待审批2审批通过3审批驳回4退款5解除认领
  96. public function PoolStatus(){
  97. $params = $this->request->param(["logNo"=>"","status"=>"","remark"=>""],"post","trim");
  98. $valid =Validate::rule(["logNo|资金认领编号"=>"require|max:255","status|状态"=>"require|in:1,2,3"]);
  99. if(!$valid->check($params)) return error($valid->getError());
  100. $log = TradePool::where(["logNo"=>$params['logNo'],"is_del"=>0])->findOrEmpty();
  101. if($log->isEmpty()) return error("资金认领信息不存在");
  102. if($log->status==$params['status']) return error("当前状态与修改状态一致");
  103. $trade = $this->model->where(["tradNo"=>$log->tradNo,"is_del"=>0])->findOrEmpty();
  104. if($trade->isEmpty()) return error("资金流水信息不存在");
  105. $this->model->startTrans();
  106. try{
  107. $log->status=$params['status'];
  108. $log->remark=$params['remark'];
  109. $save=$log->save();
  110. if(!$save) throw new \Exception("资金认领状态更新失败");
  111. Assoc::CheckTrad($log->logNo,$params['status']);
  112. if($params['status']==3){
  113. if($trade->used_fee<$log->total_fee) throw new \Exception("资金流水已使用金额不足");
  114. $trade->balance+=$log->total_fee;
  115. $trade->used_fee-=$log->total_fee;
  116. $trade->status= $trade->used_fee==0?1:2;
  117. $up=$trade->save();
  118. if(!$up) throw new \Exception("资金流水更新失败");
  119. }
  120. $this->model->commit();
  121. }catch (\Exception $e){
  122. $this->model->rollback();
  123. return error($e->getMessage());
  124. }
  125. return success("操作成功");
  126. }
  127. public function list(){
  128. $params = $this->request->param(["tradNo"=>"","status"=>[],"name"=>"","bank"=>"","start"=>"","end"=>"","companyNo"=>"",
  129. "page"=>1,"size"=>20,'userd_lower'=>"",'used_upper'=>'','total_lower'=>'','total_upper'=>'',"relaComNo"=>""],"post","trim");
  130. $where=[["is_del","=",0]];
  131. if($params['tradNo']!=="")$where[]=["tradNo","like","%".$params['tradNo']."%"];
  132. if($params['status']!=="")$where[]=["status","in",$params['status']];
  133. if($params['name']!=="")$where[]=["trade_out","like","%".$params['name']."%"];
  134. if($params['bank']!=="")$where[]=["trade_bank","like","%".$params['bank']."%"];
  135. if($params['start']!=="") $where[]=["trade_time",">=",startTime($params['start'])];
  136. if($params['end']!=="") $where[]=["trade_time","<=",endTime($params['end'])];
  137. if($params['companyNo']!=="")$where[]=["companyNo","like","%".$params['companyNo']."%"];
  138. if($params['relaComNo']!=="")$where[]=["companyNo","like","%".$params['relaComNo']."%"];
  139. if($params['userd_lower']!=="") $where[]=["used_fee",">=",$params['userd_lower']];
  140. if($params['userd_upper']!=="") $where[]=["used_fee","<=",$params['userd_upper']];
  141. if($params['total_lower']!=="") $where[]=["total_fee",">=",$params['total_lower']];
  142. if($params['total_upper']!=="") $where[]=["total_fee","<=",$params['total_upper']];
  143. $list = $this->model->with(["company"])->where($where)->order("id desc")->paginate(["list_rows"=>$params['size'],"page"=>$params['page']]);
  144. return success("成功",['list'=>$list->items(),'count'=>$list->total()]);
  145. }
  146. public function delete(){
  147. $id = $this->request->post("id","0","int");
  148. if($id<=0) return error("参数错误");
  149. $info = $this->model->where(["id"=>$id,"is_del"=>0])->findOrEmpty();
  150. if($info->isEmpty()) return error("资金流水信息不存在");
  151. if($info->status!=1) return error("当前状态不允许删除");
  152. $this->model->startTrans();
  153. try{
  154. $info->is_del=1;
  155. $save=$info->save();
  156. if(!$save) throw new \Exception("资金流水删除失败");
  157. $this->model->commit();
  158. }catch (\Exception $e){
  159. $this->model->rollback();
  160. return error($e->getMessage());
  161. }
  162. return success("删除成功");
  163. }
  164. public function PoolList(){
  165. $params=$this->request->param(["logNo"=>"","status"=>"","name"=>"","bank"=>"","start"=>"","end"=>"","companyNo"=>"",
  166. "page"=>1,"size"=>20,'apply_id'=>"",'orderCode'=>'','platform_type'=>'',"relaComNo"=>"",'cxCode'=>''],"post","trim");
  167. $where=[["a.is_del","=",0],["b.is_del","=",0]];
  168. if($params['logNo']!=="")$where[]=["a.logNo","like","%".$params['logNo']."%"];
  169. if($params['apply_id']!=="")$where[]=["a.apply_id","like","%".$params['apply_id']."%"];
  170. if($params['orderCode']!=="")$where[]=["d.orderCode","like","%".$params['orderCode']."%"];
  171. if($params['platform_type']!=="")$where[]=["a.platform_type","like","%".$params['platform_type']."%"];
  172. if($params['status']!=="")$where[]=["a.status","=",$params['status']];
  173. if($params['name']!=="")$where[]=["b.trade_out","like","%".$params['name']."%"];
  174. if($params['bank']!=="")$where[]=["b.trade_bank","like","%".$params['bank']."%"];
  175. if($params['start']!=="") $where[]=["a.addtime",">=",startTime($params['start'])];
  176. if($params['end']!=="") $where[]=["a.addtime","<=",endTime($params['end'])];
  177. if($params['companyNo']!=="")$where[]=["a.companyNo","like","%".$params['companyNo']."%"];
  178. if($params['relaComNo']!=="")$where[]=["a.companyNo","like","%".$params['relaComNo']."%"];
  179. if($params['cxCode']!=="")$where[]=["c.cxCode","like","%".$params['cxCode']."%"];
  180. $list = TradePool::alias("a")
  181. ->leftJoin('trade b','a.tradNo=b.tradNo')
  182. ->leftJoin('assoc d','a.logNo=d.viceCode AND d.type = 2')
  183. ->leftJoin('qrd_info c','d.orderCode=c.sequenceNo')
  184. ->where($where)
  185. ->field('a.*,b.trade_bank,b.trade_account,b.trade_out,b.total_fee as btotal_fee,b.used_fee,b.balance,c.customerName,c.customerNo,
  186. d.orderCode,c.qrdSource,c.cxCode,c.goodNo,c.goodName,c.qrdType,c.ownerName,c.ownerid,c.department,c.poCode,c.platName,c.totalPrice,d.cancel_fee')
  187. ->order("a.id desc")->paginate(["list_rows"=>$params['size'],"page"=>$params['page']]);
  188. return success("成功",['list'=>$list->items(),'count'=>$list->total()]);
  189. }
  190. public function PoolQuery(){
  191. $params=$this->request->param(["logNo"=>"","status"=>"","companyNo"=>"",'apply_id'=>"",'platform_type'=>'',"relaComNo"=>"",],"post","trim");
  192. $where=[["is_del","=",0]];
  193. if($params['logNo']!=="")$where[]=["logNo","like","%".$params['logNo']."%"];
  194. if($params['apply_id']!=="")$where[]=["apply_id","like","%".$params['apply_id']."%"];
  195. if($params['status']!=="")$where[]=["status","=",$params['status']];
  196. if($params['companyNo']!=="")$where[]=["companyNo","like","%".$params['companyNo']."%"];
  197. if($params['relaComNo']!=="")$where[]=["companyNo","like","%".$params['relaComNo']."%"];
  198. if($params['platform_type']!=="") $where[]=["platform_type","=",$params['platform_type']];
  199. $list = TradePool::where($where)->order("id desc")->select();
  200. return success("成功",$list);
  201. }
  202. public function PoolInfo(){
  203. $logNo = $this->request->post("logNo","","trim");
  204. if($logNo=="") return error("参数错误");
  205. $info = TradePool::with(["trade"=>"company"])->where(["logNo"=>$logNo,"is_del"=>0])->findOrEmpty();
  206. if($info->isEmpty()) return error("资金认领信息不存在");
  207. $info['orderinfo']= Assoc::alias("a")
  208. ->leftJoin('qrd_info b','a.orderCode=b.sequenceNo')
  209. ->where(["a.viceCode"=>$logNo,"a.type"=>2,'a.is_del'=>0,'a.status'=>[1,2,3]])
  210. ->field('b.*,a.cancel_fee')
  211. ->select();
  212. return success("成功",$info);
  213. }
  214. public function PoolBatchCheck(){
  215. $params=$this->request->param(["idArr"=>[],"remark"=>"","status"=>""],"post","trim");
  216. $valid = Validate::rule([
  217. "idArr|ID"=>"require|array",
  218. "remark|备注"=>"max:255",
  219. "status|状态"=>"require|in:2,3"
  220. ]);
  221. if(!$valid->check($params)) return error($valid->getError());
  222. $logInfo = TradePool::where(["id"=>$params['idArr'],"status"=>1,"is_del"=>0])->select();
  223. if($logInfo->isEmpty()) return error("资金认领信息不存在");
  224. $this->model->startTrans();
  225. try{
  226. $logInfo->each(function($item) use($params){
  227. $item->status = $params['status'];
  228. $item->remark = $params['remark'];
  229. $save=$item->save();
  230. if(!$save) throw new \Exception("资金认领信息更新失败");
  231. Assoc::CheckTrad($item->logNo,$params['status']);
  232. if($params['status']==3){
  233. $trade = Trade::where(["tradNo"=>$item->tradNo,"is_del"=>0])->findOrEmpty();
  234. if($trade->used_fee<$item->total_fee) throw new \Exception('资金流水已使用金额不足');
  235. $trade->balance+=$item->total_fee;
  236. $trade->used_fee-=$item->total_fee;
  237. $trade->status= $trade->used_fee==0?1:2;
  238. $up=$trade->save();
  239. if(!$up) throw new \Exception('资金流水更新失败');
  240. }
  241. });
  242. $this->model->commit();
  243. }catch (\Exception $e){
  244. $this->model->rollback();
  245. return error($e->getMessage());
  246. }
  247. return success("操作成功");
  248. }
  249. //认领资金退回
  250. public function Back(){
  251. $params=$this->request->param(["logNo"=>"","remark"=>"","type"=>"","return_reason"=>""],"post","trim");
  252. $valid = Validate::rule([
  253. "logNo|认领资金编号"=>"require|max:255",
  254. "type|类型"=>"require|in:1,2",
  255. "return_reason|退回原因"=>"require|max:255",
  256. "remark|备注"=>"max:255"
  257. ]);
  258. if(!$valid->check($params)) return error($valid->getError());
  259. $info = TradePool::where(["logNo"=>$params['logNo'],"is_del"=>0])->findOrEmpty();
  260. if($info->isEmpty()) return error("资金认领信息不存在");
  261. if($info->status!=2) return error("资金认领当前状态不允许退回");
  262. $isT = TradeReturn::where(["logNo"=>$params['logNo'],"status"=>[0,1]])->findOrEmpty();
  263. if(!$isT->isEmpty()) return error("当前资金认领已申请退款,请勿重复申请");
  264. $data = [
  265. 'returnCode'=>makeNo('RTA'),
  266. 'logNo'=>$params['logNo'],
  267. 'companyNo'=>$info->companyNo,
  268. 'tradNo'=>$info->tradNo,
  269. 'return_img'=>'',
  270. 'type'=>$params['type'],
  271. 'apply_id'=>$this->uid,
  272. 'apply_name'=>$this->uname,
  273. 'return_reason'=>$params['return_reason'],
  274. 'remark'=>$params['remark'],
  275. "status"=>0
  276. ];
  277. $res = TradeReturn::create($data);
  278. if($res->isEmpty()) return error("操作失败");
  279. return success("操作成功");
  280. }
  281. public function BackList(){
  282. $params=$this->request->param(["logNo"=>"","tradNo"=>"","status"=>"","companyNo"=>"",'apply_id'=>"","type"=>"",
  283. "returnCode"=>"","orderCode"=>"","relaComNo"=>"", "page"=>1,"size"=>20],"post","trim");
  284. $where=[["a.is_del","=",0]];
  285. if($params['logNo']!=="")$where[]=["a.logNo","like","%".$params['logNo']."%"];
  286. if($params['apply_id']!=="")$where[]=["a.apply_id","like","%".$params['apply_id']."%"];
  287. if($params['tradNo']!=="")$where[]=["a.tradNo","like","%".$params['tradNo']."%"];
  288. if($params['status']!=="")$where[]=["a.status","=",$params['status']];
  289. if($params['companyNo']!=="")$where[]=["a.companyNo","like","%".$params['companyNo']."%"];
  290. if($params['relaComNo']!=="")$where[]=["a.companyNo","like","%".$params['relaComNo']."%"];
  291. if($params['type']!=="")$where[]=["a.type","=",$params['type']];
  292. if($params['returnCode']!=="")$where[]=["a.returnCode","like","%".$params['returnCode']."%"];
  293. if($params['orderCode']!=="")$where[]=["b.orderCode","like","%".$params['orderCode']."%"];
  294. $list = TradeReturn::alias("a")
  295. ->leftJoin('assoc b','a.logNo=b.viceCode and b.type=2')
  296. ->leftJoin('qrd_info c','c.sequenceNo=b.orderCode and c.is_del=0')
  297. ->field('a.*,b.orderCode,c.qrdSource,goodNo,goodName,qrdType,c.ownerName,c.ownerid,c.department,c.poCode,c.platName,b.cancel_fee,c.customerName,c.customerNo')
  298. ->where($where)
  299. ->order("a.id desc")->paginate(["list_rows"=>$params['size'],"page"=>$params['page']]);
  300. return success("成功",['list'=>$list->items(),'count'=>$list->total()]);
  301. }
  302. public function BackInfo(){
  303. $returnCode = $this->request->post("returnCode","","trim");
  304. if($returnCode=="") return error("参数错误");
  305. $info = TradeReturn::with(["trade"=>"company","pool"])->where(["returnCode"=>$returnCode,"is_del"=>0])->findOrEmpty();
  306. if($info->isEmpty()) return error("退款申请数据未找到");
  307. $info['log_total_fee']= $info->pool->total_fee;
  308. $info['log_apply_id']= $info->pool->apply_id;
  309. $info['log_apply_name']= $info->pool->apply_name;
  310. $info['orderinfo']= Assoc::alias("a")
  311. ->leftJoin('qrd_info b','a.orderCode=b.sequenceNo')
  312. ->where(["a.viceCode"=>$info['logNo'],"a.type"=>2,'a.is_del'=>0,'a.status'=>[1,2,3]])
  313. ->field('b.*,a.cancel_fee')
  314. ->findOrEmpty();
  315. return success("成功",$info);
  316. }
  317. public function BackStatus(){
  318. $params=$this->request->param(["returnCode"=>"","status"=>"","remark"=>"","return_img"=>""],"post","trim");
  319. $valid = Validate::rule([
  320. "returnCode|退款申请编号"=>"require|max:255",
  321. "status|状态"=>"require|in:1,2",
  322. "remark|备注"=>"max:255"
  323. ]);
  324. if(!$valid->check($params)) return error($valid->getError());
  325. $info = TradeReturn::where(["returnCode"=>$params['returnCode'],"is_del"=>0])->findOrEmpty();
  326. if($info->isEmpty()) return error("退款申请数据未找到");
  327. $loginfo= TradePool::where(["logNo"=>$info['logNo'],"is_del"=>0])->findOrEmpty();
  328. if($loginfo->isEmpty()) return error("资金认领数据未找到");
  329. if($loginfo->status!=2) return error("认领资金信息审核未通过");
  330. $this->model->startTrans();
  331. try{
  332. $info->status=$params['status'];
  333. $info->remark=$params['remark'];
  334. $info->return_img=$params['return_img'];
  335. $save=$info->save();
  336. if ($save===false) throw new \Exception("操作失败");
  337. if($params['status']==1){
  338. $loginfo->status= $info->type==1?5:4;
  339. $logsave=$loginfo->save();
  340. if ($logsave===false) throw new \Exception("操作失败");
  341. Assoc::CheckTrad($info['logNo'],$loginfo->status);
  342. $trade = $this->model->where("tradNo",$loginfo->tradNo)->findOrEmpty();
  343. if($trade->isEmpty()) throw new \Exception("资金流水信息未找到");
  344. $trade->used_fee-=$loginfo->total_fee;
  345. $trade->balance+=$loginfo->total_fee;
  346. $trade->status= $trade->used_fee==0?1:2;
  347. $tradesave=$trade->save();
  348. if ($tradesave===false) throw new \Exception("资金流水信息更新失败");
  349. }
  350. $this->model->commit();
  351. }catch (\Exception $e){
  352. $this->model->rollback();
  353. return error($e->getMessage());
  354. }
  355. return success("退款申请审核成功");
  356. }
  357. /**
  358. * 导入交易流水
  359. * data:
  360. * companyNo
  361. * trade_in_account
  362. * tradeTime
  363. * trade_fee
  364. * trade_bank
  365. * trade_account
  366. * trade_out
  367. */
  368. public function importTradeByArr(){
  369. $data= $this->request->post("data",[],"trim");
  370. if(!is_array($data)||empty($data)) return error("参数错误");
  371. $valid = Validate::rule([
  372. "companyNo|收款方公司编号"=>"require|max:255",
  373. "trade_in_account|收款账户"=>"require|max:255",
  374. "tradeTime|交易时间"=>"require|date",
  375. "trade_fee|交易金额"=>"require|float",
  376. "trade_bank|交易银行"=>"require|max:255",
  377. "trade_account|对方账号"=>"require|max:255",
  378. "trade_out|交易方名称"=>"require|max:255",
  379. ]);
  380. $companyArr= Business::whereIn("companyNo",array_column($data,'companyNo'))->column("company","companyNo");
  381. $list=[];
  382. foreach ($data as $key=>$value){
  383. if(!$valid->check($value)) return error('第'.($key+1).'行'.$valid->getError());
  384. if(!isset($companyArr[$value['companyNo']])) return error('第'.($key+1).'行收款方业务公司不存在');
  385. $radStr= Str::random(3,3);
  386. $list[]=[
  387. "tradNo"=>makeNo("S".$radStr,str_pad($key+1,4,'0',STR_PAD_LEFT)),
  388. "companyNo"=>$value['companyNo'],
  389. "trade_in"=>$value['trade_in']??$companyArr[$value['companyNo']],
  390. "trade_out"=>$value['trade_out'],
  391. "trade_type"=>0,
  392. "trade_fee"=>str_replace(',','',$value['trade_fee']),
  393. "balance"=>str_replace(',','',$value['trade_fee']),
  394. "trade_remark"=>$value['trade_remark'],
  395. "tradTime"=>startTime($value['tradTime']),
  396. "trad_in_account"=>$value['trad_in_account']??"",
  397. "trade_used"=>$value['trade_used']??"",
  398. "trad_bank"=>$value['trad_bank'],
  399. "trad_account"=>$value['trad_account'],
  400. "poCode"=>$value['poCode'],
  401. "NdCode"=>$value['NdCode'],
  402. "customerName"=>$value['customerName']
  403. ];
  404. }
  405. if(empty($list)) return error("导入数据不能为空");
  406. $num=$this->model->saveAll($list);
  407. if($num->isEmpty()) return error("资金流水导入失败");
  408. return success("资金导入成功");
  409. }
  410. public function importTradeByBatchOrderCode(){
  411. $list= $this->request->post("list",[],"trim");
  412. if(!is_array($list)||empty($list)) return error("参数错误");
  413. $val_item = Validate::rule([
  414. 'companyNo|卖出方公司编号' => 'require|max:255',
  415. 'tradNo|资金编号' => 'require|max:255',
  416. 'orderCode|订单编号' => 'require|max:255',
  417. 'trad_fee|认领资金' => 'require|float|gt:0|max:999999999.99',
  418. ]);
  419. $companyArr= Business::whereIn('companyNo',array_column($list,'companyNo'))->column('company','companyNo');
  420. if(empty($companyArr)) return error("收款方业务公司不存在");
  421. $orderArr= QrdInfo::whereIn('sequenceNo',array_column($list,'orderCode'))->where(["is_del"=>0])->column('id,customerNo,companyNo,status,pay_fee,apay_fee,wpay_fee,pay_status,totalPrice,platform_type,cxCode',"sequenceNo");
  422. if(empty($orderArr)) return error("订单信息不存在");
  423. $tradeArr=$this->model->whereIn("tradNo",array_column($list,'tradNo'))->where(['is_del'=>0,"status"=>[1,2]])->column("id,balance,used_fee,companyNo,trade_time","tradNo");
  424. if(empty($tradeArr)) return error("资金流水信息不存在");
  425. $Assoc=[];
  426. $trade_log=[];
  427. foreach ($list as $key=>$value){
  428. if(!$val_item->check($value)) return error('第'.($key+1).'行'.$val_item->getError());
  429. if(!isset($companyArr[$value['companyNo']])) return error($value['companyNo'].'卖出方公司不存在');
  430. if(!isset($orderArr[$value['orderCode']])) return error($value['orderCode'].'订单信息不存在');
  431. if($orderArr[$value['orderCode']]['companyNo']!=$value['companyNo']) return error($value['orderCode'].'卖出方公司与订单业务公司不一致');
  432. if($orderArr[$value['orderCode']]['wpay_fee']< $value['trad_fee']) return error($value['orderCode'].'销售单未付款金额不足核销金额');
  433. $orderArr[$value['orderCode']]['wpay_fee']-=$value['trad_fee'];
  434. if(!isset($tradeArr[$value['tradNo']])) return error($value['tradNo'].'资金流水信息不存在');
  435. if($tradeArr[$value['tradNo']]['balance']< $value['trad_fee']) return error($value['tradNo'].'资金流水余额不足核销金额');
  436. $tradeArr[$value['tradNo']]['balance']-=$value['trad_fee'];
  437. if($tradeArr[$value['tradNo']]['companyNo']!=$value['companyNo']) return error($value['tradNo'].'资金流水业务公司与卖出方公司不一致');
  438. $temp=['logNo' => makeNo('TRC',str_pad($key+1,4,'0',STR_PAD_LEFT)),
  439. 'tradNo' => $value['tradNo'],
  440. 'platform_type' => $orderArr[$value['orderCode']]['platform_type'],
  441. 'companyNo' => $value['companyNo'],
  442. 'customerNo' => $orderArr[$value['orderCode']]['customerNo'],
  443. 'apply_id' => $this->uid,
  444. 'apply_name' => $this->uname,
  445. 'trade_time' => $tradeArr[$value['tradNo']]['trade_time'],
  446. 'total_fee' => $value['trad_fee'],
  447. 'status' => 2,//2审核通过
  448. ];
  449. $Assoc[]=[
  450. 'assocNo' =>makeNo("AS",str_pad($key+1,4,'0',STR_PAD_LEFT)),
  451. 'apply_id' => $this->uid,
  452. 'apply_name' => $this->uname,
  453. 'type' => 2,
  454. 'orderCode' => $value['orderCode'],
  455. 'customerNo' => $orderArr[$value['orderCode']]['customerNo'],
  456. 'viceCode' => $temp['logNo'],
  457. 'order_total' =>$orderArr[$value['orderCode']]['totalPrice'],
  458. 'vice_total' => $value['trad_fee'],
  459. 'cancel_fee' => $value['trad_fee'],
  460. 'status' => 2,//认领通过
  461. ];
  462. $trade_log[]=$temp;
  463. }
  464. $this->model->startTrans();
  465. try{
  466. foreach ($list as $key=>$value){
  467. $trade = $this->model->where('is_del', 0)
  468. ->whereIn('status', [1, 2])
  469. ->where('tradNo', $value['tradNo'])
  470. ->field('id,balance,used_fee,companyNo,trade_time')
  471. ->findOrEmpty();
  472. if($trade->isEmpty()) throw new \Exception($value['tradNo'].'资金流水信息不存在');
  473. if($trade->balance< $value['trad_fee']) throw new \Exception($value['tradNo'].'资金流水余额不足核销金额');
  474. $trade->balance-=$value['trad_fee'];
  475. $trade->used_fee+=$value['trad_fee'];
  476. $trade->status=$trade->balance==0?3:2;
  477. $tradSave=$trade->save();
  478. if(!$tradSave) throw new \Exception($value['tradNo'].'资金流水更新失败');
  479. $order = QrdInfo::where('is_del', 0)
  480. ->where('sequenceNo', $value['orderCode'])
  481. ->field('id,customerNo,companyNo,status,pay_fee,apay_fee,wpay_fee,pay_status,totalPrice,platform_type,cxCode,is_comon')
  482. ->findOrEmpty();
  483. if($order->isEmpty()) throw new \Exception($value['orderCode'].'订单信息不存在');
  484. if($order->wpay_fee< $value['trad_fee']) throw new \Exception($value['orderCode'].'销售单未付款金额不足核销金额');
  485. $order->wpay_fee-=$value['trad_fee'];
  486. $order->apay_fee+=$value['trad_fee'];
  487. $order->pay_status=$order->wpay_fee==0 && $order->pay_fee==0?3:2;
  488. $order->status=1;
  489. $ordersave=$order->save();
  490. if(!$ordersave) throw new \Exception($value['orderCode'].'订单信息更新失败');
  491. if($order->pay_status==3 && $order->is_comon=0 && $order->cxCode!=''){
  492. ComonOrder::where(['cxCode'=>$order->cxCode,'status'=>-1])->save(['status'=>0]);
  493. }
  494. }
  495. if(!empty($Assoc)) {
  496. $AssocIn= (new Assoc())->saveAll($Assoc);
  497. if($AssocIn->isEmpty()) throw new \Exception('关联信息写入失败');
  498. }
  499. if(!empty($trade_log)){
  500. $trade_logIn= (new TradePool())->saveAll($trade_log);
  501. if($trade_logIn->isEmpty()) throw new \Exception('资金流水写入失败');
  502. }
  503. $this->model->commit();
  504. }catch (\exception $e){
  505. $this->model->rollback();
  506. return error($e->getMessage());
  507. }
  508. return success("数据导入成功");
  509. }
  510. }