gruene-seele/app/Http/Requests/Inventory/StoreStockEntryRequest.php
Kevin Adametz 6bd867cbe7 Warenwirtschaft: AP-53 — Nacharbeit zur Prüfung 10.09.2026 (Toleranzregel zentral, Sockelrand, Chargenzeile, Ausschuss-Meldung, Auflösung/Obergrenze)
Prüfbericht pruefung-ap53-mengenpraezision-2026-09-10.md, alle Punkte umgesetzt:
- AP53-01: Toleranz nur noch in ProductionService (TOLERANCE_RATIO 0,05, TOLERANCE_FLOOR 0,005, COMPARE_EPSILON); das Produktions-JS rendert die Konstanten aus dem Service statt eigener Zahlen (Ampel hatte noch Sockel 0,05 g).
- AP53-02: withinTolerance() vergleicht mit Float-Ausgleich 1e-9 — 0,025 g auf 0,020 g Soll ist wieder zulässig, 0,026 g nicht; Sollwerte werden nicht gerundet.
- AP53-03: Chargen-Hauptzeile und Standortsumme im Rohstoff-Detail mit formatWawiQuantity(…, 0) (0,125 g statt 0 g).
- AP53-04: Ausschuss-Fehlermeldungen (Request ohne/mit Charge, Controller-Wiederholung) mit formatWawiQuantity().
- 4.1: Obergrenze decimal(12,3) bewusst akzeptiert (Live max. 49.000 g) und als InventoryService::QUANTITY_MAX validiert; Bereichsprüfung + Rollback-Hinweis in der Deploy-Checkliste.
- 4.2: normalizeWawiQuantity() (parsen + drei Stellen) in Bestell-/Wareneingangs-/Ausschuss-Requests, parseQuantity() rundet kanonisch für batch_key- und stock_entry_id-Zeilen; Untergrenze 0,001 g; ml-Eingaben, die umgerechnet 0,000 g ergeben, werden abgelehnt (Store/Update/Receive).
- 4.3: Einkaufs-JS trennt formatQuantity (3), formatPrice (2, wie vor AP-53) und formatDensity (4).
- 4.4: Hinweise präzisiert (Übersichten ganzzahlig, Rundungs-/Untergrenzregel), Leitfaden §26 mit Sockelrand-, Hauptzeilen- und Meldungs-Prüfschritten, Plan V7 und README aktualisiert, Prüfbericht Abschnitt 6 mit Nacharbeitsstand.

Tests: QuantityPrecisionTest +6 (AP53-01…04, 4.2 Rundung/Untergrenze/Obergrenze, ml→0 g). Suite 384 grün / 1.592 Assertions.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
2026-09-10 07:25:11 +00:00

116 lines
5.2 KiB
PHP

<?php
namespace App\Http\Requests\Inventory;
use App\Models\Ingredient;
use App\Services\InventoryService;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;
use Illuminate\Validation\Validator;
class StoreStockEntryRequest 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.'));
}
// AP-42: ml-Erfassung nur, wenn für den Rohstoff eine Dichte hinterlegt ist.
if ($this->input('ordered_unit') === 'ml') {
$density = Ingredient::query()->whereKey($this->input('ingredient_id'))->value('density');
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;
}
}