Warenwirtschaft: AP-43 — Produktion zwischenspeichern (Entwurf) + „abschließen"

Der Speicher-Button heißt jetzt „Produktion abschließen"; daneben gibt es
„Zwischenspeichern (Entwurf)". Ein Entwurf sichert den Stand ohne Pflichtfeld-
und ohne Bestandsprüfung und bucht KEINEN Bestand (Roh-/Produkt-/Verpackung).
Erst „abschließen" prüft vollständig und bucht.

- Migration: productions.status (default 'completed'; Bestand ist bereits gebucht).
- Production-Model: STATUS_DRAFT/COMPLETED, isDraft(), scopeBooked().
- InventoryService: Entwürfe fallen aus allen Verbrauchs-/Bestandsjoins heraus
  (status != draft an den 4 productions-Joins).
- ProductionService store()/updateProduction() bekommen $draft: Entwurf
  überspringt Rezeptur-/Mengen-/Bestandsprüfung + Buchungen, sichert nur
  vollständige Chargen-Zeilen (filterCompleteLines) und die angegebenen Outputs.
  Ein abgeschlossene Produktion kann NICHT zurück in den Entwurf (sonst
  verwaisen die Buchungen) — Guard wirft ValidationException.
- StoreProductionRequest: save_mode=draft => nur Kopfdaten Pflicht, Chargen/
  Ergebnis optional; validatedPayload filtert unvollständige Zeilen/Outputs.
- Controller: Entwurf löst Chargen tolerant auf (nicht auflösbare Chargen werden
  übersprungen, Entwurf ist immer speicherbar) und leitet zur Bearbeitung zurück.
- Views: create/edit „abschließen" + „Zwischenspeichern"-Button (nur solange
  Entwurf); Entwurf-Badge in Liste + Bearbeiten-Kopf.

Tests: 5 neue (kein Produktbestand, kein Rohstoffverbrauch, Abschluss bucht,
Rück-Entwurf verboten, HTTP-Entwurf ohne Ergebnis ok / Abschluss erzwingt
Ergebnis). Migration auf Dev-/Live-DB. Suite 336 grün.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
Kevin Adametz 2026-08-28 09:59:42 +00:00
parent 3d19f743b1
commit abd202a02e
10 changed files with 445 additions and 44 deletions

View file

@ -57,13 +57,11 @@ class ProductionController extends Controller
public function store(StoreProductionRequest $request): RedirectResponse
{
$payload = $request->validatedPayload();
$isDraft = (bool) $payload['is_draft'];
try {
// AP-27: Chargen-Zeilen (batch_key) FEFO auf die Einzel-Wareneingänge der Charge auflösen.
$ingredientLines = $this->productionService->expandBatchLines(
$payload['ingredient_lines'],
$payload['location_id']
);
$ingredientLines = $this->expandLines($payload['ingredient_lines'], $payload['location_id'], null, $isDraft);
$production = $this->productionService->store(
[
@ -75,12 +73,50 @@ class ProductionController extends Controller
],
$ingredientLines,
(int) $request->user()->id,
$payload['outputs']
$payload['outputs'],
$isDraft
);
} catch (ValidationException $e) {
return redirect()->back()->withInput()->withErrors($e->errors());
}
return $this->afterSave($production, $isDraft);
}
/**
* AP-27/AP-43: Chargen-Zeilen auf Einzel-Wareneingänge auflösen. Im Entwurf
* darf das Auflösen scheitern (Charge nicht verfügbar) — dann bleiben nur die
* bereits konkret gewählten Wareneingänge erhalten, der Entwurf ist immer speicherbar.
*
* @param array<int, array<string, mixed>> $lines
* @return array<int, array<string, mixed>>
*/
private function expandLines(array $lines, int $locationId, ?int $excludeProductionId, bool $isDraft): array
{
if (! $isDraft) {
return $this->productionService->expandBatchLines($lines, $locationId, $excludeProductionId);
}
try {
return $this->productionService->expandBatchLines($lines, $locationId, $excludeProductionId);
} catch (ValidationException) {
return array_values(array_filter($lines, fn ($l) => (int) ($l['stock_entry_id'] ?? 0) > 0));
}
}
/**
* AP-43: Entwurf zurück in die Bearbeitung (Weiterarbeiten), abgeschlossene
* Produktion in die Detailansicht.
*/
private function afterSave(Production $production, bool $isDraft): RedirectResponse
{
if ($isDraft) {
\Session::flash('alert-save', '1');
return redirect()->route('admin.inventory.productions.edit', $production)
->with('alert-info', __('Entwurf zwischengespeichert. Die Produktion ist noch nicht gebucht.'));
}
if ($production->mhd_warning) {
\Session::flash('alert-warning', __('Hinweis: Mindestens eine Rohstoff-Charge hat ein kürzeres MHD als das geplante Produkt-MHD.'));
}
@ -135,14 +171,11 @@ class ProductionController extends Controller
public function update(StoreProductionRequest $request, Production $production): RedirectResponse
{
$payload = $request->validatedPayload();
$isDraft = (bool) $payload['is_draft'];
try {
// AP-27: Beim Bearbeiten die eigene Produktion aus der Rest-Berechnung ausnehmen.
$ingredientLines = $this->productionService->expandBatchLines(
$payload['ingredient_lines'],
$payload['location_id'],
$production->id
);
$ingredientLines = $this->expandLines($payload['ingredient_lines'], $payload['location_id'], $production->id, $isDraft);
$production = $this->productionService->updateProduction(
$production,
@ -155,18 +188,14 @@ class ProductionController extends Controller
],
$ingredientLines,
(int) $request->user()->id,
$payload['outputs']
$payload['outputs'],
$isDraft
);
} catch (ValidationException $e) {
return redirect()->back()->withInput()->withErrors($e->errors());
}
if ($production->mhd_warning) {
\Session::flash('alert-warning', __('Hinweis: Mindestens eine Rohstoff-Charge hat ein kürzeres MHD als das geplante Produkt-MHD.'));
}
\Session::flash('alert-save', '1');
return redirect()->route('admin.inventory.productions.show', $production);
return $this->afterSave($production, $isDraft);
}
public function copy(Production $production): View|RedirectResponse

View file

@ -14,11 +14,41 @@ class StoreProductionRequest extends FormRequest
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.*.quantity_used' => ['nullable', 'string'],
'outputs' => ['nullable', 'array'],
'outputs.*.product_id' => ['nullable', 'integer', 'exists:products,id'],
'outputs.*.quantity' => ['nullable', 'integer', 'min:1'],
'outputs.*.batch_number' => ['nullable', 'string', 'max:100'],
];
}
$recipeRequired = $this->recipeRequired();
return [
@ -62,7 +92,22 @@ class StoreProductionRequest extends FormRequest
{
$data = $this->validated();
// AP-43: Im Entwurf dürfen Zeilen unvollständig sein — nur solche mit
// Rohstoff + (Charge oder Wareneingang) bzw. Ergebnis-Produkt übernehmen.
$ingredientLines = array_values(array_filter($data['ingredient_lines'] ?? [], function ($line): bool {
return is_array($line)
&& (int) ($line['ingredient_id'] ?? 0) > 0
&& (! empty($line['batch_key']) || (int) ($line['stock_entry_id'] ?? 0) > 0);
}));
$outputs = array_values(array_filter($data['outputs'] ?? [], function ($output): bool {
return is_array($output)
&& (int) ($output['product_id'] ?? 0) > 0
&& (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'],
@ -71,7 +116,7 @@ class StoreProductionRequest extends FormRequest
'ingredient_lines' => array_map(function (array $line): array {
$out = [
'ingredient_id' => (int) $line['ingredient_id'],
'quantity_used' => $line['quantity_used'],
'quantity_used' => $line['quantity_used'] ?? null,
];
if (! empty($line['batch_key'])) {
$out['batch_key'] = (string) $line['batch_key'];
@ -80,14 +125,14 @@ class StoreProductionRequest extends FormRequest
}
return $out;
}, $data['ingredient_lines'] ?? []),
}, $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,
];
}, $data['outputs'] ?? []),
}, $outputs),
];
}
}