|null $product_ids Null means every product * @property CarbonInterface|null $starts_at * @property CarbonInterface|null $ends_at * @property bool $active * @property-read Store $store * * @phpstan-import-type CartLine from Cart */ class AutomaticDiscount extends Model { /** * @return BelongsTo */ public function store(): BelongsTo { return $this->belongsTo(Store::class); } /** * Switched on, and today is within its dates. */ public function isLive(): bool { return $this->status() === 'active'; } public function status(): string { return match (true) { ! $this->active => 'off', $this->starts_at !== null && $this->starts_at->isFuture() => 'scheduled', $this->ends_at !== null && $this->ends_at->isPast() => 'expired', default => 'active', }; } public function appliesTo(Product $product): bool { return $this->product_ids === null || in_array($product->id, $this->product_ids, true); } /** * The cart lines this discount covers. * * @param Collection $lines * @return Collection */ public function eligible(Collection $lines): Collection { return $lines->filter(fn (array $line) => $this->appliesTo($line['product']))->values(); } /** * How much comes off these cart lines, in sen. Zero when the cart does not qualify. * * @param Collection $lines */ public function amountFor(Collection $lines): int { $eligible = $this->eligible($lines); $subtotal = (int) $eligible->sum('line_total'); if ($subtotal === 0 || $this->shortfall($lines) !== null) { return 0; } $amount = $this->type === DiscountCode::PERCENT ? intdiv($subtotal * min(100, $this->value), 100) : $this->value; return max(0, min($amount, $subtotal)); } /** * What the cart still needs to qualify: ["items" => 1] or ["amount" => 1200 sen]. Null when it qualifies. * * @param Collection $lines * @return array{items?: int, amount?: int}|null */ public function shortfall(Collection $lines): ?array { $eligible = $this->eligible($lines); $quantity = (int) $eligible->sum('quantity'); $subtotal = (int) $eligible->sum('line_total'); if ($this->minimum_quantity !== null && $quantity < $this->minimum_quantity) { return ['items' => $this->minimum_quantity - $quantity]; } if ($this->minimum_subtotal !== null && $subtotal < $this->minimum_subtotal) { return ['amount' => $this->minimum_subtotal - $subtotal]; } return null; } /** * "10% off" or "RM 5.00 off", in the store's language. */ public function label(string $language = 'en'): string { $amount = $this->type === DiscountCode::PERCENT ? $this->value.'%' : 'RM '.number_format($this->value / 100, 2); return $language === 'ms' ? 'diskaun '.$amount : $amount.' off'; } /** * The whole offer in one line for shoppers: "Buy 2 or more, get 10% off". */ public function describe(string $language = 'en'): string { $malay = $language === 'ms'; $condition = match (true) { $this->minimum_quantity !== null && $this->minimum_quantity > 1 => $malay ? "Beli {$this->minimum_quantity} atau lebih" : "Buy {$this->minimum_quantity} or more", $this->minimum_subtotal !== null => $malay ? 'Belanja RM '.number_format($this->minimum_subtotal / 100, 2).' atau lebih' : 'Spend RM '.number_format($this->minimum_subtotal / 100, 2).' or more', default => null, }; if ($condition === null) { return ucfirst($this->label($language)); } return $condition.($malay ? ', dapat ' : ', get ').$this->label($language); } /** * @return array */ protected function casts(): array { return [ 'value' => 'integer', 'minimum_quantity' => 'integer', 'minimum_subtotal' => 'integer', 'product_ids' => 'array', 'starts_at' => 'datetime', 'ends_at' => 'datetime', 'active' => 'boolean', ]; } }