gruene-seele/app/Http/Requests/Inventory/ReceiveStockEntryRequest.php

50 lines
1.6 KiB
PHP

<?php
namespace App\Http\Requests\Inventory;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Validator;
class ReceiveStockEntryRequest extends FormRequest
{
public function authorize(): bool
{
return true;
}
/**
* @return array<string, array<int, mixed>>
*/
public function rules(): array
{
return [
'received_at' => ['required', 'date'],
'received_quantity' => ['required', 'numeric', 'min:0.000001'],
'batch_number' => ['nullable', 'string', 'max:100'],
'best_before' => ['nullable', 'date'],
'quality_id' => ['nullable', 'integer', 'exists:material_qualities,id'],
];
}
protected function prepareForValidation(): void
{
$this->merge([
'received_quantity' => reFormatNumber($this->input('received_quantity')),
]);
}
public function withValidator(Validator $validator): void
{
$validator->after(function (Validator $validator): void {
$stockEntry = $this->route('stockEntry') ?? $this->route('stock_entry');
if ($stockEntry && $stockEntry->entry_type === 'ingredient') {
if (empty($this->input('batch_number'))) {
$validator->errors()->add('batch_number', __('Bitte eine Chargennummer angeben.'));
}
if (empty($this->input('best_before'))) {
$validator->errors()->add('best_before', __('Bitte die Mindesthaltbarkeit angeben.'));
}
}
});
}
}