'boolean', 'expires_at' => 'datetime', 'value' => 'decimal:2', 'min_order_value' => 'decimal:2', ]; public static function findValid(string $code): ?self { return self::where('code', strtoupper(trim($code))) ->where('active', true) ->where(fn($q) => $q->whereNull('expires_at')->orWhere('expires_at', '>=', now())) ->where(fn($q) => $q->whereNull('max_uses')->orWhereColumn('used_count', '<', 'max_uses')) ->first(); } public function calcDiscount(float $subtotal): float { if ($this->min_order_value && $subtotal < $this->min_order_value) { return 0.0; } if ($this->type === 'percent') { return round($subtotal * $this->value / 100, 2); } return min((float) $this->value, $subtotal); } }