OrderPay.php 33 KB

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