D7net
Home
Console
Upload
information
Create File
Create Folder
About
Tools
:
/
home
/
forge
/
developers.ghanempharmacy.com
/
app
/
Models
/
Filename :
GiftConfiguration.php
back
Copy
<?php namespace App\Models; use Illuminate\Database\Eloquent\Model; use Carbon\Carbon; use App\Traits\StorageTrait; class GiftConfiguration extends Model { use StorageTrait; protected $fillable = [ 'title', 'slug', 'description', 'gift_type', 'gift_money_type', 'gift_money_value', 'gift_money_products', 'gift_product_quantity', 'gift_product_list', 'priority', 'image', 'start_date', 'end_date', 'status', ]; protected $casts = [ 'start_date' => 'datetime', 'end_date' => 'datetime', 'status' => 'boolean', 'gift_money_products' => 'array', 'gift_product_list' => 'array', 'gift_money_value' => 'decimal:2', 'gift_product_quantity' => 'integer', 'priority' => 'integer', ]; protected $appends = ['image_full_url', 'gift_products']; /** * Get gift products for selection */ public function getGiftProductsAttribute() { if ($this->gift_type !== 'product_wise' || empty($this->gift_product_list)) { return collect([]); } return Product::whereIn('id', $this->gift_product_list)->get(); } /** * Products assigned to this gift */ public function assignedProducts() { return $this->belongsToMany(Product::class, 'gift_product_assignments', 'gift_configuration_id', 'product_id') ->withTimestamps(); } /** * Get gifts available for specific products */ public function scopeForProducts($query, array $productIds) { return $query->whereHas('assignedProducts', function ($q) use ($productIds) { $q->whereIn('product_id', $productIds); }); } /** * Check if gift is currently active */ public function isActive(): bool { if (!$this->status) { return false; } $now = Carbon::now(); if ($this->start_date && $now->lt($this->start_date)) { return false; } if ($this->end_date && $now->gt($this->end_date)) { return false; } return true; } /** * Get active gifts */ public function scopeActive($query) { return $query->where('status', 1) ->where(function ($q) { $q->whereNull('start_date') ->orWhere('start_date', '<=', Carbon::now()); }) ->where(function ($q) { $q->whereNull('end_date') ->orWhere('end_date', '>=', Carbon::now()); }); } /** * Calculate cashback for money-wise gifts */ public function calculateCashback(float $amount): float { if ($this->gift_type !== 'money_wise') { return 0.0; } if ($this->gift_money_type === 'percentage') { return (float) (($amount * $this->gift_money_value) / 100); } return (float) $this->gift_money_value; } /** * Get products eligible for money-wise cashback redemption */ public function getGiftMoneyProducts(): array { if ($this->gift_type !== 'money_wise') { return []; } return $this->gift_money_products ?? []; } /** * Get product options for product-wise gifts */ public function getGiftProductOptions(): array { if ($this->gift_type !== 'product_wise') { return []; } return $this->gift_product_list ?? []; } /** * Get maximum quantity for product-wise gifts */ public function getMaxGiftQuantity(): int { if ($this->gift_type !== 'product_wise') { return 0; } return $this->gift_product_quantity; } /** * Validate product-wise gift selection */ public function validateGiftProductSelection(array $selectedIds): array { if ($this->gift_type !== 'product_wise') { return ['valid' => false, 'message' => translate('invalid_gift_type')]; } // Check quantity limit if (count($selectedIds) > $this->gift_product_quantity) { return ['valid' => false, 'message' => translate('gift_quantity_limit_exceeded')]; } // Check if all selected products are in the allowed list $allowedProducts = $this->gift_product_list ?? []; foreach ($selectedIds as $productId) { if (!in_array($productId, $allowedProducts)) { return ['valid' => false, 'message' => translate('selected_product_is_not_allowed_as_gift')]; } } return ['valid' => true]; } public function getImageFullUrlAttribute(): array { $value = $this->image; return $this->storageLink('gift', $value, 'public'); } }