afa 5 сар өмнө
parent
commit
5e370f1b2c

+ 6 - 4
app/api/controller/Shops.php

@@ -6,6 +6,7 @@ use app\api\validate\Shop as ShopValidate;
 use app\common\model\ShopDelivery;
 use think\Exception;
 use think\facade\Db;
+use app\common\model\StockLog;
 use app\api\service\SpecService;
 use think\exception\ValidateException;
 
@@ -51,7 +52,7 @@ class Shops extends Base
 
 
     //添加记录
-    public function create(ShopDelivery $shopDelivery, SpecService $specService)
+    public function create(ShopDelivery $shopDelivery, SpecService $specService, StockLog $stockLog)
     {
         $data = $this->request->post();
         $result = false;
@@ -60,11 +61,12 @@ class Shops extends Base
             validate(ShopValidate::class)->scene('add')->check($data);
           
             //发货数据
-            $resData = $specService::getDeliveryList($this->userinfo['id'], $data);
+            list($deliveryData, $stockData) = $specService::getDeliveryList($this->userinfo['id'], $data);
 
             //扣除库存
-            $result = $shopDelivery->saveAll($resData);
-
+            $shopDelivery->saveAll($deliveryData);
+            //记录
+            $result = $stockLog->saveAll($stockData);
             Db::commit();
         }catch (ValidateException $e) {
             Db::rollback();

+ 22 - 4
app/api/service/SpecService.php

@@ -43,6 +43,7 @@ class SpecService{
       public static function getDeliveryList(int $uid, array $data): array
       {     
             $result = [];
+            $stockData = [];
             $customer_id = ShopList::where('id', $data['shop_id'])->value('customer_id');
             foreach ($data['variety'] as $item) {
                   if(count($item) != 6 || empty(floatval($item['num'])))  throw new \Exception('参数有误!');
@@ -62,12 +63,29 @@ class SpecService{
                   ];
               
                   //根据品种扣除库存
-                  StockDetail::setStockConfigNum((int)$item['variety_id'], (string)-$weight, StockConfig::VarietyName);
-
+                  $varietyNum = StockDetail::setStockConfigNum((int)$item['variety_id'], (string)-$weight, StockConfig::VarietyName);
+                  $stockData[] = [
+                        'user_id'         => $uid, 
+                        'type_id'         => StockConfig::VarietyName,
+                        'type'            => 1,
+                        'variety_id'      => $item['variety_id'],
+                        'change'          => -$weight,
+                        'after'           => $varietyNum,
+                        'remark'          => '录入出库'
+                  ];
                   //根据规格扣除包装箱
-                  StockDetail::setStockConfigNum($specs->box_id, (string)-$item['num'], StockConfig::PackingBox);
+                  $boxNum = StockDetail::setStockConfigNum($specs->box_id, (string)-$item['num'], StockConfig::PackingBox);
+                  $stockData[] = [
+                        'user_id'         => $uid, 
+                        'type_id'         => StockConfig::PackingBox,
+                        'type'            => 1,
+                        'variety_id'      => $item['variety_id'],
+                        'change'          => -$item['num'],
+                        'after'           => $boxNum,
+                        'remark'          => '录入出库'
+                  ];
             }
-            return $result;
+            return [$result, $stockData];
       }
 
       public static function getDeliveryEdit(int $user_id, array $data)