Edit File: StoreRequest.php
<?php namespace App\Http\Requests\Api\User\UnPricingOrders; use App\Enums\OrderWhenEnum; use App\Enums\ProductDeliveryTypesEnum; use App\Http\Requests\Api\BaseApiRequest; use App\Models\Product; use Carbon\Carbon; use Illuminate\Validation\Rule; class StoreRequest extends BaseApiRequest { const RATE = 'required|numeric|min:1|max:5'; const MESSAGE = 'required|string|max:500'; public function rules(): array { return [ 'product_id' => ['required', Rule::exists('products', 'id')->where('has_price', 0)->whereNull('deleted_at'), 'numeric'], 'quantity' => ['required', 'numeric', 'gt:0'], 'receiving_method' => 'required|in:' . ProductDeliveryTypesEnum::Home->value . ',' . ProductDeliveryTypesEnum::Store->value, 'order_type' => 'required|in:' . OrderWhenEnum::IMMEDIATELY->value . ',' . OrderWhenEnum::SCHEDULE->value, 'schedule_execution_date' => [ 'requiredIf:order_type,' . OrderWhenEnum::SCHEDULE->value, 'nullable','date_format:Y-m-d', 'after_or_equal:' . now()->format('Y-m-d') ], 'schedule_execution_time' => [ 'requiredIf:order_type,' . OrderWhenEnum::SCHEDULE->value,'nullable', 'date_format:H:i:s', Rule::when($this->schedule_execution_date == now()->format('Y-m-d'), [ 'after_or_equal:' . now()->format('H:i:s') ]) ], 'provider_id' => ['required'], 'provider' => ['required'], 'notes' => ['nullable', 'max:500'], 'car_color' => [Rule::requiredIf($this->receiving_method == ProductDeliveryTypesEnum::Store->value && is_null($this->receiving_in_store_notes)), 'nullable', 'string'], 'car_model' => [Rule::requiredIf($this->receiving_method == ProductDeliveryTypesEnum::Store->value && is_null($this->receiving_in_store_notes)), 'nullable', 'string'], 'car_plat_number' => [Rule::requiredIf($this->receiving_method == ProductDeliveryTypesEnum::Store->value && is_null($this->receiving_in_store_notes)), 'nullable', 'string'], 'receiving_in_store_notes' => [Rule::requiredIf($this->receiving_method == ProductDeliveryTypesEnum::Store->value && is_null($this->car_plat_number) && is_null($this->car_color) && is_null($this->car_model)), 'nullable', 'string', 'max:500'], ]; } public function messages(): array { return [ 'schedule_execution_date.after_or_equal' => __('apis.after_or_equal_today'), 'schedule_execution_time.after_or_equal' => __('apis.after_or_equal_now'), 'schedule_execution_date.*' => __('apis.execution_date.required'), 'schedule_execution_time.*' => __('apis.execution_time.required'), ]; } public function prepareForValidation(): void { $provider = Product::find($this->product_id)?->provider ?? null; $this->merge([ 'provider' => $provider, 'provider_id' => $provider ? $provider->id : null, 'schedule_execution_date' => $this->order_type == OrderWhenEnum::SCHEDULE->value ? $this->schedule_execution_date : null, 'schedule_execution_time' => $this->order_type == OrderWhenEnum::SCHEDULE->value ? $this->schedule_execution_time : null, ]); } public function withValidator($validator): void { $authUser = auth()->user(); $validator->after(function ($validator) use ($authUser) { if (!(isset($authUser->lat) && isset($authUser->lng) && isset($authUser->neighborhood_to_delivered_id)) && $this->receiving_method == ProductDeliveryTypesEnum::Home->value) { $validator->errors()->add('no_cart_found', __('apis.user_cart.need_to_set_location')); } if ($this->provider) { if ($this->provider?->providerWorkTimes()->doesntExist()) { $validator->errors()->add('provider_not_available', __('apis.user_order.provider_closed')); } if ($this->provider?->isHolidayThisDay($this->schedule_execution_date ? Carbon::parse($this->schedule_execution_date) : Carbon::today())) { $validator->errors()->add('provider_not_available', __('apis.user_order.provider_holiday')); } if (!$this->provider?->deliverByThisWay($this->receiving_method)) { $validator->errors()->add('provider_not_available', __('apis.user_cart.this_way_not_available')); } if (!$this->provider?->is_available_to_recieve_orders) { $validator->errors()->add('provider_not_available', __('apis.provider_not_available_now')); } } }); } }
Back to File Manager