Warenwirtschaft: AP-31 Löschen mit Soft-Delete + Abhängigkeitsprüfung

Anforderung 15.07.2026: Nahezu alle WaWi-Einträge sind jetzt löschbar
(Soft-Delete, deleted_at), mit Abhängigkeitsprüfung und Bestätigungs-
warnung vor jeder Löschung.

- Migration: deleted_at auf 14 WaWi-Tabellen (Stammdaten, ingredients,
  stock_entries, stock_disposals, productions); SoftDeletes-Trait in
  den Models (suppliers/packaging_items hatten es bereits).
- Neuer App\Services\DeletionGuard: zentrale Abhängigkeitsprüfung je
  Entität — referenzierte Einträge sind nicht löschbar, Fehlermeldung
  nennt die Verwendungen (behebt auch Audit M-4: kein 500er/stiller
  Cascade-Verlust mehr beim Löschen verwendeter Stammdaten).
- SettingCrudController::destroy() prüft Blocker; Supplier-,
  PackagingItem-, Ingredient-destroy ebenso (Ingredient: vollständige
  Prüfung inkl. Produktion/Ausschuss, Route GET→DELETE, Audit M-6).
- Wareneingänge: auch gebuchte Eingänge löschbar, sofern kein
  Produktions-Verbrauch/Ausschuss darauf gebucht ist.
- Ausschuss: löschbar (Menge steht dem Bestand wieder zur Verfügung).
- Produktionen: löschbar (admin >= 7); Produktbestand wird per
  Gegenbuchung zurückgeführt (reverseProductionStock, append-only,
  withTrashed), Rohstoffverbrauch zählt nicht mehr (Bestandsqueries
  schließen gelöschte Produktionen aus).
- lastNetPricePerSupplier: DB::query statt Model-Query (Global Scope
  auf Sub-Select-Alias).
- Debug-Log-Reste im PackagingItemController entfernt (Audit N-16).
- Views: Lösch-Buttons mit deutschen Bestätigungswarnungen (Ausschuss,
  Produktionen, gebuchte Wareneingänge, INCI als DELETE-Formular).

Tests: neuer InventoryDeletionTest (10 Tests), StorageSettingsTest an
neue Löschregel angepasst. Volle Suite: 214 grün, 783 Assertions.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
Kevin Adametz 2026-07-15 12:31:11 +00:00
parent d8d4a9be55
commit 35b9076cbc
32 changed files with 844 additions and 41 deletions

View file

@ -9,6 +9,7 @@ use App\Models\PackagingItem;
use App\Models\PackagingMaterial;
use App\Models\Supplier;
use App\Repositories\PackagingItemRepository;
use App\Services\DeletionGuard;
use Illuminate\Http\Request;
class PackagingItemController extends Controller
@ -58,13 +59,6 @@ class PackagingItemController extends Controller
{
$validated = $request->validated();
\Log::debug('PackagingItem STORE – raw input', [
'category_raw' => $request->input('category'),
'category_hex' => bin2hex((string) $request->input('category')),
'all_input' => $request->all(),
]);
\Log::debug('PackagingItem STORE – validated', $validated);
$item = $this->packagingItemRepository->create($validated);
\Session::flash('alert-save', '1');
@ -90,13 +84,6 @@ class PackagingItemController extends Controller
{
$validated = $request->validated();
\Log::debug('PackagingItem UPDATE – raw input', [
'category_raw' => $request->input('category'),
'category_hex' => bin2hex((string) $request->input('category')),
'all_input' => $request->all(),
]);
\Log::debug('PackagingItem UPDATE – validated', $validated);
$this->packagingItemRepository->update($packagingItem, $validated);
\Session::flash('alert-save', '1');
@ -110,6 +97,14 @@ class PackagingItemController extends Controller
{
$category = $packagingItem->category === 'shipping' ? 'shipping' : 'packaging';
$guard = app(DeletionGuard::class);
$blockers = $guard->blockersFor($packagingItem);
if ($blockers !== []) {
\Session::flash('alert-error', $guard->message($blockers));
return redirect()->route('admin.inventory.packaging-items.index', ['category' => $category]);
}
$packagingItem->delete();
\Session::flash('alert-success', __('Eintrag gelöscht'));

View file

@ -9,6 +9,7 @@ use App\Models\Product;
use App\Models\Production;
use App\Repositories\ProductionRepository;
use App\Services\ProductionService;
use App\Services\ProductStockService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
@ -179,6 +180,28 @@ class ProductionController extends Controller
]);
}
/**
* Anforderung 15.07.2026: Produktionen löschbar (Soft-Delete).
* Der gebuchte Produktbestand wird per Gegenbuchung zurückgeführt;
* der Rohstoffverbrauch der Produktion zählt danach nicht mehr
* (Bestandsqueries schließen gelöschte Produktionen aus).
*/
public function destroy(Production $production, ProductStockService $productStockService): RedirectResponse
{
if (! auth()->user()->isAdmin()) {
return redirect()->route('home');
}
\DB::transaction(function () use ($production, $productStockService) {
$productStockService->reverseProductionStock($production, (int) auth()->id());
$production->delete();
});
\Session::flash('alert-success', __('Produktion gelöscht — Produktbestand zurückgebucht, Rohstoffverbrauch freigegeben.'));
return redirect()->route('admin.inventory.productions.index');
}
public function recipeJson(Request $request, Product $product): JsonResponse
{
$locationId = (int) $request->query('location_id', 0);

View file

@ -3,6 +3,7 @@
namespace App\Http\Controllers\Admin\Inventory;
use App\Http\Controllers\Controller;
use App\Services\DeletionGuard;
use Illuminate\Contracts\View\View;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Http\FormRequest;
@ -100,7 +101,17 @@ abstract class SettingCrudController extends Controller
public function destroy(): RedirectResponse
{
$this->routeModel()->delete();
$model = $this->routeModel();
$guard = app(DeletionGuard::class);
$blockers = $guard->blockersFor($model);
if ($blockers !== []) {
\Session::flash('alert-error', $guard->message($blockers));
return redirect()->route($this->redirectRoute());
}
$model->delete();
\Session::flash('alert-success', __('Eintrag gelöscht'));

View file

@ -75,6 +75,24 @@ class StockDisposalController extends Controller
return redirect()->route('admin.inventory.stock-disposals.index');
}
/**
* Anforderung 15.07.2026: Ausschuss-Buchungen löschbar (Soft-Delete).
* Die Bestandsaggregationen schließen gelöschte Buchungen automatisch
* aus — der Bestand steht danach wieder zur Verfügung.
*/
public function destroy(StockDisposal $stockDisposal): RedirectResponse
{
if (! auth()->user()->isAdmin()) {
return redirect()->route('home');
}
$stockDisposal->delete();
\Session::flash('alert-success', __('Ausschuss-Buchung gelöscht — die Menge steht dem Bestand wieder zur Verfügung.'));
return redirect()->route('admin.inventory.stock-disposals.index');
}
public function ingredientCharges(Ingredient $ingredient): JsonResponse
{
$remainingByLocation = $this->inventoryService->remainingByLocationForIngredient($ingredient->id);

View file

@ -14,6 +14,7 @@ use App\Models\StockEntry;
use App\Models\Supplier;
use App\Models\TaxRate;
use App\Repositories\StockEntryRepository;
use App\Services\DeletionGuard;
use App\Services\InventoryService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\RedirectResponse;
@ -168,8 +169,13 @@ class StockEntryController extends Controller
if (! auth()->user()->isAdmin()) {
return redirect()->route('home');
}
if (! $stockEntry->isPending()) {
\Session::flash('alert-error', __('Nur offene Bestellungen können gelöscht werden.'));
// Anforderung 15.07.2026: auch gebuchte Eingänge löschbar (Soft-Delete),
// sofern keine Abhängigkeiten (Produktions-Verbrauch, Ausschuss) bestehen.
$guard = app(DeletionGuard::class);
$blockers = $guard->blockersFor($stockEntry);
if ($blockers !== []) {
\Session::flash('alert-error', $guard->message($blockers));
return redirect()->route('admin.inventory.stock-entries.index');
}

View file

@ -12,6 +12,7 @@ use App\Models\PackagingItem;
use App\Models\Supplier;
use App\Models\SupplierCategory;
use App\Repositories\SupplierRepository;
use App\Services\DeletionGuard;
use Illuminate\Contracts\View\View;
use Illuminate\Http\Request;
@ -137,6 +138,14 @@ class SupplierController extends Controller
public function destroy(Supplier $supplier)
{
$guard = app(DeletionGuard::class);
$blockers = $guard->blockersFor($supplier);
if ($blockers !== []) {
\Session::flash('alert-error', $guard->message($blockers));
return redirect()->route('admin.inventory.suppliers.index');
}
$supplier->delete();
\Session::flash('alert-success', __('Eintrag gelöscht'));