Refund.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595
  1. <?php
  2. declare (strict_types = 1);
  3. namespace app\Admin\controller;
  4. use app\BaseController;
  5. use think\facade\Db;
  6. use think\Request;
  7. class Refund extends BaseController
  8. {
  9. /**
  10. * 显示资源列表
  11. *
  12. * @return \think\Response
  13. */
  14. public function list()
  15. {
  16. $post = $this->request->post();
  17. $token = isset($post['token']) ? trim($post['token']) : "";
  18. if ($token == "") {
  19. return error_show(101, 'token不能为空');
  20. }
  21. $effetc = VerifyTokens($token);
  22. if (!empty($effetc) && $effetc['code'] != 0) {
  23. return error_show($effetc['code'], $effetc['message']);
  24. }
  25. $condition = [['a.is_del',"=","0"]];
  26. $guserinfo =GetUserInfo($token);
  27. if(isset($guserinfo['code']) && $guserinfo['code']!=0){
  28. return error_show($guserinfo['code'],$guserinfo['message']);
  29. }
  30. $check = checkRole($guserinfo['data']['roleid'],52);
  31. if($check){
  32. $condition[]=["a.apply_id","=", $guserinfo['data']['id']];
  33. }
  34. $type=isset($post['type'])&&$post['type']!="" ?$post['type'] :1;
  35. $condition[]=["refund_type","=",$type];
  36. $companyNo = isset($post['companyNo']) && $post['companyNo'] !== "" ? trim($post['companyNo']) : "";
  37. if($companyNo!=''){
  38. $condition[]=['customerNo',"like","%{$companyNo}%"];
  39. }
  40. $relaNo = isset($post['relaNo']) && $post['relaNo'] !== "" ? trim($post['relaNo']) : "";
  41. if($relaNo!=''){
  42. $condition[]=['relaNo',"like","%{$relaNo}%"];
  43. }
  44. $start = isset($post['start']) && $post['start'] !== "" ? $post['start'] : "";
  45. if($start!=''){
  46. $condition[]=['addtime',">=",$start];
  47. }
  48. $end = isset($post['end']) && $post['end'] !== "" ? $post['end'] : "";
  49. if($end!=''){
  50. $condition[]=['addtime',"<=",$end];
  51. }
  52. $refund_status = isset($post['refund_status']) && $post['refund_status'] !== "" ? $post['refund_status'] : "";
  53. if($refund_status!=''){
  54. $condition[]=['a.refund_status',"=",$refund_status];
  55. }
  56. $inv_type = isset($post['inv_type']) && $post['inv_type'] !== "" ? $post['inv_type'] : "";
  57. if($inv_type!='' &&$type==2){
  58. $condition[]=['b.inv_type',"=",$inv_type];
  59. }
  60. $back_type = isset($post['back_type']) && $post['back_type'] !== "" ? $post['back_type'] : "";
  61. if($back_type!='' &&$type==2){
  62. $condition[]=['a.back_type',"=",$back_type];
  63. }
  64. $page = isset($post['page']) && $post['page'] != "" ? intval($post['page']) : 1;
  65. $size = isset($post['size']) && $post['size'] != "" ? intval($post['size']) : 10;
  66. $count = Db::name("refund")->alias("a")->leftJoin("invoice_pool b","a.relaNo=b.invNo")->where($condition)->count();
  67. // echo Db::name("refund")->getLastSql();
  68. $total = ceil($count / $size) > 1 ? ceil($count / $size) : 1;
  69. $page = $page >= $total ? intval($total) : $page;
  70. $list= Db::name("refund")->alias("a")->leftJoin("invoice_pool b","a.relaNo=b.invNo")->field("a.*,ifnull(b.type_check,0) as inv_type")
  71. ->where($condition)->order("addtime desc")->page($page,$size)->select()->toArray();
  72. $data=[];
  73. foreach($list as $value){
  74. $temp=[];
  75. $temp['id'] = $value['id'];
  76. $temp['refundNo'] = $value['refundNo'];
  77. $temp['customerNo'] = $value['customerNo'];
  78. $customer = Db::name("customer_info")->where("companyNo","=",$value['customerNo'])->find();
  79. $temp['customer'] = isset($customer['companyName']) ?$customer['companyName']:"" ;
  80. if($value['refund_type']==2){
  81. $temp['invNo'] = $value['relaNo'];
  82. }else{
  83. $temp['traNo'] = $value['relaNo'];
  84. }
  85. $temp['refund_status'] = $value['refund_status'];
  86. $temp['back_type'] = $value['back_type'];
  87. $temp['inv_type'] = $value['inv_type'];
  88. $temp['apply_name'] = $value['apply_name'];
  89. $temp['refund_fee'] = $value['refund_fee'];
  90. $temp['back_fee'] = $value['back_fee'];
  91. $temp['addtime'] = $value['addtime'];
  92. $data[]=$temp;
  93. }
  94. return app_show(0,"获取成功",["list"=>$data,"count"=>$count]);
  95. }
  96. /**
  97. * 显示创建资源表单页.
  98. *
  99. * @return \think\Response
  100. */
  101. public function create()
  102. {
  103. $post = $this->request->post();
  104. $token = isset($post['token']) ? trim($post['token']) : "";
  105. if ($token == "") {
  106. return error_show(101, 'token不能为空');
  107. }
  108. $effetc = VerifyTokens($token);
  109. if (!empty($effetc) && $effetc['code'] != 0) {
  110. return error_show($effetc['code'], $effetc['message']);
  111. }
  112. $guserinfo =GetUserInfo($token);
  113. if(isset($guserinfo['code']) && $guserinfo['code']!=0){
  114. return error_show($guserinfo['code'],$guserinfo['message']);
  115. }
  116. $type=isset($post['type'])&&$post['type']!="" ?$post['type'] :1;
  117. $relaNo = isset($post['relaNo'])&&$post['relaNo']!="" ? trim($post['relaNo']) :"";
  118. if($relaNo==""){
  119. return error_show(1004,"参数relaNo 不能为空");
  120. }
  121. $back_type = isset($post['backType'])&&$post['backType']!="" ? $post['backType'] : 0;
  122. if($type==2){
  123. if($back_type==0){
  124. return error_show(1004,"参数backType 不能为空");
  125. }
  126. $inv= Db::name("invoice_pool")->where([["is_del","=",0],['invNo',"=",$relaNo]])->find();
  127. if(empty($inv)){
  128. return error_show(1004,"未找到数据");
  129. }
  130. if($inv['refund_status']!=0){
  131. return error_show(1004,"发票已在退票中");
  132. }
  133. $customer = $inv['inv_in'];
  134. $total_fee= $inv['inv_value'];
  135. }else{
  136. $trade =Db::name("trade_pool")->where([['is_del',"=",0],['traNo',"=",$relaNo]])->find();
  137. if(empty($trade)){
  138. return error_show(1004,"未找到数据");
  139. }
  140. if($trade['refund_status']!=0){
  141. return error_show(1004,"资金已在退款中");
  142. }
  143. $customer = $trade['companyNo'];
  144. $total_fee= $trade['total_fee'];
  145. }
  146. $data=[
  147. "refundNo"=>makeNo("TH"),
  148. "customerNo"=> $customer,
  149. "refund_type"=>$type,
  150. "relaNo"=>$relaNo,
  151. "refund_fee"=>$total_fee,
  152. "apply_id"=>$guserinfo['data']['id'],
  153. "apply_name"=>$guserinfo['data']['nickname'],
  154. "refund_status"=>1,
  155. "backNo"=>"",
  156. "back_type"=>$back_type,
  157. "addtime"=>date("Y-m-d H:i:s"),
  158. "updatetime"=>date("Y-m-d H:i:s")
  159. ];
  160. Db::startTrans();
  161. try{
  162. $assoc = Db::name("assoc_list")->where([["is_del","=",0],["codeNo|viceNo","=",$relaNo],["bstatus","=",1]])->select()->toArray();
  163. if(!empty($assoc)){
  164. foreach ($assoc as $value){
  165. if($value['status']!=3){
  166. Db::rollback();
  167. return error_show(1006,"存在未完成的流程");
  168. }
  169. if($type==1){
  170. if(in_array($value['rela_form'],[4,5])){
  171. Db::rollback();
  172. return error_show(1006,"存在关联的发票,请先走退票流程");
  173. }
  174. }
  175. if($value['rela_form']==2 || $value['rela_form']==1){
  176. $orderlist = Db::name("order_info")->where([['orderNo',"=",$value['codeNo']],['status',"=",
  177. 1]])->select()->toArray();
  178. if(!empty($orderlist)){
  179. $qrd = array_column($orderlist,"qrdNo");
  180. $qrdcp = array_column($orderlist,"goodNo");
  181. Db::name("qrd")->where([['sequenceNo',"in",$qrd]])->save(["status"=>2,"updatetime"=>date("Y-m-d H:i:s")]);
  182. Db::name("qrd_info")->where([['sequenceNo',"in",$qrdcp]])->save(["status"=>2]);
  183. }
  184. }
  185. if($value['rela_form']==3){
  186. $orderlist = Db::name("order_info")->where([['orderNo',"=",$value['viceNo']],['status',"=",
  187. 1]])->select()->toArray();
  188. if(!empty($orderlist)){
  189. $qrd = array_column($orderlist,"qrdNo");
  190. $qrdcp = array_column($orderlist,"goodNo");
  191. Db::name("qrd")->where([['sequenceNo',"in",$qrd]])->save(["status"=>2,"updatetime"=>date("Y-m-d H:i:s")]);
  192. Db::name("qrd_info")->where([['sequenceNo',"in",$qrdcp]])->save(["status"=>2]);
  193. }
  194. }
  195. }
  196. }
  197. $return = Db::name("refund")->insert($data);
  198. if($return){
  199. if($type==1){
  200. $trade['refund_status']=1;
  201. $trade['updatetime']=date('Y-m-d H:i:s');
  202. $up = Db::name('trade_pool')->save($trade);
  203. }else{
  204. $inv['refund_status']=1;
  205. $inv['updatetime']=date('Y-m-d H:i:s');
  206. $up = Db::name('invoice_pool')->save($inv);
  207. }
  208. if($up){
  209. Db::commit();
  210. return app_show(0,"新建成功");
  211. }
  212. }
  213. Db::rollback();
  214. return error_show(1004,"新建失败");
  215. }catch (\Exception $e){
  216. Db::rollback();
  217. return error_show(1004,$e->getMessage());
  218. }
  219. }
  220. /**
  221. * 保存新建的资源
  222. *
  223. * @param \think\Request $request
  224. * @return \think\Response
  225. */
  226. public function ExamStatus()
  227. {
  228. $post = $this->request->post();
  229. $token = isset($post['token']) ? trim($post['token']) : "";
  230. if ($token == "") {
  231. return error_show(101, 'token不能为空');
  232. }
  233. $effetc = VerifyTokens($token);
  234. if (!empty($effetc) && $effetc['code'] != 0) {
  235. return error_show($effetc['code'], $effetc['message']);
  236. }
  237. $relaNo = isset($post['refundNo'])&&$post['refundNo']!=""?trim($post['refundNo']):"";
  238. if($relaNo==""){
  239. return error_show(1004,"参数refundNo 不能为空");
  240. }
  241. $refund = Db::name("refund")->where([['refundNo',"=",$relaNo],["is_del","=",0]])->find();
  242. if(empty($refund)){
  243. return error_show(1005,"未找到数据");
  244. }
  245. $status=isset($post['status'])&&$post['status']!=="" ? $post['status']:"";
  246. if($status===""){
  247. return error_show(1004,"参数status 不能为空");
  248. }
  249. $remark= isset($post['remark'])&&$post['remark']!=""?trim($post['remark']):"";
  250. if($refund['refund_type']==2){
  251. if(!in_array($status,[1,2,3,4])){
  252. return error_show(1004,"参数status 无效");
  253. }
  254. if(($status==2&& $refund['refund_status']!=1) || ($status==3&& $refund['refund_status']!=2)|| ($status==4&&
  255. $refund['refund_status']!=3)){
  256. return error_show(1003,"数据状态有误");
  257. }
  258. } else{
  259. if(!in_array($status,[1,2,3,5,6])){
  260. return error_show(1004,"参数status 无效");
  261. }
  262. if(($status==2&& !in_array($refund['refund_status'],[1,5])) || ($status==3&& !in_array($refund['refund_status'],[2,6]))){
  263. return error_show(1003,"数据状态有误");
  264. }
  265. }
  266. Db::startTrans();
  267. try{
  268. $refund['refund_status']=$status;
  269. $refund['remark']=$remark;
  270. $refund['updatetime']=date("Y-m-d H:i:s");
  271. $update= Db::name("refund")->save($refund);
  272. if($update){
  273. if($refund['refund_type']==2&&$status==4){
  274. $invpol = Db::name("invoice_pool")->where("invNo","=",$refund['relaNo'])->save(["refund_status"=>3,"updatetime"=>date("Y-m-d H:i:s")]);
  275. if($invpol){
  276. $assoc= Db::name("assoc_list")->where([["is_del","=",0],["viceNo|codeNo","=",$refund['relaNo']],["bstatus","=",1]])->select()->toArray();
  277. if(!empty($assoc)){
  278. foreach ($assoc as $key=>$value){
  279. if($value['rela_form']==1){
  280. Db::name("order_pool")->where([["orderNo", "=", $value['codeNo']], ["is_del", "=", 0]])->save(['is_del'=>1,"updatetime"=>date("Y-m-d H:i:s")]);
  281. $order= $this->qrd_delet($value['codeNo'],$value['cancel_fee']);
  282. if(!$order){
  283. Db::rollback();
  284. return error_show(1005,"状态修改失败");
  285. }
  286. }
  287. if($value['rela_form']==4){
  288. $trade = Db::name("trade_pool")->where([['traNo',"=",$value['viceNo']],["is_del","=",0]])->find();
  289. if($trade){
  290. $trade['inv_fee'] -=$value['cancel_fee'];
  291. $trade['winv_fee'] +=$value['cancel_fee'];
  292. $trade['inv_status'] =$trade['winv_fee']==$trade['total_fee'] ? 1:2;
  293. $trade['updatetime'] =date("Y-m-d H:i:s");
  294. Db::name("trade_pool")->save($trade);
  295. }
  296. }
  297. if($value['rela_form']==5){
  298. $trade = Db::name("trade_pool")->where([['traNo',"=",$value['codeNo']],["is_del","=",0]])->find();
  299. if($trade){
  300. $trade['inv_fee'] -=$value['cancel_fee'];
  301. $trade['winv_fee'] +=$value['cancel_fee'];
  302. $trade['inv_status'] =$trade['winv_fee']==$trade['total_fee'] ? 1:2;
  303. $trade['updatetime'] =date("Y-m-d H:i:s");
  304. Db::name("trade_pool")->save($trade);
  305. }
  306. }
  307. Db::name("assoc_rela")->where([["assocNo","=",$value['assocNo']],["viceNo","=",
  308. $value['viceNo']],["status","=",1]])->save(["status"=>0,"updatetime"=>date("Y-m-d H:i:s")]);
  309. }
  310. }
  311. Db::commit();
  312. return app_show(0,"发票退票审核通过");
  313. }
  314. Db::rollback();
  315. return error_show(1005,"状态修改失败");
  316. }
  317. if($refund['refund_type']==1&&$status==3){
  318. $tradepool = Db::name("trade_pool")->where("traNo","=",$refund['relaNo'])->save
  319. (["refund_status"=>2,"updatetime"=>date("Y-m-d H:i:s")]);
  320. if($tradepool){
  321. $assoc= Db::name("assoc_list")->where([["is_del","=",0],["viceNo|codeNo","=",$refund['relaNo']],["bstatus","=",1]])->select()->toArray();
  322. if(!empty($assoc)){
  323. foreach ($assoc as $key=>$value){
  324. Db::name("assoc_rela")->where([["assocNo","=",$value['assocNo']],["viceNo","=",
  325. $value['viceNo']],["status","=",1]])->save(["status"=>0,"updatetime"=>date("Y-m-d H:i:s")]);
  326. }
  327. }
  328. Db::commit();
  329. return app_show(0,"资金退款审核通过");
  330. }
  331. Db::rollback();
  332. return error_show(1005,"状态修改失败");
  333. }
  334. Db::commit();
  335. return app_show(0,"审核通过");
  336. }
  337. Db::rollback();
  338. return error_show(1005,"状态修改失败");
  339. }catch (\Exception $e){
  340. Db::rollback();
  341. return error_show(1005,$e->getMessage());
  342. }
  343. }
  344. /**
  345. * 显示指定的资源
  346. *
  347. * @param int $id
  348. * @return \think\Response
  349. */
  350. public function read()
  351. {
  352. $post = $this->request->post();
  353. $token = isset($post['token']) ? trim($post['token']) : "";
  354. if ($token == "") {
  355. return error_show(101, 'token不能为空');
  356. }
  357. $effetc = VerifyTokens($token);
  358. if (!empty($effetc) && $effetc['code'] != 0) {
  359. return error_show($effetc['code'], $effetc['message']);
  360. }
  361. $relaNo = isset($post['refundNo'])&&$post['refundNo']!=""?trim($post['refundNo']):"";
  362. if($relaNo==""){
  363. return error_show(1004,"参数relaNo 不能为空");
  364. }
  365. $refund = Db::name("refund")->where([['refundNo',"=",$relaNo],["is_del","=",0]])->find();
  366. if(empty($refund)){
  367. return error_show(1005,"未找到数据");
  368. }
  369. $customer = Db::name('customer_info')->where("companyNo","=",$refund['customerNo'])->find();
  370. $refund['companyName']= isset($customer['companyName']) ? $customer['companyName']:"";
  371. $refund['invinfo']=[];
  372. $refund['tradeinfo']=[];
  373. if($refund['refund_type']==1){
  374. $tradeinfo = Db::name("trade_list")->where([['traNo',"=",$refund['relaNo']]])->find();
  375. if(empty($tradeinfo)){
  376. return error_show(1005,"未找到资金数据");
  377. }
  378. $customer = Db::name('customer_info')->where("companyNo","=",$tradeinfo['companyNo'])->find();
  379. $tradeinfo['companyName']= isset($customer['companyName']) ? $customer['companyName']:"";
  380. $refund['tradeinfo'] = $tradeinfo;
  381. }
  382. if($refund['refund_type']==2){
  383. $invinfo = Db::name("invoice_pool")->where([['invNo',"=",$refund['relaNo']]])->find();
  384. if(empty($invinfo)){
  385. return error_show(1005,"未找到资金数据");
  386. }
  387. $customer = Db::name('customer_info')->where("companyNo","=",$invinfo['inv_in'])->find();
  388. $invinfo['companyName']= isset($customer['companyName']) ? $customer['companyName']:"";
  389. $refund['invinfo'] = $invinfo;
  390. }
  391. return app_show(0,"获取成功",$refund);
  392. }
  393. /**
  394. * 显示编辑资源表单页.
  395. *
  396. * @param int $id
  397. * @return \think\Response
  398. */
  399. public function isRefund()
  400. {
  401. $post = $this->request->post();
  402. $token = isset($post['token']) ? trim($post['token']) : "";
  403. if ($token == "") {
  404. return error_show(101, 'token不能为空');
  405. }
  406. $effetc = VerifyTokens($token);
  407. if (!empty($effetc) && $effetc['code'] != 0) {
  408. return error_show($effetc['code'], $effetc['message']);
  409. }
  410. $type=isset($post['type'])&&$post['type']!="" ?$post['type'] :1;
  411. $relaNo = isset($post['relaNo'])&&$post['relaNo']!="" ? trim($post['relaNo']) :"";
  412. if($relaNo==""){
  413. return error_show(1004,"参数relaNo 不能为空");
  414. }
  415. if($type==2){
  416. $inv= Db::name("invoice_pool")->where([["is_del","=",0],['invNo',"=",$relaNo]])->find();
  417. if(empty($inv)){
  418. return error_show(1004,"未找到数据");
  419. }
  420. }else{
  421. $trade =Db::name("trade_pool")->where([['is_del',"=",0],['traNo',"=",$relaNo]])->find();
  422. if(empty($trade)){
  423. return error_show(1004,"未找到数据");
  424. }
  425. }
  426. $assoc = Db::name("assoc_list")->where([["is_del","=",0],["codeNo|viceNo","=",$relaNo]])->select()->toArray();
  427. if(!empty($assoc)){
  428. foreach ($assoc as $value){
  429. if($value['status']!=3){
  430. return error_show(1006,"存在未完成的流程");
  431. }
  432. if($type==1){
  433. if(in_array($value['rela_form'],[4,5])){
  434. return error_show(1006,"存在关联的发票,请先走退票流程");
  435. }
  436. }
  437. }
  438. }
  439. return app_show(0,"验证成功");
  440. }
  441. /**
  442. * 保存更新的资源
  443. *
  444. * @param \think\Request $request
  445. * @param int $id
  446. * @return \think\Response
  447. */
  448. public function update(Request $request, $id)
  449. {
  450. //
  451. }
  452. /**
  453. * 删除指定资源
  454. *
  455. * @param int $id
  456. * @return \think\Response
  457. */
  458. public function delete()
  459. {
  460. $post = $this->request->post();
  461. $token = isset($post['token']) ? trim($post['token']) : "";
  462. if ($token == "") {
  463. return error_show(101, 'token不能为空');
  464. }
  465. $effetc = VerifyTokens($token);
  466. if (!empty($effetc) && $effetc['code'] != 0) {
  467. return error_show($effetc['code'], $effetc['message']);
  468. }
  469. $relaNo = isset($post['refundNo'])&&$post['refundNo']!=""?trim($post['refundNo']):"";
  470. if($relaNo==""){
  471. return error_show(1004,"参数relaNo 不能为空");
  472. }
  473. $refund = Db::name("refund")->where([['refundNo',"=",$relaNo],["is_del","=",0]])->find();
  474. if(empty($refund)){
  475. return error_show(1005,"未找到数据");
  476. }
  477. $remark= isset($post['remark'])&&$post['remark']!=""?trim($post['remark']):"";
  478. Db::rollback();
  479. try{
  480. $refund['is_del']=1;
  481. $refund['refund_status']=0;
  482. $refund['updatetime']=date("Y-m-d H:i:s");
  483. $del = Db::name("refund")->save($refund);
  484. if($del){
  485. if($refund['refund_type']==1){
  486. $trad = Db::name("trade_pool")->where([['traNo',"=",$refund['relaNo']],['is_del',"=",0]])->save(["refund_status"=>0,"updatetime"=>date("Y-m-d H:i:s")]);
  487. if($trad){
  488. Db::commit();
  489. return app_show(0,"退款申请已取消");
  490. }
  491. };
  492. if($refund['refund_type']==2){
  493. $inv = Db::name("invoice_pool")->where([['invNo',"=",$refund['relaNo']],['is_del',"=",0]])->save(["refund_status"=>0,"updatetime"=>date("Y-m-d H:i:s")]);
  494. if($inv){
  495. Db::commit();
  496. return app_show(0,"退票申请已取消");
  497. }
  498. }
  499. }
  500. Db::rollback();
  501. return error_show(1005,"申请取消失败");
  502. }catch (\Exception $e){
  503. Db::rollback();
  504. return error_show(1005,$e->getMessage());
  505. }
  506. }
  507. private function qrd_delet($orderNo,$balance){
  508. $orderinfo = Db::name("order_pool")->where([["orderNo", "=", $orderNo], ["is_del", "=",1]])->find();
  509. if (empty($orderinfo)) {
  510. return false;
  511. }
  512. $orderlist = Db::name("order_info")->where([["orderNo", "=", $orderNo], ["status", "=", 1]])->select();
  513. if (empty($orderlist)) {
  514. return false;
  515. }
  516. foreach ($orderlist as $value) {
  517. if($balance==0){
  518. break;
  519. }
  520. if($value['ainv_fee'] == 0){
  521. continue;
  522. }
  523. $order_fee=0;
  524. if($value['ainv_fee']<=$balance){
  525. $order_fee = $value['ainv_fee'];
  526. $balance -=$value['ainv_fee'];
  527. $value['winv_fee'] += $value['ainv_fee'];
  528. $value['ainv_fee'] = 0;
  529. }else{
  530. $value['ainv_fee'] -= $balance;
  531. $value['winv_fee']+= $balance;
  532. $order_fee = $balance;
  533. $balance=0;
  534. }
  535. $value['updatetime'] = date("Y-m-d H:i:s");
  536. $orde = Db::name("order_info")->save($value);
  537. if(!$orde){
  538. return false;
  539. }
  540. $qrdinfo = Db::name("qrd_info")->where([["sequenceNo", "=", $value['goodNo']]])->field("id,Number1618248813613,ShortText1617650701648,Number1618249202608,Number1618249205231,Number1618249146997,number1618249149738,ShortText1618559043560,number1618249258956,ShortText1618559274859,inving_fee,paying_fee")->find();
  541. if ($order_fee <= $qrdinfo['Number1618249202608']) {
  542. $qrdinfo['Number1618249202608'] -= $order_fee;
  543. $qrdinfo['Number1618249205231'] += $order_fee;
  544. $qrdinfo['number1618249258956'] -= $value['good_num'];
  545. $qrdinfo['status'] =0;
  546. $qrdinfo['ShortText1618559274859'] = $qrdinfo['Number1618248813613'] == $qrdinfo['Number1618249205231'] ? "1" : "2";
  547. $qinfo = Db::name("qrd_info")->save($qrdinfo);
  548. if ($qinfo) {
  549. $qrdp = Db::name("qrd")->where([["sequenceNo", "=", $qrdinfo['ShortText1617650701648']]])->find();
  550. if ($qrdp['ainv_fee'] >= $order_fee) {
  551. $qrdp['winv_fee'] += $order_fee;
  552. $qrdp['ainv_fee'] -= $order_fee;
  553. $qrdinfo['status'] =1;
  554. $qrdp['inv_status'] = $qrdp['winv_fee'] ==$qrdp['total_fee'] ?1 : 2;
  555. $qrd = Db::name("qrd")->save($qrdp);
  556. if(!$qrd){
  557. return false;
  558. }
  559. }else {
  560. return false;
  561. }
  562. }else {
  563. return false;
  564. }
  565. } else {
  566. return false;
  567. }
  568. }
  569. return true;
  570. }
  571. }