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>
158 lines
7.8 KiB
PHP
158 lines
7.8 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Requests\Inventory;
|
|
|
|
use App\Models\Product;
|
|
use App\Services\InventoryService;
|
|
use Illuminate\Foundation\Http\FormRequest;
|
|
|
|
class StoreProductionRequest extends FormRequest
|
|
{
|
|
public function authorize(): bool
|
|
{
|
|
// Audit M-2: Produktionen buchen Produktbestand — gleiche Schranke
|
|
// wie manuelle Bestandsbewegungen (admin >= 7).
|
|
return (bool) $this->user()?->isAdmin();
|
|
}
|
|
|
|
/**
|
|
* AP-43: „Zwischenspeichern" schickt save_mode=draft und speichert einen
|
|
* Entwurf ohne Pflichtfeld-Validierung (Chargen/Ergebnis optional).
|
|
*/
|
|
public function isDraft(): bool
|
|
{
|
|
return $this->input('save_mode') === 'draft';
|
|
}
|
|
|
|
/**
|
|
* @return array<string, array<int, mixed|string>>
|
|
*/
|
|
public function rules(): array
|
|
{
|
|
// AP-43: Entwurf — nur die Kopfdaten (DB-Pflichtspalten) prüfen; Rezeptur-
|
|
// Zeilen und Produktionsergebnis dürfen unvollständig bleiben.
|
|
if ($this->isDraft()) {
|
|
return [
|
|
'product_id' => ['required', 'integer', 'exists:products,id'],
|
|
'location_id' => ['required', 'integer', 'exists:locations,id'],
|
|
'produced_at' => ['required', 'date'],
|
|
'quantity' => ['required', 'integer', 'min:1'],
|
|
'notes' => ['nullable', 'string', 'max:2000'],
|
|
'ingredient_lines' => ['nullable', 'array'],
|
|
'ingredient_lines.*.ingredient_id' => ['nullable', 'integer', 'exists:ingredients,id'],
|
|
'ingredient_lines.*.batch_key' => ['nullable', 'string', 'max:120'],
|
|
'ingredient_lines.*.stock_entry_id' => ['nullable', 'integer', 'exists:stock_entries,id'],
|
|
'ingredient_lines.*.recipe_phase_id' => ['nullable', 'integer', 'exists:recipe_phases,id'],
|
|
'ingredient_lines.*.quantity_used' => ['nullable', 'string'],
|
|
'outputs' => ['nullable', 'array'],
|
|
'outputs.*.product_id' => ['nullable', 'integer', 'exists:products,id'],
|
|
// F-03: Im Entwurf darf die Stückzahl noch leer/0 sein.
|
|
'outputs.*.quantity' => ['nullable', 'integer', 'min:0'],
|
|
'outputs.*.batch_number' => ['nullable', 'string', 'max:100'],
|
|
];
|
|
}
|
|
|
|
$recipeRequired = $this->recipeRequired();
|
|
|
|
return [
|
|
'product_id' => ['required', 'integer', 'exists:products,id'],
|
|
'location_id' => ['required', 'integer', 'exists:locations,id'],
|
|
'produced_at' => ['required', 'date'],
|
|
'quantity' => ['required', 'integer', 'min:1'],
|
|
'notes' => ['nullable', 'string', 'max:2000'],
|
|
'ingredient_lines' => [$recipeRequired ? 'required' : 'nullable', 'array', $recipeRequired ? 'min:1' : 'min:0'],
|
|
'ingredient_lines.*.ingredient_id' => ['required', 'integer', 'exists:ingredients,id'],
|
|
// AP-27: Das Formular übergibt Chargen (batch_key); Einzel-Einträge (stock_entry_id) bleiben unterstützt.
|
|
'ingredient_lines.*.batch_key' => ['required_without:ingredient_lines.*.stock_entry_id', 'nullable', 'string', 'max:120'],
|
|
'ingredient_lines.*.stock_entry_id' => ['required_without:ingredient_lines.*.batch_key', 'nullable', 'integer', 'exists:stock_entries,id'],
|
|
// F-04: Phase der Rezepturzeile — nur zur Vorbelegung beim Wiederöffnen.
|
|
'ingredient_lines.*.recipe_phase_id' => ['nullable', 'integer', 'exists:recipe_phases,id'],
|
|
// Audit H-2: Vorzeichen-/Zahlprüfung — negative Zeilen konnten sich
|
|
// in der Summenprüfung gegenseitig aufheben und Bestand aufblähen.
|
|
'ingredient_lines.*.quantity_used' => ['required', 'string', function (string $attribute, mixed $value, \Closure $fail) {
|
|
$n = reFormatWawiNumber((string) $value);
|
|
if (! is_numeric($n) || (float) $n <= 0) {
|
|
$fail(__('Die Menge je Chargen-Zeile muss größer als 0 sein.'));
|
|
|
|
return;
|
|
}
|
|
// Prüfung AP-53 (4.2): unterhalb der Speicherauflösung (0,001 g) bzw. über decimal(12,3).
|
|
if (roundWawiQuantity($n) <= 0) {
|
|
$fail(__('Die Menge je Chargen-Zeile muss mindestens 0,001 g betragen.'));
|
|
} elseif ((float) $n > InventoryService::QUANTITY_MAX) {
|
|
$fail(__('Die Menge je Chargen-Zeile ist zu groß (max. 999.999.999,999 g).'));
|
|
}
|
|
}],
|
|
// AP-28: Produktionsergebnis — bucht den Produktbestand; Planung oben ist unverbindlich.
|
|
'outputs' => ['required', 'array', 'min:1'],
|
|
'outputs.*.product_id' => ['required', 'integer', 'exists:products,id'],
|
|
'outputs.*.quantity' => ['required', 'integer', 'min:1'],
|
|
'outputs.*.batch_number' => ['nullable', 'string', 'max:100'],
|
|
];
|
|
}
|
|
|
|
private function recipeRequired(): bool
|
|
{
|
|
$product = Product::query()->find($this->input('product_id'));
|
|
|
|
return $product === null ? true : ! (bool) $product->no_recipe_required;
|
|
}
|
|
|
|
/**
|
|
* @return array<string, mixed>
|
|
*/
|
|
public function validatedPayload(): array
|
|
{
|
|
$data = $this->validated();
|
|
|
|
// AP-43: Im Entwurf dürfen Zeilen unvollständig sein — nur solche mit
|
|
// Rohstoff + (Charge oder Wareneingang) bzw. Ergebnis-Produkt übernehmen.
|
|
// N-01 (Nachprüfung 08.09.2026): Im Entwurf zählt auch eine Ist-Menge ohne
|
|
// gewählte Charge als Arbeitsstand; beim Abschluss verlangen die Regeln die Charge.
|
|
$isDraft = $this->isDraft();
|
|
$ingredientLines = array_values(array_filter($data['ingredient_lines'] ?? [], function ($line) use ($isDraft): bool {
|
|
return is_array($line)
|
|
&& (int) ($line['ingredient_id'] ?? 0) > 0
|
|
&& (! empty($line['batch_key']) || (int) ($line['stock_entry_id'] ?? 0) > 0
|
|
|| ($isDraft && trim((string) ($line['quantity_used'] ?? '')) !== ''));
|
|
}));
|
|
|
|
// F-03: Im Entwurf zählt ein gewähltes Ergebnisprodukt auch ohne Stückzahl als Arbeitsstand.
|
|
$outputs = array_values(array_filter($data['outputs'] ?? [], function ($output) use ($isDraft): bool {
|
|
return is_array($output)
|
|
&& (int) ($output['product_id'] ?? 0) > 0
|
|
&& ($isDraft || (int) ($output['quantity'] ?? 0) > 0);
|
|
}));
|
|
|
|
return [
|
|
'is_draft' => $this->isDraft(),
|
|
'product_id' => (int) $data['product_id'],
|
|
'location_id' => (int) $data['location_id'],
|
|
'produced_at' => $data['produced_at'],
|
|
'quantity' => (int) $data['quantity'],
|
|
'notes' => $data['notes'] ?? null,
|
|
'ingredient_lines' => array_map(function (array $line): array {
|
|
$out = [
|
|
'ingredient_id' => (int) $line['ingredient_id'],
|
|
'quantity_used' => $line['quantity_used'] ?? null,
|
|
'recipe_phase_id' => ! empty($line['recipe_phase_id']) ? (int) $line['recipe_phase_id'] : null,
|
|
];
|
|
if (! empty($line['batch_key'])) {
|
|
$out['batch_key'] = (string) $line['batch_key'];
|
|
} else {
|
|
// N-01: im Entwurf darf beides fehlen (Menge ohne Charge).
|
|
$out['stock_entry_id'] = (int) ($line['stock_entry_id'] ?? 0);
|
|
}
|
|
|
|
return $out;
|
|
}, $ingredientLines),
|
|
'outputs' => array_map(function (array $output): array {
|
|
return [
|
|
'product_id' => (int) $output['product_id'],
|
|
'quantity' => (int) $output['quantity'],
|
|
'batch_number' => $output['batch_number'] ?? null,
|
|
];
|
|
}, $outputs),
|
|
];
|
|
}
|
|
}
|