Warenwirtschaft: F-06 — Chargenzeilen nach Validierungsfehler aus old() wiederherstellen

Vorbelegung der Chargen-Zeilen zentral in ProductionController::existingLines();
old('ingredient_lines') hat Vorrang vor dem gespeicherten Stand.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
Kevin Adametz 2026-09-08 10:57:07 +00:00
parent a803e956e1
commit 122d28bc4f
5 changed files with 78 additions and 25 deletions

View file

@ -52,6 +52,7 @@ class ProductionController extends Controller
'defaultLocationId' => $defaultLocationId,
'defaultProductId' => $defaultProductId,
'model' => null,
'existingLines' => $this->existingLines(null),
]);
}
@ -160,6 +161,7 @@ class ProductionController extends Controller
...$this->productCatalogs($production),
'locations' => Location::query()->where('active', true)->orderBy('name')->get(),
'defaultLocationId' => $defaultLocationId,
'existingLines' => $this->existingLines($production),
]);
}
@ -217,9 +219,48 @@ class ProductionController extends Controller
...$this->productCatalogs($production),
'locations' => Location::query()->where('active', true)->orderBy('name')->get(),
'defaultLocationId' => $defaultLocationId,
'existingLines' => $this->existingLines($production),
]);
}
/**
* Vorbelegung der Chargen-Zeilen im Formular, Schlüssel „Rohstoff|Phase“ (F-04).
* Prüfung 08.09.2026 F-06: Nach einem Validierungsfehler haben die zuletzt
* eingegebenen Zeilen (old()) Vorrang vor dem gespeicherten Stand — wie bei
* Kopfdaten und Ergebniszeilen. Sonst AP-27: Zeilen je Charge (batch_key)
* zusammenfassen, mehrere Einkäufe derselben Charge = eine Formularzeile.
*
* @return array<string, array<int, array{batch_key: string, quantity_used: float|string}>>
*/
private function existingLines(?Production $production): array
{
$old = old('ingredient_lines');
if (is_array($old) && $old !== []) {
return collect($old)
->filter(fn ($l) => is_array($l)
&& (int) ($l['ingredient_id'] ?? 0) > 0
&& (trim((string) ($l['batch_key'] ?? '')) !== '' || trim((string) ($l['quantity_used'] ?? '')) !== ''))
->groupBy(fn ($l) => (int) $l['ingredient_id'].'|'.(! empty($l['recipe_phase_id']) ? (int) $l['recipe_phase_id'] : ''))
->map(fn ($group) => $group->map(fn ($l) => [
'batch_key' => trim((string) ($l['batch_key'] ?? '')),
'quantity_used' => (string) ($l['quantity_used'] ?? ''),
])->values()->all())
->all();
}
if ($production === null) {
return [];
}
return $production->productionIngredients
->groupBy(fn ($l) => $l->formGroupKey())
->map(fn ($lines) => $lines
->groupBy(fn ($l) => $l->formBatchKey())
->map(fn ($group, $key) => ['batch_key' => (string) $key, 'quantity_used' => (float) $group->sum('quantity_used')])
->values()->all())
->all();
}
/**
* Prüfung 08.09.2026 F-02: Zwei getrennte Kataloge.
* - `products`: Planungsprodukt oben — nur produktionsrelevante Produkte (AP-47),