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

AutomaticDiscountRules.php 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. namespace App\Commerce;
  3. use App\Models\AutomaticDiscount;
  4. use App\Models\DiscountCode;
  5. use App\Models\Store;
  6. use Illuminate\Support\Carbon;
  7. use Illuminate\Validation\Rule;
  8. /**
  9. * The rules for an automatic discount, shared by its form and chat commands.
  10. * Amounts arrive in ringgit as typed; dates are Malaysian days (Y-m-d).
  11. */
  12. final class AutomaticDiscountRules
  13. {
  14. /**
  15. * @return array<string, list<mixed>>
  16. */
  17. public static function rules(Store $store, bool $percent): array
  18. {
  19. return [
  20. 'name' => ['required', 'string', 'min:2', 'max:40'],
  21. 'type' => ['required', Rule::in([DiscountCode::PERCENT, DiscountCode::FIXED])],
  22. 'value' => $percent
  23. ? ['required', 'integer', 'min:1', 'max:100']
  24. : ['required', 'numeric', 'decimal:0,2', 'min:0.01', 'max:999999.99'],
  25. 'minimum_quantity' => ['nullable', 'integer', 'min:1', 'max:999'],
  26. 'minimum_subtotal' => ['nullable', 'numeric', 'decimal:0,2', 'min:0', 'max:999999.99'],
  27. 'applies_to' => ['required', Rule::in(['all', 'chosen'])],
  28. 'product_ids' => ['exclude_unless:applies_to,chosen', 'required', 'array', 'min:1', 'max:200'],
  29. 'product_ids.*' => ['integer', Rule::exists('products', 'id')->where('store_id', $store->id)],
  30. 'starts_on' => ['nullable', 'date_format:Y-m-d'],
  31. 'ends_on' => ['nullable', 'date_format:Y-m-d', 'after_or_equal:starts_on'],
  32. 'active' => ['boolean'],
  33. ];
  34. }
  35. /**
  36. * @return array<string, string>
  37. */
  38. public static function messages(bool $percent): array
  39. {
  40. return [
  41. 'value.max' => $percent ? __('A percentage cannot be more than 100.') : __('The amount is too large.'),
  42. 'product_ids.required' => __('Choose at least one product.'),
  43. 'product_ids.*.exists' => __('Choose products from this store.'),
  44. 'ends_on.after_or_equal' => __('The last day cannot be before the first day.'),
  45. ];
  46. }
  47. /**
  48. * @param array<string, mixed> $data
  49. */
  50. public static function fill(AutomaticDiscount $offer, array $data): void
  51. {
  52. $percent = $data['type'] === DiscountCode::PERCENT;
  53. $timezone = DiscountCodeRules::TIMEZONE;
  54. $offer->name = trim($data['name']);
  55. $offer->type = $data['type'];
  56. $offer->value = $percent ? (int) $data['value'] : (int) round(((float) $data['value']) * 100);
  57. $offer->minimum_quantity = filled($data['minimum_quantity'] ?? null) ? (int) $data['minimum_quantity'] : null;
  58. $offer->minimum_subtotal = filled($data['minimum_subtotal'] ?? null) ? (int) round(((float) $data['minimum_subtotal']) * 100) : null;
  59. $offer->product_ids = ($data['applies_to'] ?? 'all') === 'chosen'
  60. ? array_values(array_unique(array_map('intval', $data['product_ids'] ?? [])))
  61. : null;
  62. $offer->starts_at = filled($data['starts_on'] ?? null) ? Carbon::parse($data['starts_on'], $timezone)->startOfDay()->utc() : null;
  63. $offer->ends_at = filled($data['ends_on'] ?? null) ? Carbon::parse($data['ends_on'], $timezone)->endOfDay()->utc() : null;
  64. $offer->active = (bool) ($data['active'] ?? false);
  65. }
  66. }