Browse Source

Merge branch 'zhongqi-stockLogic'

shish 1 year ago
parent
commit
09a6fd40c5

+ 0 - 2
app-ghs/controllers/RefundController.php

@@ -10,11 +10,9 @@ use bizHd\cg\classes\CgRefundClass;
 use bizHd\cg\classes\CgRefundItemClass;
 use bizHd\notice\classes\NoticeClass;
 use bizHd\purchase\classes\PurchaseClass;
-use bizHd\purchase\classes\PurchaseClearClass;
 use common\components\dateUtil;
 use common\components\dict;
 use common\components\imgUtil;
-use common\components\stringUtil;
 use Yii;
 use bizGhs\order\classes\RefundOrderClass;
 use common\components\util;

+ 4 - 16
app-hd/controllers/NoticeController.php

@@ -142,7 +142,7 @@ class NoticeController extends PublicController
                 $saleId = $cg->saleId ?? 0;
                 $order = OrderClass::getById($saleId, true);
 
-                //不是预订单,并且采购人没有开批发店,供货商不是源花汇、小明鲜花、淄博花超、云漫梦金鹏、海翔,则订单直接完成掉。是预订单,则确认发货时直接完成掉。有多个地方要修改,请搜索关键词zjFinish
+                //不是预订单,并且供货商不是源花汇、小明鲜花、淄博花超、云漫梦金鹏、海翔,则订单直接完成掉。是预订单,则确认发货时直接完成掉。有多个地方要修改,请搜索关键词zjFinish
                 if (!empty($cg) && !empty($order)) {
                     if (isset($cg->book) && $cg->book == 0) {
                         $ghsShopId = $order->shopId ?? 0;
@@ -152,13 +152,7 @@ class NoticeController extends PublicController
                             $specialList = [36523];
                         }
                         if (!in_array($ghsShopId, $specialList)) {
-                            $cgShopId = $cg->shopId ?? 0;
-                            $cgShop = ShopClass::getById($cgShopId, true);
-                            if (!empty($cgShop)) {
-                                if (isset($cgShop->pfShopId) && $cgShop->pfShopId == 0) {
-                                    PurchaseClass::confirmTake($cg);
-                                }
-                            }
+                            PurchaseClass::confirmTake($cg);
                         }
                     }
                 }
@@ -371,7 +365,7 @@ class NoticeController extends PublicController
                 $saleId = $cg->saleId ?? 0;
                 $order = OrderClass::getById($saleId, true);
 
-                //不是预订单,并且采购人没有开批发店,供货商不是源花汇、小明鲜花、淄博花超、云漫梦金鹏、海翔、镜中花卉,则订单直接完成掉。是预订单,则确认发货时直接完成掉。有多个地方要修改,请搜索关键词zjFinish
+                //不是预订单,并且供货商不是源花汇、小明鲜花、淄博花超、云漫梦金鹏、海翔、镜中花卉,则订单直接完成掉。是预订单,则确认发货时直接完成掉。有多个地方要修改,请搜索关键词zjFinish
                 if (!empty($cg) && !empty($order)) {
                     if (isset($cg->book) && $cg->book == 0) {
                         $ghsShopId = $order->shopId ?? 0;
@@ -381,13 +375,7 @@ class NoticeController extends PublicController
                             $specialList = [36523];
                         }
                         if (!in_array($ghsShopId, $specialList)) {
-                            $cgShopId = $cg->shopId ?? 0;
-                            $cgShop = ShopClass::getById($cgShopId, true);
-                            if (!empty($cgShop)) {
-                                if (isset($cgShop->pfShopId) && $cgShop->pfShopId == 0) {
-                                    PurchaseClass::confirmTake($cg);
-                                }
-                            }
+                            PurchaseClass::confirmTake($cg);
                         }
                     }
                 }

+ 12 - 3
app-hd/controllers/ProductController.php

@@ -10,13 +10,13 @@ use bizGhs\item\classes\ItemClassClass;
 use bizGhs\product\classes\ProductClass;
 use bizGhs\product\services\ProductService;
 use bizHd\admin\classes\ShopAdminClass;
-use bizHd\item\classes\ItemClass;
 use bizHd\purchase\classes\PurchaseClass;
-use common\components\noticeUtil;
+use bizHd\purchase\classes\PurchaseItemClass;
 use common\components\printUtil;
 use common\components\stringUtil;
 use common\components\util;
 use Yii;
+use yii\helpers\ArrayHelper;
 
 class ProductController extends BaseController
 {
@@ -400,7 +400,6 @@ class ProductController extends BaseController
         $connection = Yii::$app->db;
         $transaction = $connection->beginTransaction();
         try {
-
             if ($doType == 0 || $doType == 1) {
                 $cg = PurchaseClass::getLockById($targetId);
                 if (!empty($cg)) {
@@ -416,7 +415,17 @@ class ProductController extends BaseController
                     }
                     //确认收货并入库
                     if ($cg->status == PurchaseClass::STATUS_SENDING) {
+                        // 构建“冲销”函数的参数 -- 变动前的花材存库数据
+                        $purchaseItems = PurchaseItemClass::getAllByCondition(['orderSn' => $cg->orderSn], null, "productId");
+                        $productIds = ArrayHelper::getColumn($purchaseItems, 'productId'); //$productIds 是本次入库涉及的花材ID数组
+                        $oldStocks = [];
+                        foreach ($productIds as $productId) {
+                            $product = ProductClass::getById($productId, true);
+                            $oldStocks[$productId] = $product;
+                        }
                         PurchaseClass::takeToPutIn($cg);
+                        // “冲销”本次入库的数量
+                        PurchaseClass::rollbackProductStock($oldStocks, $shop, $adminId, $this->mainId, $this->shopAdmin->name);
                         if ($doType == 1) {
                             $transaction->commit();
                             util::complete('入库成功');

+ 2 - 8
app-hd/controllers/PurchaseController.php

@@ -736,7 +736,7 @@ class PurchaseController extends BaseController
                 }
             }
 
-            //不是预订单,并且采购人没有开批发店,供货商不是源花汇、小明鲜花、淄博花超、云漫梦金鹏、海翔,则订单直接完成掉。是预订单,则确认发货时直接完成掉。有多个地方要修改,请搜索关键词zjFinish
+            //不是预订单,并且供货商不是源花汇、小明鲜花、淄博花超、云漫梦金鹏、海翔,则订单直接完成掉。是预订单,则确认发货时直接完成掉。有多个地方要修改,请搜索关键词zjFinish
             $cg = PurchaseClass::getByCondition(['orderSn' => $orderSn], true);
             if (!empty($cg)) {
                 if (isset($cg->book) && $cg->book == 0) {
@@ -747,13 +747,7 @@ class PurchaseController extends BaseController
                         $specialList = [36523];
                     }
                     if (!in_array($ghsShopId, $specialList)) {
-                        $cgShopId = $cg->shopId ?? 0;
-                        $cgShop = ShopClass::getById($cgShopId, true);
-                        if (!empty($cgShop)) {
-                            if (isset($cgShop->pfShopId) && $cgShop->pfShopId == 0) {
-                                PurchaseClass::confirmTake($cg);
-                            }
-                        }
+                        PurchaseClass::confirmTake($cg);
                     }
                 }
             }

+ 0 - 1
biz-ghs/order/classes/CheckOrderClass.php

@@ -289,7 +289,6 @@ class CheckOrderClass extends BaseClass
             }
         }
 
-
         if ($draft) {
             //保存草稿
             $data['status'] = self::STATUS_DRAFT;

+ 17 - 11
biz-ghs/order/classes/OrderClass.php

@@ -3,6 +3,7 @@
 namespace bizGhs\order\classes;
 
 use bizGhs\item\classes\ItemClassClass;
+use bizHd\purchase\classes\PurchaseItemClass;
 use wkhtmltox\Image\Converter;
 use biz\shop\classes\ShopAdminClass;
 use biz\shop\classes\ShopCapitalClass;
@@ -42,6 +43,7 @@ use common\components\stringUtil;
 use common\components\util;
 use bizGhs\base\classes\BaseClass;
 use Yii;
+use yii\helpers\ArrayHelper;
 
 class OrderClass extends BaseClass
 {
@@ -378,17 +380,6 @@ class OrderClass extends BaseClass
         if (empty($customShop)) {
             util::fail('没有找到客户的门店');
         }
-        if (isset($customShop->pfShopId) && !empty($customShop->pfShopId)) {
-            //如果客户是勇记花卉 森润 国恋 珊瑚 中华 寻花季 还是要直接完成,请搜索关键词 has_pf_direct_finish
-            if (getenv('YII_ENV') == 'production') {
-                $particularIds = [7779, 7832, 10610, 7779, 16780, 716, 8394];
-            } else {
-                $particularIds = [36750];
-            }
-            if (!in_array($customShop->id, $particularIds)) {
-                util::fail('客户有批发店,请通知对方确认');
-            }
-        }
         //订单如果没有发货则先发货
         if ($order->status == 2) {
             $staffId = $params['staffId'] ?? 0;
@@ -397,7 +388,22 @@ class OrderClass extends BaseClass
             self::orderSend($order, $element, true);
             $cg = PurchaseClass::getById($cgId, true);
         }
+        // 构建“冲销”函数的参数 -- 变动前的花材存库数据
+        $purchaseItems = PurchaseItemClass::getAllByCondition(['orderSn' => $cg->orderSn], null, "productId");
+        $productIds = ArrayHelper::getColumn($purchaseItems, 'productId'); //$productIds 是本次入库涉及的花材ID数组
+        $oldStocks = [];
+        foreach ($productIds as $productId) {
+            $product = ProductClass::getById($productId, true);
+            $oldStocks[$productId] = $product;
+        }
         PurchaseClass::takeToPutIn($cg);
+        $shop = ShopClass::getLockById($shopId);
+        if (empty($shop)) {
+            util::fail('没有找到门店14');
+        }
+        $adminId = $shop->adminId;
+        $mainId = $shop->mainId;
+        PurchaseClass::rollbackProductStock($oldStocks, $shop, $adminId, $mainId, '系统');
     }
 
     //订单发货主方法 ssh 20231119

+ 8 - 23
biz-ghs/order/services/OrderService.php

@@ -131,7 +131,7 @@ class OrderService extends BaseService
             }
         }
 
-        //不是预订单,并且采购人没有开批发店,供货商不是源花汇、小明鲜花、淄博花超、云漫梦金鹏、海翔,则订单直接完成掉。是预订单,则确认发货时直接完成掉。有多个地方要修改,请搜索关键词zjFinish
+        //不是预订单,并且供货商不是源花汇、小明鲜花、淄博花超、云漫梦金鹏、海翔,则订单直接完成掉。是预订单,则确认发货时直接完成掉。有多个地方要修改,请搜索关键词zjFinish
         if (!empty($cg) && !empty($order)) {
             $ghsShopId = $order->shopId ?? 0;
             if (getenv('YII_ENV') == 'production') {
@@ -140,14 +140,8 @@ class OrderService extends BaseService
                 $specialList = [36523];
             }
             if (!in_array($ghsShopId, $specialList)) {
-                $cgShopId = $cg->shopId ?? 0;
-                $cgShop = ShopClass::getById($cgShopId, true);
-                if (!empty($cgShop)) {
-                    if (isset($cgShop->pfShopId) && $cgShop->pfShopId == 0) {
-                        $newCg = PurchaseClass::getById($cgId, true);
-                        PurchaseClass::confirmTake($newCg);
-                    }
-                }
+                $newCg = PurchaseClass::getById($cgId, true);
+                PurchaseClass::confirmTake($newCg);
             }
         }
 
@@ -314,7 +308,7 @@ class OrderService extends BaseService
                 CustomDebtChangeClass::updateByCondition(['capitalType' => 10, 'relateId' => $returnOrder->id], ['payTime' => $currentPayTime]);
             }
 
-            //不是预订单,并且采购人没有开批发店,供货商不是源花汇、小明鲜花、淄博花超、云漫梦金鹏、海翔,则订单直接完成掉。是预订单,则确认发货时直接完成掉。有多个地方要修改,请搜索关键词zjFinish
+            //不是预订单,并且供货商不是源花汇、小明鲜花、淄博花超、云漫梦金鹏、海翔,则订单直接完成掉。是预订单,则确认发货时直接完成掉。有多个地方要修改,请搜索关键词zjFinish
             if (isset($returnOrder->book) && $returnOrder->book == 0) {
                 $ghsShopId = $ghs['shopId'] ?? 0;
                 if (getenv('YII_ENV') == 'production') {
@@ -323,19 +317,10 @@ class OrderService extends BaseService
                     $specialList = [36523];
                 }
                 if (!in_array($ghsShopId, $specialList)) {
-                    $customShop = ShopClass::getById($customShopId, true);
-                    //如果客户是勇记花卉 森润 国恋 珊瑚 中华 寻花季 还是要直接完成,请搜索关键词 has_pf_direct_finish
-                    if (getenv('YII_ENV') == 'production') {
-                        $particularIds = [7779, 7832, 10610, 7779, 16780, 716, 8394];
-                    } else {
-                        $particularIds = [36750];
-                    }
-                    if ((isset($customShop->pfShopId) && $customShop->pfShopId == 0) || in_array($customShopId, $particularIds)) {
-                        $returnId = $returnOrder->id;
-                        $order = OrderClass::getById($returnId, true);
-                        if (!empty($order)) {
-                            OrderClass::confirmReach($order);
-                        }
+                    $returnId = $returnOrder->id;
+                    $order = OrderClass::getById($returnId, true);
+                    if (!empty($order)) {
+                        OrderClass::confirmReach($order);
                     }
                 }
             }

+ 1 - 0
biz-ghs/order/services/RefundOrderService.php

@@ -106,6 +106,7 @@ class RefundOrderService extends BaseService
         if (empty($order)) {
             util::fail('没有订单信息哦');
         }
+
         RefundOrderClass::passRefund($refund, $order);
         $cgRefundId = $refund->cgRefundId ?? 0;
         $cgRefund = CgRefundClass::getById($cgRefundId, true);

+ 36 - 1
biz-hd/cg/classes/CgRefundClass.php

@@ -101,7 +101,6 @@ class CgRefundClass extends BaseClass
         $cgRefund->status = 1;
         $cgRefund->save();
 
-        $cgId = $cg->id ?? 0;
         $orderSn = $cg->orderSn ?? '';
         $mainId = $cg->mainId ?? 0;
         $shopId = $cg->shopId ?? 0;
@@ -126,6 +125,42 @@ class CgRefundClass extends BaseClass
         if (empty($main)) {
             util::fail('没有main信息7');
         }
+
+        // 处理退货退款前,把库存先补回。(本次哪些花材要退多少,就补回多少)
+        if ($refundType == CgRefundClass::REFUND_TYPE_MONEY_GOOD) {
+            $field = 'id, cgItemId, itemNum, xhNum';
+            $refundItemList = CgRefundItemClass::getAllByCondition(['orderSn' => $refundSn], null, $field, null, true);
+            if (empty($refundItemList)) {
+                util::fail('没有找到售后订单的花材哦');
+            }
+            $refundList = [];
+            foreach ($refundItemList as $val) {
+                $cgItemId = $val->cgItemId;
+                if (empty($cgItemId)) {
+                    util::fail('旧订单无法售后,请联系管理员哦');
+                }
+                $refundList[$cgItemId] = $val;
+            }
+            $field = 'id, xhNum, productId';
+            $cgItemList = PurchaseItemClass::getAllByCondition(['orderSn' => $cg->orderSn], null, $field, null, true);
+            if (empty($cgItemList)) {
+                util::fail('采购单花材错误');
+            }
+
+            $itemDatas = [];
+            foreach ($cgItemList as $cgItem) {
+                if (isset($refundList[$cgItem->id])) {
+                    $refund = $refundList[$cgItem->id];
+                    $product = ProductClass::getById($cgItem->productId, true);
+                    $itemDatas[$cgItem->productId] = ['product' => $product, 'addNum' => $refund->itemNum]; // TODO 使用 itemNum 合适,还是 xhNum ?
+                }
+            }
+            $cgShop = $shop;//采购门店
+            $adminId = $cgShop->adminId;
+            // 补回本次退货的花材数量
+            PurchaseClass::increaseProductStock($itemDatas, $cgShop, $adminId, $cg->mainId, '系统');
+        }
+
         if ($refundType == CgRefundClass::REFUND_TYPE_MONEY_GOOD) {
             $refundItemList = CgRefundItemClass::getAllByCondition(['orderSn' => $refundSn], null, '*', null, true);
             if (empty($refundItemList)) {

+ 0 - 1
biz-hd/cg/services/CgRefundService.php

@@ -244,7 +244,6 @@ class CgRefundService extends BaseService
         }
 
         return $cgRefund;
-
     }
 
 }

+ 82 - 1
biz-hd/purchase/classes/PurchaseClass.php

@@ -36,6 +36,7 @@ use common\components\util;
 use bizHd\base\classes\BaseClass;
 use common\components\lakala\Lakala;
 use Yii;
+use yii\helpers\ArrayHelper;
 
 class PurchaseClass extends BaseClass
 {
@@ -177,7 +178,22 @@ class PurchaseClass extends BaseClass
         if ($cg->status == self::STATUS_UN_SEND) {
             $cg = self::orderSend($cg, [], true);
         }
-        self::takeToPutIn($cg);
+
+        // 构建“冲销”函数的参数 -- 变动前的花材存库数据
+        $purchaseItems = PurchaseItemClass::getAllByCondition(['orderSn' => $cg->orderSn], null, "productId");
+        $productIds = ArrayHelper::getColumn($purchaseItems, 'productId'); //$productIds 是本次入库涉及的花材ID数组
+        $oldStocks = [];
+        foreach ($productIds as $productId) {
+            $product = ProductClass::getById($productId, true);
+            $oldStocks[$productId] = $product;
+        }        self::takeToPutIn($cg);
+        $shop = ShopClass::getLockById($cg->shopId);
+        if (empty($shop)) {
+            util::fail('没有找到门店14');
+        }
+        $adminId = $shop->adminId;
+        $mainId = $shop->mainId;
+        PurchaseClass::rollbackProductStock($oldStocks, $shop, $adminId, $mainId, '系统');
     }
 
     //确认收货并入库 ssh 20231119
@@ -275,6 +291,71 @@ class PurchaseClass extends BaseClass
         $cg->save();
     }
 
+    // 库存回滚为入库前的状态
+    public static function rollbackProductStock($oldStocks, $shop, $adminId, $mainId, $staffName)
+    {
+        $itemInfo = [];
+        foreach ($oldStocks as $productId => $product) {
+            $ratio = $product->ratio;
+            $formNum = ProductClass::formatStock($product->stock, $ratio); //花材的库存转换
+            $itemInfo[] = [
+                'productId' => $productId,
+                'itemNum'   => $product->stock, // 回滚为入库前的库存
+                'itemId'    => $product->itemId,
+                'bigNum'    => $formNum['bigNum'],
+                'smallNum'  => $formNum['smallNum'],
+                'price'     => $product->price,
+            ];
+        }
+
+        $data = [
+            'sjId' => $shop->sjId,
+            'shopId' => $shop->id,
+            'adminId' => $adminId,
+            'mainId' => $mainId,
+            'staffName' => $staffName,
+            'itemInfo' => $itemInfo,
+            'pdType' => 'rollbackStock',
+            'remark' => '',
+        ];
+
+        \bizGhs\order\classes\CheckOrderClass::opOrder($data, false, false, false);
+    }
+
+    // 指定花材新增库存
+    public static function increaseProductStock($itemDatas, $shop, $adminId, $mainId, $staffName)
+    {
+        $itemInfo = [];
+        foreach ($itemDatas as $productId => $arr) {
+            $addNum = $arr['addNum']; //新增的数值
+            $product = $arr['product'];
+            $ratio = $product->ratio;
+            $newStock = bcadd($product->stock, $addNum, 2);
+            $formNum = ProductClass::formatStock($newStock, $ratio); //花材的库存转换
+            $itemInfo[] = [
+                'productId' => $productId,
+                'itemNum'   => $newStock, // 新库存数
+                'itemId'    => $product->itemId,
+                'bigNum'    => $formNum['bigNum'],
+                'smallNum'  => $formNum['smallNum'],
+                'price'     => $product->price,
+            ];
+        }
+
+        $data = [
+            'sjId' => $shop->sjId,
+            'shopId' => $shop->id,
+            'adminId' => $adminId,
+            'mainId' => $mainId,
+            'staffName' => $staffName,
+            'itemInfo' => $itemInfo,
+            'pdType' => 'rollbackStock',
+            'remark' => '',
+        ];
+
+        \bizGhs\order\classes\CheckOrderClass::opOrder($data, false, false, false);
+    }
+
     public static function notSameCity($shop, $ghsShop)
     {
         return true;