OrderPay.php 56 KB

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