'decimal:2', 'min_order_amount' => 'decimal:2', 'valid_from' => 'datetime', 'valid_to' => 'datetime', 'status' => 'integer', ]; public const TABLE_NAME = 'coupons'; protected $table = self::TABLE_NAME; public function user(): BelongsTo { return $this->belongsTo(User::class, 'user_id'); } public function usages(): HasMany { return $this->hasMany(CouponUsage::class, 'coupon_id'); } // Scopes public function scopeActive($query): mixed { return $query->where('status', 1) ->where(function ($q) { $q->whereNull('valid_from')->orWhere('valid_from', '<=', now()); }) ->where(function ($q) { $q->whereNull('valid_to')->orWhere('valid_to', '>=', now()); }); } // Helper: Calculate discount for an order public function calculateDiscount(float $orderTotal): float { if ($this->discount_type === 'fixed') { return min($this->amount, $orderTotal); } if ($this->discount_type === 'percentage') { return round($orderTotal * ($this->amount / 100), 2); } if ($this->discount_type === 'free_delivery') { return 0; // discount only applies to delivery } return 0; } }