diff --git a/app/common.php b/app/common.php index 80f2a2d..e0af625 100644 --- a/app/common.php +++ b/app/common.php @@ -1398,8 +1398,14 @@ if (!function_exists('getTimeStamp')) { return [$startTime,$endTime]; } } - - +// 获取指定两个数之间的随机数,保留两位小数 +if (!function_exists('getRandom')) { + function getRandom($minNum, $maxNum){ + $rand = $minNum + mt_rand() / mt_getrandmax() * ($maxNum - $minNum); + + return floatval(number_format($rand,2)); + } +} diff --git a/app/common/repositories/store/order/StoreOrderCreateRepository.php b/app/common/repositories/store/order/StoreOrderCreateRepository.php index b6f0381..1422ea2 100644 --- a/app/common/repositories/store/order/StoreOrderCreateRepository.php +++ b/app/common/repositories/store/order/StoreOrderCreateRepository.php @@ -724,6 +724,7 @@ class StoreOrderCreateRepository extends StoreOrderRepository{ $orderTotalWineDiffMoney = 0;// 订单总差价 $orderTotalWineDiffMoneyPrice = 0;// 订单总补差价支付金额 + $orderTotalRandomReduction = 0;// 订单总随机立减金额 foreach($merchantCartList as &$merchantCart){ $merchantCart['take'] = [ @@ -755,6 +756,8 @@ class StoreOrderCreateRepository extends StoreOrderRepository{ $orderWineDiffMoney = 0;// 当前订单差价 $orderWineDiffMoneyPrice = 0;// 当前订单补差价支付金额 + $orderRandomReduction = 0;//当前订单随机立减金额 + //计算积分抵扣 foreach($merchantCart['list'] as &$cart){ //只有普通商品可以抵扣 @@ -826,6 +829,22 @@ class StoreOrderCreateRepository extends StoreOrderRepository{ $orderWineIntegral = bcadd($orderWineIntegral,$deductionAmount,2); $orderWineIntegralPrice = bcadd($orderWineIntegralPrice,$deductionAmount,2); } + // 计算随机立减 仅普通商品有效 + if($cart['product_type'] == 0 && $cart['true_price'] > 0){ + // 随机立减 + $systemConfig = systemConfig(['random_reduction_min','random_reduction_max']); + $minNum = $systemConfig['random_reduction_min'] ?? 0.1; + $maxNum = $systemConfig['random_reduction_max'] ?? 5; + // 判断:如果最大立减金额 大于剩余支付金额;则最大立减金额=剩余支付金额,否则不变 + $maxNum = $maxNum > $cart['true_price'] ? $cart['true_price'] : $maxNum; + $randomReduction = getRandom($minNum, $maxNum); + + $cart['random_reduction'] = $randomReduction; + $cart['true_price'] = bcsub($cart['true_price'],$randomReduction,2);; + $merchantCart['order']['true_price'] = bcsub($merchantCart['order']['true_price'], $randomReduction,2); + + $orderRandomReduction = bcadd($orderRandomReduction,$randomReduction,2); + } } unset($cart); $order_total_integral = bcadd($order_total_integral,$total_integral,0); @@ -836,6 +855,8 @@ class StoreOrderCreateRepository extends StoreOrderRepository{ $orderTotalWineIntegral = bcadd($orderTotalWineIntegral,$orderWineIntegral,2); $orderTotalWineIntegralPrice = bcadd($orderTotalWineIntegralPrice,$orderWineIntegralPrice,2); + $orderTotalRandomReduction = bcadd($orderTotalRandomReduction,$orderRandomReduction,2); + $orderTotalWineDiffMoney = bcadd($orderTotalWineDiffMoney,$orderWineDiffMoney,2); $orderTotalWineDiffMoneyPrice = bcadd($orderTotalWineDiffMoneyPrice,$orderWineDiffMoneyPrice,2); @@ -888,9 +909,19 @@ class StoreOrderCreateRepository extends StoreOrderRepository{ $merchantCart['order']['org_price'] = $org_price; $merchantCart['order']['pay_price'] = $pay_price; $merchantCart['order']['coupon_price'] = $coupon_price; + + $merchantCart['order']['random_reduction'] = $orderRandomReduction; + $order_price = bcadd($order_price,$pay_price,2); $order_total_price = bcadd($order_total_price,$total_price,2); } + + + // 随机立减 - 减少订单总金额 + // $order_price = (float)sprintf("%.2f",$order_price - $orderTotalRandomReduction); + // $order_total_price = (float)sprintf("%.2f",$order_total_price - $orderTotalRandomReduction); + + unset($merchantCart); if($order_model){ $allow_no_address = FALSE; @@ -910,6 +941,8 @@ class StoreOrderCreateRepository extends StoreOrderRepository{ $openIntegral = $merIntegralFlag && !$order_type && $sysIntegralConfig['integral_status'] && $sysIntegralConfig['integral_money'] > 0; + + $integralDeductionAmount = bcadd($order_total_integral_price,$orderTotalWineIntegralPrice,2); $total_coupon = bcadd($order_svip_discount,bcadd(bcadd($total_platform_coupon_price,$order_coupon_price,2),$integralDeductionAmount,2),2); @@ -935,6 +968,7 @@ class StoreOrderCreateRepository extends StoreOrderRepository{ 'orderTotalWineDiffMoney', 'orderTotalWineDiffMoneyPrice', + 'orderTotalRandomReduction', 'order_total_give_integral', 'order_svip_discount', @@ -1151,6 +1185,8 @@ class StoreOrderCreateRepository extends StoreOrderRepository{ 'wine_diff_money' => $merchantCart['order']['wine_diff_money'] ?? 0, 'wine_diff_money_price' => $merchantCart['order']['wine_diff_money_price'] ?? 0, 'point_id' => $merchantCart['take']['id'] ?? 0, + // 随机立减金额 + 'random_reduction' => $merchantCart['order']['random_reduction'] ?? 0, ]; $allUseCoupon = array_merge($allUseCoupon,$merchantCart['order']['useCouponIds']); @@ -1186,6 +1222,8 @@ class StoreOrderCreateRepository extends StoreOrderRepository{ 'exchange_integral' => $orderInfo['orderTotalWineIntegral'] ?? 0, 'wine_diff_money' => $orderInfo['orderTotalWineDiffMoney'] ?? 0, 'wine_diff_money_price' => $orderInfo['orderTotalWineDiffMoneyPrice'] ?? 0, + // 随机立减金额 + 'random_reduction' => $orderInfo['orderTotalRandomReduction'] ?? 0, ]; event('order.create.before',compact('groupOrder','orderList')); @@ -1452,6 +1490,8 @@ class StoreOrderCreateRepository extends StoreOrderRepository{ // 特殊商品 代理及省公司门店,供应商提成处理 'province_here_id' => $cart['province_here_id'] ?? 0, 'province_send_id' => $cart['province_send_id'] ?? 0, + // 随机立减金额 + 'random_reduction' => $cart['random_reduction'] ?? 0, ]; } $userMerchantRepository->getInfo($uid,$order['mer_id']);