123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416 |
- <?php
- namespace app\cxinv\controller;
- use app\cxinv\model\CgdInfo;use app\cxinv\model\FhdChild;use app\cxinv\model\InvoicePay;use app\cxinv\model\InvoicePool;use app\cxinv\model\OrderCategory;use app\cxinv\model\Pay;use app\cxinv\model\PayInfo;use app\cxinv\model\RoleAction;use app\cxinv\model\TagLog;use app\cxinv\model\Track;use app\user\model\AccountCompany;use app\user\model\Business;use app\user\model\Supplier;use think\App;use think\facade\Validate;
- class Payment extends Base{
- public function __construct(App $app) {
- parent::__construct($app);
- $this->model = new Pay();
- }
- public function create(){
- $params = $this->request->param(['cids'=>[]],"post","trim");
- $valid=Validate::rule(["cids|采购单id集合"=>"require|array"]);
- if ($valid->check($params)===false) return error($valid->getError());
- $cgdlist = CgdInfo::whereIn("id",$params['cids'])
- ->field("sequenceNo,supplierNo,totalPrice,companyNo,wpay_fee,winv_fee")->select();
- if($cgdlist->isEmpty())return error("采购单数据不能为空");
- $supplier =array_unique($cgdlist->column('supplierNo')) ;
- $company = array_unique($cgdlist->column("companyNo"));
- if (count($supplier)>1) return error("采购单数据供应商不一致");
- if (count($company)>1) return error("采购单数据业务公司不一致");
- if($this->level==2){
- $isBool = AccountCompany::SupplierHasAcount($supplier[0]);
- if ($isBool) return error('该供应商已经开通账号,不允许当前账号操作');
- }
- $orderCate = OrderCategory::where(['code'=>$cgdlist->column('sequenceNo')])->column("*","code");
- if(empty($orderCate)) return error("采购单数据商品进项类目不存在");
- $payNo=makeNo("PAY");
- $payinfo = [];
- $totalPrice='0';
- $wpay_fee = 0;
- $winv_fee=0;
- foreach ($cgdlist as $k=>$v){
- $temp= [
- "cgdNo"=>$v['sequenceNo'],
- "total_fee"=>$v['totalPrice'],
- "wapy_fee"=>$v['wpay_fee'],
- "winv_fee"=>$v['winv_fee'],
- "payNo"=>$payNo
- ];
- if (!isset($orderCate[$v['sequenceNo']])) return error("采购单{$v['sequenceNo']}数据商品进项类目不存在");
- if($v['status']==1) return error("采购单{$v['sequenceNo']}数据已付款");
- if($v['status']==2) return error("采购单{$v['sequenceNo']}数据不结算");
- $payinfo[]=$temp;
- $totalPrice+=$v['totalPrice'];
- $wpay_fee+=$v['wpay_fee'];
- $winv_fee+=$v['winv_fee'];
- }
- $paydata=[
- 'payNo'=>$payNo,
- 'apply_id'=>$this->uid,
- 'apply_name'=>$this->uname,
- 'total_fee'=>$totalPrice,
- 'is_comon'=>0,
- 'supplierNo'=>$supplier[0],
- 'supplierName'=>Supplier::where('code',$supplier[0])->value("name",''),
- 'companyNo'=>$company[0],
- 'companyName'=>Business::where("companyNo",$company[0])->value("company",""),
- 'wpay_fee'=>$wpay_fee,
- 'apay_fee'=>0,
- 'ainv_fee'=>0,
- 'winv_fee'=>$winv_fee,
- 'remark'=>'',
- 'status'=>1,
- ];
- $this->model->startTrans();
- try{
- $pay=$this->model->save($paydata);
- if(!$pay) throw new \Exception("创建付款单失败");
- $paysace =(new PayInfo())->saveAll($payinfo);
- if(!$paysace) throw new \Exception("创建付款单详情失败");
- $up= CgdInfo::whereIn("sequenceNo",array_column($payinfo,"cgdNo"))->update(['status'=>1]);
- if(!$up) throw new \Exception("更新采购单状态失败");
- $this->model->commit();
- }catch (\Exception $e){
- $this->model->rollback();
- return error($e->getMessage());
- }
- event("report_code",["type"=>"add","key"=>"payNo","value"=>$payNo,"main"=>['key'=>'cgdNo','value'=>array_column($payinfo,'cgdNo')]]);
- return success('创建付款单成功');
- }
- public function list(){
- $params = $this->request->param(['page'=>1,'size'=>20,'startTime'=>'',"endTime"=>"","supplierNo"=>"","payNo"=>"",
- "supplierName"=>"","pay_status"=>"","inv_status"=>"","tag_id"=>"","status"=>"",'companyNo'=>'','pay_type'=>1,
- 'companyName'=>'','is_comon'=>'',"relaComNo"=>""],"post","trim");
- $where=[['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[]=['addtime','>=',startTime($params['startTime'])];
- if ($params['endTime']!=="") $where[]=['addtime','<=',endTime($params['endTime'])];
- if ($params['supplierNo']!=="") $where[]=['supplierNo','like','%'.$params['supplierNo'].'%'];
- if ($params['payNo']!=="") $where[]=['payNo','like','%'.$params['payNo'].'%'];
- if ($params['supplierName']!=="") $where[]=['supplierName','like','%'.$params['supplierName'].'%'];
- if ($params['pay_status']!==""){
- if($params['pay_status']==0)$where[]=['pay_status','<>',3];
- else $where[]=['pay_status','=',$params['pay_status']];
- }
- if ($params['inv_status']!==""){
- if($params['inv_status']==0)$where[]=['inv_status','<>',3];
- else $where[]=['inv_status','=',$params['inv_status']];
- }
- if ($params['status']!=="") $where[]=['status','=',$params['status']];
- if ($params['tag_id']!=="") {
- $conditions_map = [
- 1 => ['inv_tag', '=', 1],
- 2 => ['pay_tag', '=', 1],
- 3 => ["pay_tag|inv_tag", '=', 1],
- 4 => ['pay_tag&inv_tag', '=', 1],
- ];
- $where[]=$conditions_map[$params['tag_id']];
- }
- if ($params['companyNo']!=="") $where[]=['companyNo','=',$params['companyNo']];
- if ($params['companyName']!=="") $where[]=['companyName','like','%'.$params['companyName'].'%'];
- if ($params['is_comon']!=="") $where[]=['is_comon','=',$params['is_comon']];
- if($params['relaComNo']!=="") $where[]=['companyNo|supplierNo','=',$params['relaComNo']];
- $list = $this->model->where($where)->order("id desc")->paginate(["list_rows"=>$params['size'],"page"=>$params['page']]);
- foreach ($list->items() as $k=>&$v){
- $v['cgd_inv_tag_total']=0;
- $v['cgd_pay_tag_total'] = 0;
- if( $v['pay_type']==1){
- $cgdNo = PayInfo::where(['payNo'=>$v['payNo'],'is_del'=>0,'status'=>1])->column("cgdNo");
- $cgdInfo = CgdInfo::where(['sequenceNo'=>$cgdNo,'is_del'=>0,'status'=>1])->select();
- if(!empty($cgdInfo)){
- $v['cgd_inv_tag_total'] =array_sum($cgdInfo->column('inv_tag_fee')) ;
- $v['cgd_pay_tag_total'] = array_sum($cgdInfo->column('pay_tag_fee'));
- }
- }
- $v['orderNum'] = PayInfo::where([['payNo','=',$v['payNo']],['is_del','=',0],['status','=',1]])->count();
- $v['inv_tag_name']='';
- $v['pay_tag_name']='';
- $v['pay_tag_img']='';
- $v['inv_tag_id']='0';
- $v['pay_tag_id']='0';
- $v['pay_tag_remark']='';
- $v['inv_tag_img']='';
- $v['inv_tag_remark']='';
- if($v['inv_tag']==1 || $v['pay_tag']==1){
- $tag = TagLog::alias("a")
- ->leftJoin("order_tag b", "a.tag_id=b.id")
- ->where(['code'=>$v['payNo'],'a.status'=>[1,2]])
- ->order("a.id desc")
- ->column('a.id,b.tag_name,a.tag_img,a.tag_remark','b.type');
- if($v['inv_tag']==1){
- $v['inv_tag_name']=$tag['2']['tag_name']??"";
- $v['inv_tag_img']=$tag['2']['tag_img']??'';
- $v['inv_tag_remark']=$tag['2']['tag_remark']??'';
- $v['inv_tag_id']=$tag['2']['id']??'';
- }
- if($v['pay_tag']==1){
- $v['pay_tag_name']=$tag['1']['tag_name']??'';
- $v['pay_tag_img']=$tag['1']['tag_img']??'';
- $v['pay_tag_remark']=$tag['1']['tag_remark']??'';
- $v['pay_tag_id']=$tag['1']['id']??'';
- }
- }
- $v['has_account'] = AccountCompany::SupplierHasAcount($v['supplierNo']);
- }
- return success("获取成功",["list"=>$list->items(),"count"=>$list->total()]);
- }
- public function save(){
- $params = $this->request->param(["cids"=>[],"payNo"=>""],"post","trim");
- $valid=Validate::rule(["cids|采购单集合数据"=>"require|array","payNo|对账单编号"=>"require|max:255"]);
- if(!$valid->check($params)) return error($valid->getError());
- $pay = $this->model->where(['payNo'=>$params['payNo']])->findOrEmpty();
- if($pay->isEmpty()) return error("对账单不存在");
- if($pay['status']!=1) return error("对账单已提交审核");
- $cgdlist = CgdInfo::whereIn('id',$params['cids'])
- ->field('sequenceNo,supplierNo,totalPrice,companyNo,wpay_fee,winv_fee')->select();
- if($cgdlist->isEmpty())return error('采购单数据不能为空');
- $supplier =array_unique($cgdlist->column('supplierNo')) ;
- $company = array_unique($cgdlist->column('companyNo'));
- if (count($supplier)>1) return error('采购单数据供应商不一致');
- if (count($company)>1) return error('采购单数据业务公司不一致');
- if($this->level==2){
- $isBool = AccountCompany::SupplierHasAcount($supplier[0]);
- if ($isBool) return error('该供应商已经开通账号,不允许当前账号操作');
- }
- $orderCate = OrderCategory::where(['code'=>$cgdlist->column('sequenceNo')])->column('*','code');
- if(empty($orderCate)) return error('采购单数据商品进项类目不存在');
- $payCgd = PayInfo::where([['payNo','=',$params['payNo']],['is_del','=',0]])->column("cgdNo");
- if (!empty($payCgd)){
- $add = array_diff($cgdlist->column('sequenceNo'),$payCgd);
- $remove = array_diff($payCgd,$cgdlist->column('sequenceNo'));
- }
- $payinfo = [];
- $totalPrice='0';
- $wpay_fee = 0;
- $winv_fee=0;
- foreach ($cgdlist as $k=>$v){
- if(!empty($add)&& in_array($v['sequenceNo'],$add)){
- $temp= [
- 'cgdNo'=>$v['sequenceNo'],
- 'total_fee'=>$v['totalPrice'],
- 'wapy_fee'=>$v['wpay_fee'],
- 'winv_fee'=>$v['winv_fee'],
- 'payNo'=>$params['payNo']
- ];
- if (!isset($orderCate[$v['sequenceNo']])) return error("采购单{$v['sequenceNo']}数据商品进项类目不存在");
- if($v['status']==1) return error("采购单{$v['sequenceNo']}数据已付款");
- if($v['status']==2) return error("采购单{$v['sequenceNo']}数据不结算");
- $payinfo[]=$temp;
- }
- $totalPrice+=$v['totalPrice'];
- $wpay_fee+=$v['wpay_fee'];
- $winv_fee+=$v['winv_fee'];
- }
- $paydata=[
- 'total_fee'=>$totalPrice,
- 'supplierNo'=>$supplier[0],
- 'supplierName'=>Supplier::where('code',$supplier[0])->value('name',''),
- 'companyNo'=>$company[0],
- 'companyName'=>Business::where('companyNo',$company[0])->value('company',''),
- 'wpay_fee'=>$wpay_fee,
- 'winv_fee'=>$winv_fee,
- ];
- $this->model->startTrans();
- try{
- $paySave=$pay->save($paydata);
- if($paySave==false) throw new \Exception("对账单保存失败");
- if(!empty($payinfo)){
- $payinfoSave = (new \app\cxinv\model\PayInfo)->saveAll($payinfo);
- if($payinfoSave==false) throw new \Exception("对账单明细保存失败");
- $up = CgdInfo::where([['sequenceNo','in',$add],['status','=',0]])->save(['status'=>1, 'updatetime'=>date('Y-m-d H:i:s')]);
- if($up==false) throw new \Exception("采购单状态修改失败");
- }
- if (!empty($remove)){
- $payRemove = PayInfo::where([['cgdNo','in',$remove],['payNo','=',$params['payNo']]])->save(['is_del'=>1, 'updatetime'=>date('Y-m-d H:i:s')]);
- if($payRemove==false) throw new \Exception("对账单明细删除失败");
- $up = CgdInfo::where([['sequenceNo','in',$remove],['status','=',1]])->save(['status'=>0, 'updatetime'=>date('Y-m-d H:i:s')]);
- if($up==false) throw new \Exception("采购单状态修改失败");
- }
- $this->model->commit();
- event('report_code',['type'=>'add','key'=>'payNo','value'=>$params['payNo'],'main'=>['key'=>'cgdNo','value'=>$add]]);
- event('report_code',['type'=>'sub','key'=>'payNo','value'=>$params['payNo'],'main'=>['key'=>'cgdNo','value'=>$remove]]);
- }catch (\Exception $exception){
- $this->model->rollback();
- return error($exception->getMessage());
- }
- return success('保存成功');
- }
- public function status(){
- $params = $this->request->param(["payNo"=>"","status"=>"","remark"=>""],"post","trim");
- $valid = Validate::rule(["payNo|对账申请编号"=>"require|max:255","status|状态"=>"require|in:1,2,3,4","remark|备注"=>"max:255"]);
- if(!$valid->check($params)) return error($valid->getError());
- $pay = $this->model->where(['payNo'=>$params['payNo'],"is_del"=>0])->findOrEmpty();
- if($pay->isEmpty()) return error("对账单不存在");
- if($params["status"]==4){
- if($pay['inv_tag']==1)return error('存在回票标签未解除');
- if($pay['pay_tag']==1)return error('存在付款标签未解除');
- if($pay->pay_status!=1)return error('对账单存在未解除的付款金额');
- if($pay->inv_status!=1) return error('对账单存在未解除的开票金额');
- if($pay->pay_fee!=0 || $pay->apay_fee!=0)return error("对账单存在未解除的付款金额");
- if($pay->inv_fee!=0 || $pay->ainv_fee!=0) return error("对账单存在未解除的开票金额");
- if($pay->is_comon==1){
- $invoceAr=InvoicePay::where(['payNo'=>$params['payNo']])->column('invNo');
- if(!empty($invoceAr)){
- $ist= InvoicePool::where([['invNo','in',$invoceAr],['status','in',[0,1,2,3,4]]])->select()
- ->isEmpty();
- if($ist==false)return error('通用订单存在开票申请未解除');
- }
- }
- }
- $this->model->startTrans();
- try{
- $pay->status=$params["status"];
- $pay->remark=$params["remark"];
- $pay->updatetime=date("Y-m-d H:i:s");
- $paySave=$pay->save();
- if($paySave==false) throw new \Exception("对账单状态修改失败");
- if($params["status"]==4||$params["status"]==3){
- $cgdList= PayInfo::where(['payNo'=>$params['payNo'],'is_del'=>0,"status"=>1])->select();
- if(!$cgdList->isEmpty()){
- $cgdNo = $cgdList->column('cgdNo');
- if($pay["pay_type"]==1){
- $up = CgdInfo::where([['sequenceNo','in',$cgdNo],['status','=',1]])
- ->save(['status'=>0, 'updatetime'=>date('Y-m-d H:i:s')]);
- if($up==false) throw new \Exception("采购单状态修改失败");
- }
- else {
- $up = Track::where([['TrackCode','in',$cgdNo],['status','=',1]])->save(['status'=>0, 'updatetime'=>date('Y-m-d H:i:s')]);
- if($up==false) throw new \Exception('物流单状态修改失败');
- }
- }
- $payRemove = PayInfo::where(['payNo'=>$params['payNo'],'status'=>1,'is_del'=>0])->save(['status'=>0, 'updatetime'=>date('Y-m-d H:i:s')]);
- if($payRemove==false) throw new \Exception("对账单明细删除失败");
- }
- $this->model->commit();
- event('report_code',['type'=>'sub','key'=>'payNo','value'=>$params['payNo']]);
- if(($params['status']==3 || $params['status']==4)&&$pay['is_comon']==1){
- event('comonOrder',['payNo'=>$params['payNo'],'type'=>'check']);
- }
- }catch (\Exception $exception){
- $this->model->rollback();
- return error($exception->getMessage());
- }
- return success('保存成功');
- }
- public function delete(){
- $params = $this->request->param(["payNo"=>""],"post","trim");
- $valid = Validate::rule(["payNo|对账单编号"=>"require|max:255"]);
- if(!$valid->check($params)) return error($valid->getError());
- $pay = $this->model->where(['payNo'=>$params['payNo'],"is_del"=>0])->findOrEmpty();
- if($pay->isEmpty()) return error("对账单不存在");
- if($pay->status!=1)return error('对账单当前状态不可删除');
- if($this->level==2){
- $isBool = AccountCompany::SupplierHasAcount($pay->supplierNo);
- if ($isBool) return error('该供应商已经开通账号,不允许当前账号操作');
- }
- $payinfo = PayInfo::where(['payNo'=>$params['payNo'],"status"=>1,'is_del'=>0])->select();
- if($payinfo->isEmpty()) return error('该对账单没有明细');
- $this->model->startTrans();
- try{
- $pay->is_del=1;
- $pay->updatetime=date("Y-m-d H:i:s");
- $paySave=$pay->save();
- if($paySave==false) throw new \Exception("对账单删除失败");
- $payRemove = PayInfo::where([['payNo'=>$params['payNo'],"status"=>1,'is_del'=>0]])->save(["is_del"=>1, 'updatetime'=>date('Y-m-d H:i:s')]);
- if($payRemove==false) throw new \Exception("对账单明细删除失败");
- if ($pay->pay_type==1){
- $cgdNo=$payinfo->column('cgdNo');
- if(!empty($cgdNo)){
- $up = CgdInfo::where([['sequenceNo','in',$cgdNo],['status','=',1]])->save(['status'=>0, 'updatetime'=>date('Y-m-d H:i:s')]);
- if($up==false) throw new \Exception( "采购单状态修改失败");
- }else{
- $up = Track::where([['TrackCode','in',$cgdNo],['status','=',1]])->save(['status'=>0, 'updatetime'=>date('Y-m-d H:i:s')]);
- if($up==false) throw new \Exception('物流单状态修改失败');
- }
- }
- $this->model->commit();
- event('report_code',['type'=>'sub','key'=>'payNo','value'=>$params['payNo']]);
- }catch (\Exception $exception){
- $this->model->rollback();
- return error($exception->getMessage());
- }
- return success('删除成功');
- }
- public function info(){
- $params = $this->request->param(["payNo"=>""],"post","trim");
- $valid = Validate::rule(["payNo|对账单编号"=>"require|max:255"]);
- if(!$valid->check($params)) return error($valid->getError());
- $pay = $this->model->where(['payNo'=>$params['payNo'],"is_del"=>0])->findOrEmpty();
- if($pay->isEmpty()) return error("对账单不存在");
- $payinfo= PayInfo::where(['payNo'=>$params['payNo'],"is_del"=>0])->select();
- if($payinfo->isEmpty()) return error("对账单明细不存在");
- $cgdNo = $payinfo->column('cgdNo');
- $pay['cgd_inv_tag_total']=0;
- $pay['cgd_pay_tag_total'] = 0;
- if($pay->pay_type==1){
- $cgdInfo = CgdInfo::where(['sequenceNo'=>$cgdNo,'is_del'=>0,'status'=>1])->select();
- if(!$cgdInfo->isEmpty()){
- $pay['cgd_inv_tag_total'] =array_sum($cgdInfo->column('inv_tag_fee')) ;
- $pay['cgd_pay_tag_total'] = array_sum($cgdInfo->column('pay_tag_fee'));
- $cgdInfo = $cgdInfo->toArray();
- foreach ($cgdInfo as $key=>&$value){
- $value['inv_fee'] = $value['totalPrice']-$value['ainv_fee']-$value['winv_fee']-$value['inv_tag_fee'];
- $value['pay_fee'] = $value['totalPrice']-$value['apay_fee']-$value['wpay_fee']-$value['pay_tag_fee'];
- $value['orderTax'] = OrderCategory::where(['code'=>$value['sequenceNo'],'spuCode'=>$value['goodNo']])
- ->field(['cat_code', 'cat_name', 'merge_code', 'short_name', 'tax', 'inv_good_name'])->findOrEmpty();
- $value['catInfo'] = json_decode($value['catInfo'],true);
- $value['goodNum'] =$value['goodNum']- $value['thNum'];
- $value['sendNum'] =$value['sendNum']- $value['thNum'];
- if($pay['is_comon']==1){
- $value['qrdCode'] = (new \app\cxinv\model\ComonOrder())->where(['cgdNo'=>$value['sequenceNo']])->value('orderCode','');
- }
- }
- }
- }else{
- $cgdInfo = Track::where(['TrackCode'=>$cgdNo,'is_del'=>0])->select();
- if(!$cgdInfo->isEmpty()){
- $fhds = FhdChild::where(['outChildCode'=>$cgdInfo->column("outChildCode")])->column("outCode,good_name,spuCode,post_name,post_code","outChildCode");
- $cgdInfo = $cgdInfo->toArray();
- foreach ($cgdInfo as $key=>&$value){
- if(!isset($fhds[$value['outChildCode']]))$fhds[$value['outChildCode']]=["outCode"=>"","good_name"=>"","spuCode"=>"","post_name"=>"","post_code"=>""];
- $value = array_merge($value,$fhds[$value['outChildCode']]);
- }
- }
- }
- $pay['pay_type']=Supplier::where(['code'=>$pay['supplierNo']])->value("pay_type",'');
- $pay['inv_tag_name']='';
- $pay['pay_tag_name']='';
- $pay['pay_tag_img']='';
- $pay['inv_tag_id']='0';
- $pay['pay_tag_id']='0';
- $pay['pay_tag_remark']='';
- $pay['inv_tag_img']='';
- $pay['inv_tag_remark']='';
- if($pay['inv_tag']==1 || $pay['pay_tag']==1){
- $tag = TagLog::alias('a')
- ->leftJoin('order_tag b', 'a.tag_id=b.id')
- ->where(['code'=>$pay['payNo'],'a.status'=>[1,2]])
- ->order('a.id desc')
- ->column('a.id,b.tag_name,a.tag_img,a.tag_remark','b.type');
- if($pay['inv_tag']==1){
- $pay['inv_tag_name']=$tag['2']['tag_name']??'';
- $pay['inv_tag_img']=$tag['2']['tag_img']??'';
- $pay['inv_tag_remark']=$tag['2']['tag_remark']??'';
- $pay['inv_tag_id']=$tag['2']['id']??'';
- }
- if($pay['pay_tag']==1){
- $pay['pay_tag_name']=$tag['1']['tag_name']??'';
- $pay['pay_tag_img']=$tag['1']['tag_img']??'';
- $pay['pay_tag_remark']=$tag['1']['tag_remark']??'';
- $pay['pay_tag_id']=$tag['1']['id']??'';
- }
- }
- $pay['cgdlist']= $cgdInfo;
- return success("获取成功",$pay);
- }
- }
|