OrderPay.php 56 KB

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