feat : coupon

This commit is contained in:
2025-03-20 17:59:35 +08:00
parent 3cf870d1f7
commit 469cec1e6a

View File

@@ -293,33 +293,33 @@ trait OrderTrait
$this->orderRes['total_sundry_price'] = bcmul($this->orderRes['sundry_price'],(string)$this->orderRes['sundry_num'],2);
}
/**
* 计算优惠
* @return void
*/
protected function computeFavorable(): void
{
if ($this->couponId <= 0 && $this->isAutoSelectCoupon == 1) {
$this->couponId = $this->getAutoCouponId();
}
if ($this->couponId <= 0) {
$this->orderRes['coupon_id'] = 0;
$this->orderRes['coupon'] = [];
$this->orderRes['favorable_good_price'] = '0';
$this->orderRes['favorable_sundry_price'] = '0';
return;
}
$couponInfo = []; // todo 优惠券信息
$this->orderRes['coupon_id'] = $this->couponId;
$this->orderRes['coupon'] = $couponInfo;
//todo 优惠计算
$this->orderRes['favorable_good_price'] = '1.00';
$this->orderRes['favorable_sundry_price'] = '1.00';
}
// /**
// * 计算优惠
// * @return void
// */
// protected function computeFavorable(): void
// {
// if ($this->couponId <= 0 && $this->isAutoSelectCoupon == 1) {
// $this->couponId = $this->getAutoCouponId();
// }
//
// if ($this->couponId <= 0) {
// $this->orderRes['coupon_id'] = 0;
// $this->orderRes['coupon'] = [];
// $this->orderRes['favorable_good_price'] = '0';
// $this->orderRes['favorable_sundry_price'] = '0';
// return;
// }
//
// $couponInfo = []; // todo 优惠券信息
//
// $this->orderRes['coupon_id'] = $this->couponId;
// $this->orderRes['coupon'] = $couponInfo;
//
// //todo 优惠计算
// $this->orderRes['favorable_good_price'] = '1.00';
// $this->orderRes['favorable_sundry_price'] = '1.00';
// }
/**
* 计算最终价格
@@ -362,7 +362,7 @@ trait OrderTrait
$orderMaxPrice = max(array_column($this->orderRes['good'],'price'));
$this->orderRes['coupon']['amount'] = match ($this->orderRes['coupon']['coupon_type']) {
CouponCode::COUPON_TYPE_INSTANT_REDUCTION => $this->orderRes['coupon']['amount'],
CouponCode::COUPON_TYPE_DISCOUNT => bcmul(bcdiv("100", bcsub("100",$this->orderRes['coupon']['ratio'],2),2),$orderMaxPrice,2),
CouponCode::COUPON_TYPE_DISCOUNT => bcmul(bcdiv("100", bcsub("100",(string)$this->orderRes['coupon']['ratio'],2),2),(string)$orderMaxPrice,2),
default => 0
};
}
@@ -388,7 +388,7 @@ trait OrderTrait
if (empty($couponTemplateInfo)) throw new ErrException('优惠券信息错误');
$this->couponId = $couponInfo->id;
$this->orderRes['coupon_info'] = [
$this->orderRes['coupon'] = [
'id' => $couponInfo->id,
'coupon_type' => $couponTemplateInfo->coupon_type,
'amount' => $couponTemplateInfo->amount,