123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345 |
- <?php
- namespace app\cxinv\controller;
- use app\cxinv\model\Pay;
- use app\cxinv\model\PayInfo;
- use app\cxinv\model\PaymentOrder;
- use app\cxinv\model\PayPayment;
- use app\cxinv\model\PayReturn;
- use app\cxinv\model\RoleAction;
- use app\user\model\AccountCompany;
- use think\App;
- use think\facade\Validate;
- class Stage extends Base{
- public function __construct(App $app) {
- parent::__construct($app);
- $this->model =new PayPayment();
- }
- public function create(){
- $params = $this->request->param(["payNo"=>"","cgdNos"=>[]],"post","trim");
- $valid = Validate::rule([
- "payNo|对账单申请编号"=>"require|max:255",
- "cgdNos|对账单付款申请明细"=>"require|array"
- ]);
- if (!$valid->check($params)) return error($valid->getError());
- $pay = Pay::where(["payNo"=>$params["payNo"],"is_del"=>0])->findOrEmpty();
- if ($pay->isEmpty()) return error("对账单申请不存在");
- if($pay->status!=2) return error("对账单申请状态不正确");
- $cgdInfo= PayInfo::where(['payNo'=>$params['payNo'],'is_del'=>0,'status'=>1])->select();
- if($cgdInfo->isEmpty()) return error("对账单付款申请明细不存在");
- $cgdNos = $cgdInfo->column("cgdNo");
- $dzNo= makeNo("DZ");
- $val= Validate::rule([
- "sequenceNo|采购单号"=>"require|max:255",
- "rela_fee|关联付款金额"=>"require|float"]);
- $total_fee="0";
- $relaData=[];
- foreach ($params["cgdNos"] as $v){
- if(!$val->check($v)) return error($val->getError());
- if(!in_array($v["sequenceNo"],$cgdNos)) return error("{$v["sequenceNo"]}采购单号不在当前对账单中");
- if($v["rela_fee"]<=0) continue;
- $relaData[]=[
- "dzNo"=>$dzNo,
- "payNo"=>$params["payNo"],
- "cgdNo"=>$v["sequenceNo"],
- "rela_fee"=>$v["rela_fee"],
- "status"=>0
- ];
- $total_fee=bcadd($total_fee,$v['rela_fee'],2);
- }
- $ment =[
- 'payNo'=>$params['payNo'],
- 'dzNo'=>$dzNo,
- 'apply_id'=>$this->uid,
- 'apply_name'=>$this->uname,
- 'pay_fee'=>$total_fee,
- 'return_img'=>'',
- 'status'=>1,
- 'addtime'=>date('Y-m-d H:i:s'),
- 'updatetime'=>date('Y-m-d H:i:s')
- ];
- if($total_fee>$pay->wpay_fee) return error("对账单未付金额不足");
- if($total_fee <=0) return error("付款金额不正确");
- $this->model->startTrans();
- try{
- $cerste= $this->model->create($ment);
- if($cerste->isEmpty()) throw new \Exception("创建付款申请失败");
- $relaSte= (new \app\cxinv\model\PaymentOrder)->saveAll($relaData);
- if(!$relaSte) throw new \Exception("创建付款申请明细失败");
- $pay->wpay_fee=$pay->wpay_fee-$total_fee;
- $pay->pay_fee = $pay->pay_fee+$total_fee;
- $paySte=$pay->save();
- if(!$paySte) throw new \Exception("更新付款申请失败");
- $this->model->commit();
- event('report_code',['type'=>'add','key'=>'dzNo','value'=>$dzNo,
- 'main'=>['key'=>'cgdNo','value'=>array_column($params['cgdNos'],"sequenceNo")]]);
- }catch (\Exception $e){
- $this->model->rollback();
- return error($e->getMessage());
- }
- return success('创建付款申请成功');
- }
- public function list(){
- $params= $this->request->param(["startTime"=>"","endTime"=>"","payNo"=>"","supplierNo"=>"","supplierName"=>"",
- "companyNo"=>"","apply_name"=>"","status"=>"","dzNo"=>"","relaComNo"=>"","is_comon"=>"",'pay_type'=>'1',"page"=>1,
- "size"=>20],"post","trim");
- $where=[['a.is_del','=',0],['pay_type','=',$params['pay_type']]];
- $check = RoleAction::checkRole($this->roleid, [77,117]);
- if ($check) $where[]=['apply_id','=',$this->uid];
- if ($params['startTime']!=='') $where[]=['a.addtime','>=',startTime($params['startTime'])];
- if ($params['endTime']!=='') $where[]=['a.addtime','<=',endTime($params['endTime'])];
- if ($params['supplierNo']!=='') $where[]=['supplierNo','like','%'.$params['supplierNo'].'%'];
- if ($params['supplierName']!=='') $where[]=['supplierName','like','%'.$params['supplierName'].'%'];
- if ($params['companyNo']!=='') $where[]=['companyNo','like','%'.$params['companyNo'].'%'];
- if ($params['apply_name']!=='') $where[]=['apply_name','like','%'.$params['apply_name'].'%'];
- if ($params['status']!=='') $where[]=['a.status','=',$params['status']];
- if ($params['dzNo']!=='') $where[]=['dzNo','like','%'.$params['dzNo'].'%'];
- if ($params['payNo']!=='') $where[]=['a.payNo','like','%'.$params['payNo'].'%'];
- if ($params['relaComNo']!=='') $where[]=['companyNo|supplierNo','like','%'.$params['relaComNo'].'%'];
- if ($params['is_comon']!=='') $where[]=['is_comon','=',$params['is_comon']];
- $list = $this->model->alias("a")
- ->leftJoin("pay b","`a`.`payNo` = `b`.`payNo` AND b.is_del = 0 ")
- ->where($where)
- ->field("`a`.`id` AS `id`,
- `a`.`payNo` AS `payNo`,
- `b`.`supplierNo`,
- `b`.`supplierName`,
- `b`.`apay_fee` ,
- `b`.`total_fee` AS `total_fee`,
- `a`.`apply_name`,
- `a`.`apply_id`,
- `b`.`is_comon`,
- `b`.`winv_fee` ,
- `b`.`ainv_fee` ,
- `b`.`wpay_fee` ,
- `b`.`pay_status` ,
- `b`.`inv_status` ,
- `a`.`pay_fee` AS `dpay_fee`,
- `a`.`return_img`,
- `a`.`return_time`,
- `a`.`status` AS `dstatus`,
- `b`.`status`,
- `b`.`remark` AS `remark`,
- `a`.`addtime` ,
- `b`.`companyNo` ,
- `b`.`companyName` ,
- `a`.`dzNo` AS `dzNo`")
- ->order("a.id DESC")
- ->paginate(['list_rows'=>$params['size'],'page'=>$params['page']]);
- foreach ($list->items() as $k=>&$v){
- $v['orderNum'] = PayInfo::where([['payNo','=',$v['payNo']],['is_del','=',0],['status','=',1]])->count();
- $v['has_account'] = AccountCompany::SupplierHasAcount($v['supplierNo']);
- }
- return success('获取成功',['list'=>$list->items(),'count'=>$list->total()]);
- }
- public function status(){
- $params=$this->request->param(["dzNo"=>"","status"=>"","remark"=>"","return_image"=>"","return_time"=>""],"post","trim");
- $valid =Validate::rule([
- "dzNo|付款申请编号"=>"require|max:255",
- "status|审核状态"=>"require|number|in:1,2,3,4,5,6,7",
- "remark|备注"=>"max:255",
- "return_image|回执图片"=>"requireIf:status,4|max:255",
- "return_time|回执时间"=>"requireIf:status,4|date"
- ]);
- if(!$valid->check($params)) return error($valid->getError());
- $stage = $this->model->where([['dzNo','=',$params['dzNo']],['is_del','=',0]])->findOrEmpty();
- if($stage->isEmpty()) return error("付款申请不存在");
- $pay = Pay::where([['payNo','=',$stage['payNo']],['is_del','=',0]])->findOrEmpty();
- if($pay->isEmpty()) return error("对账申请单不存在");
- if($pay->status!=2) return error("对账单状态不正确");
- if($pay->pay_fee<$stage['pay_fee']) return error("对账付款申请金额有误,请确认对账申请金额");
- $this->model->startTrans();
- try{
- $stage->status=$params['status'];
- $stage->remark=$params['remark'];
- if($params['status']==4){
- $stage->return_img=$params['return_image'];
- $stage->return_time=$params['return_time'];
- }
- $stageSte=$stage->save();
- if(!$stageSte) throw new \Exception("更新付款申请状态失败");
- $this->model->commit();
- }catch (\Exception $e){
- $this->model->rollback();
- return error($e->getMessage());
- }
- return success('更新付款申请状态成功');
- }
- public function delete(){
- $dzNo=$this->request->param("dzNo","post","trim");
- $valid =Validate::rule([
- "dzNo|付款申请编号"=>"require|max:255"
- ]);
- if(!$valid->check(['dzNo'=>$dzNo])) return error($valid->getError());
- $stage = $this->model->where([['dzNo','=',$dzNo],['is_del','=',0]])->findOrEmpty();
- if($stage->isEmpty()) return error("付款申请不存在");
- $pay = Pay::where([['payNo','=',$stage['payNo']],['is_del','=',0]])->findOrEmpty();
- if($pay->isEmpty()) return error("对账申请单不存在");
- if($stage->status==4) return error("已回执的付款申请不能删除");
- $this->model->startTrans();
- try{
- $stage->status=8;
- $stage->is_del=1;
- $stageSte=$stage->save();
- if(!$stageSte) throw new \Exception("删除付款申请失败");
- $this->model->commit();
- }catch (\Exception $e){
- $this->model->rollback();
- return error($e->getMessage());
- }
- return success('删除付款申请成功');
- }
- public function info(){
- $dzNo=$this->request->param("dzNo","post","trim");
- $valid =Validate::rule([
- "dzNo|付款申请编号"=>"require|max:255"
- ]);
- if(!$valid->check(['dzNo'=>$dzNo])) return error($valid->getError());
- $stage = $this->model->where([['dzNo','=',$dzNo],['is_del','=',0]])->findOrEmpty();
- if($stage->isEmpty()) return error("付款申请不存在");
- $pay = Pay::where([['payNo','=',$stage['payNo']],['is_del','=',0]])->findOrEmpty();
- if($pay->isEmpty()) return error("对账申请单不存在");
- $stage['pay_apply_id'] = $pay['apply_id']??'';
- $stage['pay_apply_name'] = $pay['apply_name']??'';
- $stage['supplierNo'] = $pay['supplierNo']??'';
- $stage['supplierName'] = $pay['supplierName']??'';
- $stage['companyNo'] = $pay['companyNo']??'';
- $stage['companyName'] = $pay['companyName']??'';
- $stage['total_fee'] = $pay['total_fee']??'';
- $stage['cgdlist'] = PaymentOrder::alias("a")
- ->leftJoin("cgd_info b","a.cgdNo=b.sequenceNo")
- ->where(["dzNo"=>$stage['dzNo'],"b.is_del"=>0])
- ->field("b.*,a.id rela_id,dzNo,cgdNo,rela_fee,a.status as rela_status")
- ->select();
- return success('获取成功',$stage);
- }
- public function Back(){
- $params=$this->request->param(["dzNo"=>"","reason"=>"","is_del"=>0],"post","trim");
- $valid =Validate::rule([
- "dzNo|付款申请编号"=>"require|max:255",
- "reason|退款原因"=>"require|max:255"
- ]);
- if(!$valid->check($params)) return error($valid->getError());
- $stage = $this->model->where([['dzNo','=',$params['dzNo']],['is_del','=',0]])->findOrEmpty();
- if($stage->isEmpty()) return error("付款申请不存在");
- if($stage->status!=4) return error("付款申请状态不正确");
- $pay = Pay::where([['payNo','=',$stage['payNo']],['is_del','=',0]])->findOrEmpty();
- if($pay->isEmpty()) return error("对账申请单不存在");
- if($this->level==2 && $pay->is_comon==0){
- $boolen= AccountCompany::SupplierHasAcount($pay->supplierNo);
- if($boolen) return error("该供应商已经开通账号,不允许当前账号操作");
- }
- $isR=PayReturn::where(['orderCode'=>$params['dzNo'],'status'=>[1,2],'is_del'=>0])->findOrEmpty();
- if(!$isR->isEmpty())return error(1004,'付款信息退款流程已存在');
- $reCode= makeNo("RP");
- $data = [
- 'returnCode'=>$reCode,
- 'returnType'=>2,
- 'payNo'=>$stage['payNo'],
- 'orderCode'=>$stage['dzNo'],
- 'reason'=>$params['reason'],
- 'returnImg'=>'',
- 'remark'=>'',
- 'status'=>1,
- 'apply_id'=>$this->uid,
- 'apply_name'=>$this->uname,
- 'is_del'=>0,
- 'addtime'=>date('Y-m-d H:i:s'),
- 'updatetime'=>date('Y-m-d H:i:s'),
- ];
- $this->model->startTrans();
- try{
- $create= PayReturn::create($data);
- if(!$create) throw new \Exception("退款申请创建失败");
- $this->model->commit();
- }catch (\Exception $e){
- $this->model->rollback();
- return error($e->getMessage());
- }
- return success('退款款申请创建成功');
- }
- public function BackList(){
- $param =$this->request->only(['relaComNo'=>'','companyNo'=>'','supplierNo'=>'','start'=>'','end'=>'','returnCode'=>'','payNo'=>'',
- 'status'=>'','dzNo'=>'','pay_type'=>1,'is_comon'=>'','page'=>1,'size'=>15],'post','trim');
- $condition=[['a.is_del','=',0],['a.returnType','=',2],['pay_type','=',$param['pay_type']]];
- $check = RoleAction::checkRole($this->roleid,[131,129]);
- if($check) $condition[]=['a.apply_id','=',$this->uid];
- if($param['relaComNo']!='') $condition[]=['b.companyNo|b.supplierNo','=',$param['relaComNo']];
- if($param['companyNo']!='') $condition[]=['b.companyNo','=',$param['companyNo']];
- if($param['supplierNo']!='') $condition[]=['b.supplierNo','=',$param['supplierNo']];
- if($param['start']!='') $condition[]=['a.addtime','>=',date('Y-m-d H:i:s',strtotime($param['start']))];
- if($param['end']!='') $condition[]=['a.addtime','<=',date('Y-m-d 23:59:59',strtotime($param['end']))];
- if($param['status']!='') $condition[]=['a.status','=',$param['status']];
- if($param['dzNo']!='') $condition[]=['a.orderCode','like',"%{$param['dzNo']}%"];
- if($param['returnCode']!='') $condition[]=['a.returnCode','like',"%{$param['returnCode']}%"];
- if($param['payNo']!='') $condition[]=['a.payNo','like',"%{$param['payNo']}%"];
- if(isset($param['is_comon'])&&$param['is_comon']!=='') $condition []=['is_comon','=',$param['is_comon']];
- $list = PayReturn::alias('a')
- ->leftJoin('pay b','a.payNo=b.payNo and b.is_del=0')
- ->leftJoin('pay_payment c','c.dzNo=a.orderCode and c.is_del=0')
- ->where($condition)
- ->field('a.*,b.supplierNo,b.is_comon,b.supplierName,b.companyNo,b.companyName,b.total_fee,b.apay_fee,b.wpay_fee,b.pay_fee,c.pay_fee pay_pay_fee')
- ->order('a.id desc')
- ->paginate(["list_rows"=>$param['size'],"page"=>$param['page']]);
- foreach ($list->items() as $key => &$value){
- $value['has_account'] = AccountCompany::SupplierHasAcount($value['supplierNo']);
- }
- return success("获取成功",["list"=>$list->items(),"count"=>$list->total()]);
- }
- public function BackStatus(){
- $params = $this->request->param(["returnCode"=>"","status"=>"","remark"=>"","returnImg"=>""],"post","trim");
- $valid =Validate::rule([
- "returnCode|退款单号"=>"require|max:255",
- "status|退款状态"=>"require|in:2,3",
- "remark|备注"=>"requireIf:status,3|max:255",
- "returnImg|退款凭证"=>"max:255"
- ]);
- if(!$valid->check($params)) return error($valid->getError());
- $return = PayReturn::where([['returnCode','=',$params['returnCode']],['is_del','=',0]])->findOrEmpty();
- if($return->isEmpty()) return error("退款申请不存在");
- $return->status=$params['status'];
- $return->remark=$params['remark'];
- $return->returnImg=$params['returnImg'];
- $this->model->startTrans();
- try{
- $returnSte=$return->save();
- if(!$returnSte) throw new \Exception("退款申请状态修改失败");
- if($params['status']==2){
- $stage = $this->model->where([['dzNo','=',$return['orderCode']],['is_del','=',0]])->findOrEmpty();
- if($stage->isEmpty()) throw new \Exception("付款申请不存在");
- $stage->status=5;
- $stageSte=$stage->save();
- if(!$stageSte) throw new \Exception("付款申请状态修改失败");
- }
- $this->model->commit();
- }catch (\Exception $e){
- $this->model->rollback();
- return error($e->getMessage());
- }
- return success("退款申请状态修改成功");
- }
- public function BackDetail(){
- $param = $this->request->only(['returnCode'=>''],'post','trim');
- $valide=Validate::rule([
- 'returnCode|退票申请编号'=>'require|max:255',
- ]);
- if($valide->check($param)==false)return error($valide->getError());
- $detail=PayReturn::alias('a')
- ->leftJoin('pay b','a.payNo=b.payNo and b.is_del=0')
- ->leftJoin('pay_payment c','a.orderCode=c.dzNo')
- ->where(['returnCode'=>$param['returnCode']])
- ->field('a.*,b.supplierNo,b.supplierName,b.companyNo,b.companyName,b.total_fee,b.apay_fee,b.wpay_fee,b.pay_fee,c.pay_fee pay_pay_fee')
- ->findOrEmpty();
- if($detail->isEmpty())return error("退票申请不存在");
- return success("获取成功",$detail);
- }
- }
|