OrderPay.php 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126
  1. <?php
  2. namespace app\admin\controller;
  3. use app\admin\model\ReportCode;use think\App;
  4. use think\Exception;
  5. use think\facade\Db;
  6. use think\facade\Validate;
  7. use think\queue\command\Retry;
  8. class OrderPay extends Base{
  9. public function __construct(App $app) {parent::__construct($app);}
  10. //新建资金认领数据
  11. public function create(){
  12. $tradNo = isset($this->post['tradNo'])&&$this->post['tradNo']!=""?trim($this->post['tradNo']):"";
  13. if($tradNo==""){
  14. return error_show(1004,"参数 tradNo 不能为空");
  15. }
  16. $orderArr = isset($this->post['orderArr'])&&!empty($this->post['orderArr'])?$this->post['orderArr']:[];
  17. if(empty($orderArr)){
  18. return error_show(1004,"参数 orderArr 不能为空");
  19. }
  20. Db::startTrans();
  21. try{
  22. $trade =Db::name("trade")->where(["tradNo"=>$tradNo,"is_del"=>0])->lock(true)->findOrEmpty();
  23. if(empty($trade)){
  24. Db::rollback();
  25. return error_show(1004,"未找到资金信息");
  26. }
  27. $total_fee =array_sum(array_column($orderArr,"trad_fee"));
  28. if ($trade['balance']<$total_fee){
  29. Db::rollback();
  30. return error_show(1004,"资金余额不足核销");
  31. }
  32. $assoc=[];
  33. foreach ($orderArr as $value){
  34. $logNo=makeNo("TRC");
  35. if(!isset($value['sequenceNo'])||$value['sequenceNo']==""){
  36. Db::rollback();
  37. return error_show(1004,"销售单编号不能为空");
  38. }
  39. if(!isset($value['trad_fee'])||$value['trad_fee']==""){
  40. Db::rollback();
  41. return error_show(1004,"销售单核销金额不能为空");
  42. }
  43. $qrd =Db::name("qrd_info")->where("sequenceNo","=",$value['sequenceNo'])->field("id,customerNo,status,pay_fee,wpay_fee,pay_status,totalPrice")->findOrEmpty();
  44. if(empty($qrd)){
  45. Db::rollback();
  46. return error_show(1004,"销售单信息未找到");
  47. }
  48. if($qrd['wpay_fee']<$value['trad_fee']){
  49. Db::rollback();
  50. return error_show(1004,"销售单未付款金额不足核销金额");
  51. }
  52. $updt=[
  53. "pay_fee"=>$qrd['pay_fee']+$value['trad_fee'],
  54. "wpay_fee"=>$qrd['wpay_fee']-$value['trad_fee'],
  55. "pay_status"=>2,
  56. "status"=>1,
  57. "updatetime"=>date("Y-m-d H:i:s")
  58. ];
  59. $qrdup = Db::name("qrd_info")->where($qrd)->update($updt);
  60. if($qrdup==false){
  61. Db::rollback();
  62. return error_show(1004,"销售单核销金额失败");
  63. }
  64. $temp=[
  65. "assocNo"=>makeNo("AS"),
  66. "apply_id"=>$this->uid,
  67. "apply_name"=>$this->uname,
  68. "type"=>2,
  69. "orderCode"=>$value['sequenceNo'],
  70. "customerNo"=>$qrd['customerNo'],
  71. "viceCode"=>$logNo,
  72. "order_total"=>$qrd['totalPrice'],
  73. "vice_total"=>$total_fee,
  74. "cancel_fee"=>$value['trad_fee'],
  75. "status"=>1,
  76. "addtime"=>date("Y-m-d H:i:s"),
  77. "updatetime"=>date("Y-m-d H:i:s")
  78. ];
  79. $assoc[]=$temp;
  80. $create = [
  81. "logNo"=>$logNo,
  82. "tradNo"=>$tradNo,
  83. "platform_type"=>$qrd['platform_type']??0,
  84. "companyNo"=>$trade['companyNo'],
  85. "customerNo"=>$qrd['customerNo'],
  86. "apply_id"=>$this->uid,
  87. "apply_name"=>$this->uname,
  88. "trade_time"=>$trade['trade_time'],
  89. "total_fee"=>$value['trad_fee'],
  90. "status"=>1,
  91. "addtime"=>date("Y-m-d H:i:s"),
  92. "updatetime"=>date("Y-m-d H:i:s")
  93. ];
  94. $tradchild =Db::name("trade_pool")->insert($create);
  95. if($tradchild ==false){
  96. Db::rollback();
  97. return error_show(1004,"资金认领失败");
  98. }
  99. $report=ReportCode::where(["qrdNo"=>$value['sequenceNo']])->find();
  100. if($report)$report->setField("logNo",$logNo);
  101. if($report)$report->setField("tradNo",$tradNo);
  102. }
  103. $asscin= Db::name("assoc")->insertAll($assoc);
  104. if($asscin!=count($assoc)){
  105. Db::rollback();
  106. return error_show(1004,"销售单核销金额关联失败");
  107. }
  108. $update=[
  109. "balance"=>$trade['balance']-$total_fee,
  110. "used_fee"=>$trade['used_fee']+$total_fee,
  111. "status"=>($trade['balance']-$total_fee)==0 ? 3:2,
  112. "updatetime"=>date("Y-m-d H:i:s"),
  113. ];
  114. $tradup=Db::name("trade")->where($trade)->update($update);
  115. if($tradup){
  116. Db::commit();
  117. return app_show(0,"资金认领成功");
  118. }
  119. Db::rollback();
  120. return error_show(1004,"资金认领失败");
  121. }catch (\Exception $e){
  122. Db::rollback();
  123. return error_show(1004,$e->getMessage());
  124. }
  125. }
  126. // 1待审批2审批通过3审批驳回4退款5解除认领
  127. public function status(){
  128. $logNo = isset($this->post['logNo'])&&$this->post['logNo']!=""?trim($this->post['logNo']):"";
  129. if($logNo==""){
  130. return error_show(1004,"参数logNo不能为空");
  131. }
  132. $logingo= Db::name("trade_pool")->where(["logNo"=>$logNo,"is_del"=>0])->find();
  133. if($logingo==false){
  134. return error_show(1004,"资金认领信息未找到");
  135. }
  136. $status=isset($this->post['status'])&&$this->post['status']!=""?intval($this->post['status']):"";
  137. if($status==""){
  138. return error_show(1004,"参数 status 不能为空");
  139. }
  140. if($status==5 &&$logingo['status']!=1 ){
  141. return error_show(1004,"认领资金状态有误");
  142. }
  143. $remark=isset($this->post['remark'])&&$this->post['remark']!=""?trim($this->post['remark']):"";
  144. $trade =Db::name("trade")->where(["tradNo"=>$logingo['tradNo'],"is_del"=>0])->findOrEmpty();
  145. if(empty($trade)){
  146. return error_show(1005,"资金信息未找到");
  147. }
  148. Db::startTrans();
  149. try {
  150. $up=["status"=>$status,"updatetime"=>date("Y-m-d H:i:s"),"remark"=>$remark];
  151. $logup = Db::name("trade_pool")->where($logingo)->update($up);
  152. if($logup){
  153. if($status==2){
  154. $qrdArr=Db::name("assoc")->where(["viceCode"=>$logNo,"status"=>1,"is_del"=>0])->select()->toArray();
  155. if(!empty($qrdArr)){
  156. foreach ($qrdArr as $value){
  157. $qrd =Db::name("qrd_info")->where(["sequenceNo"=>$value['orderCode']])->field("id,status,pay_fee,apay_fee,wpay_fee,pay_status,totalPrice")->findOrEmpty();
  158. if(empty($qrd)){
  159. Db::rollback();
  160. return error_show(1005,"未找到销售单数据");
  161. }
  162. if($qrd['pay_fee']<$value['cancel_fee']){
  163. Db::rollback();
  164. return error_show(1005,"销售单待付金额不足");
  165. }
  166. $update =[
  167. "apay_fee"=>$qrd['apay_fee']+$value['cancel_fee'],
  168. "pay_fee"=>$qrd['pay_fee']-$value['cancel_fee'],
  169. "paytime"=>date("Y-m-d H:i:s"),
  170. "pay_status"=>$qrd['wpay_fee']==0 && ($qrd['pay_fee']-$value['cancel_fee'])==0 ? 3:2,
  171. "updatetime"=>date("Y-m-d H:i:s")
  172. ];
  173. $qrdup =Db::name("qrd_info")->where($qrd)->update($update);
  174. if($qrdup==false){
  175. Db::rollback();
  176. return error_show(1005,"销售单更新失败");
  177. }
  178. $asscup =[
  179. "status"=>2,
  180. "assoc_time"=>date("Y-m-d H:i:s"),
  181. "updatetime"=>date("Y-m-d H:i:s")
  182. ];
  183. $assc=Db::name("assoc")->where($value)->update($asscup);
  184. if($assc==false){
  185. Db::rollback();
  186. return error_show(1005,"销售单更新失败");
  187. }
  188. }
  189. }
  190. }
  191. if($status==3 || $status==5){
  192. $qrdArr=Db::name("assoc")->where(["viceCode"=>$logNo,"status"=>1,"is_del"=>0])->select()->toArray();
  193. if(!empty($qrdArr)){
  194. foreach ($qrdArr as $value){
  195. $qrd =Db::name("qrd_info")->where(["sequenceNo"=>$value['orderCode']])->field("id,status,pay_fee,apay_fee,wpay_fee,pay_status,totalPrice")->findOrEmpty();
  196. if(empty($qrd)){
  197. Db::rollback();
  198. return error_show(1005,"未找到销售单数据");
  199. }
  200. if($qrd['pay_fee']<$value['cancel_fee']){
  201. Db::rollback();
  202. return error_show(1005,"销售单待付金额不足");
  203. }
  204. $update =[
  205. "wpay_fee"=>$qrd['wpay_fee']+$value['cancel_fee'],
  206. "pay_fee"=>$qrd['pay_fee']-$value['cancel_fee'],
  207. "pay_status"=>$qrd['apay_fee']==0 && ($qrd['pay_fee']-$value['cancel_fee'])==0 ? 1:2,
  208. "status"=>$qrd['apay_fee']==0 && ($qrd['pay_fee']-$value['cancel_fee'])==0 ?0:1,
  209. "updatetime"=>date("Y-m-d H:i:s")
  210. ];
  211. $qrdup =Db::name("qrd_info")->where($qrd)->update($update);
  212. if($qrdup==false){
  213. Db::rollback();
  214. return error_show(1005,"销售单更新失败");
  215. }
  216. $asscup =[
  217. "status"=>3,
  218. "updatetime"=>date("Y-m-d H:i:s")
  219. ];
  220. $assc=Db::name("assoc")->where($value)->update($asscup);
  221. if($assc==false){
  222. Db::rollback();
  223. return error_show(1005,"销售单更新失败");
  224. }
  225. $report=ReportCode::where(["qrdNo"=>$value['orderCode']])->find();
  226. if($report)$report->rmField("logNo",$logNo);
  227. }
  228. }
  229. if($trade['used_fee']<$logingo['total_fee']){
  230. Db::rollback();
  231. return error_show(1005,"资金信息已认领金额不足");
  232. }
  233. $tradup =[
  234. "used_fee"=>$trade['used_fee']-$logingo['total_fee'],
  235. "balance"=>$trade['balance']+$logingo['total_fee'],
  236. "status"=>($trade['used_fee']-$logingo['total_fee'])==0 ? 1 :2,
  237. "updatetime"=>date("Y-m-d H:i:s")
  238. ];
  239. $updaT=Db::name("trade")->where($trade)->update($tradup);
  240. if($updaT==false){
  241. Db::rollback();
  242. return error_show(1005,"资金信息更新失败");
  243. }
  244. }
  245. Db::commit();
  246. return app_show(0,"审核成功");
  247. }
  248. Db::rollback();
  249. return error_show(1004,"审核失败");
  250. }catch (\Exception $e){
  251. Db::rollback();
  252. return error_show(1004,$e->getMessage());
  253. }
  254. }
  255. //资金信息列表
  256. public function list(){
  257. $condition=[];
  258. $page=isset($this->post['page'])&&$this->post['page']!="" ? intval($this->post['page']) :1;
  259. $size=isset($this->post['size'])&&$this->post['size']!="" ? intval($this->post['size']) :15;
  260. $name =isset($this->post['name'])&&$this->post['name']!=""? trim($this->post['name']):"";
  261. $bank =isset($this->post['bank'])&&$this->post['bank']!=""? trim($this->post['bank']):"";
  262. $start =isset($this->post['start'])&&$this->post['start']!=""? trim($this->post['start']):"";
  263. $end =isset($this->post['end'])&&$this->post['end']!=""? trim($this->post['end']):"";
  264. $status =isset($this->post['status'])&&$this->post['status']!==""? intval($this->post['status']):"";
  265. $tradNo =isset($this->post['tradNo'])&&$this->post['tradNo']!=""? trim($this->post['tradNo']):"";
  266. $companyNo =isset($this->post['companyNo'])&&$this->post['companyNo']!=""? trim($this->post['companyNo']):"";
  267. $used_lower =isset($this->post['userd_lower'])&&$this->post['userd_lower']!==""? floor($this->post['userd_lower']) :"0";
  268. $used_upper =isset($this->post['used_upper'])&&$this->post['used_upper']!==""? floor($this->post['used_upper']):"";
  269. if($used_lower!=="")$condition[]=["used_fee",">=",$used_lower];
  270. if($used_upper!=="")$condition[]=["used_fee","<=",$used_upper];
  271. $total_lower =isset($this->post['total_lower'])&&$this->post['total_lower']!==""? floor($this->post['total_lower']) :"0";
  272. $total_upper =isset($this->post['total_upper'])&&$this->post['total_upper']!==""? floor($this->post['total_upper']):"";
  273. if($total_lower!=="")$condition[]=["total_fee",">=",$total_lower];
  274. if($total_upper!=="")$condition[]=["total_fee","<=",$total_upper];
  275. if($companyNo!==""){
  276. $condition[]=["companyNo","=",$companyNo];
  277. }
  278. $relaComNo= isset($this->post['relaComNo'])&&$this->post['relaComNo']!="" ? trim($this->post['relaComNo']) :"";
  279. if($relaComNo!=""){
  280. $condition[]=["companyNo","=",$relaComNo];
  281. }
  282. if($tradNo!==""){
  283. $condition[]=["tradNo","like","%$tradNo%"];
  284. }
  285. if($bank!=""){
  286. $condition[]=["trade_bank","like","%$bank%"];
  287. }
  288. if($name!=""){
  289. $condition[]=["trade_out","like","%$name%"];
  290. }
  291. if($start!=""){
  292. $condition[]=["trade_time",">=",$start." 00:00:00"];
  293. }
  294. if($end!=""){
  295. $condition[]=["trade_time","<=",$end." 23:59:59"];
  296. }
  297. if($status!==""){
  298. $condition[]=["status","=",$status];
  299. }
  300. $count=Db::name("trade")->where($condition)->count();
  301. $total=ceil($count/$size);
  302. $page=$page>$total? intval($total):$page;
  303. $list = Db::name("trade")
  304. ->where($condition)
  305. ->page($page, $size)
  306. ->order(['addtime' => 'desc', 'id' => 'desc'])
  307. ->select()
  308. ->toArray();
  309. foreach ($list as &$value) {
  310. $data = Db::name("trade_pool")
  311. ->alias("a")
  312. ->leftJoin("assoc b", "a.logNo=b.viceCode")
  313. ->leftJoin("qrd_info c", "b.orderCode=c.sequenceNo")
  314. ->where(["a.tradNo" => $value['tradNo'], "a.is_del" => 0])
  315. ->order(['a.addtime' => 'desc', 'a.id' => 'desc'])
  316. ->field("a.*,b.orderCode,c.cxCode,c.qrdSource,goodNo,goodName,qrdType,c.ownerName,c.ownerid,c.department,c.poCode,c.customerName,c.customerNo")
  317. ->select()
  318. ->toArray();
  319. $value['child'] = $data;
  320. $value['companyName'] = Db::name("supplier_info")->where(["code" => $value['companyNo']])->value("name", "");
  321. }
  322. return app_show(0,"获取成功",["list"=>$list,"count"=>$count]);
  323. }
  324. // 认领资金列表明细
  325. public function tradeList(){
  326. $page=isset($this->post['page'])&&$this->post['page']!="" ? intval($this->post['page']) :1;
  327. $size=isset($this->post['size'])&&$this->post['size']!="" ? intval($this->post['size']) :15;
  328. $condition =[["a.is_del","=",0],["b.is_del","=",0],["c.is_del","=",0],["d.is_del","=",0]];
  329. $roleid = $this->roleid;
  330. $check = checkRole($roleid,'87');
  331. if($check){
  332. $condition[]=["a.apply_id","=",$this->uid];
  333. }
  334. $platform_type =isset($this->post['platform_type'])&&$this->post['platform_type']!==""? intval($this->post['platform_type']):"";
  335. $name =isset($this->post['name'])&&$this->post['name']!=""? trim($this->post['name']):"";
  336. $bank =isset($this->post['bank'])&&$this->post['bank']!=""? trim($this->post['bank']):"";
  337. $start =isset($this->post['start'])&&$this->post['start']!=""? trim($this->post['start']):"";
  338. $end =isset($this->post['end'])&&$this->post['end']!=""? trim($this->post['end']):"";
  339. $status =isset($this->post['status'])&&$this->post['status']!==""? intval($this->post['status']):"";
  340. $apply_id =isset($this->post['apply_id'])&&$this->post['apply_id']!=""? intval($this->post['apply_id']):"";
  341. $apply_name =isset($this->post['apply_name'])&&$this->post['apply_name']!=""? trim($this->post['apply_name']):"";
  342. $tradNo =isset($this->post['tradNo'])&&$this->post['tradNo']!=""? trim($this->post['tradNo']):"";
  343. $orderCode =isset($this->post['orderCode'])&&$this->post['orderCode']!=''?trim($this->post['orderCode']):"";
  344. $companyNo =isset($this->post['companyNo'])&&$this->post['companyNo']!=""? trim($this->post['companyNo']):"";
  345. if($companyNo!==""){
  346. $condition[]=["a.companyNo","=",$companyNo];
  347. }
  348. if($platform_type!==""){
  349. $condition[]=["a.platform_type","=",$platform_type];
  350. }
  351. $relaComNo= isset($this->post['relaComNo'])&&$this->post['relaComNo']!="" ? trim($this->post['relaComNo']) :"";
  352. if($relaComNo!=""){
  353. $condition[]=["a.companyNo","=",$relaComNo];
  354. }
  355. if($tradNo!==""){
  356. $condition[]=["a.tradNo","like","%$tradNo%"];
  357. }
  358. if($orderCode!==""){
  359. $condition[]=["d.orderCode","like","%$orderCode%"];
  360. }
  361. $cxCode= isset($this->post['cxCode'])&&$this->post['cxCode']!="" ? trim($this->post['cxCode']) :"";
  362. if($cxCode!=""){
  363. $condition[]=["c.cxCode","like","%$cxCode%"];
  364. }
  365. $logNo =isset($this->post['logNo'])&&$this->post['logNo']!=""? trim($this->post['logNo']):"";
  366. if($logNo!==""){
  367. $condition[]=["a.logNo","like","%$logNo%"];
  368. }
  369. if($bank!=""){
  370. $condition[]=["b.trade_bank","like","%$bank%"];
  371. }
  372. if($name!=""){
  373. $condition[]=["b.trade_out","like","%$name%"];
  374. }
  375. if($start!=""){
  376. $condition[]=["a.addtime",">=",date("Y-m-d 00:00:00",strtotime($start))];
  377. }
  378. if($end!=""){
  379. $condition[]=["a.addtime","<=",date("Y-m-d 23:59:59",strtotime($start))];
  380. }
  381. if($status!==""){
  382. $condition[]=["a.status","=",$status];
  383. }
  384. if($apply_id!==""){
  385. $condition[]=["a.apply_id","=",$apply_id];
  386. }
  387. if($apply_name!==""){
  388. $condition[]=["a.apply_name","like","%$apply_name%"];
  389. }
  390. $count=Db::name("trade_pool")->alias("a")
  391. ->leftJoin("trade b","a.tradNo=b.tradNo")
  392. ->leftJoin("assoc d","a.logNo=d.viceCode AND d.type = 2")
  393. ->leftJoin("qrd_info c","d.orderCode=c.sequenceNo")
  394. ->where($condition)->count();
  395. $total=ceil($count/$size);
  396. $page=$page>$total? intval($total):$page;
  397. $list =Db::name("trade_pool")->alias("a")
  398. ->leftJoin("trade b","a.tradNo=b.tradNo")
  399. ->leftJoin("assoc d","a.logNo=d.viceCode AND d.type = 2")
  400. ->leftJoin("qrd_info c","d.orderCode=c.sequenceNo")
  401. ->where($condition)
  402. ->page($page,$size)
  403. ->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,
  404. 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")
  405. ->order("a.addtime desc")->select();
  406. return app_show(0,"获取成功",["list"=>$list,"count"=>$count]);
  407. }
  408. // 获取资金下面的认领信息
  409. public function tradeQuery(){
  410. $tradNo =isset($this->post['tradNo'])&&$this->post['tradNo']!=""? trim($this->post['tradNo']):"";
  411. $status =isset($this->post['status'])&&$this->post['status']!=""? intval($this->post['status']):"";
  412. $apply_id =isset($this->post['apply_id'])&&$this->post['apply_id']!=""? intval($this->post['apply_id']):"";
  413. $apply_name =isset($this->post['apply_name'])&&$this->post['apply_name']!=""? trim($this->post['apply_name']):"";
  414. $companyNo =isset($this->post['companyNo'])&&$this->post['companyNo']!=""? trim($this->post['companyNo']):"";
  415. $platform_type =isset($this->post['platform_type'])&&$this->post['platform_type']!==""? intval($this->post['platform_type']):"";
  416. if($companyNo!==""){
  417. $condition[]=["companyNo","=",$companyNo];
  418. }
  419. if($platform_type!==""){
  420. $condition[]=["platform_type","=",$platform_type];
  421. }
  422. $relaComNo= isset($post['relaComNo'])&&$post['relaComNo']!="" ? trim($post['relaComNo']) :"";
  423. if($relaComNo!=""){
  424. $condition[]=["companyNo","=",$relaComNo];
  425. }
  426. $condition=[["is_del","=",0]];
  427. if($apply_id!==""){
  428. $condition[]=["apply_id","=",$apply_id];
  429. }
  430. if($apply_name!==""){
  431. $condition[]=["apply_name","like","%$apply_name%"];
  432. }
  433. if($status!==""){
  434. $condition[]=["status","=",$status];
  435. }
  436. if($tradNo!==""){
  437. $condition[]=["tradNo","like","%$tradNo%"];
  438. }
  439. $list =Db::name("trade_pool")->where($condition)->order("addtime desc")->select();
  440. return app_show(0,"获取成功",$list);
  441. }
  442. //资金详情
  443. public function tradeInfo(){
  444. $tradNo =isset($this->post['tradNo'])&&$this->post['tradNo']!=""? trim($this->post['tradNo']):"";
  445. if($tradNo==""){
  446. return error_show(1004,"参数 tradNo 不能为空");
  447. }
  448. $tradinfo = Db::name("trade")->where(["tradNo"=>$tradNo,"is_del"=>0])->find();
  449. if($tradinfo==false){
  450. return error_show(1004,"资金信息未找到");
  451. }
  452. return app_show(0,"获取成功",$tradinfo);
  453. }
  454. //资金认领详情
  455. public function logInfo(){
  456. $logNo =isset($this->post['logNo'])&&$this->post['logNo']!=""? trim($this->post['logNo']):"";
  457. if($logNo==""){
  458. return error_show(1004,"参数 logNo 不能为空");
  459. }
  460. $tradinfo = Db::name("trade_pool")->where(["logNo"=>$logNo,"is_del"=>0])->find();
  461. if($tradinfo==false){
  462. return error_show(1004,"资金信息未找到");
  463. }
  464. $trade = Db::name("trade")->where(["tradNo"=>$tradinfo['tradNo'],"is_del"=>0])->find();
  465. $tradinfo['trade_out']= $trade['trade_out']??"";
  466. $tradinfo['trade_in']= $trade['trade_in']??"";
  467. $tradinfo['trade_bank']= $trade['trade_bank']??"";
  468. $tradinfo['customerName']= Db::name("customer_info")->where(["companyNo"=>$tradinfo['customerNo']])->value("companyName","");
  469. $tradinfo['companyNo']= $trade['companyNo']??"";
  470. $tradinfo['total_fee']= $trade['total_fee']??"";
  471. $tradinfo['balance']= $trade['balance']??"";
  472. $tradinfo['used_fee']= $trade['used_fee']??"";
  473. $orderinfo = Db::name("assoc")->alias("a")->leftJoin("qrd_info c","a.orderCode=c.sequenceNo")
  474. ->where(["a.viceCode"=>$logNo,"a.is_del"=>0,"a.status"=>[1,2,3]])
  475. ->order("a.addtime desc")
  476. ->field("c.*,a.cancel_fee")
  477. ->findOrEmpty();
  478. $tradinfo['orderinfo']=$orderinfo;
  479. return app_show(0,"获取成功",$tradinfo);
  480. }
  481. //认领资金退回或退款 type 1 退款 2 解除资金认领
  482. public function ReturnPay(){
  483. $logNo = isset($this->post['logNo'])&&$this->post['logNo']!="" ? trim($this->post['logNo']):"";
  484. if($logNo==''){
  485. return error_show(1004,"参数 logNo 不能为空");
  486. }
  487. $type = isset($this->post['type'])&&$this->post['type']!="" ? intval($this->post['type']):"";
  488. if($type==""){
  489. return error_show(1004,"参数 type 不能为空");
  490. }
  491. $loginfo = Db::name("trade_pool")->where(["logNo"=>$logNo,"is_del"=>0])->find();
  492. if($loginfo==false){
  493. return error_show(1004,"认领资金信息未找到");
  494. }
  495. if($loginfo['status']==2 && $type==1)return error_show(1004,"认领资金信息审核已通过");
  496. if($loginfo['status']!=2 && $type==2)return error_show(1004,"认领资金信息未审核通过");
  497. $tradinfo = Db::name("trade")->where(["tradNo"=>$loginfo['tradNo'],"is_del"=>0])->find();
  498. if($tradinfo==false){
  499. return error_show(1004,"资金信息未找到");
  500. }
  501. $isT= Db::name("trade_return")->where(["logNo"=>$logNo,"tradNo"=>$loginfo['tradNo'],"status"=>[0,1]])
  502. ->findOrEmpty();
  503. if(!empty($isT)) return error_show(1004,"资金退回流程已存在");
  504. $reason = isset($this->post['return_reason'])&&$this->post['return_reason']!="" ? trim($this->post['return_reason']):"";
  505. $remark = isset($this->post['remark'])&&$this->post['remark']!="" ? trim($this->post['remark']):"";
  506. $returnCode =makeNo("RTA");
  507. $data=[
  508. "returnCode"=>$returnCode,
  509. "logNo"=>$logNo,
  510. "companyNo"=>$tradinfo['companyNo'],
  511. "tradNo"=>$loginfo['tradNo'],
  512. "return_img"=>'',
  513. "type"=>$type,
  514. "apply_id"=>$this->uid,
  515. "apply_name"=>$this->uname,
  516. "return_reason"=>$reason,
  517. "remark"=>$remark,
  518. "addtime"=>date("Y-m-d H:i:s"),
  519. "updatetime"=>date("Y-m-d H:i:s")
  520. ];
  521. $in = Db::name("trade_return")->insert($data);
  522. if($in){
  523. return app_show(0,"申请新建成功");
  524. }else{
  525. return error_show(1004,"申请新建失败");
  526. }
  527. }
  528. /**退款列表
  529. * //page size tradNo logNo apply_id apply_name type status returnCode
  530. * @return \think\response\Json|void
  531. * @throws \think\db\exception\DataNotFoundException
  532. * @throws \think\db\exception\DbException
  533. * @throws \think\db\exception\ModelNotFoundException
  534. */
  535. public function returnList(){
  536. $condition=[["a.is_del","=",0]];
  537. $roleid = $this->roleid;
  538. $check = checkRole($roleid,'90');
  539. if($check){
  540. $condition[]=["a.apply_id","=",$this->uid];
  541. }
  542. $page=isset($this->post['page'])&&$this->post['page']!='' ? intval($this->post['page']):1;
  543. $size=isset($this->post['size'])&&$this->post['size']!='' ? intval($this->post['size']):15;
  544. $tradNo =isset($this->post['tradNo'])&&$this->post['tradNo']!=''?trim($this->post['tradNo']):"";
  545. $logNo =isset($this->post['logNo'])&&$this->post['logNo']!=''?trim($this->post['logNo']):"";
  546. $apply_id =isset($this->post['apply_id'])&&$this->post['apply_id']!=''?intval($this->post['apply_id']):"";
  547. $apply_name =isset($this->post['apply_name'])&&$this->post['apply_name']!=''?trim($this->post['apply_name']):"";
  548. $type =isset($this->post['type'])&&$this->post['type']!=''?intval($this->post['type']):"";
  549. $status =isset($this->post['status'])&&$this->post['status']!==''?intval($this->post['status']):"";
  550. $returnCode =isset($this->post['returnCode'])&&$this->post['returnCode']!=''?trim($this->post['returnCode']):"";
  551. $orderCode =isset($this->post['orderCode'])&&$this->post['orderCode']!=''?trim($this->post['orderCode']):"";
  552. $companyNo =isset($this->post['companyNo'])&&$this->post['companyNo']!=""? trim($this->post['companyNo']):"";
  553. if($companyNo!==""){
  554. $condition[]=["a.companyNo","=",$companyNo];
  555. }
  556. $relaComNo= isset($this->post['relaComNo'])&&$this->post['relaComNo']!="" ? trim($this->post['relaComNo']) :"";
  557. if($relaComNo!=""){
  558. $condition[]=["a.companyNo","=",$relaComNo];
  559. }
  560. $condition=[["a.is_del","=",0]];
  561. if($tradNo!=''){
  562. $condition[]=["a.tradNo","like","%$tradNo%"];
  563. }
  564. if($orderCode!=''){
  565. $condition[]=["b.orderCode","like","%$orderCode%"];
  566. }
  567. if($logNo!=''){
  568. $condition[]=["a.logNo","like","%$logNo%"];
  569. }
  570. if($apply_id!=''){
  571. $condition[]=["a.apply_id","=",$apply_id];
  572. }
  573. if($apply_name!=''){
  574. $condition[]=["a.apply_name","like","%$apply_name%"];
  575. }
  576. if($returnCode!=''){
  577. $condition[]=["a.returnCode","like","%$returnCode%"];
  578. }
  579. if($type!=''){
  580. $condition[]=["a.type","=",$type];
  581. }
  582. if($status!==''){
  583. $condition[]=["a.status","=",$status];
  584. }
  585. $count =Db::name("trade_return")->alias("a")
  586. ->leftJoin("assoc b","a.logNo=b.viceCode")
  587. ->leftJoin("qrd_info c","c.sequenceNo=b.orderCode")
  588. ->where($condition)->count();
  589. $total=ceil($count/$size);
  590. $page = $page>$total ? intval($total):$page;
  591. $list=Db::name("trade_return")->alias("a")->leftJoin("assoc b","a.logNo=b.viceCode")
  592. ->leftJoin("qrd_info c","c.sequenceNo=b.orderCode")
  593. ->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")
  594. ->where($condition)->page($page,$size)->order("a.addtime desc")->select();
  595. return app_show(0,"获取成功",["list"=>$list,"count"=>$count]);
  596. }
  597. //退款状态审核 0 待审核 1 财务审核 2 财务驳回
  598. public function returnStatus(){
  599. $returnCode=isset($this->post['returnCode'])&&$this->post['returnCode']!=''?trim($this->post['returnCode']):"";
  600. if($returnCode==''){
  601. return error_show(1004,"参数 returnCode 不能为空");
  602. }
  603. $returninfo =Db::name("trade_return")->where(["returnCode"=>$returnCode,"is_del"=>0])->find();
  604. if($returninfo==false){
  605. return error_show(1004,"退款申请数据未找到");
  606. }
  607. $status=isset($this->post['status']) &&$this->post['status']!=''?intval($this->post['status']) : '';
  608. if($status==''){
  609. return error_show(1004,"参数 status 不能为空");
  610. }
  611. $return_img = isset($this->post['return_img'])&&$this->post['return_img']!=''?trim($this->post['return_img']):"";
  612. $remark = isset($this->post['remark'])&&$this->post['remark']!=''?trim($this->post['remark']):"";
  613. $loginfo = Db::name("trade_pool")->where(["logNo"=>$returninfo['logNo'],"is_del"=>0])->find();
  614. if($loginfo==false){
  615. return error_show(1004,"认领资金信息未找到");
  616. }
  617. if($loginfo['status']!=2)return error_show(1004,"认领资金信息审核未通过");
  618. Db::startTrans();
  619. try{
  620. $update=["status"=>$status,"return_img"=>$return_img,"remark"=>$remark,"updatetime"=>date("Y-m-d H:i:s")];
  621. $up =Db::name("trade_return")->where($returninfo)->update($update);
  622. if($up){
  623. if($status==1){
  624. $qrdArr=Db::name("assoc")->where(["viceCode"=>$returninfo['logNo'],"status"=>[1,2],"is_del"=>0])
  625. ->select()->toArray();
  626. if(!empty($qrdArr)){
  627. foreach ($qrdArr as $value){
  628. $qrd =Db::name("qrd_info")->where(["sequenceNo"=>$value['orderCode']])->field("id,status,pay_fee,apay_fee,wpay_fee,pay_status,totalPrice")->findOrEmpty();
  629. if(empty($qrd)){
  630. Db::rollback();
  631. return error_show(1005,"未找到销售单数据");
  632. }
  633. if($returninfo['type']==2){
  634. if($qrd['apay_fee']<$value['cancel_fee']){
  635. Db::rollback();
  636. return error_show(1005,"销售单已付金额不足");
  637. }
  638. $update =[
  639. "wpay_fee"=>$qrd['wpay_fee']+$value['cancel_fee'],
  640. "apay_fee"=>$qrd['apay_fee']-$value['cancel_fee'],
  641. "pay_status"=>($qrd['apay_fee']-$value['cancel_fee'])==0 && $qrd['pay_fee']==0 ? 1:2,
  642. "status"=>$qrd['pay_fee']==0 && ($qrd['apay_fee']-$value['cancel_fee'])==0 ?0:1,
  643. "updatetime"=>date("Y-m-d H:i:s")
  644. ];
  645. }else{
  646. if($qrd['pay_fee']<$value['cancel_fee']){
  647. Db::rollback();
  648. return error_show(1005,"销售单付款中金额不足");
  649. }
  650. $update =[
  651. "wpay_fee"=>$qrd['wpay_fee']+$value['cancel_fee'],
  652. "pay_fee"=>$qrd['pay_fee']-$value['cancel_fee'],
  653. "pay_status"=>($qrd['pay_fee']-$value['cancel_fee'])==0 && $qrd['apay_fee']==0 ? 1:2,
  654. "status"=>$qrd['apay_fee']==0 && ($qrd['pay_fee']-$value['cancel_fee'])==0 ?0:1,
  655. "updatetime"=>date("Y-m-d H:i:s")
  656. ];
  657. }
  658. $qrdup =Db::name("qrd_info")->where($qrd)->update($update);
  659. if($qrdup==false){
  660. Db::rollback();
  661. return error_show(1005,"销售单更新失败");
  662. }
  663. $asscup =[
  664. "status"=>3,
  665. "updatetime"=>date("Y-m-d H:i:s")
  666. ];
  667. $assc=Db::name("assoc")->where($value)->update($asscup);
  668. if($assc==false){
  669. Db::rollback();
  670. return error_show(1005,"销售单更新失败");
  671. }
  672. $report=ReportCode::where(["qrdNo"=>$value['orderCode']])->find();
  673. if($report)$report->setField("returnTrad",$returnCode);
  674. }
  675. }
  676. $logup =["status"=>$returninfo['type']==1?5:4,"updatetime"=>date("Y-m-d H:i:s")];
  677. $upde =Db::name("trade_pool")->where($loginfo)->update($logup);
  678. if($upde==false){
  679. Db::rollback();
  680. return error_show(1005,"资金认领信息更新失败");
  681. }
  682. // if($returninfo['type']==1){
  683. $trade =Db::name("trade")->where(["tradNo"=>$returninfo['tradNo'],"is_del"=>0])->find();
  684. if($trade==false){
  685. Db::rollback();
  686. return error_show(1005,"资金信息未找到");
  687. }
  688. if($trade['used_fee']<$loginfo['total_fee']){
  689. Db::rollback();
  690. return error_show(1005,"资金信息已认领金额不足");
  691. }
  692. $tradup =[
  693. "used_fee"=>$trade['used_fee']-$loginfo['total_fee'],
  694. "balance"=>$trade['balance']+$loginfo['total_fee'],
  695. "status"=>($trade['used_fee']-$loginfo['total_fee'])==0 ?1 :2,
  696. "updatetime"=>date("Y-m-d H:i:s")
  697. ];
  698. $updaT=Db::name("trade")->where($trade)->update($tradup);
  699. if($updaT==false){
  700. Db::rollback();
  701. return error_show(1005,"资金信息更新失败");
  702. }
  703. // }
  704. }
  705. Db::commit();
  706. return app_show(0,"退款申请审核成功");
  707. }
  708. Db::rollback();
  709. return error_show(1004,'审核状态失败');
  710. }catch (\Exception $e){
  711. Db::rollback();
  712. return error_show(1004,$e->getMessage());
  713. }
  714. }
  715. //退款申请详情
  716. public function returnInfo(){
  717. $returnCode=isset($this->post['returnCode'])&&$this->post['returnCode']!=''?trim($this->post['returnCode']):"";
  718. if($returnCode==''){
  719. return error_show(1004,"参数 returnCode 不能为空");
  720. }
  721. $returninfo =Db::name("trade_return")->where(["returnCode"=>$returnCode,"is_del"=>0])->find();
  722. if($returninfo==false){
  723. return error_show(1004,"退款申请数据未找到");
  724. }
  725. $trade = Db::name("trade")->where(["tradNo"=>$returninfo['tradNo'],"is_del"=>0])->find();
  726. $returninfo['trade_out']= $trade['trade_out']??"";
  727. $returninfo['trade_in']= $trade['trade_in']??"";
  728. $returninfo['trade_bank']= $trade['trade_bank']??"";
  729. $returninfo['trade_time']= $trade['trade_time']??"";
  730. $returninfo['customerNo']= $trade['customerNo']??"";
  731. $returninfo['companyNo']= $trade['companyNo']??"";
  732. $returninfo['total_fee']= $trade['total_fee']??"";
  733. $returninfo['balance']= $trade['balance']??"";
  734. $returninfo['used_fee']= $trade['used_fee']??"";
  735. $pool =Db::name("trade_pool")->where(["logNo"=>$returninfo['logNo'],"is_del"=>0])->find();
  736. $returninfo['log_total_fee']= $pool['total_fee']??"";
  737. $returninfo['log_apply_id']= $pool['apply_id']??"";
  738. $returninfo['log_apply_name']= $pool['apply_name']??"";
  739. $orderinfo = Db::name("assoc")->alias("a")->leftJoin("qrd_info c","a.orderCode=c.sequenceNo")
  740. ->where(["a.viceCode"=>$returninfo['logNo'],"a.is_del"=>0,"a.status"=>[1,2]])
  741. ->order("a.addtime desc")
  742. ->field("c.*")
  743. ->findOrEmpty();
  744. $returninfo['orderinfo'] = $orderinfo;
  745. return app_show(0,"获取成功",$returninfo);
  746. }
  747. /**
  748. * 显示创建资源表单页.
  749. *
  750. * @return \think\Response
  751. */
  752. public function importTrade()
  753. {
  754. $files = $this->request->file("excel");
  755. $data = upload($files,$files->getOriginalExtension());
  756. if($data['code']!=0){
  757. return error_show(1004,$data['msg']);
  758. }
  759. $trade = $data['data'];
  760. $list = [];
  761. foreach ( $trade as $key => $value) {
  762. if (!isset($value[0]) || $value[0] == '') {
  763. //return ['code' => 1003, "msg" => '交易时间不能为空'];
  764. return error_show( 1003, '第'.($key+1).'行收款方公司编号不能为空!');
  765. }
  766. if (!isset($value[4]) || $value[4] == '') {
  767. // return ['code' => 1003, "msg" => '交易行名不能为空'];
  768. return error_show( 1003, '第'.($key+1).'行收入金额不能为空!');
  769. }
  770. if (!isset($value[8]) || $value[8] == '') {
  771. // return ['code' => 1003, "msg" => '对方账户不能为空'];
  772. return error_show( 1003, '第'.($key+1).'行对方账户不能为空!');
  773. }
  774. if (!isset($value[9]) || $value[9] == '') {
  775. // return ['code' => 1003, "msg" => '对方户名不能为空'];
  776. return error_show( 1003, '第'.($key+1).'行对方户名不能为空!');
  777. }
  778. if ($key == 0) {
  779. if ($value[0] != '收款方公司编号') {
  780. // return ['code' => 1003, "msg" => '模板第一列为必须为交易时间!'];
  781. return error_show( 1003, '模板第一列为必须为收款方公司编号!');
  782. }
  783. if ($value[4] != '收入金额') {
  784. // return ['code' => 1003, "msg" => '模板第二列为必须为收入金额!'];
  785. return error_show( 1003, '模板第五列为必须为收入金额!');
  786. }
  787. if ($value[6] != '交易行名') {
  788. //return ['code' => 1003, "msg" => '模板第五列为必须为交易行名!'];
  789. return error_show( 1003, '模板第七列为必须为交易行名!');
  790. }
  791. if ($value[8] != '对方账号') {
  792. // return ['code' => 1003, "msg" => '模板第七列为必须为对方账号!'];
  793. return error_show( 1003, '模板第九列为必须为对方账号!');
  794. }
  795. if ($value[9] != '对方户名') {
  796. // return ['code' => 1003, "msg" => '模板第八列为必须为对方户名!'];
  797. return error_show( 1003, '模板第十列为必须为对方户名!');
  798. }
  799. continue;
  800. }
  801. $company =Db::name("company_info")->where(["companyNo"=>$value[0]])->findOrEmpty();
  802. if(empty($company)){
  803. return error_show( 1003, "业务公司编号不存在{$value[0]}");
  804. }
  805. $total_fee = $value[4];
  806. $type = 0;
  807. $time = explode(" ", $value[3]);
  808. $list[$key]["tradeTime"] = date("Y-m-d", strtotime($time[0])) . " " . (isset($time[1]) ? $time[1] : "00:00:00");
  809. $list[$key]["trade_fee"] = str_replace(",","",$total_fee);
  810. $list[$key]["trade_bank"] = $value[6];
  811. $list[$key]["trade_account"] = $value[8];
  812. $list[$key]["trade_out"] = $value[9];
  813. $list[$key]["trade_in"] = $value[1]??$company['company_name'];
  814. $list[$key]["trade_type"] = $type;
  815. $list[$key]["trade_used"] = isset($value[10]) ? $value[10] : "";
  816. $list[$key]["trade_remark"] = '';
  817. $list[$key]["companyNo"] = $value[0];
  818. $list[$key]["trade_in_account"] = $value[2]??"";
  819. }
  820. if(empty($list)){
  821. return error_show( 1003, '导入数据不能为空!');
  822. }
  823. Db::startTrans();
  824. try{
  825. $tra=[];
  826. foreach ($list as $value) {
  827. $temp = [];
  828. $temp["tradNo"] = makeStr('S');
  829. $temp['trade_time'] = $value['tradeTime'];
  830. $temp['total_fee'] = $value['trade_fee'];
  831. $temp['trade_bank'] = $value['trade_bank'];
  832. $temp['trade_account'] = $value['trade_account'];
  833. $temp['trade_type'] =$value['trade_type'];
  834. $temp['trade_out'] = $value['trade_out'];
  835. $temp['trade_in'] = $value['trade_in'];
  836. $temp['trade_in_account'] = $value['trade_in_account'];
  837. $temp['companyNo'] = $value['companyNo'];
  838. $temp['trade_used'] = $value['trade_used'];
  839. $temp['trade_remark'] = $value['trade_remark'];
  840. $temp['balance'] =$value['trade_fee'];
  841. $temp['addtime'] = date("Y-m-d H:i:s");
  842. $temp['updatetime'] = date("Y-m-d H:i:s");
  843. $tra[]=$temp;
  844. }
  845. $list = Db::name('trade')->insertAll($tra);
  846. if($list==count($tra)){
  847. Db::commit();
  848. return app_show(0, "资金导入成功");
  849. }else{
  850. Db::rollback();
  851. return app_show(1004, "资金导入失败");
  852. }
  853. }catch (\Exception $e){
  854. Db::rollback();
  855. return app_show(1004, $e->getMessage());
  856. }
  857. }
  858. /**
  859. * 资金导入
  860. * @return \think\response\Json|void
  861. */
  862. public function importTradeByArr()
  863. {
  864. $trade =isset($this->post['data']) &&!empty($this->post['data'])? $this->post['data']:[];
  865. if(!is_array($trade) || empty($trade) ) return error_show( 1003,"参数 data 不能为空");
  866. $list = [];
  867. foreach ( $trade as $key => $value) {
  868. if (!isset($value[0]) || $value[0] == '') {
  869. //return ['code' => 1003, "msg" => '交易时间不能为空'];
  870. return error_show( 1003, '第'.($key+1).'行收款方公司编号不能为空!');
  871. }
  872. if (!isset($value[2]) || $value[2] == '') {
  873. //return ['code' => 1003, "msg" => '交易时间不能为空'];
  874. return error_show( 1003, '第'.($key+1).'行收款账户不能为空!');
  875. }
  876. if (!isset($value[3]) || $value[3] == '') {
  877. //return ['code' => 1003, "msg" => '交易时间不能为空'];
  878. return error_show( 1003, '第'.($key+1).'行交易时间不能为空!');
  879. }
  880. if (!isset($value[4]) || $value[4] == '') {
  881. // return ['code' => 1003, "msg" => '交易行名不能为空'];
  882. return error_show( 1003, '第'.($key+1).'行收入金额不能为空!');
  883. }
  884. if (!isset($value[6]) || $value[6] == '') {
  885. // return ['code' => 1003, "msg" => '交易行名不能为空'];
  886. return error_show( 1003, '第'.($key+1).'行交易银行名称不能为空!');
  887. }
  888. if (!isset($value[8]) || $value[8] == '') {
  889. // return ['code' => 1003, "msg" => '对方账户不能为空'];
  890. return error_show( 1003, '第'.($key+1).'行对方账户不能为空!');
  891. }
  892. if (!isset($value[9]) || $value[9] == '') {
  893. // return ['code' => 1003, "msg" => '对方户名不能为空'];
  894. return error_show( 1003, '第'.($key+1).'行对方户名不能为空!');
  895. }
  896. // if ($key == 0) {
  897. // if ($value[0] != '收款方公司编号') {
  898. // // return ['code' => 1003, "msg" => '模板第一列为必须为交易时间!'];
  899. // return error_show( 1003, '模板第一列为必须为收款方公司编号!');
  900. // }
  901. // if ($value[4] != '收入金额') {
  902. // // return ['code' => 1003, "msg" => '模板第二列为必须为收入金额!'];
  903. // return error_show( 1003, '模板第五列为必须为收入金额!');
  904. // }
  905. //
  906. // if ($value[6] != '交易行名') {
  907. // //return ['code' => 1003, "msg" => '模板第五列为必须为交易行名!'];
  908. // return error_show( 1003, '模板第七列为必须为交易行名!');
  909. // }
  910. // if ($value[8] != '对方账号') {
  911. // // return ['code' => 1003, "msg" => '模板第七列为必须为对方账号!'];
  912. // return error_show( 1003, '模板第九列为必须为对方账号!');
  913. // }
  914. // if ($value[9] != '对方户名') {
  915. // // return ['code' => 1003, "msg" => '模板第八列为必须为对方户名!'];
  916. // return error_show( 1003, '模板第十列为必须为对方户名!');
  917. // }
  918. // continue;
  919. // }
  920. $company =Db::name("company_info")->where(["companyNo"=>$value[0]])->findOrEmpty();
  921. if(empty($company)){
  922. return error_show( 1003, "业务公司编号不存在{$value[0]}");
  923. }
  924. $total_fee = $value[4];
  925. $type = 0;
  926. $time = explode(" ", $value[3]);
  927. $list[$key]["tradeTime"] = date("Y-m-d", strtotime($time[0])) . " " . (isset($time[1]) ? $time[1] : "00:00:00");
  928. $list[$key]["trade_fee"] = str_replace(",","",$total_fee);
  929. $list[$key]["trade_bank"] = $value[6];
  930. $list[$key]["trade_account"] = $value[8];
  931. $list[$key]["trade_out"] = $value[9];
  932. $list[$key]["trade_in"] = $value[1]??$company['company_name'];
  933. $list[$key]["trade_type"] = $type;
  934. $list[$key]["trade_used"] = isset($value[10]) ? $value[10] : "";
  935. $list[$key]["trade_remark"] = '';
  936. $list[$key]["companyNo"] = $value[0];
  937. $list[$key]["trade_in_account"] = $value[2]??"";
  938. }
  939. if(empty($list)){
  940. return error_show( 1003, '导入数据不能为空!');
  941. }
  942. Db::startTrans();
  943. try{
  944. $tra=[];
  945. $i=0;
  946. foreach ($list as $value) {
  947. $temp = [];
  948. $i++;
  949. $temp["tradNo"] = makeStr('S').str_pad($i,3,'0',STR_PAD_LEFT);
  950. $temp['trade_time'] = $value['tradeTime'];
  951. $temp['total_fee'] = $value['trade_fee'];
  952. $temp['trade_bank'] = $value['trade_bank'];
  953. $temp['trade_account'] = $value['trade_account'];
  954. $temp['trade_type'] =$value['trade_type'];
  955. $temp['trade_out'] = $value['trade_out'];
  956. $temp['trade_in'] = $value['trade_in'];
  957. $temp['trade_in_account'] = $value['trade_in_account'];
  958. $temp['companyNo'] = $value['companyNo'];
  959. $temp['trade_used'] = $value['trade_used'];
  960. $temp['trade_remark'] = $value['trade_remark'];
  961. $temp['balance'] =$value['trade_fee'];
  962. $temp['addtime'] = date("Y-m-d H:i:s");
  963. $temp['updatetime'] = date("Y-m-d H:i:s");
  964. $tra[]=$temp;
  965. }
  966. $list = Db::name('trade')->insertAll($tra);
  967. if($list==count($tra)){
  968. Db::commit();
  969. return app_show(0, "资金导入成功");
  970. }else{
  971. Db::rollback();
  972. return app_show(1004, "资金导入失败");
  973. }
  974. }catch (\Exception $e){
  975. Db::rollback();
  976. return app_show(1004, $e->getMessage());
  977. }
  978. }
  979. //批量认领资金,跟订单相关
  980. public function importTradeByBatchOrderCode()
  981. {
  982. $list = $this->request->post('list', [], 'trim');
  983. $val = Validate::rule(['list|导入数据' => 'require|array|max:100']);
  984. if (!$val->check(['list' => $list])) return error_show(1004, $val->getError());
  985. $company = $tradNos = $orderCodes = $assoc_insert_data = $OrderCodeToAssocNo = $OrderCodeToLogNo = [];
  986. $val_item = Validate::rule([
  987. 'companyNo|卖出方公司编号' => 'require|max:255',
  988. 'tradNo|资金编号' => 'require|max:255',
  989. 'orderCode|订单编号' => 'require|max:255',
  990. 'trad_fee|认领资金' => 'require|float|gt:0|max:999999999.99',
  991. ]);
  992. foreach ($list as $key => $value) {
  993. if (!$val_item->check($value)) return error_show(1004, $val_item->getError());
  994. if (!isset($company[$value['companyNo']])) $company[$value['companyNo']] = $value['companyNo'];
  995. }
  996. //判断绑定公司
  997. $companyNo = Db::name('user_role')
  998. ->where(['is_del' => 0, 'status' => 1, 'uid' => $this->uid])
  999. ->whereIn('companyNo', $company)
  1000. ->column('companyNo');
  1001. $tmp = array_diff($company, $companyNo);//二者取差集
  1002. if(!empty($tmp)) return error_show(1004,'不能操作以下公司数据:'.implode(',',$tmp));
  1003. Db::startTrans();
  1004. try {
  1005. $i=1;
  1006. //批量资金认领
  1007. foreach ($list as $key=>$value){
  1008. //资金明细判断
  1009. $i++;
  1010. $all_trade = Db::name('trade')
  1011. ->where('is_del', 0)
  1012. ->whereIn('status', [1, 2])
  1013. ->where('tradNo', $value['tradNo'])
  1014. ->field('id,balance,used_fee,companyNo,trade_time')
  1015. ->findOrEmpty();
  1016. if(empty($all_trade))throw new Exception("{$value['tradNo']}资金未找到数据");
  1017. if($all_trade['balance']<$value['trad_fee'])throw new Exception("{$value['tradNo']}资金余额不足");
  1018. //订单明细判断
  1019. $all_order=Db::name('qrd_info')
  1020. ->where('is_del', 0)
  1021. ->whereIn('sequenceNo',$value['orderCode'])
  1022. ->field('id,customerNo,status,pay_fee,apay_fee,wpay_fee,pay_status,totalPrice,platform_type')
  1023. ->findOrEmpty();
  1024. if(empty($all_order))throw new Exception("{$value['orderCode']}销售单未找到数据");
  1025. if($all_order['wpay_fee']<$value['trad_fee'])throw new Exception("{$value['orderCode']}销售单未付款金额不足核销金额");
  1026. $OrderCodeToAssocNo = substr(makeNo('AS'), 0, -3).str_pad($i,3,'0',STR_PAD_LEFT);
  1027. $OrderCodeToLogNo = substr(makeNo('TRC'), 0, -3).str_pad($i,3,'0',STR_PAD_LEFT);
  1028. //资金数据更新处理
  1029. $balance = bcsub($all_trade['balance'], $value['trad_fee'], 2);
  1030. $tradeUp=Db::name('trade')
  1031. ->where($all_trade)
  1032. ->update([
  1033. 'balance' => $balance,
  1034. 'used_fee' => bcadd($all_trade['used_fee'], $value['trad_fee'], 2),
  1035. 'status' => $balance == 0 ? 3 : 2,
  1036. 'updatetime' =>date("Y-m-d H:i:s"),
  1037. ]);
  1038. if($tradeUp==false ) throw new Exception("{$value['tradNo']}资金更新失败");
  1039. //订单数据更新处理
  1040. $orderUp=Db::name('qrd_info')
  1041. ->where($all_order)
  1042. ->update([
  1043. 'apay_fee' => bcadd($all_order['apay_fee'], $value['trad_fee'], 2),
  1044. 'wpay_fee' => bcsub($all_order['wpay_fee'], $value['trad_fee'], 2),
  1045. 'pay_status' => bcsub($all_order['wpay_fee'], $value['trad_fee'], 2)==0 &&$all_order['pay_fee']==0? 3:2,
  1046. 'status' => 1,
  1047. 'updatetime' => date("Y-m-d H:i:s")
  1048. ]);
  1049. if($orderUp==false ) throw new Exception("{$value['orderCode']}销售单更新失败");
  1050. //资金订单关联数据
  1051. $assoc_insert_data[]=[
  1052. 'assocNo' => $OrderCodeToAssocNo,
  1053. 'apply_id' => $this->uid,
  1054. 'apply_name' => $this->uname,
  1055. 'type' => 2,
  1056. 'orderCode' => $value['orderCode'],
  1057. 'customerNo' => $all_order['customerNo'],
  1058. 'viceCode' => $OrderCodeToLogNo,
  1059. 'order_total' => $all_order['totalPrice'],
  1060. 'vice_total' => $value['trad_fee'],
  1061. 'cancel_fee' => $value['trad_fee'],
  1062. 'status' => 2,//认领通过
  1063. 'addtime' => date("Y-m-d H:i:s"),
  1064. 'updatetime' => date("Y-m-d H:i:s"),
  1065. ];
  1066. //资金认领数据
  1067. $trade_pool_insert_data[]=[
  1068. 'logNo' => $OrderCodeToLogNo,
  1069. 'tradNo' => $value['tradNo'],
  1070. 'platform_type' => $all_order['platform_type'],
  1071. 'companyNo' => $value['companyNo'],
  1072. 'customerNo' => $all_order['customerNo'],
  1073. 'apply_id' => $this->uid,
  1074. 'apply_name' => $this->uname,
  1075. 'trade_time' => $all_trade['trade_time'],
  1076. 'total_fee' => $value['trad_fee'],
  1077. 'status' => 2,//2审核通过
  1078. 'addtime' => date("Y-m-d H:i:s"),
  1079. 'updatetime' => date("Y-m-d H:i:s"),
  1080. ];
  1081. $report=ReportCode::where(["qrdNo"=>$value['orderCode']])->find();
  1082. if($report){
  1083. $report->setField("logNo",$OrderCodeToLogNo);
  1084. $report->setField("tradNo",$value['tradNo']);
  1085. }
  1086. }
  1087. if ($assoc_insert_data) Db::name('assoc')->insertAll($assoc_insert_data);
  1088. if ($trade_pool_insert_data) Db::name('trade_pool')->insertAll($trade_pool_insert_data);
  1089. Db::commit();
  1090. return app_show(0, '批量认领资金成功');
  1091. } catch (Exception $exception) {
  1092. Db::rollback();
  1093. return error_show(1004, '批量认领资金失败,' . $exception->getMessage());
  1094. }
  1095. }
  1096. }