D7net
Home
Console
Upload
information
Create File
Create Folder
About
Tools
:
/
home
/
forge
/
ghanempharmacy.com
/
app
/
Models
/
Filename :
GiftDeal.php
back
Copy
<?php namespace App\Models; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\BelongsTo; class GiftDeal extends Model { protected $fillable = [ 'title', 'slug', 'buy_product_id', 'buy_products', 'min_buy_quantity', 'gift_product_id', 'gift_products', 'start_date', 'end_date', 'status', ]; protected $casts = [ 'start_date' => 'datetime', 'end_date' => 'datetime', 'status' => 'boolean', 'gift_products' => 'array', 'buy_products' => 'array', 'min_buy_quantity' => 'integer', ]; public function buyProduct(): BelongsTo { return $this->belongsTo(Product::class, 'buy_product_id'); } public function giftProduct(): BelongsTo { return $this->belongsTo(Product::class, 'gift_product_id'); } /** * Get gift products list with quantities * Returns: [['product_id' => x, 'quantity' => y], ...] */ public function getGiftProductsList(): array { // Support both new JSON format and legacy single product if (!empty($this->gift_products)) { return $this->gift_products; } if ($this->gift_product_id) { return [['product_id' => $this->gift_product_id, 'quantity' => 1]]; } return []; } /** * Get buy products list with minimum quantities * Returns: [['product_id' => x, 'min_quantity' => y], ...] */ public function getBuyProductsList(): array { // Support both new JSON format and legacy single product if (!empty($this->buy_products)) { return $this->buy_products; } if ($this->buy_product_id) { return [['product_id' => $this->buy_product_id, 'min_quantity' => $this->min_buy_quantity ?? 1]]; } return []; } /** * Check if a product purchase qualifies for this deal */ public function qualifiesForDeal(int $productId, int $quantity): bool { $buyProducts = $this->getBuyProductsList(); foreach ($buyProducts as $buyProduct) { if ($buyProduct['product_id'] == $productId) { $minQty = $buyProduct['min_quantity'] ?? 1; return $quantity >= $minQty; } } return false; } }