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>
146 lines
5.7 KiB
PHP
146 lines
5.7 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Requests\Inventory;
|
|
|
|
use App\Models\StockEntry;
|
|
use App\Services\InventoryService;
|
|
use App\Services\ProductionService;
|
|
use Carbon\Carbon;
|
|
use Illuminate\Foundation\Http\FormRequest;
|
|
use Illuminate\Validation\Rule;
|
|
use Illuminate\Validation\Validator;
|
|
|
|
class StoreStockDisposalRequest extends FormRequest
|
|
{
|
|
public function authorize(): bool
|
|
{
|
|
return (bool) $this->user()?->isAdmin();
|
|
}
|
|
|
|
/**
|
|
* @return array<string, array<int, mixed>>
|
|
*/
|
|
public function rules(): array
|
|
{
|
|
return [
|
|
'disposal_type' => ['required', Rule::in(['ingredient', 'packaging'])],
|
|
'ingredient_id' => ['nullable', 'integer', 'exists:ingredients,id'],
|
|
'packaging_item_id' => ['nullable', 'integer', 'exists:packaging_items,id'],
|
|
'stock_entry_id' => ['nullable', 'integer', 'exists:stock_entries,id'],
|
|
'location_id' => ['required', 'integer', 'exists:locations,id'],
|
|
'quantity' => ['required', 'numeric', 'min:0.001', 'max:'.InventoryService::QUANTITY_MAX],
|
|
'reason' => ['required', 'string', 'max:100'],
|
|
'note' => ['nullable', 'string', 'max:255'],
|
|
'disposed_at' => ['required', 'date'],
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @return array<string, string>
|
|
*/
|
|
public function messages(): array
|
|
{
|
|
return [
|
|
'location_id.required' => __('Bitte einen Lagerort wählen.'),
|
|
'quantity.required' => __('Bitte eine Menge angeben.'),
|
|
'quantity.min' => __('Die Menge muss größer als 0 sein.'),
|
|
'reason.required' => __('Bitte einen Grund angeben.'),
|
|
'disposed_at.required' => __('Bitte ein Datum angeben.'),
|
|
];
|
|
}
|
|
|
|
protected function prepareForValidation(): void
|
|
{
|
|
$disposedAt = $this->input('disposed_at');
|
|
if (is_string($disposedAt) && preg_match('/^\d{2}\.\d{2}\.\d{4}$/', trim($disposedAt))) {
|
|
$disposedAt = Carbon::createFromFormat('d.m.Y', trim($disposedAt))->format('Y-m-d');
|
|
}
|
|
|
|
$this->merge([
|
|
'quantity' => normalizeWawiQuantity($this->input('quantity')),
|
|
'disposed_at' => $disposedAt,
|
|
]);
|
|
}
|
|
|
|
public function withValidator(Validator $validator): void
|
|
{
|
|
$validator->after(function (Validator $validator): void {
|
|
if ($this->input('disposal_type') === 'ingredient') {
|
|
if (empty($this->input('ingredient_id'))) {
|
|
$validator->errors()->add('ingredient_id', __('Bitte einen Rohstoff wählen.'));
|
|
}
|
|
$this->validateChargeConsistency($validator);
|
|
} elseif ($this->input('disposal_type') === 'packaging') {
|
|
if (empty($this->input('packaging_item_id'))) {
|
|
$validator->errors()->add('packaging_item_id', __('Bitte einen Verpackungsartikel wählen.'));
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Audit M-1: Charge muss zum gewählten Rohstoff gehören (eingegangen,
|
|
* Typ ingredient), und die Menge darf den Restbestand nicht übersteigen.
|
|
*/
|
|
protected function validateChargeConsistency(Validator $validator): void
|
|
{
|
|
$quantity = (float) ($this->input('quantity') ?: 0);
|
|
$ingredientId = (int) $this->input('ingredient_id');
|
|
|
|
if (! $this->filled('stock_entry_id')) {
|
|
// Prüfung 08.09.2026 F-09: ohne Charge gegen den gewählten Lagerort prüfen,
|
|
// nicht gegen den globalen Bestand (sonst geht ein leerer Standort ins Minus).
|
|
$locationId = (int) $this->input('location_id');
|
|
if ($ingredientId > 0 && $quantity > 0 && $locationId > 0) {
|
|
$remaining = app(InventoryService::class)
|
|
->remainingByLocationForIngredient($ingredientId)[$locationId] ?? 0.0;
|
|
if ($quantity > $remaining) {
|
|
$validator->errors()->add('quantity', __('Die Menge (:qty g) übersteigt den Restbestand am gewählten Lagerort (:rest g).', [
|
|
'qty' => formatWawiQuantity($quantity),
|
|
'rest' => formatWawiQuantity(max(0, $remaining)),
|
|
]));
|
|
}
|
|
}
|
|
|
|
return;
|
|
}
|
|
|
|
$entry = StockEntry::query()->find((int) $this->input('stock_entry_id'));
|
|
if (! $entry || $entry->entry_type !== 'ingredient' || $entry->status !== 'received'
|
|
|| (int) $entry->ingredient_id !== $ingredientId) {
|
|
$validator->errors()->add('stock_entry_id', __('Die gewählte Charge gehört nicht zum gewählten Rohstoff.'));
|
|
|
|
return;
|
|
}
|
|
|
|
// N-05: Verfügbarkeit der Charge wie in der Produktion (inkl. FEFO-Anrechnung von Alt-Ausschuss ohne Charge).
|
|
$available = app(ProductionService::class)->availableStockEntriesForIngredient($ingredientId, (int) $entry->location_id)
|
|
->firstWhere('id', $entry->id);
|
|
$remaining = $available !== null ? roundWawiQuantity((float) $available->getAttribute('remaining_quantity')) : 0.0;
|
|
if ($quantity > $remaining) {
|
|
$validator->errors()->add('quantity', __('Die Menge (:qty g) übersteigt den Restbestand der Charge (:rest g).', [
|
|
'qty' => formatWawiQuantity($quantity),
|
|
'rest' => formatWawiQuantity(max(0, $remaining)),
|
|
]));
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @return array<string, mixed>
|
|
*/
|
|
public function validatedPayload(): array
|
|
{
|
|
$data = $this->validated();
|
|
|
|
if (($data['disposal_type'] ?? '') === 'ingredient') {
|
|
$data['packaging_item_id'] = null;
|
|
$data['unit'] = 'gram';
|
|
} else {
|
|
$data['ingredient_id'] = null;
|
|
$data['stock_entry_id'] = null;
|
|
$data['unit'] = 'piece';
|
|
}
|
|
|
|
return $data;
|
|
}
|
|
}
|