Spaces:
Sleeping
Sleeping
File size: 1,158 Bytes
10dc6f2 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | <?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class PromoCode extends Model
{
protected $fillable = ['code', 'type', 'value', 'start_at', 'end_at', 'usage_limit', 'used_count'];
protected function casts(): array
{
return [
'value' => 'decimal:2',
'start_at' => 'datetime',
'end_at' => 'datetime',
];
}
// ββ Helpers ββββββββββββββββββββββββββββββββββββββββββ
public function isValid(): bool
{
$now = now();
if ($this->start_at && $now->lt($this->start_at)) return false;
if ($this->end_at && $now->gt($this->end_at)) return false;
if ($this->usage_limit !== null && $this->used_count >= $this->usage_limit) return false;
return true;
}
public function applyDiscount(float $subtotal): float
{
if (!$this->isValid()) return 0;
if ($this->type === 'percent') {
return round($subtotal * ($this->value / 100), 2);
}
return min($this->value, $subtotal);
}
}
|