gruene-seele/app/Http/Controllers/Admin/Inventory/SupplierController.php
Kevin Adametz 35b9076cbc 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>
2026-07-15 12:31:11 +00:00

155 lines
5.1 KiB
PHP

<?php
namespace App\Http\Controllers\Admin\Inventory;
use App\Http\Controllers\Controller;
use App\Http\Requests\Inventory\StoreSupplierRequest;
use App\Http\Requests\Inventory\UpdateSupplierRequest;
use App\Models\Country;
use App\Models\DeliveryTime;
use App\Models\Ingredient;
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;
class SupplierController extends Controller
{
public function __construct(
protected SupplierRepository $supplierRepository
) {}
public function index()
{
return view('admin.inventory.suppliers.index', [
'values' => Supplier::query()->with(['country', 'supplierCategories'])->orderBy('name')->get(),
]);
}
public function create()
{
$defaultCountryId = Country::where('code', 'DE')->value('id');
return view('admin.inventory.suppliers.form', [
'model' => new Supplier(['active' => true, 'country_id' => $defaultCountryId]),
'countries' => Country::query()->orderBy('de')->get(),
'supplierCategories' => SupplierCategory::query()->orderBy('pos')->orderBy('name')->get(),
'deliveryTimes' => DeliveryTime::query()->active()->orderBy('pos')->orderBy('label')->get(),
]);
}
public function store(StoreSupplierRequest $request)
{
$this->supplierRepository->create($request->validated());
\Session::flash('alert-save', '1');
return redirect()->route('admin.inventory.suppliers.index');
}
public function show(Supplier $supplier): View
{
return $this->renderDetails($supplier);
}
public function attachIngredient(Request $request, Supplier $supplier): View
{
$validated = $request->validate([
'ingredient_id' => ['required', 'integer', 'exists:ingredients,id'],
]);
$supplier->ingredients()->syncWithoutDetaching([$validated['ingredient_id']]);
return $this->renderDetails($supplier);
}
public function detachIngredient(Supplier $supplier, Ingredient $ingredient): View
{
$supplier->ingredients()->detach($ingredient->id);
return $this->renderDetails($supplier);
}
public function attachPackagingItem(Request $request, Supplier $supplier): View
{
$validated = $request->validate([
'packaging_item_id' => ['required', 'integer', 'exists:packaging_items,id'],
]);
PackagingItem::query()
->whereKey($validated['packaging_item_id'])
->update(['supplier_id' => $supplier->id]);
return $this->renderDetails($supplier);
}
public function detachPackagingItem(Supplier $supplier, PackagingItem $packagingItem): View
{
if ((int) $packagingItem->supplier_id === (int) $supplier->id) {
$packagingItem->update(['supplier_id' => null]);
}
return $this->renderDetails($supplier);
}
protected function renderDetails(Supplier $supplier): View
{
$supplier->load(['country', 'supplierCategories', 'ingredients', 'packagingItems']);
$assignedIngredientIds = $supplier->ingredients->pluck('id')->all();
$assignedPackagingItemIds = $supplier->packagingItems->pluck('id')->all();
return view('admin.inventory.suppliers._details', [
'supplier' => $supplier,
'availableIngredients' => Ingredient::query()
->whereNotIn('id', $assignedIngredientIds)
->orderBy('name')
->get(),
'availablePackagingItems' => PackagingItem::query()
->whereNotIn('id', $assignedPackagingItemIds)
->orderBy('name')
->get(),
]);
}
public function edit(Supplier $supplier)
{
$supplier->load('supplierCategories');
return view('admin.inventory.suppliers.form', [
'model' => $supplier,
'countries' => Country::query()->orderBy('de')->get(),
'supplierCategories' => SupplierCategory::query()->orderBy('pos')->orderBy('name')->get(),
'deliveryTimes' => DeliveryTime::query()->active()->orderBy('pos')->orderBy('label')->get(),
]);
}
public function update(UpdateSupplierRequest $request, Supplier $supplier)
{
$this->supplierRepository->update($supplier, $request->validated());
\Session::flash('alert-save', '1');
return redirect()->route('admin.inventory.suppliers.index');
}
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'));
return redirect()->route('admin.inventory.suppliers.index');
}
}