Die Einheit hing bisher als Anhang rechts am Mengenfeld — auf breiten Monitoren außer Sicht, und „g" war stiller Default. Jetzt: - Eigenes Pflichtfeld „Einheit der bestellten Menge" direkt VOR der Menge mit „Bitte wählen"; beim Neuanlegen muss bewusst Gramm oder Milliliter gewählt werden, beim Bearbeiten ist die gespeicherte Einheit vorbelegt. - Hinweis unter der Einheit sagt, ob ml möglich ist (Dichte hinterlegt, Wert wird genannt) oder erst gepflegt werden muss; ml bleibt ohne Dichte gesperrt. Mengen-Hinweis und ml→g-Live-Vorschau folgen der Auswahl. - Server: Store-/UpdateStockEntryRequest weisen Rohstoff-Einkäufe ohne Einheit ab (Verpackung bleibt ohne Einheit). Bestehende Tests ergänzt, neuer Test für die Pflicht. - In-App-Doku (Hinweise, Testleitfaden) nachgezogen. Suite: 390 grün. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
127 lines
6.1 KiB
PHP
127 lines
6.1 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Requests\Inventory;
|
|
|
|
use App\Models\Ingredient;
|
|
use App\Models\StockEntry;
|
|
use App\Services\InventoryService;
|
|
use Illuminate\Foundation\Http\FormRequest;
|
|
use Illuminate\Validation\Rule;
|
|
use Illuminate\Validation\Validator;
|
|
|
|
class UpdateStockEntryRequest extends FormRequest
|
|
{
|
|
public function authorize(): bool
|
|
{
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* @return array<string, array<int, mixed>>
|
|
*/
|
|
public function rules(): array
|
|
{
|
|
return [
|
|
'entry_type' => ['required', Rule::in(['ingredient', 'packaging', 'shipping'])],
|
|
'ingredient_id' => ['nullable', 'integer', 'exists:ingredients,id'],
|
|
'packaging_item_id' => ['nullable', 'integer', 'exists:packaging_items,id'],
|
|
'supplier_id' => ['required', 'integer', 'exists:suppliers,id'],
|
|
'location_id' => ['required', 'integer', 'exists:locations,id'],
|
|
'ordered_at' => ['required', 'date'],
|
|
'ordered_quantity' => ['required', 'numeric', 'min:0.001', 'max:'.InventoryService::QUANTITY_MAX],
|
|
'ordered_unit' => ['nullable', Rule::in(['gram', 'ml'])],
|
|
'quality_id' => ['nullable', 'integer', 'exists:material_qualities,id'],
|
|
'tax_rate_id' => ['nullable', 'integer', 'exists:tax_rates,id'],
|
|
'price_per_kg' => ['nullable', 'numeric', 'min:0'],
|
|
'price_per_kg_gross' => ['nullable', 'numeric', 'min:0'],
|
|
'price_total' => ['nullable', 'numeric', 'min:0'],
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @return array<string, string>
|
|
*/
|
|
public function messages(): array
|
|
{
|
|
return [
|
|
'quality_id.required' => __('Bitte eine Rohstoffqualität wählen.'),
|
|
'price_per_kg.required' => __('Bitte den Netto- oder Brutto-Preis pro kg angeben.'),
|
|
'price_total.required' => __('Bitte den Gesamtpreis netto angeben.'),
|
|
];
|
|
}
|
|
|
|
protected function prepareForValidation(): void
|
|
{
|
|
$this->merge([
|
|
'ordered_quantity' => normalizeWawiQuantity($this->input('ordered_quantity')),
|
|
'price_per_kg' => $this->filled('price_per_kg') ? reFormatWawiNumber($this->input('price_per_kg')) : null,
|
|
'price_per_kg_gross' => $this->filled('price_per_kg_gross') ? reFormatWawiNumber($this->input('price_per_kg_gross')) : null,
|
|
'price_total' => $this->filled('price_total') ? reFormatWawiNumber($this->input('price_total')) : null,
|
|
]);
|
|
}
|
|
|
|
public function withValidator(Validator $validator): void
|
|
{
|
|
$validator->after(function (Validator $validator): void {
|
|
$type = $this->input('entry_type');
|
|
if ($type === 'ingredient') {
|
|
if (empty($this->input('ingredient_id'))) {
|
|
$validator->errors()->add('ingredient_id', __('Bitte einen Inhaltsstoff wählen.'));
|
|
}
|
|
if (empty($this->input('quality_id'))) {
|
|
$validator->errors()->add('quality_id', __('Bitte eine Rohstoffqualität wählen.'));
|
|
}
|
|
// Einheit ist Pflicht: bewusst g oder ml wählen (kein stiller Gramm-Default).
|
|
if (! in_array($this->input('ordered_unit'), ['gram', 'ml'], true)) {
|
|
$validator->errors()->add('ordered_unit', __('Bitte die Einheit der bestellten Menge wählen (Gramm oder Milliliter).'));
|
|
}
|
|
// AP-42: ml-Erfassung nur mit hinterlegter Dichte.
|
|
if ($this->input('ordered_unit') === 'ml') {
|
|
$density = Ingredient::query()->whereKey($this->input('ingredient_id'))->value('density');
|
|
// N-06: bei unverändertem Rohstoff gilt der Dichte-Snapshot der Bestellung als Rückfall.
|
|
$entry = $this->route('stock_entry');
|
|
if (($density === null || (float) $density <= 0) && $entry instanceof StockEntry
|
|
&& (int) $entry->ingredient_id === (int) $this->input('ingredient_id') && (float) $entry->density_used > 0) {
|
|
$density = $entry->density_used;
|
|
}
|
|
if ($density === null || (float) $density <= 0) {
|
|
$validator->errors()->add('ordered_quantity', __('Für diesen Rohstoff ist keine Dichte hinterlegt — bitte in Gramm erfassen oder zuerst die Dichte pflegen.'));
|
|
} elseif (is_numeric($this->input('ordered_quantity')) && roundWawiQuantity((float) $this->input('ordered_quantity') * (float) $density) <= 0) {
|
|
// Prüfung AP-53 (4.2): positive ml-Menge darf nach Umrechnung nicht auf 0,000 g fallen.
|
|
$validator->errors()->add('ordered_quantity', __('Die Menge ergibt umgerechnet weniger als 0,001 g — bitte mindestens 0,001 g erfassen.'));
|
|
}
|
|
}
|
|
$net = $this->input('price_per_kg');
|
|
$gross = $this->input('price_per_kg_gross');
|
|
$hasNet = is_numeric($net) && (float) $net > 0;
|
|
$hasGross = is_numeric($gross) && (float) $gross > 0;
|
|
if (! $hasNet && ! $hasGross) {
|
|
$validator->errors()->add('price_per_kg', __('Bitte den Netto- oder Brutto-Preis pro kg angeben.'));
|
|
}
|
|
} elseif (! empty($type)) {
|
|
if (empty($this->input('packaging_item_id'))) {
|
|
$validator->errors()->add('packaging_item_id', __('Bitte einen Verpackungsartikel wählen.'));
|
|
}
|
|
if (! is_numeric($this->input('price_total')) || (float) $this->input('price_total') <= 0) {
|
|
$validator->errors()->add('price_total', __('Bitte den Gesamtpreis netto angeben.'));
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
/**
|
|
* @return array<string, mixed>
|
|
*/
|
|
public function validatedPayload(): array
|
|
{
|
|
$data = $this->validated();
|
|
if (($data['entry_type'] ?? '') === 'ingredient') {
|
|
$data['packaging_item_id'] = null;
|
|
} else {
|
|
$data['ingredient_id'] = null;
|
|
$data['quality_id'] = null;
|
|
}
|
|
|
|
return $data;
|
|
}
|
|
}
|