OrderPay.php 57 KB

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