wuggemail@foxmail.com 3 months ago
parent
commit
3ccd8ced33
1 changed files with 34 additions and 14 deletions
  1. 34 14
      app/cxinv/model/FinancialTz.php

+ 34 - 14
app/cxinv/model/FinancialTz.php

@@ -51,21 +51,41 @@ class FinancialTz extends Base
         $change = $model->getChangedData();
         $origin = $model->getOrigin();
         if (isset($change['status']) && $change['status'] == 2 && $origin['status'] == 1) {
-            $product = TzProduct::with(['Product'])->where(['ktCode' => $model['ktCode'], 'type' => 1])->select();
+            $product = TzProduct::with(['Product'=>['ProductsCombind']])->where(['ktCode' => $model['ktCode'], 'type' => 1])->select();
             $product->each(function ($item) use ($model) {
-                ProductStock::where('product_id', $item['product_id'])->inc('residue_stock', $item['num'])->save();
-                $log = [
-                    "type" => 1,
-                    'order_item_id' => $model->manager_id,
-                    'product_id' => $item['product_id'],
-                    'num' => $item['num'],
-                    "fz_date" => $model->Manager->fz_date,
-                    'unit_price' => $item['unit_price'],
-                    'rate' => $item->Product->cat_tax,
-                    'apply_id' => $model->checkUid,
-                    'apply_name' => $model->checkUname,
-                ];
-                (new ProductOnlog)->save($log);
+                if($item['is_combind']==0){
+                    ProductStock::where('product_id', $item['product_id'])->inc('residue_stock', $item['num'])->save();
+                    $log[] = [
+                        "type" => 1,
+                        'order_item_id' => $model->manager_id,
+                        'product_id' => $item['product_id'],
+                        'num' => $item['num'],
+                        "fz_date" => $model->Manager->fz_date,
+                        'unit_price' => $item['unit_price'],
+                        'rate' => $item->Product->cat_tax,
+                        'apply_id' => $model->checkUid,
+                        'apply_name' => $model->checkUname,
+                    ];
+                }else{
+                    if($item->ProductsCombind->isEmpty()) throw new \Exception('子商品不存在');
+                    $item->ProductsCombind->each(function ($child) use (&$log, $model, $item) {
+                        $childInfo= $child->products;
+                        ProductStock::where('product_id', $child['child_id'])->inc('residue_stock', bcmul($child['child_num'], $item['num'], 8))->save();
+                        $log[] = [
+                            "type" => 1,
+                            'order_item_id' => $model->manager_id,
+                            'product_id' => $child['child_id'],
+                            'num' => bcmul($child['child_num'], $item['num'], 8),
+                            "fz_date" => $model->Manager->fz_date,
+                            'unit_price' => $childInfo->unit_price,
+                            'rate' => $childInfo->cat_tax,
+                            'apply_id' => $model->checkUid,
+                            'apply_name' => $model->checkUname,
+                        ];
+                    });
+                }
+
+                (new ProductOnlog)->saveAll($log);
             });
             if ($change['is_checkOrder'] == 1) {
                 $check=[