123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326 |
- <?php
- namespace app\cxinv\model;
- /**
- * Class TagLog
- * @package app\cxinv\model
- */class TagLog extends Base
- {
- //设置字段信息
- protected $schema = [
- 'id' =>'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;
- }
- }
|