|
@@ -439,7 +439,7 @@ class OrderService
|
|
|
* @param $payAmount
|
|
|
* @return array|mixed
|
|
|
*/
|
|
|
- public static function payUseCoupon($memberId, $goods, $coupon, $payAmount)
|
|
|
+ public static function payUseCoupon($type, $settlementNow, $memberId, $goods, $coupon, $payAmount)
|
|
|
{
|
|
|
try {
|
|
|
foreach ($coupon as $item) {
|
|
@@ -453,7 +453,7 @@ class OrderService
|
|
|
'cut_balance' => 0,
|
|
|
];
|
|
|
foreach ($goods as $good) {
|
|
|
- $result = OrderService::useCoupon($memberId, $goods, $good, $coupon, $amountBalance);
|
|
|
+ $result = OrderService::useCoupon($type,$settlementNow, $memberId, $goods, $good, $coupon, $amountBalance);
|
|
|
$amountBalance = [
|
|
|
'pay_amount' => $result['pay_amount'],
|
|
|
'welfare_balance' => $result['welfare_balance'],
|
|
@@ -476,7 +476,7 @@ class OrderService
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- public static function useCoupon($memberId, $goods, $good, $coupon, $amountBalance)
|
|
|
+ public static function useCoupon($type,$settlementNow, $memberId, $goods, $good, $coupon, $amountBalance)
|
|
|
{
|
|
|
try {
|
|
|
$cacheKey = "ORDER:USE:COUPON:" . $memberId;
|
|
@@ -500,12 +500,27 @@ class OrderService
|
|
|
->where('coupon_goods.join_coupon_goods_id', $goodsId)
|
|
|
->where('coupon_goods.join_coupon_goods_sku_id', $good['sku_id'])
|
|
|
->where('join_goods_coupon_id', $couponId)
|
|
|
- ->where('coupon_detail.join_coupon_detail_member_id', $memberId)
|
|
|
- ->where('coupon_detail.coupon_detail_status', 'ACTIVED')
|
|
|
+ ->where('coupon_detail.join_coupon_detail_member_id', $memberId);
|
|
|
+ if ($settlementNow == 'Y' && $type == 'pay') {
|
|
|
+ $couponDetail = $couponDetail->whereIn('coupon_detail.coupon_detail_status', ['ACTIVED', 'WAITING']);
|
|
|
+ } else {
|
|
|
+ $couponDetail = $couponDetail->where('coupon_detail.coupon_detail_status', 'ACTIVED');
|
|
|
+ }
|
|
|
+ $couponDetail = $couponDetail->orderBy('coupon_detail_id', 'DESC')
|
|
|
->first();
|
|
|
if (!$couponDetail) {
|
|
|
continue;
|
|
|
}
|
|
|
+ if ($settlementNow == 'Y') {
|
|
|
+ $updateData = [
|
|
|
+ 'coupon_detail_status' => 'USED',
|
|
|
+ 'coupon_detail_used_datetime' => date('Y-m-d H:i:s')
|
|
|
+ ];
|
|
|
+ } else {
|
|
|
+ $updateData = [
|
|
|
+ 'coupon_detail_status' => 'WAITING',
|
|
|
+ ];
|
|
|
+ }
|
|
|
// 计算优惠券包含的优惠商品的件数和总价
|
|
|
$countAndAmount = self::countAndAmount($goods, $couponId);
|
|
|
// 如果是计件
|
|
@@ -527,7 +542,8 @@ class OrderService
|
|
|
'coupon_detail_id' => [$couponDetail->coupon_detail_id]
|
|
|
];
|
|
|
Redis::set($cacheDiscountKey, json_encode($discountData, JSON_UNESCAPED_UNICODE));
|
|
|
- CouponDetail::where('coupon_detail_id', $couponDetail->coupon_detail_id)->update(['coupon_detail_status' => 'USED', 'coupon_detail_used_datetime' => date('Y-m-d H:i:s')]);
|
|
|
+
|
|
|
+ CouponDetail::where('coupon_detail_id', $couponDetail->coupon_detail_id)->update($updateData);
|
|
|
}
|
|
|
} elseif ($couponDetail->coupon_classify == '折扣券') {
|
|
|
if (Redis::sIsMember($cacheKey, $couponId)) {
|
|
@@ -545,14 +561,14 @@ class OrderService
|
|
|
'coupon_detail_id' => [$couponDetail->coupon_detail_id]
|
|
|
];
|
|
|
Redis::set($cacheDiscountKey, json_encode($discountData, JSON_UNESCAPED_UNICODE));
|
|
|
- CouponDetail::where('coupon_detail_id', $couponDetail->coupon_detail_id)->update(['coupon_detail_status' => 'USED', 'coupon_detail_used_datetime' => date('Y-m-d H:i:s')]);
|
|
|
+ CouponDetail::where('coupon_detail_id', $couponDetail->coupon_detail_id)->update($updateData);
|
|
|
}
|
|
|
} elseif (in_array($couponDetail->coupon_classify, ['抵用券', '赠品券'])) {
|
|
|
if (Redis::sIsMember($cacheKey, $couponId)) {
|
|
|
continue;
|
|
|
}
|
|
|
Redis::sAdd($cacheKey, $couponId);
|
|
|
- CouponDetail::where('coupon_detail_id', $couponDetail->coupon_detail_id)->update(['coupon_detail_status' => 'USED', 'coupon_detail_used_datetime' => date('Y-m-d H:i:s')]);
|
|
|
+ CouponDetail::where('coupon_detail_id', $couponDetail->coupon_detail_id)->update($updateData);
|
|
|
if ($good['nbr'] > 1) {
|
|
|
$diyongAmount = $good['goods_sales_price'];
|
|
|
$payAmount = $payAmount - $diyongAmount;
|
|
@@ -573,7 +589,7 @@ class OrderService
|
|
|
continue;
|
|
|
}
|
|
|
Redis::sAdd($cacheKey, $couponId);
|
|
|
- CouponDetail::where('coupon_detail_id', $couponDetail->coupon_detail_id)->update(['coupon_detail_status' => 'USED', 'coupon_detail_used_datetime' => date('Y-m-d H:i:s')]);
|
|
|
+ CouponDetail::where('coupon_detail_id', $couponDetail->coupon_detail_id)->update($updateData);
|
|
|
$fuliAmount = 0;
|
|
|
if (!empty($couponDetail->coupon_value)) {
|
|
|
$fuliAmount = $couponDetail->coupon_value;
|
|
@@ -651,7 +667,7 @@ class OrderService
|
|
|
* @param $amountBalance
|
|
|
* @return array|void
|
|
|
*/
|
|
|
- public static function chooseCoupon($memberId, $goods, $good, $coupon, $amountBalance)
|
|
|
+ public static function chooseCoupon($settlementNow, $memberId, $goods, $good, $coupon, $amountBalance)
|
|
|
{
|
|
|
try {
|
|
|
$cacheKey = "ORDER:USE:COUPON:" . $memberId;
|
|
@@ -667,8 +683,13 @@ class OrderService
|
|
|
->where('coupon_goods.join_coupon_goods_id', $goodsId)
|
|
|
->where('coupon_goods.join_coupon_goods_sku_id', $good['sku_id'])
|
|
|
->where('join_goods_coupon_id', $couponId)
|
|
|
- ->where('coupon_detail.join_coupon_detail_member_id', $memberId)
|
|
|
- ->where('coupon_detail.coupon_detail_status', 'ACTIVED')
|
|
|
+ ->where('coupon_detail.join_coupon_detail_member_id', $memberId);
|
|
|
+ if ($settlementNow == 'Y') {
|
|
|
+ $couponDetail = $couponDetail->whereIn('coupon_detail.coupon_detail_status', ['ACTIVED', 'WAITING']);
|
|
|
+ } else {
|
|
|
+ $couponDetail = $couponDetail->where('coupon_detail.coupon_detail_status', 'ACTIVED');
|
|
|
+ }
|
|
|
+ $couponDetail = $couponDetail->orderBy('coupon_detail_id', 'DESC')
|
|
|
->first();
|
|
|
if (!$couponDetail) {
|
|
|
continue;
|