Эх сурвалжийг харах

品种包装箱/出入库列表

afa 5 сар өмнө
parent
commit
1e8cb8dce1

+ 1 - 1
app/admin/view/goods/product_config/add.html

@@ -17,7 +17,7 @@
                 {field: 'title',title: __('名称'),edit:'text',rules:'required'},
                 {field: 'another_name',title: __('别名'),edit:'text',rules:'required'},
                 {field: 'weight',title: __('重量(斤)'),edit:'text',rules:'required'},
-                {field: 'type_box',title: __('包装箱'),edit:'radio',rules:'required', searchList: Yunqi.data.packingList},
+                {field: 'box_id',title: __('包装箱'),edit:'radio',rules:'required', searchList: Yunqi.data.packingList},
                 {field: 'sort',title: __('排序'),edit:'text'},
                 {field: 'status', title: __('状态'), edit:'switch',searchList: {1: __('正常'),2: __('隐藏')}},
             ]

+ 8 - 29
app/api/controller/Inventory.php

@@ -2,9 +2,9 @@
 
 namespace app\api\controller;
 
-use app\api\service\auth\MysqlAdapter;
+use app\common\model\StockConfig;
 use app\api\service\auth\ApiAuthService;
-use app\api\validate\User as UserValidate;
+use app\common\model\ProductConfig;
 use think\exception\ValidateException;
 use think\facade\Db;
 
@@ -13,35 +13,14 @@ class Inventory extends Base
 
 
 
-    //出入库
-    public function stock()
+    //品种包装箱/出入库列表
+    public function stockList( StockConfig $stockConfig)
     {
+        $type_id = $this->request->post('type_id', 'variety_name');
+        if(!in_array($type_id, [$stockConfig::VarietyName, $stockConfig::PackingBox, $stockConfig::Material])) $this->error(__('参数错误'));
+        $list = $stockConfig::where('type_id', $type_id)->field('id,title')->select();
 
-        
-        $data = $this->request->post();
-        $result = false;
-        Db::startTrans();
-        try {
-           
-            validate(ShopValidate::class)->scene('add')->check($data);
-
-            $data['user_id'] = $this->userinfo['id'];
-            $data['variety'] = json_encode($data['variety'], JSON_UNESCAPED_UNICODE);
-            $result = $userEnterLog::create($data);
-
-            Db::commit();
-        }catch (ValidateException $e) {
-      
-            return $this->error($e->getError());
-        } catch (\Exception $e) {
-            Db::rollback();
-            $this->error($e->getMessage());
-        }
-        if ($result === false) {
-            $this->error(__('没有新增任何数据'));
-        }
-        $this->success();
-      
+        $this->success('ok', $list);
     }
 
 

+ 18 - 26
app/api/controller/Shops.php

@@ -2,12 +2,13 @@
 
 namespace app\api\controller;
 
-use app\common\model\UserEnterLog;
+
 use app\api\validate\Shop as ShopValidate;
 use app\common\model\ShopList;
 use app\common\model\ShopDelivery;
 use think\Exception;
-use think\facade\Cache;
+use think\facade\Db;
+use app\common\service\SpecService;
 use think\exception\ValidateException;
 
 class Shops extends Base
@@ -50,42 +51,33 @@ class Shops extends Base
     public function delivery(ShopDelivery $shopDelivery){
 
         $data = $this->request->post();
-        $result = false;
-        try {
-            validate(ShopValidate::class)->scene('add')->check($data);
-           
-            $data['user_id'] = $this->userinfo['id'];
-            $data['variety'] = json_encode($data['variety'], JSON_UNESCAPED_UNICODE);
-            $result = $shopDelivery::create($data);
-            
-        }catch (ValidateException $e) {
-            return $this->error($e->getError());
-        } catch (\Exception $e) {
-            $this->error($e->getMessage());
-        }
-        if ($result === false) {
-            $this->error(__('没有新增任何数据'));
-        }
+        $result = $shopDelivery::where($data)->select();
         $this->success();
-
     }
 
+
     //添加记录
-    public function create(UserEnterLog $userEnterLog)
+    public function create(ShopDelivery $shopDelivery, SpecService $specService)
     {
- 
+
         $data = $this->request->post();
         $result = false;
+        Db::startTrans();
         try {
             validate(ShopValidate::class)->scene('add')->check($data);
-           
-            $data['user_id'] = $this->userinfo['id'];
-            $data['variety'] = json_encode($data['variety'], JSON_UNESCAPED_UNICODE);
-            $result = $userEnterLog::create($data);
-            
+          
+            //发货数据
+            $resData = $specService::getDeliveryList($this->userinfo['id'], $data);
+
+            //扣除库存
+            $result = $shopDelivery->saveAll($resData);
+
+            Db::commit();
         }catch (ValidateException $e) {
+            Db::rollback();
             return $this->error($e->getError());
         } catch (\Exception $e) {
+            Db::rollback();
             $this->error($e->getMessage());
         }
         if ($result === false) {

+ 1 - 1
app/api/route/route.php

@@ -28,7 +28,7 @@ Route::group('user', function () {
     Route::rule('delivery','shops/delivery','POST');
 
     //出入库
-    Route::rule('stock','inventory/stock','POST');
+    Route::rule('stockList','inventory/stockList','POST');
 
     //库存记录
     Route::rule('stocklog','inventory/stocklog','POST');

+ 0 - 2
app/api/service/auth/ApiAuthService.php

@@ -14,8 +14,6 @@ class ApiAuthService
     protected $allowFields = ['id', 'nickname', 'mobile', 'avatar', 'balance', 'score'];
     
 
-
-
     public function login(string $username, string $password)
     {
         $token=uuid();

+ 1 - 0
app/api/validate/Shop.php

@@ -21,5 +21,6 @@ class Shop extends Validate
  
     protected $scene = [
         'add'  =>  ['plat_id','shop_id','variety'],
+        'goods'  =>  ['plat_id','shop_id','variety'],
     ]; 
 }

+ 5 - 0
app/common/model/StockConfig.php

@@ -8,4 +8,9 @@ use app\common\model\base\BaseModel;
 class StockConfig extends BaseModel
 {
 
+      //类型
+      const VarietyName = 'variety_name';
+      const PackingBox  = 'packing_box';
+      const Material     = 'material';
+
 }

+ 48 - 0
app/common/model/StockDetail.php

@@ -0,0 +1,48 @@
+<?php
+declare(strict_types=1);
+
+namespace app\common\model;
+
+use app\common\model\base\BaseModel;
+
+class StockDetail extends BaseModel
+{
+      // 自动写入时间戳字段
+      protected $autoWriteTimestamp = true;
+      protected $createTime = 'createtime';
+      protected $updateTime = 'updatetime';
+      protected $deleteTime = false;
+
+      protected $type = [
+            'createtime'     =>  'timestamp:Y-m-d H:i',
+            'updatetime'     =>  'timestamp:Y-m-d H:i',
+     
+      ];
+
+
+
+ 
+
+
+      /**
+       * 设置库存配置,根据操作类型对指定库存进行增加或减少操作
+       * @param int $key 库存配置的标识键,通常为库存项的 ID
+       * @param float $num 要操作的数量,正数表示增加,负数表示减少
+       */
+      public static function setStockConfigNum(int $key, string $num, string $type)
+      {     
+            $row = StockDetail::where('key', $key)->where('type_id', $type)->find();
+            if(empty($row)){
+              
+                  return self::create(['key' => $key,'num' => $num, 'type_id'=> $type]);
+            }else{
+                  $row->num = bcadd($row->num, $num, 2);
+                  return $row->save()?true:false;
+            }
+      }     
+      
+
+
+   
+
+}

+ 0 - 15
app/common/model/UserEnterLog.php

@@ -1,15 +0,0 @@
-<?php
-declare(strict_types=1);
-
-namespace app\common\model;
-
-use think\Model;
-
-class UserEnterLog Extends Model
-{
-
-
-
-    
-
-}

+ 38 - 2
app/common/service/SpecService.php

@@ -2,8 +2,10 @@
 declare(strict_types=1);
 namespace app\common\service;
 
-use app\common\model\Msg;
-
+use app\common\model\ShopList;
+use app\common\model\StockConfig;
+use app\common\model\ProductConfig;
+use app\common\model\StockDetail;
 /**
  * 消息服务
  */
@@ -40,6 +42,40 @@ class SpecService extends BaseService{
             return $result;
       }
        
+      
+       //录入规格列表
+      public static function getDeliveryList(int $uid, array $data): array
+      {     
+            $result = [];
+            $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('参数有误!');
+                  $specs = ProductConfig::where('id', $item['spec_id'])->field('weight,box_id')->find();
+                  if(!$specs)  throw new \Exception('参数有误!');
+                  $weight = bcmul((string)$specs->weight, $item['num'], 2);
+                  $result[] = [
+                        'user_id'         => $uid, 
+                        'customer_id'     => $customer_id,
+                        'shop_id'         => $data['shop_id'],
+                        'plat_id'         => $data['plat_id'],
+                        'variety_id'      => $item['variety_id'],
+                        'spec_id'         => $item['spec_id'],
+                        'num'             => $item['num'],
+                        'weigh'           => $weight,
+                        'total_price'     => bcmul($item['price'], $item['num'], 2)
+                  ];
+              
+                  //根据品种扣除库存
+                  StockDetail::setStockConfigNum((int)$item['variety_id'], (string)-$weight, StockConfig::VarietyName);
+
+                  //根据规格扣除包装箱
+                  StockDetail::setStockConfigNum($specs->box_id, (string)-$item['num'], StockConfig::PackingBox);
+            }
+            return $result;
+      }
+
+
+