AI store generator for ecommerce, Malaysia first. Laravel 13 + React + TypeScript.
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. namespace App\Models;
  3. use App\Commerce\Cart;
  4. use Carbon\CarbonInterface;
  5. use Illuminate\Database\Eloquent\Model;
  6. use Illuminate\Database\Eloquent\Relations\BelongsTo;
  7. use Illuminate\Support\Collection;
  8. /**
  9. * A discount that applies by itself when the cart qualifies, with no code to type.
  10. * For example "Raya Sale: buy 2 or more, get 10% off" or "spend RM 100, get RM 15 off".
  11. * It can cover every product or only chosen ones; then only those count and only those get cheaper.
  12. * Amounts are in sen.
  13. *
  14. * @property int $id
  15. * @property int $store_id
  16. * @property string $name Shown to shoppers
  17. * @property string $type "percent" or "fixed"
  18. * @property int $value A whole percentage, or an amount in sen
  19. * @property int|null $minimum_quantity
  20. * @property int|null $minimum_subtotal
  21. * @property list<int>|null $product_ids Null means every product
  22. * @property CarbonInterface|null $starts_at
  23. * @property CarbonInterface|null $ends_at
  24. * @property bool $active
  25. * @property-read Store $store
  26. *
  27. * @phpstan-import-type CartLine from Cart
  28. */
  29. class AutomaticDiscount extends Model
  30. {
  31. /**
  32. * @return BelongsTo<Store, $this>
  33. */
  34. public function store(): BelongsTo
  35. {
  36. return $this->belongsTo(Store::class);
  37. }
  38. /**
  39. * Switched on, and today is within its dates.
  40. */
  41. public function isLive(): bool
  42. {
  43. return $this->status() === 'active';
  44. }
  45. public function status(): string
  46. {
  47. return match (true) {
  48. ! $this->active => 'off',
  49. $this->starts_at !== null && $this->starts_at->isFuture() => 'scheduled',
  50. $this->ends_at !== null && $this->ends_at->isPast() => 'expired',
  51. default => 'active',
  52. };
  53. }
  54. public function appliesTo(Product $product): bool
  55. {
  56. return $this->product_ids === null || in_array($product->id, $this->product_ids, true);
  57. }
  58. /**
  59. * The cart lines this discount covers.
  60. *
  61. * @param Collection<int, CartLine> $lines
  62. * @return Collection<int, CartLine>
  63. */
  64. public function eligible(Collection $lines): Collection
  65. {
  66. return $lines->filter(fn (array $line) => $this->appliesTo($line['product']))->values();
  67. }
  68. /**
  69. * How much comes off these cart lines, in sen. Zero when the cart does not qualify.
  70. *
  71. * @param Collection<int, CartLine> $lines
  72. */
  73. public function amountFor(Collection $lines): int
  74. {
  75. $eligible = $this->eligible($lines);
  76. $subtotal = (int) $eligible->sum('line_total');
  77. if ($subtotal === 0 || $this->shortfall($lines) !== null) {
  78. return 0;
  79. }
  80. $amount = $this->type === DiscountCode::PERCENT
  81. ? intdiv($subtotal * min(100, $this->value), 100)
  82. : $this->value;
  83. return max(0, min($amount, $subtotal));
  84. }
  85. /**
  86. * What the cart still needs to qualify: ["items" => 1] or ["amount" => 1200 sen]. Null when it qualifies.
  87. *
  88. * @param Collection<int, CartLine> $lines
  89. * @return array{items?: int, amount?: int}|null
  90. */
  91. public function shortfall(Collection $lines): ?array
  92. {
  93. $eligible = $this->eligible($lines);
  94. $quantity = (int) $eligible->sum('quantity');
  95. $subtotal = (int) $eligible->sum('line_total');
  96. if ($this->minimum_quantity !== null && $quantity < $this->minimum_quantity) {
  97. return ['items' => $this->minimum_quantity - $quantity];
  98. }
  99. if ($this->minimum_subtotal !== null && $subtotal < $this->minimum_subtotal) {
  100. return ['amount' => $this->minimum_subtotal - $subtotal];
  101. }
  102. return null;
  103. }
  104. /**
  105. * "10% off" or "RM 5.00 off", in the store's language.
  106. */
  107. public function label(string $language = 'en'): string
  108. {
  109. $amount = $this->type === DiscountCode::PERCENT
  110. ? $this->value.'%'
  111. : 'RM '.number_format($this->value / 100, 2);
  112. return $language === 'ms' ? 'diskaun '.$amount : $amount.' off';
  113. }
  114. /**
  115. * The whole offer in one line for shoppers: "Buy 2 or more, get 10% off".
  116. */
  117. public function describe(string $language = 'en'): string
  118. {
  119. $malay = $language === 'ms';
  120. $condition = match (true) {
  121. $this->minimum_quantity !== null && $this->minimum_quantity > 1 => $malay
  122. ? "Beli {$this->minimum_quantity} atau lebih"
  123. : "Buy {$this->minimum_quantity} or more",
  124. $this->minimum_subtotal !== null => $malay
  125. ? 'Belanja RM '.number_format($this->minimum_subtotal / 100, 2).' atau lebih'
  126. : 'Spend RM '.number_format($this->minimum_subtotal / 100, 2).' or more',
  127. default => null,
  128. };
  129. if ($condition === null) {
  130. return ucfirst($this->label($language));
  131. }
  132. return $condition.($malay ? ', dapat ' : ', get ').$this->label($language);
  133. }
  134. /**
  135. * @return array<string, string>
  136. */
  137. protected function casts(): array
  138. {
  139. return [
  140. 'value' => 'integer',
  141. 'minimum_quantity' => 'integer',
  142. 'minimum_subtotal' => 'integer',
  143. 'product_ids' => 'array',
  144. 'starts_at' => 'datetime',
  145. 'ends_at' => 'datetime',
  146. 'active' => 'boolean',
  147. ];
  148. }
  149. }