> */ 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 */ 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 $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); } }