瀏覽代碼

配送方式 正式开始大改

shish 1 月之前
父節點
當前提交
b98584c981
共有 2 個文件被更改,包括 244 次插入108 次删除
  1. 123 108
      app-hd/controllers/PurchaseController.php
  2. 121 0
      biz-ghs/order/classes/ShMethodClass.php

+ 123 - 108
app-hd/controllers/PurchaseController.php

@@ -11,6 +11,7 @@ use bizGhs\custom\classes\AccountMoneyClass;
 use bizGhs\custom\classes\CustomClass;
 use bizGhs\express\classes\GhsDeliveryOrderClass;
 use bizGhs\order\classes\OrderClass;
+use bizGhs\order\classes\ShMethodClass;
 use bizHd\cg\classes\CgMergeSnClass;
 use bizHd\purchase\classes\PurchaseClass;
 use bizHd\purchase\classes\PurchaseItemClass;
@@ -520,6 +521,9 @@ class PurchaseController extends BaseController
 
             $post['product'] = $productList;
 
+            // xhShMethod 门店下单前保留花材合计,供后续 packCost 计算(pfLevel==0 分支会 unset itemTotalAmount)
+            $shMethodItemAmount = $post['itemTotalAmount'] ?? 0;
+
             $sendType = $post['sendType'] ?? 0;
             $transType = $post['transType'] ?? 0;
             if ($sendType == dict::getDict('sendType', 'thirdSend')) {
@@ -696,142 +700,153 @@ class PurchaseController extends BaseController
                 noticeUtil::push("包装费:" . $packCost, '15280215347');
             }
 
+            // xhShMethod 门店:下单前按配置写入不满最低消费时的附加包装费/运费
+            if (in_array($ghsShopId, [36523, 0])) {
+                $shMethodBigNum = 0;
+                foreach ($productList as $itemData) {
+                    $shMethodBigNum += floatval($itemData['bigNum'] ?? 0);
+                }
+                $post['packCost'] = ShMethodClass::resolvePurchasePackCost(
+                    $ghsShopId,
+                    $ghsMainId,
+                    $sendType,
+                    $shMethodItemAmount,
+                    $shMethodBigNum
+                );
+            }
+
             $respond = PurchaseService::createPurchase($post, $ghsInfo);
 
-            if ($sendType == 0) {
-                if (getenv('YII_ENV') == 'production') {
-                    //杭州斗南鲜花批发部,满500,10公里内免费配送,满1000,20公里免费配送
-                    if ($ghsShopId == 4608) {
-                        if ($respond->actPrice < 500) {
-                            $transaction->rollBack();
-                            util::fail('满500元支持免费配送');
+            if (in_array($ghsShopId, [36523, 0])) {
+                // 使用 xhShMethod 配置校验配送限制,替代旧版门店硬编码规则
+                $shMethodError = ShMethodClass::validatePurchaseOrder(
+                    $ghsShopId,
+                    $ghsMainId,
+                    $sendType,
+                    $respond->actPrice ?? 0,
+                    $respond->bigNum ?? 0,
+                    $respond->packCost ?? 0,
+                    $post['wlName'] ?? ''
+                );
+                if ($shMethodError !== '') {
+                    $transaction->rollBack();
+                    util::fail($shMethodError);
+                }
+            } else {
+
+                if ($sendType == 0) {
+                    if (getenv('YII_ENV') == 'production') {
+                        //杭州斗南鲜花批发部,满500,10公里内免费配送,满1000,20公里免费配送
+                        if ($ghsShopId == 4608) {
+                            if ($respond->actPrice < 500) {
+                                $transaction->rollBack();
+                                util::fail('满500元支持免费配送');
+                            }
                         }
-                    }
-                    //叶上花,满500元市区免费配送
-                    if ($ghsShopId == 4804) {
-                        if ($respond->actPrice < 500) {
-                            $transaction->rollBack();
-                            util::fail('满500元支持免费配送');
+                        //叶上花,满500元市区免费配送
+                        if ($ghsShopId == 4804) {
+                            if ($respond->actPrice < 500) {
+                                $transaction->rollBack();
+                                util::fail('满500元支持免费配送');
+                            }
                         }
-                    }
-                    //花悠星 国恋 勇记 万丽 陆丰暂时不支持免费配送
-                    if ($ghsShopId == 7687 || $ghsShopId == 7831 || $ghsShopId == 7778 || $ghsShopId == 12003 || $ghsShopId == 38128) {
-                        util::fail('暂不支持送货上门,请选其它配送方式!');
-                    }
-
-                    //广州鑫源花卉,选择送货上门必须选必选商品
-                    if ($ghsShopId == 12439) {
-                        $hasMust = array_intersect($ids, [3446945, 3446970, 3446973, 3446979, 3446995, 3447010, 3447023, 3459983, 4333184, 4317745, 1585738]);
-                        if (empty($hasMust)) {
-                            util::fail('请返回选择 必选商品');
+                        //花悠星 国恋 勇记 万丽 陆丰暂时不支持免费配送
+                        if ($ghsShopId == 7687 || $ghsShopId == 7831 || $ghsShopId == 7778 || $ghsShopId == 12003 || $ghsShopId == 38128) {
+                            util::fail('暂不支持送货上门,请选其它配送方式!');
                         }
-                    }
 
-                    //三明易批花
-                    if ($ghsShopId == 82200) {
-                        if ($respond->actPrice < 100 || $respond->bigNum < 15) {
-                            $hasMust = array_intersect($ids, [4283478]);
+                        //广州鑫源花卉,选择送货上门必须选必选商品
+                        if ($ghsShopId == 12439) {
+                            $hasMust = array_intersect($ids, [3446945, 3446970, 3446973, 3446979, 3446995, 3447010, 3447023, 3459983, 4333184, 4317745, 1585738]);
                             if (empty($hasMust)) {
-                                util::fail('没满100元且15扎,请返回选运费');
+                                util::fail('请返回选择 必选商品');
                             }
                         }
-                        // 按当天计时,把 sendTimeWant 要限制在两天的时间范围之内
-                        $future = date("Y-m-d", strtotime('+2 day'));
-                        if (strtotime($respond->sendTimeWant) > strtotime($future)) {
-                            util::fail('配送日期只能选最近二天');
-                        }
-                    }
 
-                    if ($ghsShopId == 77703) {
-                        //天天鲜花思明店,暂时关闭送货上门
-                        if ($respond->sendType == 0) {
-                            //util::fail('节日暂停送货上门,请选到店自取');
+                        //三明易批花
+                        if ($ghsShopId == 82200) {
+                            if ($respond->actPrice < 100 || $respond->bigNum < 15) {
+                                $hasMust = array_intersect($ids, [4283478]);
+                                if (empty($hasMust)) {
+                                    util::fail('没满100元且15扎,请返回选运费');
+                                }
+                            }
+                            // 按当天计时,把 sendTimeWant 要限制在两天的时间范围之内
+                            $future = date("Y-m-d", strtotime('+2 day'));
+                            if (strtotime($respond->sendTimeWant) > strtotime($future)) {
+                                util::fail('配送日期只能选最近二天');
+                            }
                         }
-                    }
 
-                    if ($ghsShopId == 86726) {
-                        //藏花林
-                        if ($respond->transType != 4 && $respond->transType != 5) {
-                            util::fail('请选同城配送或到店自取');
+                        if ($ghsShopId == 77703) {
+                            //天天鲜花思明店,暂时关闭送货上门
+                            if ($respond->sendType == 0) {
+                                //util::fail('节日暂停送货上门,请选到店自取');
+                            }
                         }
-                    }
-
-                } else {
-
-                    //好运鲜花批发,选择送货上门必须选必选商品
-//                    if ($ghsShopId == 36523) {
-//                        if ($respond->transType != 4 && $respond->transType != 5) {
-//                            util::fail('请选同城配送或到店自取');
-//                        }
-//                    }
 
-//                    if ($ghsShopId == 36523) {
-//                        if ($respond->actPrice < 50 || $respond->bigNum < 10) {
-//                            $hasMust = array_intersect($ids, [85295]);
-//                            if (empty($hasMust)) {
-//                                util::fail('没满50元且满10扎,请运费必选商品');
-//                            }
-//                        }
-//                        // 按当天计时,把 sendTimeWant 要限制在两天的时间范围之内
-//                        $future = date("Y-m-d", strtotime('+2 day'));
-//                        if (strtotime($respond->sendTimeWant) > strtotime($future)) {
-//                            util::fail('配送日期只能选最近二天');
-//                        }
-//                    }
-
-                }
+                        if ($ghsShopId == 86726) {
+                            //藏花林
+                            if ($respond->transType != 4 && $respond->transType != 5) {
+                                util::fail('请选同城配送或到店自取');
+                            }
+                        }
 
-                if (isset($ghsInfo['home'])) {
-                    if ($ghsInfo['home'] == 0) {
-                        util::fail('暂不支持送货上门,请选其它配送方式');
-                    }
-                    $homeAmount = $ghsInfo['homeAmount'] ? floatval($ghsInfo['homeAmount']) : 0;
-                    $homeNum = $ghsInfo['homeNum'] ? floatval($ghsInfo['homeNum']) : 0;
-                    if ($homeAmount > 0 && $homeAmount > $respond->actPrice) {
-                        util::fail("满{$homeAmount}元 可送货上门");
                     }
-                    if ($homeNum > 0 && $homeNum > $respond->bigNum) {
-                        util::fail("满{$homeNum}扎 可送货上门");
+
+                    if (isset($ghsInfo['home'])) {
+                        if ($ghsInfo['home'] == 0) {
+                            util::fail('暂不支持送货上门,请选其它配送方式');
+                        }
+                        $homeAmount = $ghsInfo['homeAmount'] ? floatval($ghsInfo['homeAmount']) : 0;
+                        $homeNum = $ghsInfo['homeNum'] ? floatval($ghsInfo['homeNum']) : 0;
+                        if ($homeAmount > 0 && $homeAmount > $respond->actPrice) {
+                            util::fail("满{$homeAmount}元 可送货上门");
+                        }
+                        if ($homeNum > 0 && $homeNum > $respond->bigNum) {
+                            util::fail("满{$homeNum}扎 可送货上门");
+                        }
                     }
-                }
 
-            }
+                }
 
-            if ($sendType == 2) {
-                //温州易批花,跑腿,不满100,要选10元的补运费商品
-                if ($ghsShopId == 91115) {
-                    if ($respond->actPrice < 100) {
-                        $transaction->rollBack();
-                        $hasMust = array_intersect($ids, [4449812, 4449811]);
-                        if (empty($hasMust)) {
-                            util::fail('不满100,必选商品 补运费10元');
+                if ($sendType == 2) {
+                    //温州易批花,跑腿,不满100,要选10元的补运费商品
+                    if ($ghsShopId == 91115) {
+                        if ($respond->actPrice < 100) {
+                            $transaction->rollBack();
+                            $hasMust = array_intersect($ids, [4449812, 4449811]);
+                            if (empty($hasMust)) {
+                                util::fail('不满100,必选商品 补运费10元');
+                            }
                         }
                     }
                 }
-            }
-            if ($sendType == 4) {
-                //温州易批花,快递,不满100,要选10元的补运费商品
-                if ($ghsShopId == 91115) {
-                    if ($respond->actPrice < 100) {
-                        $transaction->rollBack();
-                        $hasMust = array_intersect($ids, [4449812, 4449811]);
-                        if (empty($hasMust)) {
-                            util::fail('不满100,必选商品 补运费10元');
+                if ($sendType == 4) {
+                    //温州易批花,快递,不满100,要选10元的补运费商品
+                    if ($ghsShopId == 91115) {
+                        if ($respond->actPrice < 100) {
+                            $transaction->rollBack();
+                            $hasMust = array_intersect($ids, [4449812, 4449811]);
+                            if (empty($hasMust)) {
+                                util::fail('不满100,必选商品 补运费10元');
+                            }
                         }
                     }
                 }
-            }
-            if ($sendType == 3) {
-                //温州易批花,物流,不满200,要选10元的补运费商品
-                if ($ghsShopId == 91115) {
-                    if ($respond->actPrice < 200) {
-                        $transaction->rollBack();
-                        $hasMust = array_intersect($ids, [4449812, 4449811]);
-                        if (empty($hasMust)) {
-                            util::fail('不满200,必选商品 补运费10元');
+                if ($sendType == 3) {
+                    //温州易批花,物流,不满200,要选10元的补运费商品
+                    if ($ghsShopId == 91115) {
+                        if ($respond->actPrice < 200) {
+                            $transaction->rollBack();
+                            $hasMust = array_intersect($ids, [4449812, 4449811]);
+                            if (empty($hasMust)) {
+                                util::fail('不满200,必选商品 补运费10元');
+                            }
                         }
                     }
                 }
+
             }
 
             $transaction->commit();

+ 121 - 0
biz-ghs/order/classes/ShMethodClass.php

@@ -175,6 +175,127 @@ class ShMethodClass extends BaseClass
         return $result;
     }
 
+    /**
+     * 判断配送方式配置是否未达最低消费(金额或扎数任一不满足即视为未达标)
+     * 与 hdApp affirm-sh-method-panel.checkShMethodBelowMinimum 规则一致
+     *
+     * @param array $config xhShMethod 配置
+     * @param float $actPrice 实付花材金额
+     * @param int|float $bigNum 下单扎数
+     * @return bool
+     */
+    public static function isBelowMinimum($config, $actPrice, $bigNum)
+    {
+        $minAmount = isset($config['minAmount']) ? (float)$config['minAmount'] : 0;
+        $minNum = isset($config['minNum']) ? (int)$config['minNum'] : 0;
+        if ($minAmount <= 0 && $minNum <= 0) {
+            return false;
+        }
+        $belowAmount = $minAmount > 0 && (float)$actPrice < $minAmount;
+        $belowNum = $minNum > 0 && (float)$bigNum < $minNum;
+        return $belowAmount || $belowNum;
+    }
+
+    /**
+     * 采购下单后校验 xhShMethod 配送限制
+     * 供 hd PurchaseController::actionCreateOrder 在 createPurchase 之后调用,与旧版 else 分支门店硬编码限制同级
+     *
+     * @param int $shopId 供货商门店 ID
+     * @param int $mainId 供货商商户 ID
+     * @param int $sendType 配送方式,与 xhShMethod.style 一致
+     * @param float $actPrice 下单后实付花材金额
+     * @param int|float $bigNum 下单扎数
+     * @param float $packCost 已写入采购单的包装费
+     * @param string $wlName 物流公司名称(style=3 时必填)
+     * @return string 空字符串表示通过,否则为需提示用户的错误文案
+     */
+    public static function validatePurchaseOrder($shopId, $mainId, $sendType, $actPrice, $bigNum, $packCost = 0, $wlName = '')
+    {
+        $style = (int)$sendType;
+        if ($style < 0 || $style > 4) {
+            return '配送方式无效';
+        }
+
+        $config = self::getConfig($shopId, $mainId, $style);
+        if (empty($config)) {
+            return '配送方式配置异常';
+        }
+
+        $methodName = !empty($config['name']) ? $config['name'] : '该配送方式';
+
+        // 配送方式已禁用,对齐旧逻辑「暂不支持送货上门,请选其它配送方式」
+        if (isset($config['status']) && (int)$config['status'] === 0) {
+            return "暂不支持{$methodName},请选其它配送方式";
+        }
+
+        // 物流需填物流公司,与 hdApp validateBeforeSubmit 一致
+        if ($style === 3 && trim((string)$wlName) === '') {
+            return '请填选物流';
+        }
+
+        if (!self::isBelowMinimum($config, $actPrice, $bigNum)) {
+            return '';
+        }
+
+        $unMeet = isset($config['unMeet']) ? (int)$config['unMeet'] : 0;
+        $unMeetFee = isset($config['unMeetFee']) ? (float)$config['unMeetFee'] : 0;
+
+        // 不满最低消费且不允许下单
+        if ($unMeet === 1) {
+            $minAmount = isset($config['minAmount']) ? (float)$config['minAmount'] : 0;
+            $minNum = isset($config['minNum']) ? (int)$config['minNum'] : 0;
+            if ($minAmount > 0 && (float)$actPrice < $minAmount) {
+                return "满{$minAmount}元 可{$methodName}";
+            }
+            if ($minNum > 0 && (float)$bigNum < $minNum) {
+                return "满{$minNum}扎 可{$methodName}";
+            }
+            return '未达到最低消费,不能下单';
+        }
+
+        // 不满最低消费但允许加收费用时,校验包装费/运费是否已写入采购单,防止前端绕过
+        if (($unMeet === 0 || $unMeet === 2) && $unMeetFee > 0) {
+            if (bccomp((string)$packCost, (string)$unMeetFee, 2) < 0) {
+                $feeLabel = $unMeet === 2 ? '包装费' : '运费';
+                return "未达最低消费,需加收{$feeLabel}{$unMeetFee}元";
+            }
+        }
+
+        return '';
+    }
+
+    /**
+     * 计算采购单应写入的附加费用(不满最低消费时按 unMeet 加收运费或包装费)
+     * 供 createPurchase 前写入 packCost,与 hdApp syncPackCostByMethod 一致
+     *
+     * @param int $shopId 供货商门店 ID
+     * @param int $mainId 供货商商户 ID
+     * @param int $sendType 配送方式
+     * @param float $itemTotalAmount 花材合计金额(下单前,与前端 commodityPrice 一致)
+     * @param int|float $bigNum 下单扎数
+     * @return float
+     */
+    public static function resolvePurchasePackCost($shopId, $mainId, $sendType, $itemTotalAmount, $bigNum)
+    {
+        $style = (int)$sendType;
+        if ($style < 0 || $style > 4) {
+            return 0;
+        }
+
+        $config = self::getConfig($shopId, $mainId, $style);
+        if (empty($config) || !self::isBelowMinimum($config, $itemTotalAmount, $bigNum)) {
+            return 0;
+        }
+
+        $unMeet = isset($config['unMeet']) ? (int)$config['unMeet'] : 0;
+        $unMeetFee = isset($config['unMeetFee']) ? (float)$config['unMeetFee'] : 0;
+        if (($unMeet === 0 || $unMeet === 2) && $unMeetFee > 0) {
+            return $unMeetFee;
+        }
+
+        return 0;
+    }
+
     /**
      * 保存配送方式配置,包含说明项和满减规则
      * @param int $shopId 门店ID