D7net
Home
Console
Upload
information
Create File
Create Folder
About
Tools
:
/
home
/
forge
/
ghanempharmacy.com
/
app
/
Models
/
Filename :
ProductBundle.php
back
Copy
<?php namespace App\Models; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\HasMany; use Carbon\Carbon; use App\Traits\StorageTrait; class ProductBundle extends Model { use StorageTrait; protected $fillable = [ 'title', 'slug', 'description', 'bundle_price', 'discount_type', 'discount_amount', 'image', 'priority', 'start_date', 'end_date', 'status', // 'has_gift', // 'gift_type', // 'gift_money_type', // 'gift_money_value', // 'gift_money_products', // 'gift_product_quantity', // 'gift_product_list', ]; protected $casts = [ 'start_date' => 'datetime', 'end_date' => 'datetime', 'status' => 'boolean', 'bundle_price' => 'decimal:2', 'discount_amount' => 'decimal:2', 'priority' => 'integer', // 'has_gift' => 'boolean', // 'gift_money_products' => 'array', // 'gift_product_list' => 'array', // 'gift_money_value' => 'decimal:2', // 'gift_product_quantity' => 'integer', ]; protected $appends = ['image_full_url']; /** * Get the bundle items */ public function bundleItems(): HasMany { return $this->hasMany(ProductBundleItem::class, 'bundle_id'); } /** * Calculate original price (sum of all products) */ public function getOriginalPrice(): float { $total = 0; foreach ($this->bundleItems as $item) { if ($item->product) { $total += $item->product->unit_price * $item->quantity; } } return $total; } /** * Calculate savings amount */ public function getSavingsAmount(): float { $original = $this->getOriginalPrice(); return $original - $this->bundle_price; } /** * Calculate savings percentage */ public function getSavingsPercentage(): float { $original = $this->getOriginalPrice(); if ($original <= 0) { return 0; } return ($this->getSavingsAmount() / $original) * 100; } /** * Check if bundle 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 bundles */ 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()); }); } /** * Get discounted price */ public function getDiscountedPrice(): float { if ($this->discount_type === 'percentage') { return $this->bundle_price - ($this->bundle_price * ($this->discount_amount / 100)); } return $this->bundle_price - $this->discount_amount; } /** * Get final price (considering both bundle price and additional discount) */ public function getFinalPrice(): float { if ($this->discount_amount > 0) { return $this->getDiscountedPrice(); } return $this->bundle_price; } /** * Check if bundle has a gift */ /* // Legacy Gift Methods - Moved to GiftConfiguration public function hasGift(): bool { return $this->has_gift && $this->gift_type !== null; } public function getGiftType(): ?string { return $this->gift_type; } public function calculateCashback(float $bundlePrice = null): float { if (!$this->hasGift() || $this->gift_type !== 'money_wise') { return 0; } $price = $bundlePrice ?? $this->getFinalPrice(); if ($this->gift_money_type === 'percentage') { return ($price * $this->gift_money_value) / 100; } return $this->gift_money_value; } public function getGiftMoneyProducts(): array { if ($this->gift_type !== 'money_wise') { return []; } return $this->gift_money_products ?? []; } public function getGiftProductOptions(): array { if ($this->gift_type !== 'product_wise') { return []; } return $this->gift_product_list ?? []; } public function getMaxGiftQuantity(): int { if ($this->gift_type !== 'product_wise') { return 0; } return $this->gift_product_quantity; } 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('bundle', $value, 'public'); } }