| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170 |
- <?php
-
- namespace App\Models;
-
- use App\Commerce\Cart;
- use Carbon\CarbonInterface;
- use Illuminate\Database\Eloquent\Model;
- use Illuminate\Database\Eloquent\Relations\BelongsTo;
- use Illuminate\Support\Collection;
-
- /**
- * A discount that applies by itself when the cart qualifies, with no code to type.
- * For example "Raya Sale: buy 2 or more, get 10% off" or "spend RM 100, get RM 15 off".
- * It can cover every product or only chosen ones; then only those count and only those get cheaper.
- * Amounts are in sen.
- *
- * @property int $id
- * @property int $store_id
- * @property string $name Shown to shoppers
- * @property string $type "percent" or "fixed"
- * @property int $value A whole percentage, or an amount in sen
- * @property int|null $minimum_quantity
- * @property int|null $minimum_subtotal
- * @property list<int>|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<Store, $this>
- */
- 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<int, CartLine> $lines
- * @return Collection<int, CartLine>
- */
- 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<int, CartLine> $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<int, CartLine> $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<string, string>
- */
- protected function casts(): array
- {
- return [
- 'value' => 'integer',
- 'minimum_quantity' => 'integer',
- 'minimum_subtotal' => 'integer',
- 'product_ids' => 'array',
- 'starts_at' => 'datetime',
- 'ends_at' => 'datetime',
- 'active' => 'boolean',
- ];
- }
- }
|