OrderPay.php 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725
  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();
  283. return app_show(0,"获取成功",["list"=>$list,"count"=>$count]);
  284. }
  285. // 认领资金列表明细
  286. public function tradeList(){
  287. $page=isset($this->post['page'])&&$this->post['page']!="" ? intval($this->post['page']) :1;
  288. $size=isset($this->post['size'])&&$this->post['size']!="" ? intval($this->post['size']) :15;
  289. $condition =[["a.is_del","=",0],["b.is_del","=",0]];
  290. $name =isset($this->post['name'])&&$this->post['name']!=""? trim($this->post['name']):"";
  291. $bank =isset($this->post['bank'])&&$this->post['bank']!=""? trim($this->post['bank']):"";
  292. $start =isset($this->post['start'])&&$this->post['start']!=""? trim($this->post['start']):"";
  293. $end =isset($this->post['end'])&&$this->post['end']!=""? trim($this->post['end']):"";
  294. $status =isset($this->post['status'])&&$this->post['status']!==""? intval($this->post['status']):"";
  295. $apply_id =isset($this->post['apply_id'])&&$this->post['apply_id']!=""? intval($this->post['apply_id']):"";
  296. $apply_name =isset($this->post['apply_name'])&&$this->post['apply_name']!=""? trim($this->post['apply_name']):"";
  297. $tradNo =isset($this->post['tradNo'])&&$this->post['tradNo']!=""? trim($this->post['tradNo']):"";
  298. if($tradNo!==""){
  299. $condition[]=["a.tradNo","like","%$tradNo%"];
  300. }
  301. $logNo =isset($this->post['logNo'])&&$this->post['logNo']!=""? trim($this->post['logNo']):"";
  302. if($logNo!==""){
  303. $condition[]=["a.logNo","like","%$logNo%"];
  304. }
  305. if($bank!=""){
  306. $condition[]=["b.trade_bank","like","%$bank%"];
  307. }
  308. if($name!=""){
  309. $condition[]=["b.trade_out","like","%$name%"];
  310. }
  311. if($start!=""){
  312. $condition[]=["a.add_time",">=",$start." 00:00:00"];
  313. }
  314. if($end!=""){
  315. $condition[]=["a.add_time","<=",$end." 23:59:59"];
  316. }
  317. if($status!==""){
  318. $condition[]=["a.status","=",$status];
  319. }
  320. if($apply_id!==""){
  321. $condition[]=["a.apply_id","=",$apply_id];
  322. }
  323. if($apply_name!==""){
  324. $condition[]=["a.apply_name","like","%$apply_name%"];
  325. }
  326. $count=Db::name("trade_pool")->alias("a")->leftJoin("trade b","a.tradNo=b.tradNo")->where($condition)->count();
  327. $total=ceil($count/$size);
  328. $page=$page>$total? intval($total):$page;
  329. $list =Db::name("trade_pool")->alias("a")->leftJoin("trade b","a.tradNo=b.tradNo")->where($condition)
  330. ->page($page,$size)->field("a.*,b.trade_bank,b.trade_account,b.trade_out,b.total_fee as btotal_fee,b.used_fee,b.balance")->order("a.addtime desc")->select();
  331. return app_show(0,"获取成功",["list"=>$list,"count"=>$count]);
  332. }
  333. // 获取资金下面的认领信息
  334. public function tradeQuery(){
  335. $tradNo =isset($this->post['tradNo'])&&$this->post['tradNo']!=""? trim($this->post['tradNo']):"";
  336. $status =isset($this->post['status'])&&$this->post['status']!=""? intval($this->post['status']):"";
  337. $apply_id =isset($this->post['apply_id'])&&$this->post['apply_id']!=""? intval($this->post['apply_id']):"";
  338. $apply_name =isset($this->post['apply_name'])&&$this->post['apply_name']!=""? trim($this->post['apply_name']):"";
  339. $condition=[["is_del","=",0]];
  340. if($apply_id!==""){
  341. $condition[]=["apply_id","=",$apply_id];
  342. }
  343. if($apply_name!==""){
  344. $condition[]=["apply_name","like","%$apply_name%"];
  345. }
  346. if($status!==""){
  347. $condition[]=["status","=",$status];
  348. }
  349. if($tradNo!==""){
  350. $condition[]=["tradNo","like","%$tradNo%"];
  351. }
  352. $list =Db::name("trade_pool")->where($condition)->order("addtime desc")->select();
  353. return app_show(0,"获取成功",$list);
  354. }
  355. //资金详情
  356. public function tradeInfo(){
  357. $tradNo =isset($this->post['tradNo'])&&$this->post['tradNo']!=""? trim($this->post['tradNo']):"";
  358. if($tradNo==""){
  359. return error_show(1004,"参数 tradNo 不能为空");
  360. }
  361. $tradinfo = Db::name("trade")->where(["tradNo"=>$tradNo,"is_del"=>0])->find();
  362. if($tradinfo==false){
  363. return error_show(1004,"资金信息未找到");
  364. }
  365. return app_show(0,"获取成功",$tradinfo);
  366. }
  367. //资金认领详情
  368. public function logInfo(){
  369. $logNo =isset($this->post['logNo'])&&$this->post['logNo']!=""? trim($this->post['logNo']):"";
  370. if($logNo==""){
  371. return error_show(1004,"参数 logNo 不能为空");
  372. }
  373. $tradinfo = Db::name("trade_pool")->where(["logNo"=>$logNo,"is_del"=>0])->find();
  374. if($tradinfo==false){
  375. return error_show(1004,"资金信息未找到");
  376. }
  377. $trade = Db::name("trade")->where(["tradNo"=>$tradinfo['tradNo'],"is_del"=>0])->find();
  378. $tradinfo['trade_out']= $trade['trade_out']??"";
  379. $tradinfo['trade_in']= $trade['trade_in']??"";
  380. $tradinfo['trade_bank']= $trade['trade_bank']??"";
  381. $tradinfo['customerNo']= $trade['customerNo']??"";
  382. $tradinfo['companyNo']= $trade['companyNo']??"";
  383. $tradinfo['total_fee']= $trade['total_fee']??"";
  384. $tradinfo['balance']= $trade['balance']??"";
  385. $tradinfo['used_fee']= $trade['used_fee']??"";
  386. return app_show(0,"获取成功",$tradinfo);
  387. }
  388. //认领资金退回或退款 type 1 退款 2 解除资金认领
  389. public function ReturnPay(){
  390. $logNo = isset($this->post['logNo'])&&$this->post['logNo']!="" ? trim($this->post['logNo']):"";
  391. if($logNo==''){
  392. return error_show(1004,"参数 logNo 不能为空");
  393. }
  394. $type = isset($this->post['type'])&&$this->post['type']!="" ? intval($this->post['type']):"";
  395. if($type==""){
  396. return error_show(1004,"参数 type 不能为空");
  397. }
  398. $loginfo = Db::name("trade_pool")->where(["logNo"=>$logNo,"is_del"=>0])->find();
  399. if($loginfo==false){
  400. return error_show(1004,"认领资金信息未找到");
  401. }
  402. if($loginfo['status']!=2)return error_show(1004,"认领资金信息审核未通过");
  403. $tradinfo = Db::name("trade")->where(["tradNo"=>$loginfo['tradNo'],"is_del"=>0])->find();
  404. if($tradinfo==false){
  405. return error_show(1004,"资金信息未找到");
  406. }
  407. $reason = isset($this->post['return_reason'])&&$this->post['return_reason']!="" ? trim($this->post['return_reason']):"";
  408. $remark = isset($this->post['remark'])&&$this->post['remark']!="" ? trim($this->post['remark']):"";
  409. $returnCode =makeNo("RTA");
  410. $data=[
  411. "returnCode"=>$returnCode,
  412. "logNo"=>$logNo,
  413. "tradNo"=>$loginfo['tradNo'],
  414. "type"=>$type,
  415. "apply_id"=>$this->uid,
  416. "apply_name"=>$this->uname,
  417. "return_reason"=>$reason,
  418. "remark"=>$remark,
  419. "addtime"=>date("Y-m-d H:i:s"),
  420. "updatetime"=>date("Y-m-d H:i:s")
  421. ];
  422. $in = Db::name("trade_return")->insert($data);
  423. if($in){
  424. return app_show(0,"申请新建成功");
  425. }else{
  426. return error_show(1004,"申请新建失败");
  427. }
  428. }
  429. /**退款列表
  430. * //page size tradNo logNo apply_id apply_name type status returnCode
  431. * @return \think\response\Json|void
  432. * @throws \think\db\exception\DataNotFoundException
  433. * @throws \think\db\exception\DbException
  434. * @throws \think\db\exception\ModelNotFoundException
  435. */
  436. public function returnList(){
  437. $page=isset($this->post['page'])&&$this->post['page']!='' ? intval($this->post['page']):1;
  438. $size=isset($this->post['size'])&&$this->post['size']!='' ? intval($this->post['size']):15;
  439. $tradNo =isset($this->post['tradNo'])&&$this->post['tradNo']!=''?trim($this->post['tradNo']):"";
  440. $logNo =isset($this->post['logNo'])&&$this->post['logNo']!=''?trim($this->post['logNo']):"";
  441. $apply_id =isset($this->post['apply_id'])&&$this->post['apply_id']!=''?intval($this->post['apply_id']):"";
  442. $apply_name =isset($this->post['apply_name'])&&$this->post['apply_name']!=''?trim($this->post['apply_name']):"";
  443. $type =isset($this->post['type'])&&$this->post['type']!=''?intval($this->post['type']):"";
  444. $status =isset($this->post['status'])&&$this->post['status']!==''?intval($this->post['status']):"";
  445. $returnCode =isset($this->post['returnCode'])&&$this->post['returnCode']!=''?trim($this->post['returnCode']):"";
  446. $condition=[["is_del","=",0]];
  447. if($tradNo!=''){
  448. $condition[]=["tradNo","like","%$tradNo%"];
  449. }
  450. if($logNo!=''){
  451. $condition[]=["logNo","like","%$logNo%"];
  452. }
  453. if($apply_id!=''){
  454. $condition[]=["apply_id","=",$apply_id];
  455. }
  456. if($apply_name!=''){
  457. $condition[]=["apply_name","like","%$apply_name%"];
  458. }
  459. if($returnCode!=''){
  460. $condition[]=["returnCode","like","%$returnCode%"];
  461. }
  462. if($type!=''){
  463. $condition[]=["type","=",$type];
  464. }
  465. if($status!=''){
  466. $condition[]=["status","=",$status];
  467. }
  468. $count =Db::name("trade_return")->where($condition)->count();
  469. $total=ceil($count/$size);
  470. $page = $page>$total ? intval($total):$page;
  471. $list=Db::name("trade_return")->where($condition)->page($page,$size)->order("addtime desc")->select();
  472. return app_show(0,"获取成功",["list"=>$list,"count"=>$count]);
  473. }
  474. //退款状态审核 0 待审核 1 财务审核 2 财务驳回
  475. public function returnStatus(){
  476. $returnCode=isset($this->post['returnCode'])&&$this->post['returnCode']!=''?trim($this->post['returnCode']):"";
  477. if($returnCode==''){
  478. return error_show(1004,"参数 returnCode 不能为空");
  479. }
  480. $returninfo =Db::name("trade_return")->where(["returnCode"=>$returnCode,"is_del"=>0])->find();
  481. if($returninfo==false){
  482. return error_show(1004,"退款申请数据未找到");
  483. }
  484. $status=isset($this->post['status']) &&$this->post['status']!=''?intval($this->post['status']) : '';
  485. if($status==''){
  486. return error_show(1004,"参数 status 不能为空");
  487. }
  488. $return_img = isset($this->post['return_img'])&&$this->post['return_img']!=''?trim($this->post['return_img']):"";
  489. $remark = isset($this->post['remark'])&&$this->post['remark']!=''?trim($this->post['remark']):"";
  490. $loginfo = Db::name("trade_pool")->where(["logNo"=>$returninfo['logNo'],"is_del"=>0])->find();
  491. if($loginfo==false){
  492. return error_show(1004,"认领资金信息未找到");
  493. }
  494. if($loginfo['status']!=2)return error_show(1004,"认领资金信息审核未通过");
  495. Db::startTrans();
  496. try{
  497. $update=["status"=>$status,"return_img"=>$return_img,"remark"=>$remark,"updatetime"=>date("Y-m-d H:i:s")];
  498. $up =Db::name("trade_return")->where($returninfo)->update($update);
  499. if($up){
  500. if($status==1){
  501. $qrdArr=Db::name("assoc")->where(["viceCode"=>$returninfo['logNo'],"status"=>1,"is_del"=>0])->select()
  502. ->toArray();
  503. if(!empty($qrdArr)){
  504. foreach ($qrdArr as $value){
  505. $qrd =Db::name("qrd_info")->where(["sequenceNo"=>$value['orderCode']])->field("id,status,pay_fee,apay_fee,wpay_fee,pay_status,totalPrice")->findOrEmpty();
  506. if(empty($qrd)){
  507. Db::rollback();
  508. return error_show(1005,"未找到销售单数据");
  509. }
  510. if($qrd['pay_fee']<$value['cancel_fee']){
  511. Db::rollback();
  512. return error_show(1005,"销售单待付金额不足");
  513. }
  514. $update =[
  515. "wpay_fee"=>$qrd['wpay_fee']+$value['cancel_fee'],
  516. "apay_fee"=>$qrd['apay_fee']-$value['cancel_fee'],
  517. "pay_status"=>($qrd['apay_fee']-$value['cancel_fee'])==0 && $qrd['pay_fee']==0 ? 1:2,
  518. "status"=>$qrd['pay_fee']==0 && ($qrd['apay_fee']-$value['cancel_fee'])==0 ?0:1,
  519. "updatetime"=>date("Y-m-d H:i:s")
  520. ];
  521. $qrdup =Db::name("qrd_info")->where($qrd)->update($update);
  522. if($qrdup==false){
  523. Db::rollback();
  524. return error_show(1005,"销售单更新失败");
  525. }
  526. $asscup =[
  527. "status"=>3,
  528. "updatetime"=>date("Y-m-d H:i:s")
  529. ];
  530. $assc=Db::name("assoc")->where($value)->update($asscup);
  531. if($assc==false){
  532. Db::rollback();
  533. return error_show(1005,"销售单更新失败");
  534. }
  535. $report=ReportCode::where(["qrdNo"=>$value['orderCode']])->find();
  536. if($report)$report->setField("returnTrad",$returnCode);
  537. }
  538. }
  539. $logup =["status"=>$returninfo['type']==1?5:4,"updatetime"=>date("Y-m-d H:i:s")];
  540. $upde =Db::name("trade_pool")->where($loginfo)->update($logup);
  541. if($upde==false){
  542. Db::rollback();
  543. return error_show(1005,"资金认领信息更新失败");
  544. }
  545. if($returninfo['type']==1){
  546. $trade =Db::name("trade")->where(["tradNo"=>$returninfo['tradNo'],"is_del"=>0])->find();
  547. if($trade==false){
  548. Db::rollback();
  549. return error_show(1005,"资金信息未找到");
  550. }
  551. if($trade['used_fee']<$loginfo['total_fee']){
  552. Db::rollback();
  553. return error_show(1005,"资金信息已认领金额不足");
  554. }
  555. $tradup =[
  556. "used_fee"=>$trade['used_fee']-$loginfo['total_fee'],
  557. "balance"=>$trade['balance']+$loginfo['total_fee'],
  558. "status"=>($trade['used_fee']-$loginfo['total_fee'])==0 ? 0 :1,
  559. "updatetime"=>date("Y-m-d H:i:s")
  560. ];
  561. $updaT=Db::name("trade")->where($trade)->update($tradup);
  562. if($updaT==false){
  563. Db::rollback();
  564. return error_show(1005,"资金信息更新失败");
  565. }
  566. }
  567. }
  568. Db::commit();
  569. return app_show(0,"退款申请审核成功");
  570. }
  571. Db::rollback();
  572. return error_show(1004,'审核状态失败');
  573. }catch (\Exception $e){
  574. Db::rollback();
  575. return error_show(1004,$e->getMessage());
  576. }
  577. }
  578. //退款申请详情
  579. public function returnInfo(){
  580. $returnCode=isset($this->post['returnCode'])&&$this->post['returnCode']!=''?trim($this->post['returnCode']):"";
  581. if($returnCode==''){
  582. return error_show(1004,"参数 returnCode 不能为空");
  583. }
  584. $returninfo =Db::name("trade_return")->where(["returnCode"=>$returnCode,"is_del"=>0])->find();
  585. if($returninfo==false){
  586. return error_show(1004,"退款申请数据未找到");
  587. }
  588. $trade = Db::name("trade")->where(["tradNo"=>$returninfo['tradNo'],"is_del"=>0])->find();
  589. $returninfo['trade_out']= $trade['trade_out']??"";
  590. $returninfo['trade_in']= $trade['trade_in']??"";
  591. $returninfo['trade_bank']= $trade['trade_bank']??"";
  592. $returninfo['trade_time']= $trade['trade_time']??"";
  593. $returninfo['customerNo']= $trade['customerNo']??"";
  594. $returninfo['companyNo']= $trade['companyNo']??"";
  595. $returninfo['total_fee']= $trade['total_fee']??"";
  596. $returninfo['balance']= $trade['balance']??"";
  597. $returninfo['used_fee']= $trade['used_fee']??"";
  598. $pool =Db::name("trade_pool")->where(["logNo"=>$returninfo['logNo'],"is_del"=>0])->find();
  599. $returninfo['log_total_fee']= $pool['total_fee']??"";
  600. $returninfo['log_apply_id']= $pool['apply_id']??"";
  601. $returninfo['log_apply_name']= $pool['apply_name']??"";
  602. return app_show(0,"获取成功",$returninfo);
  603. }
  604. /**
  605. * 显示创建资源表单页.
  606. *
  607. * @return \think\Response
  608. */
  609. public function importTrade()
  610. {
  611. $files = $this->request->file("excel");
  612. $data = upload($files,$files->getOriginalExtension());
  613. if($data['code']!=0){
  614. return error_show(1004,$data['msg']);
  615. }
  616. $trade = $data['data'];
  617. $list = [];
  618. foreach ( $trade as $key => $value) {
  619. if (!isset($value[0]) || $value[0] == '') {
  620. //return ['code' => 1003, "msg" => '交易时间不能为空'];
  621. return error_show( 1003, '收款方公司编号不能为空!');
  622. }
  623. if (!isset($value[4]) || $value[4] == '') {
  624. // return ['code' => 1003, "msg" => '交易行名不能为空'];
  625. return error_show( 1003, '收入金额不能为空!');
  626. }
  627. if (!isset($value[8]) || $value[8] == '') {
  628. // return ['code' => 1003, "msg" => '对方账户不能为空'];
  629. return error_show( 1003, '对方账户不能为空!');
  630. }
  631. if (!isset($value[9]) || $value[9] == '') {
  632. // return ['code' => 1003, "msg" => '对方户名不能为空'];
  633. return error_show( 1003, '对方户名不能为空!');
  634. }
  635. if ($key == 0) {
  636. if ($value[0] != '收款方公司编号') {
  637. // return ['code' => 1003, "msg" => '模板第一列为必须为交易时间!'];
  638. return error_show( 1003, '模板第一列为必须为收款方公司编号!');
  639. }
  640. if ($value[4] != '收入金额') {
  641. // return ['code' => 1003, "msg" => '模板第二列为必须为收入金额!'];
  642. return error_show( 1003, '模板第五列为必须为收入金额!');
  643. }
  644. if ($value[6] != '交易行名') {
  645. //return ['code' => 1003, "msg" => '模板第五列为必须为交易行名!'];
  646. return error_show( 1003, '模板第七列为必须为交易行名!');
  647. }
  648. if ($value[8] != '对方账号') {
  649. // return ['code' => 1003, "msg" => '模板第七列为必须为对方账号!'];
  650. return error_show( 1003, '模板第九列为必须为对方账号!');
  651. }
  652. if ($value[9] != '对方户名') {
  653. // return ['code' => 1003, "msg" => '模板第八列为必须为对方户名!'];
  654. return error_show( 1003, '模板第十列为必须为对方户名!');
  655. }
  656. continue;
  657. }
  658. $company =Db::name("company_info")->where(["companyNo"=>$value[0]])->findOrEmpty();
  659. if(empty($company)){
  660. return error_show( 1003, "业务公司编号不存在{$value[0]}");
  661. }
  662. $total_fee = $value[4];
  663. $type = 0;
  664. $time = explode(" ", $value[3]);
  665. $list[$key]["tradeTime"] = date("Y-m-d", strtotime($time[0])) . " " . (isset($time[1]) ? $time[1] : "00:00:00");
  666. $list[$key]["trade_fee"] = str_replace(",","",$total_fee);
  667. $list[$key]["trade_bank"] = $value[6];
  668. $list[$key]["trade_account"] = $value[8];
  669. $list[$key]["trade_out"] = $value[9];
  670. $list[$key]["trade_in"] = $value[1]??$company['company_name'];
  671. $list[$key]["trade_type"] = $type;
  672. $list[$key]["trade_used"] = isset($value[10]) ? $value[10] : "";
  673. $list[$key]["trade_remark"] = '';
  674. $list[$key]["companyNo"] = $value[0];
  675. $list[$key]["trade_in_account"] = $value[2]??"";
  676. }
  677. if(empty($list)){
  678. return error_show( 1003, '导入数据不能为空!');
  679. }
  680. Db::startTrans();
  681. try{
  682. $tra=[];
  683. foreach ($list as $value) {
  684. $temp = [];
  685. $temp["tradNo"] = makeStr('S');
  686. $temp['trade_time'] = $value['tradeTime'];
  687. $temp['total_fee'] = $value['trade_fee'];
  688. $temp['trade_bank'] = $value['trade_bank'];
  689. $temp['trade_account'] = $value['trade_account'];
  690. $temp['trade_type'] =$value['trade_type'];
  691. $temp['trade_out'] = $value['trade_out'];
  692. $temp['trade_in'] = $value['trade_in'];
  693. $temp['trade_in_account'] = $value['trade_in_account'];
  694. $temp['companyNo'] = $value['companyNo'];
  695. $temp['trade_used'] = $value['trade_used'];
  696. $temp['trade_remark'] = $value['trade_remark'];
  697. $temp['balance'] =$value['trade_fee'];
  698. $temp['addtime'] = date("Y-m-d H:i:s");
  699. $temp['updatetime'] = date("Y-m-d H:i:s");
  700. $tra[]=$temp;
  701. }
  702. $list = Db::name('trade')->insertAll($tra);
  703. if($list==count($tra)){
  704. Db::commit();
  705. return app_show(0, "资金导入成功");
  706. }else{
  707. Db::rollback();
  708. return app_show(1004, "资金导入失败");
  709. }
  710. }catch (\Exception $e){
  711. Db::rollback();
  712. return app_show(1004, $e->getMessage());
  713. }
  714. }
  715. }