llp 2 месяцев назад
Родитель
Сommit
e8c8882922

+ 76 - 0
packages/Longyi/Gift/src/Http/Controllers/Shop/GiftController.php

@@ -155,4 +155,80 @@ class GiftController extends Controller
             'message'  => trans('gift::app.giftcard.remove'),
         ]);
     }
+
+    /**
+     * Get the gift card that expires soonest (within specified days)
+     */
+    public function getExpiringSoonGiftCard(int $days = 7): ?GiftCards
+    {
+        try {
+            $customerId = auth()->user()->id;
+
+            $expiringDate = now()->addDays($days);
+
+            $giftCard = GiftCards::where('customer_id', $customerId)
+                ->where('remaining_giftcard_amount', '>', 0)
+                ->where('expirationdate', '>=', now())
+                ->where('expirationdate', '<=', $expiringDate)
+                ->orderBy('expirationdate', 'asc')
+                ->first();
+            return $giftCard;
+        } catch (\Exception $e) {
+            return null;
+        }
+    }
+
+    /**
+     * Automatically apply the soonest expiring gift card to cart
+     */
+    public function autoApplyExpiringGiftCard(): JsonResponse
+    {
+        try {
+            if (!auth()->check()) {
+                return response()->json([
+                    'success' => false,
+                    'message' => 'User not authenticated',
+                ], Response::HTTP_UNAUTHORIZED);
+            }
+            $cart = Cart::getCart();
+            if (!$cart) {
+                return response()->json([
+                    'success' => false,
+                    'message' => 'Cart not found',
+                ], Response::HTTP_NOT_FOUND);
+            }
+            if (!empty($cart->giftcard_number)) {
+                return response()->json([
+                    'success' => false,
+                    'message' => 'A gift card is already applied to the cart',
+                    'applied' => true,
+                ], Response::HTTP_OK);
+            }
+            $expiringGiftCard = $this->getExpiringSoonGiftCard(10);
+            if (!$expiringGiftCard) {
+                return response()->json([
+                    'success' => false,
+                    'message' => 'No expiring gift cards found',
+                    'applied' => false,
+                ], Response::HTTP_OK);
+            }
+            GiftCards::setGiftCardCode($expiringGiftCard);
+            Cart::collectTotals();
+            return response()->json([
+                'success' => true,
+                'message' => 'Expiring gift card applied automatically',
+                'giftcard_number' => $expiringGiftCard->giftcard_number,
+                'expiration_date' => $expiringGiftCard->expirationdate->format('Y-m-d'),
+                'remaining_amount' => $expiringGiftCard->remaining_giftcard_amount,
+                'applied' => true,
+            ], Response::HTTP_OK);
+        } catch (\Exception $e) {
+            return response()->json([
+                'success' => false,
+                'message' => 'Failed to auto-apply gift card',
+                'error' => $e->getMessage(),
+            ], Response::HTTP_INTERNAL_SERVER_ERROR);
+        }
+    }
+
 }

+ 30 - 0
packages/Longyi/Gift/src/Resources/views/components/giftcard-cartsummary.blade.php

@@ -126,7 +126,36 @@
                 };
             },
 
+            mounted() {
+                this.autoApplyExpiringGiftCard();
+            },
+
             methods: {
+                // 自动应用即将过期的礼品卡
+                autoApplyExpiringGiftCard() {
+                    console.log('Checking for expiring gift cards...');
+                    this.$axios.post("{{ route('shop.api.checkout.cart.giftcard.auto-apply') }}", {
+                        _token: "{{ csrf_token() }}"
+                    })
+                        .then((response) => {
+                            console.log('Auto-apply response:', response.data);
+                            if (response.data.success && response.data.applied) {
+                                this.$emitter.emit('add-flash', {
+                                    type: 'success',
+                                    message: `已自动应用即将过期的礼品卡: ${response.data.giftcard_number} (有效期至: ${response.data.expiration_date})`
+                                });
+                                this.$emit('giftcard-applied', response.data);
+                            } else if (response.data.applied) {
+                                console.log('Gift card already applied');
+                            } else {
+                                console.log('No expiring gift cards found');
+                            }
+                        })
+                        .catch((error) => {
+                            console.error('Auto-apply gift card error:', error);
+                        });
+                },
+
                 // 加载用户的礼品卡列表
                 loadGiftCards() {
                     console.log('Loading gift cards...');
@@ -277,3 +306,4 @@
 {{--</div>--}}
 {!! view_render_event('bagisto.shop.checkout.onepage.summary.giftcard_amount.after') !!}
 
+

+ 1 - 0
packages/Longyi/Gift/src/Routes/shop-routes.php

@@ -10,4 +10,5 @@ Route::group(['middleware' => ['web', 'theme', 'locale', 'currency', 'customer']
     Route::get('/my-gift-cards', [GiftController::class, 'getCustomerGiftCards'])->name('shop.api.checkout.cart.giftcard.list');
     Route::post('/activate', [GiftController::class, 'activateGiftCard'])->name('shop.api.checkout.cart.giftcard.activate');
     Route::delete('/remove', [GiftController::class, 'destroyGiftCard'])->name('shop.api.checkout.cart.giftcard.remove');
+    Route::post('/auto-apply-expiring', [GiftController::class, 'autoApplyExpiringGiftCard'])->name('shop.api.checkout.cart.giftcard.auto-apply');
 });