Browse Source

库存变动优化

wufeng 2 years ago
parent
commit
005100814a

+ 4 - 3
app/admin/controller/InventoryExchange.php

@@ -41,11 +41,12 @@ class InventoryExchange extends BaseController
     //编辑兑换商品库存
     public function edit()
     {
-        $param = $this->request->only(['id', 'inventory'], 'post');
+        $param = $this->request->only(['id', 'flag', 'number'], 'post');
 
         $val = Validate::rule([
-            'id|兑换商品库存id' => 'require|number|gt:0',
-            'inventory|库存数' => 'require|number|gt:0',
+            'id|兑换商品库存id' => 'require|number|gt:0|lt:999999999',
+            'flag|变化方式' => 'require|number|in:1,-1',
+            'number|库存变化数' => 'require|number|gt:0|lt:999999999',
         ]);
 
         if (!$val->check($param)) throw new ValidateException($val->getError());

+ 4 - 3
app/admin/controller/InventoryShopping.php

@@ -42,11 +42,12 @@ class InventoryShopping extends BaseController
     //编辑商城商品库存
     public function edit()
     {
-        $param = $this->request->only(['id', 'inventory'], 'post');
+        $param = $this->request->only(['id', 'flag', 'number'], 'post');
 
         $val = Validate::rule([
-            'id|商城商品库存id' => 'require|number|gt:0',
-            'inventory|库存数' => 'require|number|gt:0',
+            'id|商城商品库存id' => 'require|number|gt:0|lt:999999999',
+            'flag|变化方式' => 'require|number|in:1,-1',
+            'number|库存变化数' => 'require|number|gt:0|lt:999999999',
         ]);
 
         if (!$val->check($param)) throw new ValidateException($val->getError());

+ 18 - 16
app/admin/logic/InventoryExchangeLogic.php

@@ -143,23 +143,25 @@ class InventoryExchangeLogic extends BaseLogic
 
             $date = date('Y-m-d H:i:s');
 
-            if ($rs->inventory != $data['inventory']) {
-                Db::name('inventory_exchange_log')->insert([
-                    'inventory_exchange_id' => $data['id'],
-                    'before_inventory' => $rs->inventory,
-                    'after_inventory' => $data['inventory'],
-                    'good_id' => $rs->good_id,
-                    'good_code' => $rs->good_code,
-                    'good_name' => $rs->good_name,
-                    'source' => CommonModel::$source_admin,
-                    'createrid' => self::$uid,
-                    'creater' => self::$uname,
-                    'addtime' => $date,
-                ]);
-            }
-
+            $inventory = bcadd($rs->inventory, bcmul($data['flag'], $data['number']));
+
+            if ($inventory < 0) throw new Exception('最终库存数少于0');
+
+            Db::name('inventory_exchange_log')->insert([
+                'inventory_exchange_id' => $data['id'],
+                'before_inventory' => $rs->inventory,
+                'after_inventory' => $inventory,
+                'flag' => $data['flag'],
+                'good_id' => $rs->good_id,
+                'good_code' => $rs->good_code,
+                'good_name' => $rs->good_name,
+                'source' => CommonModel::$source_admin,
+                'createrid' => self::$uid,
+                'creater' => self::$uname,
+                'addtime' => $date,
+            ]);
             InventoryExchangeModel::where('id', $data['id'])
-                ->save(array_merge($data, ['updatetime' => $date, 'updaterid' => self::$uid, 'updater' => self::$uname]));
+                ->save(['updatetime' => $date, 'updaterid' => self::$uid, 'updater' => self::$uname, 'inventory' => $inventory]);
 
             Db::commit();
 

+ 19 - 17
app/admin/logic/InventoryShoppingLogic.php

@@ -122,23 +122,25 @@ class InventoryShoppingLogic extends BaseLogic
 
             $date = date('Y-m-d H:i:s');
 
-            if ($rs->inventory != $data['inventory']) {
-                Db::name('inventory_shopping_log')->insert([
-                    'inventory_shopping_id' => $data['id'],
-                    'before_inventory' => $rs->inventory,
-                    'after_inventory' => $data['inventory'],
-                    'good_id' => $rs->good_id,
-                    'good_code' => $rs->good_code,
-                    'good_name' => $rs->good_name,
-                    'source' => CommonModel::$source_admin,
-                    'createrid' => self::$uid,
-                    'creater' => self::$uname,
-                    'addtime' => $date,
-                ]);
-
-            }
-
-            InventoryShoppingModel::where('id', $data['id'])->save(array_merge($data, ['updatetime' => $date]));
+            $inventory = bcadd($rs->inventory, bcmul($data['flag'], $data['number']));
+
+            if ($inventory < 0) throw new Exception('最终库存数少于0');
+
+            Db::name('inventory_shopping_log')->insert([
+                'inventory_shopping_id' => $data['id'],
+                'before_inventory' => $rs->inventory,
+                'after_inventory' => $inventory,
+                'flag' => $data['flag'],
+                'good_id' => $rs->good_id,
+                'good_code' => $rs->good_code,
+                'good_name' => $rs->good_name,
+                'source' => CommonModel::$source_admin,
+                'createrid' => self::$uid,
+                'creater' => self::$uname,
+                'addtime' => $date,
+            ]);
+
+            InventoryShoppingModel::where('id', $data['id'])->save(['updatetime' => $date, 'inventory' => $inventory]);
 
             Db::commit();