Trade.php 34 KB

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