OrderPay.php 58 KB

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