OrderPay.php 55 KB

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