Parcourir la source

配送方式 正式开始大改 3

shish il y a 1 mois
Parent
commit
ebb4f45b91

+ 24 - 6
app-hd/controllers/NoticeController.php

@@ -196,8 +196,14 @@ class NoticeController extends PublicController
                         //标记当天发快递方式已收一次包装费,岭南批发市场用的功能
                         $customId = $cg->customId ?? 0;
                         $current = date("Y_m_d");
-                        $key = 'ghs_custom_today_has_get_pack_cost_' . $current . '_' . $customId;
-                        Yii::$app->redis->executeCommand('SETEX', [$key, 86400, '1']);
+                        if ($cg->packCost > 0) {
+                            $key = 'ghs_custom_today_has_get_pack_cost_' . $current . '_' . $customId;
+                            Yii::$app->redis->executeCommand('SETEX', [$key, 86400, '1']);
+                        }
+                        if ($cg->sendCost > 0) {
+                            $sendKey = 'ghs_custom_today_has_get_send_cost_' . $current . '_' . $customId;
+                            Yii::$app->redis->executeCommand('SETEX', [$sendKey, 86400, '1']);
+                        }
                     }
                 }
 
@@ -411,8 +417,14 @@ class NoticeController extends PublicController
                         //标记当天发快递方式已收一次包装费,岭南批发市场用的功能
                         $customId = $cg->customId ?? 0;
                         $current = date("Y_m_d");
-                        $key = 'ghs_custom_today_has_get_pack_cost_' . $current . '_' . $customId;
-                        Yii::$app->redis->executeCommand('SETEX', [$key, 86400, '1']);
+                        if ($cg->packCost > 0) {
+                            $key = 'ghs_custom_today_has_get_pack_cost_' . $current . '_' . $customId;
+                            Yii::$app->redis->executeCommand('SETEX', [$key, 86400, '1']);
+                        }
+                        if ($cg->sendCost > 0) {
+                            $sendKey = 'ghs_custom_today_has_get_send_cost_' . $current . '_' . $customId;
+                            Yii::$app->redis->executeCommand('SETEX', [$sendKey, 86400, '1']);
+                        }
                     }
                 }
 
@@ -574,8 +586,14 @@ class NoticeController extends PublicController
                         //标记当天发快递方式已收一次包装费,岭南批发市场用的功能
                         $customId = $cg->customId ?? 0;
                         $current = date("Y_m_d");
-                        $key = 'ghs_custom_today_has_get_pack_cost_' . $current . '_' . $customId;
-                        Yii::$app->redis->executeCommand('SETEX', [$key, 86400, '1']);
+                        if ($cg->packCost > 0) {
+                            $key = 'ghs_custom_today_has_get_pack_cost_' . $current . '_' . $customId;
+                            Yii::$app->redis->executeCommand('SETEX', [$key, 86400, '1']);
+                        }
+                        if ($cg->sendCost > 0) {
+                            $sendKey = 'ghs_custom_today_has_get_send_cost_' . $current . '_' . $customId;
+                            Yii::$app->redis->executeCommand('SETEX', [$sendKey, 86400, '1']);
+                        }
                     }
                 }
 

+ 24 - 6
app-hd/controllers/PurchaseController.php

@@ -706,27 +706,39 @@ class PurchaseController extends BaseController
                 foreach ($productList as $itemData) {
                     $shMethodBigNum += floatval($itemData['bigNum'] ?? 0);
                 }
-                $post['packCost'] = ShMethodClass::resolvePurchasePackCost(
+                $additionalCosts = ShMethodClass::calcFee(
                     $ghsShopId,
                     $ghsMainId,
                     $sendType,
                     $shMethodItemAmount,
                     $shMethodBigNum
                 );
+                
+                // 如果产生了附加运费,与前面可能产生的 sendCost 进行累加或覆盖
+                if ($additionalCosts['sendCost'] > 0) {
+                    $post['sendCost'] = bcadd($post['sendCost'] ?? 0, $additionalCosts['sendCost'], 2);
+                }
+                
+                // 如果产生了附加包装费,覆盖前面赋值的 packCost
+                if ($additionalCosts['packCost'] > 0) {
+                    $post['packCost'] = $additionalCosts['packCost'];
+                }
             }
 
             $respond = PurchaseService::createPurchase($post, $ghsInfo);
 
             if (in_array($ghsShopId, [36523, 0])) {
                 // 使用 xhShMethod 配置校验配送限制,替代旧版门店硬编码规则,关键词sh_method_area
-                $shMethodError = ShMethodClass::validatePurchaseOrder(
+                $shMethodError = ShMethodClass::checkLimit(
                     $ghsShopId,
                     $ghsMainId,
                     $sendType,
                     $respond->actPrice ?? 0,
                     $respond->bigNum ?? 0,
-                    $respond->packCost ?? 0,
-                    $post['wlName'] ?? ''
+                    $post['packCost'] ?? 0,
+                    $post['sendCost'] ?? 0,
+                    $post['wlName'] ?? '',
+                    $customId
                 );
                 if ($shMethodError !== '') {
                     $transaction->rollBack();
@@ -940,8 +952,14 @@ class PurchaseController extends BaseController
                 if (isset($info->sendType) && $info->sendType == 4) {
                     //标记当天已收一次包装费,岭南批发市场用的功能
                     $current = date("Y_m_d");
-                    $key = 'ghs_custom_today_has_get_pack_cost_' . $current . '_' . $customId;
-                    Yii::$app->redis->executeCommand('SETEX', [$key, 86400, '1']);
+                    if ($info->packCost > 0) {
+                        $key = 'ghs_custom_today_has_get_pack_cost_' . $current . '_' . $customId;
+                        Yii::$app->redis->executeCommand('SETEX', [$key, 86400, '1']);
+                    }
+                    if ($info->sendCost > 0) {
+                        $sendKey = 'ghs_custom_today_has_get_send_cost_' . $current . '_' . $customId;
+                        Yii::$app->redis->executeCommand('SETEX', [$sendKey, 86400, '1']);
+                    }
                 }
             }
 

+ 22 - 0
app-hd/controllers/ShMethodController.php

@@ -32,7 +32,29 @@ class ShMethodController extends BaseController
         }
 
         try {
+            $customId = $ghs['customId'] ?? 0;
             $configs = ShMethodClass::getAllConfigs($shopId, $mainId);
+
+            if ($customId > 0) {
+                $hasPaidSendCost = ShMethodClass::isSendCostPaidToday($customId);
+                $hasPaidPackCost = ShMethodClass::isPaidToday($customId);
+                
+                foreach ($configs as &$config) {
+                    $unMeet = (int)($config['unMeet'] ?? 0);
+                    // 如果是加收运费或加收包装费,并且今天已经收过对应的费用
+                    // 则将前端展示的条件清空(设为0),从而不再显示要收附加费的提示
+                    if ($unMeet === 0 && $hasPaidSendCost) {
+                        $config['minAmount'] = 0;
+                        $config['minNum'] = 0;
+                        $config['unMeetFee'] = 0;
+                    } elseif ($unMeet === 2 && $hasPaidPackCost) {
+                        $config['minAmount'] = 0;
+                        $config['minNum'] = 0;
+                        $config['unMeetFee'] = 0;
+                    }
+                }
+            }
+
             util::success(['method' => $configs]);
         } catch (\Exception $e) {
             util::fail($e->getMessage());

+ 107 - 26
biz-ghs/order/classes/ShMethodClass.php

@@ -184,18 +184,6 @@ class ShMethodClass extends BaseClass
      * @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 分支门店硬编码限制同级
@@ -206,10 +194,12 @@ class ShMethodClass extends BaseClass
      * @param float $actPrice 下单后实付花材金额
      * @param int|float $bigNum 下单扎数
      * @param float $packCost 已写入采购单的包装费
+     * @param float $sendCost 已写入采购单的运费
      * @param string $wlName 物流公司名称(style=3 时必填)
+     * @param int $customId 客户ID,用于判断当天是否已收过
      * @return string 空字符串表示通过,否则为需提示用户的错误文案
      */
-    public static function validatePurchaseOrder($shopId, $mainId, $sendType, $actPrice, $bigNum, $packCost = 0, $wlName = '')
+    public static function checkLimit($shopId, $mainId, $sendType, $actPrice, $bigNum, $packCost = 0, $sendCost = 0, $wlName = '', $customId = 0)
     {
         $style = (int)$sendType;
         if ($style < 0 || $style > 4) {
@@ -233,7 +223,22 @@ class ShMethodClass extends BaseClass
             return '请填选物流';
         }
 
-        if (!self::isBelowMinimum($config, $actPrice, $bigNum)) {
+        $minAmount = isset($config['minAmount']) ? (float)$config['minAmount'] : 0.00;
+        $minNum = isset($config['minNum']) ? (int)$config['minNum'] : 0;
+        
+        // 只要满足任意一个条件,就不算“未达标”
+        $isBelowMinimum = true;
+        if ($minAmount > 0 && $actPrice >= $minAmount) {
+            $isBelowMinimum = false;
+        }
+        if ($minNum > 0 && (float)$bigNum >= $minNum) {
+            $isBelowMinimum = false;
+        }
+        if ($minAmount <= 0 && $minNum <= 0) {
+            $isBelowMinimum = false;
+        }
+
+        if (!$isBelowMinimum) {
             return '';
         }
 
@@ -242,8 +247,6 @@ class ShMethodClass extends BaseClass
 
         // 不满最低消费且不允许下单
         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}";
             }
@@ -255,7 +258,18 @@ class ShMethodClass extends BaseClass
 
         // 不满最低消费但允许加收费用时,校验包装费/运费是否已写入采购单,防止前端绕过
         if (($unMeet === 0 || $unMeet === 2) && $unMeetFee > 0) {
-            if (bccomp((string)$packCost, (string)$unMeetFee, 2) < 0) {
+            // unMeet === 0 加收运费, unMeet === 2 加收包装费
+            $feeToCheck = $unMeet === 2 ? $packCost : $sendCost;
+            
+            // 如果今天已经收过对应的附加费用,则直接通过校验
+            if ($unMeet === 2 && self::isPaidToday($customId)) {
+                return '';
+            }
+            if ($unMeet === 0 && self::isSendCostPaidToday($customId)) {
+                return '';
+            }
+            
+            if (bccomp((string)$feeToCheck, (string)$unMeetFee, 2) < 0) {
                 $feeLabel = $unMeet === 2 ? '包装费' : '运费';
                 return "未达最低消费,需加收{$feeLabel}{$unMeetFee}元";
             }
@@ -264,36 +278,103 @@ class ShMethodClass extends BaseClass
         return '';
     }
 
+    /**
+     * 判断当天是否已经收过附加费(运费或包装费)
+     * 与其他逻辑共用同一个 Redis Key,保证一天只收一次
+     * @param int $customId 客户ID
+     * @return bool
+     */
+    public static function isPaidToday($customId)
+    {
+        if (empty($customId)) {
+            return false;
+        }
+        $current = date("Y_m_d");
+        $key = 'ghs_custom_today_has_get_pack_cost_' . $current . '_' . $customId;
+        $has = \Yii::$app->redis->executeCommand('GET', [$key]);
+        return (!empty($has) && $has == 1);
+    }
+
+    /**
+     * 判断当天是否已经收过运费
+     * @param int $customId 客户ID
+     * @return bool
+     */
+    public static function isSendCostPaidToday($customId)
+    {
+        if (empty($customId)) {
+            return false;
+        }
+        $current = date("Y_m_d");
+        $key = 'ghs_custom_today_has_get_send_cost_' . $current . '_' . $customId;
+        $has = \Yii::$app->redis->executeCommand('GET', [$key]);
+        return (!empty($has) && $has == 1);
+    }
+
     /**
      * 计算采购单应写入的附加费用(不满最低消费时按 unMeet 加收运费或包装费)
-     * 供 createPurchase 前写入 packCost,与 hdApp syncPackCostByMethod 一致
+     * 供 createPurchase 前写入 sendCost 和 packCost,与 hdApp syncPackCostByMethod 一致
      *
      * @param int $shopId 供货商门店 ID
      * @param int $mainId 供货商商户 ID
      * @param int $sendType 配送方式
      * @param float $itemTotalAmount 花材合计金额(下单前,与前端 commodityPrice 一致)
      * @param int|float $bigNum 下单扎数
-     * @return float
+     * @param int $customId 客户ID,用于判断当天是否已收过
+     * @return array ['sendCost' => 运费, 'packCost' => 包装费]
      */
-    public static function resolvePurchasePackCost($shopId, $mainId, $sendType, $itemTotalAmount, $bigNum)
+    public static function calcFee($shopId, $mainId, $sendType, $itemTotalAmount, $bigNum, $customId = 0)
     {
+        $costs = ['sendCost' => 0, 'packCost' => 0];
+        
         $style = (int)$sendType;
         if ($style < 0 || $style > 4) {
-            return 0;
+            return $costs;
         }
 
         $config = self::getConfig($shopId, $mainId, $style);
-        if (empty($config) || !self::isBelowMinimum($config, $itemTotalAmount, $bigNum)) {
-            return 0;
+        if (empty($config)) {
+            return $costs;
+        }
+
+        // 直接合并 isBelowMinimum 的逻辑,方便理解,不再套用额外的方法
+        $minAmount = isset($config['minAmount']) ? (float)$config['minAmount'] : 0.00;
+        $minNum = isset($config['minNum']) ? (int)$config['minNum'] : 0;
+        
+        // 只要满足任意一个条件,就不算“未达标”
+        $isBelowMinimum = true;
+        if ($minAmount > 0 && $itemTotalAmount >= $minAmount) {
+            $isBelowMinimum = false;
+        }
+        if ($minNum > 0 && (float)$bigNum >= $minNum) {
+            $isBelowMinimum = false;
+        }
+        if ($minAmount <= 0 && $minNum <= 0) {
+            $isBelowMinimum = false;
+        }
+
+        // 如果已经达标,不收取附加费用
+        if (!$isBelowMinimum) {
+            return $costs;
         }
 
         $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;
+        
+        if ($unMeetFee > 0) {
+            // unMeet: 0 加收运费(默认),2 加收包装费
+            if ($unMeet === 0) {
+                if (!self::isSendCostPaidToday($customId)) {
+                    $costs['sendCost'] = $unMeetFee;
+                }
+            } elseif ($unMeet === 2) {
+                if (!self::isPaidToday($customId)) {
+                    $costs['packCost'] = $unMeetFee;
+                }
+            }
         }
 
-        return 0;
+        return $costs;
     }
 
     /**