OrderPay.php 57 KB

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