增加:进货订单 支持使用补货额度进行减免

This commit is contained in:
wuhui_zzw 2024-05-27 16:34:38 +08:00
parent d7e3e8e8f8
commit cdb0589221
4 changed files with 136 additions and 94 deletions

View File

@ -43,6 +43,7 @@ class StoreOrderCreateRepository extends StoreOrderRepository{
$userIntegral = $user->integral;
$exchangeIntegral = $user->exchange_integral ?? 0;// 持有可用酒水卡积分
$isWithGoods = $withGoods['is_with_goods'] ?? 0;// 0=用户购物1=酒道馆进货
$with_goods_mer_id = $withGoods['with_goods_mer_id'] ?? 0;
$diffRate = 30;// 酒道馆商品 补差价比例 这里暂时默认固定为30%
$key = md5(json_encode(compact('cartId','takes','useCoupon','useIntegral','addressId'))).$uid;
//去掉过期的优惠券信息
@ -723,8 +724,10 @@ class StoreOrderCreateRepository extends StoreOrderRepository{
$orderTotalWineIntegralPrice = 0;// 订单总使用酒水卡积分抵扣的金额
$orderTotalWineDiffMoney = 0;// 订单总差价
$orderTotalWineDiffMoneyPrice = 0;// 订单总补差价支付金额
$orderTotalRandomReduction = 0;// 订单总随机立减金额
$orderTotalMerQuota = 0;// 进货订单-补货额度抵扣金额
// 获取当前进货商户的补货额度
$merQuotaSurplus = app()->make(MerchantRepository::class)->getSearch(['mer_id'=>$with_goods_mer_id])->value('quota_surplus');
foreach($merchantCartList as &$merchantCart){
$merchantCart['take'] = [
@ -755,52 +758,24 @@ class StoreOrderCreateRepository extends StoreOrderRepository{
$orderWineIntegralPrice = 0;// 当前订单使用就是快积分抵扣的金额
$orderWineDiffMoney = 0;// 当前订单差价
$orderWineDiffMoneyPrice = 0;// 当前订单补差价支付金额
$orderRandomReduction = 0;//当前订单随机立减金额
$orderMerQuota = 0;// 进货订单-补货额度抵扣金额
//计算积分抵扣
foreach($merchantCart['list'] as &$cart){
//只有普通商品可以抵扣
if($cart['product_type'] == 0 && $integralFlag && $userIntegral > 0 && $merchantCart['order']['true_price'] > 0){
$integralRate = $cart['product']['integral_rate'];
if($integralRate < 0){
$integralRate = $merIntegralConfig['mer_integral_rate'];
}
elseif($integralRate > 0){
$integralRate = min(bcdiv($integralRate,100,4),1);
}
if($integralRate > 0){
$productIntegralPrice = min(bcmul(bcmul($this->cartByPrice($cart),$cart['cart_num'],2),$integralRate,2),$cart['true_price']);
if($productIntegralPrice > 0){
$productIntegral = ceil(bcdiv($productIntegralPrice,$sysIntegralConfig['integral_money'],3));
if($productIntegral <= $userIntegral){
$userIntegral = bcsub($userIntegral,$productIntegral,0);
//使用多少积分抵扣了多少金额
$cart['integral'] = [
'use' => $productIntegral,
'price' => $productIntegralPrice
];
}
else{
$productIntegralPrice = bcmul($userIntegral,$sysIntegralConfig['integral_money'],2);
//使用多少积分抵扣了多少金额
$cart['integral'] = [
'use' => $userIntegral,
'price' => $productIntegralPrice
];
$userIntegral = 0;
}
$cart['true_price'] = bcsub($cart['true_price'],$cart['integral']['price'],2);
$merchantCart['order']['true_price'] = bcsub($merchantCart['order']['true_price'],$cart['integral']['price'],2);
$total_integral_price = bcadd($total_integral_price,$cart['integral']['price'],2);
$total_integral = bcadd($total_integral,$cart['integral']['use'],0);
continue;
}
}
if($cart['product_type'] == 35){
// 进货订单
$deductionAmount = (float)$merQuotaSurplus >= (float)$cart['true_price'] ? (float)$cart['true_price'] : (float)$merQuotaSurplus;// 实际抵扣金额
$merQuotaSurplus = bcsub((float)$merQuotaSurplus,(float)$deductionAmount,2);// 剩余补货额度
$cart['mer_quota_deduction'] = $deductionAmount;
// 订单总支付金额 先减去抵扣金额
$merchantCart['order']['true_price'] = bcsub($merchantCart['order']['true_price'],$deductionAmount,2);
// 当前商品剩余应支付金额
$cart['true_price'] = bcsub($cart['true_price'],$deductionAmount,2);
// 当前订单使用的补货额度及抵扣金额
$orderMerQuota = bcadd($orderMerQuota,$deductionAmount,2);
}
$cart['integral'] = NULL;
// 兑换酒道馆商品时 酒水卡积分抵扣处理
if($cart['product_type'] == 36){
else if($cart['product_type'] == 36){
// 兑换酒道馆商品时 酒水卡积分抵扣处理
// 当前商品消耗积分 如果持有酒水卡积分大于等于支付金额,使用支付金额;小于支付金额,使用酒水卡积分
$deductionAmount = (float)$exchangeIntegral >= (float)$cart['true_price'] ? (float)$cart['true_price'] : (float)$exchangeIntegral;// 实际抵扣金额
$exchangeIntegral = bcsub((float)$exchangeIntegral,(float)$deductionAmount,2);// 剩余酒水卡积分
@ -829,21 +804,62 @@ 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((float)$minNum, (float)$maxNum);
else{
//只有普通商品可以抵扣
if($cart['product_type'] == 0 && $integralFlag && $userIntegral > 0 && $merchantCart['order']['true_price'] > 0){
$integralRate = $cart['product']['integral_rate'];
if($integralRate < 0){
$integralRate = $merIntegralConfig['mer_integral_rate'];
}
elseif($integralRate > 0){
$integralRate = min(bcdiv($integralRate,100,4),1);
}
if($integralRate > 0){
$productIntegralPrice = min(bcmul(bcmul($this->cartByPrice($cart),$cart['cart_num'],2),$integralRate,2),$cart['true_price']);
if($productIntegralPrice > 0){
$productIntegral = ceil(bcdiv($productIntegralPrice,$sysIntegralConfig['integral_money'],3));
if($productIntegral <= $userIntegral){
$userIntegral = bcsub($userIntegral,$productIntegral,0);
//使用多少积分抵扣了多少金额
$cart['integral'] = [
'use' => $productIntegral,
'price' => $productIntegralPrice
];
}
else{
$productIntegralPrice = bcmul($userIntegral,$sysIntegralConfig['integral_money'],2);
//使用多少积分抵扣了多少金额
$cart['integral'] = [
'use' => $userIntegral,
'price' => $productIntegralPrice
];
$userIntegral = 0;
}
$cart['true_price'] = bcsub($cart['true_price'],$cart['integral']['price'],2);
$merchantCart['order']['true_price'] = bcsub($merchantCart['order']['true_price'],$cart['integral']['price'],2);
$total_integral_price = bcadd($total_integral_price,$cart['integral']['price'],2);
$total_integral = bcadd($total_integral,$cart['integral']['use'],0);
continue;
}
}
}
$cart['integral'] = NULL;
// 计算随机立减 仅普通商品有效
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((float)$minNum, (float)$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);
$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);
$orderRandomReduction = bcadd($orderRandomReduction,$randomReduction,2);
}
}
}
unset($cart);
@ -854,12 +870,10 @@ 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);
$orderTotalMerQuota = bcadd($orderTotalMerQuota,$orderMerQuota,2);
$_pay_price = $merchantCart['order']['true_price'];
$valid_total_price = $merchantCart['order']['valid_total_price'];
@ -905,23 +919,16 @@ class StoreOrderCreateRepository extends StoreOrderRepository{
$merchantCart['order']['exchange_integral'] = $orderWineIntegral;
$merchantCart['order']['wine_diff_money'] = $orderWineDiffMoney;
$merchantCart['order']['wine_diff_money_price'] = $orderWineDiffMoneyPrice;
$merchantCart['order']['org_price'] = $org_price;
$merchantCart['order']['pay_price'] = $pay_price;
$merchantCart['order']['coupon_price'] = $coupon_price;
$merchantCart['order']['random_reduction'] = $orderRandomReduction;
$merchantCart['order']['mer_quota_deduction'] = $orderMerQuota;
$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;
@ -941,13 +948,10 @@ 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);
$with_goods_mer_id = $withGoods['with_goods_mer_id'] ?? 0;
$data = compact(
'with_goods_mer_id',
'order_type',
@ -962,13 +966,12 @@ class StoreOrderCreateRepository extends StoreOrderRepository{
'order_total_integral',
'order_total_integral_price',
'orderTotalWineIntegral',
'orderTotalWineIntegralPrice',
'orderTotalWineDiffMoney',
'orderTotalWineDiffMoneyPrice',
'orderTotalRandomReduction',
'orderTotalMerQuota',
'order_total_give_integral',
'order_svip_discount',
@ -1187,6 +1190,8 @@ class StoreOrderCreateRepository extends StoreOrderRepository{
'point_id' => $merchantCart['take']['id'] ?? 0,
// 随机立减金额
'random_reduction' => $merchantCart['order']['random_reduction'] ?? 0,
// 进货订单 - 补货额度抵扣金额
'mer_quota_deduction' => $merchantCart['order']['mer_quota_deduction'] ?? 0,
];
$allUseCoupon = array_merge($allUseCoupon,$merchantCart['order']['useCouponIds']);
@ -1224,6 +1229,8 @@ class StoreOrderCreateRepository extends StoreOrderRepository{
'wine_diff_money_price' => $orderInfo['orderTotalWineDiffMoneyPrice'] ?? 0,
// 随机立减金额
'random_reduction' => $orderInfo['orderTotalRandomReduction'] ?? 0,
// 进货订单 - 补货额度抵扣金额
'mer_quota_deduction' => $orderInfo['mer_quota_deduction'] ?? 0,
];
event('order.create.before',compact('groupOrder','orderList'));
@ -1492,6 +1499,8 @@ class StoreOrderCreateRepository extends StoreOrderRepository{
'province_send_id' => $cart['province_send_id'] ?? 0,
// 随机立减金额
'random_reduction' => $cart['random_reduction'] ?? 0,
// 进货订单 - 补货额度抵扣金额
'mer_quota_deduction' => $cart['mer_quota_deduction'] ?? 0,
];
}
$userMerchantRepository->getInfo($uid,$order['mer_id']);

View File

@ -34,15 +34,17 @@ class MerchantQuotaRecordRepository extends BaseRepository{
/**
* Common: 商户补货额度变更 并且添加变更记录
* Author: wu-hui
* Time: 2024/05/27 11:51
* @param $params
* @return array|\think\Model
* Time: 2024/05/27 15:56
* @param int $merId 商户id
* @param float $quantity 变更数量
* @param int $changeType 变更类型0=减少1=增加
* @param int $source 变更来源0=后台手动处理1=补货减少
* @param int $orderId 订单id
* @return mixed
*/
public function changeQuota($params){
$quantity = abs($params['change_quantity'] ?? 0);
$merId = $params['mer_id'] ?? 0;
public function changeQuota(int $merId, float $quantity,int $changeType,int $source = 0,int $orderId = 0){
// 是否允许生成兑换码
if(!in_array((int)$params['change_type'],[0, 1])) throw new ValidateException('非法请求,变更类型不明确!');
if(!in_array($changeType,[0, 1])) throw new ValidateException('非法请求,变更类型不明确!');
if($quantity <= 0) throw new ValidateException('变更数量必须大于0');
if($merId <= 0) throw new ValidateException('非法请求,商户不明确!');
// 处理
@ -52,7 +54,7 @@ class MerchantQuotaRecordRepository extends BaseRepository{
$merInfo = app()->make(MerchantRepository::class)->getSearch(['mer_id'=>$merId])->field('quota_total,quota_used,quota_surplus')->findOrEmpty();
$changeFront = $merInfo->quota_surplus ?? 0;// 变更前数量
// 根据操作类型进行处理 变更类型0=减少1=增加
if((int)$params['change_type'] == 1){
if($changeType == 1){
// 增加
$changeAfter = sprintf("%.2f", $changeFront + $quantity);
$merInfo->quota_total += $quantity;
@ -67,10 +69,12 @@ class MerchantQuotaRecordRepository extends BaseRepository{
// 记录
$record = [
'mer_id' => $merId,
'change_type' => (int)$params['change_type'],
'change_type' => (int)$changeType,
'change_front' => $changeFront,
'change_quantity' => $quantity,
'change_after' => $changeAfter,
'source' => $source,
'order_id' => $orderId
];
$this->dao->create($record);

View File

@ -389,7 +389,8 @@ class Merchant extends BaseController
['change_quantity', 0],
['mer_id', 0]
]);
app()->make(MerchantQuotaRecordRepository::class)->changeQuota($params);
app()->make(MerchantQuotaRecordRepository::class)
->changeQuota((int)$params['mer_id'],(float)$params['change_quantity'],(int)$params['change_type']);
return app('json')->success('变更成功');
}

View File

@ -10,6 +10,7 @@ use app\common\model\user\User;
use app\common\model\user\UserInviteCode;
use app\common\repositories\marketing\activity\RecordRepository;
use app\common\repositories\marketing\AgentBrokerageRepository;
use app\common\repositories\system\merchant\MerchantQuotaRecordRepository;
use app\common\repositories\system\merchant\MerchantRepository;
use app\common\repositories\user\ExchangePickupPointRepository;
use app\common\repositories\user\ExchangeQuotaRepository;
@ -32,21 +33,26 @@ class OrderPaySuccessEvent{
if($groupOrder->activity_type == 30){
// 在线支付订单
$this->giveExchangeIntegral($groupOrder);
}else if($groupOrder->activity_type == 31){
}
else if($groupOrder->activity_type == 31){
// 兑换商品补差价处理
$this->exchangeGoodsHandle($groupOrder);
}else if($groupOrder->activity_type == 32){
}
else if($groupOrder->activity_type == 32){
// 代理入驻支付
$this->agentPaySuccessHandle($groupOrder);
}else if($groupOrder->activity_type == 33){
}
else if($groupOrder->activity_type == 33){
// 加入活动支付成功
$this->joinActivitySuccessHandle($groupOrder);
}else if($groupOrder->activity_type == 34){
}
else if($groupOrder->activity_type == 34){
// 邀请码激活
$this->inviteCodeActivation($groupOrder);
}else if($groupOrder->activity_type == 35){
// 酒道馆进货订单 暂无处理
}
else if($groupOrder->activity_type == 35){
// 进货订单
$this->quotaHandle($groupOrder);
}else{
// 其他订单
$this->orderPaySuccessHandle($groupOrder);
@ -68,15 +74,23 @@ class OrderPaySuccessEvent{
if($groupOrder->activity_type == 30){
// 在线支付订单
Log::info('支付成功 - 赠送酒水卡积分 - 错误: '.var_export($data,1));
}else if($groupOrder->activity_type == 31){
}
else if($groupOrder->activity_type == 31){
Log::info('支付成功 - 兑换商品补差价处理 - 错误: '.var_export($data,1));
}else if($groupOrder->activity_type == 32){
}
else if($groupOrder->activity_type == 32){
Log::info('支付成功 - 邀请代理人员奖励及免审核 - 错误: '.var_export($data,1));
}else if($groupOrder->activity_type == 33){
}
else if($groupOrder->activity_type == 33){
Log::info('支付成功 - 加入活动支付成功 - 错误: '.var_export($data,1));
}else if($groupOrder->activity_type == 34){
}
else if($groupOrder->activity_type == 34){
Log::info('支付成功 - 邀请码激活 - 错误: '.var_export($data,1));
}else{
}
else if($groupOrder->activity_type == 35){
Log::info('支付成功 - 进货订单 - 错误: '.var_export($data,1));
}
else{
Log::info('支付成功 - 赠送酒卡额度 - 错误: '.var_export($data,1));
}
}
@ -262,7 +276,21 @@ class OrderPaySuccessEvent{
return true;
}
// 支付成功 - 进货订单相关处理
public function quotaHandle($groupOrder){
// 循环处理
foreach($groupOrder->orderList as $orderInfo){
$with_goods_mer_id = $orderInfo->with_goods_mer_id ?? 0;
$mer_quota_deduction = $orderInfo->mer_quota_deduction ?? 0;
// 存在进货商户id 存在补货金额
if($with_goods_mer_id > 0 && $mer_quota_deduction > 0){
app()->make(MerchantQuotaRecordRepository::class)
->changeQuota((int)$with_goods_mer_id,(float)$mer_quota_deduction, 0, 1,(int)$orderInfo->order_id);
}
}
return true;
}