OrderPay.php 58 KB

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