|
|
@@ -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;
|
|
|
}
|
|
|
|
|
|
/**
|