D7net
Home
Console
Upload
information
Create File
Create Folder
About
Tools
:
/
home
/
forge
/
developers.ghanempharmacy.com
/
app
/
Services
/
Filename :
GiftService.php
back
Copy
<?php namespace App\Services; use App\Models\GiftConfiguration; use App\Models\Cart; use Illuminate\Support\Collection; class GiftService { /** * Get available gifts for cart items */ public function getAvailableGiftsForCart($customerId = null, $guestId = null): Collection { // Get cart items $cartQuery = Cart::query(); if ($customerId) { $cartQuery->where('customer_id', $customerId)->where('is_guest', 0); } elseif ($guestId) { $cartQuery->where('customer_id', $guestId)->where('is_guest', 1); } else { return collect([]); } $cartItems = $cartQuery->get(); if ($cartItems->isEmpty()) { return collect([]); } // Get product IDs from cart $productIds = $cartItems->pluck('product_id')->unique()->toArray(); // Get active gifts for these products $gifts = GiftConfiguration::active() ->forProducts($productIds) ->with(['assignedProducts']) ->orderBy('priority', 'desc') ->get(); return $gifts; } /** * Calculate cashback for money-wise gift */ public function calculateCashback(GiftConfiguration $gift, Collection $cartItems): float { if ($gift->gift_type !== 'money_wise') { return 0.0; } // Get eligible products for cashback from gift_money_products $eligibleProducts = $gift->gift_money_products ?? []; // If no specific products are selected for cashback, fallback to the assigned products if (empty($eligibleProducts)) { $eligibleProducts = $gift->assignedProducts->pluck('id')->toArray(); } // Check if all assigned products are present in the cart for this gift to be valid $assignedProducts = $gift->assignedProducts; foreach ($assignedProducts as $assigned) { if (!$cartItems->contains('product_id', $assigned->id)) { return 0.0; // Return 0 cashback if any assigned product is missing } } // Filter cart items to include only eligible products $eligibleTotal = $cartItems->filter(function ($item) use ($eligibleProducts) { if (empty($eligibleProducts)) { return true; // IF somehow assignedProducts is also empty, apply to all } return in_array($item->product_id, $eligibleProducts); })->sum(function ($item) { return ($item->price - $item->discount) * $item->quantity; }); return $gift->calculateCashback((float) $eligibleTotal); } /** * Validate product-wise gift selection */ public function validateProductSelection(GiftConfiguration $gift, array $selectedProductIds): array { if ($gift->gift_type !== 'product_wise') { return ['valid' => false, 'message' => translate('invalid_gift_type')]; } return $gift->validateGiftProductSelection($selectedProductIds); } /** * Get gift products for selection */ public function getGiftProducts(GiftConfiguration $gift): array { if ($gift->gift_type === 'product_wise') { return $gift->getGiftProductOptions(); } return []; } /** * Check if customer is eligible for gift */ public function isEligibleForGift(GiftConfiguration $gift, $customerId = null, $guestId = null): bool { if (!$gift->isActive()) { return false; } // Get cart items $cartQuery = Cart::query(); if ($customerId) { $cartQuery->where('customer_id', $customerId)->where('is_guest', 0); } elseif ($guestId) { $cartQuery->where('customer_id', $guestId)->where('is_guest', 1); } else { return false; } $cartItems = $cartQuery->where('variant', '!=', 'gift')->get(); $productIds = $cartItems->pluck('product_id')->toArray(); // Check if all assigned products are present in the cart $assignedProductIds = $gift->assignedProducts->pluck('id')->toArray(); if (empty($assignedProductIds)) return false; foreach ($assignedProductIds as $id) { if (!in_array($id, $productIds)) { return false; } } return true; } /** * Revalidate and remove applied gift if not eligible anymore */ public function removeAppliedGiftIfNotEligible($customerId = null, $guestId = null) { if (!session()->has('gift_id')) { return; } $giftId = session('gift_id'); $gift = GiftConfiguration::find($giftId); if (!$gift || !$this->isEligibleForGift($gift, $customerId, $guestId)) { // Get assigned products for the gift before clearing session $assignedProductIds = $gift ? $gift->assignedProducts->pluck('id')->toArray() : []; // Remove gift session data session()->forget([ 'gift_id', 'gift_type', 'gift_discount', 'gift_wallet_credit', 'gift_products' ]); // Define customer/guest filter $filter = function ($query) use ($customerId, $guestId) { if ($customerId) { $query->where('customer_id', $customerId)->where('is_guest', 0); } elseif ($guestId) { $query->where('customer_id', $guestId)->where('is_guest', 1); } }; // Remove gift products from cart (variant = 'gift') Cart::where($filter)->where('variant', 'gift')->delete(); // Remove all associated products that were part of the offer (assignedProducts) if (!empty($assignedProductIds)) { Cart::where($filter)->whereIn('product_id', $assignedProductIds)->delete(); } } else { // Re-calculate cashback if it's money_wise, as cart totals might have changed if ($gift->gift_type == 'money_wise') { $cartQuery = Cart::query(); if ($customerId) { $cartQuery->where('customer_id', $customerId)->where('is_guest', 0); } elseif ($guestId) { $cartQuery->where('customer_id', $guestId)->where('is_guest', 1); } $cartItems = $cartQuery->where('variant', '!=', 'gift')->get(); $cashbackAmount = $this->calculateCashback($gift, $cartItems); if (session('gift_type') == 'immediate_discount') { session()->put('gift_discount', $cashbackAmount); } elseif (session('gift_type') == 'wallet_credit') { session()->put('gift_wallet_credit', $cashbackAmount); } } } } }