AI store generator for ecommerce, Malaysia first. Laravel 13 + React + TypeScript.
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

DiscountCodeRules.php 3.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. namespace App\Commerce;
  3. use App\Models\DiscountCode;
  4. use App\Models\Store;
  5. use Illuminate\Support\Carbon;
  6. use Illuminate\Validation\Rule;
  7. /**
  8. * The rules for a discount code, shared by the Discounts form and chat commands, so both accept the same codes.
  9. * Amounts arrive in ringgit as typed, such as "12.50". Dates are Malaysian days (Y-m-d).
  10. */
  11. final class DiscountCodeRules
  12. {
  13. public const string TIMEZONE = 'Asia/Kuala_Lumpur';
  14. /**
  15. * @return array<string, list<mixed>>
  16. */
  17. public static function rules(Store $store, bool $percent, ?DiscountCode $existing = null): array
  18. {
  19. return [
  20. 'code' => [
  21. 'required', 'string', 'regex:'.DiscountCode::CODE_PATTERN,
  22. Rule::unique('discount_codes', 'code')->where('store_id', $store->id)->ignore($existing?->id),
  23. ],
  24. 'type' => ['required', Rule::in([DiscountCode::PERCENT, DiscountCode::FIXED])],
  25. 'value' => $percent
  26. ? ['required', 'integer', 'min:1', 'max:100']
  27. : ['required', 'numeric', 'decimal:0,2', 'min:0.01', 'max:999999.99'],
  28. 'minimum_subtotal' => ['nullable', 'numeric', 'decimal:0,2', 'min:0', 'max:999999.99'],
  29. 'starts_on' => ['nullable', 'date_format:Y-m-d'],
  30. 'ends_on' => ['nullable', 'date_format:Y-m-d', 'after_or_equal:starts_on'],
  31. 'usage_limit' => ['nullable', 'integer', 'min:1', 'max:1000000'],
  32. 'active' => ['boolean'],
  33. ];
  34. }
  35. /**
  36. * @return array<string, string>
  37. */
  38. public static function messages(bool $percent): array
  39. {
  40. return [
  41. 'code.regex' => __('Use 3 to 30 letters, numbers or dashes, such as RAYA10.'),
  42. 'code.unique' => __('This store already has that code.'),
  43. 'value.max' => $percent ? __('A percentage cannot be more than 100.') : __('The amount is too large.'),
  44. 'ends_on.after_or_equal' => __('The last day cannot be before the first day.'),
  45. ];
  46. }
  47. /**
  48. * Copy checked data onto a code, converting ringgit to sen and Malaysian days to times.
  49. *
  50. * @param array<string, mixed> $data
  51. */
  52. public static function fill(DiscountCode $code, array $data): void
  53. {
  54. $percent = $data['type'] === DiscountCode::PERCENT;
  55. $code->code = $data['code'];
  56. $code->type = $data['type'];
  57. $code->value = $percent ? (int) $data['value'] : self::sen($data['value']);
  58. $code->minimum_subtotal = filled($data['minimum_subtotal'] ?? null) ? self::sen($data['minimum_subtotal']) : null;
  59. $code->starts_at = filled($data['starts_on'] ?? null) ? Carbon::parse($data['starts_on'], self::TIMEZONE)->startOfDay()->utc() : null;
  60. $code->ends_at = filled($data['ends_on'] ?? null) ? Carbon::parse($data['ends_on'], self::TIMEZONE)->endOfDay()->utc() : null;
  61. $code->usage_limit = filled($data['usage_limit'] ?? null) ? (int) $data['usage_limit'] : null;
  62. $code->active = (bool) ($data['active'] ?? false);
  63. }
  64. private static function sen(mixed $ringgit): int
  65. {
  66. return (int) round(((float) $ringgit) * 100);
  67. }
  68. }