OrderPay.php 53 KB

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