OrderPay.php 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098
  1. <?php
  2. namespace app\admin\controller;
  3. use app\admin\BaseController;
  4. use app\admin\model\ReportCode;use think\App;
  5. use think\Exception;
  6. use think\facade\Db;
  7. use think\facade\Validate;
  8. use think\queue\command\Retry;
  9. class OrderPay extends BaseController{
  10. public function __construct(App $app) {parent::__construct($app);}
  11. //新建资金认领数据
  12. public function create(){
  13. $tradNo = isset($this->post['tradNo'])&&$this->post['tradNo']!=""?trim($this->post['tradNo']):"";
  14. if($tradNo==""){
  15. return error_show(1004,"参数 tradNo 不能为空");
  16. }
  17. $orderArr = isset($this->post['orderArr'])&&!empty($this->post['orderArr'])?$this->post['orderArr']:[];
  18. if(empty($orderArr)){
  19. return error_show(1004,"参数 orderArr 不能为空");
  20. }
  21. Db::startTrans();
  22. try{
  23. $trade =Db::name("trade")->where(["tradNo"=>$tradNo,"is_del"=>0])->lock(true)->findOrEmpty();
  24. if(empty($trade)){
  25. Db::rollback();
  26. return error_show(1004,"未找到资金信息");
  27. }
  28. $total_fee =array_sum(array_column($orderArr,"trad_fee"));
  29. if ($trade['balance']<$total_fee){
  30. Db::rollback();
  31. return error_show(1004,"资金余额不足核销");
  32. }
  33. $assoc=[];
  34. foreach ($orderArr as $value){
  35. $logNo=makeNo("TRC");
  36. if(!isset($value['sequenceNo'])||$value['sequenceNo']==""){
  37. Db::rollback();
  38. return error_show(1004,"销售单编号不能为空");
  39. }
  40. if(!isset($value['trad_fee'])||$value['trad_fee']==""){
  41. Db::rollback();
  42. return error_show(1004,"销售单核销金额不能为空");
  43. }
  44. $qrd =Db::name("qrd_info")->where("sequenceNo","=",$value['sequenceNo'])->field("id,customerNo,status,pay_fee,wpay_fee,pay_status,totalPrice")->findOrEmpty();
  45. if(empty($qrd)){
  46. Db::rollback();
  47. return error_show(1004,"销售单信息未找到");
  48. }
  49. if($qrd['wpay_fee']<$value['trad_fee']){
  50. Db::rollback();
  51. return error_show(1004,"销售单未付款金额不足核销金额");
  52. }
  53. $updt=[
  54. "pay_fee"=>$qrd['pay_fee']+$value['trad_fee'],
  55. "wpay_fee"=>$qrd['wpay_fee']-$value['trad_fee'],
  56. "pay_status"=>2,
  57. "status"=>1,
  58. "updatetime"=>date("Y-m-d H:i:s")
  59. ];
  60. $qrdup = Db::name("qrd_info")->where($qrd)->update($updt);
  61. if($qrdup==false){
  62. Db::rollback();
  63. return error_show(1004,"销售单核销金额失败");
  64. }
  65. $temp=[
  66. "assocNo"=>makeNo("AS"),
  67. "apply_id"=>$this->uid,
  68. "apply_name"=>$this->uname,
  69. "type"=>2,
  70. "orderCode"=>$value['sequenceNo'],
  71. "customerNo"=>$qrd['customerNo'],
  72. "viceCode"=>$logNo,
  73. "order_total"=>$qrd['totalPrice'],
  74. "vice_total"=>$total_fee,
  75. "cancel_fee"=>$value['trad_fee'],
  76. "status"=>1,
  77. "addtime"=>date("Y-m-d H:i:s"),
  78. "updatetime"=>date("Y-m-d H:i:s")
  79. ];
  80. $assoc[]=$temp;
  81. $create = [
  82. "logNo"=>$logNo,
  83. "tradNo"=>$tradNo,
  84. "companyNo"=>$trade['companyNo'],
  85. "customerNo"=>$qrd['customerNo'],
  86. "apply_id"=>$this->uid,
  87. "apply_name"=>$this->uname,
  88. "trade_time"=>$trade['trade_time'],
  89. "total_fee"=>$value['trad_fee'],
  90. "status"=>1,
  91. "addtime"=>date("Y-m-d H:i:s"),
  92. "updatetime"=>date("Y-m-d H:i:s")
  93. ];
  94. $tradchild =Db::name("trade_pool")->insert($create);
  95. if($tradchild ==false){
  96. Db::rollback();
  97. return error_show(1004,"资金认领失败");
  98. }
  99. $report=ReportCode::where(["qrdNo"=>$value['sequenceNo']])->find();
  100. if($report)$report->setField("logNo",$logNo);
  101. if($report)$report->setField("tradNo",$tradNo);
  102. }
  103. $asscin= Db::name("assoc")->insertAll($assoc);
  104. if($asscin!=count($assoc)){
  105. Db::rollback();
  106. return error_show(1004,"销售单核销金额关联失败");
  107. }
  108. $update=[
  109. "balance"=>$trade['balance']-$total_fee,
  110. "used_fee"=>$trade['used_fee']+$total_fee,
  111. "status"=>($trade['balance']-$total_fee)==0 ? 3:2,
  112. "updatetime"=>date("Y-m-d H:i:s"),
  113. ];
  114. $tradup=Db::name("trade")->where($trade)->update($update);
  115. if($tradup){
  116. Db::commit();
  117. return app_show(0,"资金认领成功");
  118. }
  119. Db::rollback();
  120. return error_show(1004,"资金认领失败");
  121. }catch (\Exception $e){
  122. Db::rollback();
  123. return error_show(1004,$e->getMessage());
  124. }
  125. }
  126. // 1待审批2审批通过3审批驳回4退款5解除认领
  127. public function status(){
  128. $logNo = isset($this->post['logNo'])&&$this->post['logNo']!=""?trim($this->post['logNo']):"";
  129. if($logNo==""){
  130. return error_show(1004,"参数logNo不能为空");
  131. }
  132. $logingo= Db::name("trade_pool")->where(["logNo"=>$logNo,"is_del"=>0])->find();
  133. if($logingo==false){
  134. return error_show(1004,"资金认领信息未找到");
  135. }
  136. $status=isset($this->post['status'])&&$this->post['status']!=""?intval($this->post['status']):"";
  137. if($status==""){
  138. return error_show(1004,"参数 status 不能为空");
  139. }
  140. if($status==5 &&$logingo['status']!=1 ){
  141. return error_show(1004,"认领资金状态有误");
  142. }
  143. $remark=isset($this->post['remark'])&&$this->post['remark']!=""?trim($this->post['remark']):"";
  144. $trade =Db::name("trade")->where(["tradNo"=>$logingo['tradNo'],"is_del"=>0])->findOrEmpty();
  145. if(empty($trade)){
  146. return error_show(1005,"资金信息未找到");
  147. }
  148. Db::startTrans();
  149. try {
  150. $up=["status"=>$status,"updatetime"=>date("Y-m-d H:i:s"),"remark"=>$remark];
  151. $logup = Db::name("trade_pool")->where($logingo)->update($up);
  152. if($logup){
  153. if($status==2){
  154. $qrdArr=Db::name("assoc")->where(["viceCode"=>$logNo,"status"=>1,"is_del"=>0])->select()->toArray();
  155. if(!empty($qrdArr)){
  156. foreach ($qrdArr as $value){
  157. $qrd =Db::name("qrd_info")->where(["sequenceNo"=>$value['orderCode']])->field("id,status,pay_fee,apay_fee,wpay_fee,pay_status,totalPrice")->findOrEmpty();
  158. if(empty($qrd)){
  159. Db::rollback();
  160. return error_show(1005,"未找到销售单数据");
  161. }
  162. if($qrd['pay_fee']<$value['cancel_fee']){
  163. Db::rollback();
  164. return error_show(1005,"销售单待付金额不足");
  165. }
  166. $update =[
  167. "apay_fee"=>$qrd['apay_fee']+$value['cancel_fee'],
  168. "pay_fee"=>$qrd['pay_fee']-$value['cancel_fee'],
  169. "paytime"=>date("Y-m-d H:i:s"),
  170. "pay_status"=>$qrd['wpay_fee']==0 && ($qrd['pay_fee']-$value['cancel_fee'])==0 ? 3:2,
  171. "updatetime"=>date("Y-m-d H:i:s")
  172. ];
  173. $qrdup =Db::name("qrd_info")->where($qrd)->update($update);
  174. if($qrdup==false){
  175. Db::rollback();
  176. return error_show(1005,"销售单更新失败");
  177. }
  178. $asscup =[
  179. "status"=>2,
  180. "assoc_time"=>date("Y-m-d H:i:s"),
  181. "updatetime"=>date("Y-m-d H:i:s")
  182. ];
  183. $assc=Db::name("assoc")->where($value)->update($asscup);
  184. if($assc==false){
  185. Db::rollback();
  186. return error_show(1005,"销售单更新失败");
  187. }
  188. }
  189. }
  190. }
  191. if($status==3 || $status==5){
  192. $qrdArr=Db::name("assoc")->where(["viceCode"=>$logNo,"status"=>1,"is_del"=>0])->select()->toArray();
  193. if(!empty($qrdArr)){
  194. foreach ($qrdArr as $value){
  195. $qrd =Db::name("qrd_info")->where(["sequenceNo"=>$value['orderCode']])->field("id,status,pay_fee,apay_fee,wpay_fee,pay_status,totalPrice")->findOrEmpty();
  196. if(empty($qrd)){
  197. Db::rollback();
  198. return error_show(1005,"未找到销售单数据");
  199. }
  200. if($qrd['pay_fee']<$value['cancel_fee']){
  201. Db::rollback();
  202. return error_show(1005,"销售单待付金额不足");
  203. }
  204. $update =[
  205. "wpay_fee"=>$qrd['wpay_fee']+$value['cancel_fee'],
  206. "pay_fee"=>$qrd['pay_fee']-$value['cancel_fee'],
  207. "pay_status"=>$qrd['apay_fee']==0 && ($qrd['pay_fee']-$value['cancel_fee'])==0 ? 1:2,
  208. "status"=>$qrd['apay_fee']==0 && ($qrd['pay_fee']-$value['cancel_fee'])==0 ?0:1,
  209. "updatetime"=>date("Y-m-d H:i:s")
  210. ];
  211. $qrdup =Db::name("qrd_info")->where($qrd)->update($update);
  212. if($qrdup==false){
  213. Db::rollback();
  214. return error_show(1005,"销售单更新失败");
  215. }
  216. $asscup =[
  217. "status"=>3,
  218. "updatetime"=>date("Y-m-d H:i:s")
  219. ];
  220. $assc=Db::name("assoc")->where($value)->update($asscup);
  221. if($assc==false){
  222. Db::rollback();
  223. return error_show(1005,"销售单更新失败");
  224. }
  225. $report=ReportCode::where(["qrdNo"=>$value['orderCode']])->find();
  226. if($report)$report->rmField("logNo",$logNo);
  227. }
  228. }
  229. if($trade['used_fee']<$logingo['total_fee']){
  230. Db::rollback();
  231. return error_show(1005,"资金信息已认领金额不足");
  232. }
  233. $tradup =[
  234. "used_fee"=>$trade['used_fee']-$logingo['total_fee'],
  235. "balance"=>$trade['balance']+$logingo['total_fee'],
  236. "status"=>($trade['used_fee']-$logingo['total_fee'])==0 ? 1 :2,
  237. "updatetime"=>date("Y-m-d H:i:s")
  238. ];
  239. $updaT=Db::name("trade")->where($trade)->update($tradup);
  240. if($updaT==false){
  241. Db::rollback();
  242. return error_show(1005,"资金信息更新失败");
  243. }
  244. }
  245. Db::commit();
  246. return app_show(0,"审核成功");
  247. }
  248. Db::rollback();
  249. return error_show(1004,"审核失败");
  250. }catch (\Exception $e){
  251. Db::rollback();
  252. return error_show(1004,$e->getMessage());
  253. }
  254. }
  255. //资金信息列表
  256. public function list(){
  257. $condition=[];
  258. $page=isset($this->post['page'])&&$this->post['page']!="" ? intval($this->post['page']) :1;
  259. $size=isset($this->post['size'])&&$this->post['size']!="" ? intval($this->post['size']) :15;
  260. $name =isset($this->post['name'])&&$this->post['name']!=""? trim($this->post['name']):"";
  261. $bank =isset($this->post['bank'])&&$this->post['bank']!=""? trim($this->post['bank']):"";
  262. $start =isset($this->post['start'])&&$this->post['start']!=""? trim($this->post['start']):"";
  263. $end =isset($this->post['end'])&&$this->post['end']!=""? trim($this->post['end']):"";
  264. $status =isset($this->post['status'])&&$this->post['status']!==""? intval($this->post['status']):"";
  265. $tradNo =isset($this->post['tradNo'])&&$this->post['tradNo']!=""? trim($this->post['tradNo']):"";
  266. $companyNo =isset($this->post['companyNo'])&&$this->post['companyNo']!=""? trim($this->post['companyNo']):"";
  267. $used_lower =isset($this->post['userd_lower'])&&$this->post['userd_lower']!==""? floor($this->post['userd_lower']) :"0";
  268. $used_upper =isset($this->post['used_upper'])&&$this->post['used_upper']!==""? floor($this->post['used_upper']):"";
  269. if($used_lower!=="")$condition[]=["used_fee",">=",$used_lower];
  270. if($used_upper!=="")$condition[]=["used_fee","<=",$used_upper];
  271. $total_lower =isset($this->post['total_lower'])&&$this->post['total_lower']!==""? floor($this->post['total_lower']) :"0";
  272. $total_upper =isset($this->post['total_upper'])&&$this->post['total_upper']!==""? floor($this->post['total_upper']):"";
  273. if($total_lower!=="")$condition[]=["total_fee",">=",$total_lower];
  274. if($total_upper!=="")$condition[]=["total_fee","<=",$total_upper];
  275. if($companyNo!==""){
  276. $condition[]=["companyNo","=",$companyNo];
  277. }
  278. $relaComNo= isset($this->post['relaComNo'])&&$this->post['relaComNo']!="" ? trim($this->post['relaComNo']) :"";
  279. if($relaComNo!=""){
  280. $condition[]=["companyNo","=",$relaComNo];
  281. }
  282. if($tradNo!==""){
  283. $condition[]=["tradNo","like","%$tradNo%"];
  284. }
  285. if($bank!=""){
  286. $condition[]=["trade_bank","like","%$bank%"];
  287. }
  288. if($name!=""){
  289. $condition[]=["trade_out","like","%$name%"];
  290. }
  291. if($start!=""){
  292. $condition[]=["trade_time",">=",$start." 00:00:00"];
  293. }
  294. if($end!=""){
  295. $condition[]=["trade_time","<=",$end." 23:59:59"];
  296. }
  297. if($status!==""){
  298. $condition[]=["status","=",$status];
  299. }
  300. $count=Db::name("trade")->where($condition)->count();
  301. $total=ceil($count/$size);
  302. $page=$page>$total? intval($total):$page;
  303. $list = Db::name("trade")
  304. ->where($condition)
  305. ->page($page, $size)
  306. ->order(['addtime' => 'desc', 'id' => 'desc'])
  307. ->select()
  308. ->toArray();
  309. foreach ($list as &$value) {
  310. $data = Db::name("trade_pool")
  311. ->alias("a")
  312. ->leftJoin("assoc b", "a.logNo=b.viceCode")
  313. ->leftJoin("qrd_info c", "b.orderCode=c.sequenceNo")
  314. ->where(["a.tradNo" => $value['tradNo'], "a.is_del" => 0])
  315. ->order(['a.addtime' => 'desc', 'a.id' => 'desc'])
  316. ->field("a.*,b.orderCode,c.qrdSource,goodNo,goodName,qrdType,c.ownerName,c.ownerid,c.department,c.poCode,c.customerName,c.customerNo")
  317. ->select()
  318. ->toArray();
  319. $value['child'] = $data;
  320. $value['companyName'] = Db::name("supplier_info")->where(["code" => $value['companyNo']])->value("name", "");
  321. }
  322. return app_show(0,"获取成功",["list"=>$list,"count"=>$count]);
  323. }
  324. // 认领资金列表明细
  325. public function tradeList(){
  326. $page=isset($this->post['page'])&&$this->post['page']!="" ? intval($this->post['page']) :1;
  327. $size=isset($this->post['size'])&&$this->post['size']!="" ? intval($this->post['size']) :15;
  328. $condition =[["a.is_del","=",0],["b.is_del","=",0]];
  329. $name =isset($this->post['name'])&&$this->post['name']!=""? trim($this->post['name']):"";
  330. $bank =isset($this->post['bank'])&&$this->post['bank']!=""? trim($this->post['bank']):"";
  331. $start =isset($this->post['start'])&&$this->post['start']!=""? trim($this->post['start']):"";
  332. $end =isset($this->post['end'])&&$this->post['end']!=""? trim($this->post['end']):"";
  333. $status =isset($this->post['status'])&&$this->post['status']!==""? intval($this->post['status']):"";
  334. $apply_id =isset($this->post['apply_id'])&&$this->post['apply_id']!=""? intval($this->post['apply_id']):"";
  335. $apply_name =isset($this->post['apply_name'])&&$this->post['apply_name']!=""? trim($this->post['apply_name']):"";
  336. $tradNo =isset($this->post['tradNo'])&&$this->post['tradNo']!=""? trim($this->post['tradNo']):"";
  337. $orderCode =isset($this->post['orderCode'])&&$this->post['orderCode']!=''?trim($this->post['orderCode']):"";
  338. $companyNo =isset($this->post['companyNo'])&&$this->post['companyNo']!=""? trim($this->post['companyNo']):"";
  339. if($companyNo!==""){
  340. $condition[]=["a.companyNo","=",$companyNo];
  341. }
  342. $relaComNo= isset($this->post['relaComNo'])&&$this->post['relaComNo']!="" ? trim($this->post['relaComNo']) :"";
  343. if($relaComNo!=""){
  344. $condition[]=["a.companyNo","=",$relaComNo];
  345. }
  346. if($tradNo!==""){
  347. $condition[]=["a.tradNo","like","%$tradNo%"];
  348. }
  349. if($orderCode!==""){
  350. $condition[]=["d.orderCode","like","%$orderCode%"];
  351. }
  352. $logNo =isset($this->post['logNo'])&&$this->post['logNo']!=""? trim($this->post['logNo']):"";
  353. if($logNo!==""){
  354. $condition[]=["a.logNo","like","%$logNo%"];
  355. }
  356. if($bank!=""){
  357. $condition[]=["b.trade_bank","like","%$bank%"];
  358. }
  359. if($name!=""){
  360. $condition[]=["b.trade_out","like","%$name%"];
  361. }
  362. if($start!=""){
  363. $condition[]=["a.addtime",">=",date("Y-m-d 00:00:00",strtotime($start))];
  364. }
  365. if($end!=""){
  366. $condition[]=["a.addtime","<=",date("Y-m-d 23:59:59",strtotime($start))];
  367. }
  368. if($status!==""){
  369. $condition[]=["a.status","=",$status];
  370. }
  371. if($apply_id!==""){
  372. $condition[]=["a.apply_id","=",$apply_id];
  373. }
  374. if($apply_name!==""){
  375. $condition[]=["a.apply_name","like","%$apply_name%"];
  376. }
  377. $count=Db::name("trade_pool")->alias("a")
  378. ->leftJoin("trade b","a.tradNo=b.tradNo")
  379. ->leftJoin("assoc d","a.logNo=d.viceCode")
  380. ->leftJoin("qrd_info c","d.orderCode=c.sequenceNo")
  381. ->where($condition)->count();
  382. $total=ceil($count/$size);
  383. $page=$page>$total? intval($total):$page;
  384. $list =Db::name("trade_pool")->alias("a")
  385. ->leftJoin("trade b","a.tradNo=b.tradNo")
  386. ->leftJoin("assoc d","a.logNo=d.viceCode")
  387. ->leftJoin("qrd_info c","d.orderCode=c.sequenceNo")
  388. ->where($condition)
  389. ->page($page,$size)
  390. ->field("a.*,b.trade_bank,b.trade_account,b.trade_out,b.total_fee as btotal_fee,b.used_fee,b.balance,c.customerName,c.customerNo,
  391. 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")
  392. ->order("a.addtime desc")->select();
  393. return app_show(0,"获取成功",["list"=>$list,"count"=>$count]);
  394. }
  395. // 获取资金下面的认领信息
  396. public function tradeQuery(){
  397. $tradNo =isset($this->post['tradNo'])&&$this->post['tradNo']!=""? trim($this->post['tradNo']):"";
  398. $status =isset($this->post['status'])&&$this->post['status']!=""? intval($this->post['status']):"";
  399. $apply_id =isset($this->post['apply_id'])&&$this->post['apply_id']!=""? intval($this->post['apply_id']):"";
  400. $apply_name =isset($this->post['apply_name'])&&$this->post['apply_name']!=""? trim($this->post['apply_name']):"";
  401. $companyNo =isset($this->post['companyNo'])&&$this->post['companyNo']!=""? trim($this->post['companyNo']):"";
  402. if($companyNo!==""){
  403. $condition[]=["companyNo","=",$companyNo];
  404. }
  405. $relaComNo= isset($post['relaComNo'])&&$post['relaComNo']!="" ? trim($post['relaComNo']) :"";
  406. if($relaComNo!=""){
  407. $condition[]=["companyNo","=",$relaComNo];
  408. }
  409. $condition=[["is_del","=",0]];
  410. if($apply_id!==""){
  411. $condition[]=["apply_id","=",$apply_id];
  412. }
  413. if($apply_name!==""){
  414. $condition[]=["apply_name","like","%$apply_name%"];
  415. }
  416. if($status!==""){
  417. $condition[]=["status","=",$status];
  418. }
  419. if($tradNo!==""){
  420. $condition[]=["tradNo","like","%$tradNo%"];
  421. }
  422. $list =Db::name("trade_pool")->where($condition)->order("addtime desc")->select();
  423. return app_show(0,"获取成功",$list);
  424. }
  425. //资金详情
  426. public function tradeInfo(){
  427. $tradNo =isset($this->post['tradNo'])&&$this->post['tradNo']!=""? trim($this->post['tradNo']):"";
  428. if($tradNo==""){
  429. return error_show(1004,"参数 tradNo 不能为空");
  430. }
  431. $tradinfo = Db::name("trade")->where(["tradNo"=>$tradNo,"is_del"=>0])->find();
  432. if($tradinfo==false){
  433. return error_show(1004,"资金信息未找到");
  434. }
  435. return app_show(0,"获取成功",$tradinfo);
  436. }
  437. //资金认领详情
  438. public function logInfo(){
  439. $logNo =isset($this->post['logNo'])&&$this->post['logNo']!=""? trim($this->post['logNo']):"";
  440. if($logNo==""){
  441. return error_show(1004,"参数 logNo 不能为空");
  442. }
  443. $tradinfo = Db::name("trade_pool")->where(["logNo"=>$logNo,"is_del"=>0])->find();
  444. if($tradinfo==false){
  445. return error_show(1004,"资金信息未找到");
  446. }
  447. $trade = Db::name("trade")->where(["tradNo"=>$tradinfo['tradNo'],"is_del"=>0])->find();
  448. $tradinfo['trade_out']= $trade['trade_out']??"";
  449. $tradinfo['trade_in']= $trade['trade_in']??"";
  450. $tradinfo['trade_bank']= $trade['trade_bank']??"";
  451. $tradinfo['customerName']= Db::name("customer_info")->where(["companyNo"=>$tradinfo['customerNo']])->value("companyName","");
  452. $tradinfo['companyNo']= $trade['companyNo']??"";
  453. $tradinfo['total_fee']= $trade['total_fee']??"";
  454. $tradinfo['balance']= $trade['balance']??"";
  455. $tradinfo['used_fee']= $trade['used_fee']??"";
  456. $orderinfo = Db::name("assoc")->alias("a")->leftJoin("qrd_info c","a.orderCode=c.sequenceNo")
  457. ->where(["a.viceCode"=>$logNo,"a.is_del"=>0,"a.status"=>[1,2,3]])
  458. ->order("a.addtime desc")
  459. ->field("c.*,a.cancel_fee")
  460. ->findOrEmpty();
  461. $tradinfo['orderinfo']=$orderinfo;
  462. return app_show(0,"获取成功",$tradinfo);
  463. }
  464. //认领资金退回或退款 type 1 退款 2 解除资金认领
  465. public function ReturnPay(){
  466. $logNo = isset($this->post['logNo'])&&$this->post['logNo']!="" ? trim($this->post['logNo']):"";
  467. if($logNo==''){
  468. return error_show(1004,"参数 logNo 不能为空");
  469. }
  470. $type = isset($this->post['type'])&&$this->post['type']!="" ? intval($this->post['type']):"";
  471. if($type==""){
  472. return error_show(1004,"参数 type 不能为空");
  473. }
  474. $loginfo = Db::name("trade_pool")->where(["logNo"=>$logNo,"is_del"=>0])->find();
  475. if($loginfo==false){
  476. return error_show(1004,"认领资金信息未找到");
  477. }
  478. if($loginfo['status']==2 && $type==1)return error_show(1004,"认领资金信息审核已通过");
  479. if($loginfo['status']!=2 && $type==2)return error_show(1004,"认领资金信息未审核通过");
  480. $tradinfo = Db::name("trade")->where(["tradNo"=>$loginfo['tradNo'],"is_del"=>0])->find();
  481. if($tradinfo==false){
  482. return error_show(1004,"资金信息未找到");
  483. }
  484. $isT= Db::name("trade_return")->where(["logNo"=>$logNo,"tradNo"=>$loginfo['tradNo'],"status"=>[0,1]])
  485. ->findOrEmpty();
  486. if(!empty($isT)) return error_show(1004,"资金退回流程已存在");
  487. $reason = isset($this->post['return_reason'])&&$this->post['return_reason']!="" ? trim($this->post['return_reason']):"";
  488. $remark = isset($this->post['remark'])&&$this->post['remark']!="" ? trim($this->post['remark']):"";
  489. $returnCode =makeNo("RTA");
  490. $data=[
  491. "returnCode"=>$returnCode,
  492. "logNo"=>$logNo,
  493. "companyNo"=>$tradinfo['companyNo'],
  494. "tradNo"=>$loginfo['tradNo'],
  495. "return_img"=>'',
  496. "type"=>$type,
  497. "apply_id"=>$this->uid,
  498. "apply_name"=>$this->uname,
  499. "return_reason"=>$reason,
  500. "remark"=>$remark,
  501. "addtime"=>date("Y-m-d H:i:s"),
  502. "updatetime"=>date("Y-m-d H:i:s")
  503. ];
  504. $in = Db::name("trade_return")->insert($data);
  505. if($in){
  506. return app_show(0,"申请新建成功");
  507. }else{
  508. return error_show(1004,"申请新建失败");
  509. }
  510. }
  511. /**退款列表
  512. * //page size tradNo logNo apply_id apply_name type status returnCode
  513. * @return \think\response\Json|void
  514. * @throws \think\db\exception\DataNotFoundException
  515. * @throws \think\db\exception\DbException
  516. * @throws \think\db\exception\ModelNotFoundException
  517. */
  518. public function returnList(){
  519. $page=isset($this->post['page'])&&$this->post['page']!='' ? intval($this->post['page']):1;
  520. $size=isset($this->post['size'])&&$this->post['size']!='' ? intval($this->post['size']):15;
  521. $tradNo =isset($this->post['tradNo'])&&$this->post['tradNo']!=''?trim($this->post['tradNo']):"";
  522. $logNo =isset($this->post['logNo'])&&$this->post['logNo']!=''?trim($this->post['logNo']):"";
  523. $apply_id =isset($this->post['apply_id'])&&$this->post['apply_id']!=''?intval($this->post['apply_id']):"";
  524. $apply_name =isset($this->post['apply_name'])&&$this->post['apply_name']!=''?trim($this->post['apply_name']):"";
  525. $type =isset($this->post['type'])&&$this->post['type']!=''?intval($this->post['type']):"";
  526. $status =isset($this->post['status'])&&$this->post['status']!==''?intval($this->post['status']):"";
  527. $returnCode =isset($this->post['returnCode'])&&$this->post['returnCode']!=''?trim($this->post['returnCode']):"";
  528. $orderCode =isset($this->post['orderCode'])&&$this->post['orderCode']!=''?trim($this->post['orderCode']):"";
  529. $companyNo =isset($this->post['companyNo'])&&$this->post['companyNo']!=""? trim($this->post['companyNo']):"";
  530. if($companyNo!==""){
  531. $condition[]=["a.companyNo","=",$companyNo];
  532. }
  533. $relaComNo= isset($this->post['relaComNo'])&&$this->post['relaComNo']!="" ? trim($this->post['relaComNo']) :"";
  534. if($relaComNo!=""){
  535. $condition[]=["a.companyNo","=",$relaComNo];
  536. }
  537. $condition=[["a.is_del","=",0]];
  538. if($tradNo!=''){
  539. $condition[]=["a.tradNo","like","%$tradNo%"];
  540. }
  541. if($orderCode!=''){
  542. $condition[]=["b.orderCode","like","%$orderCode%"];
  543. }
  544. if($logNo!=''){
  545. $condition[]=["a.logNo","like","%$logNo%"];
  546. }
  547. if($apply_id!=''){
  548. $condition[]=["a.apply_id","=",$apply_id];
  549. }
  550. if($apply_name!=''){
  551. $condition[]=["a.apply_name","like","%$apply_name%"];
  552. }
  553. if($returnCode!=''){
  554. $condition[]=["a.returnCode","like","%$returnCode%"];
  555. }
  556. if($type!=''){
  557. $condition[]=["a.type","=",$type];
  558. }
  559. if($status!==''){
  560. $condition[]=["a.status","=",$status];
  561. }
  562. $count =Db::name("trade_return")->alias("a")
  563. ->leftJoin("assoc b","a.logNo=b.viceCode")
  564. ->leftJoin("qrd_info c","c.sequenceNo=b.orderCode")
  565. ->where($condition)->count();
  566. $total=ceil($count/$size);
  567. $page = $page>$total ? intval($total):$page;
  568. $list=Db::name("trade_return")->alias("a")->leftJoin("assoc b","a.logNo=b.viceCode")
  569. ->leftJoin("qrd_info c","c.sequenceNo=b.orderCode")
  570. ->field("a.*,b.orderCode,c.qrdSource,goodNo,goodName,qrdType,c.ownerName,c.ownerid,c.department,c.poCode,c.platName,b.cancel_fee,c.customerName")
  571. ->where($condition)->page($page,$size)->order("a.addtime desc")->select();
  572. return app_show(0,"获取成功",["list"=>$list,"count"=>$count]);
  573. }
  574. //退款状态审核 0 待审核 1 财务审核 2 财务驳回
  575. public function returnStatus(){
  576. $returnCode=isset($this->post['returnCode'])&&$this->post['returnCode']!=''?trim($this->post['returnCode']):"";
  577. if($returnCode==''){
  578. return error_show(1004,"参数 returnCode 不能为空");
  579. }
  580. $returninfo =Db::name("trade_return")->where(["returnCode"=>$returnCode,"is_del"=>0])->find();
  581. if($returninfo==false){
  582. return error_show(1004,"退款申请数据未找到");
  583. }
  584. $status=isset($this->post['status']) &&$this->post['status']!=''?intval($this->post['status']) : '';
  585. if($status==''){
  586. return error_show(1004,"参数 status 不能为空");
  587. }
  588. $return_img = isset($this->post['return_img'])&&$this->post['return_img']!=''?trim($this->post['return_img']):"";
  589. $remark = isset($this->post['remark'])&&$this->post['remark']!=''?trim($this->post['remark']):"";
  590. $loginfo = Db::name("trade_pool")->where(["logNo"=>$returninfo['logNo'],"is_del"=>0])->find();
  591. if($loginfo==false){
  592. return error_show(1004,"认领资金信息未找到");
  593. }
  594. if($loginfo['status']!=2)return error_show(1004,"认领资金信息审核未通过");
  595. Db::startTrans();
  596. try{
  597. $update=["status"=>$status,"return_img"=>$return_img,"remark"=>$remark,"updatetime"=>date("Y-m-d H:i:s")];
  598. $up =Db::name("trade_return")->where($returninfo)->update($update);
  599. if($up){
  600. if($status==1){
  601. $qrdArr=Db::name("assoc")->where(["viceCode"=>$returninfo['logNo'],"status"=>[1,2],"is_del"=>0])
  602. ->select()->toArray();
  603. if(!empty($qrdArr)){
  604. foreach ($qrdArr as $value){
  605. $qrd =Db::name("qrd_info")->where(["sequenceNo"=>$value['orderCode']])->field("id,status,pay_fee,apay_fee,wpay_fee,pay_status,totalPrice")->findOrEmpty();
  606. if(empty($qrd)){
  607. Db::rollback();
  608. return error_show(1005,"未找到销售单数据");
  609. }
  610. if($returninfo['type']==2){
  611. if($qrd['apay_fee']<$value['cancel_fee']){
  612. Db::rollback();
  613. return error_show(1005,"销售单已付金额不足");
  614. }
  615. $update =[
  616. "wpay_fee"=>$qrd['wpay_fee']+$value['cancel_fee'],
  617. "apay_fee"=>$qrd['apay_fee']-$value['cancel_fee'],
  618. "pay_status"=>($qrd['apay_fee']-$value['cancel_fee'])==0 && $qrd['pay_fee']==0 ? 1:2,
  619. "status"=>$qrd['pay_fee']==0 && ($qrd['apay_fee']-$value['cancel_fee'])==0 ?0:1,
  620. "updatetime"=>date("Y-m-d H:i:s")
  621. ];
  622. }else{
  623. if($qrd['pay_fee']<$value['cancel_fee']){
  624. Db::rollback();
  625. return error_show(1005,"销售单付款中金额不足");
  626. }
  627. $update =[
  628. "wpay_fee"=>$qrd['wpay_fee']+$value['cancel_fee'],
  629. "pay_fee"=>$qrd['pay_fee']-$value['cancel_fee'],
  630. "pay_status"=>($qrd['pay_fee']-$value['cancel_fee'])==0 && $qrd['apay_fee']==0 ? 1:2,
  631. "status"=>$qrd['apay_fee']==0 && ($qrd['pay_fee']-$value['cancel_fee'])==0 ?0:1,
  632. "updatetime"=>date("Y-m-d H:i:s")
  633. ];
  634. }
  635. $qrdup =Db::name("qrd_info")->where($qrd)->update($update);
  636. if($qrdup==false){
  637. Db::rollback();
  638. return error_show(1005,"销售单更新失败");
  639. }
  640. $asscup =[
  641. "status"=>3,
  642. "updatetime"=>date("Y-m-d H:i:s")
  643. ];
  644. $assc=Db::name("assoc")->where($value)->update($asscup);
  645. if($assc==false){
  646. Db::rollback();
  647. return error_show(1005,"销售单更新失败");
  648. }
  649. $report=ReportCode::where(["qrdNo"=>$value['orderCode']])->find();
  650. if($report)$report->setField("returnTrad",$returnCode);
  651. }
  652. }
  653. $logup =["status"=>$returninfo['type']==1?5:4,"updatetime"=>date("Y-m-d H:i:s")];
  654. $upde =Db::name("trade_pool")->where($loginfo)->update($logup);
  655. if($upde==false){
  656. Db::rollback();
  657. return error_show(1005,"资金认领信息更新失败");
  658. }
  659. // if($returninfo['type']==1){
  660. $trade =Db::name("trade")->where(["tradNo"=>$returninfo['tradNo'],"is_del"=>0])->find();
  661. if($trade==false){
  662. Db::rollback();
  663. return error_show(1005,"资金信息未找到");
  664. }
  665. if($trade['used_fee']<$loginfo['total_fee']){
  666. Db::rollback();
  667. return error_show(1005,"资金信息已认领金额不足");
  668. }
  669. $tradup =[
  670. "used_fee"=>$trade['used_fee']-$loginfo['total_fee'],
  671. "balance"=>$trade['balance']+$loginfo['total_fee'],
  672. "status"=>($trade['used_fee']-$loginfo['total_fee'])==0 ?1 :2,
  673. "updatetime"=>date("Y-m-d H:i:s")
  674. ];
  675. $updaT=Db::name("trade")->where($trade)->update($tradup);
  676. if($updaT==false){
  677. Db::rollback();
  678. return error_show(1005,"资金信息更新失败");
  679. }
  680. // }
  681. }
  682. Db::commit();
  683. return app_show(0,"退款申请审核成功");
  684. }
  685. Db::rollback();
  686. return error_show(1004,'审核状态失败');
  687. }catch (\Exception $e){
  688. Db::rollback();
  689. return error_show(1004,$e->getMessage());
  690. }
  691. }
  692. //退款申请详情
  693. public function returnInfo(){
  694. $returnCode=isset($this->post['returnCode'])&&$this->post['returnCode']!=''?trim($this->post['returnCode']):"";
  695. if($returnCode==''){
  696. return error_show(1004,"参数 returnCode 不能为空");
  697. }
  698. $returninfo =Db::name("trade_return")->where(["returnCode"=>$returnCode,"is_del"=>0])->find();
  699. if($returninfo==false){
  700. return error_show(1004,"退款申请数据未找到");
  701. }
  702. $trade = Db::name("trade")->where(["tradNo"=>$returninfo['tradNo'],"is_del"=>0])->find();
  703. $returninfo['trade_out']= $trade['trade_out']??"";
  704. $returninfo['trade_in']= $trade['trade_in']??"";
  705. $returninfo['trade_bank']= $trade['trade_bank']??"";
  706. $returninfo['trade_time']= $trade['trade_time']??"";
  707. $returninfo['customerNo']= $trade['customerNo']??"";
  708. $returninfo['companyNo']= $trade['companyNo']??"";
  709. $returninfo['total_fee']= $trade['total_fee']??"";
  710. $returninfo['balance']= $trade['balance']??"";
  711. $returninfo['used_fee']= $trade['used_fee']??"";
  712. $pool =Db::name("trade_pool")->where(["logNo"=>$returninfo['logNo'],"is_del"=>0])->find();
  713. $returninfo['log_total_fee']= $pool['total_fee']??"";
  714. $returninfo['log_apply_id']= $pool['apply_id']??"";
  715. $returninfo['log_apply_name']= $pool['apply_name']??"";
  716. $orderinfo = Db::name("assoc")->alias("a")->leftJoin("qrd_info c","a.orderCode=c.sequenceNo")
  717. ->where(["a.viceCode"=>$returninfo['logNo'],"a.is_del"=>0,"a.status"=>[1,2]])
  718. ->order("a.addtime desc")
  719. ->field("c.*")
  720. ->findOrEmpty();
  721. $returninfo['orderinfo'] = $orderinfo;
  722. return app_show(0,"获取成功",$returninfo);
  723. }
  724. /**
  725. * 显示创建资源表单页.
  726. *
  727. * @return \think\Response
  728. */
  729. public function importTrade()
  730. {
  731. $files = $this->request->file("excel");
  732. $data = upload($files,$files->getOriginalExtension());
  733. if($data['code']!=0){
  734. return error_show(1004,$data['msg']);
  735. }
  736. $trade = $data['data'];
  737. $list = [];
  738. foreach ( $trade as $key => $value) {
  739. if (!isset($value[0]) || $value[0] == '') {
  740. //return ['code' => 1003, "msg" => '交易时间不能为空'];
  741. return error_show( 1003, '第'.($key+1).'行收款方公司编号不能为空!');
  742. }
  743. if (!isset($value[4]) || $value[4] == '') {
  744. // return ['code' => 1003, "msg" => '交易行名不能为空'];
  745. return error_show( 1003, '第'.($key+1).'行收入金额不能为空!');
  746. }
  747. if (!isset($value[8]) || $value[8] == '') {
  748. // return ['code' => 1003, "msg" => '对方账户不能为空'];
  749. return error_show( 1003, '第'.($key+1).'行对方账户不能为空!');
  750. }
  751. if (!isset($value[9]) || $value[9] == '') {
  752. // return ['code' => 1003, "msg" => '对方户名不能为空'];
  753. return error_show( 1003, '第'.($key+1).'行对方户名不能为空!');
  754. }
  755. if ($key == 0) {
  756. if ($value[0] != '收款方公司编号') {
  757. // return ['code' => 1003, "msg" => '模板第一列为必须为交易时间!'];
  758. return error_show( 1003, '模板第一列为必须为收款方公司编号!');
  759. }
  760. if ($value[4] != '收入金额') {
  761. // return ['code' => 1003, "msg" => '模板第二列为必须为收入金额!'];
  762. return error_show( 1003, '模板第五列为必须为收入金额!');
  763. }
  764. if ($value[6] != '交易行名') {
  765. //return ['code' => 1003, "msg" => '模板第五列为必须为交易行名!'];
  766. return error_show( 1003, '模板第七列为必须为交易行名!');
  767. }
  768. if ($value[8] != '对方账号') {
  769. // return ['code' => 1003, "msg" => '模板第七列为必须为对方账号!'];
  770. return error_show( 1003, '模板第九列为必须为对方账号!');
  771. }
  772. if ($value[9] != '对方户名') {
  773. // return ['code' => 1003, "msg" => '模板第八列为必须为对方户名!'];
  774. return error_show( 1003, '模板第十列为必须为对方户名!');
  775. }
  776. continue;
  777. }
  778. $company =Db::name("company_info")->where(["companyNo"=>$value[0]])->findOrEmpty();
  779. if(empty($company)){
  780. return error_show( 1003, "业务公司编号不存在{$value[0]}");
  781. }
  782. $total_fee = $value[4];
  783. $type = 0;
  784. $time = explode(" ", $value[3]);
  785. $list[$key]["tradeTime"] = date("Y-m-d", strtotime($time[0])) . " " . (isset($time[1]) ? $time[1] : "00:00:00");
  786. $list[$key]["trade_fee"] = str_replace(",","",$total_fee);
  787. $list[$key]["trade_bank"] = $value[6];
  788. $list[$key]["trade_account"] = $value[8];
  789. $list[$key]["trade_out"] = $value[9];
  790. $list[$key]["trade_in"] = $value[1]??$company['company_name'];
  791. $list[$key]["trade_type"] = $type;
  792. $list[$key]["trade_used"] = isset($value[10]) ? $value[10] : "";
  793. $list[$key]["trade_remark"] = '';
  794. $list[$key]["companyNo"] = $value[0];
  795. $list[$key]["trade_in_account"] = $value[2]??"";
  796. }
  797. if(empty($list)){
  798. return error_show( 1003, '导入数据不能为空!');
  799. }
  800. Db::startTrans();
  801. try{
  802. $tra=[];
  803. foreach ($list as $value) {
  804. $temp = [];
  805. $temp["tradNo"] = makeStr('S');
  806. $temp['trade_time'] = $value['tradeTime'];
  807. $temp['total_fee'] = $value['trade_fee'];
  808. $temp['trade_bank'] = $value['trade_bank'];
  809. $temp['trade_account'] = $value['trade_account'];
  810. $temp['trade_type'] =$value['trade_type'];
  811. $temp['trade_out'] = $value['trade_out'];
  812. $temp['trade_in'] = $value['trade_in'];
  813. $temp['trade_in_account'] = $value['trade_in_account'];
  814. $temp['companyNo'] = $value['companyNo'];
  815. $temp['trade_used'] = $value['trade_used'];
  816. $temp['trade_remark'] = $value['trade_remark'];
  817. $temp['balance'] =$value['trade_fee'];
  818. $temp['addtime'] = date("Y-m-d H:i:s");
  819. $temp['updatetime'] = date("Y-m-d H:i:s");
  820. $tra[]=$temp;
  821. }
  822. $list = Db::name('trade')->insertAll($tra);
  823. if($list==count($tra)){
  824. Db::commit();
  825. return app_show(0, "资金导入成功");
  826. }else{
  827. Db::rollback();
  828. return app_show(1004, "资金导入失败");
  829. }
  830. }catch (\Exception $e){
  831. Db::rollback();
  832. return app_show(1004, $e->getMessage());
  833. }
  834. }
  835. /**
  836. * 资金导入
  837. * @return \think\response\Json|void
  838. */
  839. public function importTradeByArr()
  840. {
  841. $trade =isset($this->post['data']) &&!empty($this->post['data'])? $this->post['data']:[];
  842. if(!is_array($trade) || empty($trade) ) return error_show( 1003,"参数 data 不能为空");
  843. $list = [];
  844. foreach ( $trade as $key => $value) {
  845. if (!isset($value[0]) || $value[0] == '') {
  846. //return ['code' => 1003, "msg" => '交易时间不能为空'];
  847. return error_show( 1003, '第'.($key+1).'行收款方公司编号不能为空!');
  848. }
  849. if (!isset($value[2]) || $value[2] == '') {
  850. //return ['code' => 1003, "msg" => '交易时间不能为空'];
  851. return error_show( 1003, '第'.($key+1).'行收款账户不能为空!');
  852. }
  853. if (!isset($value[3]) || $value[3] == '') {
  854. //return ['code' => 1003, "msg" => '交易时间不能为空'];
  855. return error_show( 1003, '第'.($key+1).'行交易时间不能为空!');
  856. }
  857. if (!isset($value[4]) || $value[4] == '') {
  858. // return ['code' => 1003, "msg" => '交易行名不能为空'];
  859. return error_show( 1003, '第'.($key+1).'行收入金额不能为空!');
  860. }
  861. if (!isset($value[6]) || $value[6] == '') {
  862. // return ['code' => 1003, "msg" => '交易行名不能为空'];
  863. return error_show( 1003, '第'.($key+1).'行交易银行名称不能为空!');
  864. }
  865. if (!isset($value[8]) || $value[8] == '') {
  866. // return ['code' => 1003, "msg" => '对方账户不能为空'];
  867. return error_show( 1003, '第'.($key+1).'行对方账户不能为空!');
  868. }
  869. if (!isset($value[9]) || $value[9] == '') {
  870. // return ['code' => 1003, "msg" => '对方户名不能为空'];
  871. return error_show( 1003, '第'.($key+1).'行对方户名不能为空!');
  872. }
  873. // if ($key == 0) {
  874. // if ($value[0] != '收款方公司编号') {
  875. // // return ['code' => 1003, "msg" => '模板第一列为必须为交易时间!'];
  876. // return error_show( 1003, '模板第一列为必须为收款方公司编号!');
  877. // }
  878. // if ($value[4] != '收入金额') {
  879. // // return ['code' => 1003, "msg" => '模板第二列为必须为收入金额!'];
  880. // return error_show( 1003, '模板第五列为必须为收入金额!');
  881. // }
  882. //
  883. // if ($value[6] != '交易行名') {
  884. // //return ['code' => 1003, "msg" => '模板第五列为必须为交易行名!'];
  885. // return error_show( 1003, '模板第七列为必须为交易行名!');
  886. // }
  887. // if ($value[8] != '对方账号') {
  888. // // return ['code' => 1003, "msg" => '模板第七列为必须为对方账号!'];
  889. // return error_show( 1003, '模板第九列为必须为对方账号!');
  890. // }
  891. // if ($value[9] != '对方户名') {
  892. // // return ['code' => 1003, "msg" => '模板第八列为必须为对方户名!'];
  893. // return error_show( 1003, '模板第十列为必须为对方户名!');
  894. // }
  895. // continue;
  896. // }
  897. $company =Db::name("company_info")->where(["companyNo"=>$value[0]])->findOrEmpty();
  898. if(empty($company)){
  899. return error_show( 1003, "业务公司编号不存在{$value[0]}");
  900. }
  901. $total_fee = $value[4];
  902. $type = 0;
  903. $time = explode(" ", $value[3]);
  904. $list[$key]["tradeTime"] = date("Y-m-d", strtotime($time[0])) . " " . (isset($time[1]) ? $time[1] : "00:00:00");
  905. $list[$key]["trade_fee"] = str_replace(",","",$total_fee);
  906. $list[$key]["trade_bank"] = $value[6];
  907. $list[$key]["trade_account"] = $value[8];
  908. $list[$key]["trade_out"] = $value[9];
  909. $list[$key]["trade_in"] = $value[1]??$company['company_name'];
  910. $list[$key]["trade_type"] = $type;
  911. $list[$key]["trade_used"] = isset($value[10]) ? $value[10] : "";
  912. $list[$key]["trade_remark"] = '';
  913. $list[$key]["companyNo"] = $value[0];
  914. $list[$key]["trade_in_account"] = $value[2]??"";
  915. }
  916. if(empty($list)){
  917. return error_show( 1003, '导入数据不能为空!');
  918. }
  919. Db::startTrans();
  920. try{
  921. $tra=[];
  922. $i=0;
  923. foreach ($list as $value) {
  924. $temp = [];
  925. $i++;
  926. $temp["tradNo"] = makeStr('S').str_pad($i,3,'0',STR_PAD_LEFT);
  927. $temp['trade_time'] = $value['tradeTime'];
  928. $temp['total_fee'] = $value['trade_fee'];
  929. $temp['trade_bank'] = $value['trade_bank'];
  930. $temp['trade_account'] = $value['trade_account'];
  931. $temp['trade_type'] =$value['trade_type'];
  932. $temp['trade_out'] = $value['trade_out'];
  933. $temp['trade_in'] = $value['trade_in'];
  934. $temp['trade_in_account'] = $value['trade_in_account'];
  935. $temp['companyNo'] = $value['companyNo'];
  936. $temp['trade_used'] = $value['trade_used'];
  937. $temp['trade_remark'] = $value['trade_remark'];
  938. $temp['balance'] =$value['trade_fee'];
  939. $temp['addtime'] = date("Y-m-d H:i:s");
  940. $temp['updatetime'] = date("Y-m-d H:i:s");
  941. $tra[]=$temp;
  942. }
  943. $list = Db::name('trade')->insertAll($tra);
  944. if($list==count($tra)){
  945. Db::commit();
  946. return app_show(0, "资金导入成功");
  947. }else{
  948. Db::rollback();
  949. return app_show(1004, "资金导入失败");
  950. }
  951. }catch (\Exception $e){
  952. Db::rollback();
  953. return app_show(1004, $e->getMessage());
  954. }
  955. }
  956. //批量认领资金,跟订单相关
  957. public function importTradeByBatchOrderCode()
  958. {
  959. $list = $this->request->post('list', [], 'trim');
  960. $val = Validate::rule(['list|导入数据' => 'require|array|max:100']);
  961. if (!$val->check(['list' => $list])) return error_show(1004, $val->getError());
  962. $company = $tradNos = $orderCodes = $assoc_insert_data = $OrderCodeToAssocNo = $OrderCodeToLogNo = [];
  963. $val_item = Validate::rule([
  964. 'companyNo|卖出方公司编号' => 'require|max:255',
  965. 'tradNo|资金编号' => 'require|max:255',
  966. 'orderCode|订单编号' => 'require|max:255',
  967. 'trad_fee|认领资金' => 'require|float|gt:0|max:999999999.99',
  968. ]);
  969. foreach ($list as $key => $value) {
  970. if (!$val_item->check($value)) return error_show(1004, $val_item->getError());
  971. if (!isset($company[$value['companyNo']])) $company[$value['companyNo']] = $value['companyNo'];
  972. }
  973. //判断绑定公司
  974. $companyNo = Db::name('user_role')
  975. ->where(['is_del' => 0, 'status' => 1, 'uid' => $this->uid])
  976. ->whereIn('companyNo', $company)
  977. ->column('companyNo');
  978. $tmp = array_diff($company, $companyNo);//二者取差集
  979. if(!empty($tmp)) return error_show(1004,'不能操作以下公司数据:'.implode(',',$tmp));
  980. Db::startTrans();
  981. try {
  982. $i=1;
  983. //批量资金认领
  984. foreach ($list as $key=>$value){
  985. //资金明细判断
  986. $i++;
  987. $all_trade = Db::name('trade')
  988. ->where('is_del', 0)
  989. ->whereIn('status', [1, 2])
  990. ->where('tradNo', $value['tradNo'])
  991. ->field('id,balance,used_fee,companyNo,trade_time')
  992. ->findOrEmpty();
  993. if(empty($all_trade))throw new Exception("{$value['tradNo']}资金未找到数据");
  994. if($all_trade['balance']<$value['trad_fee'])throw new Exception("{$value['tradNo']}资金余额不足");
  995. //订单明细判断
  996. $all_order=Db::name('qrd_info')
  997. ->where('is_del', 0)
  998. ->whereIn('sequenceNo',$value['orderCode'])
  999. ->field('id,customerNo,status,pay_fee,apay_fee,wpay_fee,pay_status,totalPrice')
  1000. ->findOrEmpty();
  1001. if(empty($all_order))throw new Exception("{$value['orderCode']}销售单未找到数据");
  1002. if($all_order['wpay_fee']<$value['trad_fee'])throw new Exception("{$value['orderCode']}销售单未付款金额不足核销金额");
  1003. $OrderCodeToAssocNo = substr(makeNo('AS'), 0, -3).str_pad($i,3,'0',STR_PAD_LEFT);
  1004. $OrderCodeToLogNo = substr(makeNo('TRC'), 0, -3).str_pad($i,3,'0',STR_PAD_LEFT);
  1005. //资金数据更新处理
  1006. $balance = bcsub($all_trade['balance'], $value['trad_fee'], 2);
  1007. $tradeUp=Db::name('trade')
  1008. ->where($all_trade)
  1009. ->update([
  1010. 'balance' => $balance,
  1011. 'used_fee' => bcadd($all_trade['used_fee'], $value['trad_fee'], 2),
  1012. 'status' => $balance == 0 ? 3 : 2,
  1013. 'updatetime' =>date("Y-m-d H:i:s"),
  1014. ]);
  1015. if($tradeUp==false ) throw new Exception("{$value['tradNo']}资金更新失败");
  1016. //订单数据更新处理
  1017. $orderUp=Db::name('qrd_info')
  1018. ->where($all_order)
  1019. ->update([
  1020. 'apay_fee' => bcadd($all_order['apay_fee'], $value['trad_fee'], 2),
  1021. 'wpay_fee' => bcsub($all_order['wpay_fee'], $value['trad_fee'], 2),
  1022. 'pay_status' => bcsub($all_order['wpay_fee'], $value['trad_fee'], 2)==0 &&$all_order['pay_fee']==0? 3:2,
  1023. 'status' => 1,
  1024. 'updatetime' => date("Y-m-d H:i:s")
  1025. ]);
  1026. if($orderUp==false ) throw new Exception("{$value['orderCode']}销售单更新失败");
  1027. //资金订单关联数据
  1028. $assoc_insert_data[]=[
  1029. 'assocNo' => $OrderCodeToAssocNo,
  1030. 'apply_id' => $this->uid,
  1031. 'apply_name' => $this->uname,
  1032. 'type' => 2,
  1033. 'orderCode' => $value['orderCode'],
  1034. 'customerNo' => $all_order['customerNo'],
  1035. 'viceCode' => $OrderCodeToLogNo,
  1036. 'order_total' => $all_order['totalPrice'],
  1037. 'vice_total' => $value['trad_fee'],
  1038. 'cancel_fee' => $value['trad_fee'],
  1039. 'status' => 2,//认领通过
  1040. 'addtime' => date("Y-m-d H:i:s"),
  1041. 'updatetime' => date("Y-m-d H:i:s"),
  1042. ];
  1043. //资金认领数据
  1044. $trade_pool_insert_data[]=[
  1045. 'logNo' => $OrderCodeToLogNo,
  1046. 'tradNo' => $value['tradNo'],
  1047. 'companyNo' => $value['companyNo'],
  1048. 'customerNo' => $all_order['customerNo'],
  1049. 'apply_id' => $this->uid,
  1050. 'apply_name' => $this->uname,
  1051. 'trade_time' => $all_trade['trade_time'],
  1052. 'total_fee' => $value['trad_fee'],
  1053. 'status' => 2,//2审核通过
  1054. 'addtime' => date("Y-m-d H:i:s"),
  1055. 'updatetime' => date("Y-m-d H:i:s"),
  1056. ];
  1057. }
  1058. if ($assoc_insert_data) Db::name('assoc')->insertAll($assoc_insert_data);
  1059. if ($trade_pool_insert_data) Db::name('trade_pool')->insertAll($trade_pool_insert_data);
  1060. Db::commit();
  1061. return app_show(0, '批量认领资金成功');
  1062. } catch (Exception $exception) {
  1063. Db::rollback();
  1064. return error_show(1004, '批量认领资金失败,' . $exception->getMessage());
  1065. }
  1066. }
  1067. }