'bigint',// 'code' =>'varchar',//单号 'tag_id' =>'int',//标签id 'creater' =>'varchar',//创建人 'createrid' =>'int',//创建人id 'tag_fee' =>'decimal',//标签金额 'tag_img' =>'varchar',//标签图片 'status' =>'tinyint',//0/1 状态 有效无效 'addtime' =>'datetime',// 'updatetime' =>'datetime',// 'tag_remark' =>'varchar',//备注 ]; protected $createTime = 'addtime'; protected $updateTime = 'updatetime'; public function TagInfo(){ return $this->belongsTo(OrderTag::class,'tag_id','id')->bind(['type','tag_name']); } public function OrderInfo(){ return $this->belongsTo(QrdInfo::class,'code','sequenceNo') ->bind(['companyNo','companyName','customerNo','customerName',]); } public function PayInfo(){ return $this->belongsTo(Pay::class,'code','payNo') ->bind(['companyNo','companyName','supplierNo','supplierName']); } public function CgdInfo(){ return $this->belongsTo(CgdInfo::class,'code','sequenceNo') ->bind(['companyNo','companyName','supplierNo','supplierName']); } /** 添加标签 * @param $type 标签类型 1对账单付款2对账单回票3销售回款4销售开票5采购单付款 6采购单回票 * @param $code 单编号 * @param $total_fee 标签金额 * @return \think\response\Json|void * @throws \think\db\exception\DbException */ public static function CheckOrderInfo($code,$total_fee,$type){ switch($type){ case 1: case 2: self::payTag($type,$code,$total_fee); break; case 3: case 4: self::qrdTag($type,$code,$total_fee); break; case 5: case 6: self::cgdTag($type,$code,$total_fee); break; default: throw new \Exception('标签类型错误'); } } /** 解除标签 * @param $type * @param $code * @return int|\think\response\Json|void * @throws \think\db\exception\DbException */ public static function ReturnTag($type,$code){ $fee=0; switch($type){ case 1: case 2: $fee=self::payReTag($type,$code); break; case 3: case 4: $fee=self::qrdReTag($type,$code); break; case 5: case 6: $fee=self::cgdReTag($type,$code); break; default: throw new \Exception('标签类型错误'); } return $fee; } /**# 对账单打标签判断 9/25后对账单不允许打标签 采购单非通用类型对账单 * @param $tagId 标签类型 1 付款2回票 * @param $code 对账单编号 * @param $total_fee 标签金额 * @return \think\response\Json|void * @throws \think\db\exception\DbException */ private static function payTag($type,$code,$total_fee){ $pay=Pay::where(['payNo'=>$code,'is_del'=>0])->findOrEmpty(); if($pay->isEmpty())throw new \Exception('未找到对账单数据'); if($pay->is_comon==0 && $pay->pay_type==1 && strtotime($pay->addtime)>=strtotime(env("tag.endtime"))) throw new \Exception('非通用采购对账单不可加标签'); if($type==1){ if($pay->wpay_fee<$total_fee)throw new \Exception('对账单未付金额不足'); $pay->wpay_fee=$pay->wpay_fee-$total_fee; $pay->pay_status = $pay->wpay_fee==0 &&$pay->pay_fee==0 ? 3:($pay->apay_fee==0?1:2); $pay->pay_tag_fee+=$total_fee; $pay->pay_tag= 1; } if($type==2){ if($pay->winv_fee<$total_fee)throw new \Exception('对账单未开票金额不足'); $pay->winv_fee=$pay->winv_fee-$total_fee; $pay->inv_status = $pay->winv_fee==0 &&$pay->inv_fee==0 ? 3:($pay->ainv_fee==0?1:2); $pay->inv_tag_fee+=$total_fee; $pay->inv_tag =1; } $resulr= $pay->save(); if($resulr==false) throw new \Exception('对账单更新失败'); } /**解除标签 * @param $tagId 标签类型 1 付款2回票 * @param $code 对账单编号 * @param $total_fee 标签金额 * @return \think\response\Json|void * @throws \think\db\exception\DbException */ private static function payReTag($type,$code){ $pay=Pay::where(['payNo'=>$code,'is_del'=>0])->findOrEmpty(); if($pay->isEmpty())throw new \Exception('未找到对账单数据'); $fee=0; if($type==1){ if($pay->pay_tag_fee<0)throw new \Exception('对账单付款标签金额不足'); $fee=$pay->pay_tag_fee; $pay->wpay_fee=$pay->wpay_fee+$pay->pay_tag_fee; $pay->pay_tag_fee=0; $pay->pay_status = $pay->wpay_fee==0 && $pay->pay_fee==0?3:($pay->apay_fee==0?1:2); $pay->pay_tag=0; } if($type==2){ if($pay->inv_tag_fee<0)throw new \Exception('对账单回票标签金额不足'); $fee=$pay->inv_tag_fee; $pay->winv_fee=$pay->winv_fee+$pay->inv_tag_fee; $pay->inv_tag_fee=0; $pay->inv_status = $pay->winv_fee==0 && $pay->inv_fee==0?3:($pay->ainv_fee==0?1:2); $pay->inv_tag=0; } $resulr= $pay->save(); if($resulr==false)throw new \Exception('对账单更新失败'); return $fee; } /** * @param $tagId 标签类型 3 回款4开票 * @param $code 销售单编号 * @param $total_fee 标签金额 * @return \think\response\Json|void * @throws \think\db\exception\DbException */ private static function qrdTag($type,$code,$total_fee){ $qrd=QrdInfo::where(['sequenceNo'=>$code,'is_del'=>0])->findOrEmpty(); if($qrd->isEmpty())throw new \Exception('未找到销售单数据'); if($type==3){ if($qrd->wpay_fee<$total_fee){ throw new \Exception('销售单未付金额不足'); } $qrd->wpay_fee=$qrd->wpay_fee-$total_fee; $qrd->pay_status = $qrd->wpay_fee==0 &&$qrd->pay_fee==0 ? 3:($qrd->apay_fee==0?1:2); $qrd->pay_tag_fee+=$total_fee; $qrd->pay_tag= 1; } if($type==4){ if($qrd->winv_fee<$total_fee)throw new \Exception('对账单未开票金额不足'); $qrd->winv_fee=$qrd->winv_fee-$total_fee; $qrd->inv_status = $qrd->winv_fee==0 &&$qrd->inv_fee==0 ? 3:($qrd->ainv_fee==0?1:2); $qrd->inv_tag_fee+=$total_fee; $qrd->inv_tag =1; } $resulr=$qrd->save(); if($resulr==false) throw new \Exception('标签添加失败'); } /** 解除标签销售单 * @param $tagId 标签类型 3 回款4开票 * @param $code 销售单编号 * @param $total_fee 标签金额 * @return \think\response\Json|void * @throws \think\db\exception\DbException */ private static function qrdReTag($tagId,$code){ $qrd=QrdInfo::where(['sequenceNo'=>$code,'is_del'=>0])->findOrEmpty(); if($qrd->isEmpty())throw new \Exception('未找到销售单数据'); $fee =0 ; if($tagId==3){ if($qrd->pay_tag_fee<0)throw new \Exception('销售单回款标签金额不足'); $fee =$qrd->pay_tag_fee ; $qrd->wpay_fee=$qrd->wpay_fee+$qrd->pay_tag_fee; $qrd->pay_tag_fee=0; $qrd->pay_status = $qrd->wpay_fee==0 && $qrd->pay_fee==0?3:($qrd->apay_fee==0?1:2); $qrd->pay_tag=0; } if($tagId==4){ if($qrd->inv_tag_fee<0)throw new \Exception('销售单开票标签金额不足'); $fee =$qrd->inv_tag_fee ; $qrd->winv_fee=$qrd->winv_fee+$qrd->inv_tag_fee; $qrd->inv_tag_fee=0; $qrd->inv_status = $qrd->winv_fee==0 && $qrd->inv_fee==0?3:($qrd->ainv_fee==0?1:2); $qrd->inv_tag=0; } $resulr= $qrd->save(); if($resulr==false)throw new \Exception('销售单更新失败'); return $fee; } /** * @param $tagId 标签类型 3 回款4开票 * @param $code 销售单编号 * @param $total_fee 标签金额 * @return \think\response\Json|void * @throws \think\db\exception\DbException */ private static function cgdTag($type,$code,$total_fee){ $cgd=CgdInfo::where(['sequenceNo'=>$code,'is_del'=>0])->findOrEmpty(); if($cgd->isEmpty())throw new \Exception('未找到采购单数据'); if($cgd->is_comon==1) throw new \Exception('该采购单为通用采购单,不允许添加标签'); $payInfo = PayInfo::where(["cgdNo"=>$code,"status"=>1,"is_del"=>0])->findOrEmpty(); if(!$payInfo->isEmpty()){ $pay= Pay::where(["payNo"=>$payInfo->payNo,"is_del"=>0,"status"=>[0,1,2]])->findOrEmpty(); } if($type==5){ if($cgd->wpay_fee<$total_fee)throw new \Exception('采购单未付金额不足'); $cgd->wpay_fee=$cgd->wpay_fee-$total_fee; $cgd->pay_status = $cgd->wpay_fee==0 ? 3:($cgd->apay_fee==0?1:2); $cgd->pay_tag_fee+=$total_fee; $cgd->pay_tag= 1; if(!$payInfo->isEmpty()){ $payInfo->wpay_fee=$payInfo->wpay_fee-$total_fee; if(!$pay->isEmpty()){ if ($pay->wpay_fee<$total_fee) throw new \Exception('对账单未付金额不足'); $pay->wpay_fee=$pay->wpay_fee-$total_fee; $pay->pay_status = $pay->wpay_fee==0 &&$pay->pay_fee==0 ? 3:($pay->apay_fee==0?1:2); $payInfo->save(); $pay->save(); } } } if($type==6){ if($cgd->winv_fee<$total_fee)throw new \Exception('对账单未开票金额不足'); $cgd->winv_fee=$cgd->winv_fee-$total_fee; $cgd->inv_status = $cgd->winv_fee==0? 3:($cgd->ainv_fee==0?1:2); $cgd->inv_tag_fee+=$total_fee; $cgd->inv_tag =1; if(!$payInfo->isEmpty()){ $payInfo->winv_fee=$payInfo->winv_fee-$total_fee; if(!$pay->isEmpty()){ if ($pay->winv_fee<$total_fee) throw new \Exception('对账单未开票金额不足'); $pay->winv_fee=$pay->winv_fee-$total_fee; $pay->inv_status = $pay->winv_fee==0 && $pay->inv_fee==0? 3:($pay->ainv_fee==0?1:2); $payInfo->save(); $pay->save(); } } } $resulr= $cgd->save(); if($resulr==false) throw new \Exception('标签添加失败'); } /** 解除标签销售单 * @param $tagId 标签类型 3 回款4开票 * @param $code 销售单编号 * @param $total_fee 标签金额 * @return \think\response\Json|void * @throws \think\db\exception\DbException */ private static function cgdReTag($type,$code){ $cgd=CgdInfo::where(['sequenceNo'=>$code,'is_del'=>0])->findOrEmpty(); if($cgd->isEmpty())throw new \Exception('未找到采购单数据'); $payInfo = PayInfo::where(['cgdNo'=>$code,'status'=>1,'is_del'=>0])->findOrEmpty(); if(!$payInfo->isEmpty()){ $pay= Pay::where(['payNo'=>$payInfo->payNo,'is_del'=>0,'status'=>[0,1,2]])->findOrEmpty(); } $fee =0 ; if($type==5){ if($cgd->pay_tag_fee<0)throw new \Exception('采购单回款标签金额不足'); $fee =$cgd->pay_tag_fee ; $cgd->wpay_fee=$cgd->wpay_fee+$cgd->pay_tag_fee; $cgd->pay_tag_fee=0; $cgd->pay_status = $cgd->wpay_fee==0?3:($cgd->apay_fee==0?1:2); $cgd->pay_tag=0; if(!$payInfo->isEmpty()){ $payInfo->wpay_fee=$payInfo->wpay_fee+$fee; if(!$pay->isEmpty()){ $pay->wpay_fee=$pay->wpay_fee+$fee; $pay->pay_status = $pay->wpay_fee==0 &&$pay->pay_fee==0 ? 3:($pay->apay_fee==0?1:2); $payInfo->save(); $pay->save(); } } } if($type==6){ if($cgd->inv_tag_fee<0)throw new \Exception('采购单开票标签金额不足'); $fee =$cgd->inv_tag_fee ; $cgd->winv_fee=$cgd->winv_fee+$cgd->inv_tag_fee; $cgd->inv_tag_fee=0; $cgd->inv_status = $cgd->winv_fee==0?3:($cgd->ainv_fee==0?1:2); $cgd->inv_tag=0; if(!$payInfo->isEmpty()){ $payInfo->winv_fee=$payInfo->winv_fee+$fee; if(!$pay->isEmpty()){ $pay->winv_fee=$pay->winv_fee+$fee; $pay->inv_status = $pay->winv_fee==0 && $pay->inv_fee==0? 3:($pay->ainv_fee==0?1:2); $payInfo->save(); $pay->save(); } } } $resulr= $cgd->save(); if($resulr==false)throw new \Exception('采购单更新失败'); return $fee; } }