123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675 |
- <?php
- namespace app\cxinv\controller;
- use app\cxinv\model\Assoc;
- use app\cxinv\model\ComonOrder;
- use app\cxinv\model\QrdInfo;
- use app\cxinv\model\TradeLib;
- use app\cxinv\model\TradePool;
- use app\cxinv\model\TradeReturn;
- use app\user\model\Business;
- use think\App;
- use think\facade\Validate;
- use think\helper\Str;
- class Trade extends Base{
- public function __construct(App $app) {
- parent::__construct($app);
- $this->model = new \app\cxinv\model\Trade();
- }
- public function PoolCreate(){
- $params = $this->request->param(["tradNo"=>"","orderArr"=>[]],"post","trim");
- $valid =Validate::rule(["tradNo|交易流水编号"=>"require|max:255","orderArr|关联订单信息"=>"require|array"]);
- if(!$valid->check($params)) return error($valid->getError());
- $trade = $this->model->where(["tradNo"=>$params['tradNo'],"is_del"=>0])->findOrEmpty();
- if($trade->isEmpty()) return error("交易流水信息不存在");
- $orderValid = Validate::rule([
- "sequenceNo|关联订单编号"=>"require|max:255",
- "trad_fee|关联订单金额"=>"require|float|min:0.01"]);
- $assoc=[];
- $log_total = "0";
- $logInfo=[];
- $qrdList = QrdInfo::where('sequenceNo','in',array_column($params['orderArr'],'sequenceNo'))
- ->column("id,sequenceNo,platform_type,wpay_fee,pay_fee,is_comon,pay_status,totalPrice,customerNo","sequenceNo");
- foreach ($params['orderArr'] as $key=>$order){
- if(!$orderValid->check($order)) return error($orderValid->getError());
- if(!isset($qrdList[$order['sequenceNo']])) return error("关联订单信息不存在");
- if($qrdList[$order['sequenceNo']]['wpay_fee']<$order['trad_fee']) return error("销售单{$order['sequenceNo']}未付款金额不足核销金额");
- //if($qrdList[$order['sequenceNo']]['is_comon']==1) return error("{$order['sequenceNo']} 通用订单不可认领资金");
- $qrdList[$order['sequenceNo']]['wpay_fee']=bcsub($qrdList[$order['sequenceNo']]['wpay_fee'],$order['trad_fee'],2) ;
- $log_total=bcadd($log_total,$order['trad_fee'],2);
- $temp=[
- 'logNo'=>makeNo("TRC",str_pad($key+1,4,'0',STR_PAD_LEFT)),
- 'tradNo'=>$params['tradNo'],
- 'platform_type'=>$qrdList[$order['sequenceNo']]['platform_type']??0,
- 'companyNo'=>$trade['companyNo'],
- 'customerNo'=>$qrdList[$order['sequenceNo']]['customerNo'],
- 'apply_id'=>$this->uid,
- 'apply_name'=>$this->uname,
- 'trade_time'=>$trade['trade_time'],
- 'total_fee'=>$order['trad_fee'],
- 'status'=>1,
- ];
- $assoc[]=[
- "assocNo"=>makeNo("AS",str_pad($key+1,4,"0",STR_PAD_LEFT)),
- "apply_id"=>$this->uid,
- "apply_name"=>$this->uname,
- "type"=>2,
- "orderCode"=>$order['sequenceNo'],
- "customerNo"=>$qrdList[$order['sequenceNo']]['customerNo'],
- "viceCode"=>$temp['logNo'],
- "order_total"=>$qrdList[$order['sequenceNo']]['totalPrice'],
- "vice_total"=>$order['trad_fee'],
- "cancel_fee"=>$order['trad_fee'],
- "status"=>1,
- ];
- $logInfo[]=$temp;
- }
- if($trade->balance<$log_total) return error("交易流水余额不足");
- $this->model->startTrans();
- try{
- $trade->balance-=$log_total;
- $trade->used_fee+=$log_total;
- $trade->status= $trade->balance==0?3:2;
- $trup=$trade->save();
- if(!$trup) throw new \Exception("资金流水更新失败");
- $pool = (new \app\cxinv\model\TradePool)->saveAll($logInfo);
- if($pool->isEmpty()) throw new \Exception("资金认领记录失败");
- $assoc_res = (new \app\cxinv\model\Assoc)->saveAll($assoc);
- if($assoc_res->isEmpty()) throw new \Exception("资金关联记录失败");
- foreach ($params['orderArr'] as $key=>$qrd){
- $qrdInfo = QrdInfo::where(["sequenceNo"=>$qrd['sequenceNo'],"is_del"=>0])->find();
- if($qrdInfo->isEmpty()) throw new \Exception("关联订单{$qrd['sequenceNo']}信息不存在");
- if($qrdInfo->wpay_fee<$qrd['trad_fee']) throw new \Exception("关联订单{$qrd['sequenceNo']}未付款金额不足核销金额");
- $qrdInfo->pay_fee+=$qrd['trad_fee'];
- $qrdInfo->wpay_fee-=$qrd['trad_fee'];
- $qrdInfo->pay_status= $qrdInfo->apay_fee==0?1:2;
- $qrdInfo->status=1;
- $sav= $qrdInfo->save();
- if(!$sav) throw new \Exception("关联订单更新失败");
- }
- $this->model->commit();
- }catch (\Exception $e){
- $this->model->rollback();
- return error($e->getMessage());
- }
- return success("资金认领成功");
- }
- // 1待审批2审批通过3审批驳回4退款5解除认领
- public function PoolStatus(){
- $params = $this->request->param(["logNo"=>"","status"=>"","remark"=>""],"post","trim");
- $valid =Validate::rule(["logNo|资金认领编号"=>"require|max:255","status|状态"=>"require|in:1,2,3,5"]);
- if(!$valid->check($params)) return error($valid->getError());
- $log = TradePool::where(["logNo"=>$params['logNo'],"is_del"=>0])->findOrEmpty();
- if($log->isEmpty()) return error("资金认领信息不存在");
- if($log->status==$params['status']) return error("当前状态与修改状态一致");
- if($log->tag_type==1) return error('平台衍生资金认领不允许操作');
- $trade = $this->model->where(["tradNo"=>$log->tradNo,"is_del"=>0])->findOrEmpty();
- if($trade->isEmpty()) return error("资金流水信息不存在");
- $this->model->startTrans();
- try{
- Assoc::CheckTrad($log->logNo,$params['status']);
- if($params['status']==3|| $params['status']==5){
- if($trade->used_fee<$log->total_fee) throw new \Exception("资金流水已使用金额不足");
- $trade->balance+=$log->total_fee;
- $trade->used_fee-=$log->total_fee;
- $trade->status= $trade->used_fee==0?1:2;
- $up=$trade->save();
- if(!$up) throw new \Exception("资金流水更新失败");
- }
- $log->status=$params['status'];
- $log->remark=$params['remark'];
- $save=$log->save();
- if(!$save) throw new \Exception('资金认领状态更新失败');
- $this->model->commit();
- }catch (\Exception $e){
- $this->model->rollback();
- return error($e->getMessage());
- }
- return success("操作成功");
- }
- public function list(){
- $params = $this->request->param(["tradNo"=>"","status"=>[],"name"=>"","bank"=>"","start"=>"","end"=>"","companyNo"=>"",
- 'poCode'=>'','pcCode'=>'',"page"=>1,"size"=>20,'used_lower'=>"",'used_upper'=>'','total_lower'=>'','total_upper'=>'',"relaComNo"=>"",'trade_in_code'=>'',
- "trade_type"=>"","palt_po"=>"","skuCode"=>"","goodName"=>"","plat_total_lower"=>"",'plat_total_upper'=>'',"com_total_lower"=>"",
- "com_total_upper"=>"","trade_used"=>"","create_start"=>"","create_end"=>"",'customerName'=>''],"post","trim");
- $where=[["is_del","=",0]];
- if($params['tradNo']!=="")$where[]=["trade.tradNo","like","%".$params['tradNo']."%"];
- if(!empty($params['status']))$where[]=["status","in",$params['status']];
- if($params['name']!=="")$where[]=["trade_out","like","%".$params['name']."%"];
- if($params['bank']!=="")$where[]=["trade_bank","like","%".$params['bank']."%"];
- if($params['start']!=="") $where[]=["trade_time",">=",startTime($params['start'])];
- if($params['end']!=="") $where[]=["trade_time","<=",endTime($params['end'])];
- if($params['companyNo']!=="")$where[]=["companyNo","like","%".$params['companyNo']."%"];
- if($params['customerName']!=="")$where[]=["customerName","like","%".$params['customerName']."%"];
- if($params['trade_in_code']!=="") $where[]=["trade_in_code","like","%".$params['trade_in_code']."%"];
- if($params['relaComNo']!=="")$where[]=["companyNo","like","%".$params['relaComNo']."%"];
- if($params['used_lower']!=="") $where[]=["used_fee",">=",$params['used_lower']];
- if($params['used_upper']!=="") $where[]=["used_fee","<=",$params['used_upper']];
- if($params['total_lower']!=="") $where[]=["total_fee",">=",$params['total_lower']];
- if($params['total_upper']!=="") $where[]=["total_fee","<=",$params['total_upper']];
- if($params['poCode']!=="") $where[]=["poCode","like","%".$params['poCode']."%"];
- if($params['pcCode']!=="") $where[]=["pcCode","like","%".$params['pcCode']."%"];
- if($params['trade_type']!=="") $where[]=["trade_type","=",$params['trade_type']];
- if($params['palt_po']!=="") $where[]=["palt_po","like","%".$params['palt_po']."%"];
- if($params['skuCode']!=="") $where[]=["skuCode","like","%".$params['skuCode']."%"];
- if($params['goodName']!=="") $where[]=["goodName","like","%".$params['goodName']."%"];
- if($params['plat_total_lower']!=="") $where[]=["plat_total",">=",$params['plat_total_lower']];
- if($params['plat_total_upper']!=="") $where[]=["plat_total","<=",$params['plat_total_upper']];
- if($params['com_total_lower']!=="") $where[]=["com_total",">=",$params['com_total_lower']];
- if($params['com_total_upper']!=="") $where[]=["com_total","<=",$params['com_total_upper']];
- if($params['trade_used']!=="") $where[]=["trade_used","like",'%'.$params['trade_used'].'%'];
- if($params['create_start']!=="") $where[]=["addtime",">=",startTime($params['create_start'])];
- if($params['create_end']!=="") $where[]=["addtime","<=",endTime($params['create_end'])];
- $list = $this->model->with(['Lib'])->withJoin("Lib","left")->where($where)->order("id desc")->paginate(["list_rows"=>$params['size'],"page"=>$params['page']]);
- return success("成功",['list'=>$list->items(),'count'=>$list->total()]);
- }
- public function delete(){
- $id = $this->request->post("id","0","int");
- if($id<=0) return error("参数错误");
- $info = $this->model->where(["id"=>$id,"is_del"=>0])->findOrEmpty();
- if($info->isEmpty()) return error("资金流水信息不存在");
- if($info->status!=1) return error("当前状态不允许删除");
- $this->model->startTrans();
- try{
- $info->is_del=1;
- $save=$info->save();
- if(!$save) throw new \Exception("资金流水删除失败");
- $this->model->commit();
- }catch (\Exception $e){
- $this->model->rollback();
- return error($e->getMessage());
- }
- return success("删除成功");
- }
- public function info(){
- $tradNo = $this->request->post('tradNo','','trim');
- if($tradNo =='') return error('参数错误');
- $info = $this->model->with(["lib"])->where(['tradNo'=>$tradNo,'is_del'=>0])->findOrEmpty();
- return success('查询成功',$info);
- }
- public function PoolList(){
- $params=$this->request->param(["logNo"=>"","status"=>"","name"=>"","bank"=>"","start"=>"","end"=>"","companyNo"=>"",
- "page"=>1,"size"=>20,'apply_id'=>"",'orderCode'=>'',"relaNo"=>"",'platform_type'=>'',"relaComNo"=>"",'cxCode'=>'',"pcCode"=>"",
- "trade_type"=>"","tag_type"=>"","tradNo"=>""],"post","trim");
- $where=[["a.is_del","=",0],["b.is_del","=",0],['c.is_del','=',0],['d.is_del','=',0]];
- if($params['logNo']!=="")$where[]=["a.logNo","like","%".$params['logNo']."%"];
- if($params['tradNo']!=='')$where[]=['a.tradNo','like','%'.$params['tradNo'].'%'];
- if($params['apply_id']!=="")$where[]=["a.apply_id","like","%".$params['apply_id']."%"];
- if($params['orderCode']!=="")$where[]=["d.orderCode","like","%".$params['orderCode']."%"];
- if($params['platform_type']!=="")$where[]=["a.platform_type","like","%".$params['platform_type']."%"];
- if($params['status']!=="")$where[]=["a.status","=",$params['status']];
- if($params['name']!=="")$where[]=["b.trade_out","like","%".$params['name']."%"];
- if($params['bank']!=="")$where[]=["b.trade_bank","like","%".$params['bank']."%"];
- if($params['start']!=="") $where[]=["a.addtime",">=",startTime($params['start'])];
- if($params['end']!=="") $where[]=["a.addtime","<=",endTime($params['end'])];
- if($params['companyNo']!=="")$where[]=["a.companyNo","like","%".$params['companyNo']."%"];
- if($params['relaComNo']!=="")$where[]=["a.companyNo","like","%".$params['relaComNo']."%"];
- if($params['cxCode']!=="")$where[]=["c.cxCode","like","%".$params['cxCode']."%"];
- if($params['relaNo']!=="")$where[]=["a.relaNo","like","%".$params['relaNo']."%"];
- $pcData=['totalPrice'=>0,'usedPrice'=>0,'balancePrce'=>0,'tradPrice'=>0];// 统计数据
- if($params['pcCode']!==""){
- $where[]=['b.pcCode','like','%'.$params['pcCode'].'%'];
- $pc= $this->model->with(["lib"])->where('pcCode',$params['pcCode'])
- ->select();
- if(!$pc->isEmpty()){
- $pcData['totalPrice'] = array_sum(array_column($pc->toArray(),"total_fee"));
- $pcData["usedPrice"]= TradePool::where(["tradNo"=>array_column($pc->toArray(),'tradNo'),"status"=>2,"is_del"=>0,'tag_type'=>0])->sum("total_fee");
- $pcData['tradPrice']= TradePool::where(['tradNo'=>array_column($pc->toArray(),'tradNo'),'status'=>1,'is_del'=>0,'tag_type'=>0])->sum('total_fee');
- $pcData["balancePrce"] = array_sum(array_column($pc->toArray(),'balance'));
- }
- }
- if($params['trade_type']!=="") $where[]=["b.trade_type","=",$params['trade_type']];
- if($params['tag_type']!=="") $where[]=["a.tag_type","=",$params['tag_type']];
- $list = TradePool::alias("a")
- ->leftjoin('trade_lib e','a.tradNo=e.tradNo')
- ->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($where)
- ->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,
- e.goodNo as lib_goodNo,
- c.goodName,
- e.goodName as lib_goodName,
- c.goodNum-c.thNum as goodNum,
- e.goodNum as lib_goodNum,
- c.goodPrice,
- e.comPrice as lib_price,
- c.qrdType,
- c.ownerName,
- c.ownerid,
- c.department,
- c.platName,
- c.totalPrice,
- e.com_total as lib_total,
- d.cancel_fee,
- b.pcCode,
- c.poCode,
- b.poCode as order_poCode,
- e.palt_po')
- ->order("a.id desc")->paginate(["list_rows"=>$params['size'],"page"=>$params['page']]);
- return success("成功",['list'=>$list->items(),'count'=>$list->total(),"pcData"=>$pcData]);
- }
- public function PoolQuery(){
- $params=$this->request->param(["logNo"=>"","tradNo"=>"","status"=>"","companyNo"=>"",'apply_id'=>"",'platform_type'=>'',"relaComNo"=>"",],"post","trim");
- $where=[["is_del","=",0]];
- if($params['logNo']!=="")$where[]=["logNo","like","%".$params['logNo']."%"];
- if($params['tradNo']!=="")$where[]=["tradNo","like","%".$params['tradNo']."%"];
- if($params['apply_id']!=="")$where[]=["apply_id","like","%".$params['apply_id']."%"];
- if($params['status']!=="")$where[]=["status","=",$params['status']];
- if($params['companyNo']!=="")$where[]=["companyNo","like","%".$params['companyNo']."%"];
- if($params['relaComNo']!=="")$where[]=["companyNo","like","%".$params['relaComNo']."%"];
- if($params['platform_type']!=="") $where[]=["platform_type","=",$params['platform_type']];
- $list = TradePool::with(["Lib"])->where($where)->order("id desc")->select();
- return success("成功",$list);
- }
- public function PoolInfo(){
- $logNo = $this->request->post("logNo","","trim");
- if($logNo=="") return error("参数错误");
- $info = TradePool::with(["Trade","Lib"])
- ->where(["logNo"=>$logNo,"is_del"=>0])
- ->findOrEmpty();
- if($info->isEmpty()) return error("资金认领信息不存在");
- $info['orderinfo']= Assoc::alias("a")
- ->leftJoin('qrd_info b','a.orderCode=b.sequenceNo')
- ->where(["a.viceCode"=>$logNo,"a.type"=>2,'a.is_del'=>0,'a.status'=>[1,2,3]])
- ->field('b.*,c.goodNum-c.thNum as goodNum,a.cancel_fee')
- ->select();
- return success("成功",$info);
- }
- public function PoolBatchCheck(){
- $params=$this->request->param(["idArr"=>[],"remark"=>"","status"=>""],"post","trim");
- $valid = Validate::rule([
- "idArr|ID"=>"require|array",
- "remark|备注"=>"max:255",
- "status|状态"=>"require|in:2,3"
- ]);
- if(!$valid->check($params)) return error($valid->getError());
- $logInfo = TradePool::where(["id"=>$params['idArr'],"status"=>1,"is_del"=>0])->select();
- if($logInfo->isEmpty()) return error("资金认领信息不存在");
- $this->model->startTrans();
- try{
- $logInfo->each(function($item) use($params){
- Assoc::CheckTrad($item->logNo,$params['status']);
- $item->status = $params['status'];
- $item->remark = $params['remark'];
- $save=$item->save();
- if(!$save) throw new \Exception("资金认领信息更新失败");
- if($params['status']==3){
- $trade = $this->model->where(["tradNo"=>$item->tradNo,"is_del"=>0])->findOrEmpty();
- if($trade->used_fee<$item->total_fee) throw new \Exception('资金流水已使用金额不足');
- $trade->balance+=$item->total_fee;
- $trade->used_fee-=$item->total_fee;
- $trade->status= $trade->used_fee==0?1:2;
- $up=$trade->save();
- if(!$up) throw new \Exception('资金流水更新失败');
- }
- });
- $this->model->commit();
- }catch (\Exception $e){
- $this->model->rollback();
- return error($e->getMessage());
- }
- return success("操作成功");
- }
- //认领资金退回
- public function Back(){
- $params=$this->request->param(["logNo"=>"","remark"=>"","type"=>"","return_reason"=>""],"post","trim");
- $valid = Validate::rule([
- "logNo|认领资金编号"=>"require|max:255",
- "type|类型"=>"require|in:1,2",
- "return_reason|退回原因"=>"require|max:255",
- "remark|备注"=>"max:255"
- ]);
- if(!$valid->check($params)) return error($valid->getError());
- $info = TradePool::where(["logNo"=>$params['logNo'],"is_del"=>0])->findOrEmpty();
- if($info->isEmpty()) return error("资金认领信息不存在");
- if($info->status!=2) return error("资金认领当前状态不允许退回");
- if($info->tag_type==1) return error("平台衍生资金认领不允许退回");
- $isT = TradeReturn::where(["logNo"=>$params['logNo'],"status"=>[0,1]])->findOrEmpty();
- if(!$isT->isEmpty()) return error("当前资金认领已申请退款,请勿重复申请");
- $data = [
- 'returnCode'=>makeNo('RTA'),
- 'logNo'=>$params['logNo'],
- 'companyNo'=>$info->companyNo,
- 'tradNo'=>$info->tradNo,
- 'return_img'=>'',
- 'type'=>$params['type'],
- 'apply_id'=>$this->uid,
- 'apply_name'=>$this->uname,
- 'return_reason'=>$params['return_reason'],
- 'remark'=>$params['remark'],
- "status"=>0
- ];
- $res = TradeReturn::create($data);
- if($res->isEmpty()) return error("操作失败");
- return success("操作成功");
- }
- public function BackList(){
- $params=$this->request->param(["logNo"=>"","tradNo"=>"","status"=>"","companyNo"=>"",'apply_id'=>"","type"=>"",
- "returnCode"=>"","orderCode"=>"","relaComNo"=>"", "page"=>1,"size"=>20],"post","trim");
- $where=[["a.is_del","=",0]];
- if($params['logNo']!=="")$where[]=["a.logNo","like","%".$params['logNo']."%"];
- if($params['apply_id']!=="")$where[]=["a.apply_id","like","%".$params['apply_id']."%"];
- if($params['tradNo']!=="")$where[]=["a.tradNo","like","%".$params['tradNo']."%"];
- if($params['status']!=="")$where[]=["a.status","=",$params['status']];
- if($params['companyNo']!=="")$where[]=["a.companyNo","like","%".$params['companyNo']."%"];
- if($params['relaComNo']!=="")$where[]=["a.companyNo","like","%".$params['relaComNo']."%"];
- if($params['type']!=="")$where[]=["a.type","=",$params['type']];
- if($params['returnCode']!=="")$where[]=["a.returnCode","like","%".$params['returnCode']."%"];
- if($params['orderCode']!=="")$where[]=["b.orderCode","like","%".$params['orderCode']."%"];
- $list = TradeReturn::alias("a")
- ->leftJoin('trade_pool d','a.logNo=d.logNo and d.is_del=0')
- ->leftJoin('assoc b','a.logNo=b.viceCode and b.type=2 and b.is_del=0')
- ->leftJoin('qrd_info c','c.sequenceNo=b.orderCode and c.is_del=0')
- ->field('a.*,d.tag_type,d.relaNo,b.orderCode,c.qrdSource,goodNo,goodName,qrdType,c.ownerName,c.ownerid,c.department,c.poCode,c.platName,b.cancel_fee,c.customerName,c.customerNo')
- ->where($where)
- ->order("a.id desc")->paginate(["list_rows"=>$params['size'],"page"=>$params['page']]);
- return success("成功",['list'=>$list->items(),'count'=>$list->total()]);
- }
- public function BackInfo(){
- $returnCode = $this->request->post("returnCode","","trim");
- if($returnCode=="") return error("参数错误");
- $info = TradeReturn::with(["trade","pool"])->where(["returnCode"=>$returnCode,"is_del"=>0])->findOrEmpty();
- if($info->isEmpty()) return error("退款申请数据未找到");
- $info['log_total_fee']= $info->pool->total_fee;
- $info['log_apply_id']= $info->pool->apply_id;
- $info['log_apply_name']= $info->pool->apply_name;
- $info['orderinfo']= Assoc::alias("a")
- ->leftJoin('qrd_info b','a.orderCode=b.sequenceNo')
- ->where(["a.viceCode"=>$info['logNo'],"a.type"=>2,'a.is_del'=>0,'a.status'=>[1,2,3]])
- ->field('b.*,c.goodNum-c.thNum as goodNum,a.cancel_fee')
- ->findOrEmpty();
- return success("成功",$info);
- }
- public function BackStatus(){
- $params=$this->request->param(["returnCode"=>"","status"=>"","remark"=>"","return_img"=>""],"post","trim");
- $valid = Validate::rule([
- "returnCode|退款申请编号"=>"require|max:255",
- "status|状态"=>"require|in:1,2",
- "remark|备注"=>"max:255"
- ]);
- if(!$valid->check($params)) return error($valid->getError());
- $info = TradeReturn::where(["returnCode"=>$params['returnCode'],"is_del"=>0])->findOrEmpty();
- if($info->isEmpty()) return error("退款申请数据未找到");
- $loginfo= TradePool::where(["logNo"=>$info['logNo'],"is_del"=>0])->findOrEmpty();
- if($loginfo->isEmpty()) return error("资金认领数据未找到");
- if($loginfo->status!=2) return error("认领资金信息审核未通过");
- $this->model->startTrans();
- try{
- $info->status=$params['status'];
- $info->remark=$params['remark'];
- $info->return_img=$params['return_img'];
- $save=$info->save();
- if ($save===false) throw new \Exception("操作失败");
- if($params['status']==1){
- $loginfo->status= 4;
- $logsave=$loginfo->save();
- if ($logsave===false) throw new \Exception("操作失败");
- Assoc::CheckTrad($info['logNo'],$loginfo->status);
- $trade = $this->model->where("tradNo",$loginfo->tradNo)->findOrEmpty();
- if($trade->isEmpty()) throw new \Exception("资金流水信息未找到");
- $trade->used_fee-=$loginfo->total_fee;
- $trade->balance+=$loginfo->total_fee;
- $trade->status= $trade->used_fee==0?1:2;
- $tradesave=$trade->save();
- if ($tradesave===false) throw new \Exception("资金流水信息更新失败");
- }
- $this->model->commit();
- }catch (\Exception $e){
- $this->model->rollback();
- return error($e->getMessage());
- }
- return success("退款申请审核成功");
- }
- /**
- * 导入交易流水
- * data:
- * companyNo
- * trade_in_account
- * tradeTime
- * trade_fee
- * trade_bank
- * trade_account
- * trade_out
- */
- public function importTradeByArr(){
- $data= $this->request->post("data",[],"trim");
- if(!is_array($data)||empty($data)) return error("参数错误");
- /**
- * 'palt_po' =>'varchar',//平台采购po
- 'skuCode' =>'varchar',//泰E购sku
- 'goodName' =>'varchar',//平台物料名称
- 'goodNum' =>'varchar',//商品数量
- 'platPrice' =>'decimal',//平台业务公司单价
- 'plat_total' =>'decimal',//平台业务公司金额
- 'tax' =>'varchar',//税率
- 'cat_code' =>'varchar',//税收编码
- 'cat_name' =>'varchar',//税收名称
- 'comPrice' =>'decimal',//业务公司单价
- 'com_total' =>'decimal',//业务公司总额
- 'pcCode' =>'varchar',//批次号
- */
- $valid = Validate::rule([
- "companyNo|业务公司编号"=>"require|max:255",
- "trade_type|交易类型"=>"require|number|in:0,1",
- "trade_in_code|收款方公司编号"=>"require|max:255",
- "trade_in_account|收款账户"=>"require|max:255",
- "tradeTime|交易时间"=>"require|date",
- "trade_fee|交易金额"=>"require|float",
- "trade_bank|交易银行"=>"require|max:255",
- "trade_account|对方账号"=>"require|max:255",
- "trade_out|交易方名称"=>"require|max:255",
- "poCode|PO编号" =>"requireIf:trade_type,1|max:255",
- "NdCode|ND编号"=>"requireIf:trade_type,1|max:255",
- "palt_po|平台采购po"=>"requireIf:trade_type,1|max:255",
- "skuCode|SKU"=>"requireIf:trade_type,1|max:255",
- "goodNo|商品编码"=>"max:255",
- "goodName|商品名称"=>"requireIf:trade_type,1|max:255",
- "goodNum|商品数量"=>"requireIf:trade_type,1|float",
- "platPrice|平台单价"=>"requireIf:trade_type,1|float",
- "plat_total|平台金额"=>"requireIf:trade_type,1|float",
- "unit|单位"=>"requireIf:trade_type,1|max:255",
- "tax|税率"=>"requireIf:trade_type,1|max:255",
- "cat_code|税收编码"=>"requireIf:trade_type,1|max:255",
- "cat_name|税收名称"=>"requireIf:trade_type,1|max:255",
- "comPrice|业务公司单价"=>"requireIf:trade_type,1|float",
- "com_total|业务公司总额"=>"requireIf:trade_type,1|float",
- ]);
- $companyArr= Business::whereIn("companyNo",array_merge(array_column($data,'companyNo'),array_column($data,'trade_in_code')))->column("company","companyNo");
- $list=[];
- $lib=[];
- $pcCode= makeNo("PC");
- foreach ($data as $key=>$value){
- if(!$valid->check($value)) return error('第'.($key+1).'行'.$valid->getError());
- if(!isset($companyArr[$value['companyNo']])) return error('第'.($key+1).'行收款方业务公司不存在');
- if(!isset($companyArr[$value['trade_in_code']])) return error('第'.($key+1).'行收款方公司不存在');
- $radStr= Str::random(3,3);
- $tradNo= makeNo('S'.$radStr,str_pad($key+1,4,'0',STR_PAD_LEFT));
- $list[]=[
- "tradNo"=>$tradNo,
- "companyNo"=>$value['companyNo'],
- "companyName"=>$companyArr[$value['companyNo']]??"",
- "trade_in_code"=>$value['trade_in_code'],
- "trade_in"=>$value['trade_in']??$companyArr[$value['trade_in_code']],
- "trade_out"=>$value['trade_out'],
- "trade_type"=>$value['trade_type']??0,
- "total_fee"=>str_replace(',','',$value['trade_fee']),
- "balance"=>str_replace(',','',$value['trade_fee']),
- "trade_remark"=>$value['trade_remark']??"",
- "trade_time"=>startTime($value['tradeTime']),
- "trade_in_account"=>$value['trade_in_account']??"",
- "trade_used"=>$value['trade_used']??"",
- "trade_bank"=>$value['trade_bank'],
- "trade_account"=>$value['trade_account'],
- "poCode"=>$value['poCode'],
- "NdCode"=>$value['NdCode'],
- "customerName"=>$value['customerName'],
- 'pcCode'=>$pcCode,
- "apply_id"=>$this->uid,
- "apply_name"=>$this->uname,
- ];
- if($value['trade_type']==1){
- $lib[]=[
- "tradNo"=>$tradNo,
- "palt_po"=>$value['palt_po'],
- "skuCode"=>$value['skuCode'],
- "goodNo"=>$value['goodNo'],
- "goodName"=>$value['goodName'],
- "goodNum"=>$value['goodNum'],
- "platPrice"=>$value['platPrice'],
- "plat_total"=>$value['plat_total'],
- "tax"=>$value['tax'],
- "unit"=>$value['unit'],
- "cat_code"=>$value['cat_code'],
- "cat_name"=>$value['cat_name'],
- "comPrice"=>$value['comPrice'],
- "com_total"=>$value['com_total'],
- ];
- }
- }
- if(empty($list)) return error("导入数据不能为空");
- $this->model->startTrans();
- try{
- $num=$this->model->saveAll($list);
- if($num->isEmpty())throw new \Exception('资金流水导入失败');
- if(!empty($lib)) (new TradeLib())->saveAll($lib);
- $this->model->commit();
- }catch (\Exception $e){
- $this->model->rollback();
- return error($e->getMessage());
- }
- return success("资金导入成功");
- }
- public function importTradeByBatchOrderCode(){
- $list= $this->request->post("list",[],"trim");
- if(!is_array($list)||empty($list)) return error("参数错误");
- $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',
- ]);
- $companyArr= Business::whereIn('companyNo',array_column($list,'companyNo'))->column('company','companyNo');
- if(empty($companyArr)) return error("收款方业务公司不存在");
- $orderArr= QrdInfo::whereIn('sequenceNo',array_column($list,'orderCode'))->where(["is_del"=>0])->column('id,customerNo,companyNo,is_comon,status,pay_fee,apay_fee,wpay_fee,pay_status,totalPrice,platform_type,cxCode',"sequenceNo");
- if(empty($orderArr)) return error("订单信息不存在");
- $tradeArr=$this->model->whereIn("tradNo",array_column($list,'tradNo'))->where(['is_del'=>0,"status"=>[1,2]])->column("id,balance,used_fee,companyNo,trade_time,trade_type","tradNo");
- if(empty($tradeArr)) return error("资金流水信息不存在");
- $Assoc=[];
- $trade_log=[];
- foreach ($list as $key=>$value){
- if(!$val_item->check($value)) return error('第'.($key+1).'行'.$val_item->getError());
- if(!isset($companyArr[$value['companyNo']])) return error($value['companyNo'].'卖出方公司不存在');
- if(!isset($orderArr[$value['orderCode']])) return error($value['orderCode'].'订单信息不存在');
- if($orderArr[$value['orderCode']]['companyNo']!=$value['companyNo']) return error($value['orderCode'].'卖出方公司与订单业务公司不一致');
- if($orderArr[$value['orderCode']]['wpay_fee']< $value['trad_fee']) return error($value['orderCode'].'销售单未付款金额不足核销金额');
- $orderArr[$value['orderCode']]['wpay_fee']=bcsub($orderArr[$value['orderCode']]['wpay_fee'], $value['trad_fee'],2);
- if(!isset($tradeArr[$value['tradNo']])) return error($value['tradNo'].'资金流水信息不存在');
- if($tradeArr[$value['tradNo']]['trade_type']!=1 && $orderArr[$value['orderCode']]['is_comon']==1) return error($value['orderCode'].'销售单类型与资金类型不匹配');
- if($tradeArr[$value['tradNo']]['balance']< $value['trad_fee']){
- return error($value['tradNo'].'资金流水余额不足核销金额');
- }
- $tradeArr[$value['tradNo']]['balance']=bcsub($tradeArr[$value['tradNo']]['balance'], $value['trad_fee'],2);
- if($tradeArr[$value['tradNo']]['companyNo']!=$value['companyNo']) return error($value['tradNo'].'资金流水业务公司与卖出方公司不一致');
- $temp=['logNo' => makeNo('TRC',str_pad($key+1,4,'0',STR_PAD_LEFT)),
- 'tradNo' => $value['tradNo'],
- 'platform_type' => $orderArr[$value['orderCode']]['platform_type'],
- 'companyNo' => $value['companyNo'],
- 'customerNo' => $orderArr[$value['orderCode']]['customerNo'],
- 'apply_id' => $this->uid,
- 'apply_name' => $this->uname,
- 'trade_time' => $tradeArr[$value['tradNo']]['trade_time'],
- 'total_fee' => $value['trad_fee'],
- 'status' => 2,//2审核通过
- ];
- $Assoc[]=[
- 'assocNo' =>makeNo("AS",str_pad($key+1,4,'0',STR_PAD_LEFT)),
- 'apply_id' => $this->uid,
- 'apply_name' => $this->uname,
- 'type' => 2,
- 'orderCode' => $value['orderCode'],
- 'customerNo' => $orderArr[$value['orderCode']]['customerNo'],
- 'viceCode' => $temp['logNo'],
- 'order_total' =>$orderArr[$value['orderCode']]['totalPrice'],
- 'vice_total' => $value['trad_fee'],
- 'cancel_fee' => $value['trad_fee'],
- 'status' => 2,//认领通过
- ];
- $trade_log[]=$temp;
- }
- $this->model->startTrans();
- try{
- foreach ($list as $key=>$value){
- $trade = $this->model->where('is_del', 0)
- ->whereIn('status', [1, 2])
- ->where('tradNo', $value['tradNo'])
- ->field('id,balance,used_fee,companyNo,trade_time')
- ->findOrEmpty();
- if($trade->isEmpty()) throw new \Exception($value['tradNo'].'资金流水信息不存在');
- if($trade->balance< $value['trad_fee']) throw new \Exception($value['tradNo'].'资金流水余额不足核销金额');
- $trade->balance=bcsub($trade->balance, $value['trad_fee'],2);
- $trade->used_fee=bcadd($trade->used_fee, $value['trad_fee'],2);
- $trade->status=$trade->balance==0?3:2;
- $tradSave=$trade->save();
- if(!$tradSave) throw new \Exception($value['tradNo'].'资金流水更新失败');
- $order = QrdInfo::where('is_del', 0)
- ->where('sequenceNo', $value['orderCode'])
- ->field('id,customerNo,companyNo,status,pay_fee,apay_fee,wpay_fee,pay_status,totalPrice,platform_type,cxCode,is_comon')
- ->findOrEmpty();
- if($order->isEmpty()) throw new \Exception($value['orderCode'].'订单信息不存在');
- if($order->wpay_fee< $value['trad_fee']) throw new \Exception($value['orderCode'].'销售单未付款金额不足核销金额');
- $order->wpay_fee=bcsub($order->wpay_fee, $value['trad_fee'],2);
- $order->apay_fee=bcadd( $order->apay_fee, $value['trad_fee'],2);
- $order->pay_status=$order->wpay_fee==0 && $order->pay_fee==0?3:2;
- $order->status=1;
- $ordersave=$order->save();
- if(!$ordersave) throw new \Exception($value['orderCode'].'订单信息更新失败');
- if($order->pay_status==3 && $order->is_comon==0 && $order->cxCode!=''){
- ComonOrder::where(['cxCode'=>$order->cxCode,'status'=>-1])->save(['status'=>0]);
- }
- }
- if(!empty($Assoc)) {
- $AssocIn= (new Assoc())->saveAll($Assoc);
- if($AssocIn->isEmpty()) throw new \Exception('关联信息写入失败');
- }
- if(!empty($trade_log)){
- $trade_logIn= (new TradePool())->saveAll($trade_log);
- if($trade_logIn->isEmpty()) throw new \Exception('资金流水写入失败');
- }
- $this->model->commit();
- }catch (\exception $e){
- $this->model->rollback();
- return error($e->getMessage());
- }
- return success("数据导入成功");
- }
- }
|