Trade.php 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675
  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;
  7. use app\cxinv\model\TradePool;
  8. use app\cxinv\model\TradeReturn;
  9. use app\user\model\Business;
  10. use think\App;
  11. use think\facade\Validate;
  12. use think\helper\Str;
  13. class Trade extends Base{
  14. public function __construct(App $app) {
  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']=bcsub($qrdList[$order['sequenceNo']]['wpay_fee'],$order['trad_fee'],2) ;
  38. $log_total=bcadd($log_total,$order['trad_fee'],2);
  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,5"]);
  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. Assoc::CheckTrad($log->logNo,$params['status']);
  110. if($params['status']==3|| $params['status']==5){
  111. if($trade->used_fee<$log->total_fee) throw new \Exception("资金流水已使用金额不足");
  112. $trade->balance+=$log->total_fee;
  113. $trade->used_fee-=$log->total_fee;
  114. $trade->status= $trade->used_fee==0?1:2;
  115. $up=$trade->save();
  116. if(!$up) throw new \Exception("资金流水更新失败");
  117. }
  118. $log->status=$params['status'];
  119. $log->remark=$params['remark'];
  120. $save=$log->save();
  121. if(!$save) throw new \Exception('资金认领状态更新失败');
  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"=>"",
  131. 'poCode'=>'','pcCode'=>'',"page"=>1,"size"=>20,'used_lower'=>"",'used_upper'=>'','total_lower'=>'','total_upper'=>'',"relaComNo"=>"",'trade_in_code'=>'',
  132. "trade_type"=>"","palt_po"=>"","skuCode"=>"","goodName"=>"","plat_total_lower"=>"",'plat_total_upper'=>'',"com_total_lower"=>"",
  133. "com_total_upper"=>"","trade_used"=>"","create_start"=>"","create_end"=>"",'customerName'=>''],"post","trim");
  134. $where=[["is_del","=",0]];
  135. if($params['tradNo']!=="")$where[]=["trade.tradNo","like","%".$params['tradNo']."%"];
  136. if(!empty($params['status']))$where[]=["status","in",$params['status']];
  137. if($params['name']!=="")$where[]=["trade_out","like","%".$params['name']."%"];
  138. if($params['bank']!=="")$where[]=["trade_bank","like","%".$params['bank']."%"];
  139. if($params['start']!=="") $where[]=["trade_time",">=",startTime($params['start'])];
  140. if($params['end']!=="") $where[]=["trade_time","<=",endTime($params['end'])];
  141. if($params['companyNo']!=="")$where[]=["companyNo","like","%".$params['companyNo']."%"];
  142. if($params['customerName']!=="")$where[]=["customerName","like","%".$params['customerName']."%"];
  143. if($params['trade_in_code']!=="") $where[]=["trade_in_code","like","%".$params['trade_in_code']."%"];
  144. if($params['relaComNo']!=="")$where[]=["companyNo","like","%".$params['relaComNo']."%"];
  145. if($params['used_lower']!=="") $where[]=["used_fee",">=",$params['used_lower']];
  146. if($params['used_upper']!=="") $where[]=["used_fee","<=",$params['used_upper']];
  147. if($params['total_lower']!=="") $where[]=["total_fee",">=",$params['total_lower']];
  148. if($params['total_upper']!=="") $where[]=["total_fee","<=",$params['total_upper']];
  149. if($params['poCode']!=="") $where[]=["poCode","like","%".$params['poCode']."%"];
  150. if($params['pcCode']!=="") $where[]=["pcCode","like","%".$params['pcCode']."%"];
  151. if($params['trade_type']!=="") $where[]=["trade_type","=",$params['trade_type']];
  152. if($params['palt_po']!=="") $where[]=["palt_po","like","%".$params['palt_po']."%"];
  153. if($params['skuCode']!=="") $where[]=["skuCode","like","%".$params['skuCode']."%"];
  154. if($params['goodName']!=="") $where[]=["goodName","like","%".$params['goodName']."%"];
  155. if($params['plat_total_lower']!=="") $where[]=["plat_total",">=",$params['plat_total_lower']];
  156. if($params['plat_total_upper']!=="") $where[]=["plat_total","<=",$params['plat_total_upper']];
  157. if($params['com_total_lower']!=="") $where[]=["com_total",">=",$params['com_total_lower']];
  158. if($params['com_total_upper']!=="") $where[]=["com_total","<=",$params['com_total_upper']];
  159. if($params['trade_used']!=="") $where[]=["trade_used","like",'%'.$params['trade_used'].'%'];
  160. if($params['create_start']!=="") $where[]=["addtime",">=",startTime($params['create_start'])];
  161. if($params['create_end']!=="") $where[]=["addtime","<=",endTime($params['create_end'])];
  162. $list = $this->model->with(['Lib'])->withJoin("Lib","left")->where($where)->order("id desc")->paginate(["list_rows"=>$params['size'],"page"=>$params['page']]);
  163. return success("成功",['list'=>$list->items(),'count'=>$list->total()]);
  164. }
  165. public function delete(){
  166. $id = $this->request->post("id","0","int");
  167. if($id<=0) return error("参数错误");
  168. $info = $this->model->where(["id"=>$id,"is_del"=>0])->findOrEmpty();
  169. if($info->isEmpty()) return error("资金流水信息不存在");
  170. if($info->status!=1) return error("当前状态不允许删除");
  171. $this->model->startTrans();
  172. try{
  173. $info->is_del=1;
  174. $save=$info->save();
  175. if(!$save) throw new \Exception("资金流水删除失败");
  176. $this->model->commit();
  177. }catch (\Exception $e){
  178. $this->model->rollback();
  179. return error($e->getMessage());
  180. }
  181. return success("删除成功");
  182. }
  183. public function info(){
  184. $tradNo = $this->request->post('tradNo','','trim');
  185. if($tradNo =='') return error('参数错误');
  186. $info = $this->model->with(["lib"])->where(['tradNo'=>$tradNo,'is_del'=>0])->findOrEmpty();
  187. return success('查询成功',$info);
  188. }
  189. public function PoolList(){
  190. $params=$this->request->param(["logNo"=>"","status"=>"","name"=>"","bank"=>"","start"=>"","end"=>"","companyNo"=>"",
  191. "page"=>1,"size"=>20,'apply_id'=>"",'orderCode'=>'',"relaNo"=>"",'platform_type'=>'',"relaComNo"=>"",'cxCode'=>'',"pcCode"=>"",
  192. "trade_type"=>"","tag_type"=>"","tradNo"=>""],"post","trim");
  193. $where=[["a.is_del","=",0],["b.is_del","=",0],['c.is_del','=',0],['d.is_del','=',0]];
  194. if($params['logNo']!=="")$where[]=["a.logNo","like","%".$params['logNo']."%"];
  195. if($params['tradNo']!=='')$where[]=['a.tradNo','like','%'.$params['tradNo'].'%'];
  196. if($params['apply_id']!=="")$where[]=["a.apply_id","like","%".$params['apply_id']."%"];
  197. if($params['orderCode']!=="")$where[]=["d.orderCode","like","%".$params['orderCode']."%"];
  198. if($params['platform_type']!=="")$where[]=["a.platform_type","like","%".$params['platform_type']."%"];
  199. if($params['status']!=="")$where[]=["a.status","=",$params['status']];
  200. if($params['name']!=="")$where[]=["b.trade_out","like","%".$params['name']."%"];
  201. if($params['bank']!=="")$where[]=["b.trade_bank","like","%".$params['bank']."%"];
  202. if($params['start']!=="") $where[]=["a.addtime",">=",startTime($params['start'])];
  203. if($params['end']!=="") $where[]=["a.addtime","<=",endTime($params['end'])];
  204. if($params['companyNo']!=="")$where[]=["a.companyNo","like","%".$params['companyNo']."%"];
  205. if($params['relaComNo']!=="")$where[]=["a.companyNo","like","%".$params['relaComNo']."%"];
  206. if($params['cxCode']!=="")$where[]=["c.cxCode","like","%".$params['cxCode']."%"];
  207. if($params['relaNo']!=="")$where[]=["a.relaNo","like","%".$params['relaNo']."%"];
  208. $pcData=['totalPrice'=>0,'usedPrice'=>0,'balancePrce'=>0,'tradPrice'=>0];// 统计数据
  209. if($params['pcCode']!==""){
  210. $where[]=['b.pcCode','like','%'.$params['pcCode'].'%'];
  211. $pc= $this->model->with(["lib"])->where('pcCode',$params['pcCode'])
  212. ->select();
  213. if(!$pc->isEmpty()){
  214. $pcData['totalPrice'] = array_sum(array_column($pc->toArray(),"total_fee"));
  215. $pcData["usedPrice"]= TradePool::where(["tradNo"=>array_column($pc->toArray(),'tradNo'),"status"=>2,"is_del"=>0,'tag_type'=>0])->sum("total_fee");
  216. $pcData['tradPrice']= TradePool::where(['tradNo'=>array_column($pc->toArray(),'tradNo'),'status'=>1,'is_del'=>0,'tag_type'=>0])->sum('total_fee');
  217. $pcData["balancePrce"] = array_sum(array_column($pc->toArray(),'balance'));
  218. }
  219. }
  220. if($params['trade_type']!=="") $where[]=["b.trade_type","=",$params['trade_type']];
  221. if($params['tag_type']!=="") $where[]=["a.tag_type","=",$params['tag_type']];
  222. $list = TradePool::alias("a")
  223. ->leftjoin('trade_lib e','a.tradNo=e.tradNo')
  224. ->leftJoin('trade b','a.tradNo=b.tradNo')
  225. ->leftJoin('assoc d','a.logNo=d.viceCode AND d.type = 2')
  226. ->leftJoin('qrd_info c','d.orderCode=c.sequenceNo')
  227. ->where($where)
  228. ->field('a.*,
  229. b.trade_bank,
  230. b.trade_account,
  231. b.trade_out,
  232. b.total_fee AS btotal_fee,
  233. b.used_fee,
  234. b.balance,
  235. c.customerName,
  236. c.customerNo,
  237. d.orderCode,
  238. c.qrdSource,
  239. c.cxCode,
  240. c.goodNo,
  241. e.goodNo as lib_goodNo,
  242. c.goodName,
  243. e.goodName as lib_goodName,
  244. c.goodNum-c.thNum as goodNum,
  245. e.goodNum as lib_goodNum,
  246. c.goodPrice,
  247. e.comPrice as lib_price,
  248. c.qrdType,
  249. c.ownerName,
  250. c.ownerid,
  251. c.department,
  252. c.platName,
  253. c.totalPrice,
  254. e.com_total as lib_total,
  255. d.cancel_fee,
  256. b.pcCode,
  257. c.poCode,
  258. b.poCode as order_poCode,
  259. e.palt_po')
  260. ->order("a.id desc")->paginate(["list_rows"=>$params['size'],"page"=>$params['page']]);
  261. return success("成功",['list'=>$list->items(),'count'=>$list->total(),"pcData"=>$pcData]);
  262. }
  263. public function PoolQuery(){
  264. $params=$this->request->param(["logNo"=>"","tradNo"=>"","status"=>"","companyNo"=>"",'apply_id'=>"",'platform_type'=>'',"relaComNo"=>"",],"post","trim");
  265. $where=[["is_del","=",0]];
  266. if($params['logNo']!=="")$where[]=["logNo","like","%".$params['logNo']."%"];
  267. if($params['tradNo']!=="")$where[]=["tradNo","like","%".$params['tradNo']."%"];
  268. if($params['apply_id']!=="")$where[]=["apply_id","like","%".$params['apply_id']."%"];
  269. if($params['status']!=="")$where[]=["status","=",$params['status']];
  270. if($params['companyNo']!=="")$where[]=["companyNo","like","%".$params['companyNo']."%"];
  271. if($params['relaComNo']!=="")$where[]=["companyNo","like","%".$params['relaComNo']."%"];
  272. if($params['platform_type']!=="") $where[]=["platform_type","=",$params['platform_type']];
  273. $list = TradePool::with(["Lib"])->where($where)->order("id desc")->select();
  274. return success("成功",$list);
  275. }
  276. public function PoolInfo(){
  277. $logNo = $this->request->post("logNo","","trim");
  278. if($logNo=="") return error("参数错误");
  279. $info = TradePool::with(["Trade","Lib"])
  280. ->where(["logNo"=>$logNo,"is_del"=>0])
  281. ->findOrEmpty();
  282. if($info->isEmpty()) return error("资金认领信息不存在");
  283. $info['orderinfo']= Assoc::alias("a")
  284. ->leftJoin('qrd_info b','a.orderCode=b.sequenceNo')
  285. ->where(["a.viceCode"=>$logNo,"a.type"=>2,'a.is_del'=>0,'a.status'=>[1,2,3]])
  286. ->field('b.*,c.goodNum-c.thNum as goodNum,a.cancel_fee')
  287. ->select();
  288. return success("成功",$info);
  289. }
  290. public function PoolBatchCheck(){
  291. $params=$this->request->param(["idArr"=>[],"remark"=>"","status"=>""],"post","trim");
  292. $valid = Validate::rule([
  293. "idArr|ID"=>"require|array",
  294. "remark|备注"=>"max:255",
  295. "status|状态"=>"require|in:2,3"
  296. ]);
  297. if(!$valid->check($params)) return error($valid->getError());
  298. $logInfo = TradePool::where(["id"=>$params['idArr'],"status"=>1,"is_del"=>0])->select();
  299. if($logInfo->isEmpty()) return error("资金认领信息不存在");
  300. $this->model->startTrans();
  301. try{
  302. $logInfo->each(function($item) use($params){
  303. Assoc::CheckTrad($item->logNo,$params['status']);
  304. $item->status = $params['status'];
  305. $item->remark = $params['remark'];
  306. $save=$item->save();
  307. if(!$save) throw new \Exception("资金认领信息更新失败");
  308. if($params['status']==3){
  309. $trade = $this->model->where(["tradNo"=>$item->tradNo,"is_del"=>0])->findOrEmpty();
  310. if($trade->used_fee<$item->total_fee) throw new \Exception('资金流水已使用金额不足');
  311. $trade->balance+=$item->total_fee;
  312. $trade->used_fee-=$item->total_fee;
  313. $trade->status= $trade->used_fee==0?1:2;
  314. $up=$trade->save();
  315. if(!$up) throw new \Exception('资金流水更新失败');
  316. }
  317. });
  318. $this->model->commit();
  319. }catch (\Exception $e){
  320. $this->model->rollback();
  321. return error($e->getMessage());
  322. }
  323. return success("操作成功");
  324. }
  325. //认领资金退回
  326. public function Back(){
  327. $params=$this->request->param(["logNo"=>"","remark"=>"","type"=>"","return_reason"=>""],"post","trim");
  328. $valid = Validate::rule([
  329. "logNo|认领资金编号"=>"require|max:255",
  330. "type|类型"=>"require|in:1,2",
  331. "return_reason|退回原因"=>"require|max:255",
  332. "remark|备注"=>"max:255"
  333. ]);
  334. if(!$valid->check($params)) return error($valid->getError());
  335. $info = TradePool::where(["logNo"=>$params['logNo'],"is_del"=>0])->findOrEmpty();
  336. if($info->isEmpty()) return error("资金认领信息不存在");
  337. if($info->status!=2) return error("资金认领当前状态不允许退回");
  338. if($info->tag_type==1) return error("平台衍生资金认领不允许退回");
  339. $isT = TradeReturn::where(["logNo"=>$params['logNo'],"status"=>[0,1]])->findOrEmpty();
  340. if(!$isT->isEmpty()) return error("当前资金认领已申请退款,请勿重复申请");
  341. $data = [
  342. 'returnCode'=>makeNo('RTA'),
  343. 'logNo'=>$params['logNo'],
  344. 'companyNo'=>$info->companyNo,
  345. 'tradNo'=>$info->tradNo,
  346. 'return_img'=>'',
  347. 'type'=>$params['type'],
  348. 'apply_id'=>$this->uid,
  349. 'apply_name'=>$this->uname,
  350. 'return_reason'=>$params['return_reason'],
  351. 'remark'=>$params['remark'],
  352. "status"=>0
  353. ];
  354. $res = TradeReturn::create($data);
  355. if($res->isEmpty()) return error("操作失败");
  356. return success("操作成功");
  357. }
  358. public function BackList(){
  359. $params=$this->request->param(["logNo"=>"","tradNo"=>"","status"=>"","companyNo"=>"",'apply_id'=>"","type"=>"",
  360. "returnCode"=>"","orderCode"=>"","relaComNo"=>"", "page"=>1,"size"=>20],"post","trim");
  361. $where=[["a.is_del","=",0]];
  362. if($params['logNo']!=="")$where[]=["a.logNo","like","%".$params['logNo']."%"];
  363. if($params['apply_id']!=="")$where[]=["a.apply_id","like","%".$params['apply_id']."%"];
  364. if($params['tradNo']!=="")$where[]=["a.tradNo","like","%".$params['tradNo']."%"];
  365. if($params['status']!=="")$where[]=["a.status","=",$params['status']];
  366. if($params['companyNo']!=="")$where[]=["a.companyNo","like","%".$params['companyNo']."%"];
  367. if($params['relaComNo']!=="")$where[]=["a.companyNo","like","%".$params['relaComNo']."%"];
  368. if($params['type']!=="")$where[]=["a.type","=",$params['type']];
  369. if($params['returnCode']!=="")$where[]=["a.returnCode","like","%".$params['returnCode']."%"];
  370. if($params['orderCode']!=="")$where[]=["b.orderCode","like","%".$params['orderCode']."%"];
  371. $list = TradeReturn::alias("a")
  372. ->leftJoin('trade_pool d','a.logNo=d.logNo and d.is_del=0')
  373. ->leftJoin('assoc b','a.logNo=b.viceCode and b.type=2 and b.is_del=0')
  374. ->leftJoin('qrd_info c','c.sequenceNo=b.orderCode and c.is_del=0')
  375. ->field('a.*,d.tag_type,d.relaNo,b.orderCode,c.qrdSource,goodNo,goodName,qrdType,c.ownerName,c.ownerid,c.department,c.poCode,c.platName,b.cancel_fee,c.customerName,c.customerNo')
  376. ->where($where)
  377. ->order("a.id desc")->paginate(["list_rows"=>$params['size'],"page"=>$params['page']]);
  378. return success("成功",['list'=>$list->items(),'count'=>$list->total()]);
  379. }
  380. public function BackInfo(){
  381. $returnCode = $this->request->post("returnCode","","trim");
  382. if($returnCode=="") return error("参数错误");
  383. $info = TradeReturn::with(["trade","pool"])->where(["returnCode"=>$returnCode,"is_del"=>0])->findOrEmpty();
  384. if($info->isEmpty()) return error("退款申请数据未找到");
  385. $info['log_total_fee']= $info->pool->total_fee;
  386. $info['log_apply_id']= $info->pool->apply_id;
  387. $info['log_apply_name']= $info->pool->apply_name;
  388. $info['orderinfo']= Assoc::alias("a")
  389. ->leftJoin('qrd_info b','a.orderCode=b.sequenceNo')
  390. ->where(["a.viceCode"=>$info['logNo'],"a.type"=>2,'a.is_del'=>0,'a.status'=>[1,2,3]])
  391. ->field('b.*,c.goodNum-c.thNum as goodNum,a.cancel_fee')
  392. ->findOrEmpty();
  393. return success("成功",$info);
  394. }
  395. public function BackStatus(){
  396. $params=$this->request->param(["returnCode"=>"","status"=>"","remark"=>"","return_img"=>""],"post","trim");
  397. $valid = Validate::rule([
  398. "returnCode|退款申请编号"=>"require|max:255",
  399. "status|状态"=>"require|in:1,2",
  400. "remark|备注"=>"max:255"
  401. ]);
  402. if(!$valid->check($params)) return error($valid->getError());
  403. $info = TradeReturn::where(["returnCode"=>$params['returnCode'],"is_del"=>0])->findOrEmpty();
  404. if($info->isEmpty()) return error("退款申请数据未找到");
  405. $loginfo= TradePool::where(["logNo"=>$info['logNo'],"is_del"=>0])->findOrEmpty();
  406. if($loginfo->isEmpty()) return error("资金认领数据未找到");
  407. if($loginfo->status!=2) return error("认领资金信息审核未通过");
  408. $this->model->startTrans();
  409. try{
  410. $info->status=$params['status'];
  411. $info->remark=$params['remark'];
  412. $info->return_img=$params['return_img'];
  413. $save=$info->save();
  414. if ($save===false) throw new \Exception("操作失败");
  415. if($params['status']==1){
  416. $loginfo->status= 4;
  417. $logsave=$loginfo->save();
  418. if ($logsave===false) throw new \Exception("操作失败");
  419. Assoc::CheckTrad($info['logNo'],$loginfo->status);
  420. $trade = $this->model->where("tradNo",$loginfo->tradNo)->findOrEmpty();
  421. if($trade->isEmpty()) throw new \Exception("资金流水信息未找到");
  422. $trade->used_fee-=$loginfo->total_fee;
  423. $trade->balance+=$loginfo->total_fee;
  424. $trade->status= $trade->used_fee==0?1:2;
  425. $tradesave=$trade->save();
  426. if ($tradesave===false) throw new \Exception("资金流水信息更新失败");
  427. }
  428. $this->model->commit();
  429. }catch (\Exception $e){
  430. $this->model->rollback();
  431. return error($e->getMessage());
  432. }
  433. return success("退款申请审核成功");
  434. }
  435. /**
  436. * 导入交易流水
  437. * data:
  438. * companyNo
  439. * trade_in_account
  440. * tradeTime
  441. * trade_fee
  442. * trade_bank
  443. * trade_account
  444. * trade_out
  445. */
  446. public function importTradeByArr(){
  447. $data= $this->request->post("data",[],"trim");
  448. if(!is_array($data)||empty($data)) return error("参数错误");
  449. /**
  450. * 'palt_po' =>'varchar',//平台采购po
  451. 'skuCode' =>'varchar',//泰E购sku
  452. 'goodName' =>'varchar',//平台物料名称
  453. 'goodNum' =>'varchar',//商品数量
  454. 'platPrice' =>'decimal',//平台业务公司单价
  455. 'plat_total' =>'decimal',//平台业务公司金额
  456. 'tax' =>'varchar',//税率
  457. 'cat_code' =>'varchar',//税收编码
  458. 'cat_name' =>'varchar',//税收名称
  459. 'comPrice' =>'decimal',//业务公司单价
  460. 'com_total' =>'decimal',//业务公司总额
  461. 'pcCode' =>'varchar',//批次号
  462. */
  463. $valid = Validate::rule([
  464. "companyNo|业务公司编号"=>"require|max:255",
  465. "trade_type|交易类型"=>"require|number|in:0,1",
  466. "trade_in_code|收款方公司编号"=>"require|max:255",
  467. "trade_in_account|收款账户"=>"require|max:255",
  468. "tradeTime|交易时间"=>"require|date",
  469. "trade_fee|交易金额"=>"require|float",
  470. "trade_bank|交易银行"=>"require|max:255",
  471. "trade_account|对方账号"=>"require|max:255",
  472. "trade_out|交易方名称"=>"require|max:255",
  473. "poCode|PO编号" =>"requireIf:trade_type,1|max:255",
  474. "NdCode|ND编号"=>"requireIf:trade_type,1|max:255",
  475. "palt_po|平台采购po"=>"requireIf:trade_type,1|max:255",
  476. "skuCode|SKU"=>"requireIf:trade_type,1|max:255",
  477. "goodNo|商品编码"=>"max:255",
  478. "goodName|商品名称"=>"requireIf:trade_type,1|max:255",
  479. "goodNum|商品数量"=>"requireIf:trade_type,1|float",
  480. "platPrice|平台单价"=>"requireIf:trade_type,1|float",
  481. "plat_total|平台金额"=>"requireIf:trade_type,1|float",
  482. "unit|单位"=>"requireIf:trade_type,1|max:255",
  483. "tax|税率"=>"requireIf:trade_type,1|max:255",
  484. "cat_code|税收编码"=>"requireIf:trade_type,1|max:255",
  485. "cat_name|税收名称"=>"requireIf:trade_type,1|max:255",
  486. "comPrice|业务公司单价"=>"requireIf:trade_type,1|float",
  487. "com_total|业务公司总额"=>"requireIf:trade_type,1|float",
  488. ]);
  489. $companyArr= Business::whereIn("companyNo",array_merge(array_column($data,'companyNo'),array_column($data,'trade_in_code')))->column("company","companyNo");
  490. $list=[];
  491. $lib=[];
  492. $pcCode= makeNo("PC");
  493. foreach ($data as $key=>$value){
  494. if(!$valid->check($value)) return error('第'.($key+1).'行'.$valid->getError());
  495. if(!isset($companyArr[$value['companyNo']])) return error('第'.($key+1).'行收款方业务公司不存在');
  496. if(!isset($companyArr[$value['trade_in_code']])) return error('第'.($key+1).'行收款方公司不存在');
  497. $radStr= Str::random(3,3);
  498. $tradNo= makeNo('S'.$radStr,str_pad($key+1,4,'0',STR_PAD_LEFT));
  499. $list[]=[
  500. "tradNo"=>$tradNo,
  501. "companyNo"=>$value['companyNo'],
  502. "companyName"=>$companyArr[$value['companyNo']]??"",
  503. "trade_in_code"=>$value['trade_in_code'],
  504. "trade_in"=>$value['trade_in']??$companyArr[$value['trade_in_code']],
  505. "trade_out"=>$value['trade_out'],
  506. "trade_type"=>$value['trade_type']??0,
  507. "total_fee"=>str_replace(',','',$value['trade_fee']),
  508. "balance"=>str_replace(',','',$value['trade_fee']),
  509. "trade_remark"=>$value['trade_remark']??"",
  510. "trade_time"=>startTime($value['tradeTime']),
  511. "trade_in_account"=>$value['trade_in_account']??"",
  512. "trade_used"=>$value['trade_used']??"",
  513. "trade_bank"=>$value['trade_bank'],
  514. "trade_account"=>$value['trade_account'],
  515. "poCode"=>$value['poCode'],
  516. "NdCode"=>$value['NdCode'],
  517. "customerName"=>$value['customerName'],
  518. 'pcCode'=>$pcCode,
  519. "apply_id"=>$this->uid,
  520. "apply_name"=>$this->uname,
  521. ];
  522. if($value['trade_type']==1){
  523. $lib[]=[
  524. "tradNo"=>$tradNo,
  525. "palt_po"=>$value['palt_po'],
  526. "skuCode"=>$value['skuCode'],
  527. "goodNo"=>$value['goodNo'],
  528. "goodName"=>$value['goodName'],
  529. "goodNum"=>$value['goodNum'],
  530. "platPrice"=>$value['platPrice'],
  531. "plat_total"=>$value['plat_total'],
  532. "tax"=>$value['tax'],
  533. "unit"=>$value['unit'],
  534. "cat_code"=>$value['cat_code'],
  535. "cat_name"=>$value['cat_name'],
  536. "comPrice"=>$value['comPrice'],
  537. "com_total"=>$value['com_total'],
  538. ];
  539. }
  540. }
  541. if(empty($list)) return error("导入数据不能为空");
  542. $this->model->startTrans();
  543. try{
  544. $num=$this->model->saveAll($list);
  545. if($num->isEmpty())throw new \Exception('资金流水导入失败');
  546. if(!empty($lib)) (new TradeLib())->saveAll($lib);
  547. $this->model->commit();
  548. }catch (\Exception $e){
  549. $this->model->rollback();
  550. return error($e->getMessage());
  551. }
  552. return success("资金导入成功");
  553. }
  554. public function importTradeByBatchOrderCode(){
  555. $list= $this->request->post("list",[],"trim");
  556. if(!is_array($list)||empty($list)) return error("参数错误");
  557. $val_item = Validate::rule([
  558. 'companyNo|卖出方公司编号' => 'require|max:255',
  559. 'tradNo|资金编号' => 'require|max:255',
  560. 'orderCode|订单编号' => 'require|max:255',
  561. 'trad_fee|认领资金' => 'require|float|gt:0|max:999999999.99',
  562. ]);
  563. $companyArr= Business::whereIn('companyNo',array_column($list,'companyNo'))->column('company','companyNo');
  564. if(empty($companyArr)) return error("收款方业务公司不存在");
  565. $orderArr= QrdInfo::whereIn('sequenceNo',array_column($list,'orderCode'))->where(["is_del"=>0])->column('id,customerNo,companyNo,is_comon,status,pay_fee,apay_fee,wpay_fee,pay_status,totalPrice,platform_type,cxCode',"sequenceNo");
  566. if(empty($orderArr)) return error("订单信息不存在");
  567. $tradeArr=$this->model->whereIn("tradNo",array_column($list,'tradNo'))->where(['is_del'=>0,"status"=>[1,2]])->column("id,balance,used_fee,companyNo,trade_time,trade_type","tradNo");
  568. if(empty($tradeArr)) return error("资金流水信息不存在");
  569. $Assoc=[];
  570. $trade_log=[];
  571. foreach ($list as $key=>$value){
  572. if(!$val_item->check($value)) return error('第'.($key+1).'行'.$val_item->getError());
  573. if(!isset($companyArr[$value['companyNo']])) return error($value['companyNo'].'卖出方公司不存在');
  574. if(!isset($orderArr[$value['orderCode']])) return error($value['orderCode'].'订单信息不存在');
  575. if($orderArr[$value['orderCode']]['companyNo']!=$value['companyNo']) return error($value['orderCode'].'卖出方公司与订单业务公司不一致');
  576. if($orderArr[$value['orderCode']]['wpay_fee']< $value['trad_fee']) return error($value['orderCode'].'销售单未付款金额不足核销金额');
  577. $orderArr[$value['orderCode']]['wpay_fee']=bcsub($orderArr[$value['orderCode']]['wpay_fee'], $value['trad_fee'],2);
  578. if(!isset($tradeArr[$value['tradNo']])) return error($value['tradNo'].'资金流水信息不存在');
  579. if($tradeArr[$value['tradNo']]['trade_type']!=1 && $orderArr[$value['orderCode']]['is_comon']==1) return error($value['orderCode'].'销售单类型与资金类型不匹配');
  580. if($tradeArr[$value['tradNo']]['balance']< $value['trad_fee']){
  581. return error($value['tradNo'].'资金流水余额不足核销金额');
  582. }
  583. $tradeArr[$value['tradNo']]['balance']=bcsub($tradeArr[$value['tradNo']]['balance'], $value['trad_fee'],2);
  584. if($tradeArr[$value['tradNo']]['companyNo']!=$value['companyNo']) return error($value['tradNo'].'资金流水业务公司与卖出方公司不一致');
  585. $temp=['logNo' => makeNo('TRC',str_pad($key+1,4,'0',STR_PAD_LEFT)),
  586. 'tradNo' => $value['tradNo'],
  587. 'platform_type' => $orderArr[$value['orderCode']]['platform_type'],
  588. 'companyNo' => $value['companyNo'],
  589. 'customerNo' => $orderArr[$value['orderCode']]['customerNo'],
  590. 'apply_id' => $this->uid,
  591. 'apply_name' => $this->uname,
  592. 'trade_time' => $tradeArr[$value['tradNo']]['trade_time'],
  593. 'total_fee' => $value['trad_fee'],
  594. 'status' => 2,//2审核通过
  595. ];
  596. $Assoc[]=[
  597. 'assocNo' =>makeNo("AS",str_pad($key+1,4,'0',STR_PAD_LEFT)),
  598. 'apply_id' => $this->uid,
  599. 'apply_name' => $this->uname,
  600. 'type' => 2,
  601. 'orderCode' => $value['orderCode'],
  602. 'customerNo' => $orderArr[$value['orderCode']]['customerNo'],
  603. 'viceCode' => $temp['logNo'],
  604. 'order_total' =>$orderArr[$value['orderCode']]['totalPrice'],
  605. 'vice_total' => $value['trad_fee'],
  606. 'cancel_fee' => $value['trad_fee'],
  607. 'status' => 2,//认领通过
  608. ];
  609. $trade_log[]=$temp;
  610. }
  611. $this->model->startTrans();
  612. try{
  613. foreach ($list as $key=>$value){
  614. $trade = $this->model->where('is_del', 0)
  615. ->whereIn('status', [1, 2])
  616. ->where('tradNo', $value['tradNo'])
  617. ->field('id,balance,used_fee,companyNo,trade_time')
  618. ->findOrEmpty();
  619. if($trade->isEmpty()) throw new \Exception($value['tradNo'].'资金流水信息不存在');
  620. if($trade->balance< $value['trad_fee']) throw new \Exception($value['tradNo'].'资金流水余额不足核销金额');
  621. $trade->balance=bcsub($trade->balance, $value['trad_fee'],2);
  622. $trade->used_fee=bcadd($trade->used_fee, $value['trad_fee'],2);
  623. $trade->status=$trade->balance==0?3:2;
  624. $tradSave=$trade->save();
  625. if(!$tradSave) throw new \Exception($value['tradNo'].'资金流水更新失败');
  626. $order = QrdInfo::where('is_del', 0)
  627. ->where('sequenceNo', $value['orderCode'])
  628. ->field('id,customerNo,companyNo,status,pay_fee,apay_fee,wpay_fee,pay_status,totalPrice,platform_type,cxCode,is_comon')
  629. ->findOrEmpty();
  630. if($order->isEmpty()) throw new \Exception($value['orderCode'].'订单信息不存在');
  631. if($order->wpay_fee< $value['trad_fee']) throw new \Exception($value['orderCode'].'销售单未付款金额不足核销金额');
  632. $order->wpay_fee=bcsub($order->wpay_fee, $value['trad_fee'],2);
  633. $order->apay_fee=bcadd( $order->apay_fee, $value['trad_fee'],2);
  634. $order->pay_status=$order->wpay_fee==0 && $order->pay_fee==0?3:2;
  635. $order->status=1;
  636. $ordersave=$order->save();
  637. if(!$ordersave) throw new \Exception($value['orderCode'].'订单信息更新失败');
  638. if($order->pay_status==3 && $order->is_comon==0 && $order->cxCode!=''){
  639. ComonOrder::where(['cxCode'=>$order->cxCode,'status'=>-1])->save(['status'=>0]);
  640. }
  641. }
  642. if(!empty($Assoc)) {
  643. $AssocIn= (new Assoc())->saveAll($Assoc);
  644. if($AssocIn->isEmpty()) throw new \Exception('关联信息写入失败');
  645. }
  646. if(!empty($trade_log)){
  647. $trade_logIn= (new TradePool())->saveAll($trade_log);
  648. if($trade_logIn->isEmpty()) throw new \Exception('资金流水写入失败');
  649. }
  650. $this->model->commit();
  651. }catch (\exception $e){
  652. $this->model->rollback();
  653. return error($e->getMessage());
  654. }
  655. return success("数据导入成功");
  656. }
  657. }