OrderPay.php 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913
  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. "companyNo"=>$trade['companyNo'],
  99. "apply_id"=>$this->uid,
  100. "apply_name"=>$this->uname,
  101. "trade_time"=>$trade['trade_time'],
  102. "total_fee"=>$total_fee,
  103. "status"=>1,
  104. "addtime"=>date("Y-m-d H:i:s"),
  105. "updatetime"=>date("Y-m-d H:i:s")
  106. ];
  107. $tradchild =Db::name("trade_pool")->insert($create);
  108. if($tradchild){
  109. Db::commit();
  110. return app_show(0,"资金认领成功",["logNo"=>$logNo]);
  111. }
  112. }
  113. Db::rollback();
  114. return error_show(1004,"资金认领失败");
  115. }catch (\Exception $e){
  116. Db::rollback();
  117. return error_show(1004,$e->getMessage());
  118. }
  119. }
  120. // 1待审批2审批通过3审批驳回4退款5解除认领
  121. public function status(){
  122. $logNo = isset($this->post['logNo'])&&$this->post['logNo']!=""?trim($this->post['logNo']):"";
  123. if($logNo==""){
  124. return error_show(1004,"参数logNo不能为空");
  125. }
  126. $status=isset($this->post['status'])&&$this->post['status']!=""?intval($this->post['status']):"";
  127. if($status==""){
  128. return error_show(1004,"参数 status 不能为空");
  129. }
  130. $remark=isset($this->post['remark'])&&$this->post['remark']!=""?trim($this->post['remark']):"";
  131. if($remark==""){
  132. return error_show(1004,"参数 remark 不能为空");
  133. }
  134. Db::startTrans();
  135. try {
  136. $logingo= Db::name("trade_pool")->where(["logNo"=>$logNo,"is_del"=>0])->find();
  137. if($logingo==false){
  138. Db::rollback();
  139. return error_show(1004,"资金认领信息未找到");
  140. }
  141. $up=["status"=>$status,"updatetime"=>date("Y-m-d H:i:s"),"remark"=>$remark];
  142. $logup = Db::name("trade_pool")->where($logingo)->update($up);
  143. if($logup){
  144. if($status==2){
  145. $qrdArr=Db::name("assoc")->where(["viceCode"=>$logNo,"status"=>1,"is_del"=>0])->select()->toArray();
  146. if(!empty($qrdArr)){
  147. foreach ($qrdArr as $value){
  148. $qrd =Db::name("qrd_info")->where(["sequenceNo"=>$value['orderCode']])->field("id,status,pay_fee,apay_fee,wpay_fee,pay_status,totalPrice")->findOrEmpty();
  149. if(empty($qrd)){
  150. Db::rollback();
  151. return error_show(1005,"未找到销售单数据");
  152. }
  153. if($qrd['pay_fee']<$value['cancel_fee']){
  154. Db::rollback();
  155. return error_show(1005,"销售单待付金额不足");
  156. }
  157. $update =[
  158. "apay_fee"=>$qrd['apay_fee']+$value['cancel_fee'],
  159. "pay_fee"=>$qrd['pay_fee']-$value['cancel_fee'],
  160. "paytime"=>date("Y-m-d H:i:s"),
  161. "pay_status"=>$qrd['wpay_fee']==0 && ($qrd['pay_fee']-$value['cancel_fee'])==0 ? 3:2,
  162. "updatetime"=>date("Y-m-d H:i:s")
  163. ];
  164. $qrdup =Db::name("qrd_info")->where($qrd)->update($update);
  165. if($qrdup==false){
  166. Db::rollback();
  167. return error_show(1005,"销售单更新失败");
  168. }
  169. $asscup =[
  170. "status"=>2,
  171. "assoc_time"=>date("Y-m-d H:i:s"),
  172. "updatetime"=>date("Y-m-d H:i:s")
  173. ];
  174. $assc=Db::name("assoc")->where($value)->update($asscup);
  175. if($assc==false){
  176. Db::rollback();
  177. return error_show(1005,"销售单更新失败");
  178. }
  179. }
  180. }
  181. }
  182. if($status==3){
  183. $qrdArr=Db::name("assoc")->where(["viceCode"=>$logNo,"status"=>1,"is_del"=>0])->select()->toArray();
  184. if(!empty($qrdArr)){
  185. foreach ($qrdArr as $value){
  186. $qrd =Db::name("qrd_info")->where(["sequenceNo"=>$value['orderCode']])->field("id,status,pay_fee,apay_fee,wpay_fee,pay_status,totalPrice")->findOrEmpty();
  187. if(empty($qrd)){
  188. Db::rollback();
  189. return error_show(1005,"未找到销售单数据");
  190. }
  191. if($qrd['pay_fee']<$value['cancel_fee']){
  192. Db::rollback();
  193. return error_show(1005,"销售单待付金额不足");
  194. }
  195. $update =[
  196. "wpay_fee"=>$qrd['wpay_fee']+$value['cancel_fee'],
  197. "pay_fee"=>$qrd['pay_fee']-$value['cancel_fee'],
  198. "pay_status"=>$qrd['apay_fee']==0 && ($qrd['pay_fee']-$value['cancel_fee'])==0 ? 1:2,
  199. "status"=>$qrd['apay_fee']==0 && ($qrd['pay_fee']-$value['cancel_fee'])==0 ?0:1,
  200. "updatetime"=>date("Y-m-d H:i:s")
  201. ];
  202. $qrdup =Db::name("qrd_info")->where($qrd)->update($update);
  203. if($qrdup==false){
  204. Db::rollback();
  205. return error_show(1005,"销售单更新失败");
  206. }
  207. $asscup =[
  208. "status"=>3,
  209. "updatetime"=>date("Y-m-d H:i:s")
  210. ];
  211. $assc=Db::name("assoc")->where($value)->update($asscup);
  212. if($assc==false){
  213. Db::rollback();
  214. return error_show(1005,"销售单更新失败");
  215. }
  216. $report=ReportCode::where(["qrdNo"=>$value['orderCode']])->find();
  217. if($report)$report->rmField("logNo",$logNo);
  218. }
  219. }
  220. $trade =Db::name("trade")->where(["tradNo"=>$logingo['tradNo'],"is_del"=>0])->findOrEmpty();
  221. if($trade==false){
  222. Db::rollback();
  223. return error_show(1005,"资金信息未找到");
  224. }
  225. if($trade['used_fee']<$logingo['total_fee']){
  226. Db::rollback();
  227. return error_show(1005,"资金信息已认领金额不足");
  228. }
  229. $tradup =[
  230. "used_fee"=>$trade['used_fee']-$logingo['total_fee'],
  231. "balance"=>$trade['balance']+$logingo['total_fee'],
  232. "status"=>($trade['used_fee']-$logingo['total_fee'])==0 ? 0 :1,
  233. "updatetime"=>date("Y-m-d H:i:s")
  234. ];
  235. $updaT=Db::name("trade")->where($trade)->update($tradup);
  236. if($updaT==false){
  237. Db::rollback();
  238. return error_show(1005,"资金信息更新失败");
  239. }
  240. }
  241. Db::commit();
  242. return app_show(0,"审核成功");
  243. }
  244. Db::rollback();
  245. return error_show(1004,"审核失败");
  246. }catch (\Exception $e){
  247. Db::rollback();
  248. return error_show(1004,$e->getMessage());
  249. }
  250. }
  251. //资金信息列表
  252. public function list(){
  253. $condition=[];
  254. $page=isset($this->post['page'])&&$this->post['page']!="" ? intval($this->post['page']) :1;
  255. $size=isset($this->post['size'])&&$this->post['size']!="" ? intval($this->post['size']) :15;
  256. $name =isset($this->post['name'])&&$this->post['name']!=""? trim($this->post['name']):"";
  257. $bank =isset($this->post['bank'])&&$this->post['bank']!=""? trim($this->post['bank']):"";
  258. $start =isset($this->post['start'])&&$this->post['start']!=""? trim($this->post['start']):"";
  259. $end =isset($this->post['end'])&&$this->post['end']!=""? trim($this->post['end']):"";
  260. $status =isset($this->post['status'])&&$this->post['status']!==""? intval($this->post['status']):"";
  261. $tradNo =isset($this->post['tradNo'])&&$this->post['tradNo']!=""? trim($this->post['tradNo']):"";
  262. $companyNo =isset($this->post['companyNo'])&&$this->post['companyNo']!=""? trim($this->post['companyNo']):"";
  263. if($companyNo!==""){
  264. $condition[]=["companyNo","=",$companyNo];
  265. }
  266. $relaComNo= isset($post['relaComNo'])&&$post['relaComNo']!="" ? trim($post['relaComNo']) :"";
  267. if($relaComNo!=""){
  268. $condition[]=["companyNo","=",$relaComNo];
  269. }
  270. if($tradNo!==""){
  271. $condition[]=["tradNo","like","%$tradNo%"];
  272. }
  273. if($bank!=""){
  274. $condition[]=["trade_bank","like","%$bank%"];
  275. }
  276. if($name!=""){
  277. $condition[]=["trade_out","like","%$name%"];
  278. }
  279. if($start!=""){
  280. $condition[]=["trade_time",">=",$start." 00:00:00"];
  281. }
  282. if($end!=""){
  283. $condition[]=["trade_time","<=",$end." 23:59:59"];
  284. }
  285. if($status!==""){
  286. $condition[]=["status","=",$status];
  287. }
  288. $count=Db::name("trade")->where($condition)->count();
  289. $total=ceil($count/$size);
  290. $page=$page>$total? intval($total):$page;
  291. $list =Db::name("trade")->where($condition)->page($page,$size)->order("addtime desc")->select()->toArray();
  292. foreach ($list as &$value){
  293. $data = Db::name("trade_pool")->alias("a")
  294. ->leftJoin("assoc b","a.logNo=b.viceCode")
  295. ->leftJoin("qrd_info c","b.orderCode=c.sequenceNo")
  296. ->where(["a.tradNo"=>$value['tradNo'],"a.is_del"=>0])
  297. ->order("a.addtime desc")
  298. ->field("a.*,b.orderCode,c.qrdSource,goodNo,goodName,qrdType,c.ownerName,c.ownerid,c.department,c.poCode")
  299. ->select()->toArray();
  300. $value['child']=$data;
  301. }
  302. return app_show(0,"获取成功",["list"=>$list,"count"=>$count]);
  303. }
  304. // 认领资金列表明细
  305. public function tradeList(){
  306. $page=isset($this->post['page'])&&$this->post['page']!="" ? intval($this->post['page']) :1;
  307. $size=isset($this->post['size'])&&$this->post['size']!="" ? intval($this->post['size']) :15;
  308. $condition =[["a.is_del","=",0],["b.is_del","=",0]];
  309. $name =isset($this->post['name'])&&$this->post['name']!=""? trim($this->post['name']):"";
  310. $bank =isset($this->post['bank'])&&$this->post['bank']!=""? trim($this->post['bank']):"";
  311. $start =isset($this->post['start'])&&$this->post['start']!=""? trim($this->post['start']):"";
  312. $end =isset($this->post['end'])&&$this->post['end']!=""? trim($this->post['end']):"";
  313. $status =isset($this->post['status'])&&$this->post['status']!==""? intval($this->post['status']):"";
  314. $apply_id =isset($this->post['apply_id'])&&$this->post['apply_id']!=""? intval($this->post['apply_id']):"";
  315. $apply_name =isset($this->post['apply_name'])&&$this->post['apply_name']!=""? trim($this->post['apply_name']):"";
  316. $tradNo =isset($this->post['tradNo'])&&$this->post['tradNo']!=""? trim($this->post['tradNo']):"";
  317. $orderCode =isset($this->post['orderCode'])&&$this->post['orderCode']!=''?trim($this->post['orderCode']):"";
  318. $companyNo =isset($this->post['companyNo'])&&$this->post['companyNo']!=""? trim($this->post['companyNo']):"";
  319. if($companyNo!==""){
  320. $condition[]=["a.companyNo","=",$companyNo];
  321. }
  322. $relaComNo= isset($post['relaComNo'])&&$post['relaComNo']!="" ? trim($post['relaComNo']) :"";
  323. if($relaComNo!=""){
  324. $condition[]=["companyNo","=",$relaComNo];
  325. }
  326. if($tradNo!==""){
  327. $condition[]=["a.tradNo","like","%$tradNo%"];
  328. }
  329. if($orderCode!==""){
  330. $condition[]=["d.orderCode","like","%$orderCode%"];
  331. }
  332. $logNo =isset($this->post['logNo'])&&$this->post['logNo']!=""? trim($this->post['logNo']):"";
  333. if($logNo!==""){
  334. $condition[]=["a.logNo","like","%$logNo%"];
  335. }
  336. if($bank!=""){
  337. $condition[]=["b.trade_bank","like","%$bank%"];
  338. }
  339. if($name!=""){
  340. $condition[]=["b.trade_out","like","%$name%"];
  341. }
  342. if($start!=""){
  343. $condition[]=["a.add_time",">=",$start." 00:00:00"];
  344. }
  345. if($end!=""){
  346. $condition[]=["a.add_time","<=",$end." 23:59:59"];
  347. }
  348. if($status!==""){
  349. $condition[]=["a.status","=",$status];
  350. }
  351. if($apply_id!==""){
  352. $condition[]=["a.apply_id","=",$apply_id];
  353. }
  354. if($apply_name!==""){
  355. $condition[]=["a.apply_name","like","%$apply_name%"];
  356. }
  357. $count=Db::name("trade_pool")->alias("a")
  358. ->leftJoin("trade b","a.tradNo=b.tradNo")
  359. ->leftJoin("assoc d","a.logNo=d.viceCode")
  360. ->leftJoin("qrd_info c","d.orderCode=c.sequenceNo")
  361. ->where($condition)->count();
  362. $total=ceil($count/$size);
  363. $page=$page>$total? intval($total):$page;
  364. $list =Db::name("trade_pool")->alias("a")
  365. ->leftJoin("trade b","a.tradNo=b.tradNo")
  366. ->leftJoin("assoc d","a.logNo=d.viceCode")
  367. ->leftJoin("qrd_info c","d.orderCode=c.sequenceNo")
  368. ->where($condition)
  369. ->page($page,$size)
  370. ->field("a.*,b.trade_bank,b.trade_account,b.trade_out,b.total_fee as btotal_fee,b.used_fee,b.balance,
  371. d.orderCode,c.qrdSource,c.goodNo,c.goodName,c.qrdType,c.ownerName,c.ownerid,c.department,c.poCode,c.platName")
  372. ->order("a.addtime desc")->select();
  373. return app_show(0,"获取成功",["list"=>$list,"count"=>$count]);
  374. }
  375. // 获取资金下面的认领信息
  376. public function tradeQuery(){
  377. $tradNo =isset($this->post['tradNo'])&&$this->post['tradNo']!=""? trim($this->post['tradNo']):"";
  378. $status =isset($this->post['status'])&&$this->post['status']!=""? intval($this->post['status']):"";
  379. $apply_id =isset($this->post['apply_id'])&&$this->post['apply_id']!=""? intval($this->post['apply_id']):"";
  380. $apply_name =isset($this->post['apply_name'])&&$this->post['apply_name']!=""? trim($this->post['apply_name']):"";
  381. $companyNo =isset($this->post['companyNo'])&&$this->post['companyNo']!=""? trim($this->post['companyNo']):"";
  382. if($companyNo!==""){
  383. $condition[]=["companyNo","=",$companyNo];
  384. }
  385. $relaComNo= isset($post['relaComNo'])&&$post['relaComNo']!="" ? trim($post['relaComNo']) :"";
  386. if($relaComNo!=""){
  387. $condition[]=["companyNo","=",$relaComNo];
  388. }
  389. $condition=[["is_del","=",0]];
  390. if($apply_id!==""){
  391. $condition[]=["apply_id","=",$apply_id];
  392. }
  393. if($apply_name!==""){
  394. $condition[]=["apply_name","like","%$apply_name%"];
  395. }
  396. if($status!==""){
  397. $condition[]=["status","=",$status];
  398. }
  399. if($tradNo!==""){
  400. $condition[]=["tradNo","like","%$tradNo%"];
  401. }
  402. $list =Db::name("trade_pool")->where($condition)->order("addtime desc")->select();
  403. return app_show(0,"获取成功",$list);
  404. }
  405. //资金详情
  406. public function tradeInfo(){
  407. $tradNo =isset($this->post['tradNo'])&&$this->post['tradNo']!=""? trim($this->post['tradNo']):"";
  408. if($tradNo==""){
  409. return error_show(1004,"参数 tradNo 不能为空");
  410. }
  411. $tradinfo = Db::name("trade")->where(["tradNo"=>$tradNo,"is_del"=>0])->find();
  412. if($tradinfo==false){
  413. return error_show(1004,"资金信息未找到");
  414. }
  415. return app_show(0,"获取成功",$tradinfo);
  416. }
  417. //资金认领详情
  418. public function logInfo(){
  419. $logNo =isset($this->post['logNo'])&&$this->post['logNo']!=""? trim($this->post['logNo']):"";
  420. if($logNo==""){
  421. return error_show(1004,"参数 logNo 不能为空");
  422. }
  423. $tradinfo = Db::name("trade_pool")->where(["logNo"=>$logNo,"is_del"=>0])->find();
  424. if($tradinfo==false){
  425. return error_show(1004,"资金信息未找到");
  426. }
  427. $trade = Db::name("trade")->where(["tradNo"=>$tradinfo['tradNo'],"is_del"=>0])->find();
  428. $tradinfo['trade_out']= $trade['trade_out']??"";
  429. $tradinfo['trade_in']= $trade['trade_in']??"";
  430. $tradinfo['trade_bank']= $trade['trade_bank']??"";
  431. $tradinfo['customerNo']= $trade['customerNo']??"";
  432. $tradinfo['companyNo']= $trade['companyNo']??"";
  433. $tradinfo['total_fee']= $trade['total_fee']??"";
  434. $tradinfo['balance']= $trade['balance']??"";
  435. $tradinfo['used_fee']= $trade['used_fee']??"";
  436. $orderinfo = Db::name("assoc")->alias("a")->leftJoin("qrd_info c","a.orderCode=c.sequenceNo")
  437. ->where(["a.viceCode"=>$logNo,"a.is_del"=>0,"a.status"=>[1,2]])
  438. ->order("a.addtime desc")
  439. ->field("c.*")
  440. ->findOrEmpty();
  441. $tradinfo['orderinfo']=$orderinfo;
  442. return app_show(0,"获取成功",$tradinfo);
  443. }
  444. //认领资金退回或退款 type 1 退款 2 解除资金认领
  445. public function ReturnPay(){
  446. $logNo = isset($this->post['logNo'])&&$this->post['logNo']!="" ? trim($this->post['logNo']):"";
  447. if($logNo==''){
  448. return error_show(1004,"参数 logNo 不能为空");
  449. }
  450. $type = isset($this->post['type'])&&$this->post['type']!="" ? intval($this->post['type']):"";
  451. if($type==""){
  452. return error_show(1004,"参数 type 不能为空");
  453. }
  454. $loginfo = Db::name("trade_pool")->where(["logNo"=>$logNo,"is_del"=>0])->find();
  455. if($loginfo==false){
  456. return error_show(1004,"认领资金信息未找到");
  457. }
  458. if($loginfo['status']!=2)return error_show(1004,"认领资金信息审核未通过");
  459. $tradinfo = Db::name("trade")->where(["tradNo"=>$loginfo['tradNo'],"is_del"=>0])->find();
  460. if($tradinfo==false){
  461. return error_show(1004,"资金信息未找到");
  462. }
  463. $reason = isset($this->post['return_reason'])&&$this->post['return_reason']!="" ? trim($this->post['return_reason']):"";
  464. $remark = isset($this->post['remark'])&&$this->post['remark']!="" ? trim($this->post['remark']):"";
  465. $returnCode =makeNo("RTA");
  466. $data=[
  467. "returnCode"=>$returnCode,
  468. "logNo"=>$logNo,
  469. "companyNo"=>$tradinfo['companyNo'],
  470. "tradNo"=>$loginfo['tradNo'],
  471. "type"=>$type,
  472. "apply_id"=>$this->uid,
  473. "apply_name"=>$this->uname,
  474. "return_reason"=>$reason,
  475. "remark"=>$remark,
  476. "addtime"=>date("Y-m-d H:i:s"),
  477. "updatetime"=>date("Y-m-d H:i:s")
  478. ];
  479. $in = Db::name("trade_return")->insert($data);
  480. if($in){
  481. return app_show(0,"申请新建成功");
  482. }else{
  483. return error_show(1004,"申请新建失败");
  484. }
  485. }
  486. /**退款列表
  487. * //page size tradNo logNo apply_id apply_name type status returnCode
  488. * @return \think\response\Json|void
  489. * @throws \think\db\exception\DataNotFoundException
  490. * @throws \think\db\exception\DbException
  491. * @throws \think\db\exception\ModelNotFoundException
  492. */
  493. public function returnList(){
  494. $page=isset($this->post['page'])&&$this->post['page']!='' ? intval($this->post['page']):1;
  495. $size=isset($this->post['size'])&&$this->post['size']!='' ? intval($this->post['size']):15;
  496. $tradNo =isset($this->post['tradNo'])&&$this->post['tradNo']!=''?trim($this->post['tradNo']):"";
  497. $logNo =isset($this->post['logNo'])&&$this->post['logNo']!=''?trim($this->post['logNo']):"";
  498. $apply_id =isset($this->post['apply_id'])&&$this->post['apply_id']!=''?intval($this->post['apply_id']):"";
  499. $apply_name =isset($this->post['apply_name'])&&$this->post['apply_name']!=''?trim($this->post['apply_name']):"";
  500. $type =isset($this->post['type'])&&$this->post['type']!=''?intval($this->post['type']):"";
  501. $status =isset($this->post['status'])&&$this->post['status']!==''?intval($this->post['status']):"";
  502. $returnCode =isset($this->post['returnCode'])&&$this->post['returnCode']!=''?trim($this->post['returnCode']):"";
  503. $orderCode =isset($this->post['orderCode'])&&$this->post['orderCode']!=''?trim($this->post['orderCode']):"";
  504. $companyNo =isset($this->post['companyNo'])&&$this->post['companyNo']!=""? trim($this->post['companyNo']):"";
  505. if($companyNo!==""){
  506. $condition[]=["a.companyNo","=",$companyNo];
  507. }
  508. $relaComNo= isset($post['relaComNo'])&&$post['relaComNo']!="" ? trim($post['relaComNo']) :"";
  509. if($relaComNo!=""){
  510. $condition[]=["a.companyNo","=",$relaComNo];
  511. }
  512. $condition=[["a.is_del","=",0]];
  513. if($tradNo!=''){
  514. $condition[]=["a.tradNo","like","%$tradNo%"];
  515. }
  516. if($orderCode!=''){
  517. $condition[]=["b.orderCode","like","%$orderCode%"];
  518. }
  519. if($logNo!=''){
  520. $condition[]=["a.logNo","like","%$logNo%"];
  521. }
  522. if($apply_id!=''){
  523. $condition[]=["a.apply_id","=",$apply_id];
  524. }
  525. if($apply_name!=''){
  526. $condition[]=["a.apply_name","like","%$apply_name%"];
  527. }
  528. if($returnCode!=''){
  529. $condition[]=["a.returnCode","like","%$returnCode%"];
  530. }
  531. if($type!=''){
  532. $condition[]=["a.type","=",$type];
  533. }
  534. if($status!=''){
  535. $condition[]=["a.status","=",$status];
  536. }
  537. $count =Db::name("trade_return")->alias("a")
  538. ->leftJoin("assoc b","a.logNo=b.viceCode")
  539. ->leftJoin("qrd_info c","c.sequenceNo=b.orderCode")
  540. ->where($condition)->count();
  541. $total=ceil($count/$size);
  542. $page = $page>$total ? intval($total):$page;
  543. $list=Db::name("trade_return")->alias("a")->leftJoin("assoc b","a.logNo=b.viceCode")
  544. ->leftJoin("qrd_info c","c.sequenceNo=b.orderCode")
  545. ->field("a.*,b.orderCode,c.qrdSource,goodNo,goodName,qrdType,c.ownerName,c.ownerid,c.department,c.poCode,c.platName")
  546. ->where($condition)->page($page,$size)->order("a.addtime desc")->select();
  547. return app_show(0,"获取成功",["list"=>$list,"count"=>$count]);
  548. }
  549. //退款状态审核 0 待审核 1 财务审核 2 财务驳回
  550. public function returnStatus(){
  551. $returnCode=isset($this->post['returnCode'])&&$this->post['returnCode']!=''?trim($this->post['returnCode']):"";
  552. if($returnCode==''){
  553. return error_show(1004,"参数 returnCode 不能为空");
  554. }
  555. $returninfo =Db::name("trade_return")->where(["returnCode"=>$returnCode,"is_del"=>0])->find();
  556. if($returninfo==false){
  557. return error_show(1004,"退款申请数据未找到");
  558. }
  559. $status=isset($this->post['status']) &&$this->post['status']!=''?intval($this->post['status']) : '';
  560. if($status==''){
  561. return error_show(1004,"参数 status 不能为空");
  562. }
  563. $return_img = isset($this->post['return_img'])&&$this->post['return_img']!=''?trim($this->post['return_img']):"";
  564. $remark = isset($this->post['remark'])&&$this->post['remark']!=''?trim($this->post['remark']):"";
  565. $loginfo = Db::name("trade_pool")->where(["logNo"=>$returninfo['logNo'],"is_del"=>0])->find();
  566. if($loginfo==false){
  567. return error_show(1004,"认领资金信息未找到");
  568. }
  569. if($loginfo['status']!=2)return error_show(1004,"认领资金信息审核未通过");
  570. Db::startTrans();
  571. try{
  572. $update=["status"=>$status,"return_img"=>$return_img,"remark"=>$remark,"updatetime"=>date("Y-m-d H:i:s")];
  573. $up =Db::name("trade_return")->where($returninfo)->update($update);
  574. if($up){
  575. if($status==1){
  576. $qrdArr=Db::name("assoc")->where(["viceCode"=>$returninfo['logNo'],"status"=>1,"is_del"=>0])->select()
  577. ->toArray();
  578. if(!empty($qrdArr)){
  579. foreach ($qrdArr as $value){
  580. $qrd =Db::name("qrd_info")->where(["sequenceNo"=>$value['orderCode']])->field("id,status,pay_fee,apay_fee,wpay_fee,pay_status,totalPrice")->findOrEmpty();
  581. if(empty($qrd)){
  582. Db::rollback();
  583. return error_show(1005,"未找到销售单数据");
  584. }
  585. if($qrd['pay_fee']<$value['cancel_fee']){
  586. Db::rollback();
  587. return error_show(1005,"销售单待付金额不足");
  588. }
  589. $update =[
  590. "wpay_fee"=>$qrd['wpay_fee']+$value['cancel_fee'],
  591. "apay_fee"=>$qrd['apay_fee']-$value['cancel_fee'],
  592. "pay_status"=>($qrd['apay_fee']-$value['cancel_fee'])==0 && $qrd['pay_fee']==0 ? 1:2,
  593. "status"=>$qrd['pay_fee']==0 && ($qrd['apay_fee']-$value['cancel_fee'])==0 ?0:1,
  594. "updatetime"=>date("Y-m-d H:i:s")
  595. ];
  596. $qrdup =Db::name("qrd_info")->where($qrd)->update($update);
  597. if($qrdup==false){
  598. Db::rollback();
  599. return error_show(1005,"销售单更新失败");
  600. }
  601. $asscup =[
  602. "status"=>3,
  603. "updatetime"=>date("Y-m-d H:i:s")
  604. ];
  605. $assc=Db::name("assoc")->where($value)->update($asscup);
  606. if($assc==false){
  607. Db::rollback();
  608. return error_show(1005,"销售单更新失败");
  609. }
  610. $report=ReportCode::where(["qrdNo"=>$value['orderCode']])->find();
  611. if($report)$report->setField("returnTrad",$returnCode);
  612. }
  613. }
  614. $logup =["status"=>$returninfo['type']==1?5:4,"updatetime"=>date("Y-m-d H:i:s")];
  615. $upde =Db::name("trade_pool")->where($loginfo)->update($logup);
  616. if($upde==false){
  617. Db::rollback();
  618. return error_show(1005,"资金认领信息更新失败");
  619. }
  620. if($returninfo['type']==1){
  621. $trade =Db::name("trade")->where(["tradNo"=>$returninfo['tradNo'],"is_del"=>0])->find();
  622. if($trade==false){
  623. Db::rollback();
  624. return error_show(1005,"资金信息未找到");
  625. }
  626. if($trade['used_fee']<$loginfo['total_fee']){
  627. Db::rollback();
  628. return error_show(1005,"资金信息已认领金额不足");
  629. }
  630. $tradup =[
  631. "used_fee"=>$trade['used_fee']-$loginfo['total_fee'],
  632. "balance"=>$trade['balance']+$loginfo['total_fee'],
  633. "status"=>($trade['used_fee']-$loginfo['total_fee'])==0 ? 0 :1,
  634. "updatetime"=>date("Y-m-d H:i:s")
  635. ];
  636. $updaT=Db::name("trade")->where($trade)->update($tradup);
  637. if($updaT==false){
  638. Db::rollback();
  639. return error_show(1005,"资金信息更新失败");
  640. }
  641. }
  642. }
  643. Db::commit();
  644. return app_show(0,"退款申请审核成功");
  645. }
  646. Db::rollback();
  647. return error_show(1004,'审核状态失败');
  648. }catch (\Exception $e){
  649. Db::rollback();
  650. return error_show(1004,$e->getMessage());
  651. }
  652. }
  653. //退款申请详情
  654. public function returnInfo(){
  655. $returnCode=isset($this->post['returnCode'])&&$this->post['returnCode']!=''?trim($this->post['returnCode']):"";
  656. if($returnCode==''){
  657. return error_show(1004,"参数 returnCode 不能为空");
  658. }
  659. $returninfo =Db::name("trade_return")->where(["returnCode"=>$returnCode,"is_del"=>0])->find();
  660. if($returninfo==false){
  661. return error_show(1004,"退款申请数据未找到");
  662. }
  663. $trade = Db::name("trade")->where(["tradNo"=>$returninfo['tradNo'],"is_del"=>0])->find();
  664. $returninfo['trade_out']= $trade['trade_out']??"";
  665. $returninfo['trade_in']= $trade['trade_in']??"";
  666. $returninfo['trade_bank']= $trade['trade_bank']??"";
  667. $returninfo['trade_time']= $trade['trade_time']??"";
  668. $returninfo['customerNo']= $trade['customerNo']??"";
  669. $returninfo['companyNo']= $trade['companyNo']??"";
  670. $returninfo['total_fee']= $trade['total_fee']??"";
  671. $returninfo['balance']= $trade['balance']??"";
  672. $returninfo['used_fee']= $trade['used_fee']??"";
  673. $pool =Db::name("trade_pool")->where(["logNo"=>$returninfo['logNo'],"is_del"=>0])->find();
  674. $returninfo['log_total_fee']= $pool['total_fee']??"";
  675. $returninfo['log_apply_id']= $pool['apply_id']??"";
  676. $returninfo['log_apply_name']= $pool['apply_name']??"";
  677. $orderinfo = Db::name("assoc")->alias("a")->leftJoin("qrd_info c","a.orderCode=c.sequenceNo")
  678. ->where(["a.viceCode"=>$returninfo['logNo'],"a.is_del"=>0,"a.status"=>[1,2]])
  679. ->order("a.addtime desc")
  680. ->field("c.*")
  681. ->findOrEmpty();
  682. $returninfo['orderinfo'] = $orderinfo;
  683. return app_show(0,"获取成功",$returninfo);
  684. }
  685. /**
  686. * 显示创建资源表单页.
  687. *
  688. * @return \think\Response
  689. */
  690. public function importTrade()
  691. {
  692. $files = $this->request->file("excel");
  693. $data = upload($files,$files->getOriginalExtension());
  694. if($data['code']!=0){
  695. return error_show(1004,$data['msg']);
  696. }
  697. $trade = $data['data'];
  698. $list = [];
  699. foreach ( $trade as $key => $value) {
  700. if (!isset($value[0]) || $value[0] == '') {
  701. //return ['code' => 1003, "msg" => '交易时间不能为空'];
  702. return error_show( 1003, '第'.($key+1).'行收款方公司编号不能为空!');
  703. }
  704. if (!isset($value[4]) || $value[4] == '') {
  705. // return ['code' => 1003, "msg" => '交易行名不能为空'];
  706. return error_show( 1003, '第'.($key+1).'行收入金额不能为空!');
  707. }
  708. if (!isset($value[8]) || $value[8] == '') {
  709. // return ['code' => 1003, "msg" => '对方账户不能为空'];
  710. return error_show( 1003, '第'.($key+1).'行对方账户不能为空!');
  711. }
  712. if (!isset($value[9]) || $value[9] == '') {
  713. // return ['code' => 1003, "msg" => '对方户名不能为空'];
  714. return error_show( 1003, '第'.($key+1).'行对方户名不能为空!');
  715. }
  716. if ($key == 0) {
  717. if ($value[0] != '收款方公司编号') {
  718. // return ['code' => 1003, "msg" => '模板第一列为必须为交易时间!'];
  719. return error_show( 1003, '模板第一列为必须为收款方公司编号!');
  720. }
  721. if ($value[4] != '收入金额') {
  722. // return ['code' => 1003, "msg" => '模板第二列为必须为收入金额!'];
  723. return error_show( 1003, '模板第五列为必须为收入金额!');
  724. }
  725. if ($value[6] != '交易行名') {
  726. //return ['code' => 1003, "msg" => '模板第五列为必须为交易行名!'];
  727. return error_show( 1003, '模板第七列为必须为交易行名!');
  728. }
  729. if ($value[8] != '对方账号') {
  730. // return ['code' => 1003, "msg" => '模板第七列为必须为对方账号!'];
  731. return error_show( 1003, '模板第九列为必须为对方账号!');
  732. }
  733. if ($value[9] != '对方户名') {
  734. // return ['code' => 1003, "msg" => '模板第八列为必须为对方户名!'];
  735. return error_show( 1003, '模板第十列为必须为对方户名!');
  736. }
  737. continue;
  738. }
  739. $company =Db::name("company_info")->where(["companyNo"=>$value[0]])->findOrEmpty();
  740. if(empty($company)){
  741. return error_show( 1003, "业务公司编号不存在{$value[0]}");
  742. }
  743. $total_fee = $value[4];
  744. $type = 0;
  745. $time = explode(" ", $value[3]);
  746. $list[$key]["tradeTime"] = date("Y-m-d", strtotime($time[0])) . " " . (isset($time[1]) ? $time[1] : "00:00:00");
  747. $list[$key]["trade_fee"] = str_replace(",","",$total_fee);
  748. $list[$key]["trade_bank"] = $value[6];
  749. $list[$key]["trade_account"] = $value[8];
  750. $list[$key]["trade_out"] = $value[9];
  751. $list[$key]["trade_in"] = $value[1]??$company['company_name'];
  752. $list[$key]["trade_type"] = $type;
  753. $list[$key]["trade_used"] = isset($value[10]) ? $value[10] : "";
  754. $list[$key]["trade_remark"] = '';
  755. $list[$key]["companyNo"] = $value[0];
  756. $list[$key]["trade_in_account"] = $value[2]??"";
  757. }
  758. if(empty($list)){
  759. return error_show( 1003, '导入数据不能为空!');
  760. }
  761. Db::startTrans();
  762. try{
  763. $tra=[];
  764. foreach ($list as $value) {
  765. $temp = [];
  766. $temp["tradNo"] = makeStr('S');
  767. $temp['trade_time'] = $value['tradeTime'];
  768. $temp['total_fee'] = $value['trade_fee'];
  769. $temp['trade_bank'] = $value['trade_bank'];
  770. $temp['trade_account'] = $value['trade_account'];
  771. $temp['trade_type'] =$value['trade_type'];
  772. $temp['trade_out'] = $value['trade_out'];
  773. $temp['trade_in'] = $value['trade_in'];
  774. $temp['trade_in_account'] = $value['trade_in_account'];
  775. $temp['companyNo'] = $value['companyNo'];
  776. $temp['trade_used'] = $value['trade_used'];
  777. $temp['trade_remark'] = $value['trade_remark'];
  778. $temp['balance'] =$value['trade_fee'];
  779. $temp['addtime'] = date("Y-m-d H:i:s");
  780. $temp['updatetime'] = date("Y-m-d H:i:s");
  781. $tra[]=$temp;
  782. }
  783. $list = Db::name('trade')->insertAll($tra);
  784. if($list==count($tra)){
  785. Db::commit();
  786. return app_show(0, "资金导入成功");
  787. }else{
  788. Db::rollback();
  789. return app_show(1004, "资金导入失败");
  790. }
  791. }catch (\Exception $e){
  792. Db::rollback();
  793. return app_show(1004, $e->getMessage());
  794. }
  795. }
  796. /**
  797. * @return \think\response\Json|void
  798. */
  799. public function importTradeByArr()
  800. {
  801. $trade =isset($this->post['data']) &&!empty($this->post['data'])? $this->post['data']:[];
  802. if(!is_array($trade) || empty($trade) ) return error_show( 1003,"参数 data 不能为空");
  803. $list = [];
  804. foreach ( $trade as $key => $value) {
  805. if (!isset($value[0]) || $value[0] == '') {
  806. //return ['code' => 1003, "msg" => '交易时间不能为空'];
  807. return error_show( 1003, '第'.($key+1).'行收款方公司编号不能为空!');
  808. }
  809. if (!isset($value[4]) || $value[4] == '') {
  810. // return ['code' => 1003, "msg" => '交易行名不能为空'];
  811. return error_show( 1003, '第'.($key+1).'行收入金额不能为空!');
  812. }
  813. if (!isset($value[8]) || $value[8] == '') {
  814. // return ['code' => 1003, "msg" => '对方账户不能为空'];
  815. return error_show( 1003, '第'.($key+1).'行对方账户不能为空!');
  816. }
  817. if (!isset($value[9]) || $value[9] == '') {
  818. // return ['code' => 1003, "msg" => '对方户名不能为空'];
  819. return error_show( 1003, '第'.($key+1).'行对方户名不能为空!');
  820. }
  821. // if ($key == 0) {
  822. // if ($value[0] != '收款方公司编号') {
  823. // // return ['code' => 1003, "msg" => '模板第一列为必须为交易时间!'];
  824. // return error_show( 1003, '模板第一列为必须为收款方公司编号!');
  825. // }
  826. // if ($value[4] != '收入金额') {
  827. // // return ['code' => 1003, "msg" => '模板第二列为必须为收入金额!'];
  828. // return error_show( 1003, '模板第五列为必须为收入金额!');
  829. // }
  830. //
  831. // if ($value[6] != '交易行名') {
  832. // //return ['code' => 1003, "msg" => '模板第五列为必须为交易行名!'];
  833. // return error_show( 1003, '模板第七列为必须为交易行名!');
  834. // }
  835. // if ($value[8] != '对方账号') {
  836. // // return ['code' => 1003, "msg" => '模板第七列为必须为对方账号!'];
  837. // return error_show( 1003, '模板第九列为必须为对方账号!');
  838. // }
  839. // if ($value[9] != '对方户名') {
  840. // // return ['code' => 1003, "msg" => '模板第八列为必须为对方户名!'];
  841. // return error_show( 1003, '模板第十列为必须为对方户名!');
  842. // }
  843. // continue;
  844. // }
  845. $company =Db::name("company_info")->where(["companyNo"=>$value[0]])->findOrEmpty();
  846. if(empty($company)){
  847. return error_show( 1003, "业务公司编号不存在{$value[0]}");
  848. }
  849. $total_fee = $value[4];
  850. $type = 0;
  851. $time = explode(" ", $value[3]);
  852. $list[$key]["tradeTime"] = date("Y-m-d", strtotime($time[0])) . " " . (isset($time[1]) ? $time[1] : "00:00:00");
  853. $list[$key]["trade_fee"] = str_replace(",","",$total_fee);
  854. $list[$key]["trade_bank"] = $value[6];
  855. $list[$key]["trade_account"] = $value[8];
  856. $list[$key]["trade_out"] = $value[9];
  857. $list[$key]["trade_in"] = $value[1]??$company['company_name'];
  858. $list[$key]["trade_type"] = $type;
  859. $list[$key]["trade_used"] = isset($value[10]) ? $value[10] : "";
  860. $list[$key]["trade_remark"] = '';
  861. $list[$key]["companyNo"] = $value[0];
  862. $list[$key]["trade_in_account"] = $value[2]??"";
  863. }
  864. if(empty($list)){
  865. return error_show( 1003, '导入数据不能为空!');
  866. }
  867. Db::startTrans();
  868. try{
  869. $tra=[];
  870. foreach ($list as $value) {
  871. $temp = [];
  872. $temp["tradNo"] = makeStr('S');
  873. $temp['trade_time'] = $value['tradeTime'];
  874. $temp['total_fee'] = $value['trade_fee'];
  875. $temp['trade_bank'] = $value['trade_bank'];
  876. $temp['trade_account'] = $value['trade_account'];
  877. $temp['trade_type'] =$value['trade_type'];
  878. $temp['trade_out'] = $value['trade_out'];
  879. $temp['trade_in'] = $value['trade_in'];
  880. $temp['trade_in_account'] = $value['trade_in_account'];
  881. $temp['companyNo'] = $value['companyNo'];
  882. $temp['trade_used'] = $value['trade_used'];
  883. $temp['trade_remark'] = $value['trade_remark'];
  884. $temp['balance'] =$value['trade_fee'];
  885. $temp['addtime'] = date("Y-m-d H:i:s");
  886. $temp['updatetime'] = date("Y-m-d H:i:s");
  887. $tra[]=$temp;
  888. }
  889. $list = Db::name('trade')->insertAll($tra);
  890. if($list==count($tra)){
  891. Db::commit();
  892. return app_show(0, "资金导入成功");
  893. }else{
  894. Db::rollback();
  895. return app_show(1004, "资金导入失败");
  896. }
  897. }catch (\Exception $e){
  898. Db::rollback();
  899. return app_show(1004, $e->getMessage());
  900. }
  901. }
  902. }