OrderPay.php 54 KB

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