Trade.php 27 KB

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