Trade.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  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"=>"",'poCode'=>'',
  129. "page"=>1,"size"=>20,'used_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(!empty($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['used_lower']!=="") $where[]=["used_fee",">=",$params['used_lower']];
  140. if($params['used_upper']!=="") $where[]=["used_fee","<=",$params['used_upper']];
  141. if($params['total_lower']!=="") $where[]=["total_fee",">=",$params['total_lower']];
  142. if($params['total_upper']!=="") $where[]=["total_fee","<=",$params['total_upper']];
  143. if($params['poCode']!=="") $where[]=["poCode","like","%".$params['poCode']."%"];
  144. $list = $this->model->with(["company"])->where($where)->order("id desc")->paginate(["list_rows"=>$params['size'],"page"=>$params['page']]);
  145. return success("成功",['list'=>$list->items(),'count'=>$list->total()]);
  146. }
  147. public function delete(){
  148. $id = $this->request->post("id","0","int");
  149. if($id<=0) return error("参数错误");
  150. $info = $this->model->where(["id"=>$id,"is_del"=>0])->findOrEmpty();
  151. if($info->isEmpty()) return error("资金流水信息不存在");
  152. if($info->status!=1) return error("当前状态不允许删除");
  153. $this->model->startTrans();
  154. try{
  155. $info->is_del=1;
  156. $save=$info->save();
  157. if(!$save) throw new \Exception("资金流水删除失败");
  158. $this->model->commit();
  159. }catch (\Exception $e){
  160. $this->model->rollback();
  161. return error($e->getMessage());
  162. }
  163. return success("删除成功");
  164. }
  165. public function PoolList(){
  166. $params=$this->request->param(["logNo"=>"","status"=>"","name"=>"","bank"=>"","start"=>"","end"=>"","companyNo"=>"",
  167. "page"=>1,"size"=>20,'apply_id'=>"",'orderCode'=>'','platform_type'=>'',"relaComNo"=>"",'cxCode'=>''],"post","trim");
  168. $where=[["a.is_del","=",0],["b.is_del","=",0]];
  169. if($params['logNo']!=="")$where[]=["a.logNo","like","%".$params['logNo']."%"];
  170. if($params['apply_id']!=="")$where[]=["a.apply_id","like","%".$params['apply_id']."%"];
  171. if($params['orderCode']!=="")$where[]=["d.orderCode","like","%".$params['orderCode']."%"];
  172. if($params['platform_type']!=="")$where[]=["a.platform_type","like","%".$params['platform_type']."%"];
  173. if($params['status']!=="")$where[]=["a.status","=",$params['status']];
  174. if($params['name']!=="")$where[]=["b.trade_out","like","%".$params['name']."%"];
  175. if($params['bank']!=="")$where[]=["b.trade_bank","like","%".$params['bank']."%"];
  176. if($params['start']!=="") $where[]=["a.addtime",">=",startTime($params['start'])];
  177. if($params['end']!=="") $where[]=["a.addtime","<=",endTime($params['end'])];
  178. if($params['companyNo']!=="")$where[]=["a.companyNo","like","%".$params['companyNo']."%"];
  179. if($params['relaComNo']!=="")$where[]=["a.companyNo","like","%".$params['relaComNo']."%"];
  180. if($params['cxCode']!=="")$where[]=["c.cxCode","like","%".$params['cxCode']."%"];
  181. $list = TradePool::alias("a")
  182. ->leftJoin('trade b','a.tradNo=b.tradNo')
  183. ->leftJoin('assoc d','a.logNo=d.viceCode AND d.type = 2')
  184. ->leftJoin('qrd_info c','d.orderCode=c.sequenceNo')
  185. ->where($where)
  186. ->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,
  187. 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')
  188. ->order("a.id desc")->paginate(["list_rows"=>$params['size'],"page"=>$params['page']]);
  189. return success("成功",['list'=>$list->items(),'count'=>$list->total()]);
  190. }
  191. public function PoolQuery(){
  192. $params=$this->request->param(["logNo"=>"","tradNo"=>"","status"=>"","companyNo"=>"",'apply_id'=>"",'platform_type'=>'',"relaComNo"=>"",],"post","trim");
  193. $where=[["is_del","=",0]];
  194. if($params['logNo']!=="")$where[]=["logNo","like","%".$params['logNo']."%"];
  195. if($params['tradNo']!=="")$where[]=["tradNo","like","%".$params['tradNo']."%"];
  196. if($params['apply_id']!=="")$where[]=["apply_id","like","%".$params['apply_id']."%"];
  197. if($params['status']!=="")$where[]=["status","=",$params['status']];
  198. if($params['companyNo']!=="")$where[]=["companyNo","like","%".$params['companyNo']."%"];
  199. if($params['relaComNo']!=="")$where[]=["companyNo","like","%".$params['relaComNo']."%"];
  200. if($params['platform_type']!=="") $where[]=["platform_type","=",$params['platform_type']];
  201. $list = TradePool::where($where)->order("id desc")->select();
  202. return success("成功",$list);
  203. }
  204. public function PoolInfo(){
  205. $logNo = $this->request->post("logNo","","trim");
  206. if($logNo=="") return error("参数错误");
  207. $info = TradePool::with(["trade"=>"company"])->where(["logNo"=>$logNo,"is_del"=>0])->findOrEmpty();
  208. if($info->isEmpty()) return error("资金认领信息不存在");
  209. $info['orderinfo']= Assoc::alias("a")
  210. ->leftJoin('qrd_info b','a.orderCode=b.sequenceNo')
  211. ->where(["a.viceCode"=>$logNo,"a.type"=>2,'a.is_del'=>0,'a.status'=>[1,2,3]])
  212. ->field('b.*,a.cancel_fee')
  213. ->select();
  214. return success("成功",$info);
  215. }
  216. public function PoolBatchCheck(){
  217. $params=$this->request->param(["idArr"=>[],"remark"=>"","status"=>""],"post","trim");
  218. $valid = Validate::rule([
  219. "idArr|ID"=>"require|array",
  220. "remark|备注"=>"max:255",
  221. "status|状态"=>"require|in:2,3"
  222. ]);
  223. if(!$valid->check($params)) return error($valid->getError());
  224. $logInfo = TradePool::where(["id"=>$params['idArr'],"status"=>1,"is_del"=>0])->select();
  225. if($logInfo->isEmpty()) return error("资金认领信息不存在");
  226. $this->model->startTrans();
  227. try{
  228. $logInfo->each(function($item) use($params){
  229. $item->status = $params['status'];
  230. $item->remark = $params['remark'];
  231. $save=$item->save();
  232. if(!$save) throw new \Exception("资金认领信息更新失败");
  233. Assoc::CheckTrad($item->logNo,$params['status']);
  234. if($params['status']==3){
  235. $trade = $this->model->where(["tradNo"=>$item->tradNo,"is_del"=>0])->findOrEmpty();
  236. if($trade->used_fee<$item->total_fee) throw new \Exception('资金流水已使用金额不足');
  237. $trade->balance+=$item->total_fee;
  238. $trade->used_fee-=$item->total_fee;
  239. $trade->status= $trade->used_fee==0?1:2;
  240. $up=$trade->save();
  241. if(!$up) throw new \Exception('资金流水更新失败');
  242. }
  243. });
  244. $this->model->commit();
  245. }catch (\Exception $e){
  246. $this->model->rollback();
  247. return error($e->getMessage());
  248. }
  249. return success("操作成功");
  250. }
  251. //认领资金退回
  252. public function Back(){
  253. $params=$this->request->param(["logNo"=>"","remark"=>"","type"=>"","return_reason"=>""],"post","trim");
  254. $valid = Validate::rule([
  255. "logNo|认领资金编号"=>"require|max:255",
  256. "type|类型"=>"require|in:1,2",
  257. "return_reason|退回原因"=>"require|max:255",
  258. "remark|备注"=>"max:255"
  259. ]);
  260. if(!$valid->check($params)) return error($valid->getError());
  261. $info = TradePool::where(["logNo"=>$params['logNo'],"is_del"=>0])->findOrEmpty();
  262. if($info->isEmpty()) return error("资金认领信息不存在");
  263. if($info->status!=2) return error("资金认领当前状态不允许退回");
  264. $isT = TradeReturn::where(["logNo"=>$params['logNo'],"status"=>[0,1]])->findOrEmpty();
  265. if(!$isT->isEmpty()) return error("当前资金认领已申请退款,请勿重复申请");
  266. $data = [
  267. 'returnCode'=>makeNo('RTA'),
  268. 'logNo'=>$params['logNo'],
  269. 'companyNo'=>$info->companyNo,
  270. 'tradNo'=>$info->tradNo,
  271. 'return_img'=>'',
  272. 'type'=>$params['type'],
  273. 'apply_id'=>$this->uid,
  274. 'apply_name'=>$this->uname,
  275. 'return_reason'=>$params['return_reason'],
  276. 'remark'=>$params['remark'],
  277. "status"=>0
  278. ];
  279. $res = TradeReturn::create($data);
  280. if($res->isEmpty()) return error("操作失败");
  281. return success("操作成功");
  282. }
  283. public function BackList(){
  284. $params=$this->request->param(["logNo"=>"","tradNo"=>"","status"=>"","companyNo"=>"",'apply_id'=>"","type"=>"",
  285. "returnCode"=>"","orderCode"=>"","relaComNo"=>"", "page"=>1,"size"=>20],"post","trim");
  286. $where=[["a.is_del","=",0]];
  287. if($params['logNo']!=="")$where[]=["a.logNo","like","%".$params['logNo']."%"];
  288. if($params['apply_id']!=="")$where[]=["a.apply_id","like","%".$params['apply_id']."%"];
  289. if($params['tradNo']!=="")$where[]=["a.tradNo","like","%".$params['tradNo']."%"];
  290. if($params['status']!=="")$where[]=["a.status","=",$params['status']];
  291. if($params['companyNo']!=="")$where[]=["a.companyNo","like","%".$params['companyNo']."%"];
  292. if($params['relaComNo']!=="")$where[]=["a.companyNo","like","%".$params['relaComNo']."%"];
  293. if($params['type']!=="")$where[]=["a.type","=",$params['type']];
  294. if($params['returnCode']!=="")$where[]=["a.returnCode","like","%".$params['returnCode']."%"];
  295. if($params['orderCode']!=="")$where[]=["b.orderCode","like","%".$params['orderCode']."%"];
  296. $list = TradeReturn::alias("a")
  297. ->leftJoin('assoc b','a.logNo=b.viceCode and b.type=2')
  298. ->leftJoin('qrd_info c','c.sequenceNo=b.orderCode and c.is_del=0')
  299. ->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')
  300. ->where($where)
  301. ->order("a.id desc")->paginate(["list_rows"=>$params['size'],"page"=>$params['page']]);
  302. return success("成功",['list'=>$list->items(),'count'=>$list->total()]);
  303. }
  304. public function BackInfo(){
  305. $returnCode = $this->request->post("returnCode","","trim");
  306. if($returnCode=="") return error("参数错误");
  307. $info = TradeReturn::with(["trade"=>"company","pool"])->where(["returnCode"=>$returnCode,"is_del"=>0])->findOrEmpty();
  308. if($info->isEmpty()) return error("退款申请数据未找到");
  309. $info['log_total_fee']= $info->pool->total_fee;
  310. $info['log_apply_id']= $info->pool->apply_id;
  311. $info['log_apply_name']= $info->pool->apply_name;
  312. $info['orderinfo']= Assoc::alias("a")
  313. ->leftJoin('qrd_info b','a.orderCode=b.sequenceNo')
  314. ->where(["a.viceCode"=>$info['logNo'],"a.type"=>2,'a.is_del'=>0,'a.status'=>[1,2,3]])
  315. ->field('b.*,a.cancel_fee')
  316. ->findOrEmpty();
  317. return success("成功",$info);
  318. }
  319. public function BackStatus(){
  320. $params=$this->request->param(["returnCode"=>"","status"=>"","remark"=>"","return_img"=>""],"post","trim");
  321. $valid = Validate::rule([
  322. "returnCode|退款申请编号"=>"require|max:255",
  323. "status|状态"=>"require|in:1,2",
  324. "remark|备注"=>"max:255"
  325. ]);
  326. if(!$valid->check($params)) return error($valid->getError());
  327. $info = TradeReturn::where(["returnCode"=>$params['returnCode'],"is_del"=>0])->findOrEmpty();
  328. if($info->isEmpty()) return error("退款申请数据未找到");
  329. $loginfo= TradePool::where(["logNo"=>$info['logNo'],"is_del"=>0])->findOrEmpty();
  330. if($loginfo->isEmpty()) return error("资金认领数据未找到");
  331. if($loginfo->status!=2) return error("认领资金信息审核未通过");
  332. $this->model->startTrans();
  333. try{
  334. $info->status=$params['status'];
  335. $info->remark=$params['remark'];
  336. $info->return_img=$params['return_img'];
  337. $save=$info->save();
  338. if ($save===false) throw new \Exception("操作失败");
  339. if($params['status']==1){
  340. $loginfo->status= $info->type==1?5:4;
  341. $logsave=$loginfo->save();
  342. if ($logsave===false) throw new \Exception("操作失败");
  343. Assoc::CheckTrad($info['logNo'],$loginfo->status);
  344. $trade = $this->model->where("tradNo",$loginfo->tradNo)->findOrEmpty();
  345. if($trade->isEmpty()) throw new \Exception("资金流水信息未找到");
  346. $trade->used_fee-=$loginfo->total_fee;
  347. $trade->balance+=$loginfo->total_fee;
  348. $trade->status= $trade->used_fee==0?1:2;
  349. $tradesave=$trade->save();
  350. if ($tradesave===false) throw new \Exception("资金流水信息更新失败");
  351. }
  352. $this->model->commit();
  353. }catch (\Exception $e){
  354. $this->model->rollback();
  355. return error($e->getMessage());
  356. }
  357. return success("退款申请审核成功");
  358. }
  359. /**
  360. * 导入交易流水
  361. * data:
  362. * companyNo
  363. * trade_in_account
  364. * tradeTime
  365. * trade_fee
  366. * trade_bank
  367. * trade_account
  368. * trade_out
  369. */
  370. public function importTradeByArr(){
  371. $data= $this->request->post("data",[],"trim");
  372. if(!is_array($data)||empty($data)) return error("参数错误");
  373. $valid = Validate::rule([
  374. "companyNo|收款方公司编号"=>"require|max:255",
  375. "trade_in_account|收款账户"=>"require|max:255",
  376. "tradeTime|交易时间"=>"require|date",
  377. "trade_fee|交易金额"=>"require|float",
  378. "trade_bank|交易银行"=>"require|max:255",
  379. "trade_account|对方账号"=>"require|max:255",
  380. "trade_out|交易方名称"=>"require|max:255",
  381. ]);
  382. $companyArr= Business::whereIn("companyNo",array_column($data,'companyNo'))->column("company","companyNo");
  383. $list=[];
  384. foreach ($data as $key=>$value){
  385. if(!$valid->check($value)) return error('第'.($key+1).'行'.$valid->getError());
  386. if(!isset($companyArr[$value['companyNo']])) return error('第'.($key+1).'行收款方业务公司不存在');
  387. $radStr= Str::random(3,3);
  388. $list[]=[
  389. "tradNo"=>makeNo("S".$radStr,str_pad($key+1,4,'0',STR_PAD_LEFT)),
  390. "companyNo"=>$value['companyNo'],
  391. "trade_in"=>$value['trade_in']??$companyArr[$value['companyNo']],
  392. "trade_out"=>$value['trade_out'],
  393. "trade_type"=>0,
  394. "total_fee"=>str_replace(',','',$value['trade_fee']),
  395. "balance"=>str_replace(',','',$value['trade_fee']),
  396. "trade_remark"=>$value['trade_remark']??"",
  397. "tradeTime"=>startTime($value['tradeTime']),
  398. "trade_in_account"=>$value['trade_in_account']??"",
  399. "trade_used"=>$value['trade_used']??"",
  400. "trade_bank"=>$value['trade_bank'],
  401. "trade_account"=>$value['trade_account'],
  402. "poCode"=>$value['poCode'],
  403. "NdCode"=>$value['NdCode'],
  404. "customerName"=>$value['customerName']
  405. ];
  406. }
  407. if(empty($list)) return error("导入数据不能为空");
  408. $num=$this->model->saveAll($list);
  409. if($num->isEmpty()) return error("资金流水导入失败");
  410. return success("资金导入成功");
  411. }
  412. public function importTradeByBatchOrderCode(){
  413. $list= $this->request->post("list",[],"trim");
  414. if(!is_array($list)||empty($list)) return error("参数错误");
  415. $val_item = Validate::rule([
  416. 'companyNo|卖出方公司编号' => 'require|max:255',
  417. 'tradNo|资金编号' => 'require|max:255',
  418. 'orderCode|订单编号' => 'require|max:255',
  419. 'trad_fee|认领资金' => 'require|float|gt:0|max:999999999.99',
  420. ]);
  421. $companyArr= Business::whereIn('companyNo',array_column($list,'companyNo'))->column('company','companyNo');
  422. if(empty($companyArr)) return error("收款方业务公司不存在");
  423. $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");
  424. if(empty($orderArr)) return error("订单信息不存在");
  425. $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");
  426. if(empty($tradeArr)) return error("资金流水信息不存在");
  427. $Assoc=[];
  428. $trade_log=[];
  429. foreach ($list as $key=>$value){
  430. if(!$val_item->check($value)) return error('第'.($key+1).'行'.$val_item->getError());
  431. if(!isset($companyArr[$value['companyNo']])) return error($value['companyNo'].'卖出方公司不存在');
  432. if(!isset($orderArr[$value['orderCode']])) return error($value['orderCode'].'订单信息不存在');
  433. if($orderArr[$value['orderCode']]['companyNo']!=$value['companyNo']) return error($value['orderCode'].'卖出方公司与订单业务公司不一致');
  434. if($orderArr[$value['orderCode']]['wpay_fee']< $value['trad_fee']) return error($value['orderCode'].'销售单未付款金额不足核销金额');
  435. $orderArr[$value['orderCode']]['wpay_fee']-=$value['trad_fee'];
  436. if(!isset($tradeArr[$value['tradNo']])) return error($value['tradNo'].'资金流水信息不存在');
  437. if($tradeArr[$value['tradNo']]['balance']< $value['trad_fee']) return error($value['tradNo'].'资金流水余额不足核销金额');
  438. $tradeArr[$value['tradNo']]['balance']-=$value['trad_fee'];
  439. if($tradeArr[$value['tradNo']]['companyNo']!=$value['companyNo']) return error($value['tradNo'].'资金流水业务公司与卖出方公司不一致');
  440. $temp=['logNo' => makeNo('TRC',str_pad($key+1,4,'0',STR_PAD_LEFT)),
  441. 'tradNo' => $value['tradNo'],
  442. 'platform_type' => $orderArr[$value['orderCode']]['platform_type'],
  443. 'companyNo' => $value['companyNo'],
  444. 'customerNo' => $orderArr[$value['orderCode']]['customerNo'],
  445. 'apply_id' => $this->uid,
  446. 'apply_name' => $this->uname,
  447. 'trade_time' => $tradeArr[$value['tradNo']]['trade_time'],
  448. 'total_fee' => $value['trad_fee'],
  449. 'status' => 2,//2审核通过
  450. ];
  451. $Assoc[]=[
  452. 'assocNo' =>makeNo("AS",str_pad($key+1,4,'0',STR_PAD_LEFT)),
  453. 'apply_id' => $this->uid,
  454. 'apply_name' => $this->uname,
  455. 'type' => 2,
  456. 'orderCode' => $value['orderCode'],
  457. 'customerNo' => $orderArr[$value['orderCode']]['customerNo'],
  458. 'viceCode' => $temp['logNo'],
  459. 'order_total' =>$orderArr[$value['orderCode']]['totalPrice'],
  460. 'vice_total' => $value['trad_fee'],
  461. 'cancel_fee' => $value['trad_fee'],
  462. 'status' => 2,//认领通过
  463. ];
  464. $trade_log[]=$temp;
  465. }
  466. $this->model->startTrans();
  467. try{
  468. foreach ($list as $key=>$value){
  469. $trade = $this->model->where('is_del', 0)
  470. ->whereIn('status', [1, 2])
  471. ->where('tradNo', $value['tradNo'])
  472. ->field('id,balance,used_fee,companyNo,trade_time')
  473. ->findOrEmpty();
  474. if($trade->isEmpty()) throw new \Exception($value['tradNo'].'资金流水信息不存在');
  475. if($trade->balance< $value['trad_fee']) throw new \Exception($value['tradNo'].'资金流水余额不足核销金额');
  476. $trade->balance-=$value['trad_fee'];
  477. $trade->used_fee+=$value['trad_fee'];
  478. $trade->status=$trade->balance==0?3:2;
  479. $tradSave=$trade->save();
  480. if(!$tradSave) throw new \Exception($value['tradNo'].'资金流水更新失败');
  481. $order = QrdInfo::where('is_del', 0)
  482. ->where('sequenceNo', $value['orderCode'])
  483. ->field('id,customerNo,companyNo,status,pay_fee,apay_fee,wpay_fee,pay_status,totalPrice,platform_type,cxCode,is_comon')
  484. ->findOrEmpty();
  485. if($order->isEmpty()) throw new \Exception($value['orderCode'].'订单信息不存在');
  486. if($order->wpay_fee< $value['trad_fee']) throw new \Exception($value['orderCode'].'销售单未付款金额不足核销金额');
  487. $order->wpay_fee-=$value['trad_fee'];
  488. $order->apay_fee+=$value['trad_fee'];
  489. $order->pay_status=$order->wpay_fee==0 && $order->pay_fee==0?3:2;
  490. $order->status=1;
  491. $ordersave=$order->save();
  492. if(!$ordersave) throw new \Exception($value['orderCode'].'订单信息更新失败');
  493. if($order->pay_status==3 && $order->is_comon=0 && $order->cxCode!=''){
  494. ComonOrder::where(['cxCode'=>$order->cxCode,'status'=>-1])->save(['status'=>0]);
  495. }
  496. }
  497. if(!empty($Assoc)) {
  498. $AssocIn= (new Assoc())->saveAll($Assoc);
  499. if($AssocIn->isEmpty()) throw new \Exception('关联信息写入失败');
  500. }
  501. if(!empty($trade_log)){
  502. $trade_logIn= (new TradePool())->saveAll($trade_log);
  503. if($trade_logIn->isEmpty()) throw new \Exception('资金流水写入失败');
  504. }
  505. $this->model->commit();
  506. }catch (\exception $e){
  507. $this->model->rollback();
  508. return error($e->getMessage());
  509. }
  510. return success("数据导入成功");
  511. }
  512. }