wugg 6 maanden geleden
bovenliggende
commit
210d4b52f9

+ 0 - 3
app/cxinv/controller/InvoiceItem.php

@@ -45,9 +45,6 @@ class InvoiceItem extends Base{
                 ->field("b.sequenceNo,b.goodNo,b.goodName,a.goodNum,a.goodPrice,a.totalPrice,
                                 c.merge_code,c.cat_code,c.cat_name,c.short_name,c.tax,c.inv_good_name")
             ->select();
-          $list->each(function (&$item){
-            $item['balance_amount']= bcsub($item['totalPrice'],InvoiceOrder::getInvoiceOrderTotalFee($item['sequenceNo']),2);
-        });
          return  success('获取成功',$list);
     }
     //添加发票明细对应关系

+ 0 - 35
app/cxinv/model/Invoice.php

@@ -28,39 +28,4 @@ class Invoice extends Base{
         return $this->hasMany(InvoiceItem::class,'invoiceCode','InvCode');
     }
 
-//    public static function onAfterWrite(Model $model) : void{
-//         $change = $model->getChangedData();
-//         if (isset($change['status'])){
-//             if($change['status']==4){
-//                $itemid= InvoiceItem::where(['invoiceCode'=>$model->InvCode,'status'=>[1,2]])->column("id");
-//                 if(!empty($itemid)){
-//                    $invo= InvoiceOrder::whereIn('itemId',$itemid)->where(['status'=>[1,2]])->column("code,balance_amount,total_amount");
-//                    if(!empty($invo)){
-//                        foreach ($invo as $key=>$val){
-//                            $cgdinfo = CgdInfo::where(['sequenceNo'=>$val['code']])->findOrEmpty();
-//                             if(!$cgdinfo->isEmpty()){
-//                                 $cgdinfo->ainv_fee = bcadd($cgdinfo->winv_fee,bcsub($val['total_amount'],$val['balance_amount'],2),2);
-//                                 $cgdinfo->inv_status = $cgdinfo->winv_fee==0 && $cgdinfo->ainv_fee==$cgdinfo->totalPrice ?3:2;
-//                                 $cgdinfo->save();
-//                             }
-//                             $payinfo = PayInfo::where(['cgdNo'=>$val['code'],'status'=>1,'is_del'=>0])->findOrEmpty();
-//                             if(!$payinfo->isEmpty()){
-//                               $pay= Pay::where(['payNo'=>$payinfo->payNo,'status'=>2])->findOrEmpty();
-//                               if(!$pay->isEmpty()){
-//                                   $pay->inv_fee=bcsub($pay->winv_fee,bcsub($val['total_amount'],$val['balance_amount'],2),2);
-//                                   $pay->ainv_fee = bcadd($pay->inv_fee,bcsub($val['total_amount'],$val['balance_amount'],2),2);
-//                                   $pay->inv_status = $pay->winv_fee==0 && $pay->inv_fee==0?3:2;
-//                                   $pay->save();
-//                               }
-//                             }
-//                        }
-//                    }
-//                 }
-//             }
-//
-//             if($change['status']==9){
-//                  InvoiceItem::refund($model->InvCode);
-//             }
-//         }
-//    }
 }

+ 1 - 3
app/cxinv/model/InvoiceItem.php

@@ -33,14 +33,12 @@ class InvoiceItem extends Base{
         if(isset($change['status'])&&in_array($change["status"],[0,1,2])){
              Log::info('跟后数据:'.json_encode($model->toArray(),JSON_UNESCAPED_UNICODE));
              $num = self::where(['invoiceCode'=>$code,'order_type'=>$orderType,'status'=>0])->count();
-             if($orderType==2){
+             if($orderType==2 && $num==0){
                     $info= PayInvoice::where(['hpNo'=>$code,"status"=>11])->findOrEmpty();
                      Log::info('跟后数据:'.json_encode( $info->toArray(),JSON_UNESCAPED_UNICODE));
                     if(!$info->isEmpty()){
-                        if ($num==0){
                               $info->status=12;
                               $info->save();
-                        }
                     }
                 }
         }

+ 67 - 35
app/cxinv/model/InvoiceOrder.php

@@ -75,56 +75,88 @@ class InvoiceOrder extends Base{
       $id= $model->id;
       $info = InvoiceOrder::where(['id'=>$id])->findOrEmpty();
       if(!$info->isEmpty())return ;
-      if(in_array($info->status,[1,2])){
-         $cgdinfo = CgdInfo::where(['sequenceNo'=>$info->code])->findOrEmpty();
-         if(!$cgdinfo->isEmpty()){
-             $cgdinfo->winv_fee = bcsub($cgdinfo->winv_fee,bcsub($info->total_amount,$info->balance_amount,2),2);
+      if(in_array($info->status,[1,2])) self::orderIn($info->code,bcsub($info->total_amount,$info->balance_amount,2));
+
+      if($info->status==3) self::orderOut($info->code,bcsub($info->total_amount,$info->balance_amount,2));
+      if($info->status==4) self::orderCancel($info->code,bcsub($info->total_amount,$info->balance_amount,2));
+    }
+
+    public static function onBeforeDelete($model){
+        $id=$model->id;
+        $info = InvoiceOrder::where(['id'=>$id])->findOrEmpty();
+        if(!$info->isEmpty())return ;
+        if(in_array($info->status,[1,2])) self::orderOut($info->code,bcsub($info->total_amount,$info->balance_amount,2));
+    }
+    //订单关联初始化金额
+    public static function orderIn($code,$fee){
+         $cgdinfo = CgdInfo::where(['sequenceNo'=>$code])->findOrEmpty();
+         if(!$cgdinfo->isEmpty() && $cgdinfo->winv_fee>=strval($fee)){
+             $cgdinfo->winv_fee = bcsub($cgdinfo->winv_fee,strval($fee),2);
              $cgdinfo->save();
          }
-         $payinfo = PayInfo::where(["cgdNo"=>$info->code,"status"=>1,"is_del"=>0])->findOrEmpty();
+         $payinfo = PayInfo::where(['cgdNo'=>$code,'status'=>1,'is_del'=>0])->findOrEmpty();
          if(!$payinfo->isEmpty()){
-           $pay= Pay::where(['payNo'=>$payinfo->payNo,"status"=>2])->findOrEmpty();
-           if(!$pay->isEmpty()){
-               $pay->winv_fee=bcsub($pay->winv_fee,bcsub($info->total_amount,$info->balance_amount,2),2);
-               $pay->inv_fee = bcadd($pay->inv_fee,bcsub($info->total_amount,$info->balance_amount,2),2);
+           $pay= Pay::where(['payNo'=>$payinfo->payNo,'status'=>2])->findOrEmpty();
+           if(!$pay->isEmpty() && $pay->winv_fee>=strval($fee)){
+               $pay->winv_fee=bcsub($pay->winv_fee,strval($fee),2);
+               $pay->inv_fee = bcadd($pay->inv_fee,strval($fee),2);
                $pay->save();
            }
          }
-      }
-      if($info->status==3){
-          $cgdinfo = CgdInfo::where(['sequenceNo'=>$info->code])->findOrEmpty();
-                    if(!$cgdinfo->isEmpty()){
-                        $cgdinfo->winv_fee = bcadd($cgdinfo->winv_fee,bcsub($info->total_amount,$info->balance_amount,2),2);
-                        $cgdinfo->save();
-                    }
-                   $payinfo = PayInfo::where(['cgdNo'=>$info->code,'status'=>1,'is_del'=>0])->findOrEmpty();
-                   if(!$payinfo->isEmpty()){
-                     $pay= Pay::where(['payNo'=>$payinfo->payNo,'status'=>2])->findOrEmpty();
-                     if(!$pay->isEmpty()){
-                         $pay->winv_fee=bcadd($pay->winv_fee,bcsub($info->total_amount,$info->balance_amount,2),2);
-                         $pay->inv_fee = bcsub($pay->inv_fee,bcsub($info->total_amount,$info->balance_amount,2),2);
-                         $pay->save();
-                     }
-                   }
-      }
-      if($info->status==4){
-          $cgdinfo = CgdInfo::where(['sequenceNo'=>$info->code])->findOrEmpty();
+    }
+    //流程未结束驳回中断
+    public static function orderOut($code,$fee){
+        $cgdinfo = CgdInfo::where(['sequenceNo'=>$code])->findOrEmpty();
+        if(!$cgdinfo->isEmpty()){
+            $cgdinfo->winv_fee = bcadd($cgdinfo->winv_fee,strval($fee),2);
+            $cgdinfo->save();
+        }
+        $payinfo = PayInfo::where(['cgdNo'=>$code,'status'=>1,'is_del'=>0])->findOrEmpty();
+        if(!$payinfo->isEmpty()){
+          $pay= Pay::where(['payNo'=>$payinfo->payNo,'status'=>2])->findOrEmpty();
+          if(!$pay->isEmpty() && $pay->inv_fee>=strval($fee)){
+              $pay->winv_fee=bcadd($pay->winv_fee,strval($fee),2);
+              $pay->inv_fee = bcsub($pay->inv_fee,strval($fee),2);
+              $pay->save();
+          }
+        }
+    }
+
+    public static function orderFinish($code,$fee){
+        $cgdinfo = CgdInfo::where(['sequenceNo'=>$code])->findOrEmpty();
+        if(!$cgdinfo->isEmpty()){
+            $cgdinfo->ainv_fee = bcadd($cgdinfo->ainv_fee,strval($fee),2);
+            $cgdinfo->inv_status = $cgdinfo->ainv_fee==$cgdinfo->totalPrice&& $cgdinfo->winv_fee==0 ?3:2;
+            $cgdinfo->save();
+        }
+        $payinfo = PayInfo::where(['cgdNo'=>$code,'status'=>1,'is_del'=>0])->findOrEmpty();
+        if(!$payinfo->isEmpty()){
+          $pay= Pay::where(['payNo'=>$payinfo->payNo,'status'=>2])->findOrEmpty();
+          if(!$pay->isEmpty() && $pay->inv_fee>=strval($fee)){
+              $pay->ainv_fee=bcadd($pay->ainv_fee,strval($fee),2);
+              $pay->inv_fee = bcsub($pay->inv_fee,strval($fee),2);
+              $pay->inv_status = $pay->winv_fee==0 && $pay->inv_fee==0?3:2;
+              $pay->save();
+          }
+        }
+    }
+    public static function orderCancel($code,$fee){
+         $cgdinfo = CgdInfo::where(['sequenceNo'=>$code])->findOrEmpty();
           if(!$cgdinfo->isEmpty()){
-              $cgdinfo->winv_fee = bcadd($cgdinfo->winv_fee,bcsub($info->total_amount,$info->balance_amount,2),2);
-              $cgdinfo->ainv_fee = bcsub($cgdinfo->ainv_fee,bcsub($info->total_amount,$info->balance_amount,2),2);
-              $cgdinfo->inv_status = $cgdinfo->ainv_fee==0?1:2;
+              $cgdinfo->winv_fee = bcadd($cgdinfo->winv_fee,strval($fee),2);
+              $cgdinfo->ainv_fee = bcsub($cgdinfo->ainv_fee,strval($fee),2);
+              $cgdinfo->inv_status = $cgdinfo->ainv_fee==0 && $cgdinfo->winv_fee==$cgdinfo->totalPrice?1:2;
               $cgdinfo->save();
           }
-         $payinfo = PayInfo::where(['cgdNo'=>$info->code,'status'=>1,'is_del'=>0])->findOrEmpty();
+         $payinfo = PayInfo::where(['cgdNo'=>$code,'status'=>1,'is_del'=>0])->findOrEmpty();
          if(!$payinfo->isEmpty()){
            $pay= Pay::where(['payNo'=>$payinfo->payNo,'status'=>2])->findOrEmpty();
            if(!$pay->isEmpty()){
-               $pay->winv_fee=bcadd($pay->winv_fee,bcsub($info->total_amount,$info->balance_amount,2),2);
-               $pay->ainv_fee = bcsub($pay->ainv_fee,bcsub($info->total_amount,$info->balance_amount,2),2);
+               $pay->winv_fee=bcadd($pay->winv_fee,strval($fee),2);
+               $pay->ainv_fee = bcsub($pay->ainv_fee,strval($fee),2);
                $pay->inv_status = $pay->ainv_fee==0?1:2;
                $pay->save();
            }
          }
-      }
     }
 }

+ 23 - 0
app/cxinv/model/PayInvoice.php

@@ -65,4 +65,27 @@ class PayInvoice extends Base
             }
         }
     }
+
+    public static function onAfterUpdate(Model $model) : void{
+        if ($model->invType==4){
+            $change = $model->getChangedData();
+            if (isset($change['status'])){
+             if($change['status']==4){
+                $itemid= InvoiceItem::where(['invoiceCode'=>$model->hpNo,'status'=>[1,2]])->column('id');
+                 if(!empty($itemid)){
+                    $invo= InvoiceOrder::whereIn('itemId',$itemid)->where(['status'=>[1,2]])->column('code,balance_amount,total_amount');
+                    if(!empty($invo)){
+                        foreach ($invo as $key=>$val){
+                            InvoiceOrder::orderFinish($val['code'],bcsub($val['total_amount'],$val['balance_amount'],2));
+                        }
+                    }
+                 }
+             }
+
+             if($change['status']==9){
+                  InvoiceItem::refund($model->hpNo);
+             }
+         }
+        }
+    }
 }