shish před 4 roky
rodič
revize
b026fdaa35

+ 12 - 66
app-ghs/controllers/ProductController.php

@@ -186,19 +186,13 @@ class ProductController extends BaseController
     //批量改价
     public function actionBatchChangePrice()
     {
-
         $shopAdmin = $this->shopAdmin;
         if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
             util::fail('超管才能修改');
         }
 
-        //花材价格只能在首店修改,并同步到分店
         $shop = $this->shop;
         $default = $shop->default ?? 0;
-        $join = $shop->join ?? 0;
-        if ($default != 1 && $join == 1) {
-            util::fail('请在首店修改');
-        }
 
         $data = Yii::$app->request->post('data', '');
         if (empty($data)) {
@@ -206,7 +200,7 @@ class ProductController extends BaseController
         }
         $arr = json_decode($data, true);
         if (empty($arr)) {
-            util::fail('没有修改');
+            util::fail('没有修改');
         }
 
         $connection = Yii::$app->db;
@@ -223,9 +217,6 @@ class ProductController extends BaseController
             $chainShopList = [];
             if ($default == 1) {
                 $chainShopList = ShopClass::getAllByCondition(['sjId' => $sjId, 'join' => 0], null, '*', null, true);
-                if (empty($chainShopList)) {
-                    util::fail('没有找到直营店');
-                }
             }
 
             $productList = ProductClass::getAllByCondition(['id' => ['in', $ids]], null, '*', 'id', true);
@@ -250,13 +241,15 @@ class ProductController extends BaseController
                     continue;
                 }
                 $ptItemId = $product->itemId;
+                ProductClass::changeSinglePrice($shop, $ptItemId, $price);
                 //在首店修改需要同步到所有直营店
                 if ($default == 1) {
                     foreach ($chainShopList as $chain) {
-                        ProductClass::changePrice($chain, $ptItemId, $price);
+                        if ($chain->id == $shop->id) {
+                            continue;
+                        }
+                        ProductClass::changeSinglePrice($chain, $ptItemId, $price);
                     }
-                } else {
-                    ProductClass::changePrice($shop, $ptItemId, $price);
                 }
             }
             $transaction->commit();
@@ -278,13 +271,6 @@ class ProductController extends BaseController
             util::fail('超管才能修改');
         }
 
-        //花材价格只能在首店修改,并同步到分店
-        $shop = $this->shop;
-        $default = $shop->default ?? 0;
-        if ($default != 1) {
-            util::fail('请在首店修改');
-        }
-
         $productId = Yii::$app->request->post('productId', 0);
         $productInfo = ProductClass::getById($productId, true);
         if (empty($productInfo)) {
@@ -295,10 +281,10 @@ class ProductController extends BaseController
         }
         $price = Yii::$app->request->post('price', '');
         if (is_numeric($price) && $price > 0) {
-            ProductClass::changePrice($productInfo, $price, ProductClass::PRICE_LABEL_CHANGE);
+            ProductClass::changeSinglePrice($productInfo, $price, ProductClass::PRICE_LABEL_CHANGE);
         } else {
             $price = bcadd($productInfo->cost, $productInfo->addPrice, 2);
-            ProductClass::changePrice($productInfo, $price, ProductClass::PRICE_LABEL_AUTO);
+            ProductClass::changeSinglePrice($productInfo, $price, ProductClass::PRICE_LABEL_AUTO);
         }
         util::success(['price' => $price]);
     }
@@ -313,7 +299,7 @@ class ProductController extends BaseController
         }
         //2021.04.06改为用product中的成本价+加价
         $price = bcadd($productData['cost'], $productData['addPrice'], 2);
-        ProductClass::changePrice($productId, $this->shopId, $price, ProductClass::PRICE_LABEL_AUTO);
+        ProductClass::changeSinglePrice($productId, $this->shopId, $price, ProductClass::PRICE_LABEL_AUTO);
         util::complete("操作成功");
     }
 
@@ -408,13 +394,6 @@ class ProductController extends BaseController
             util::fail('超管才能修改');
         }
 
-        //花材价格只能在首店修改,并同步到分店
-        $shop = $this->shop;
-        $default = $shop->default ?? 0;
-        if ($default != 1) {
-            util::fail('请在首店修改');
-        }
-
         $data = Yii::$app->request->post('data', '');
         $type = Yii::$app->request->post('type', '');
         if (empty($data) || empty($type)) {
@@ -503,20 +482,10 @@ class ProductController extends BaseController
 
                     }
                 }
-                ProductClass::changePrice($product, $val, ProductClass::PRICE_LABEL_CHANGE);
+                ProductClass::changeSinglePrice($product, $val, ProductClass::PRICE_LABEL_CHANGE);
                 continue;
             }
 
-//            $where = [];
-//            $where['id'] = $id;
-//            $where['shopId'] = $this->shopId;
-//            $where['sjId'] = $this->sjId;
-//            if (empty($val)) {
-//                util::fail("参数错误!");
-//            }
-//            $data = [$type => $val];
-//            ProductClass::updateByCondition($where, $data);
-
             $data = [$type => $val];
 
             $currentProduct = ProductClass::getById($id, true);
@@ -544,7 +513,7 @@ class ProductController extends BaseController
             $currentProduct->save();
 
             //2021.7.6 lqh 如果是首店,则更新全部分店
-            ProductClass::updateSyncProduct($id, $this->shopId, $data);
+            ProductClass::updateSyncChainShop($id, $this->shopId, $data);
 
         }
         util::complete();
@@ -556,16 +525,6 @@ class ProductController extends BaseController
         if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
             util::fail('超管才能操作');
         }
-
-        $shop = $this->shop;
-        $default = $shop->default ?? 0;
-        if ($default != 1) {
-            //除了上下架其它只能在首店修改
-            if (isset($post['status']) == false) {
-                util::fail('请在首店修改');
-            }
-        }
-
         $post = Yii::$app->request->post();
         $post['adminId'] = $this->adminId;
         ProductClass::updateProduct($post, $this->shop);
@@ -638,13 +597,6 @@ class ProductController extends BaseController
     {
         $post = Yii::$app->request->post();
 
-        $shop = $this->shop;
-        $default = $shop->default ?? 0;
-        $join = $shop->join ?? 0;
-        if ($join == 1 && $default != 1) {
-            util::fail('直营店暂无权限操作');
-        }
-
         $shopAdmin = $this->shopAdmin;
         if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
             util::fail('管理员才能添加花材');
@@ -660,7 +612,7 @@ class ProductController extends BaseController
         }
         $cost = bcsub($price, $addPrice, 2);
         $post['cost'] = $cost;
-        ProductClass::addProduct($post);
+        ProductClass::addProduct($post, $this->shop);
         util::complete('添加成功');
     }
 
@@ -723,12 +675,6 @@ class ProductController extends BaseController
             util::fail('超管才能添加花材');
         }
 
-        $shop = $this->shop;
-        $default = $shop->default ?? 0;
-        if ($default != 1) {
-            util::fail('请在首店添加');
-        }
-
         $id = Yii::$app->request->get('id');
         $productData = ProductClass::getById($id);
         if (!$productData) {

+ 4 - 4
app-ghs/controllers/PurchaseOrderController.php

@@ -20,10 +20,10 @@ class PurchaseOrderController extends BaseController
         $post['shopId'] = $this->shopId;
         $post['adminId'] = $this->adminId;
         $post['mainId'] = $this->mainId;
-        $shop = $this->shop;
-        $default = $shop->default ?? 0;
-        if ($default != 1) {
-            util::fail('请在首店发起采购');
+
+        $shopAdmin = $this->shopAdmin;
+        if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
+            util::fail('超管才能修改');
         }
 
         // [{productId:0,itemPrice:1,bigNum:1,smallNum:0}]

+ 2 - 21
app-hd/controllers/ProductController.php

@@ -1,4 +1,5 @@
 <?php
+
 namespace hd\controllers;
 
 use biz\ghs\classes\GhsClass;
@@ -28,13 +29,6 @@ class ProductController extends BaseController
             util::fail('请在批发端添加');
         }
 
-        $shop = $this->shop;
-        $default = $shop->default ?? 0;
-        $join = $shop->join ?? 0;
-        if ($join == 1 && $default != 1) {
-            util::fail('直营店暂无权限操作');
-        }
-
         $shopAdmin = $this->shopAdmin;
         if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
             util::fail('管理员才能添加花材');
@@ -178,13 +172,6 @@ class ProductController extends BaseController
             util::fail('超管才能修改');
         }
 
-        //花材价格只能在首店修改,并同步到分店
-        $shop = $this->shop;
-        $default = $shop->default ?? 0;
-        if ($default != 1) {
-            util::fail('请在首店修改');
-        }
-
         $data = Yii::$app->request->post('data', '');
         if (empty($data)) {
             util::fail('没有修改');
@@ -226,7 +213,7 @@ class ProductController extends BaseController
                 if (empty($price)) {
                     continue;
                 }
-                ProductClass::changePrice($product, $price, ProductClass::PRICE_LABEL_CHANGE);
+                ProductClass::changeSinglePrice($product, $price, ProductClass::PRICE_LABEL_CHANGE);
             }
             $transaction->commit();
             util::complete('修改成功');
@@ -298,12 +285,6 @@ class ProductController extends BaseController
             util::fail('请在批发端修改');
         }
 
-        $default = $shop->default ?? 0;
-        $join = $shop->join ?? 0;
-        if ($join == 1 && $default != 1) {
-            util::fail('直营店暂无权限操作');
-        }
-
         $post = Yii::$app->request->post();
         $shopAdmin = $this->shopAdmin;
         if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {

+ 134 - 115
biz-ghs/product/classes/ProductClass.php

@@ -551,11 +551,15 @@ class ProductClass extends BaseClass
         return ProductClass::getByCondition(['id' => $id, 'mainId' => $mainId], $obj);
     }
 
-    //修改价格 lqh 2021.1.28
-    public static function changePrice($shop, $ptItemId, $price)
+    //单个修改价格 lqh 2021.1.28
+    public static function changeSinglePrice($shop, $ptItemId, $price)
     {
         $mainId = $shop->mainId ?? 0;
         $productInfo = ProductClass::getByCondition(['itemId' => $ptItemId, 'mainId' => $mainId], true);
+        if (empty($productInfo)) {
+            return false;
+        }
+
         $productInfo->price = $price;
 
         $addPrice = $productInfo->addPrice ?? 0;
@@ -575,35 +579,6 @@ class ProductClass extends BaseClass
         $productInfo->save();
     }
 
-    //2021.7.6 首店修改同步到其它的直营店
-    public static function updateSyncProduct($productId, $shop, $upData)
-    {
-        $shopId = $shop->id ?? 0;
-        $productData = ProductClass::getById($productId);
-        $ptItemId = $productData['itemId'];
-        if (isset($shop->default) == false || $shop->default != 1) {
-            return false;
-        }
-        $sjId = $shop->sjId;
-        $shopList = ShopClass::getAllByCondition(['sjId' => $sjId], null, '*', null, true);
-        if (empty($shopList)) {
-            return false;
-        }
-        foreach ($shopList as $current) {
-            if ($current->id == $shopId) {
-                continue;
-            }
-            //加盟店不需要同步
-            if (isset($current->join) && $current->join == 1) {
-                continue;
-            }
-            $mainId = $current->mainId ?? 0;
-            //前面已经修改价格
-            unset($upData['price']);
-            ProductClass::updateByCondition(['itemId' => $ptItemId, 'mainId' => $mainId], $upData);
-        }
-    }
-
     //下单时计算商品的价格  lqh 2021.1.28
     public static function formatProductInfo($itemInfo, $level = 0)
     {
@@ -744,6 +719,7 @@ class ProductClass extends BaseClass
     public static function updateProduct($data, $shop)
     {
         $id = $data['id'] ?? 0;
+        $sjId = $shop->sjId ?? 0;
         $product = self::getLockById($id);
         if (empty($product)) {
             util::fail("该花材不存在");
@@ -839,21 +815,47 @@ class ProductClass extends BaseClass
             $upData['classId'] = $classId;
         }
 
-        unset($data['id']);
-        if (empty($upData)) {
-            util::fail("参数错误");
+        $paramsBack = $upData;
+        $ptItemId = $product->itemId ?? 0;
+
+        //修改价格
+        $price = $upData['price'] ?? 0;
+        if ($price != $product->price) {
+            self::changeSinglePrice($shop, $ptItemId, $price);
+            unset($upData['price']);
         }
+        //修改其它
+        self::updateById($id, $upData);
 
-        $currentData = $upData;
-        //当前花材上下架状态
-        if (isset($data['status'])) {
-            $currentData['status'] = $data['status'];
+        if (isset($shop->default) && $shop->default == 1) {
+            //首店修改同步到所有直营店
+            $chainShopList = ShopClass::getAllByCondition(['sjId' => $sjId], null, '*', null, true);
+            foreach ($chainShopList as $chainShop) {
+                $chainShopId = $chainShop->id ?? 0;
+                if (isset($chainShop->join) && $chainShop->join == 1) {
+                    continue;
+                }
+                if ($chainShopId == $shop->id) {
+                    //前面已经添加过了
+                    continue;
+                }
+                $params = $paramsBack;
+                //所有直营店的分类不修改
+                unset($params['classId']);
+                $chainMainId = $chainShop->mainId ?? 0;
+                $chainProduct = ProductClass::getByCondition(['mainId' => $chainMainId, 'itemId' => $ptItemId], true);
+                if (empty($chainProduct)) {
+                    continue;
+                }
+                $price = $params['price'] ?? 0;
+                if ($price != $chainProduct->price) {
+                    self::changeSinglePrice($chainShop, $ptItemId, $price);
+                    unset($params['price']);
+                }
+                self::updateById($chainProduct->id, $params);
+            }
         }
-        unset($currentData['price']);
-        self::updateById($id, $currentData);
 
-        //2021.7.6 lqh 如果是首店,则更新全部分店
-        self::updateSyncProduct($id, $shop, $upData);
     }
 
     //变成自动改价 2021.4.14
@@ -983,8 +985,9 @@ class ProductClass extends BaseClass
     }
 
     //新增花材
-    public static function addProduct($data)
+    public static function addProduct($data, $currentShop)
     {
+        $currentMainId = $currentShop->mainId ?? 0;
         $classId = $data['classId'] ?? 0;
         $mainId = $data['mainId'] ?? 0;
         $stockWarning = $data['stockWarning'] ?? false;
@@ -1095,78 +1098,94 @@ class ProductClass extends BaseClass
             $data['skPrice'] = $skPrice;
             $data['hjPrice'] = $hjPrice;
 
-            //找到父级sj下的所有直营店,添加花材
-            $shopList = ShopClass::getAllByCondition(['sjId' => $data['sjId']], null, '*', null, true);
-            foreach ($shopList as $shop) {
-                if (isset($shop->join) && $shop->join == 1) {
-                    continue;
-                }
-                $shopId = $shop->id ?? 0;
-                $currentMainId = $shop->mainId ?? 0;
-                $data['shopId'] = $shopId;
-                $data['mainId'] = $currentMainId;
-                //非当前门店库存默认为0并且是下架状态
-                if ($currentShopId != $shopId) {
-                    $data['stock'] = 0;
-                    $data['status'] = 2;
-                }
-                $res = self::add($data);
-                if ($stock > 0 && $res && $currentShopId == $shopId) {
-                    //有库存数量,增加盘盈记录 2021.7.9 lqh
-                    //增加盘点订单
-                    $pdOrderSn = orderSn::getGhsCheckSn();
-                    $pdOrderData = [];
-                    $pdOrderData['orderSn'] = $pdOrderSn;
-                    $pdOrderData['sjId'] = $data['sjId'];
-                    $pdOrderData['shopId'] = $data['shopId'];
-                    $pdOrderData['status'] = CheckOrderClass::STATUS_COMPLETE;
-                    $pdOrderData['bigNum'] = $stock;
-                    $pdOrderData['adminId'] = $data['adminId'];
-                    $pdOrderData['smallNum'] = 0;
-                    $pdOrderData['price'] = $data['price'];
-                    $pdOrderData['remark'] = '添加花材时初始化库存';
-                    CheckOrderClass::add($pdOrderData);
-
-                    $pdOrderItemData = [];
-                    $pdOrderItemData['orderSn'] = $pdOrderSn;
-                    $pdOrderItemData['productId'] = $res['id'];
-                    $pdOrderItemData['itemId'] = $data['itemId'];
-                    $pdOrderItemData['itemId'] = $data['itemId'];
-                    $pdOrderItemData['itemNum'] = $stock;
-                    $pdOrderItemData['itemStock'] = 0;
-                    $pdOrderItemData['profitLostNum'] = $stock;
-                    $itemInfo = [
-                        'itemName' => $name,
-                        'itemCover' => $data['cover'],
-                        'bigNum' => $stock,
-                        'smallNum' => 0,
-                        'bigNumStock' => 0,
-                        'smallNumStock' => 0,
-                        'bigNumProfitLoss' => $stock,
-                        'smallNumProfitLoss' => 0,
-                        'itemUnit' => 0,
-                        'ratio' => $ratio,
-                        'rank' => 'A',
-                        'bigUnit' => $data['bigUnit'],
-                        'smallUnit' => $data['smallUnit'],
-                    ];
-                    $pdOrderItemData['itemInfo'] = json_encode($itemInfo);
-                    $pdOrderItemData['itemPrice'] = $data['price'];
-                    $pdOrderItemData['itemCost'] = $data['cost'];
-                    CheckOrderItemClass::add($pdOrderItemData);
-
-                    $record = [];
-                    $record['sjId'] = $data['sjId'];
-                    $record['shopId'] = $data['shopId'];
-                    //增加中央ID
-                    $record['mainId'] = $currentMainId;
-                    $record['orderSn'] = $pdOrderSn;
-                    $record['itemId'] = $data['itemId'];
-                    $record['productId'] = $res['id'];
-                    $record['itemNum'] = $stock;//盈亏数
-                    $record['oldStock'] = 0;
-                    $record['newStock'] = $stock; //新库存直接等于盘点的数量
-                    StockRecordClass::addCheckOrderRecord($record);
+            $respond = self::add($data, true);
+
+            if ($stock > 0) {
+                //有库存数量,增加盘盈记录 2021.7.9 lqh
+                //增加盘点订单
+                $pdOrderSn = orderSn::getGhsCheckSn();
+                $pdOrderData = [];
+                $pdOrderData['orderSn'] = $pdOrderSn;
+                $pdOrderData['sjId'] = $data['sjId'];
+                $pdOrderData['shopId'] = $data['shopId'];
+                $pdOrderData['status'] = CheckOrderClass::STATUS_COMPLETE;
+                $pdOrderData['bigNum'] = $stock;
+                $pdOrderData['adminId'] = $data['adminId'];
+                $pdOrderData['smallNum'] = 0;
+                $pdOrderData['price'] = $data['price'];
+                $pdOrderData['remark'] = '添加花材时初始化库存';
+                CheckOrderClass::add($pdOrderData);
+
+                $pdOrderItemData = [];
+                $pdOrderItemData['orderSn'] = $pdOrderSn;
+                $pdOrderItemData['productId'] = $respond->id ?? 0;
+                $pdOrderItemData['itemId'] = $data['itemId'];
+                $pdOrderItemData['itemId'] = $data['itemId'];
+                $pdOrderItemData['itemNum'] = $stock;
+                $pdOrderItemData['itemStock'] = 0;
+                $pdOrderItemData['profitLostNum'] = $stock;
+                $itemInfo = [
+                    'itemName' => $name,
+                    'itemCover' => $data['cover'],
+                    'bigNum' => $stock,
+                    'smallNum' => 0,
+                    'bigNumStock' => 0,
+                    'smallNumStock' => 0,
+                    'bigNumProfitLoss' => $stock,
+                    'smallNumProfitLoss' => 0,
+                    'itemUnit' => 0,
+                    'ratio' => $ratio,
+                    'rank' => 'A',
+                    'bigUnit' => $data['bigUnit'],
+                    'smallUnit' => $data['smallUnit'],
+                ];
+                $pdOrderItemData['itemInfo'] = json_encode($itemInfo);
+                $pdOrderItemData['itemPrice'] = $data['price'];
+                $pdOrderItemData['itemCost'] = $data['cost'];
+                CheckOrderItemClass::add($pdOrderItemData);
+
+                $record = [];
+                $record['sjId'] = $data['sjId'];
+                $record['shopId'] = $data['shopId'];
+                //增加中央ID
+                $record['mainId'] = $currentMainId;
+                $record['orderSn'] = $pdOrderSn;
+                $record['itemId'] = $data['itemId'];
+                $record['productId'] = $respond->id ?? 0;
+                $record['itemNum'] = $stock;//盈亏数
+                $record['oldStock'] = 0;
+                $record['newStock'] = $stock; //新库存直接等于盘点的数量
+                StockRecordClass::addCheckOrderRecord($record);
+            }
+
+            //在首店添加,则找到父级sj下的所有直营店,同步添加花材
+            if (isset($currentShop->default) && $currentShop->default == 1) {
+                $shopList = ShopClass::getAllByCondition(['sjId' => $data['sjId']], null, '*', null, true);
+                foreach ($shopList as $shop) {
+                    $shopId = $shop->id ?? 0;
+                    if (isset($shop->join) && $shop->join == 1) {
+                        continue;
+                    }
+                    if ($shopId == $currentShop->id) {
+                        //前面已经添加过了
+                        continue;
+                    }
+                    $thisMainId = $shop->mainId ?? 0;
+                    $data['shopId'] = $shopId;
+                    $data['mainId'] = $thisMainId;
+                    //不是当前门店,库存默认为0并且是下架状态
+                    //不是当前门店,花材分类放在默认分类下
+                    if ($currentShopId != $shopId) {
+                        $data['stock'] = 0;
+                        $data['status'] = 2;
+                        $defaultCat = ItemClassClass::getByCondition(['mainId' => $thisMainId, 'isDefault' => 1], true);
+                        $defaultCatId = $defaultCat->id ?? 0;
+                        if (empty($defaultCatId)) {
+                            util::fail($shop->shopName . "没有默认分类");
+                        }
+                        $data['classId'] = $defaultCatId;
+                    }
+                    self::add($data);
                 }
             }
             $transaction->commit();