wuggemail@foxmail.com vor 3 Monaten
Ursprung
Commit
b6ee2660ce
2 geänderte Dateien mit 21 neuen und 0 gelöschten Zeilen
  1. 14 0
      app/cxinv/model/FinancialProducts.php
  2. 7 0
      app/cxinv/model/ProductStock.php

+ 14 - 0
app/cxinv/model/FinancialProducts.php

@@ -2,6 +2,7 @@
 namespace app\cxinv\model;
 
 use app\user\model\TaxCategory;
+use think\Model;
 use think\model\concern\SoftDelete;
 class FinancialProducts extends Base
 {
@@ -51,4 +52,17 @@ class FinancialProducts extends Base
     public function ProductStock(){
         return $this->hasOne(ProductStock::class,'product_id','id')->bind(['residue_stock','total_stock','pending_stock']);
     }
+
+    public static function onAfterInsert(Model $model) : void{
+        $stock = ProductStock::where('product_id',$model->id)->findOrEmpty();
+        if($stock->isEmpty()){
+            $stock_data=[
+                'product_id'=>$model->id,
+                'residue_stock'=>0,
+                'total_stock'=>0,
+                'pending_stock'=>0,
+            ];
+            ProductStock::create($stock_data);
+        }
+    }
 }

+ 7 - 0
app/cxinv/model/ProductStock.php

@@ -38,6 +38,13 @@ class ProductStock extends Base
         return true;
     }
 
+    /**
+     * @param $product_id    商品id
+     * @param $num 待出库数量
+     * @param $type 1 正常出库 2 强制出库
+     * @return true
+     * @throws \Exception
+     */
     public static function OutStock($product_id,$num,$type=1){
         $product=self::where("product_id",$product_id)->findOrEmpty();
         if(!$product->isEmpty()){