Ohne Charge wird gegen den Restbestand des gewählten Lagerorts geprüft und die Menge FEFO auf die dort verfügbaren Chargen verteilt (je Charge gebucht, in einer Transaktion) — wie der Produktionsverbrauch. Gesamt-, Standort- und Chargenbestand stimmen damit überein. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
144 lines
5.5 KiB
PHP
144 lines
5.5 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.01'],
|
|
'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' => reFormatWawiNumber($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' => number_format($quantity, 2, ',', '.'),
|
|
'rest' => number_format(max(0, $remaining), 2, ',', '.'),
|
|
]));
|
|
}
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
$consumed = app(ProductionService::class)->consumedByStockEntry([$entry->id])[$entry->id] ?? 0.0;
|
|
$remaining = round((float) $entry->received_quantity - $consumed, 2);
|
|
if ($quantity > $remaining) {
|
|
$validator->errors()->add('quantity', __('Die Menge (:qty g) übersteigt den Restbestand der Charge (:rest g).', [
|
|
'qty' => number_format($quantity, 2, ',', '.'),
|
|
'rest' => number_format(max(0, $remaining), 2, ',', '.'),
|
|
]));
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @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;
|
|
}
|
|
}
|