OrderPay.php 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878
  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. $orderCode =isset($this->post['orderCode'])&&$this->post['orderCode']!=''?trim($this->post['orderCode']):"";
  309. if($tradNo!==""){
  310. $condition[]=["a.tradNo","like","%$tradNo%"];
  311. }
  312. if($orderCode!==""){
  313. $condition[]=["d.orderCode","like","%$orderCode%"];
  314. }
  315. $logNo =isset($this->post['logNo'])&&$this->post['logNo']!=""? trim($this->post['logNo']):"";
  316. if($logNo!==""){
  317. $condition[]=["a.logNo","like","%$logNo%"];
  318. }
  319. if($bank!=""){
  320. $condition[]=["b.trade_bank","like","%$bank%"];
  321. }
  322. if($name!=""){
  323. $condition[]=["b.trade_out","like","%$name%"];
  324. }
  325. if($start!=""){
  326. $condition[]=["a.add_time",">=",$start." 00:00:00"];
  327. }
  328. if($end!=""){
  329. $condition[]=["a.add_time","<=",$end." 23:59:59"];
  330. }
  331. if($status!==""){
  332. $condition[]=["a.status","=",$status];
  333. }
  334. if($apply_id!==""){
  335. $condition[]=["a.apply_id","=",$apply_id];
  336. }
  337. if($apply_name!==""){
  338. $condition[]=["a.apply_name","like","%$apply_name%"];
  339. }
  340. $count=Db::name("trade_pool")->alias("a")
  341. ->leftJoin("trade b","a.tradNo=b.tradNo")
  342. ->leftJoin("assoc d","a.logNo=d.viceCode")
  343. ->leftJoin("qrd_info c","d.orderCode=c.sequenceNo")
  344. ->where($condition)->count();
  345. $total=ceil($count/$size);
  346. $page=$page>$total? intval($total):$page;
  347. $list =Db::name("trade_pool")->alias("a")
  348. ->leftJoin("trade b","a.tradNo=b.tradNo")
  349. ->leftJoin("assoc d","a.logNo=d.viceCode")
  350. ->leftJoin("qrd_info c","d.orderCode=c.sequenceNo")
  351. ->where($condition)
  352. ->page($page,$size)
  353. ->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")
  354. ->order("a.addtime desc")->select();
  355. return app_show(0,"获取成功",["list"=>$list,"count"=>$count]);
  356. }
  357. // 获取资金下面的认领信息
  358. public function tradeQuery(){
  359. $tradNo =isset($this->post['tradNo'])&&$this->post['tradNo']!=""? trim($this->post['tradNo']):"";
  360. $status =isset($this->post['status'])&&$this->post['status']!=""? intval($this->post['status']):"";
  361. $apply_id =isset($this->post['apply_id'])&&$this->post['apply_id']!=""? intval($this->post['apply_id']):"";
  362. $apply_name =isset($this->post['apply_name'])&&$this->post['apply_name']!=""? trim($this->post['apply_name']):"";
  363. $condition=[["is_del","=",0]];
  364. if($apply_id!==""){
  365. $condition[]=["apply_id","=",$apply_id];
  366. }
  367. if($apply_name!==""){
  368. $condition[]=["apply_name","like","%$apply_name%"];
  369. }
  370. if($status!==""){
  371. $condition[]=["status","=",$status];
  372. }
  373. if($tradNo!==""){
  374. $condition[]=["tradNo","like","%$tradNo%"];
  375. }
  376. $list =Db::name("trade_pool")->where($condition)->order("addtime desc")->select();
  377. return app_show(0,"获取成功",$list);
  378. }
  379. //资金详情
  380. public function tradeInfo(){
  381. $tradNo =isset($this->post['tradNo'])&&$this->post['tradNo']!=""? trim($this->post['tradNo']):"";
  382. if($tradNo==""){
  383. return error_show(1004,"参数 tradNo 不能为空");
  384. }
  385. $tradinfo = Db::name("trade")->where(["tradNo"=>$tradNo,"is_del"=>0])->find();
  386. if($tradinfo==false){
  387. return error_show(1004,"资金信息未找到");
  388. }
  389. return app_show(0,"获取成功",$tradinfo);
  390. }
  391. //资金认领详情
  392. public function logInfo(){
  393. $logNo =isset($this->post['logNo'])&&$this->post['logNo']!=""? trim($this->post['logNo']):"";
  394. if($logNo==""){
  395. return error_show(1004,"参数 logNo 不能为空");
  396. }
  397. $tradinfo = Db::name("trade_pool")->where(["logNo"=>$logNo,"is_del"=>0])->find();
  398. if($tradinfo==false){
  399. return error_show(1004,"资金信息未找到");
  400. }
  401. $trade = Db::name("trade")->where(["tradNo"=>$tradinfo['tradNo'],"is_del"=>0])->find();
  402. $tradinfo['trade_out']= $trade['trade_out']??"";
  403. $tradinfo['trade_in']= $trade['trade_in']??"";
  404. $tradinfo['trade_bank']= $trade['trade_bank']??"";
  405. $tradinfo['customerNo']= $trade['customerNo']??"";
  406. $tradinfo['companyNo']= $trade['companyNo']??"";
  407. $tradinfo['total_fee']= $trade['total_fee']??"";
  408. $tradinfo['balance']= $trade['balance']??"";
  409. $tradinfo['used_fee']= $trade['used_fee']??"";
  410. $orderinfo = Db::name("assoc")->alias("a")->leftJoin("qrd_info c","a.orderCode=c.sequenceNo")
  411. ->where(["a.viceCode"=>$logNo,"a.is_del"=>0,"a.status"=>[1,2]])
  412. ->order("a.addtime desc")
  413. ->field("c.*")
  414. ->findOrEmpty();
  415. $tradinfo['orderinfo']=$orderinfo;
  416. return app_show(0,"获取成功",$tradinfo);
  417. }
  418. //认领资金退回或退款 type 1 退款 2 解除资金认领
  419. public function ReturnPay(){
  420. $logNo = isset($this->post['logNo'])&&$this->post['logNo']!="" ? trim($this->post['logNo']):"";
  421. if($logNo==''){
  422. return error_show(1004,"参数 logNo 不能为空");
  423. }
  424. $type = isset($this->post['type'])&&$this->post['type']!="" ? intval($this->post['type']):"";
  425. if($type==""){
  426. return error_show(1004,"参数 type 不能为空");
  427. }
  428. $loginfo = Db::name("trade_pool")->where(["logNo"=>$logNo,"is_del"=>0])->find();
  429. if($loginfo==false){
  430. return error_show(1004,"认领资金信息未找到");
  431. }
  432. if($loginfo['status']!=2)return error_show(1004,"认领资金信息审核未通过");
  433. $tradinfo = Db::name("trade")->where(["tradNo"=>$loginfo['tradNo'],"is_del"=>0])->find();
  434. if($tradinfo==false){
  435. return error_show(1004,"资金信息未找到");
  436. }
  437. $reason = isset($this->post['return_reason'])&&$this->post['return_reason']!="" ? trim($this->post['return_reason']):"";
  438. $remark = isset($this->post['remark'])&&$this->post['remark']!="" ? trim($this->post['remark']):"";
  439. $returnCode =makeNo("RTA");
  440. $data=[
  441. "returnCode"=>$returnCode,
  442. "logNo"=>$logNo,
  443. "tradNo"=>$loginfo['tradNo'],
  444. "type"=>$type,
  445. "apply_id"=>$this->uid,
  446. "apply_name"=>$this->uname,
  447. "return_reason"=>$reason,
  448. "remark"=>$remark,
  449. "addtime"=>date("Y-m-d H:i:s"),
  450. "updatetime"=>date("Y-m-d H:i:s")
  451. ];
  452. $in = Db::name("trade_return")->insert($data);
  453. if($in){
  454. return app_show(0,"申请新建成功");
  455. }else{
  456. return error_show(1004,"申请新建失败");
  457. }
  458. }
  459. /**退款列表
  460. * //page size tradNo logNo apply_id apply_name type status returnCode
  461. * @return \think\response\Json|void
  462. * @throws \think\db\exception\DataNotFoundException
  463. * @throws \think\db\exception\DbException
  464. * @throws \think\db\exception\ModelNotFoundException
  465. */
  466. public function returnList(){
  467. $page=isset($this->post['page'])&&$this->post['page']!='' ? intval($this->post['page']):1;
  468. $size=isset($this->post['size'])&&$this->post['size']!='' ? intval($this->post['size']):15;
  469. $tradNo =isset($this->post['tradNo'])&&$this->post['tradNo']!=''?trim($this->post['tradNo']):"";
  470. $logNo =isset($this->post['logNo'])&&$this->post['logNo']!=''?trim($this->post['logNo']):"";
  471. $apply_id =isset($this->post['apply_id'])&&$this->post['apply_id']!=''?intval($this->post['apply_id']):"";
  472. $apply_name =isset($this->post['apply_name'])&&$this->post['apply_name']!=''?trim($this->post['apply_name']):"";
  473. $type =isset($this->post['type'])&&$this->post['type']!=''?intval($this->post['type']):"";
  474. $status =isset($this->post['status'])&&$this->post['status']!==''?intval($this->post['status']):"";
  475. $returnCode =isset($this->post['returnCode'])&&$this->post['returnCode']!=''?trim($this->post['returnCode']):"";
  476. $orderCode =isset($this->post['orderCode'])&&$this->post['orderCode']!=''?trim($this->post['orderCode']):"";
  477. $condition=[["a.is_del","=",0]];
  478. if($tradNo!=''){
  479. $condition[]=["a.tradNo","like","%$tradNo%"];
  480. }
  481. if($orderCode!=''){
  482. $condition[]=["b.orderCode","like","%$orderCode%"];
  483. }
  484. if($logNo!=''){
  485. $condition[]=["a.logNo","like","%$logNo%"];
  486. }
  487. if($apply_id!=''){
  488. $condition[]=["a.apply_id","=",$apply_id];
  489. }
  490. if($apply_name!=''){
  491. $condition[]=["a.apply_name","like","%$apply_name%"];
  492. }
  493. if($returnCode!=''){
  494. $condition[]=["a.returnCode","like","%$returnCode%"];
  495. }
  496. if($type!=''){
  497. $condition[]=["a.type","=",$type];
  498. }
  499. if($status!=''){
  500. $condition[]=["a.status","=",$status];
  501. }
  502. $count =Db::name("trade_return")->alias("a")
  503. ->leftJoin("assoc b","a.logNo=b.viceCode")
  504. ->leftJoin("qrd_info c","c.sequenceNo=b.orderCode")
  505. ->where($condition)->count();
  506. $total=ceil($count/$size);
  507. $page = $page>$total ? intval($total):$page;
  508. $list=Db::name("trade_return")->alias("a")->leftJoin("assoc b","a.logNo=b.viceCode")
  509. ->leftJoin("qrd_info c","c.sequenceNo=b.orderCode")
  510. ->field("a.*,b.orderCode,c.qrdSource,goodNo,goodName,qrdType,c.ownerName,c.ownerid,c.department,c.poCode")
  511. ->where($condition)->page($page,$size)->order("a.addtime desc")->select();
  512. return app_show(0,"获取成功",["list"=>$list,"count"=>$count]);
  513. }
  514. //退款状态审核 0 待审核 1 财务审核 2 财务驳回
  515. public function returnStatus(){
  516. $returnCode=isset($this->post['returnCode'])&&$this->post['returnCode']!=''?trim($this->post['returnCode']):"";
  517. if($returnCode==''){
  518. return error_show(1004,"参数 returnCode 不能为空");
  519. }
  520. $returninfo =Db::name("trade_return")->where(["returnCode"=>$returnCode,"is_del"=>0])->find();
  521. if($returninfo==false){
  522. return error_show(1004,"退款申请数据未找到");
  523. }
  524. $status=isset($this->post['status']) &&$this->post['status']!=''?intval($this->post['status']) : '';
  525. if($status==''){
  526. return error_show(1004,"参数 status 不能为空");
  527. }
  528. $return_img = isset($this->post['return_img'])&&$this->post['return_img']!=''?trim($this->post['return_img']):"";
  529. $remark = isset($this->post['remark'])&&$this->post['remark']!=''?trim($this->post['remark']):"";
  530. $loginfo = Db::name("trade_pool")->where(["logNo"=>$returninfo['logNo'],"is_del"=>0])->find();
  531. if($loginfo==false){
  532. return error_show(1004,"认领资金信息未找到");
  533. }
  534. if($loginfo['status']!=2)return error_show(1004,"认领资金信息审核未通过");
  535. Db::startTrans();
  536. try{
  537. $update=["status"=>$status,"return_img"=>$return_img,"remark"=>$remark,"updatetime"=>date("Y-m-d H:i:s")];
  538. $up =Db::name("trade_return")->where($returninfo)->update($update);
  539. if($up){
  540. if($status==1){
  541. $qrdArr=Db::name("assoc")->where(["viceCode"=>$returninfo['logNo'],"status"=>1,"is_del"=>0])->select()
  542. ->toArray();
  543. if(!empty($qrdArr)){
  544. foreach ($qrdArr as $value){
  545. $qrd =Db::name("qrd_info")->where(["sequenceNo"=>$value['orderCode']])->field("id,status,pay_fee,apay_fee,wpay_fee,pay_status,totalPrice")->findOrEmpty();
  546. if(empty($qrd)){
  547. Db::rollback();
  548. return error_show(1005,"未找到销售单数据");
  549. }
  550. if($qrd['pay_fee']<$value['cancel_fee']){
  551. Db::rollback();
  552. return error_show(1005,"销售单待付金额不足");
  553. }
  554. $update =[
  555. "wpay_fee"=>$qrd['wpay_fee']+$value['cancel_fee'],
  556. "apay_fee"=>$qrd['apay_fee']-$value['cancel_fee'],
  557. "pay_status"=>($qrd['apay_fee']-$value['cancel_fee'])==0 && $qrd['pay_fee']==0 ? 1:2,
  558. "status"=>$qrd['pay_fee']==0 && ($qrd['apay_fee']-$value['cancel_fee'])==0 ?0:1,
  559. "updatetime"=>date("Y-m-d H:i:s")
  560. ];
  561. $qrdup =Db::name("qrd_info")->where($qrd)->update($update);
  562. if($qrdup==false){
  563. Db::rollback();
  564. return error_show(1005,"销售单更新失败");
  565. }
  566. $asscup =[
  567. "status"=>3,
  568. "updatetime"=>date("Y-m-d H:i:s")
  569. ];
  570. $assc=Db::name("assoc")->where($value)->update($asscup);
  571. if($assc==false){
  572. Db::rollback();
  573. return error_show(1005,"销售单更新失败");
  574. }
  575. $report=ReportCode::where(["qrdNo"=>$value['orderCode']])->find();
  576. if($report)$report->setField("returnTrad",$returnCode);
  577. }
  578. }
  579. $logup =["status"=>$returninfo['type']==1?5:4,"updatetime"=>date("Y-m-d H:i:s")];
  580. $upde =Db::name("trade_pool")->where($loginfo)->update($logup);
  581. if($upde==false){
  582. Db::rollback();
  583. return error_show(1005,"资金认领信息更新失败");
  584. }
  585. if($returninfo['type']==1){
  586. $trade =Db::name("trade")->where(["tradNo"=>$returninfo['tradNo'],"is_del"=>0])->find();
  587. if($trade==false){
  588. Db::rollback();
  589. return error_show(1005,"资金信息未找到");
  590. }
  591. if($trade['used_fee']<$loginfo['total_fee']){
  592. Db::rollback();
  593. return error_show(1005,"资金信息已认领金额不足");
  594. }
  595. $tradup =[
  596. "used_fee"=>$trade['used_fee']-$loginfo['total_fee'],
  597. "balance"=>$trade['balance']+$loginfo['total_fee'],
  598. "status"=>($trade['used_fee']-$loginfo['total_fee'])==0 ? 0 :1,
  599. "updatetime"=>date("Y-m-d H:i:s")
  600. ];
  601. $updaT=Db::name("trade")->where($trade)->update($tradup);
  602. if($updaT==false){
  603. Db::rollback();
  604. return error_show(1005,"资金信息更新失败");
  605. }
  606. }
  607. }
  608. Db::commit();
  609. return app_show(0,"退款申请审核成功");
  610. }
  611. Db::rollback();
  612. return error_show(1004,'审核状态失败');
  613. }catch (\Exception $e){
  614. Db::rollback();
  615. return error_show(1004,$e->getMessage());
  616. }
  617. }
  618. //退款申请详情
  619. public function returnInfo(){
  620. $returnCode=isset($this->post['returnCode'])&&$this->post['returnCode']!=''?trim($this->post['returnCode']):"";
  621. if($returnCode==''){
  622. return error_show(1004,"参数 returnCode 不能为空");
  623. }
  624. $returninfo =Db::name("trade_return")->where(["returnCode"=>$returnCode,"is_del"=>0])->find();
  625. if($returninfo==false){
  626. return error_show(1004,"退款申请数据未找到");
  627. }
  628. $trade = Db::name("trade")->where(["tradNo"=>$returninfo['tradNo'],"is_del"=>0])->find();
  629. $returninfo['trade_out']= $trade['trade_out']??"";
  630. $returninfo['trade_in']= $trade['trade_in']??"";
  631. $returninfo['trade_bank']= $trade['trade_bank']??"";
  632. $returninfo['trade_time']= $trade['trade_time']??"";
  633. $returninfo['customerNo']= $trade['customerNo']??"";
  634. $returninfo['companyNo']= $trade['companyNo']??"";
  635. $returninfo['total_fee']= $trade['total_fee']??"";
  636. $returninfo['balance']= $trade['balance']??"";
  637. $returninfo['used_fee']= $trade['used_fee']??"";
  638. $pool =Db::name("trade_pool")->where(["logNo"=>$returninfo['logNo'],"is_del"=>0])->find();
  639. $returninfo['log_total_fee']= $pool['total_fee']??"";
  640. $returninfo['log_apply_id']= $pool['apply_id']??"";
  641. $returninfo['log_apply_name']= $pool['apply_name']??"";
  642. $orderinfo = Db::name("assoc")->alias("a")->leftJoin("qrd_info c","a.orderCode=c.sequenceNo")
  643. ->where(["a.viceCode"=>$returninfo['logNo'],"a.is_del"=>0,"a.status"=>[1,2]])
  644. ->order("a.addtime desc")
  645. ->field("c.*")
  646. ->findOrEmpty();
  647. $returninfo['orderinfo'] = $orderinfo;
  648. return app_show(0,"获取成功",$returninfo);
  649. }
  650. /**
  651. * 显示创建资源表单页.
  652. *
  653. * @return \think\Response
  654. */
  655. public function importTrade()
  656. {
  657. $files = $this->request->file("excel");
  658. $data = upload($files,$files->getOriginalExtension());
  659. if($data['code']!=0){
  660. return error_show(1004,$data['msg']);
  661. }
  662. $trade = $data['data'];
  663. $list = [];
  664. foreach ( $trade as $key => $value) {
  665. if (!isset($value[0]) || $value[0] == '') {
  666. //return ['code' => 1003, "msg" => '交易时间不能为空'];
  667. return error_show( 1003, '第'.($key+1).'行收款方公司编号不能为空!');
  668. }
  669. if (!isset($value[4]) || $value[4] == '') {
  670. // return ['code' => 1003, "msg" => '交易行名不能为空'];
  671. return error_show( 1003, '第'.($key+1).'行收入金额不能为空!');
  672. }
  673. if (!isset($value[8]) || $value[8] == '') {
  674. // return ['code' => 1003, "msg" => '对方账户不能为空'];
  675. return error_show( 1003, '第'.($key+1).'行对方账户不能为空!');
  676. }
  677. if (!isset($value[9]) || $value[9] == '') {
  678. // return ['code' => 1003, "msg" => '对方户名不能为空'];
  679. return error_show( 1003, '第'.($key+1).'行对方户名不能为空!');
  680. }
  681. if ($key == 0) {
  682. if ($value[0] != '收款方公司编号') {
  683. // return ['code' => 1003, "msg" => '模板第一列为必须为交易时间!'];
  684. return error_show( 1003, '模板第一列为必须为收款方公司编号!');
  685. }
  686. if ($value[4] != '收入金额') {
  687. // return ['code' => 1003, "msg" => '模板第二列为必须为收入金额!'];
  688. return error_show( 1003, '模板第五列为必须为收入金额!');
  689. }
  690. if ($value[6] != '交易行名') {
  691. //return ['code' => 1003, "msg" => '模板第五列为必须为交易行名!'];
  692. return error_show( 1003, '模板第七列为必须为交易行名!');
  693. }
  694. if ($value[8] != '对方账号') {
  695. // return ['code' => 1003, "msg" => '模板第七列为必须为对方账号!'];
  696. return error_show( 1003, '模板第九列为必须为对方账号!');
  697. }
  698. if ($value[9] != '对方户名') {
  699. // return ['code' => 1003, "msg" => '模板第八列为必须为对方户名!'];
  700. return error_show( 1003, '模板第十列为必须为对方户名!');
  701. }
  702. continue;
  703. }
  704. $company =Db::name("company_info")->where(["companyNo"=>$value[0]])->findOrEmpty();
  705. if(empty($company)){
  706. return error_show( 1003, "业务公司编号不存在{$value[0]}");
  707. }
  708. $total_fee = $value[4];
  709. $type = 0;
  710. $time = explode(" ", $value[3]);
  711. $list[$key]["tradeTime"] = date("Y-m-d", strtotime($time[0])) . " " . (isset($time[1]) ? $time[1] : "00:00:00");
  712. $list[$key]["trade_fee"] = str_replace(",","",$total_fee);
  713. $list[$key]["trade_bank"] = $value[6];
  714. $list[$key]["trade_account"] = $value[8];
  715. $list[$key]["trade_out"] = $value[9];
  716. $list[$key]["trade_in"] = $value[1]??$company['company_name'];
  717. $list[$key]["trade_type"] = $type;
  718. $list[$key]["trade_used"] = isset($value[10]) ? $value[10] : "";
  719. $list[$key]["trade_remark"] = '';
  720. $list[$key]["companyNo"] = $value[0];
  721. $list[$key]["trade_in_account"] = $value[2]??"";
  722. }
  723. if(empty($list)){
  724. return error_show( 1003, '导入数据不能为空!');
  725. }
  726. Db::startTrans();
  727. try{
  728. $tra=[];
  729. foreach ($list as $value) {
  730. $temp = [];
  731. $temp["tradNo"] = makeStr('S');
  732. $temp['trade_time'] = $value['tradeTime'];
  733. $temp['total_fee'] = $value['trade_fee'];
  734. $temp['trade_bank'] = $value['trade_bank'];
  735. $temp['trade_account'] = $value['trade_account'];
  736. $temp['trade_type'] =$value['trade_type'];
  737. $temp['trade_out'] = $value['trade_out'];
  738. $temp['trade_in'] = $value['trade_in'];
  739. $temp['trade_in_account'] = $value['trade_in_account'];
  740. $temp['companyNo'] = $value['companyNo'];
  741. $temp['trade_used'] = $value['trade_used'];
  742. $temp['trade_remark'] = $value['trade_remark'];
  743. $temp['balance'] =$value['trade_fee'];
  744. $temp['addtime'] = date("Y-m-d H:i:s");
  745. $temp['updatetime'] = date("Y-m-d H:i:s");
  746. $tra[]=$temp;
  747. }
  748. $list = Db::name('trade')->insertAll($tra);
  749. if($list==count($tra)){
  750. Db::commit();
  751. return app_show(0, "资金导入成功");
  752. }else{
  753. Db::rollback();
  754. return app_show(1004, "资金导入失败");
  755. }
  756. }catch (\Exception $e){
  757. Db::rollback();
  758. return app_show(1004, $e->getMessage());
  759. }
  760. }
  761. /**
  762. * @return \think\response\Json|void
  763. */
  764. public function importTradeByArr()
  765. {
  766. $trade =isset($this->post['data']) &&!empty($this->post['data'])? $this->post['data']:[];
  767. if(!is_array($trade) || empty($trade) ) return error_show( 1003,"参数 data 不能为空");
  768. $list = [];
  769. foreach ( $trade as $key => $value) {
  770. if (!isset($value[0]) || $value[0] == '') {
  771. //return ['code' => 1003, "msg" => '交易时间不能为空'];
  772. return error_show( 1003, '第'.($key+1).'行收款方公司编号不能为空!');
  773. }
  774. if (!isset($value[4]) || $value[4] == '') {
  775. // return ['code' => 1003, "msg" => '交易行名不能为空'];
  776. return error_show( 1003, '第'.($key+1).'行收入金额不能为空!');
  777. }
  778. if (!isset($value[8]) || $value[8] == '') {
  779. // return ['code' => 1003, "msg" => '对方账户不能为空'];
  780. return error_show( 1003, '第'.($key+1).'行对方账户不能为空!');
  781. }
  782. if (!isset($value[9]) || $value[9] == '') {
  783. // return ['code' => 1003, "msg" => '对方户名不能为空'];
  784. return error_show( 1003, '第'.($key+1).'行对方户名不能为空!');
  785. }
  786. // if ($key == 0) {
  787. // if ($value[0] != '收款方公司编号') {
  788. // // return ['code' => 1003, "msg" => '模板第一列为必须为交易时间!'];
  789. // return error_show( 1003, '模板第一列为必须为收款方公司编号!');
  790. // }
  791. // if ($value[4] != '收入金额') {
  792. // // return ['code' => 1003, "msg" => '模板第二列为必须为收入金额!'];
  793. // return error_show( 1003, '模板第五列为必须为收入金额!');
  794. // }
  795. //
  796. // if ($value[6] != '交易行名') {
  797. // //return ['code' => 1003, "msg" => '模板第五列为必须为交易行名!'];
  798. // return error_show( 1003, '模板第七列为必须为交易行名!');
  799. // }
  800. // if ($value[8] != '对方账号') {
  801. // // return ['code' => 1003, "msg" => '模板第七列为必须为对方账号!'];
  802. // return error_show( 1003, '模板第九列为必须为对方账号!');
  803. // }
  804. // if ($value[9] != '对方户名') {
  805. // // return ['code' => 1003, "msg" => '模板第八列为必须为对方户名!'];
  806. // return error_show( 1003, '模板第十列为必须为对方户名!');
  807. // }
  808. // continue;
  809. // }
  810. $company =Db::name("company_info")->where(["companyNo"=>$value[0]])->findOrEmpty();
  811. if(empty($company)){
  812. return error_show( 1003, "业务公司编号不存在{$value[0]}");
  813. }
  814. $total_fee = $value[4];
  815. $type = 0;
  816. $time = explode(" ", $value[3]);
  817. $list[$key]["tradeTime"] = date("Y-m-d", strtotime($time[0])) . " " . (isset($time[1]) ? $time[1] : "00:00:00");
  818. $list[$key]["trade_fee"] = str_replace(",","",$total_fee);
  819. $list[$key]["trade_bank"] = $value[6];
  820. $list[$key]["trade_account"] = $value[8];
  821. $list[$key]["trade_out"] = $value[9];
  822. $list[$key]["trade_in"] = $value[1]??$company['company_name'];
  823. $list[$key]["trade_type"] = $type;
  824. $list[$key]["trade_used"] = isset($value[10]) ? $value[10] : "";
  825. $list[$key]["trade_remark"] = '';
  826. $list[$key]["companyNo"] = $value[0];
  827. $list[$key]["trade_in_account"] = $value[2]??"";
  828. }
  829. if(empty($list)){
  830. return error_show( 1003, '导入数据不能为空!');
  831. }
  832. Db::startTrans();
  833. try{
  834. $tra=[];
  835. foreach ($list as $value) {
  836. $temp = [];
  837. $temp["tradNo"] = makeStr('S');
  838. $temp['trade_time'] = $value['tradeTime'];
  839. $temp['total_fee'] = $value['trade_fee'];
  840. $temp['trade_bank'] = $value['trade_bank'];
  841. $temp['trade_account'] = $value['trade_account'];
  842. $temp['trade_type'] =$value['trade_type'];
  843. $temp['trade_out'] = $value['trade_out'];
  844. $temp['trade_in'] = $value['trade_in'];
  845. $temp['trade_in_account'] = $value['trade_in_account'];
  846. $temp['companyNo'] = $value['companyNo'];
  847. $temp['trade_used'] = $value['trade_used'];
  848. $temp['trade_remark'] = $value['trade_remark'];
  849. $temp['balance'] =$value['trade_fee'];
  850. $temp['addtime'] = date("Y-m-d H:i:s");
  851. $temp['updatetime'] = date("Y-m-d H:i:s");
  852. $tra[]=$temp;
  853. }
  854. $list = Db::name('trade')->insertAll($tra);
  855. if($list==count($tra)){
  856. Db::commit();
  857. return app_show(0, "资金导入成功");
  858. }else{
  859. Db::rollback();
  860. return app_show(1004, "资金导入失败");
  861. }
  862. }catch (\Exception $e){
  863. Db::rollback();
  864. return app_show(1004, $e->getMessage());
  865. }
  866. }
  867. }