1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102 |
- <?php
- namespace app\admin\controller;
- use app\admin\BaseController;
- use app\admin\model\ReportCode;use think\App;
- use think\Exception;
- use think\facade\Db;
- use think\facade\Validate;
- use think\queue\command\Retry;
- class OrderPay extends BaseController{
- public function __construct(App $app) {parent::__construct($app);}
- //新建资金认领数据
- public function create(){
- $tradNo = isset($this->post['tradNo'])&&$this->post['tradNo']!=""?trim($this->post['tradNo']):"";
- if($tradNo==""){
- return error_show(1004,"参数 tradNo 不能为空");
- }
- $orderArr = isset($this->post['orderArr'])&&!empty($this->post['orderArr'])?$this->post['orderArr']:[];
- if(empty($orderArr)){
- return error_show(1004,"参数 orderArr 不能为空");
- }
- Db::startTrans();
- try{
- $trade =Db::name("trade")->where(["tradNo"=>$tradNo,"is_del"=>0])->lock(true)->findOrEmpty();
- if(empty($trade)){
- Db::rollback();
- return error_show(1004,"未找到资金信息");
- }
- $total_fee =array_sum(array_column($orderArr,"trad_fee"));
- if ($trade['balance']<$total_fee){
- Db::rollback();
- return error_show(1004,"资金余额不足核销");
- }
- $assoc=[];
- foreach ($orderArr as $value){
- $logNo=makeNo("TRC");
- if(!isset($value['sequenceNo'])||$value['sequenceNo']==""){
- Db::rollback();
- return error_show(1004,"销售单编号不能为空");
- }
- if(!isset($value['trad_fee'])||$value['trad_fee']==""){
- Db::rollback();
- return error_show(1004,"销售单核销金额不能为空");
- }
- $qrd =Db::name("qrd_info")->where("sequenceNo","=",$value['sequenceNo'])->field("id,customerNo,status,pay_fee,wpay_fee,pay_status,totalPrice")->findOrEmpty();
- if(empty($qrd)){
- Db::rollback();
- return error_show(1004,"销售单信息未找到");
- }
- if($qrd['wpay_fee']<$value['trad_fee']){
- Db::rollback();
- return error_show(1004,"销售单未付款金额不足核销金额");
- }
- $updt=[
- "pay_fee"=>$qrd['pay_fee']+$value['trad_fee'],
- "wpay_fee"=>$qrd['wpay_fee']-$value['trad_fee'],
- "pay_status"=>2,
- "status"=>1,
- "updatetime"=>date("Y-m-d H:i:s")
- ];
- $qrdup = Db::name("qrd_info")->where($qrd)->update($updt);
- if($qrdup==false){
- Db::rollback();
- return error_show(1004,"销售单核销金额失败");
- }
- $temp=[
- "assocNo"=>makeNo("AS"),
- "apply_id"=>$this->uid,
- "apply_name"=>$this->uname,
- "type"=>2,
- "orderCode"=>$value['sequenceNo'],
- "customerNo"=>$qrd['customerNo'],
- "viceCode"=>$logNo,
- "order_total"=>$qrd['totalPrice'],
- "vice_total"=>$total_fee,
- "cancel_fee"=>$value['trad_fee'],
- "status"=>1,
- "addtime"=>date("Y-m-d H:i:s"),
- "updatetime"=>date("Y-m-d H:i:s")
- ];
- $assoc[]=$temp;
- $create = [
- "logNo"=>$logNo,
- "tradNo"=>$tradNo,
- "companyNo"=>$trade['companyNo'],
- "customerNo"=>$qrd['customerNo'],
- "apply_id"=>$this->uid,
- "apply_name"=>$this->uname,
- "trade_time"=>$trade['trade_time'],
- "total_fee"=>$value['trad_fee'],
- "status"=>1,
- "addtime"=>date("Y-m-d H:i:s"),
- "updatetime"=>date("Y-m-d H:i:s")
- ];
- $tradchild =Db::name("trade_pool")->insert($create);
- if($tradchild ==false){
- Db::rollback();
- return error_show(1004,"资金认领失败");
- }
- $report=ReportCode::where(["qrdNo"=>$value['sequenceNo']])->find();
- if($report)$report->setField("logNo",$logNo);
- if($report)$report->setField("tradNo",$tradNo);
- }
- $asscin= Db::name("assoc")->insertAll($assoc);
- if($asscin!=count($assoc)){
- Db::rollback();
- return error_show(1004,"销售单核销金额关联失败");
- }
- $update=[
- "balance"=>$trade['balance']-$total_fee,
- "used_fee"=>$trade['used_fee']+$total_fee,
- "status"=>($trade['balance']-$total_fee)==0 ? 3:2,
- "updatetime"=>date("Y-m-d H:i:s"),
- ];
- $tradup=Db::name("trade")->where($trade)->update($update);
- if($tradup){
- Db::commit();
- return app_show(0,"资金认领成功");
- }
- Db::rollback();
- return error_show(1004,"资金认领失败");
- }catch (\Exception $e){
- Db::rollback();
- return error_show(1004,$e->getMessage());
- }
- }
- // 1待审批2审批通过3审批驳回4退款5解除认领
- public function status(){
- $logNo = isset($this->post['logNo'])&&$this->post['logNo']!=""?trim($this->post['logNo']):"";
- if($logNo==""){
- return error_show(1004,"参数logNo不能为空");
- }
- $logingo= Db::name("trade_pool")->where(["logNo"=>$logNo,"is_del"=>0])->find();
- if($logingo==false){
- return error_show(1004,"资金认领信息未找到");
- }
- $status=isset($this->post['status'])&&$this->post['status']!=""?intval($this->post['status']):"";
- if($status==""){
- return error_show(1004,"参数 status 不能为空");
- }
- if($status==5 &&$logingo['status']!=1 ){
- return error_show(1004,"认领资金状态有误");
- }
- $remark=isset($this->post['remark'])&&$this->post['remark']!=""?trim($this->post['remark']):"";
- $trade =Db::name("trade")->where(["tradNo"=>$logingo['tradNo'],"is_del"=>0])->findOrEmpty();
- if(empty($trade)){
- return error_show(1005,"资金信息未找到");
- }
- Db::startTrans();
- try {
- $up=["status"=>$status,"updatetime"=>date("Y-m-d H:i:s"),"remark"=>$remark];
- $logup = Db::name("trade_pool")->where($logingo)->update($up);
- if($logup){
- if($status==2){
- $qrdArr=Db::name("assoc")->where(["viceCode"=>$logNo,"status"=>1,"is_del"=>0])->select()->toArray();
- if(!empty($qrdArr)){
- foreach ($qrdArr as $value){
- $qrd =Db::name("qrd_info")->where(["sequenceNo"=>$value['orderCode']])->field("id,status,pay_fee,apay_fee,wpay_fee,pay_status,totalPrice")->findOrEmpty();
- if(empty($qrd)){
- Db::rollback();
- return error_show(1005,"未找到销售单数据");
- }
- if($qrd['pay_fee']<$value['cancel_fee']){
- Db::rollback();
- return error_show(1005,"销售单待付金额不足");
- }
- $update =[
- "apay_fee"=>$qrd['apay_fee']+$value['cancel_fee'],
- "pay_fee"=>$qrd['pay_fee']-$value['cancel_fee'],
- "paytime"=>date("Y-m-d H:i:s"),
- "pay_status"=>$qrd['wpay_fee']==0 && ($qrd['pay_fee']-$value['cancel_fee'])==0 ? 3:2,
- "updatetime"=>date("Y-m-d H:i:s")
- ];
- $qrdup =Db::name("qrd_info")->where($qrd)->update($update);
- if($qrdup==false){
- Db::rollback();
- return error_show(1005,"销售单更新失败");
- }
- $asscup =[
- "status"=>2,
- "assoc_time"=>date("Y-m-d H:i:s"),
- "updatetime"=>date("Y-m-d H:i:s")
- ];
- $assc=Db::name("assoc")->where($value)->update($asscup);
- if($assc==false){
- Db::rollback();
- return error_show(1005,"销售单更新失败");
- }
- }
- }
- }
- if($status==3 || $status==5){
- $qrdArr=Db::name("assoc")->where(["viceCode"=>$logNo,"status"=>1,"is_del"=>0])->select()->toArray();
- if(!empty($qrdArr)){
- foreach ($qrdArr as $value){
- $qrd =Db::name("qrd_info")->where(["sequenceNo"=>$value['orderCode']])->field("id,status,pay_fee,apay_fee,wpay_fee,pay_status,totalPrice")->findOrEmpty();
- if(empty($qrd)){
- Db::rollback();
- return error_show(1005,"未找到销售单数据");
- }
- if($qrd['pay_fee']<$value['cancel_fee']){
- Db::rollback();
- return error_show(1005,"销售单待付金额不足");
- }
- $update =[
- "wpay_fee"=>$qrd['wpay_fee']+$value['cancel_fee'],
- "pay_fee"=>$qrd['pay_fee']-$value['cancel_fee'],
- "pay_status"=>$qrd['apay_fee']==0 && ($qrd['pay_fee']-$value['cancel_fee'])==0 ? 1:2,
- "status"=>$qrd['apay_fee']==0 && ($qrd['pay_fee']-$value['cancel_fee'])==0 ?0:1,
- "updatetime"=>date("Y-m-d H:i:s")
- ];
- $qrdup =Db::name("qrd_info")->where($qrd)->update($update);
- if($qrdup==false){
- Db::rollback();
- return error_show(1005,"销售单更新失败");
- }
- $asscup =[
- "status"=>3,
- "updatetime"=>date("Y-m-d H:i:s")
- ];
- $assc=Db::name("assoc")->where($value)->update($asscup);
- if($assc==false){
- Db::rollback();
- return error_show(1005,"销售单更新失败");
- }
- $report=ReportCode::where(["qrdNo"=>$value['orderCode']])->find();
- if($report)$report->rmField("logNo",$logNo);
- }
- }
- if($trade['used_fee']<$logingo['total_fee']){
- Db::rollback();
- return error_show(1005,"资金信息已认领金额不足");
- }
- $tradup =[
- "used_fee"=>$trade['used_fee']-$logingo['total_fee'],
- "balance"=>$trade['balance']+$logingo['total_fee'],
- "status"=>($trade['used_fee']-$logingo['total_fee'])==0 ? 1 :2,
- "updatetime"=>date("Y-m-d H:i:s")
- ];
- $updaT=Db::name("trade")->where($trade)->update($tradup);
- if($updaT==false){
- Db::rollback();
- return error_show(1005,"资金信息更新失败");
- }
- }
- Db::commit();
- return app_show(0,"审核成功");
- }
- Db::rollback();
- return error_show(1004,"审核失败");
- }catch (\Exception $e){
- Db::rollback();
- return error_show(1004,$e->getMessage());
- }
- }
- //资金信息列表
- public function list(){
- $condition=[];
- $page=isset($this->post['page'])&&$this->post['page']!="" ? intval($this->post['page']) :1;
- $size=isset($this->post['size'])&&$this->post['size']!="" ? intval($this->post['size']) :15;
- $name =isset($this->post['name'])&&$this->post['name']!=""? trim($this->post['name']):"";
- $bank =isset($this->post['bank'])&&$this->post['bank']!=""? trim($this->post['bank']):"";
- $start =isset($this->post['start'])&&$this->post['start']!=""? trim($this->post['start']):"";
- $end =isset($this->post['end'])&&$this->post['end']!=""? trim($this->post['end']):"";
- $status =isset($this->post['status'])&&$this->post['status']!==""? intval($this->post['status']):"";
- $tradNo =isset($this->post['tradNo'])&&$this->post['tradNo']!=""? trim($this->post['tradNo']):"";
- $companyNo =isset($this->post['companyNo'])&&$this->post['companyNo']!=""? trim($this->post['companyNo']):"";
- $used_lower =isset($this->post['userd_lower'])&&$this->post['userd_lower']!==""? floor($this->post['userd_lower']) :"0";
- $used_upper =isset($this->post['used_upper'])&&$this->post['used_upper']!==""? floor($this->post['used_upper']):"";
- if($used_lower!=="")$condition[]=["used_fee",">=",$used_lower];
- if($used_upper!=="")$condition[]=["used_fee","<=",$used_upper];
- $total_lower =isset($this->post['total_lower'])&&$this->post['total_lower']!==""? floor($this->post['total_lower']) :"0";
- $total_upper =isset($this->post['total_upper'])&&$this->post['total_upper']!==""? floor($this->post['total_upper']):"";
- if($total_lower!=="")$condition[]=["total_fee",">=",$total_lower];
- if($total_upper!=="")$condition[]=["total_fee","<=",$total_upper];
- if($companyNo!==""){
- $condition[]=["companyNo","=",$companyNo];
- }
- $relaComNo= isset($this->post['relaComNo'])&&$this->post['relaComNo']!="" ? trim($this->post['relaComNo']) :"";
- if($relaComNo!=""){
- $condition[]=["companyNo","=",$relaComNo];
- }
- if($tradNo!==""){
- $condition[]=["tradNo","like","%$tradNo%"];
- }
- if($bank!=""){
- $condition[]=["trade_bank","like","%$bank%"];
- }
- if($name!=""){
- $condition[]=["trade_out","like","%$name%"];
- }
- if($start!=""){
- $condition[]=["trade_time",">=",$start." 00:00:00"];
- }
- if($end!=""){
- $condition[]=["trade_time","<=",$end." 23:59:59"];
- }
- if($status!==""){
- $condition[]=["status","=",$status];
- }
- $count=Db::name("trade")->where($condition)->count();
- $total=ceil($count/$size);
- $page=$page>$total? intval($total):$page;
- $list = Db::name("trade")
- ->where($condition)
- ->page($page, $size)
- ->order(['addtime' => 'desc', 'id' => 'desc'])
- ->select()
- ->toArray();
- foreach ($list as &$value) {
- $data = Db::name("trade_pool")
- ->alias("a")
- ->leftJoin("assoc b", "a.logNo=b.viceCode")
- ->leftJoin("qrd_info c", "b.orderCode=c.sequenceNo")
- ->where(["a.tradNo" => $value['tradNo'], "a.is_del" => 0])
- ->order(['a.addtime' => 'desc', 'a.id' => 'desc'])
- ->field("a.*,b.orderCode,c.cxCode,c.qrdSource,goodNo,goodName,qrdType,c.ownerName,c.ownerid,c.department,c.poCode,c.customerName,c.customerNo")
- ->select()
- ->toArray();
- $value['child'] = $data;
- $value['companyName'] = Db::name("supplier_info")->where(["code" => $value['companyNo']])->value("name", "");
- }
- return app_show(0,"获取成功",["list"=>$list,"count"=>$count]);
- }
- // 认领资金列表明细
- public function tradeList(){
- $page=isset($this->post['page'])&&$this->post['page']!="" ? intval($this->post['page']) :1;
- $size=isset($this->post['size'])&&$this->post['size']!="" ? intval($this->post['size']) :15;
- $condition =[["a.is_del","=",0],["b.is_del","=",0],["c.is_del","=",0],["d.is_del","=",0]];
- $name =isset($this->post['name'])&&$this->post['name']!=""? trim($this->post['name']):"";
- $bank =isset($this->post['bank'])&&$this->post['bank']!=""? trim($this->post['bank']):"";
- $start =isset($this->post['start'])&&$this->post['start']!=""? trim($this->post['start']):"";
- $end =isset($this->post['end'])&&$this->post['end']!=""? trim($this->post['end']):"";
- $status =isset($this->post['status'])&&$this->post['status']!==""? intval($this->post['status']):"";
- $apply_id =isset($this->post['apply_id'])&&$this->post['apply_id']!=""? intval($this->post['apply_id']):"";
- $apply_name =isset($this->post['apply_name'])&&$this->post['apply_name']!=""? trim($this->post['apply_name']):"";
- $tradNo =isset($this->post['tradNo'])&&$this->post['tradNo']!=""? trim($this->post['tradNo']):"";
- $orderCode =isset($this->post['orderCode'])&&$this->post['orderCode']!=''?trim($this->post['orderCode']):"";
- $companyNo =isset($this->post['companyNo'])&&$this->post['companyNo']!=""? trim($this->post['companyNo']):"";
- if($companyNo!==""){
- $condition[]=["a.companyNo","=",$companyNo];
- }
- $relaComNo= isset($this->post['relaComNo'])&&$this->post['relaComNo']!="" ? trim($this->post['relaComNo']) :"";
- if($relaComNo!=""){
- $condition[]=["a.companyNo","=",$relaComNo];
- }
- if($tradNo!==""){
- $condition[]=["a.tradNo","like","%$tradNo%"];
- }
- if($orderCode!==""){
- $condition[]=["d.orderCode","like","%$orderCode%"];
- }
- $cxCode= isset($this->post['cxCode'])&&$this->post['cxCode']!="" ? trim($this->post['cxCode']) :"";
- if($cxCode!=""){
- $condition[]=["c.cxCode","like","%$cxCode%"];
- }
- $logNo =isset($this->post['logNo'])&&$this->post['logNo']!=""? trim($this->post['logNo']):"";
- if($logNo!==""){
- $condition[]=["a.logNo","like","%$logNo%"];
- }
- if($bank!=""){
- $condition[]=["b.trade_bank","like","%$bank%"];
- }
- if($name!=""){
- $condition[]=["b.trade_out","like","%$name%"];
- }
- if($start!=""){
- $condition[]=["a.addtime",">=",date("Y-m-d 00:00:00",strtotime($start))];
- }
- if($end!=""){
- $condition[]=["a.addtime","<=",date("Y-m-d 23:59:59",strtotime($start))];
- }
- if($status!==""){
- $condition[]=["a.status","=",$status];
- }
- if($apply_id!==""){
- $condition[]=["a.apply_id","=",$apply_id];
- }
- if($apply_name!==""){
- $condition[]=["a.apply_name","like","%$apply_name%"];
- }
- $count=Db::name("trade_pool")->alias("a")
- ->leftJoin("trade b","a.tradNo=b.tradNo")
- ->leftJoin("assoc d","a.logNo=d.viceCode AND d.type = 2")
- ->leftJoin("qrd_info c","d.orderCode=c.sequenceNo")
- ->where($condition)->count();
- $total=ceil($count/$size);
- $page=$page>$total? intval($total):$page;
- $list =Db::name("trade_pool")->alias("a")
- ->leftJoin("trade b","a.tradNo=b.tradNo")
- ->leftJoin("assoc d","a.logNo=d.viceCode AND d.type = 2")
- ->leftJoin("qrd_info c","d.orderCode=c.sequenceNo")
- ->where($condition)
- ->page($page,$size)
- ->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,
- 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")
- ->order("a.addtime desc")->select();
- return app_show(0,"获取成功",["list"=>$list,"count"=>$count]);
- }
- // 获取资金下面的认领信息
- public function tradeQuery(){
- $tradNo =isset($this->post['tradNo'])&&$this->post['tradNo']!=""? trim($this->post['tradNo']):"";
- $status =isset($this->post['status'])&&$this->post['status']!=""? intval($this->post['status']):"";
- $apply_id =isset($this->post['apply_id'])&&$this->post['apply_id']!=""? intval($this->post['apply_id']):"";
- $apply_name =isset($this->post['apply_name'])&&$this->post['apply_name']!=""? trim($this->post['apply_name']):"";
- $companyNo =isset($this->post['companyNo'])&&$this->post['companyNo']!=""? trim($this->post['companyNo']):"";
- if($companyNo!==""){
- $condition[]=["companyNo","=",$companyNo];
- }
- $relaComNo= isset($post['relaComNo'])&&$post['relaComNo']!="" ? trim($post['relaComNo']) :"";
- if($relaComNo!=""){
- $condition[]=["companyNo","=",$relaComNo];
- }
- $condition=[["is_del","=",0]];
- if($apply_id!==""){
- $condition[]=["apply_id","=",$apply_id];
- }
- if($apply_name!==""){
- $condition[]=["apply_name","like","%$apply_name%"];
- }
- if($status!==""){
- $condition[]=["status","=",$status];
- }
- if($tradNo!==""){
- $condition[]=["tradNo","like","%$tradNo%"];
- }
- $list =Db::name("trade_pool")->where($condition)->order("addtime desc")->select();
- return app_show(0,"获取成功",$list);
- }
- //资金详情
- public function tradeInfo(){
- $tradNo =isset($this->post['tradNo'])&&$this->post['tradNo']!=""? trim($this->post['tradNo']):"";
- if($tradNo==""){
- return error_show(1004,"参数 tradNo 不能为空");
- }
- $tradinfo = Db::name("trade")->where(["tradNo"=>$tradNo,"is_del"=>0])->find();
- if($tradinfo==false){
- return error_show(1004,"资金信息未找到");
- }
- return app_show(0,"获取成功",$tradinfo);
- }
- //资金认领详情
- public function logInfo(){
- $logNo =isset($this->post['logNo'])&&$this->post['logNo']!=""? trim($this->post['logNo']):"";
- if($logNo==""){
- return error_show(1004,"参数 logNo 不能为空");
- }
- $tradinfo = Db::name("trade_pool")->where(["logNo"=>$logNo,"is_del"=>0])->find();
- if($tradinfo==false){
- return error_show(1004,"资金信息未找到");
- }
- $trade = Db::name("trade")->where(["tradNo"=>$tradinfo['tradNo'],"is_del"=>0])->find();
- $tradinfo['trade_out']= $trade['trade_out']??"";
- $tradinfo['trade_in']= $trade['trade_in']??"";
- $tradinfo['trade_bank']= $trade['trade_bank']??"";
- $tradinfo['customerName']= Db::name("customer_info")->where(["companyNo"=>$tradinfo['customerNo']])->value("companyName","");
- $tradinfo['companyNo']= $trade['companyNo']??"";
- $tradinfo['total_fee']= $trade['total_fee']??"";
- $tradinfo['balance']= $trade['balance']??"";
- $tradinfo['used_fee']= $trade['used_fee']??"";
- $orderinfo = Db::name("assoc")->alias("a")->leftJoin("qrd_info c","a.orderCode=c.sequenceNo")
- ->where(["a.viceCode"=>$logNo,"a.is_del"=>0,"a.status"=>[1,2,3]])
- ->order("a.addtime desc")
- ->field("c.*,a.cancel_fee")
- ->findOrEmpty();
- $tradinfo['orderinfo']=$orderinfo;
- return app_show(0,"获取成功",$tradinfo);
- }
- //认领资金退回或退款 type 1 退款 2 解除资金认领
- public function ReturnPay(){
- $logNo = isset($this->post['logNo'])&&$this->post['logNo']!="" ? trim($this->post['logNo']):"";
- if($logNo==''){
- return error_show(1004,"参数 logNo 不能为空");
- }
- $type = isset($this->post['type'])&&$this->post['type']!="" ? intval($this->post['type']):"";
- if($type==""){
- return error_show(1004,"参数 type 不能为空");
- }
- $loginfo = Db::name("trade_pool")->where(["logNo"=>$logNo,"is_del"=>0])->find();
- if($loginfo==false){
- return error_show(1004,"认领资金信息未找到");
- }
- if($loginfo['status']==2 && $type==1)return error_show(1004,"认领资金信息审核已通过");
- if($loginfo['status']!=2 && $type==2)return error_show(1004,"认领资金信息未审核通过");
- $tradinfo = Db::name("trade")->where(["tradNo"=>$loginfo['tradNo'],"is_del"=>0])->find();
- if($tradinfo==false){
- return error_show(1004,"资金信息未找到");
- }
- $isT= Db::name("trade_return")->where(["logNo"=>$logNo,"tradNo"=>$loginfo['tradNo'],"status"=>[0,1]])
- ->findOrEmpty();
- if(!empty($isT)) return error_show(1004,"资金退回流程已存在");
- $reason = isset($this->post['return_reason'])&&$this->post['return_reason']!="" ? trim($this->post['return_reason']):"";
- $remark = isset($this->post['remark'])&&$this->post['remark']!="" ? trim($this->post['remark']):"";
- $returnCode =makeNo("RTA");
- $data=[
- "returnCode"=>$returnCode,
- "logNo"=>$logNo,
- "companyNo"=>$tradinfo['companyNo'],
- "tradNo"=>$loginfo['tradNo'],
- "return_img"=>'',
- "type"=>$type,
- "apply_id"=>$this->uid,
- "apply_name"=>$this->uname,
- "return_reason"=>$reason,
- "remark"=>$remark,
- "addtime"=>date("Y-m-d H:i:s"),
- "updatetime"=>date("Y-m-d H:i:s")
- ];
- $in = Db::name("trade_return")->insert($data);
- if($in){
- return app_show(0,"申请新建成功");
- }else{
- return error_show(1004,"申请新建失败");
- }
- }
- /**退款列表
- * //page size tradNo logNo apply_id apply_name type status returnCode
- * @return \think\response\Json|void
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function returnList(){
- $page=isset($this->post['page'])&&$this->post['page']!='' ? intval($this->post['page']):1;
- $size=isset($this->post['size'])&&$this->post['size']!='' ? intval($this->post['size']):15;
- $tradNo =isset($this->post['tradNo'])&&$this->post['tradNo']!=''?trim($this->post['tradNo']):"";
- $logNo =isset($this->post['logNo'])&&$this->post['logNo']!=''?trim($this->post['logNo']):"";
- $apply_id =isset($this->post['apply_id'])&&$this->post['apply_id']!=''?intval($this->post['apply_id']):"";
- $apply_name =isset($this->post['apply_name'])&&$this->post['apply_name']!=''?trim($this->post['apply_name']):"";
- $type =isset($this->post['type'])&&$this->post['type']!=''?intval($this->post['type']):"";
- $status =isset($this->post['status'])&&$this->post['status']!==''?intval($this->post['status']):"";
- $returnCode =isset($this->post['returnCode'])&&$this->post['returnCode']!=''?trim($this->post['returnCode']):"";
- $orderCode =isset($this->post['orderCode'])&&$this->post['orderCode']!=''?trim($this->post['orderCode']):"";
- $companyNo =isset($this->post['companyNo'])&&$this->post['companyNo']!=""? trim($this->post['companyNo']):"";
- if($companyNo!==""){
- $condition[]=["a.companyNo","=",$companyNo];
- }
- $relaComNo= isset($this->post['relaComNo'])&&$this->post['relaComNo']!="" ? trim($this->post['relaComNo']) :"";
- if($relaComNo!=""){
- $condition[]=["a.companyNo","=",$relaComNo];
- }
- $condition=[["a.is_del","=",0]];
- if($tradNo!=''){
- $condition[]=["a.tradNo","like","%$tradNo%"];
- }
- if($orderCode!=''){
- $condition[]=["b.orderCode","like","%$orderCode%"];
- }
- if($logNo!=''){
- $condition[]=["a.logNo","like","%$logNo%"];
- }
- if($apply_id!=''){
- $condition[]=["a.apply_id","=",$apply_id];
- }
- if($apply_name!=''){
- $condition[]=["a.apply_name","like","%$apply_name%"];
- }
- if($returnCode!=''){
- $condition[]=["a.returnCode","like","%$returnCode%"];
- }
- if($type!=''){
- $condition[]=["a.type","=",$type];
- }
- if($status!==''){
- $condition[]=["a.status","=",$status];
- }
- $count =Db::name("trade_return")->alias("a")
- ->leftJoin("assoc b","a.logNo=b.viceCode")
- ->leftJoin("qrd_info c","c.sequenceNo=b.orderCode")
- ->where($condition)->count();
- $total=ceil($count/$size);
- $page = $page>$total ? intval($total):$page;
- $list=Db::name("trade_return")->alias("a")->leftJoin("assoc b","a.logNo=b.viceCode")
- ->leftJoin("qrd_info c","c.sequenceNo=b.orderCode")
- ->field("a.*,b.orderCode,c.qrdSource,goodNo,goodName,qrdType,c.ownerName,c.ownerid,c.department,c.poCode,c.platName,b.cancel_fee,c.customerName")
- ->where($condition)->page($page,$size)->order("a.addtime desc")->select();
- return app_show(0,"获取成功",["list"=>$list,"count"=>$count]);
- }
- //退款状态审核 0 待审核 1 财务审核 2 财务驳回
- public function returnStatus(){
- $returnCode=isset($this->post['returnCode'])&&$this->post['returnCode']!=''?trim($this->post['returnCode']):"";
- if($returnCode==''){
- return error_show(1004,"参数 returnCode 不能为空");
- }
- $returninfo =Db::name("trade_return")->where(["returnCode"=>$returnCode,"is_del"=>0])->find();
- if($returninfo==false){
- return error_show(1004,"退款申请数据未找到");
- }
- $status=isset($this->post['status']) &&$this->post['status']!=''?intval($this->post['status']) : '';
- if($status==''){
- return error_show(1004,"参数 status 不能为空");
- }
- $return_img = isset($this->post['return_img'])&&$this->post['return_img']!=''?trim($this->post['return_img']):"";
- $remark = isset($this->post['remark'])&&$this->post['remark']!=''?trim($this->post['remark']):"";
- $loginfo = Db::name("trade_pool")->where(["logNo"=>$returninfo['logNo'],"is_del"=>0])->find();
- if($loginfo==false){
- return error_show(1004,"认领资金信息未找到");
- }
- if($loginfo['status']!=2)return error_show(1004,"认领资金信息审核未通过");
- Db::startTrans();
- try{
- $update=["status"=>$status,"return_img"=>$return_img,"remark"=>$remark,"updatetime"=>date("Y-m-d H:i:s")];
- $up =Db::name("trade_return")->where($returninfo)->update($update);
- if($up){
- if($status==1){
- $qrdArr=Db::name("assoc")->where(["viceCode"=>$returninfo['logNo'],"status"=>[1,2],"is_del"=>0])
- ->select()->toArray();
- if(!empty($qrdArr)){
- foreach ($qrdArr as $value){
- $qrd =Db::name("qrd_info")->where(["sequenceNo"=>$value['orderCode']])->field("id,status,pay_fee,apay_fee,wpay_fee,pay_status,totalPrice")->findOrEmpty();
- if(empty($qrd)){
- Db::rollback();
- return error_show(1005,"未找到销售单数据");
- }
- if($returninfo['type']==2){
- if($qrd['apay_fee']<$value['cancel_fee']){
- Db::rollback();
- return error_show(1005,"销售单已付金额不足");
- }
- $update =[
- "wpay_fee"=>$qrd['wpay_fee']+$value['cancel_fee'],
- "apay_fee"=>$qrd['apay_fee']-$value['cancel_fee'],
- "pay_status"=>($qrd['apay_fee']-$value['cancel_fee'])==0 && $qrd['pay_fee']==0 ? 1:2,
- "status"=>$qrd['pay_fee']==0 && ($qrd['apay_fee']-$value['cancel_fee'])==0 ?0:1,
- "updatetime"=>date("Y-m-d H:i:s")
- ];
- }else{
- if($qrd['pay_fee']<$value['cancel_fee']){
- Db::rollback();
- return error_show(1005,"销售单付款中金额不足");
- }
- $update =[
- "wpay_fee"=>$qrd['wpay_fee']+$value['cancel_fee'],
- "pay_fee"=>$qrd['pay_fee']-$value['cancel_fee'],
- "pay_status"=>($qrd['pay_fee']-$value['cancel_fee'])==0 && $qrd['apay_fee']==0 ? 1:2,
- "status"=>$qrd['apay_fee']==0 && ($qrd['pay_fee']-$value['cancel_fee'])==0 ?0:1,
- "updatetime"=>date("Y-m-d H:i:s")
- ];
- }
- $qrdup =Db::name("qrd_info")->where($qrd)->update($update);
- if($qrdup==false){
- Db::rollback();
- return error_show(1005,"销售单更新失败");
- }
- $asscup =[
- "status"=>3,
- "updatetime"=>date("Y-m-d H:i:s")
- ];
- $assc=Db::name("assoc")->where($value)->update($asscup);
- if($assc==false){
- Db::rollback();
- return error_show(1005,"销售单更新失败");
- }
- $report=ReportCode::where(["qrdNo"=>$value['orderCode']])->find();
- if($report)$report->setField("returnTrad",$returnCode);
- }
- }
- $logup =["status"=>$returninfo['type']==1?5:4,"updatetime"=>date("Y-m-d H:i:s")];
- $upde =Db::name("trade_pool")->where($loginfo)->update($logup);
- if($upde==false){
- Db::rollback();
- return error_show(1005,"资金认领信息更新失败");
- }
- // if($returninfo['type']==1){
- $trade =Db::name("trade")->where(["tradNo"=>$returninfo['tradNo'],"is_del"=>0])->find();
- if($trade==false){
- Db::rollback();
- return error_show(1005,"资金信息未找到");
- }
- if($trade['used_fee']<$loginfo['total_fee']){
- Db::rollback();
- return error_show(1005,"资金信息已认领金额不足");
- }
- $tradup =[
- "used_fee"=>$trade['used_fee']-$loginfo['total_fee'],
- "balance"=>$trade['balance']+$loginfo['total_fee'],
- "status"=>($trade['used_fee']-$loginfo['total_fee'])==0 ?1 :2,
- "updatetime"=>date("Y-m-d H:i:s")
- ];
- $updaT=Db::name("trade")->where($trade)->update($tradup);
- if($updaT==false){
- Db::rollback();
- return error_show(1005,"资金信息更新失败");
- }
- // }
- }
- Db::commit();
- return app_show(0,"退款申请审核成功");
- }
- Db::rollback();
- return error_show(1004,'审核状态失败');
- }catch (\Exception $e){
- Db::rollback();
- return error_show(1004,$e->getMessage());
- }
- }
- //退款申请详情
- public function returnInfo(){
- $returnCode=isset($this->post['returnCode'])&&$this->post['returnCode']!=''?trim($this->post['returnCode']):"";
- if($returnCode==''){
- return error_show(1004,"参数 returnCode 不能为空");
- }
- $returninfo =Db::name("trade_return")->where(["returnCode"=>$returnCode,"is_del"=>0])->find();
- if($returninfo==false){
- return error_show(1004,"退款申请数据未找到");
- }
- $trade = Db::name("trade")->where(["tradNo"=>$returninfo['tradNo'],"is_del"=>0])->find();
- $returninfo['trade_out']= $trade['trade_out']??"";
- $returninfo['trade_in']= $trade['trade_in']??"";
- $returninfo['trade_bank']= $trade['trade_bank']??"";
- $returninfo['trade_time']= $trade['trade_time']??"";
- $returninfo['customerNo']= $trade['customerNo']??"";
- $returninfo['companyNo']= $trade['companyNo']??"";
- $returninfo['total_fee']= $trade['total_fee']??"";
- $returninfo['balance']= $trade['balance']??"";
- $returninfo['used_fee']= $trade['used_fee']??"";
- $pool =Db::name("trade_pool")->where(["logNo"=>$returninfo['logNo'],"is_del"=>0])->find();
- $returninfo['log_total_fee']= $pool['total_fee']??"";
- $returninfo['log_apply_id']= $pool['apply_id']??"";
- $returninfo['log_apply_name']= $pool['apply_name']??"";
- $orderinfo = Db::name("assoc")->alias("a")->leftJoin("qrd_info c","a.orderCode=c.sequenceNo")
- ->where(["a.viceCode"=>$returninfo['logNo'],"a.is_del"=>0,"a.status"=>[1,2]])
- ->order("a.addtime desc")
- ->field("c.*")
- ->findOrEmpty();
- $returninfo['orderinfo'] = $orderinfo;
- return app_show(0,"获取成功",$returninfo);
- }
- /**
- * 显示创建资源表单页.
- *
- * @return \think\Response
- */
- public function importTrade()
- {
- $files = $this->request->file("excel");
- $data = upload($files,$files->getOriginalExtension());
- if($data['code']!=0){
- return error_show(1004,$data['msg']);
- }
- $trade = $data['data'];
- $list = [];
- foreach ( $trade as $key => $value) {
- if (!isset($value[0]) || $value[0] == '') {
- //return ['code' => 1003, "msg" => '交易时间不能为空'];
- return error_show( 1003, '第'.($key+1).'行收款方公司编号不能为空!');
- }
- if (!isset($value[4]) || $value[4] == '') {
- // return ['code' => 1003, "msg" => '交易行名不能为空'];
- return error_show( 1003, '第'.($key+1).'行收入金额不能为空!');
- }
- if (!isset($value[8]) || $value[8] == '') {
- // return ['code' => 1003, "msg" => '对方账户不能为空'];
- return error_show( 1003, '第'.($key+1).'行对方账户不能为空!');
- }
- if (!isset($value[9]) || $value[9] == '') {
- // return ['code' => 1003, "msg" => '对方户名不能为空'];
- return error_show( 1003, '第'.($key+1).'行对方户名不能为空!');
- }
- if ($key == 0) {
- if ($value[0] != '收款方公司编号') {
- // return ['code' => 1003, "msg" => '模板第一列为必须为交易时间!'];
- return error_show( 1003, '模板第一列为必须为收款方公司编号!');
- }
- if ($value[4] != '收入金额') {
- // return ['code' => 1003, "msg" => '模板第二列为必须为收入金额!'];
- return error_show( 1003, '模板第五列为必须为收入金额!');
- }
- if ($value[6] != '交易行名') {
- //return ['code' => 1003, "msg" => '模板第五列为必须为交易行名!'];
- return error_show( 1003, '模板第七列为必须为交易行名!');
- }
- if ($value[8] != '对方账号') {
- // return ['code' => 1003, "msg" => '模板第七列为必须为对方账号!'];
- return error_show( 1003, '模板第九列为必须为对方账号!');
- }
- if ($value[9] != '对方户名') {
- // return ['code' => 1003, "msg" => '模板第八列为必须为对方户名!'];
- return error_show( 1003, '模板第十列为必须为对方户名!');
- }
- continue;
- }
- $company =Db::name("company_info")->where(["companyNo"=>$value[0]])->findOrEmpty();
- if(empty($company)){
- return error_show( 1003, "业务公司编号不存在{$value[0]}");
- }
- $total_fee = $value[4];
- $type = 0;
- $time = explode(" ", $value[3]);
- $list[$key]["tradeTime"] = date("Y-m-d", strtotime($time[0])) . " " . (isset($time[1]) ? $time[1] : "00:00:00");
- $list[$key]["trade_fee"] = str_replace(",","",$total_fee);
- $list[$key]["trade_bank"] = $value[6];
- $list[$key]["trade_account"] = $value[8];
- $list[$key]["trade_out"] = $value[9];
- $list[$key]["trade_in"] = $value[1]??$company['company_name'];
- $list[$key]["trade_type"] = $type;
- $list[$key]["trade_used"] = isset($value[10]) ? $value[10] : "";
- $list[$key]["trade_remark"] = '';
- $list[$key]["companyNo"] = $value[0];
- $list[$key]["trade_in_account"] = $value[2]??"";
- }
- if(empty($list)){
- return error_show( 1003, '导入数据不能为空!');
- }
- Db::startTrans();
- try{
- $tra=[];
- foreach ($list as $value) {
- $temp = [];
- $temp["tradNo"] = makeStr('S');
- $temp['trade_time'] = $value['tradeTime'];
- $temp['total_fee'] = $value['trade_fee'];
- $temp['trade_bank'] = $value['trade_bank'];
- $temp['trade_account'] = $value['trade_account'];
- $temp['trade_type'] =$value['trade_type'];
- $temp['trade_out'] = $value['trade_out'];
- $temp['trade_in'] = $value['trade_in'];
- $temp['trade_in_account'] = $value['trade_in_account'];
- $temp['companyNo'] = $value['companyNo'];
- $temp['trade_used'] = $value['trade_used'];
- $temp['trade_remark'] = $value['trade_remark'];
- $temp['balance'] =$value['trade_fee'];
- $temp['addtime'] = date("Y-m-d H:i:s");
- $temp['updatetime'] = date("Y-m-d H:i:s");
- $tra[]=$temp;
- }
- $list = Db::name('trade')->insertAll($tra);
- if($list==count($tra)){
- Db::commit();
- return app_show(0, "资金导入成功");
- }else{
- Db::rollback();
- return app_show(1004, "资金导入失败");
- }
- }catch (\Exception $e){
- Db::rollback();
- return app_show(1004, $e->getMessage());
- }
- }
- /**
- * 资金导入
- * @return \think\response\Json|void
- */
- public function importTradeByArr()
- {
- $trade =isset($this->post['data']) &&!empty($this->post['data'])? $this->post['data']:[];
- if(!is_array($trade) || empty($trade) ) return error_show( 1003,"参数 data 不能为空");
- $list = [];
- foreach ( $trade as $key => $value) {
- if (!isset($value[0]) || $value[0] == '') {
- //return ['code' => 1003, "msg" => '交易时间不能为空'];
- return error_show( 1003, '第'.($key+1).'行收款方公司编号不能为空!');
- }
- if (!isset($value[2]) || $value[2] == '') {
- //return ['code' => 1003, "msg" => '交易时间不能为空'];
- return error_show( 1003, '第'.($key+1).'行收款账户不能为空!');
- }
- if (!isset($value[3]) || $value[3] == '') {
- //return ['code' => 1003, "msg" => '交易时间不能为空'];
- return error_show( 1003, '第'.($key+1).'行交易时间不能为空!');
- }
- if (!isset($value[4]) || $value[4] == '') {
- // return ['code' => 1003, "msg" => '交易行名不能为空'];
- return error_show( 1003, '第'.($key+1).'行收入金额不能为空!');
- }
- if (!isset($value[6]) || $value[6] == '') {
- // return ['code' => 1003, "msg" => '交易行名不能为空'];
- return error_show( 1003, '第'.($key+1).'行交易银行名称不能为空!');
- }
- if (!isset($value[8]) || $value[8] == '') {
- // return ['code' => 1003, "msg" => '对方账户不能为空'];
- return error_show( 1003, '第'.($key+1).'行对方账户不能为空!');
- }
- if (!isset($value[9]) || $value[9] == '') {
- // return ['code' => 1003, "msg" => '对方户名不能为空'];
- return error_show( 1003, '第'.($key+1).'行对方户名不能为空!');
- }
- // if ($key == 0) {
- // if ($value[0] != '收款方公司编号') {
- // // return ['code' => 1003, "msg" => '模板第一列为必须为交易时间!'];
- // return error_show( 1003, '模板第一列为必须为收款方公司编号!');
- // }
- // if ($value[4] != '收入金额') {
- // // return ['code' => 1003, "msg" => '模板第二列为必须为收入金额!'];
- // return error_show( 1003, '模板第五列为必须为收入金额!');
- // }
- //
- // if ($value[6] != '交易行名') {
- // //return ['code' => 1003, "msg" => '模板第五列为必须为交易行名!'];
- // return error_show( 1003, '模板第七列为必须为交易行名!');
- // }
- // if ($value[8] != '对方账号') {
- // // return ['code' => 1003, "msg" => '模板第七列为必须为对方账号!'];
- // return error_show( 1003, '模板第九列为必须为对方账号!');
- // }
- // if ($value[9] != '对方户名') {
- // // return ['code' => 1003, "msg" => '模板第八列为必须为对方户名!'];
- // return error_show( 1003, '模板第十列为必须为对方户名!');
- // }
- // continue;
- // }
- $company =Db::name("company_info")->where(["companyNo"=>$value[0]])->findOrEmpty();
- if(empty($company)){
- return error_show( 1003, "业务公司编号不存在{$value[0]}");
- }
- $total_fee = $value[4];
- $type = 0;
- $time = explode(" ", $value[3]);
- $list[$key]["tradeTime"] = date("Y-m-d", strtotime($time[0])) . " " . (isset($time[1]) ? $time[1] : "00:00:00");
- $list[$key]["trade_fee"] = str_replace(",","",$total_fee);
- $list[$key]["trade_bank"] = $value[6];
- $list[$key]["trade_account"] = $value[8];
- $list[$key]["trade_out"] = $value[9];
- $list[$key]["trade_in"] = $value[1]??$company['company_name'];
- $list[$key]["trade_type"] = $type;
- $list[$key]["trade_used"] = isset($value[10]) ? $value[10] : "";
- $list[$key]["trade_remark"] = '';
- $list[$key]["companyNo"] = $value[0];
- $list[$key]["trade_in_account"] = $value[2]??"";
- }
- if(empty($list)){
- return error_show( 1003, '导入数据不能为空!');
- }
- Db::startTrans();
- try{
- $tra=[];
- $i=0;
- foreach ($list as $value) {
- $temp = [];
- $i++;
- $temp["tradNo"] = makeStr('S').str_pad($i,3,'0',STR_PAD_LEFT);
- $temp['trade_time'] = $value['tradeTime'];
- $temp['total_fee'] = $value['trade_fee'];
- $temp['trade_bank'] = $value['trade_bank'];
- $temp['trade_account'] = $value['trade_account'];
- $temp['trade_type'] =$value['trade_type'];
- $temp['trade_out'] = $value['trade_out'];
- $temp['trade_in'] = $value['trade_in'];
- $temp['trade_in_account'] = $value['trade_in_account'];
- $temp['companyNo'] = $value['companyNo'];
- $temp['trade_used'] = $value['trade_used'];
- $temp['trade_remark'] = $value['trade_remark'];
- $temp['balance'] =$value['trade_fee'];
- $temp['addtime'] = date("Y-m-d H:i:s");
- $temp['updatetime'] = date("Y-m-d H:i:s");
- $tra[]=$temp;
- }
- $list = Db::name('trade')->insertAll($tra);
- if($list==count($tra)){
- Db::commit();
- return app_show(0, "资金导入成功");
- }else{
- Db::rollback();
- return app_show(1004, "资金导入失败");
- }
- }catch (\Exception $e){
- Db::rollback();
- return app_show(1004, $e->getMessage());
- }
- }
- //批量认领资金,跟订单相关
- public function importTradeByBatchOrderCode()
- {
- $list = $this->request->post('list', [], 'trim');
- $val = Validate::rule(['list|导入数据' => 'require|array|max:100']);
- if (!$val->check(['list' => $list])) return error_show(1004, $val->getError());
- $company = $tradNos = $orderCodes = $assoc_insert_data = $OrderCodeToAssocNo = $OrderCodeToLogNo = [];
- $val_item = Validate::rule([
- 'companyNo|卖出方公司编号' => 'require|max:255',
- 'tradNo|资金编号' => 'require|max:255',
- 'orderCode|订单编号' => 'require|max:255',
- 'trad_fee|认领资金' => 'require|float|gt:0|max:999999999.99',
- ]);
- foreach ($list as $key => $value) {
- if (!$val_item->check($value)) return error_show(1004, $val_item->getError());
- if (!isset($company[$value['companyNo']])) $company[$value['companyNo']] = $value['companyNo'];
- }
- //判断绑定公司
- $companyNo = Db::name('user_role')
- ->where(['is_del' => 0, 'status' => 1, 'uid' => $this->uid])
- ->whereIn('companyNo', $company)
- ->column('companyNo');
- $tmp = array_diff($company, $companyNo);//二者取差集
- if(!empty($tmp)) return error_show(1004,'不能操作以下公司数据:'.implode(',',$tmp));
- Db::startTrans();
- try {
- $i=1;
- //批量资金认领
- foreach ($list as $key=>$value){
- //资金明细判断
- $i++;
- $all_trade = Db::name('trade')
- ->where('is_del', 0)
- ->whereIn('status', [1, 2])
- ->where('tradNo', $value['tradNo'])
- ->field('id,balance,used_fee,companyNo,trade_time')
- ->findOrEmpty();
- if(empty($all_trade))throw new Exception("{$value['tradNo']}资金未找到数据");
- if($all_trade['balance']<$value['trad_fee'])throw new Exception("{$value['tradNo']}资金余额不足");
- //订单明细判断
- $all_order=Db::name('qrd_info')
- ->where('is_del', 0)
- ->whereIn('sequenceNo',$value['orderCode'])
- ->field('id,customerNo,status,pay_fee,apay_fee,wpay_fee,pay_status,totalPrice')
- ->findOrEmpty();
- if(empty($all_order))throw new Exception("{$value['orderCode']}销售单未找到数据");
- if($all_order['wpay_fee']<$value['trad_fee'])throw new Exception("{$value['orderCode']}销售单未付款金额不足核销金额");
- $OrderCodeToAssocNo = substr(makeNo('AS'), 0, -3).str_pad($i,3,'0',STR_PAD_LEFT);
- $OrderCodeToLogNo = substr(makeNo('TRC'), 0, -3).str_pad($i,3,'0',STR_PAD_LEFT);
- //资金数据更新处理
- $balance = bcsub($all_trade['balance'], $value['trad_fee'], 2);
- $tradeUp=Db::name('trade')
- ->where($all_trade)
- ->update([
- 'balance' => $balance,
- 'used_fee' => bcadd($all_trade['used_fee'], $value['trad_fee'], 2),
- 'status' => $balance == 0 ? 3 : 2,
- 'updatetime' =>date("Y-m-d H:i:s"),
- ]);
- if($tradeUp==false ) throw new Exception("{$value['tradNo']}资金更新失败");
- //订单数据更新处理
- $orderUp=Db::name('qrd_info')
- ->where($all_order)
- ->update([
- 'apay_fee' => bcadd($all_order['apay_fee'], $value['trad_fee'], 2),
- 'wpay_fee' => bcsub($all_order['wpay_fee'], $value['trad_fee'], 2),
- 'pay_status' => bcsub($all_order['wpay_fee'], $value['trad_fee'], 2)==0 &&$all_order['pay_fee']==0? 3:2,
- 'status' => 1,
- 'updatetime' => date("Y-m-d H:i:s")
- ]);
- if($orderUp==false ) throw new Exception("{$value['orderCode']}销售单更新失败");
- //资金订单关联数据
- $assoc_insert_data[]=[
- 'assocNo' => $OrderCodeToAssocNo,
- 'apply_id' => $this->uid,
- 'apply_name' => $this->uname,
- 'type' => 2,
- 'orderCode' => $value['orderCode'],
- 'customerNo' => $all_order['customerNo'],
- 'viceCode' => $OrderCodeToLogNo,
- 'order_total' => $all_order['totalPrice'],
- 'vice_total' => $value['trad_fee'],
- 'cancel_fee' => $value['trad_fee'],
- 'status' => 2,//认领通过
- 'addtime' => date("Y-m-d H:i:s"),
- 'updatetime' => date("Y-m-d H:i:s"),
- ];
- //资金认领数据
- $trade_pool_insert_data[]=[
- 'logNo' => $OrderCodeToLogNo,
- 'tradNo' => $value['tradNo'],
- 'companyNo' => $value['companyNo'],
- 'customerNo' => $all_order['customerNo'],
- 'apply_id' => $this->uid,
- 'apply_name' => $this->uname,
- 'trade_time' => $all_trade['trade_time'],
- 'total_fee' => $value['trad_fee'],
- 'status' => 2,//2审核通过
- 'addtime' => date("Y-m-d H:i:s"),
- 'updatetime' => date("Y-m-d H:i:s"),
- ];
- }
- if ($assoc_insert_data) Db::name('assoc')->insertAll($assoc_insert_data);
- if ($trade_pool_insert_data) Db::name('trade_pool')->insertAll($trade_pool_insert_data);
- Db::commit();
- return app_show(0, '批量认领资金成功');
- } catch (Exception $exception) {
- Db::rollback();
- return error_show(1004, '批量认领资金失败,' . $exception->getMessage());
- }
- }
- }
|