OrderPay.php 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102
  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. "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. $name =isset($this->post['name'])&&$this->post['name']!=""? trim($this->post['name']):"";
  330. $bank =isset($this->post['bank'])&&$this->post['bank']!=""? trim($this->post['bank']):"";
  331. $start =isset($this->post['start'])&&$this->post['start']!=""? trim($this->post['start']):"";
  332. $end =isset($this->post['end'])&&$this->post['end']!=""? trim($this->post['end']):"";
  333. $status =isset($this->post['status'])&&$this->post['status']!==""? intval($this->post['status']):"";
  334. $apply_id =isset($this->post['apply_id'])&&$this->post['apply_id']!=""? intval($this->post['apply_id']):"";
  335. $apply_name =isset($this->post['apply_name'])&&$this->post['apply_name']!=""? trim($this->post['apply_name']):"";
  336. $tradNo =isset($this->post['tradNo'])&&$this->post['tradNo']!=""? trim($this->post['tradNo']):"";
  337. $orderCode =isset($this->post['orderCode'])&&$this->post['orderCode']!=''?trim($this->post['orderCode']):"";
  338. $companyNo =isset($this->post['companyNo'])&&$this->post['companyNo']!=""? trim($this->post['companyNo']):"";
  339. if($companyNo!==""){
  340. $condition[]=["a.companyNo","=",$companyNo];
  341. }
  342. $relaComNo= isset($this->post['relaComNo'])&&$this->post['relaComNo']!="" ? trim($this->post['relaComNo']) :"";
  343. if($relaComNo!=""){
  344. $condition[]=["a.companyNo","=",$relaComNo];
  345. }
  346. if($tradNo!==""){
  347. $condition[]=["a.tradNo","like","%$tradNo%"];
  348. }
  349. if($orderCode!==""){
  350. $condition[]=["d.orderCode","like","%$orderCode%"];
  351. }
  352. $cxCode= isset($this->post['cxCode'])&&$this->post['cxCode']!="" ? trim($this->post['cxCode']) :"";
  353. if($cxCode!=""){
  354. $condition[]=["c.cxCode","like","%$cxCode%"];
  355. }
  356. $logNo =isset($this->post['logNo'])&&$this->post['logNo']!=""? trim($this->post['logNo']):"";
  357. if($logNo!==""){
  358. $condition[]=["a.logNo","like","%$logNo%"];
  359. }
  360. if($bank!=""){
  361. $condition[]=["b.trade_bank","like","%$bank%"];
  362. }
  363. if($name!=""){
  364. $condition[]=["b.trade_out","like","%$name%"];
  365. }
  366. if($start!=""){
  367. $condition[]=["a.addtime",">=",date("Y-m-d 00:00:00",strtotime($start))];
  368. }
  369. if($end!=""){
  370. $condition[]=["a.addtime","<=",date("Y-m-d 23:59:59",strtotime($start))];
  371. }
  372. if($status!==""){
  373. $condition[]=["a.status","=",$status];
  374. }
  375. if($apply_id!==""){
  376. $condition[]=["a.apply_id","=",$apply_id];
  377. }
  378. if($apply_name!==""){
  379. $condition[]=["a.apply_name","like","%$apply_name%"];
  380. }
  381. $count=Db::name("trade_pool")->alias("a")
  382. ->leftJoin("trade b","a.tradNo=b.tradNo")
  383. ->leftJoin("assoc d","a.logNo=d.viceCode AND d.type = 2")
  384. ->leftJoin("qrd_info c","d.orderCode=c.sequenceNo")
  385. ->where($condition)->count();
  386. $total=ceil($count/$size);
  387. $page=$page>$total? intval($total):$page;
  388. $list =Db::name("trade_pool")->alias("a")
  389. ->leftJoin("trade b","a.tradNo=b.tradNo")
  390. ->leftJoin("assoc d","a.logNo=d.viceCode AND d.type = 2")
  391. ->leftJoin("qrd_info c","d.orderCode=c.sequenceNo")
  392. ->where($condition)
  393. ->page($page,$size)
  394. ->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,
  395. 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")
  396. ->order("a.addtime desc")->select();
  397. return app_show(0,"获取成功",["list"=>$list,"count"=>$count]);
  398. }
  399. // 获取资金下面的认领信息
  400. public function tradeQuery(){
  401. $tradNo =isset($this->post['tradNo'])&&$this->post['tradNo']!=""? trim($this->post['tradNo']):"";
  402. $status =isset($this->post['status'])&&$this->post['status']!=""? intval($this->post['status']):"";
  403. $apply_id =isset($this->post['apply_id'])&&$this->post['apply_id']!=""? intval($this->post['apply_id']):"";
  404. $apply_name =isset($this->post['apply_name'])&&$this->post['apply_name']!=""? trim($this->post['apply_name']):"";
  405. $companyNo =isset($this->post['companyNo'])&&$this->post['companyNo']!=""? trim($this->post['companyNo']):"";
  406. if($companyNo!==""){
  407. $condition[]=["companyNo","=",$companyNo];
  408. }
  409. $relaComNo= isset($post['relaComNo'])&&$post['relaComNo']!="" ? trim($post['relaComNo']) :"";
  410. if($relaComNo!=""){
  411. $condition[]=["companyNo","=",$relaComNo];
  412. }
  413. $condition=[["is_del","=",0]];
  414. if($apply_id!==""){
  415. $condition[]=["apply_id","=",$apply_id];
  416. }
  417. if($apply_name!==""){
  418. $condition[]=["apply_name","like","%$apply_name%"];
  419. }
  420. if($status!==""){
  421. $condition[]=["status","=",$status];
  422. }
  423. if($tradNo!==""){
  424. $condition[]=["tradNo","like","%$tradNo%"];
  425. }
  426. $list =Db::name("trade_pool")->where($condition)->order("addtime desc")->select();
  427. return app_show(0,"获取成功",$list);
  428. }
  429. //资金详情
  430. public function tradeInfo(){
  431. $tradNo =isset($this->post['tradNo'])&&$this->post['tradNo']!=""? trim($this->post['tradNo']):"";
  432. if($tradNo==""){
  433. return error_show(1004,"参数 tradNo 不能为空");
  434. }
  435. $tradinfo = Db::name("trade")->where(["tradNo"=>$tradNo,"is_del"=>0])->find();
  436. if($tradinfo==false){
  437. return error_show(1004,"资金信息未找到");
  438. }
  439. return app_show(0,"获取成功",$tradinfo);
  440. }
  441. //资金认领详情
  442. public function logInfo(){
  443. $logNo =isset($this->post['logNo'])&&$this->post['logNo']!=""? trim($this->post['logNo']):"";
  444. if($logNo==""){
  445. return error_show(1004,"参数 logNo 不能为空");
  446. }
  447. $tradinfo = Db::name("trade_pool")->where(["logNo"=>$logNo,"is_del"=>0])->find();
  448. if($tradinfo==false){
  449. return error_show(1004,"资金信息未找到");
  450. }
  451. $trade = Db::name("trade")->where(["tradNo"=>$tradinfo['tradNo'],"is_del"=>0])->find();
  452. $tradinfo['trade_out']= $trade['trade_out']??"";
  453. $tradinfo['trade_in']= $trade['trade_in']??"";
  454. $tradinfo['trade_bank']= $trade['trade_bank']??"";
  455. $tradinfo['customerName']= Db::name("customer_info")->where(["companyNo"=>$tradinfo['customerNo']])->value("companyName","");
  456. $tradinfo['companyNo']= $trade['companyNo']??"";
  457. $tradinfo['total_fee']= $trade['total_fee']??"";
  458. $tradinfo['balance']= $trade['balance']??"";
  459. $tradinfo['used_fee']= $trade['used_fee']??"";
  460. $orderinfo = Db::name("assoc")->alias("a")->leftJoin("qrd_info c","a.orderCode=c.sequenceNo")
  461. ->where(["a.viceCode"=>$logNo,"a.is_del"=>0,"a.status"=>[1,2,3]])
  462. ->order("a.addtime desc")
  463. ->field("c.*,a.cancel_fee")
  464. ->findOrEmpty();
  465. $tradinfo['orderinfo']=$orderinfo;
  466. return app_show(0,"获取成功",$tradinfo);
  467. }
  468. //认领资金退回或退款 type 1 退款 2 解除资金认领
  469. public function ReturnPay(){
  470. $logNo = isset($this->post['logNo'])&&$this->post['logNo']!="" ? trim($this->post['logNo']):"";
  471. if($logNo==''){
  472. return error_show(1004,"参数 logNo 不能为空");
  473. }
  474. $type = isset($this->post['type'])&&$this->post['type']!="" ? intval($this->post['type']):"";
  475. if($type==""){
  476. return error_show(1004,"参数 type 不能为空");
  477. }
  478. $loginfo = Db::name("trade_pool")->where(["logNo"=>$logNo,"is_del"=>0])->find();
  479. if($loginfo==false){
  480. return error_show(1004,"认领资金信息未找到");
  481. }
  482. if($loginfo['status']==2 && $type==1)return error_show(1004,"认领资金信息审核已通过");
  483. if($loginfo['status']!=2 && $type==2)return error_show(1004,"认领资金信息未审核通过");
  484. $tradinfo = Db::name("trade")->where(["tradNo"=>$loginfo['tradNo'],"is_del"=>0])->find();
  485. if($tradinfo==false){
  486. return error_show(1004,"资金信息未找到");
  487. }
  488. $isT= Db::name("trade_return")->where(["logNo"=>$logNo,"tradNo"=>$loginfo['tradNo'],"status"=>[0,1]])
  489. ->findOrEmpty();
  490. if(!empty($isT)) return error_show(1004,"资金退回流程已存在");
  491. $reason = isset($this->post['return_reason'])&&$this->post['return_reason']!="" ? trim($this->post['return_reason']):"";
  492. $remark = isset($this->post['remark'])&&$this->post['remark']!="" ? trim($this->post['remark']):"";
  493. $returnCode =makeNo("RTA");
  494. $data=[
  495. "returnCode"=>$returnCode,
  496. "logNo"=>$logNo,
  497. "companyNo"=>$tradinfo['companyNo'],
  498. "tradNo"=>$loginfo['tradNo'],
  499. "return_img"=>'',
  500. "type"=>$type,
  501. "apply_id"=>$this->uid,
  502. "apply_name"=>$this->uname,
  503. "return_reason"=>$reason,
  504. "remark"=>$remark,
  505. "addtime"=>date("Y-m-d H:i:s"),
  506. "updatetime"=>date("Y-m-d H:i:s")
  507. ];
  508. $in = Db::name("trade_return")->insert($data);
  509. if($in){
  510. return app_show(0,"申请新建成功");
  511. }else{
  512. return error_show(1004,"申请新建失败");
  513. }
  514. }
  515. /**退款列表
  516. * //page size tradNo logNo apply_id apply_name type status returnCode
  517. * @return \think\response\Json|void
  518. * @throws \think\db\exception\DataNotFoundException
  519. * @throws \think\db\exception\DbException
  520. * @throws \think\db\exception\ModelNotFoundException
  521. */
  522. public function returnList(){
  523. $page=isset($this->post['page'])&&$this->post['page']!='' ? intval($this->post['page']):1;
  524. $size=isset($this->post['size'])&&$this->post['size']!='' ? intval($this->post['size']):15;
  525. $tradNo =isset($this->post['tradNo'])&&$this->post['tradNo']!=''?trim($this->post['tradNo']):"";
  526. $logNo =isset($this->post['logNo'])&&$this->post['logNo']!=''?trim($this->post['logNo']):"";
  527. $apply_id =isset($this->post['apply_id'])&&$this->post['apply_id']!=''?intval($this->post['apply_id']):"";
  528. $apply_name =isset($this->post['apply_name'])&&$this->post['apply_name']!=''?trim($this->post['apply_name']):"";
  529. $type =isset($this->post['type'])&&$this->post['type']!=''?intval($this->post['type']):"";
  530. $status =isset($this->post['status'])&&$this->post['status']!==''?intval($this->post['status']):"";
  531. $returnCode =isset($this->post['returnCode'])&&$this->post['returnCode']!=''?trim($this->post['returnCode']):"";
  532. $orderCode =isset($this->post['orderCode'])&&$this->post['orderCode']!=''?trim($this->post['orderCode']):"";
  533. $companyNo =isset($this->post['companyNo'])&&$this->post['companyNo']!=""? trim($this->post['companyNo']):"";
  534. if($companyNo!==""){
  535. $condition[]=["a.companyNo","=",$companyNo];
  536. }
  537. $relaComNo= isset($this->post['relaComNo'])&&$this->post['relaComNo']!="" ? trim($this->post['relaComNo']) :"";
  538. if($relaComNo!=""){
  539. $condition[]=["a.companyNo","=",$relaComNo];
  540. }
  541. $condition=[["a.is_del","=",0]];
  542. if($tradNo!=''){
  543. $condition[]=["a.tradNo","like","%$tradNo%"];
  544. }
  545. if($orderCode!=''){
  546. $condition[]=["b.orderCode","like","%$orderCode%"];
  547. }
  548. if($logNo!=''){
  549. $condition[]=["a.logNo","like","%$logNo%"];
  550. }
  551. if($apply_id!=''){
  552. $condition[]=["a.apply_id","=",$apply_id];
  553. }
  554. if($apply_name!=''){
  555. $condition[]=["a.apply_name","like","%$apply_name%"];
  556. }
  557. if($returnCode!=''){
  558. $condition[]=["a.returnCode","like","%$returnCode%"];
  559. }
  560. if($type!=''){
  561. $condition[]=["a.type","=",$type];
  562. }
  563. if($status!==''){
  564. $condition[]=["a.status","=",$status];
  565. }
  566. $count =Db::name("trade_return")->alias("a")
  567. ->leftJoin("assoc b","a.logNo=b.viceCode")
  568. ->leftJoin("qrd_info c","c.sequenceNo=b.orderCode")
  569. ->where($condition)->count();
  570. $total=ceil($count/$size);
  571. $page = $page>$total ? intval($total):$page;
  572. $list=Db::name("trade_return")->alias("a")->leftJoin("assoc b","a.logNo=b.viceCode")
  573. ->leftJoin("qrd_info c","c.sequenceNo=b.orderCode")
  574. ->field("a.*,b.orderCode,c.qrdSource,goodNo,goodName,qrdType,c.ownerName,c.ownerid,c.department,c.poCode,c.platName,b.cancel_fee,c.customerName")
  575. ->where($condition)->page($page,$size)->order("a.addtime desc")->select();
  576. return app_show(0,"获取成功",["list"=>$list,"count"=>$count]);
  577. }
  578. //退款状态审核 0 待审核 1 财务审核 2 财务驳回
  579. public function returnStatus(){
  580. $returnCode=isset($this->post['returnCode'])&&$this->post['returnCode']!=''?trim($this->post['returnCode']):"";
  581. if($returnCode==''){
  582. return error_show(1004,"参数 returnCode 不能为空");
  583. }
  584. $returninfo =Db::name("trade_return")->where(["returnCode"=>$returnCode,"is_del"=>0])->find();
  585. if($returninfo==false){
  586. return error_show(1004,"退款申请数据未找到");
  587. }
  588. $status=isset($this->post['status']) &&$this->post['status']!=''?intval($this->post['status']) : '';
  589. if($status==''){
  590. return error_show(1004,"参数 status 不能为空");
  591. }
  592. $return_img = isset($this->post['return_img'])&&$this->post['return_img']!=''?trim($this->post['return_img']):"";
  593. $remark = isset($this->post['remark'])&&$this->post['remark']!=''?trim($this->post['remark']):"";
  594. $loginfo = Db::name("trade_pool")->where(["logNo"=>$returninfo['logNo'],"is_del"=>0])->find();
  595. if($loginfo==false){
  596. return error_show(1004,"认领资金信息未找到");
  597. }
  598. if($loginfo['status']!=2)return error_show(1004,"认领资金信息审核未通过");
  599. Db::startTrans();
  600. try{
  601. $update=["status"=>$status,"return_img"=>$return_img,"remark"=>$remark,"updatetime"=>date("Y-m-d H:i:s")];
  602. $up =Db::name("trade_return")->where($returninfo)->update($update);
  603. if($up){
  604. if($status==1){
  605. $qrdArr=Db::name("assoc")->where(["viceCode"=>$returninfo['logNo'],"status"=>[1,2],"is_del"=>0])
  606. ->select()->toArray();
  607. if(!empty($qrdArr)){
  608. foreach ($qrdArr as $value){
  609. $qrd =Db::name("qrd_info")->where(["sequenceNo"=>$value['orderCode']])->field("id,status,pay_fee,apay_fee,wpay_fee,pay_status,totalPrice")->findOrEmpty();
  610. if(empty($qrd)){
  611. Db::rollback();
  612. return error_show(1005,"未找到销售单数据");
  613. }
  614. if($returninfo['type']==2){
  615. if($qrd['apay_fee']<$value['cancel_fee']){
  616. Db::rollback();
  617. return error_show(1005,"销售单已付金额不足");
  618. }
  619. $update =[
  620. "wpay_fee"=>$qrd['wpay_fee']+$value['cancel_fee'],
  621. "apay_fee"=>$qrd['apay_fee']-$value['cancel_fee'],
  622. "pay_status"=>($qrd['apay_fee']-$value['cancel_fee'])==0 && $qrd['pay_fee']==0 ? 1:2,
  623. "status"=>$qrd['pay_fee']==0 && ($qrd['apay_fee']-$value['cancel_fee'])==0 ?0:1,
  624. "updatetime"=>date("Y-m-d H:i:s")
  625. ];
  626. }else{
  627. if($qrd['pay_fee']<$value['cancel_fee']){
  628. Db::rollback();
  629. return error_show(1005,"销售单付款中金额不足");
  630. }
  631. $update =[
  632. "wpay_fee"=>$qrd['wpay_fee']+$value['cancel_fee'],
  633. "pay_fee"=>$qrd['pay_fee']-$value['cancel_fee'],
  634. "pay_status"=>($qrd['pay_fee']-$value['cancel_fee'])==0 && $qrd['apay_fee']==0 ? 1:2,
  635. "status"=>$qrd['apay_fee']==0 && ($qrd['pay_fee']-$value['cancel_fee'])==0 ?0:1,
  636. "updatetime"=>date("Y-m-d H:i:s")
  637. ];
  638. }
  639. $qrdup =Db::name("qrd_info")->where($qrd)->update($update);
  640. if($qrdup==false){
  641. Db::rollback();
  642. return error_show(1005,"销售单更新失败");
  643. }
  644. $asscup =[
  645. "status"=>3,
  646. "updatetime"=>date("Y-m-d H:i:s")
  647. ];
  648. $assc=Db::name("assoc")->where($value)->update($asscup);
  649. if($assc==false){
  650. Db::rollback();
  651. return error_show(1005,"销售单更新失败");
  652. }
  653. $report=ReportCode::where(["qrdNo"=>$value['orderCode']])->find();
  654. if($report)$report->setField("returnTrad",$returnCode);
  655. }
  656. }
  657. $logup =["status"=>$returninfo['type']==1?5:4,"updatetime"=>date("Y-m-d H:i:s")];
  658. $upde =Db::name("trade_pool")->where($loginfo)->update($logup);
  659. if($upde==false){
  660. Db::rollback();
  661. return error_show(1005,"资金认领信息更新失败");
  662. }
  663. // if($returninfo['type']==1){
  664. $trade =Db::name("trade")->where(["tradNo"=>$returninfo['tradNo'],"is_del"=>0])->find();
  665. if($trade==false){
  666. Db::rollback();
  667. return error_show(1005,"资金信息未找到");
  668. }
  669. if($trade['used_fee']<$loginfo['total_fee']){
  670. Db::rollback();
  671. return error_show(1005,"资金信息已认领金额不足");
  672. }
  673. $tradup =[
  674. "used_fee"=>$trade['used_fee']-$loginfo['total_fee'],
  675. "balance"=>$trade['balance']+$loginfo['total_fee'],
  676. "status"=>($trade['used_fee']-$loginfo['total_fee'])==0 ?1 :2,
  677. "updatetime"=>date("Y-m-d H:i:s")
  678. ];
  679. $updaT=Db::name("trade")->where($trade)->update($tradup);
  680. if($updaT==false){
  681. Db::rollback();
  682. return error_show(1005,"资金信息更新失败");
  683. }
  684. // }
  685. }
  686. Db::commit();
  687. return app_show(0,"退款申请审核成功");
  688. }
  689. Db::rollback();
  690. return error_show(1004,'审核状态失败');
  691. }catch (\Exception $e){
  692. Db::rollback();
  693. return error_show(1004,$e->getMessage());
  694. }
  695. }
  696. //退款申请详情
  697. public function returnInfo(){
  698. $returnCode=isset($this->post['returnCode'])&&$this->post['returnCode']!=''?trim($this->post['returnCode']):"";
  699. if($returnCode==''){
  700. return error_show(1004,"参数 returnCode 不能为空");
  701. }
  702. $returninfo =Db::name("trade_return")->where(["returnCode"=>$returnCode,"is_del"=>0])->find();
  703. if($returninfo==false){
  704. return error_show(1004,"退款申请数据未找到");
  705. }
  706. $trade = Db::name("trade")->where(["tradNo"=>$returninfo['tradNo'],"is_del"=>0])->find();
  707. $returninfo['trade_out']= $trade['trade_out']??"";
  708. $returninfo['trade_in']= $trade['trade_in']??"";
  709. $returninfo['trade_bank']= $trade['trade_bank']??"";
  710. $returninfo['trade_time']= $trade['trade_time']??"";
  711. $returninfo['customerNo']= $trade['customerNo']??"";
  712. $returninfo['companyNo']= $trade['companyNo']??"";
  713. $returninfo['total_fee']= $trade['total_fee']??"";
  714. $returninfo['balance']= $trade['balance']??"";
  715. $returninfo['used_fee']= $trade['used_fee']??"";
  716. $pool =Db::name("trade_pool")->where(["logNo"=>$returninfo['logNo'],"is_del"=>0])->find();
  717. $returninfo['log_total_fee']= $pool['total_fee']??"";
  718. $returninfo['log_apply_id']= $pool['apply_id']??"";
  719. $returninfo['log_apply_name']= $pool['apply_name']??"";
  720. $orderinfo = Db::name("assoc")->alias("a")->leftJoin("qrd_info c","a.orderCode=c.sequenceNo")
  721. ->where(["a.viceCode"=>$returninfo['logNo'],"a.is_del"=>0,"a.status"=>[1,2]])
  722. ->order("a.addtime desc")
  723. ->field("c.*")
  724. ->findOrEmpty();
  725. $returninfo['orderinfo'] = $orderinfo;
  726. return app_show(0,"获取成功",$returninfo);
  727. }
  728. /**
  729. * 显示创建资源表单页.
  730. *
  731. * @return \think\Response
  732. */
  733. public function importTrade()
  734. {
  735. $files = $this->request->file("excel");
  736. $data = upload($files,$files->getOriginalExtension());
  737. if($data['code']!=0){
  738. return error_show(1004,$data['msg']);
  739. }
  740. $trade = $data['data'];
  741. $list = [];
  742. foreach ( $trade as $key => $value) {
  743. if (!isset($value[0]) || $value[0] == '') {
  744. //return ['code' => 1003, "msg" => '交易时间不能为空'];
  745. return error_show( 1003, '第'.($key+1).'行收款方公司编号不能为空!');
  746. }
  747. if (!isset($value[4]) || $value[4] == '') {
  748. // return ['code' => 1003, "msg" => '交易行名不能为空'];
  749. return error_show( 1003, '第'.($key+1).'行收入金额不能为空!');
  750. }
  751. if (!isset($value[8]) || $value[8] == '') {
  752. // return ['code' => 1003, "msg" => '对方账户不能为空'];
  753. return error_show( 1003, '第'.($key+1).'行对方账户不能为空!');
  754. }
  755. if (!isset($value[9]) || $value[9] == '') {
  756. // return ['code' => 1003, "msg" => '对方户名不能为空'];
  757. return error_show( 1003, '第'.($key+1).'行对方户名不能为空!');
  758. }
  759. if ($key == 0) {
  760. if ($value[0] != '收款方公司编号') {
  761. // return ['code' => 1003, "msg" => '模板第一列为必须为交易时间!'];
  762. return error_show( 1003, '模板第一列为必须为收款方公司编号!');
  763. }
  764. if ($value[4] != '收入金额') {
  765. // return ['code' => 1003, "msg" => '模板第二列为必须为收入金额!'];
  766. return error_show( 1003, '模板第五列为必须为收入金额!');
  767. }
  768. if ($value[6] != '交易行名') {
  769. //return ['code' => 1003, "msg" => '模板第五列为必须为交易行名!'];
  770. return error_show( 1003, '模板第七列为必须为交易行名!');
  771. }
  772. if ($value[8] != '对方账号') {
  773. // return ['code' => 1003, "msg" => '模板第七列为必须为对方账号!'];
  774. return error_show( 1003, '模板第九列为必须为对方账号!');
  775. }
  776. if ($value[9] != '对方户名') {
  777. // return ['code' => 1003, "msg" => '模板第八列为必须为对方户名!'];
  778. return error_show( 1003, '模板第十列为必须为对方户名!');
  779. }
  780. continue;
  781. }
  782. $company =Db::name("company_info")->where(["companyNo"=>$value[0]])->findOrEmpty();
  783. if(empty($company)){
  784. return error_show( 1003, "业务公司编号不存在{$value[0]}");
  785. }
  786. $total_fee = $value[4];
  787. $type = 0;
  788. $time = explode(" ", $value[3]);
  789. $list[$key]["tradeTime"] = date("Y-m-d", strtotime($time[0])) . " " . (isset($time[1]) ? $time[1] : "00:00:00");
  790. $list[$key]["trade_fee"] = str_replace(",","",$total_fee);
  791. $list[$key]["trade_bank"] = $value[6];
  792. $list[$key]["trade_account"] = $value[8];
  793. $list[$key]["trade_out"] = $value[9];
  794. $list[$key]["trade_in"] = $value[1]??$company['company_name'];
  795. $list[$key]["trade_type"] = $type;
  796. $list[$key]["trade_used"] = isset($value[10]) ? $value[10] : "";
  797. $list[$key]["trade_remark"] = '';
  798. $list[$key]["companyNo"] = $value[0];
  799. $list[$key]["trade_in_account"] = $value[2]??"";
  800. }
  801. if(empty($list)){
  802. return error_show( 1003, '导入数据不能为空!');
  803. }
  804. Db::startTrans();
  805. try{
  806. $tra=[];
  807. foreach ($list as $value) {
  808. $temp = [];
  809. $temp["tradNo"] = makeStr('S');
  810. $temp['trade_time'] = $value['tradeTime'];
  811. $temp['total_fee'] = $value['trade_fee'];
  812. $temp['trade_bank'] = $value['trade_bank'];
  813. $temp['trade_account'] = $value['trade_account'];
  814. $temp['trade_type'] =$value['trade_type'];
  815. $temp['trade_out'] = $value['trade_out'];
  816. $temp['trade_in'] = $value['trade_in'];
  817. $temp['trade_in_account'] = $value['trade_in_account'];
  818. $temp['companyNo'] = $value['companyNo'];
  819. $temp['trade_used'] = $value['trade_used'];
  820. $temp['trade_remark'] = $value['trade_remark'];
  821. $temp['balance'] =$value['trade_fee'];
  822. $temp['addtime'] = date("Y-m-d H:i:s");
  823. $temp['updatetime'] = date("Y-m-d H:i:s");
  824. $tra[]=$temp;
  825. }
  826. $list = Db::name('trade')->insertAll($tra);
  827. if($list==count($tra)){
  828. Db::commit();
  829. return app_show(0, "资金导入成功");
  830. }else{
  831. Db::rollback();
  832. return app_show(1004, "资金导入失败");
  833. }
  834. }catch (\Exception $e){
  835. Db::rollback();
  836. return app_show(1004, $e->getMessage());
  837. }
  838. }
  839. /**
  840. * 资金导入
  841. * @return \think\response\Json|void
  842. */
  843. public function importTradeByArr()
  844. {
  845. $trade =isset($this->post['data']) &&!empty($this->post['data'])? $this->post['data']:[];
  846. if(!is_array($trade) || empty($trade) ) return error_show( 1003,"参数 data 不能为空");
  847. $list = [];
  848. foreach ( $trade as $key => $value) {
  849. if (!isset($value[0]) || $value[0] == '') {
  850. //return ['code' => 1003, "msg" => '交易时间不能为空'];
  851. return error_show( 1003, '第'.($key+1).'行收款方公司编号不能为空!');
  852. }
  853. if (!isset($value[2]) || $value[2] == '') {
  854. //return ['code' => 1003, "msg" => '交易时间不能为空'];
  855. return error_show( 1003, '第'.($key+1).'行收款账户不能为空!');
  856. }
  857. if (!isset($value[3]) || $value[3] == '') {
  858. //return ['code' => 1003, "msg" => '交易时间不能为空'];
  859. return error_show( 1003, '第'.($key+1).'行交易时间不能为空!');
  860. }
  861. if (!isset($value[4]) || $value[4] == '') {
  862. // return ['code' => 1003, "msg" => '交易行名不能为空'];
  863. return error_show( 1003, '第'.($key+1).'行收入金额不能为空!');
  864. }
  865. if (!isset($value[6]) || $value[6] == '') {
  866. // return ['code' => 1003, "msg" => '交易行名不能为空'];
  867. return error_show( 1003, '第'.($key+1).'行交易银行名称不能为空!');
  868. }
  869. if (!isset($value[8]) || $value[8] == '') {
  870. // return ['code' => 1003, "msg" => '对方账户不能为空'];
  871. return error_show( 1003, '第'.($key+1).'行对方账户不能为空!');
  872. }
  873. if (!isset($value[9]) || $value[9] == '') {
  874. // return ['code' => 1003, "msg" => '对方户名不能为空'];
  875. return error_show( 1003, '第'.($key+1).'行对方户名不能为空!');
  876. }
  877. // if ($key == 0) {
  878. // if ($value[0] != '收款方公司编号') {
  879. // // return ['code' => 1003, "msg" => '模板第一列为必须为交易时间!'];
  880. // return error_show( 1003, '模板第一列为必须为收款方公司编号!');
  881. // }
  882. // if ($value[4] != '收入金额') {
  883. // // return ['code' => 1003, "msg" => '模板第二列为必须为收入金额!'];
  884. // return error_show( 1003, '模板第五列为必须为收入金额!');
  885. // }
  886. //
  887. // if ($value[6] != '交易行名') {
  888. // //return ['code' => 1003, "msg" => '模板第五列为必须为交易行名!'];
  889. // return error_show( 1003, '模板第七列为必须为交易行名!');
  890. // }
  891. // if ($value[8] != '对方账号') {
  892. // // return ['code' => 1003, "msg" => '模板第七列为必须为对方账号!'];
  893. // return error_show( 1003, '模板第九列为必须为对方账号!');
  894. // }
  895. // if ($value[9] != '对方户名') {
  896. // // return ['code' => 1003, "msg" => '模板第八列为必须为对方户名!'];
  897. // return error_show( 1003, '模板第十列为必须为对方户名!');
  898. // }
  899. // continue;
  900. // }
  901. $company =Db::name("company_info")->where(["companyNo"=>$value[0]])->findOrEmpty();
  902. if(empty($company)){
  903. return error_show( 1003, "业务公司编号不存在{$value[0]}");
  904. }
  905. $total_fee = $value[4];
  906. $type = 0;
  907. $time = explode(" ", $value[3]);
  908. $list[$key]["tradeTime"] = date("Y-m-d", strtotime($time[0])) . " " . (isset($time[1]) ? $time[1] : "00:00:00");
  909. $list[$key]["trade_fee"] = str_replace(",","",$total_fee);
  910. $list[$key]["trade_bank"] = $value[6];
  911. $list[$key]["trade_account"] = $value[8];
  912. $list[$key]["trade_out"] = $value[9];
  913. $list[$key]["trade_in"] = $value[1]??$company['company_name'];
  914. $list[$key]["trade_type"] = $type;
  915. $list[$key]["trade_used"] = isset($value[10]) ? $value[10] : "";
  916. $list[$key]["trade_remark"] = '';
  917. $list[$key]["companyNo"] = $value[0];
  918. $list[$key]["trade_in_account"] = $value[2]??"";
  919. }
  920. if(empty($list)){
  921. return error_show( 1003, '导入数据不能为空!');
  922. }
  923. Db::startTrans();
  924. try{
  925. $tra=[];
  926. $i=0;
  927. foreach ($list as $value) {
  928. $temp = [];
  929. $i++;
  930. $temp["tradNo"] = makeStr('S').str_pad($i,3,'0',STR_PAD_LEFT);
  931. $temp['trade_time'] = $value['tradeTime'];
  932. $temp['total_fee'] = $value['trade_fee'];
  933. $temp['trade_bank'] = $value['trade_bank'];
  934. $temp['trade_account'] = $value['trade_account'];
  935. $temp['trade_type'] =$value['trade_type'];
  936. $temp['trade_out'] = $value['trade_out'];
  937. $temp['trade_in'] = $value['trade_in'];
  938. $temp['trade_in_account'] = $value['trade_in_account'];
  939. $temp['companyNo'] = $value['companyNo'];
  940. $temp['trade_used'] = $value['trade_used'];
  941. $temp['trade_remark'] = $value['trade_remark'];
  942. $temp['balance'] =$value['trade_fee'];
  943. $temp['addtime'] = date("Y-m-d H:i:s");
  944. $temp['updatetime'] = date("Y-m-d H:i:s");
  945. $tra[]=$temp;
  946. }
  947. $list = Db::name('trade')->insertAll($tra);
  948. if($list==count($tra)){
  949. Db::commit();
  950. return app_show(0, "资金导入成功");
  951. }else{
  952. Db::rollback();
  953. return app_show(1004, "资金导入失败");
  954. }
  955. }catch (\Exception $e){
  956. Db::rollback();
  957. return app_show(1004, $e->getMessage());
  958. }
  959. }
  960. //批量认领资金,跟订单相关
  961. public function importTradeByBatchOrderCode()
  962. {
  963. $list = $this->request->post('list', [], 'trim');
  964. $val = Validate::rule(['list|导入数据' => 'require|array|max:100']);
  965. if (!$val->check(['list' => $list])) return error_show(1004, $val->getError());
  966. $company = $tradNos = $orderCodes = $assoc_insert_data = $OrderCodeToAssocNo = $OrderCodeToLogNo = [];
  967. $val_item = Validate::rule([
  968. 'companyNo|卖出方公司编号' => 'require|max:255',
  969. 'tradNo|资金编号' => 'require|max:255',
  970. 'orderCode|订单编号' => 'require|max:255',
  971. 'trad_fee|认领资金' => 'require|float|gt:0|max:999999999.99',
  972. ]);
  973. foreach ($list as $key => $value) {
  974. if (!$val_item->check($value)) return error_show(1004, $val_item->getError());
  975. if (!isset($company[$value['companyNo']])) $company[$value['companyNo']] = $value['companyNo'];
  976. }
  977. //判断绑定公司
  978. $companyNo = Db::name('user_role')
  979. ->where(['is_del' => 0, 'status' => 1, 'uid' => $this->uid])
  980. ->whereIn('companyNo', $company)
  981. ->column('companyNo');
  982. $tmp = array_diff($company, $companyNo);//二者取差集
  983. if(!empty($tmp)) return error_show(1004,'不能操作以下公司数据:'.implode(',',$tmp));
  984. Db::startTrans();
  985. try {
  986. $i=1;
  987. //批量资金认领
  988. foreach ($list as $key=>$value){
  989. //资金明细判断
  990. $i++;
  991. $all_trade = Db::name('trade')
  992. ->where('is_del', 0)
  993. ->whereIn('status', [1, 2])
  994. ->where('tradNo', $value['tradNo'])
  995. ->field('id,balance,used_fee,companyNo,trade_time')
  996. ->findOrEmpty();
  997. if(empty($all_trade))throw new Exception("{$value['tradNo']}资金未找到数据");
  998. if($all_trade['balance']<$value['trad_fee'])throw new Exception("{$value['tradNo']}资金余额不足");
  999. //订单明细判断
  1000. $all_order=Db::name('qrd_info')
  1001. ->where('is_del', 0)
  1002. ->whereIn('sequenceNo',$value['orderCode'])
  1003. ->field('id,customerNo,status,pay_fee,apay_fee,wpay_fee,pay_status,totalPrice')
  1004. ->findOrEmpty();
  1005. if(empty($all_order))throw new Exception("{$value['orderCode']}销售单未找到数据");
  1006. if($all_order['wpay_fee']<$value['trad_fee'])throw new Exception("{$value['orderCode']}销售单未付款金额不足核销金额");
  1007. $OrderCodeToAssocNo = substr(makeNo('AS'), 0, -3).str_pad($i,3,'0',STR_PAD_LEFT);
  1008. $OrderCodeToLogNo = substr(makeNo('TRC'), 0, -3).str_pad($i,3,'0',STR_PAD_LEFT);
  1009. //资金数据更新处理
  1010. $balance = bcsub($all_trade['balance'], $value['trad_fee'], 2);
  1011. $tradeUp=Db::name('trade')
  1012. ->where($all_trade)
  1013. ->update([
  1014. 'balance' => $balance,
  1015. 'used_fee' => bcadd($all_trade['used_fee'], $value['trad_fee'], 2),
  1016. 'status' => $balance == 0 ? 3 : 2,
  1017. 'updatetime' =>date("Y-m-d H:i:s"),
  1018. ]);
  1019. if($tradeUp==false ) throw new Exception("{$value['tradNo']}资金更新失败");
  1020. //订单数据更新处理
  1021. $orderUp=Db::name('qrd_info')
  1022. ->where($all_order)
  1023. ->update([
  1024. 'apay_fee' => bcadd($all_order['apay_fee'], $value['trad_fee'], 2),
  1025. 'wpay_fee' => bcsub($all_order['wpay_fee'], $value['trad_fee'], 2),
  1026. 'pay_status' => bcsub($all_order['wpay_fee'], $value['trad_fee'], 2)==0 &&$all_order['pay_fee']==0? 3:2,
  1027. 'status' => 1,
  1028. 'updatetime' => date("Y-m-d H:i:s")
  1029. ]);
  1030. if($orderUp==false ) throw new Exception("{$value['orderCode']}销售单更新失败");
  1031. //资金订单关联数据
  1032. $assoc_insert_data[]=[
  1033. 'assocNo' => $OrderCodeToAssocNo,
  1034. 'apply_id' => $this->uid,
  1035. 'apply_name' => $this->uname,
  1036. 'type' => 2,
  1037. 'orderCode' => $value['orderCode'],
  1038. 'customerNo' => $all_order['customerNo'],
  1039. 'viceCode' => $OrderCodeToLogNo,
  1040. 'order_total' => $all_order['totalPrice'],
  1041. 'vice_total' => $value['trad_fee'],
  1042. 'cancel_fee' => $value['trad_fee'],
  1043. 'status' => 2,//认领通过
  1044. 'addtime' => date("Y-m-d H:i:s"),
  1045. 'updatetime' => date("Y-m-d H:i:s"),
  1046. ];
  1047. //资金认领数据
  1048. $trade_pool_insert_data[]=[
  1049. 'logNo' => $OrderCodeToLogNo,
  1050. 'tradNo' => $value['tradNo'],
  1051. 'companyNo' => $value['companyNo'],
  1052. 'customerNo' => $all_order['customerNo'],
  1053. 'apply_id' => $this->uid,
  1054. 'apply_name' => $this->uname,
  1055. 'trade_time' => $all_trade['trade_time'],
  1056. 'total_fee' => $value['trad_fee'],
  1057. 'status' => 2,//2审核通过
  1058. 'addtime' => date("Y-m-d H:i:s"),
  1059. 'updatetime' => date("Y-m-d H:i:s"),
  1060. ];
  1061. }
  1062. if ($assoc_insert_data) Db::name('assoc')->insertAll($assoc_insert_data);
  1063. if ($trade_pool_insert_data) Db::name('trade_pool')->insertAll($trade_pool_insert_data);
  1064. Db::commit();
  1065. return app_show(0, '批量认领资金成功');
  1066. } catch (Exception $exception) {
  1067. Db::rollback();
  1068. return error_show(1004, '批量认领资金失败,' . $exception->getMessage());
  1069. }
  1070. }
  1071. }