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