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>
178 lines
5.4 KiB
PHP
178 lines
5.4 KiB
PHP
<?php
|
|
|
|
namespace App\Repositories;
|
|
|
|
use App\Models\StockEntry;
|
|
use App\Models\TaxRate;
|
|
use Illuminate\Database\Eloquent\Collection;
|
|
|
|
class StockEntryRepository
|
|
{
|
|
/**
|
|
* @param array<string, mixed> $data
|
|
*/
|
|
public function create(array $data): StockEntry
|
|
{
|
|
$data['unit'] = ($data['entry_type'] ?? '') === 'ingredient' ? 'gram' : 'piece';
|
|
$data = $this->resolvePrices($data);
|
|
|
|
return StockEntry::query()->create($data);
|
|
}
|
|
|
|
/**
|
|
* @param array<string, mixed> $data
|
|
*/
|
|
public function update(StockEntry $stockEntry, array $data): StockEntry
|
|
{
|
|
if (array_key_exists('entry_type', $data)) {
|
|
$data['unit'] = ($data['entry_type'] ?? '') === 'ingredient' ? 'gram' : 'piece';
|
|
}
|
|
|
|
$data = $this->resolvePrices($data);
|
|
|
|
$stockEntry->update($data);
|
|
|
|
return $stockEntry->fresh();
|
|
}
|
|
|
|
/**
|
|
* Ergänzt Netto-/Brutto-Preis pro kg und den UST-Snapshot.
|
|
* Es genügt, Netto oder Brutto anzugeben; der jeweils fehlende Wert wird
|
|
* aus dem gewählten Steuersatz berechnet.
|
|
*
|
|
* @param array<string, mixed> $data
|
|
* @return array<string, mixed>
|
|
*/
|
|
protected function resolvePrices(array $data): array
|
|
{
|
|
if (($data['entry_type'] ?? '') !== 'ingredient') {
|
|
$data['price_per_kg'] = null;
|
|
$data['price_per_kg_gross'] = null;
|
|
$data['tax_rate_id'] = null;
|
|
$data['tax_rate_percent'] = null;
|
|
|
|
return $data;
|
|
}
|
|
|
|
$percent = null;
|
|
if (! empty($data['tax_rate_id'])) {
|
|
$percent = TaxRate::query()->whereKey($data['tax_rate_id'])->value('percent');
|
|
}
|
|
$data['tax_rate_percent'] = $percent;
|
|
|
|
$factor = 1 + ((float) ($percent ?? 0)) / 100;
|
|
|
|
$net = isset($data['price_per_kg']) && $data['price_per_kg'] !== null && $data['price_per_kg'] !== ''
|
|
? (float) $data['price_per_kg']
|
|
: null;
|
|
$gross = isset($data['price_per_kg_gross']) && $data['price_per_kg_gross'] !== null && $data['price_per_kg_gross'] !== ''
|
|
? (float) $data['price_per_kg_gross']
|
|
: null;
|
|
|
|
if ($net !== null && $gross === null) {
|
|
$gross = round($net * $factor, 4);
|
|
} elseif ($gross !== null && $net === null) {
|
|
$net = $factor > 0 ? round($gross / $factor, 4) : $gross;
|
|
} elseif ($net !== null && $gross !== null) {
|
|
$gross = round($net * $factor, 4);
|
|
}
|
|
|
|
$data['price_per_kg'] = $net;
|
|
$data['price_per_kg_gross'] = $gross;
|
|
$data['price_total'] = null;
|
|
|
|
return $data;
|
|
}
|
|
|
|
/**
|
|
* @param array<string, mixed> $data
|
|
*/
|
|
public function receive(StockEntry $stockEntry, array $data): StockEntry
|
|
{
|
|
$data['status'] = 'received';
|
|
$data['received_by'] = auth()->id();
|
|
|
|
$stockEntry->update($data);
|
|
|
|
return $stockEntry->fresh();
|
|
}
|
|
|
|
/**
|
|
* @return Collection<int, StockEntry>
|
|
*/
|
|
public function getByStatus(string $status): Collection
|
|
{
|
|
return StockEntry::query()
|
|
->where('status', $status)
|
|
->orderByDesc('ordered_at')
|
|
->get();
|
|
}
|
|
|
|
/**
|
|
* @return Collection<int, StockEntry>
|
|
*/
|
|
public function getForIngredient(int $ingredientId): Collection
|
|
{
|
|
return StockEntry::query()
|
|
->where('ingredient_id', $ingredientId)
|
|
->where('status', 'received')
|
|
->orderByDesc('received_at')
|
|
->get();
|
|
}
|
|
|
|
/**
|
|
* Letzter Netto-kg-Preis je Lieferant für einen Rohstoff (neuester Einkauf zählt).
|
|
*
|
|
* @return array<int, float> [supplier_id => price_per_kg_net]
|
|
*/
|
|
public function lastNetPricePerSupplier(int $ingredientId): array
|
|
{
|
|
$ranked = StockEntry::query()
|
|
->selectRaw('supplier_id, price_per_kg, ROW_NUMBER() OVER (PARTITION BY supplier_id ORDER BY ordered_at DESC, id DESC) as rn')
|
|
->where('entry_type', 'ingredient')
|
|
->where('ingredient_id', $ingredientId)
|
|
->whereNotNull('supplier_id')
|
|
->whereNotNull('price_per_kg');
|
|
|
|
// DB::query statt StockEntry::query: der SoftDeletes-Global-Scope würde
|
|
// sonst "stock_entries.deleted_at" auf dem Sub-Select-Alias referenzieren.
|
|
return \DB::query()
|
|
->fromSub($ranked, 'ranked')
|
|
->where('rn', 1)
|
|
->pluck('price_per_kg', 'supplier_id')
|
|
->map(fn ($price) => (float) $price)
|
|
->all();
|
|
}
|
|
|
|
/**
|
|
* Liste: Pending zuerst (neuestes Bestelldatum), dann Received (neuester Eingang).
|
|
* Eingegangene Einträge sind auf $receivedLimit gedeckelt (OPT-3); null = alle.
|
|
*
|
|
* @return Collection<int, StockEntry>
|
|
*/
|
|
public function listForIndex(?int $receivedLimit = 500): Collection
|
|
{
|
|
$with = [
|
|
'ingredient',
|
|
'packagingItem',
|
|
'supplier',
|
|
'location',
|
|
'quality',
|
|
];
|
|
|
|
$pending = StockEntry::query()->with($with)
|
|
->where('status', 'pending')
|
|
->orderByDesc('ordered_at')
|
|
->orderByDesc('id')
|
|
->get();
|
|
|
|
$received = StockEntry::query()->with($with)
|
|
->where('status', 'received')
|
|
->orderByDesc('received_at')
|
|
->orderByDesc('id')
|
|
->when($receivedLimit !== null, fn ($q) => $q->limit($receivedLimit))
|
|
->get();
|
|
|
|
return $pending->concat($received)->values();
|
|
}
|
|
}
|