128 lines
4.6 KiB
PHP
128 lines
4.6 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Modules\RestaurantDelivery\Http\Requests;
|
|
|
|
use Illuminate\Foundation\Http\FormRequest;
|
|
use Illuminate\Validation\Rule;
|
|
|
|
/**
|
|
* CreateDeliveryRequest
|
|
*
|
|
* IMPORTANT: restaurant_id is NEVER accepted from request input.
|
|
* It is always obtained via getUserRestaurantId() in the controller.
|
|
*/
|
|
class CreateDeliveryRequest extends FormRequest
|
|
{
|
|
/**
|
|
* Determine if the user is authorized to make this request.
|
|
*/
|
|
public function authorize(): bool
|
|
{
|
|
// User must be authenticated and have a restaurant_id
|
|
return auth()->check() && getUserRestaurantId() !== null;
|
|
}
|
|
|
|
/**
|
|
* Get the validation rules that apply to the request.
|
|
*
|
|
* NOTE: restaurant_id is NOT in this list - it comes from getUserRestaurantId()
|
|
*/
|
|
public function rules(): array
|
|
{
|
|
return [
|
|
// Order Info
|
|
'orderable_type' => ['nullable', 'string', 'max:255'],
|
|
'orderable_id' => ['nullable', 'integer'],
|
|
'zone_id' => ['nullable', 'integer', 'exists:restaurant_delivery_zones,id'],
|
|
|
|
// Pickup Details
|
|
'restaurant_name' => ['required', 'string', 'max:255'],
|
|
'pickup_address' => ['required', 'string', 'max:500'],
|
|
'pickup_latitude' => ['required', 'numeric', 'between:-90,90'],
|
|
'pickup_longitude' => ['required', 'numeric', 'between:-180,180'],
|
|
'pickup_contact_name' => ['nullable', 'string', 'max:100'],
|
|
'pickup_contact_phone' => ['nullable', 'string', 'max:20'],
|
|
'pickup_instructions' => ['nullable', 'string', 'max:500'],
|
|
|
|
// Drop-off Details
|
|
'customer_name' => ['required', 'string', 'max:100'],
|
|
'drop_address' => ['required', 'string', 'max:500'],
|
|
'drop_latitude' => ['required', 'numeric', 'between:-90,90'],
|
|
'drop_longitude' => ['required', 'numeric', 'between:-180,180'],
|
|
'drop_contact_name' => ['nullable', 'string', 'max:100'],
|
|
'drop_contact_phone' => ['required', 'string', 'max:20'],
|
|
'drop_instructions' => ['nullable', 'string', 'max:500'],
|
|
'drop_floor' => ['nullable', 'string', 'max:50'],
|
|
'drop_apartment' => ['nullable', 'string', 'max:50'],
|
|
|
|
// Scheduling
|
|
'is_scheduled' => ['boolean'],
|
|
'scheduled_for' => ['nullable', 'required_if:is_scheduled,true', 'date', 'after:now'],
|
|
'is_priority' => ['boolean'],
|
|
|
|
// Order Value
|
|
'order_value' => ['nullable', 'numeric', 'min:0'],
|
|
|
|
// Pre-delivery Tip
|
|
'tip_amount' => ['nullable', 'numeric', 'min:0'],
|
|
'tip_type' => ['nullable', Rule::in(['amount', 'percentage'])],
|
|
|
|
// Additional Metadata
|
|
'meta' => ['nullable', 'array'],
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Get custom attributes for validator errors.
|
|
*/
|
|
public function attributes(): array
|
|
{
|
|
return [
|
|
'pickup_latitude' => 'pickup location latitude',
|
|
'pickup_longitude' => 'pickup location longitude',
|
|
'drop_latitude' => 'delivery location latitude',
|
|
'drop_longitude' => 'delivery location longitude',
|
|
'drop_contact_phone' => 'customer phone',
|
|
'scheduled_for' => 'scheduled delivery time',
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Get the error messages for the defined validation rules.
|
|
*/
|
|
public function messages(): array
|
|
{
|
|
return [
|
|
'pickup_latitude.between' => 'The pickup latitude must be between -90 and 90.',
|
|
'pickup_longitude.between' => 'The pickup longitude must be between -180 and 180.',
|
|
'drop_latitude.between' => 'The delivery latitude must be between -90 and 90.',
|
|
'drop_longitude.between' => 'The delivery longitude must be between -180 and 180.',
|
|
'scheduled_for.after' => 'The scheduled delivery time must be in the future.',
|
|
'scheduled_for.required_if' => 'The scheduled delivery time is required when scheduling a delivery.',
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Prepare the data for validation.
|
|
*/
|
|
protected function prepareForValidation(): void
|
|
{
|
|
$this->merge([
|
|
'is_scheduled' => $this->boolean('is_scheduled'),
|
|
'is_priority' => $this->boolean('is_priority'),
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Get validated data with restaurant_id from getUserRestaurantId().
|
|
*/
|
|
public function validatedWithRestaurant(): array
|
|
{
|
|
return array_merge($this->validated(), [
|
|
'restaurant_id' => getUserRestaurantId(),
|
|
]);
|
|
}
|
|
}
|