AI store generator for ecommerce, Malaysia first. Laravel 13 + React + TypeScript.
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

DiscountCode.php 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. namespace App\Models;
  3. use Carbon\CarbonInterface;
  4. use Database\Factories\DiscountCodeFactory;
  5. use Illuminate\Database\Eloquent\Factories\HasFactory;
  6. use Illuminate\Database\Eloquent\Model;
  7. use Illuminate\Database\Eloquent\Relations\BelongsTo;
  8. /**
  9. * A code shoppers type in the cart to get money off. Amounts are in sen (1 ringgit = 100 sen).
  10. *
  11. * @property int $id
  12. * @property int $store_id
  13. * @property string $code
  14. * @property string $type "percent" or "fixed"
  15. * @property int $value A whole percentage, or an amount in sen
  16. * @property int|null $minimum_subtotal
  17. * @property CarbonInterface|null $starts_at
  18. * @property CarbonInterface|null $ends_at
  19. * @property int|null $usage_limit
  20. * @property int $times_used
  21. * @property bool $active
  22. * @property CarbonInterface|null $created_at
  23. * @property CarbonInterface|null $updated_at
  24. * @property-read Store $store
  25. */
  26. class DiscountCode extends Model
  27. {
  28. /** @use HasFactory<DiscountCodeFactory> */
  29. use HasFactory;
  30. public const string PERCENT = 'percent';
  31. public const string FIXED = 'fixed';
  32. /** Letters, digits and dashes, 3 to 30 long. */
  33. public const string CODE_PATTERN = '/^[A-Z0-9-]{3,30}$/';
  34. /**
  35. * Shoppers may type " raya10 "; we store and compare "RAYA10".
  36. */
  37. public static function normalize(string $code): string
  38. {
  39. return strtoupper(trim($code));
  40. }
  41. /**
  42. * @return BelongsTo<Store, $this>
  43. */
  44. public function store(): BelongsTo
  45. {
  46. return $this->belongsTo(Store::class);
  47. }
  48. /**
  49. * Why this code cannot be used right now for products adding up to $subtotal (in sen),
  50. * as a key for StorefrontText. Null when it can be used.
  51. */
  52. public function problemFor(int $subtotal): ?string
  53. {
  54. return match (true) {
  55. ! $this->active => 'discount_invalid',
  56. $this->starts_at !== null && $this->starts_at->isFuture() => 'discount_not_started',
  57. $this->ends_at !== null && $this->ends_at->isPast() => 'discount_expired',
  58. $this->usage_limit !== null && $this->times_used >= $this->usage_limit => 'discount_used_up',
  59. $this->minimum_subtotal !== null && $subtotal < $this->minimum_subtotal => 'discount_minimum',
  60. default => null,
  61. };
  62. }
  63. /**
  64. * How much comes off products adding up to $subtotal, in sen. Never more than the subtotal.
  65. * Percentages are rounded down to the sen, so the shopper never gets less than promised by more than a sen.
  66. */
  67. public function amountFor(int $subtotal): int
  68. {
  69. $amount = $this->type === self::PERCENT
  70. ? intdiv($subtotal * min(100, $this->value), 100)
  71. : $this->value;
  72. return max(0, min($amount, $subtotal));
  73. }
  74. /**
  75. * "10% off" or "RM 5.00 off", in the store's language.
  76. */
  77. public function label(string $language = 'en'): string
  78. {
  79. $amount = $this->type === self::PERCENT
  80. ? $this->value.'%'
  81. : 'RM '.number_format($this->value / 100, 2);
  82. return $language === 'ms' ? 'Diskaun '.$amount : $amount.' off';
  83. }
  84. /**
  85. * The state a merchant sees in the list.
  86. */
  87. public function status(): string
  88. {
  89. return match (true) {
  90. ! $this->active => 'off',
  91. $this->starts_at !== null && $this->starts_at->isFuture() => 'scheduled',
  92. $this->ends_at !== null && $this->ends_at->isPast() => 'expired',
  93. $this->usage_limit !== null && $this->times_used >= $this->usage_limit => 'used_up',
  94. default => 'active',
  95. };
  96. }
  97. /**
  98. * @return array<string, string>
  99. */
  100. protected function casts(): array
  101. {
  102. return [
  103. 'value' => 'integer',
  104. 'minimum_subtotal' => 'integer',
  105. 'starts_at' => 'datetime',
  106. 'ends_at' => 'datetime',
  107. 'usage_limit' => 'integer',
  108. 'times_used' => 'integer',
  109. 'active' => 'boolean',
  110. ];
  111. }
  112. }