gruene-seele/app/Models/StockEntry.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

146 lines
3.3 KiB
PHP

<?php
namespace App\Models;
use App\User;
use Database\Factories\StockEntryFactory;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\SoftDeletes;
class StockEntry extends Model
{
/** @use HasFactory<StockEntryFactory> */
use HasFactory;
use SoftDeletes;
protected $fillable = [
'entry_type',
'ingredient_id',
'packaging_item_id',
'supplier_id',
'location_id',
'unit',
'ordered_by',
'ordered_at',
'ordered_quantity',
'price_per_kg',
'price_per_kg_gross',
'price_total',
'tax_rate_id',
'tax_rate_percent',
'received_by',
'received_at',
'received_quantity',
'batch_number',
'best_before',
'quality_id',
'status',
];
/**
* @return array<string, string>
*/
protected function casts(): array
{
return [
'ordered_at' => 'date',
'received_at' => 'date',
'best_before' => 'date',
'ordered_quantity' => 'decimal:2',
'received_quantity' => 'decimal:2',
'price_per_kg' => 'decimal:4',
'price_per_kg_gross' => 'decimal:4',
'price_total' => 'decimal:4',
'tax_rate_percent' => 'decimal:2',
];
}
/**
* @return BelongsTo<Ingredient, $this>
*/
public function ingredient(): BelongsTo
{
return $this->belongsTo(Ingredient::class);
}
/**
* @return BelongsTo<PackagingItem, $this>
*/
public function packagingItem(): BelongsTo
{
return $this->belongsTo(PackagingItem::class);
}
/**
* @return BelongsTo<Supplier, $this>
*/
public function supplier(): BelongsTo
{
return $this->belongsTo(Supplier::class);
}
/**
* @return BelongsTo<Location, $this>
*/
public function location(): BelongsTo
{
return $this->belongsTo(Location::class);
}
/**
* @return BelongsTo<TaxRate, $this>
*/
public function taxRate(): BelongsTo
{
return $this->belongsTo(TaxRate::class);
}
/**
* @return BelongsTo<MaterialQuality, $this>
*/
public function quality(): BelongsTo
{
return $this->belongsTo(MaterialQuality::class, 'quality_id');
}
/**
* @return BelongsTo<User, $this>
*/
public function orderedByUser(): BelongsTo
{
return $this->belongsTo(User::class, 'ordered_by');
}
/**
* @return BelongsTo<User, $this>
*/
public function receivedByUser(): BelongsTo
{
return $this->belongsTo(User::class, 'received_by');
}
public function isPending(): bool
{
return $this->status === 'pending';
}
/**
* AP-27: Gruppierungsschlüssel für die Chargen-Zusammenführung.
* Einträge mit Chargennummer werden je Charge zusammengefasst,
* Einträge ohne Chargennummer bleiben einzeln.
*/
public function batchGroupKey(): string
{
$batch = trim((string) $this->batch_number);
return $batch !== '' ? 'b:'.$batch : 'e:'.$this->id;
}
public function isReceived(): bool
{
return $this->status === 'received';
}
}