OrderPay.php 46 KB

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