|
@@ -155,4 +155,80 @@ class GiftController extends Controller
|
|
|
'message' => trans('gift::app.giftcard.remove'),
|
|
'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);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
}
|
|
}
|