diff --git a/app/Admin/Controllers/ActivityController.php b/app/Admin/Controllers/ActivityController.php index 31b583d5..3cdd43f7 100644 --- a/app/Admin/Controllers/ActivityController.php +++ b/app/Admin/Controllers/ActivityController.php @@ -245,7 +245,8 @@ class ActivityController extends AdminController 0=>'按订单赠送', 1=>'其他活动来源', ])->default(0); - $form->number('value', '门槛')->help('每N元赠送一份'); + $form->number('value', '门槛')->help('满N元赠送'); + // $form->number('qty', '总量')->help('前N单能获得'); $form->radio('times', '可领次数')->options([ '0'=>'仅首单', '1'=>'不限', @@ -257,7 +258,8 @@ class ActivityController extends AdminController 0=>'按订单赠送', 1=>'其他活动来源', ])->default(0); - $form->number('value', '门槛')->help('每N元赠送一份'); + $form->number('value', '门槛')->help('满N元赠送'); + // $form->number('qty', '总量')->help('前N单能获得'); $form->radio('times', '可领次数')->options([ '0'=>'仅首单', '1'=>'不限', diff --git a/app/Models/UserCoupon.php b/app/Models/UserCoupon.php index 3b80e851..ece65ab8 100644 --- a/app/Models/UserCoupon.php +++ b/app/Models/UserCoupon.php @@ -42,6 +42,7 @@ class UserCoupon extends Model 'is_use', 'created_at', 'updated_at', + 'activity_id', ]; /** diff --git a/app/Services/CouponService.php b/app/Services/CouponService.php index de955f88..21200aba 100644 --- a/app/Services/CouponService.php +++ b/app/Services/CouponService.php @@ -92,6 +92,7 @@ class CouponService */ public function receiveActivityCoupons(Activity $activity, User $user) { + $someCoupons = []; foreach ($activity->coupons as $coupon) { $someCoupons[] = [ 'coupon'=>$coupon, @@ -121,7 +122,7 @@ class CouponService $coupon['coupon']->decrement('stock', $coupon['num']); } } - + // dd($userCoupons); UserCoupon::insert($userCoupons); } diff --git a/app/Services/OrderService.php b/app/Services/OrderService.php index 0712e643..553ca6b9 100644 --- a/app/Services/OrderService.php +++ b/app/Services/OrderService.php @@ -421,40 +421,45 @@ class OrderService } //返回赠品 $_gifts = $partActivity->activity->gifts; + $_num = 0; foreach ($_gifts as $_gift) { - $giveGifts[] = [ - 'gift_for_sku_id'=> null, - 'user_id' => $order->user_id, - 'order_id' => $order->id, - 'spu_id' => $_gift->spu_id, - 'sku_id' => $_gift->id, - 'category_id' => $_gift->category_id, - 'name' => $_gift->name, - 'specs' => json_encode($_gift->specs), - 'cover' => $_gift->cover, - 'weight' => $_gift->weight, - 'sell_price' => $_gift->sell_price, - 'vip_price' => $_gift->vip_price, - 'sales_value' => 0, // 赠品不算销售值 - 'quantity' => $_gift->pivot->qty, - 'remain_quantity' => $_gift->pivot->qty, // 剩余发货数量 - 'coupon_discount_amount' => 0, - 'vip_discount_amount' => 0, - 'total_amount' => 0, - 'created_at' => $order->created_at, - 'updated_at' => $order->updated_at, - 'is_gift'=> true, - 'activity_id'=>$partActivity->activity_id, - ]; - // 扣除商品库存 - $this->deductProduct($_gift, $_gift->pivot->qty); + if ($_gift->stock - $_gift->pivot->qty >= 0) { + $_num += $_gift->pivot->qty; + $giveGifts[] = [ + 'gift_for_sku_id'=> null, + 'user_id' => $order->user_id, + 'order_id' => $order->id, + 'spu_id' => $_gift->spu_id, + 'sku_id' => $_gift->id, + 'category_id' => $_gift->category_id, + 'name' => $_gift->name, + 'specs' => json_encode($_gift->specs), + 'cover' => $_gift->cover, + 'weight' => $_gift->weight, + 'sell_price' => $_gift->sell_price, + 'vip_price' => $_gift->vip_price, + 'sales_value' => 0, // 赠品不算销售值 + 'quantity' => $_gift->pivot->qty, + 'remain_quantity' => $_gift->pivot->qty, // 剩余发货数量 + 'coupon_discount_amount' => 0, + 'vip_discount_amount' => 0, + 'total_amount' => 0, + 'created_at' => $order->created_at, + 'updated_at' => $order->updated_at, + 'is_gift'=> true, + 'activity_id'=>$partActivity->activity_id, + ]; + // 扣除商品库存 + $this->deductProduct($_gift, $_gift->pivot->qty); + } + } + if ($_num > 0) { + //记录订单参与活动信息 + OrderActivity::firstOrCreate([ + 'order_id'=>$order->id, + 'activity_id'=>$partActivity->activity_id, + ]); } - - //记录订单参与活动信息 - OrderActivity::firstOrCreate([ - 'order_id'=>$order->id, - 'activity_id'=>$partActivity->activity_id, - ]); } return $giveGifts; @@ -1001,21 +1006,32 @@ class OrderService continue; } - $gift = ProductGift::where('sku_id', $product->gift_for_sku_id) - ->where('gift_sku_id', $product->sku_id) - ->first(); + //原赠品 + if ($product->gift_for_sku_id) { + $gift = ProductGift::where('sku_id', $product->gift_for_sku_id) + ->where('gift_sku_id', $product->sku_id) + ->first(); - if ($gift === null) { + if ($gift === null) { + continue; + } + + if ($gift->isLimit()) { + $gift->update([ + 'remaining' => DB::raw("remaining+{$product->quantity}"), + 'sent' => DB::raw("sent-{$product->quantity}"), + ]); + } else { + $gift->decrement('sent', $product->quantity); + } continue; - } - - if ($gift->isLimit()) { - $gift->update([ - 'remaining' => DB::raw("remaining+{$product->quantity}"), - 'sent' => DB::raw("sent-{$product->quantity}"), + } else {//新赠品, 则直接增加商品库存 + $product->spu?->increment('sales', -$product->quantity); + $product->sku?->update([ + 'stock' => DB::Raw("stock + {$product->quantity}"), + 'sales' => DB::Raw("sales - {$product->quantity}"), ]); - } else { - $gift->decrement('sent', $product->quantity); + continue; } }