OrderPay.php 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763
  1. <?php
  2. namespace app\admin\controller;
  3. use app\admin\BaseController;
  4. use app\admin\model\ReportCode;use think\App;
  5. use think\facade\Db;use think\queue\command\Retry;
  6. class OrderPay extends BaseController{
  7. public function __construct(App $app) {parent::__construct($app);}
  8. //新建资金认领数据
  9. public function create(){
  10. $tradNo = isset($this->post['tradNo'])&&$this->post['tradNo']!=""?trim($this->post['tradNo']):"";
  11. if($tradNo==""){
  12. return error_show(1004,"参数 tradNo 不能为空");
  13. }
  14. $orderArr = isset($this->post['orderArr'])&&!empty($this->post['orderArr'])?$this->post['orderArr']:[];
  15. if(empty($orderArr)){
  16. return error_show(1004,"参数 orderArr 不能为空");
  17. }
  18. Db::startTrans();
  19. try{
  20. $logNo=makeNo("TRC");
  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. if(!isset($value['sequenceNo'])||$value['sequenceNo']==""){
  34. Db::rollback();
  35. return error_show(1004,"销售单编号不能为空");
  36. }
  37. if(!isset($value['trad_fee'])||$value['trad_fee']==""){
  38. Db::rollback();
  39. return error_show(1004,"销售单核销金额不能为空");
  40. }
  41. $qrd =Db::name("qrd_info")->where("sequenceNo","=",$value['sequenceNo'])->field("id,customerNo,status,pay_fee,wpay_fee,pay_status,totalPrice")->findOrEmpty();
  42. if(empty($qrd)){
  43. Db::rollback();
  44. return error_show(1004,"销售单信息未找到");
  45. }
  46. if($qrd['wpay_fee']<$value['trad_fee']){
  47. Db::rollback();
  48. return error_show(1004,"销售单未付款金额不足核销金额");
  49. }
  50. $updt=[
  51. "pay_fee"=>$qrd['pay_fee']+$value['trad_fee'],
  52. "wpay_fee"=>$qrd['wpay_fee']-$value['trad_fee'],
  53. "pay_status"=>2,
  54. "status"=>1,
  55. "updatetime"=>date("Y-m-d H:i:s")
  56. ];
  57. $qrdup = Db::name("qrd_info")->where($qrd)->update($updt);
  58. if($qrdup==false){
  59. Db::rollback();
  60. return error_show(1004,"销售单核销金额失败");
  61. }
  62. $temp=[
  63. "assocNo"=>makeNo("AS"),
  64. "apply_id"=>$this->uid,
  65. "apply_name"=>$this->uname,
  66. "type"=>2,
  67. "orderCode"=>$value['sequenceNo'],
  68. "customerNo"=>$qrd['customerNo'],
  69. "viceCode"=>$logNo,
  70. "order_total"=>$qrd['totalPrice'],
  71. "vice_total"=>$total_fee,
  72. "cancel_fee"=>$value['trad_fee'],
  73. "status"=>1,
  74. "addtime"=>date("Y-m-d H:i:s"),
  75. "updatetime"=>date("Y-m-d H:i:s")
  76. ];
  77. $assoc[]=$temp;
  78. $report=ReportCode::where(["qrdNo"=>$value['sequenceNo']])->find();
  79. if($report)$report->setField("logNo",$logNo);
  80. if($report)$report->setField("tradNo",$tradNo);
  81. }
  82. $asscin= Db::name("assoc")->insertAll($assoc);
  83. if($asscin!=count($assoc)){
  84. Db::rollback();
  85. return error_show(1004,"销售单核销金额关联失败");
  86. }
  87. $update=[
  88. "balance"=>$trade['balance']-$total_fee,
  89. "used_fee"=>$trade['used_fee']+$total_fee,
  90. "status"=>($trade['balance']-$total_fee)==0 ? 2:1,
  91. "updatetime"=>date("Y-m-d H:i:s"),
  92. ];
  93. $tradup=Db::name("trade")->where($trade)->update($update);
  94. if($tradup){
  95. $create = [
  96. "logNo"=>$logNo,
  97. "tradNo"=>$tradNo,
  98. "apply_id"=>$this->uid,
  99. "apply_name"=>$this->uname,
  100. "trade_time"=>$trade['trade_time'],
  101. "total_fee"=>$total_fee,
  102. "status"=>1,
  103. "addtime"=>date("Y-m-d H:i:s"),
  104. "updatetime"=>date("Y-m-d H:i:s")
  105. ];
  106. $tradchild =Db::name("trade_pool")->insert($create);
  107. if($tradchild){
  108. Db::commit();
  109. return app_show(0,"资金认领成功",["logNo"=>$logNo]);
  110. }
  111. }
  112. Db::rollback();
  113. return error_show(1004,"资金认领失败");
  114. }catch (\Exception $e){
  115. Db::rollback();
  116. return error_show(1004,$e->getMessage());
  117. }
  118. }
  119. // 1待审批2审批通过3审批驳回4退款5解除认领
  120. public function status(){
  121. $logNo = isset($this->post['logNo'])&&$this->post['logNo']!=""?trim($this->post['logNo']):"";
  122. if($logNo==""){
  123. return error_show(1004,"参数logNo不能为空");
  124. }
  125. $status=isset($this->post['status'])&&$this->post['status']!=""?intval($this->post['status']):"";
  126. if($status==""){
  127. return error_show(1004,"参数 status 不能为空");
  128. }
  129. $remark=isset($this->post['remark'])&&$this->post['remark']!=""?trim($this->post['remark']):"";
  130. if($remark==""){
  131. return error_show(1004,"参数 remark 不能为空");
  132. }
  133. Db::startTrans();
  134. try {
  135. $logingo= Db::name("trade_pool")->where(["logNo"=>$logNo,"is_del"=>0])->find();
  136. if($logingo==false){
  137. Db::rollback();
  138. return error_show(1004,"资金认领信息未找到");
  139. }
  140. $up=["status"=>$status,"updatetime"=>date("Y-m-d H:i:s"),"remark"=>$remark];
  141. $logup = Db::name("trade_pool")->where($logingo)->update($up);
  142. if($logup){
  143. if($status==2){
  144. $qrdArr=Db::name("assoc")->where(["viceCode"=>$logNo,"status"=>1,"is_del"=>0])->select()->toArray();
  145. if(!empty($qrdArr)){
  146. foreach ($qrdArr as $value){
  147. $qrd =Db::name("qrd_info")->where(["sequenceNo"=>$value['orderCode']])->field("id,status,pay_fee,apay_fee,wpay_fee,pay_status,totalPrice")->findOrEmpty();
  148. if(empty($qrd)){
  149. Db::rollback();
  150. return error_show(1005,"未找到销售单数据");
  151. }
  152. if($qrd['pay_fee']<$value['cancel_fee']){
  153. Db::rollback();
  154. return error_show(1005,"销售单待付金额不足");
  155. }
  156. $update =[
  157. "apay_fee"=>$qrd['apay_fee']+$value['cancel_fee'],
  158. "pay_fee"=>$qrd['pay_fee']-$value['cancel_fee'],
  159. "paytime"=>date("Y-m-d H:i:s"),
  160. "pay_status"=>$qrd['wpay_fee']==0 && ($qrd['pay_fee']-$value['cancel_fee'])==0 ? 3:2,
  161. "updatetime"=>date("Y-m-d H:i:s")
  162. ];
  163. $qrdup =Db::name("qrd_info")->where($qrd)->update($update);
  164. if($qrdup==false){
  165. Db::rollback();
  166. return error_show(1005,"销售单更新失败");
  167. }
  168. $asscup =[
  169. "status"=>2,
  170. "assoc_time"=>date("Y-m-d H:i:s"),
  171. "updatetime"=>date("Y-m-d H:i:s")
  172. ];
  173. $assc=Db::name("assoc")->where($value)->update($asscup);
  174. if($assc==false){
  175. Db::rollback();
  176. return error_show(1005,"销售单更新失败");
  177. }
  178. }
  179. }
  180. }
  181. if($status==3){
  182. $qrdArr=Db::name("assoc")->where(["viceCode"=>$logNo,"status"=>1,"is_del"=>0])->select()->toArray();
  183. if(!empty($qrdArr)){
  184. foreach ($qrdArr as $value){
  185. $qrd =Db::name("qrd_info")->where(["sequenceNo"=>$value['orderCode']])->field("id,status,pay_fee,apay_fee,wpay_fee,pay_status,totalPrice")->findOrEmpty();
  186. if(empty($qrd)){
  187. Db::rollback();
  188. return error_show(1005,"未找到销售单数据");
  189. }
  190. if($qrd['pay_fee']<$value['cancel_fee']){
  191. Db::rollback();
  192. return error_show(1005,"销售单待付金额不足");
  193. }
  194. $update =[
  195. "wpay_fee"=>$qrd['wpay_fee']+$value['cancel_fee'],
  196. "pay_fee"=>$qrd['pay_fee']-$value['cancel_fee'],
  197. "pay_status"=>$qrd['apay_fee']==0 && ($qrd['pay_fee']-$value['cancel_fee'])==0 ? 1:2,
  198. "status"=>$qrd['apay_fee']==0 && ($qrd['pay_fee']-$value['cancel_fee'])==0 ?0:1,
  199. "updatetime"=>date("Y-m-d H:i:s")
  200. ];
  201. $qrdup =Db::name("qrd_info")->where($qrd)->update($update);
  202. if($qrdup==false){
  203. Db::rollback();
  204. return error_show(1005,"销售单更新失败");
  205. }
  206. $asscup =[
  207. "status"=>3,
  208. "updatetime"=>date("Y-m-d H:i:s")
  209. ];
  210. $assc=Db::name("assoc")->where($value)->update($asscup);
  211. if($assc==false){
  212. Db::rollback();
  213. return error_show(1005,"销售单更新失败");
  214. }
  215. $report=ReportCode::where(["qrdNo"=>$value['orderCode']])->find();
  216. if($report)$report->rmField("logNo",$logNo);
  217. }
  218. }
  219. $trade =Db::name("trade")->where(["tradNo"=>$logingo['tradNo'],"is_del"=>0])->findOrEmpty();
  220. if($trade==false){
  221. Db::rollback();
  222. return error_show(1005,"资金信息未找到");
  223. }
  224. if($trade['used_fee']<$logingo['total_fee']){
  225. Db::rollback();
  226. return error_show(1005,"资金信息已认领金额不足");
  227. }
  228. $tradup =[
  229. "used_fee"=>$trade['used_fee']-$logingo['total_fee'],
  230. "balance"=>$trade['balance']+$logingo['total_fee'],
  231. "status"=>($trade['used_fee']-$logingo['total_fee'])==0 ? 0 :1,
  232. "updatetime"=>date("Y-m-d H:i:s")
  233. ];
  234. $updaT=Db::name("trade")->where($trade)->update($tradup);
  235. if($updaT==false){
  236. Db::rollback();
  237. return error_show(1005,"资金信息更新失败");
  238. }
  239. }
  240. Db::commit();
  241. return app_show(0,"审核成功");
  242. }
  243. Db::rollback();
  244. return error_show(1004,"审核失败");
  245. }catch (\Exception $e){
  246. Db::rollback();
  247. return error_show(1004,$e->getMessage());
  248. }
  249. }
  250. //资金信息列表
  251. public function list(){
  252. $condition=[];
  253. $page=isset($this->post['page'])&&$this->post['page']!="" ? intval($this->post['page']) :1;
  254. $size=isset($this->post['size'])&&$this->post['size']!="" ? intval($this->post['size']) :15;
  255. $name =isset($this->post['name'])&&$this->post['name']!=""? trim($this->post['name']):"";
  256. $bank =isset($this->post['bank'])&&$this->post['bank']!=""? trim($this->post['bank']):"";
  257. $start =isset($this->post['start'])&&$this->post['start']!=""? trim($this->post['start']):"";
  258. $end =isset($this->post['end'])&&$this->post['end']!=""? trim($this->post['end']):"";
  259. $status =isset($this->post['status'])&&$this->post['status']!==""? intval($this->post['status']):"";
  260. $tradNo =isset($this->post['tradNo'])&&$this->post['tradNo']!=""? trim($this->post['tradNo']):"";
  261. if($tradNo!==""){
  262. $condition[]=["a.tradNo","like","%$tradNo%"];
  263. }
  264. if($bank!=""){
  265. $condition[]=["trade_bank","like","%$bank%"];
  266. }
  267. if($name!=""){
  268. $condition[]=["trade_out","like","%$name%"];
  269. }
  270. if($start!=""){
  271. $condition[]=["trade_time",">=",$start." 00:00:00"];
  272. }
  273. if($end!=""){
  274. $condition[]=["trade_time","<=",$end." 23:59:59"];
  275. }
  276. if($status!==""){
  277. $condition[]=["status","=",$status];
  278. }
  279. $count=Db::name("trade")->where($condition)->count();
  280. $total=ceil($count/$size);
  281. $page=$page>$total? intval($total):$page;
  282. $list =Db::name("trade")->where($condition)->page($page,$size)->order("addtime desc")->select()->toArray();
  283. foreach ($list as &$value){
  284. $data = Db::name("trade_pool")->alias("a")
  285. ->leftJoin("assoc b","a.logNo=b.viceCode")
  286. ->leftJoin("qrd_info c","b.orderCode=c.sequenceNo")
  287. ->where(["a.tradNo"=>$value['tradNo'],"a.is_del"=>0])
  288. ->order("a.addtime desc")
  289. ->field("a.*,b.orderCode,c.qrdSource,goodNo,goodName,qrdType,c.ownerName,c.ownerid,c.department,c.poCode")
  290. ->select()->toArray();
  291. $value['child']=$data;
  292. }
  293. return app_show(0,"获取成功",["list"=>$list,"count"=>$count]);
  294. }
  295. // 认领资金列表明细
  296. public function tradeList(){
  297. $page=isset($this->post['page'])&&$this->post['page']!="" ? intval($this->post['page']) :1;
  298. $size=isset($this->post['size'])&&$this->post['size']!="" ? intval($this->post['size']) :15;
  299. $condition =[["a.is_del","=",0],["b.is_del","=",0]];
  300. $name =isset($this->post['name'])&&$this->post['name']!=""? trim($this->post['name']):"";
  301. $bank =isset($this->post['bank'])&&$this->post['bank']!=""? trim($this->post['bank']):"";
  302. $start =isset($this->post['start'])&&$this->post['start']!=""? trim($this->post['start']):"";
  303. $end =isset($this->post['end'])&&$this->post['end']!=""? trim($this->post['end']):"";
  304. $status =isset($this->post['status'])&&$this->post['status']!==""? intval($this->post['status']):"";
  305. $apply_id =isset($this->post['apply_id'])&&$this->post['apply_id']!=""? intval($this->post['apply_id']):"";
  306. $apply_name =isset($this->post['apply_name'])&&$this->post['apply_name']!=""? trim($this->post['apply_name']):"";
  307. $tradNo =isset($this->post['tradNo'])&&$this->post['tradNo']!=""? trim($this->post['tradNo']):"";
  308. if($tradNo!==""){
  309. $condition[]=["a.tradNo","like","%$tradNo%"];
  310. }
  311. $logNo =isset($this->post['logNo'])&&$this->post['logNo']!=""? trim($this->post['logNo']):"";
  312. if($logNo!==""){
  313. $condition[]=["a.logNo","like","%$logNo%"];
  314. }
  315. if($bank!=""){
  316. $condition[]=["b.trade_bank","like","%$bank%"];
  317. }
  318. if($name!=""){
  319. $condition[]=["b.trade_out","like","%$name%"];
  320. }
  321. if($start!=""){
  322. $condition[]=["a.add_time",">=",$start." 00:00:00"];
  323. }
  324. if($end!=""){
  325. $condition[]=["a.add_time","<=",$end." 23:59:59"];
  326. }
  327. if($status!==""){
  328. $condition[]=["a.status","=",$status];
  329. }
  330. if($apply_id!==""){
  331. $condition[]=["a.apply_id","=",$apply_id];
  332. }
  333. if($apply_name!==""){
  334. $condition[]=["a.apply_name","like","%$apply_name%"];
  335. }
  336. $count=Db::name("trade_pool")->alias("a")
  337. ->leftJoin("trade b","a.tradNo=b.tradNo")
  338. ->leftJoin("assoc d","a.logNo=d.viceCode")
  339. ->leftJoin("qrd_info c","d.orderCode=c.sequenceNo")
  340. ->where($condition)->count();
  341. $total=ceil($count/$size);
  342. $page=$page>$total? intval($total):$page;
  343. $list =Db::name("trade_pool")->alias("a")
  344. ->leftJoin("trade b","a.tradNo=b.tradNo")
  345. ->leftJoin("assoc d","a.logNo=d.viceCode")
  346. ->leftJoin("qrd_info c","d.orderCode=c.sequenceNo")
  347. ->where($condition)
  348. ->page($page,$size)
  349. ->field("a.*,b.trade_bank,b.trade_account,b.trade_out,b.total_fee as btotal_fee,b.used_fee,b.balance,d.orderCode,c.qrdSource,c.goodNo,c.goodName,c.qrdType,c.ownerName,c.ownerid,c.department,c.poCode")
  350. ->order("a.addtime desc")->select();
  351. return app_show(0,"获取成功",["list"=>$list,"count"=>$count]);
  352. }
  353. // 获取资金下面的认领信息
  354. public function tradeQuery(){
  355. $tradNo =isset($this->post['tradNo'])&&$this->post['tradNo']!=""? trim($this->post['tradNo']):"";
  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. $condition=[["is_del","=",0]];
  360. if($apply_id!==""){
  361. $condition[]=["apply_id","=",$apply_id];
  362. }
  363. if($apply_name!==""){
  364. $condition[]=["apply_name","like","%$apply_name%"];
  365. }
  366. if($status!==""){
  367. $condition[]=["status","=",$status];
  368. }
  369. if($tradNo!==""){
  370. $condition[]=["tradNo","like","%$tradNo%"];
  371. }
  372. $list =Db::name("trade_pool")->where($condition)->order("addtime desc")->select();
  373. return app_show(0,"获取成功",$list);
  374. }
  375. //资金详情
  376. public function tradeInfo(){
  377. $tradNo =isset($this->post['tradNo'])&&$this->post['tradNo']!=""? trim($this->post['tradNo']):"";
  378. if($tradNo==""){
  379. return error_show(1004,"参数 tradNo 不能为空");
  380. }
  381. $tradinfo = Db::name("trade")->where(["tradNo"=>$tradNo,"is_del"=>0])->find();
  382. if($tradinfo==false){
  383. return error_show(1004,"资金信息未找到");
  384. }
  385. return app_show(0,"获取成功",$tradinfo);
  386. }
  387. //资金认领详情
  388. public function logInfo(){
  389. $logNo =isset($this->post['logNo'])&&$this->post['logNo']!=""? trim($this->post['logNo']):"";
  390. if($logNo==""){
  391. return error_show(1004,"参数 logNo 不能为空");
  392. }
  393. $tradinfo = Db::name("trade_pool")->where(["logNo"=>$logNo,"is_del"=>0])->find();
  394. if($tradinfo==false){
  395. return error_show(1004,"资金信息未找到");
  396. }
  397. $trade = Db::name("trade")->where(["tradNo"=>$tradinfo['tradNo'],"is_del"=>0])->find();
  398. $tradinfo['trade_out']= $trade['trade_out']??"";
  399. $tradinfo['trade_in']= $trade['trade_in']??"";
  400. $tradinfo['trade_bank']= $trade['trade_bank']??"";
  401. $tradinfo['customerNo']= $trade['customerNo']??"";
  402. $tradinfo['companyNo']= $trade['companyNo']??"";
  403. $tradinfo['total_fee']= $trade['total_fee']??"";
  404. $tradinfo['balance']= $trade['balance']??"";
  405. $tradinfo['used_fee']= $trade['used_fee']??"";
  406. $orderinfo = Db::name("assoc")->alias("a")->leftJoin("qrd_info c","a.orderCode=c.sequenceNo")
  407. ->where(["a.viceCode"=>$logNo,"a.is_del"=>0,"a.status"=>[1,2]])
  408. ->order("a.addtime desc")
  409. ->field("c.*")
  410. ->findOrEmpty();
  411. $tradinfo['orderinfo']=$orderinfo;
  412. return app_show(0,"获取成功",$tradinfo);
  413. }
  414. //认领资金退回或退款 type 1 退款 2 解除资金认领
  415. public function ReturnPay(){
  416. $logNo = isset($this->post['logNo'])&&$this->post['logNo']!="" ? trim($this->post['logNo']):"";
  417. if($logNo==''){
  418. return error_show(1004,"参数 logNo 不能为空");
  419. }
  420. $type = isset($this->post['type'])&&$this->post['type']!="" ? intval($this->post['type']):"";
  421. if($type==""){
  422. return error_show(1004,"参数 type 不能为空");
  423. }
  424. $loginfo = Db::name("trade_pool")->where(["logNo"=>$logNo,"is_del"=>0])->find();
  425. if($loginfo==false){
  426. return error_show(1004,"认领资金信息未找到");
  427. }
  428. if($loginfo['status']!=2)return error_show(1004,"认领资金信息审核未通过");
  429. $tradinfo = Db::name("trade")->where(["tradNo"=>$loginfo['tradNo'],"is_del"=>0])->find();
  430. if($tradinfo==false){
  431. return error_show(1004,"资金信息未找到");
  432. }
  433. $reason = isset($this->post['return_reason'])&&$this->post['return_reason']!="" ? trim($this->post['return_reason']):"";
  434. $remark = isset($this->post['remark'])&&$this->post['remark']!="" ? trim($this->post['remark']):"";
  435. $returnCode =makeNo("RTA");
  436. $data=[
  437. "returnCode"=>$returnCode,
  438. "logNo"=>$logNo,
  439. "tradNo"=>$loginfo['tradNo'],
  440. "type"=>$type,
  441. "apply_id"=>$this->uid,
  442. "apply_name"=>$this->uname,
  443. "return_reason"=>$reason,
  444. "remark"=>$remark,
  445. "addtime"=>date("Y-m-d H:i:s"),
  446. "updatetime"=>date("Y-m-d H:i:s")
  447. ];
  448. $in = Db::name("trade_return")->insert($data);
  449. if($in){
  450. return app_show(0,"申请新建成功");
  451. }else{
  452. return error_show(1004,"申请新建失败");
  453. }
  454. }
  455. /**退款列表
  456. * //page size tradNo logNo apply_id apply_name type status returnCode
  457. * @return \think\response\Json|void
  458. * @throws \think\db\exception\DataNotFoundException
  459. * @throws \think\db\exception\DbException
  460. * @throws \think\db\exception\ModelNotFoundException
  461. */
  462. public function returnList(){
  463. $page=isset($this->post['page'])&&$this->post['page']!='' ? intval($this->post['page']):1;
  464. $size=isset($this->post['size'])&&$this->post['size']!='' ? intval($this->post['size']):15;
  465. $tradNo =isset($this->post['tradNo'])&&$this->post['tradNo']!=''?trim($this->post['tradNo']):"";
  466. $logNo =isset($this->post['logNo'])&&$this->post['logNo']!=''?trim($this->post['logNo']):"";
  467. $apply_id =isset($this->post['apply_id'])&&$this->post['apply_id']!=''?intval($this->post['apply_id']):"";
  468. $apply_name =isset($this->post['apply_name'])&&$this->post['apply_name']!=''?trim($this->post['apply_name']):"";
  469. $type =isset($this->post['type'])&&$this->post['type']!=''?intval($this->post['type']):"";
  470. $status =isset($this->post['status'])&&$this->post['status']!==''?intval($this->post['status']):"";
  471. $returnCode =isset($this->post['returnCode'])&&$this->post['returnCode']!=''?trim($this->post['returnCode']):"";
  472. $condition=[["a.is_del","=",0]];
  473. if($tradNo!=''){
  474. $condition[]=["a.tradNo","like","%$tradNo%"];
  475. }
  476. if($logNo!=''){
  477. $condition[]=["a.logNo","like","%$logNo%"];
  478. }
  479. if($apply_id!=''){
  480. $condition[]=["a.apply_id","=",$apply_id];
  481. }
  482. if($apply_name!=''){
  483. $condition[]=["a.apply_name","like","%$apply_name%"];
  484. }
  485. if($returnCode!=''){
  486. $condition[]=["a.returnCode","like","%$returnCode%"];
  487. }
  488. if($type!=''){
  489. $condition[]=["a.type","=",$type];
  490. }
  491. if($status!=''){
  492. $condition[]=["a.status","=",$status];
  493. }
  494. $count =Db::name("trade_return")->alias("a")
  495. ->leftJoin("assoc b","a.logNo=b.viceCode")
  496. ->leftJoin("qrd_info c","c.sequenceNo=b.orderCode")
  497. ->where($condition)->count();
  498. $total=ceil($count/$size);
  499. $page = $page>$total ? intval($total):$page;
  500. $list=Db::name("trade_return") ->leftJoin("assoc b","a.logNo=b.viceCode")
  501. ->leftJoin("qrd_info c","c.sequenceNo=b.orderCode")
  502. ->field("a.*,b.orderCode,c.qrdSource,goodNo,goodName,qrdType,c.ownerName,c.ownerid,c.department,c.poCode")
  503. ->where($condition)->page($page,$size)->order("a.addtime desc")->select();
  504. return app_show(0,"获取成功",["list"=>$list,"count"=>$count]);
  505. }
  506. //退款状态审核 0 待审核 1 财务审核 2 财务驳回
  507. public function returnStatus(){
  508. $returnCode=isset($this->post['returnCode'])&&$this->post['returnCode']!=''?trim($this->post['returnCode']):"";
  509. if($returnCode==''){
  510. return error_show(1004,"参数 returnCode 不能为空");
  511. }
  512. $returninfo =Db::name("trade_return")->where(["returnCode"=>$returnCode,"is_del"=>0])->find();
  513. if($returninfo==false){
  514. return error_show(1004,"退款申请数据未找到");
  515. }
  516. $status=isset($this->post['status']) &&$this->post['status']!=''?intval($this->post['status']) : '';
  517. if($status==''){
  518. return error_show(1004,"参数 status 不能为空");
  519. }
  520. $return_img = isset($this->post['return_img'])&&$this->post['return_img']!=''?trim($this->post['return_img']):"";
  521. $remark = isset($this->post['remark'])&&$this->post['remark']!=''?trim($this->post['remark']):"";
  522. $loginfo = Db::name("trade_pool")->where(["logNo"=>$returninfo['logNo'],"is_del"=>0])->find();
  523. if($loginfo==false){
  524. return error_show(1004,"认领资金信息未找到");
  525. }
  526. if($loginfo['status']!=2)return error_show(1004,"认领资金信息审核未通过");
  527. Db::startTrans();
  528. try{
  529. $update=["status"=>$status,"return_img"=>$return_img,"remark"=>$remark,"updatetime"=>date("Y-m-d H:i:s")];
  530. $up =Db::name("trade_return")->where($returninfo)->update($update);
  531. if($up){
  532. if($status==1){
  533. $qrdArr=Db::name("assoc")->where(["viceCode"=>$returninfo['logNo'],"status"=>1,"is_del"=>0])->select()
  534. ->toArray();
  535. if(!empty($qrdArr)){
  536. foreach ($qrdArr as $value){
  537. $qrd =Db::name("qrd_info")->where(["sequenceNo"=>$value['orderCode']])->field("id,status,pay_fee,apay_fee,wpay_fee,pay_status,totalPrice")->findOrEmpty();
  538. if(empty($qrd)){
  539. Db::rollback();
  540. return error_show(1005,"未找到销售单数据");
  541. }
  542. if($qrd['pay_fee']<$value['cancel_fee']){
  543. Db::rollback();
  544. return error_show(1005,"销售单待付金额不足");
  545. }
  546. $update =[
  547. "wpay_fee"=>$qrd['wpay_fee']+$value['cancel_fee'],
  548. "apay_fee"=>$qrd['apay_fee']-$value['cancel_fee'],
  549. "pay_status"=>($qrd['apay_fee']-$value['cancel_fee'])==0 && $qrd['pay_fee']==0 ? 1:2,
  550. "status"=>$qrd['pay_fee']==0 && ($qrd['apay_fee']-$value['cancel_fee'])==0 ?0:1,
  551. "updatetime"=>date("Y-m-d H:i:s")
  552. ];
  553. $qrdup =Db::name("qrd_info")->where($qrd)->update($update);
  554. if($qrdup==false){
  555. Db::rollback();
  556. return error_show(1005,"销售单更新失败");
  557. }
  558. $asscup =[
  559. "status"=>3,
  560. "updatetime"=>date("Y-m-d H:i:s")
  561. ];
  562. $assc=Db::name("assoc")->where($value)->update($asscup);
  563. if($assc==false){
  564. Db::rollback();
  565. return error_show(1005,"销售单更新失败");
  566. }
  567. $report=ReportCode::where(["qrdNo"=>$value['orderCode']])->find();
  568. if($report)$report->setField("returnTrad",$returnCode);
  569. }
  570. }
  571. $logup =["status"=>$returninfo['type']==1?5:4,"updatetime"=>date("Y-m-d H:i:s")];
  572. $upde =Db::name("trade_pool")->where($loginfo)->update($logup);
  573. if($upde==false){
  574. Db::rollback();
  575. return error_show(1005,"资金认领信息更新失败");
  576. }
  577. if($returninfo['type']==1){
  578. $trade =Db::name("trade")->where(["tradNo"=>$returninfo['tradNo'],"is_del"=>0])->find();
  579. if($trade==false){
  580. Db::rollback();
  581. return error_show(1005,"资金信息未找到");
  582. }
  583. if($trade['used_fee']<$loginfo['total_fee']){
  584. Db::rollback();
  585. return error_show(1005,"资金信息已认领金额不足");
  586. }
  587. $tradup =[
  588. "used_fee"=>$trade['used_fee']-$loginfo['total_fee'],
  589. "balance"=>$trade['balance']+$loginfo['total_fee'],
  590. "status"=>($trade['used_fee']-$loginfo['total_fee'])==0 ? 0 :1,
  591. "updatetime"=>date("Y-m-d H:i:s")
  592. ];
  593. $updaT=Db::name("trade")->where($trade)->update($tradup);
  594. if($updaT==false){
  595. Db::rollback();
  596. return error_show(1005,"资金信息更新失败");
  597. }
  598. }
  599. }
  600. Db::commit();
  601. return app_show(0,"退款申请审核成功");
  602. }
  603. Db::rollback();
  604. return error_show(1004,'审核状态失败');
  605. }catch (\Exception $e){
  606. Db::rollback();
  607. return error_show(1004,$e->getMessage());
  608. }
  609. }
  610. //退款申请详情
  611. public function returnInfo(){
  612. $returnCode=isset($this->post['returnCode'])&&$this->post['returnCode']!=''?trim($this->post['returnCode']):"";
  613. if($returnCode==''){
  614. return error_show(1004,"参数 returnCode 不能为空");
  615. }
  616. $returninfo =Db::name("trade_return")->where(["returnCode"=>$returnCode,"is_del"=>0])->find();
  617. if($returninfo==false){
  618. return error_show(1004,"退款申请数据未找到");
  619. }
  620. $trade = Db::name("trade")->where(["tradNo"=>$returninfo['tradNo'],"is_del"=>0])->find();
  621. $returninfo['trade_out']= $trade['trade_out']??"";
  622. $returninfo['trade_in']= $trade['trade_in']??"";
  623. $returninfo['trade_bank']= $trade['trade_bank']??"";
  624. $returninfo['trade_time']= $trade['trade_time']??"";
  625. $returninfo['customerNo']= $trade['customerNo']??"";
  626. $returninfo['companyNo']= $trade['companyNo']??"";
  627. $returninfo['total_fee']= $trade['total_fee']??"";
  628. $returninfo['balance']= $trade['balance']??"";
  629. $returninfo['used_fee']= $trade['used_fee']??"";
  630. $pool =Db::name("trade_pool")->where(["logNo"=>$returninfo['logNo'],"is_del"=>0])->find();
  631. $returninfo['log_total_fee']= $pool['total_fee']??"";
  632. $returninfo['log_apply_id']= $pool['apply_id']??"";
  633. $returninfo['log_apply_name']= $pool['apply_name']??"";
  634. $orderinfo = Db::name("assoc")->alias("a")->leftJoin("qrd_info c","a.orderCode=c.sequenceNo")
  635. ->where(["a.viceCode"=>$returninfo['logNo'],"a.is_del"=>0,"a.status"=>[1,2]])
  636. ->order("a.addtime desc")
  637. ->field("c.*")
  638. ->findOrEmpty();
  639. $returninfo['orderinfo'] = $orderinfo;
  640. return app_show(0,"获取成功",$returninfo);
  641. }
  642. /**
  643. * 显示创建资源表单页.
  644. *
  645. * @return \think\Response
  646. */
  647. public function importTrade()
  648. {
  649. $files = $this->request->file("excel");
  650. $data = upload($files,$files->getOriginalExtension());
  651. if($data['code']!=0){
  652. return error_show(1004,$data['msg']);
  653. }
  654. $trade = $data['data'];
  655. $list = [];
  656. foreach ( $trade as $key => $value) {
  657. if (!isset($value[0]) || $value[0] == '') {
  658. //return ['code' => 1003, "msg" => '交易时间不能为空'];
  659. return error_show( 1003, '收款方公司编号不能为空!');
  660. }
  661. if (!isset($value[4]) || $value[4] == '') {
  662. // return ['code' => 1003, "msg" => '交易行名不能为空'];
  663. return error_show( 1003, '收入金额不能为空!');
  664. }
  665. if (!isset($value[8]) || $value[8] == '') {
  666. // return ['code' => 1003, "msg" => '对方账户不能为空'];
  667. return error_show( 1003, '对方账户不能为空!');
  668. }
  669. if (!isset($value[9]) || $value[9] == '') {
  670. // return ['code' => 1003, "msg" => '对方户名不能为空'];
  671. return error_show( 1003, '对方户名不能为空!');
  672. }
  673. if ($key == 0) {
  674. if ($value[0] != '收款方公司编号') {
  675. // return ['code' => 1003, "msg" => '模板第一列为必须为交易时间!'];
  676. return error_show( 1003, '模板第一列为必须为收款方公司编号!');
  677. }
  678. if ($value[4] != '收入金额') {
  679. // return ['code' => 1003, "msg" => '模板第二列为必须为收入金额!'];
  680. return error_show( 1003, '模板第五列为必须为收入金额!');
  681. }
  682. if ($value[6] != '交易行名') {
  683. //return ['code' => 1003, "msg" => '模板第五列为必须为交易行名!'];
  684. return error_show( 1003, '模板第七列为必须为交易行名!');
  685. }
  686. if ($value[8] != '对方账号') {
  687. // return ['code' => 1003, "msg" => '模板第七列为必须为对方账号!'];
  688. return error_show( 1003, '模板第九列为必须为对方账号!');
  689. }
  690. if ($value[9] != '对方户名') {
  691. // return ['code' => 1003, "msg" => '模板第八列为必须为对方户名!'];
  692. return error_show( 1003, '模板第十列为必须为对方户名!');
  693. }
  694. continue;
  695. }
  696. $company =Db::name("company_info")->where(["companyNo"=>$value[0]])->findOrEmpty();
  697. if(empty($company)){
  698. return error_show( 1003, "业务公司编号不存在{$value[0]}");
  699. }
  700. $total_fee = $value[4];
  701. $type = 0;
  702. $time = explode(" ", $value[3]);
  703. $list[$key]["tradeTime"] = date("Y-m-d", strtotime($time[0])) . " " . (isset($time[1]) ? $time[1] : "00:00:00");
  704. $list[$key]["trade_fee"] = str_replace(",","",$total_fee);
  705. $list[$key]["trade_bank"] = $value[6];
  706. $list[$key]["trade_account"] = $value[8];
  707. $list[$key]["trade_out"] = $value[9];
  708. $list[$key]["trade_in"] = $value[1]??$company['company_name'];
  709. $list[$key]["trade_type"] = $type;
  710. $list[$key]["trade_used"] = isset($value[10]) ? $value[10] : "";
  711. $list[$key]["trade_remark"] = '';
  712. $list[$key]["companyNo"] = $value[0];
  713. $list[$key]["trade_in_account"] = $value[2]??"";
  714. }
  715. if(empty($list)){
  716. return error_show( 1003, '导入数据不能为空!');
  717. }
  718. Db::startTrans();
  719. try{
  720. $tra=[];
  721. foreach ($list as $value) {
  722. $temp = [];
  723. $temp["tradNo"] = makeStr('S');
  724. $temp['trade_time'] = $value['tradeTime'];
  725. $temp['total_fee'] = $value['trade_fee'];
  726. $temp['trade_bank'] = $value['trade_bank'];
  727. $temp['trade_account'] = $value['trade_account'];
  728. $temp['trade_type'] =$value['trade_type'];
  729. $temp['trade_out'] = $value['trade_out'];
  730. $temp['trade_in'] = $value['trade_in'];
  731. $temp['trade_in_account'] = $value['trade_in_account'];
  732. $temp['companyNo'] = $value['companyNo'];
  733. $temp['trade_used'] = $value['trade_used'];
  734. $temp['trade_remark'] = $value['trade_remark'];
  735. $temp['balance'] =$value['trade_fee'];
  736. $temp['addtime'] = date("Y-m-d H:i:s");
  737. $temp['updatetime'] = date("Y-m-d H:i:s");
  738. $tra[]=$temp;
  739. }
  740. $list = Db::name('trade')->insertAll($tra);
  741. if($list==count($tra)){
  742. Db::commit();
  743. return app_show(0, "资金导入成功");
  744. }else{
  745. Db::rollback();
  746. return app_show(1004, "资金导入失败");
  747. }
  748. }catch (\Exception $e){
  749. Db::rollback();
  750. return app_show(1004, $e->getMessage());
  751. }
  752. }
  753. }