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>
138 lines
6.3 KiB
PHP
138 lines
6.3 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Requests\Inventory;
|
|
|
|
use App\Models\Product;
|
|
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.*.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 [
|
|
'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'],
|
|
// 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.'));
|
|
}
|
|
}],
|
|
// 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.
|
|
$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'],
|
|
'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,
|
|
];
|
|
if (! empty($line['batch_key'])) {
|
|
$out['batch_key'] = (string) $line['batch_key'];
|
|
} else {
|
|
$out['stock_entry_id'] = (int) $line['stock_entry_id'];
|
|
}
|
|
|
|
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),
|
|
];
|
|
}
|
|
}
|