Kirin 2 years ago
parent
commit
d0b9cad962
2 changed files with 69 additions and 20 deletions
  1. 44 9
      app/admin/controller/company/Goods.php
  2. 25 11
      app/models/system/SystemStoreStock.php

+ 44 - 9
app/admin/controller/company/Goods.php

@@ -8,7 +8,12 @@ use app\models\system\SystemStore;
 use app\models\system\SystemStoreStock;
 use app\models\system\SystemStoreStockBill;
 use app\admin\model\store\{StoreCategory as CategoryModel, StoreProduct as ProductModel, StoreProductAttrValue};
-use crmeb\services\{FormBuilder, JsonService, JsonService as Json, UtilService as Util, FormBuilder as Form};
+use crmeb\services\{FormBuilder,
+    JsonService,
+    JsonService as Json,
+    UtilService as Util,
+    FormBuilder as Form
+};
 use crmeb\traits\CurdControllerTrait;
 use think\facade\Route as Url;
 
@@ -129,6 +134,7 @@ class Goods extends AuthController
         if (!$this->adminInfo['store_id']) return app('json')->fail('门店铺货用通道');
         if ($where['in_stock'] < 1) return app('json')->fail('补货数量不能少于1');
         $info = SystemStoreStock::alias('a')->join("StoreProduct b", "a.product_id=b.id", "left")->where('a.bar_code', $where['bar_code'])->field('a.price,a.unique,a.in_stock,a.is_consumer,a.product_id,b.store_name,b.image,a.bar_code')->find();
+        if (!$info && $where['info'] != 1) return app('json')->fail('门店尚无库存无法减少');
         if (!$info) {
             $store_product = StoreProductAttrValue::where('type', 0)->where('bar_code', $where['bar_code'])->find();
             if (!$store_product)
@@ -202,7 +208,6 @@ class Goods extends AuthController
 
         $this->assign('product_id', $product_id);
         $this->assign('store_id', $store_id);
-
         $this->assign('count', SystemStoreStockBill::where(compact('product_id', 'store_id'))->value('count(id)') ?: 0);
         return $this->fetch();
     }
@@ -251,15 +256,23 @@ class Goods extends AuthController
             ['refuse_msg', ''],
             ['status', 1],
         ]);
-        if (!SystemStoreStockBill::be(['id' => $id, 'status' => 0])) return $this->failed('数据不存在或已经审核');
-        $where['audit_time'] = time();
-        $where['audit_admin'] = $this->adminId;
-        SystemStoreStockBill::edit($where, $id);
+        $rs = true;
         if ($where['status'] == 1) {
+            if (!SystemStoreStockBill::be(['id' => $id, 'status' => 0])) Json::fail('数据不存在或已经审核');
             $info = SystemStoreStockBill::find($id)->toArray();
+            $stock_info = SystemStoreStock::where(['store_id' => $info['store_id'], 'product_id' => $info['product_id'], 'bar_code' => $info['bar_code']])->find();
+            if (!$stock_info && $info['type'] == 2) Json::fail('库存无法减少');
+            if ($info['type'] == 2 && $info['in_stock'] > $stock_info['in_stock']) Json::fail('库存不足');
             $rs = SystemStoreStock::replenish($info['store_id'], $info);
         }
-        return Json::successful('审核成功');
+        if ($rs) {
+            $where['audit_time'] = time();
+            $where['audit_admin'] = $this->adminId;
+            SystemStoreStockBill::edit($where, $id);
+            Json::successful('审核成功');
+        } else {
+            Json::fail(SystemStoreStock::getErrorInfo());
+        }
     }
 
     /**
@@ -272,18 +285,40 @@ class Goods extends AuthController
             ['status', 1],
         ]);
         $ids = explode(',', input('id'));
+        $msg = [];
         if ($where['status'] == 1) {
-            foreach ($ids as $v) {
+            foreach ($ids as $k => $v) {
                 $info = SystemStoreStockBill::where('id', $v)->where('status', 0)->find();
+                if (!$info) {
+                    unset($ids[$k]);
+                    $msg[] = $v . ':数据不存在或已经审核';
+                }
+                $stock_info = SystemStoreStock::where(['store_id' => $info['store_id'], 'product_id' => $info['product_id'], 'bar_code' => $info['bar_code']])->find();
+                if (!$stock_info && $info['type'] == 2) {
+                    unset($ids[$k]);
+                    $msg[] = $v . ':库存无法减少';
+                }
+                if ($info['type'] == 2 && $info['in_stock'] > $stock_info['in_stock']) {
+                    unset($ids[$k]);
+                    $msg[] = $v . ':库存不足';
+                }
                 if ($info) {
                     $rs = SystemStoreStock::replenish($info['store_id'], $info);
+                    if (!$rs) {
+                        unset($ids[$k]);
+                        $msg[] = $v . ':' . SystemStoreStock::getErrorInfo();
+                    }
                 }
             }
         }
         $where['audit_time'] = time();
         $where['audit_admin'] = $this->adminId;
         SystemStoreStockBill::where('id', 'in', join(",", $ids))->update($where);
-        return Json::successful('批量审核成功');
+        if (!count($msg)) {
+            return Json::successful('批量审核成功');
+        } else {
+            return Json::successful('批量审核部分成功' . implode(',', $msg));
+        }
     }
 
 }

+ 25 - 11
app/models/system/SystemStoreStock.php

@@ -68,30 +68,44 @@ class SystemStoreStock extends BaseModel
         if ($data['product_id'] > 0 && $data['in_stock'] > 0) {
             $info = self::where(['store_id' => $store_id, 'product_id' => $data['product_id'], 'bar_code' => $data['bar_code']])->find();
             if ($info) {
-                $add = bcsub($data['in_stock'], $info['repair_sales']);
-                if ($add > 0) {
-                    self::where(['store_id' => $store_id, 'product_id' => $data['product_id'], 'bar_code' => $data['bar_code']])->update(['in_stock' => bcadd($info['in_stock'], $add), 'repair_sales' => 0]);
+                if ($data['type'] == 1) {
+                    $add = bcsub($data['in_stock'], $info['repair_sales']);
+                    if ($add > 0) {
+                        self::where(['store_id' => $store_id, 'product_id' => $data['product_id'], 'bar_code' => $data['bar_code']])->update(['in_stock' => bcadd($info['in_stock'], $add), 'repair_sales' => 0]);
+                    } else {
+                        self::where(['store_id' => $store_id, 'product_id' => $data['product_id'], 'bar_code' => $data['bar_code']])->update(['repair_sales' => bcsub($info['repair_sales'], $data['in_stock'])]);
+                    }
+                    @file_put_contents("stock.txt", self::getlastsql());
                 } else {
-                    self::where(['store_id' => $store_id, 'product_id' => $data['product_id'], 'bar_code' => $data['bar_code']])->update(['repair_sales' => bcsub($info['repair_sales'], $data['in_stock'])]);
+                    if ($info['in_stock'] < $data['in_stock']) {
+                        return self::setErrorInfo('库存不足');
+                    }
+                    $add = bcsub($data['in_stock'], $info['repair_sales']);
+                    if ($add > 0) {
+                        self::where(['store_id' => $store_id, 'product_id' => $data['product_id'], 'bar_code' => $data['bar_code']])->update(['in_stock' => bcadd($info['in_stock'], $add), 'repair_sales' => 0]);
+                    } else {
+                        self::where(['store_id' => $store_id, 'product_id' => $data['product_id'], 'bar_code' => $data['bar_code']])->update(['repair_sales' => bcsub($info['repair_sales'], $data['in_stock'])]);
+                    }
+                    @file_put_contents("stock.txt", self::getlastsql());
                 }
-                @file_put_contents("stock.txt",self::getlastsql());
                 return true;
-            }
-            else
-            {
+            } else {
+                if ($data['type'] == 2) {
+                    return self::setErrorInfo('库存无法减少');
+                }
                 $data1['product_id'] = $data['product_id'];
                 $data1['in_last_time'] = time();
                 $data1['in_stock'] = $data['in_stock'];
-                $data1['unique'] =  StoreProductAttrValue::where('product_id', $data['product_id'])->value('unique');
+                $data1['unique'] = StoreProductAttrValue::where('product_id', $data['product_id'])->value('unique');
                 $data1['price'] = $data['price'];
                 $data1['image'] = $data['image'];
                 $data1['bar_code'] = $data['bar_code'];
-                $data1['is_consumer'] =$data['is_consumer'];
+                $data1['is_consumer'] = $data['is_consumer'];
                 $data1['store_id'] = $store_id;
                 self::create($data1);
             }
         }
-        return false;
+        return self::setErrorInfo('参数异常');
     }
 
     public static function Refund($orderInfo, $data)