OrderPay.php 55 KB

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