| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172 |
- <?php
-
- namespace App\Commerce;
-
- use App\Models\AutomaticDiscount;
- use App\Models\DiscountCode;
- use App\Models\Store;
- use Illuminate\Support\Carbon;
- use Illuminate\Validation\Rule;
-
- /**
- * The rules for an automatic discount, shared by its form and chat commands.
- * Amounts arrive in ringgit as typed; dates are Malaysian days (Y-m-d).
- */
- final class AutomaticDiscountRules
- {
- /**
- * @return array<string, list<mixed>>
- */
- public static function rules(Store $store, bool $percent): array
- {
- return [
- 'name' => ['required', 'string', 'min:2', 'max:40'],
- 'type' => ['required', Rule::in([DiscountCode::PERCENT, DiscountCode::FIXED])],
- 'value' => $percent
- ? ['required', 'integer', 'min:1', 'max:100']
- : ['required', 'numeric', 'decimal:0,2', 'min:0.01', 'max:999999.99'],
- 'minimum_quantity' => ['nullable', 'integer', 'min:1', 'max:999'],
- 'minimum_subtotal' => ['nullable', 'numeric', 'decimal:0,2', 'min:0', 'max:999999.99'],
- 'applies_to' => ['required', Rule::in(['all', 'chosen'])],
- 'product_ids' => ['exclude_unless:applies_to,chosen', 'required', 'array', 'min:1', 'max:200'],
- 'product_ids.*' => ['integer', Rule::exists('products', 'id')->where('store_id', $store->id)],
- 'starts_on' => ['nullable', 'date_format:Y-m-d'],
- 'ends_on' => ['nullable', 'date_format:Y-m-d', 'after_or_equal:starts_on'],
- 'active' => ['boolean'],
- ];
- }
-
- /**
- * @return array<string, string>
- */
- public static function messages(bool $percent): array
- {
- return [
- 'value.max' => $percent ? __('A percentage cannot be more than 100.') : __('The amount is too large.'),
- 'product_ids.required' => __('Choose at least one product.'),
- 'product_ids.*.exists' => __('Choose products from this store.'),
- 'ends_on.after_or_equal' => __('The last day cannot be before the first day.'),
- ];
- }
-
- /**
- * @param array<string, mixed> $data
- */
- public static function fill(AutomaticDiscount $offer, array $data): void
- {
- $percent = $data['type'] === DiscountCode::PERCENT;
- $timezone = DiscountCodeRules::TIMEZONE;
-
- $offer->name = trim($data['name']);
- $offer->type = $data['type'];
- $offer->value = $percent ? (int) $data['value'] : (int) round(((float) $data['value']) * 100);
- $offer->minimum_quantity = filled($data['minimum_quantity'] ?? null) ? (int) $data['minimum_quantity'] : null;
- $offer->minimum_subtotal = filled($data['minimum_subtotal'] ?? null) ? (int) round(((float) $data['minimum_subtotal']) * 100) : null;
- $offer->product_ids = ($data['applies_to'] ?? 'all') === 'chosen'
- ? array_values(array_unique(array_map('intval', $data['product_ids'] ?? [])))
- : null;
- $offer->starts_at = filled($data['starts_on'] ?? null) ? Carbon::parse($data['starts_on'], $timezone)->startOfDay()->utc() : null;
- $offer->ends_at = filled($data['ends_on'] ?? null) ? Carbon::parse($data['ends_on'], $timezone)->endOfDay()->utc() : null;
- $offer->active = (bool) ($data['active'] ?? false);
- }
- }
|