Warenwirtschaft: AP-09 bis AP-13 (Produktbestand, Set-Produkte, Ausschuss, Konzepte)
- AP-09 Produktbestand inkl. Bewegungshistorie (product_stock_movements, ProductStockService) - AP-10 Rohstoffbestand-Ansicht je Lager (RawMaterialStockController) - AP-11 Bestandsschwellen / Out-of-Stock-Handling fuer Produkte und Shop - AP-12 Ausgang/Ausschuss (stock_disposals, StockDisposalController, InventoryService) - Set-Produkte (product_set_items) inkl. Aufloesung - Produktentwicklung & Hinweise-Verwaltung (Notices) - AP-13 Entwicklungskonzept Shop-Bestandsabzug im Plan dokumentiert - Feature-Tests fuer neue Module + aktualisierter Entwicklungsplan Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
parent
78679e0c55
commit
3ee2d756e9
63 changed files with 5968 additions and 901 deletions
309
app/Services/InventoryService.php
Normal file
309
app/Services/InventoryService.php
Normal file
|
|
@ -0,0 +1,309 @@
|
|||
<?php
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use App\Models\Ingredient;
|
||||
use App\Models\ProductionIngredient;
|
||||
use App\Models\ProductionPackaging;
|
||||
use App\Models\StockDisposal;
|
||||
use App\Models\StockEntry;
|
||||
use Carbon\Carbon;
|
||||
|
||||
class InventoryService
|
||||
{
|
||||
/**
|
||||
* Lookback-Fenster (Tage) für die Berechnung des durchschnittlichen Tagesverbrauchs
|
||||
* aus der Produktionshistorie.
|
||||
*/
|
||||
public const CONSUMPTION_WINDOW_DAYS = 90;
|
||||
|
||||
/**
|
||||
* Standard-Lieferzeit (Tage), falls weder am Rohstoff noch am Lieferanten ein Wert gepflegt ist.
|
||||
*/
|
||||
public const DEFAULT_LEAD_DAYS = 14;
|
||||
|
||||
/**
|
||||
* Gesamter Restbestand je Rohstoff in Gramm (nur eingegangene Chargen, abzüglich Produktionsverbrauch).
|
||||
*
|
||||
* @param array<int, int>|null $ingredientIds Optionaler Filter; null = alle.
|
||||
* @return array<int, float> [ingredient_id => grams]
|
||||
*/
|
||||
public function remainingByIngredient(?array $ingredientIds = null): array
|
||||
{
|
||||
$received = StockEntry::query()
|
||||
->where('status', 'received')
|
||||
->where('entry_type', 'ingredient')
|
||||
->whereNotNull('ingredient_id')
|
||||
->when($ingredientIds !== null, fn ($q) => $q->whereIn('ingredient_id', $ingredientIds))
|
||||
->selectRaw('ingredient_id, SUM(received_quantity) as qty')
|
||||
->groupBy('ingredient_id')
|
||||
->pluck('qty', 'ingredient_id');
|
||||
|
||||
$consumed = ProductionIngredient::query()
|
||||
->when($ingredientIds !== null, fn ($q) => $q->whereIn('ingredient_id', $ingredientIds))
|
||||
->selectRaw('ingredient_id, SUM(quantity_used) as qty')
|
||||
->groupBy('ingredient_id')
|
||||
->pluck('qty', 'ingredient_id');
|
||||
|
||||
$disposed = $this->disposedByIngredient($ingredientIds);
|
||||
|
||||
$result = [];
|
||||
foreach ($received as $id => $qty) {
|
||||
$result[(int) $id] = round((float) $qty - (float) ($consumed[$id] ?? 0) - (float) ($disposed[(int) $id] ?? 0), 2);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Per Ausgang/Ausschuss entnommene Menge je Rohstoff in Gramm.
|
||||
*
|
||||
* @param array<int, int>|null $ingredientIds
|
||||
* @return array<int, float> [ingredient_id => grams]
|
||||
*/
|
||||
public function disposedByIngredient(?array $ingredientIds = null): array
|
||||
{
|
||||
$rows = StockDisposal::query()
|
||||
->where('disposal_type', 'ingredient')
|
||||
->whereNotNull('ingredient_id')
|
||||
->when($ingredientIds !== null, fn ($q) => $q->whereIn('ingredient_id', $ingredientIds))
|
||||
->selectRaw('ingredient_id, SUM(quantity) as qty')
|
||||
->groupBy('ingredient_id')
|
||||
->pluck('qty', 'ingredient_id');
|
||||
|
||||
$result = [];
|
||||
foreach ($rows as $id => $qty) {
|
||||
$result[(int) $id] = round((float) $qty, 2);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Restbestand eines Rohstoffs je Lagerort.
|
||||
*
|
||||
* @return array<int, float> [location_id => grams]
|
||||
*/
|
||||
public function remainingByLocationForIngredient(int $ingredientId): array
|
||||
{
|
||||
$received = StockEntry::query()
|
||||
->where('status', 'received')
|
||||
->where('entry_type', 'ingredient')
|
||||
->where('ingredient_id', $ingredientId)
|
||||
->selectRaw('location_id, SUM(received_quantity) as qty')
|
||||
->groupBy('location_id')
|
||||
->pluck('qty', 'location_id');
|
||||
|
||||
$consumed = ProductionIngredient::query()
|
||||
->join('stock_entries', 'stock_entries.id', '=', 'production_ingredients.stock_entry_id')
|
||||
->where('production_ingredients.ingredient_id', $ingredientId)
|
||||
->selectRaw('stock_entries.location_id as location_id, SUM(production_ingredients.quantity_used) as qty')
|
||||
->groupBy('stock_entries.location_id')
|
||||
->pluck('qty', 'location_id');
|
||||
|
||||
$disposed = StockDisposal::query()
|
||||
->where('disposal_type', 'ingredient')
|
||||
->where('ingredient_id', $ingredientId)
|
||||
->selectRaw('location_id, SUM(quantity) as qty')
|
||||
->groupBy('location_id')
|
||||
->pluck('qty', 'location_id');
|
||||
|
||||
$result = [];
|
||||
foreach ($received as $locationId => $qty) {
|
||||
$result[(int) $locationId] = round((float) $qty - (float) ($consumed[$locationId] ?? 0) - (float) ($disposed[$locationId] ?? 0), 2);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Restbestand je Verpackungsartikel in Stück (Wareneingang − Produktionsverbrauch − Ausschuss).
|
||||
*
|
||||
* @param array<int, int>|null $packagingItemIds
|
||||
* @return array<int, float> [packaging_item_id => pieces]
|
||||
*/
|
||||
public function remainingByPackagingItem(?array $packagingItemIds = null): array
|
||||
{
|
||||
$received = StockEntry::query()
|
||||
->where('status', 'received')
|
||||
->whereIn('entry_type', ['packaging', 'shipping'])
|
||||
->whereNotNull('packaging_item_id')
|
||||
->when($packagingItemIds !== null, fn ($q) => $q->whereIn('packaging_item_id', $packagingItemIds))
|
||||
->selectRaw('packaging_item_id, SUM(received_quantity) as qty')
|
||||
->groupBy('packaging_item_id')
|
||||
->pluck('qty', 'packaging_item_id');
|
||||
|
||||
$consumed = ProductionPackaging::query()
|
||||
->when($packagingItemIds !== null, fn ($q) => $q->whereIn('packaging_item_id', $packagingItemIds))
|
||||
->selectRaw('packaging_item_id, SUM(quantity_used) as qty')
|
||||
->groupBy('packaging_item_id')
|
||||
->pluck('qty', 'packaging_item_id');
|
||||
|
||||
$disposed = $this->disposedByPackagingItem($packagingItemIds);
|
||||
|
||||
$result = [];
|
||||
foreach ($received as $id => $qty) {
|
||||
$result[(int) $id] = round((float) $qty - (float) ($consumed[$id] ?? 0) - (float) ($disposed[(int) $id] ?? 0), 2);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Per Ausgang/Ausschuss entnommene Menge je Verpackungsartikel in Stück.
|
||||
*
|
||||
* @param array<int, int>|null $packagingItemIds
|
||||
* @return array<int, float> [packaging_item_id => pieces]
|
||||
*/
|
||||
public function disposedByPackagingItem(?array $packagingItemIds = null): array
|
||||
{
|
||||
$rows = StockDisposal::query()
|
||||
->where('disposal_type', 'packaging')
|
||||
->whereNotNull('packaging_item_id')
|
||||
->when($packagingItemIds !== null, fn ($q) => $q->whereIn('packaging_item_id', $packagingItemIds))
|
||||
->selectRaw('packaging_item_id, SUM(quantity) as qty')
|
||||
->groupBy('packaging_item_id')
|
||||
->pluck('qty', 'packaging_item_id');
|
||||
|
||||
$result = [];
|
||||
foreach ($rows as $id => $qty) {
|
||||
$result[(int) $id] = round((float) $qty, 2);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Offene (bestellte, noch nicht eingegangene) Menge je Rohstoff in Gramm.
|
||||
*
|
||||
* @param array<int, int>|null $ingredientIds
|
||||
* @return array<int, float> [ingredient_id => grams]
|
||||
*/
|
||||
public function openOrderQuantityByIngredient(?array $ingredientIds = null): array
|
||||
{
|
||||
$rows = StockEntry::query()
|
||||
->where('status', 'pending')
|
||||
->where('entry_type', 'ingredient')
|
||||
->whereNotNull('ingredient_id')
|
||||
->when($ingredientIds !== null, fn ($q) => $q->whereIn('ingredient_id', $ingredientIds))
|
||||
->selectRaw('ingredient_id, SUM(ordered_quantity) as qty')
|
||||
->groupBy('ingredient_id')
|
||||
->pluck('qty', 'ingredient_id');
|
||||
|
||||
$result = [];
|
||||
foreach ($rows as $id => $qty) {
|
||||
$result[(int) $id] = round((float) $qty, 2);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Durchschnittlicher Tagesverbrauch je Rohstoff (Gramm/Tag) aus der Produktionshistorie.
|
||||
*
|
||||
* @param array<int, int>|null $ingredientIds
|
||||
* @return array<int, float> [ingredient_id => grams_per_day]
|
||||
*/
|
||||
public function dailyConsumptionByIngredient(?array $ingredientIds = null, int $windowDays = self::CONSUMPTION_WINDOW_DAYS): array
|
||||
{
|
||||
$windowDays = max(1, $windowDays);
|
||||
$since = Carbon::now()->subDays($windowDays)->startOfDay();
|
||||
|
||||
$rows = ProductionIngredient::query()
|
||||
->join('productions', 'productions.id', '=', 'production_ingredients.production_id')
|
||||
->where('productions.produced_at', '>=', $since)
|
||||
->when($ingredientIds !== null, fn ($q) => $q->whereIn('production_ingredients.ingredient_id', $ingredientIds))
|
||||
->selectRaw('production_ingredients.ingredient_id as ingredient_id, SUM(production_ingredients.quantity_used) as qty')
|
||||
->groupBy('production_ingredients.ingredient_id')
|
||||
->pluck('qty', 'ingredient_id');
|
||||
|
||||
$result = [];
|
||||
foreach ($rows as $id => $qty) {
|
||||
$result[(int) $id] = round((float) $qty / $windowDays, 4);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Tage bis der Bestand bei gleichbleibendem Verbrauch aufgebraucht ist (null = kein Verbrauch).
|
||||
*/
|
||||
public function daysUntilEmpty(float $remaining, ?float $dailyConsumption): ?int
|
||||
{
|
||||
if ($dailyConsumption === null || $dailyConsumption <= 0) {
|
||||
return null;
|
||||
}
|
||||
if ($remaining <= 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return (int) floor($remaining / $dailyConsumption);
|
||||
}
|
||||
|
||||
/**
|
||||
* Voraussichtliches Datum, an dem der Bestand auf null fällt (null = kein Verbrauch).
|
||||
*/
|
||||
public function expectedEmptyDate(float $remaining, ?float $dailyConsumption): ?Carbon
|
||||
{
|
||||
$days = $this->daysUntilEmpty($remaining, $dailyConsumption);
|
||||
if ($days === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return Carbon::now()->startOfDay()->addDays($days);
|
||||
}
|
||||
|
||||
/**
|
||||
* Bestands-Status eines Rohstoffs:
|
||||
* - "critical": Meldebestand gepflegt und unterschritten (rot, Badge-relevant).
|
||||
* - "critical_ordered": kritisch, aber es existiert bereits eine offene Bestellung (entschärft).
|
||||
* - "warning": Bestand reicht voraussichtlich nicht mehr bis zur nächsten Lieferung.
|
||||
* - "ok": ausreichend.
|
||||
*/
|
||||
public function stockStatus(?float $minStockAlert, float $remaining, ?float $dailyConsumption, ?int $leadDays, bool $hasOpenOrder = false): string
|
||||
{
|
||||
if ($minStockAlert !== null && $minStockAlert > 0 && $remaining <= $minStockAlert) {
|
||||
return $hasOpenOrder ? 'critical_ordered' : 'critical';
|
||||
}
|
||||
|
||||
$days = $this->daysUntilEmpty($remaining, $dailyConsumption);
|
||||
if ($days !== null) {
|
||||
$lead = $leadDays !== null && $leadDays > 0 ? $leadDays : self::DEFAULT_LEAD_DAYS;
|
||||
if ($days <= $lead) {
|
||||
return 'warning';
|
||||
}
|
||||
}
|
||||
|
||||
return 'ok';
|
||||
}
|
||||
|
||||
/**
|
||||
* Anzahl der kritischen (Meldebestand unterschritten) aktiven Rohstoffe – für das Sidenav-Badge.
|
||||
*/
|
||||
public function criticalIngredientCount(): int
|
||||
{
|
||||
$ingredients = Ingredient::query()
|
||||
->where('active', true)
|
||||
->whereNotNull('min_stock_alert')
|
||||
->where('min_stock_alert', '>', 0)
|
||||
->get(['id', 'min_stock_alert']);
|
||||
|
||||
if ($ingredients->isEmpty()) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
$ids = $ingredients->pluck('id')->all();
|
||||
$remaining = $this->remainingByIngredient($ids);
|
||||
$openOrders = $this->openOrderQuantityByIngredient($ids);
|
||||
|
||||
return $ingredients->filter(function (Ingredient $ingredient) use ($remaining, $openOrders) {
|
||||
$rem = $remaining[$ingredient->id] ?? 0.0;
|
||||
if ($rem > (float) $ingredient->min_stock_alert) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Kritisch, aber bereits nachbestellt => entschärft, nicht im Badge zählen.
|
||||
return ($openOrders[$ingredient->id] ?? 0.0) <= 0;
|
||||
})->count();
|
||||
}
|
||||
}
|
||||
146
app/Services/ProductStockService.php
Normal file
146
app/Services/ProductStockService.php
Normal file
|
|
@ -0,0 +1,146 @@
|
|||
<?php
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use App\Models\Product;
|
||||
use App\Models\Production;
|
||||
use App\Models\ProductStockMovement;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class ProductStockService
|
||||
{
|
||||
/**
|
||||
* Aktueller Bestand je Produkt (Summe Eingänge − Summe Ausgänge).
|
||||
*
|
||||
* @param array<int, int>|null $productIds
|
||||
* @return array<int, int> [product_id => stock]
|
||||
*/
|
||||
public function currentStockByProduct(?array $productIds = null): array
|
||||
{
|
||||
$rows = ProductStockMovement::query()
|
||||
->when($productIds !== null, fn ($q) => $q->whereIn('product_id', $productIds))
|
||||
->selectRaw("product_id, SUM(CASE WHEN direction = 'in' THEN quantity ELSE -quantity END) as stock")
|
||||
->groupBy('product_id')
|
||||
->pluck('stock', 'product_id');
|
||||
|
||||
$result = [];
|
||||
foreach ($rows as $id => $stock) {
|
||||
$result[(int) $id] = (int) $stock;
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function currentStock(int $productId): int
|
||||
{
|
||||
return $this->currentStockByProduct([$productId])[$productId] ?? 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Bucht eine Bestandsbewegung. Menge wird immer positiv gespeichert, die Richtung steuert das Vorzeichen.
|
||||
*
|
||||
* @param array{type: string, id: int}|Model|null $reference
|
||||
*/
|
||||
public function recordMovement(
|
||||
Product $product,
|
||||
string $direction,
|
||||
int $quantity,
|
||||
string $reason,
|
||||
string $source = 'manual',
|
||||
?string $note = null,
|
||||
?int $userId = null,
|
||||
Model|array|null $reference = null,
|
||||
): ProductStockMovement {
|
||||
$attributes = [
|
||||
'product_id' => $product->id,
|
||||
'direction' => $direction === 'out' ? 'out' : 'in',
|
||||
'quantity' => max(0, $quantity),
|
||||
'reason' => $reason,
|
||||
'source' => $source,
|
||||
'note' => $note,
|
||||
'user_id' => $userId,
|
||||
];
|
||||
|
||||
if ($reference instanceof Model) {
|
||||
$attributes['reference_type'] = $reference->getMorphClass();
|
||||
$attributes['reference_id'] = $reference->getKey();
|
||||
} elseif (is_array($reference)) {
|
||||
$attributes['reference_type'] = $reference['type'];
|
||||
$attributes['reference_id'] = $reference['id'];
|
||||
}
|
||||
|
||||
return ProductStockMovement::query()->create($attributes);
|
||||
}
|
||||
|
||||
/**
|
||||
* Verbucht den Produktbestand einer Produktion idempotent: gleicht die bereits gebuchte
|
||||
* Menge dieser Produktion mit der Soll-Stückzahl ab und bucht nur die Differenz.
|
||||
*/
|
||||
public function recordProductionStock(Production $production, ?int $userId = null): void
|
||||
{
|
||||
$production->loadMissing('product');
|
||||
|
||||
$alreadyBooked = (int) ProductStockMovement::query()
|
||||
->where('source', 'production')
|
||||
->where('reference_type', $production->getMorphClass())
|
||||
->where('reference_id', $production->getKey())
|
||||
->selectRaw("COALESCE(SUM(CASE WHEN direction = 'in' THEN quantity ELSE -quantity END), 0) as net")
|
||||
->value('net');
|
||||
|
||||
$target = (int) $production->quantity;
|
||||
$delta = $target - $alreadyBooked;
|
||||
|
||||
if ($delta === 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->recordMovement(
|
||||
$production->product,
|
||||
$delta > 0 ? 'in' : 'out',
|
||||
abs($delta),
|
||||
$alreadyBooked === 0 ? __('Produktion') : __('Produktionskorrektur'),
|
||||
'production',
|
||||
null,
|
||||
$userId ?? $production->produced_by,
|
||||
$production,
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Bestands-Status: "critical" (rot) ≤ kritischer Schwellwert, "warning" (gelb) ≤ Meldebestand, sonst "ok".
|
||||
*/
|
||||
public function productStatus(int $stock, ?int $minStock, ?int $criticalStock): string
|
||||
{
|
||||
if ($criticalStock !== null && $stock <= $criticalStock) {
|
||||
return 'critical';
|
||||
}
|
||||
if ($minStock !== null && $stock <= $minStock) {
|
||||
return 'warning';
|
||||
}
|
||||
|
||||
return 'ok';
|
||||
}
|
||||
|
||||
/**
|
||||
* Anzahl kritischer Hauptprodukte (Bestand ≤ kritischer Schwellwert) – für das Sidenav-Badge.
|
||||
*/
|
||||
public function criticalProductCount(): int
|
||||
{
|
||||
$products = Product::query()
|
||||
->where('active', true)
|
||||
->where('is_set', false)
|
||||
->whereNull('main_product_id')
|
||||
->whereNotNull('critical_product_stock')
|
||||
->get(['id', 'critical_product_stock']);
|
||||
|
||||
if ($products->isEmpty()) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
$stock = $this->currentStockByProduct($products->pluck('id')->all());
|
||||
|
||||
return $products->filter(function (Product $product) use ($stock) {
|
||||
return ($stock[$product->id] ?? 0) <= (int) $product->critical_product_stock;
|
||||
})->count();
|
||||
}
|
||||
}
|
||||
|
|
@ -4,6 +4,7 @@ namespace App\Services;
|
|||
|
||||
use App\Models\Product;
|
||||
use App\Models\Production;
|
||||
use App\Models\ProductionIngredient;
|
||||
use App\Models\StockEntry;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Support\Collection;
|
||||
|
|
@ -12,6 +13,10 @@ use Illuminate\Validation\ValidationException;
|
|||
|
||||
class ProductionService
|
||||
{
|
||||
public function __construct(
|
||||
protected ProductStockService $productStockService,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $data
|
||||
* @param array<int, array{ingredient_id: int, stock_entry_id: int, quantity_used: float|int|string}> $ingredientLines
|
||||
|
|
@ -20,7 +25,7 @@ class ProductionService
|
|||
{
|
||||
return DB::transaction(function () use ($data, $ingredientLines, $userId) {
|
||||
$product = Product::query()
|
||||
->with(['p_ingredients', 'packagings'])
|
||||
->with(['manufacturer_ingredients', 'packagings'])
|
||||
->findOrFail($data['product_id']);
|
||||
|
||||
$locationId = (int) $data['location_id'];
|
||||
|
|
@ -29,44 +34,22 @@ class ProductionService
|
|||
throw ValidationException::withMessages(['quantity' => __('Die Stückzahl muss mindestens 1 sein.')]);
|
||||
}
|
||||
|
||||
if ($product->p_ingredients->isEmpty()) {
|
||||
throw ValidationException::withMessages(['product_id' => __('Das Produkt hat keine Rezeptur (Inhaltsstoffe).')]);
|
||||
}
|
||||
$this->assertNotASet($product);
|
||||
|
||||
$requiredGrams = $this->requiredGramsByIngredient($product, $producedQty);
|
||||
if ($product->no_recipe_required) {
|
||||
$ingredientLines = [];
|
||||
} else {
|
||||
$this->assertManufacturerRecipe($product);
|
||||
|
||||
$sums = [];
|
||||
foreach ($ingredientLines as $line) {
|
||||
$iid = (int) $line['ingredient_id'];
|
||||
$used = $this->parseQuantity($line['quantity_used'] ?? null);
|
||||
$sums[$iid] = ($sums[$iid] ?? 0) + $used;
|
||||
}
|
||||
$requiredGrams = $this->requiredGramsByIngredient($product, $producedQty);
|
||||
|
||||
foreach ($requiredGrams as $iid => $req) {
|
||||
$sum = $sums[$iid] ?? 0;
|
||||
if (abs($sum - $req) > 0.02) {
|
||||
throw ValidationException::withMessages([
|
||||
'ingredient_lines' => __('Summe der Chargen-Mengen pro INCI muss dem Soll-Verbrauch entsprechen (INCI :id: Soll :req g, Ist :sum g).', [
|
||||
'id' => $iid,
|
||||
'req' => number_format($req, 2, ',', '.'),
|
||||
'sum' => number_format($sum, 2, ',', '.'),
|
||||
]),
|
||||
]);
|
||||
$this->assertLinesMatchRecipe($requiredGrams, $ingredientLines);
|
||||
|
||||
foreach ($ingredientLines as $line) {
|
||||
$this->assertStockEntryMatchesLine($line, $locationId);
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($sums as $iid => $_sum) {
|
||||
if (! isset($requiredGrams[$iid])) {
|
||||
throw ValidationException::withMessages([
|
||||
'ingredient_lines' => __('Unerwarteter Inhaltsstoff in den Chargen-Zeilen.'),
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($ingredientLines as $line) {
|
||||
$this->assertStockEntryMatchesLine($line, $locationId);
|
||||
}
|
||||
|
||||
$mhdWarning = $this->computeMhdWarning($product, $data['produced_at'], $ingredientLines);
|
||||
|
||||
$production = Production::query()->create([
|
||||
|
|
@ -87,17 +70,10 @@ class ProductionService
|
|||
]);
|
||||
}
|
||||
|
||||
foreach ($product->packagings as $bom) {
|
||||
$perUnit = (float) ($bom->pivot->quantity ?? 1);
|
||||
$pieces = (int) round($perUnit * $producedQty);
|
||||
if ($pieces < 1) {
|
||||
$pieces = 1;
|
||||
}
|
||||
$production->productionPackagings()->create([
|
||||
'packaging_item_id' => $bom->id,
|
||||
'quantity_used' => $pieces,
|
||||
]);
|
||||
}
|
||||
$this->syncPackagingSnapshot($production, $product, $producedQty);
|
||||
|
||||
$production->setRelation('product', $product);
|
||||
$this->productStockService->recordProductionStock($production, $userId);
|
||||
|
||||
return $production->fresh(['product', 'location', 'productionIngredients', 'productionPackagings']);
|
||||
});
|
||||
|
|
@ -109,9 +85,9 @@ class ProductionService
|
|||
*/
|
||||
public function updateProduction(Production $production, array $data, array $ingredientLines, int $userId): Production
|
||||
{
|
||||
return DB::transaction(function () use ($production, $data, $ingredientLines) {
|
||||
return DB::transaction(function () use ($production, $data, $ingredientLines, $userId) {
|
||||
$product = Product::query()
|
||||
->with(['p_ingredients', 'packagings'])
|
||||
->with(['manufacturer_ingredients', 'packagings'])
|
||||
->findOrFail($data['product_id']);
|
||||
|
||||
$locationId = (int) $data['location_id'];
|
||||
|
|
@ -120,36 +96,22 @@ class ProductionService
|
|||
throw ValidationException::withMessages(['quantity' => __('Die Stückzahl muss mindestens 1 sein.')]);
|
||||
}
|
||||
|
||||
if ($product->p_ingredients->isEmpty()) {
|
||||
throw ValidationException::withMessages(['product_id' => __('Das Produkt hat keine Rezeptur (Inhaltsstoffe).')]);
|
||||
}
|
||||
$this->assertNotASet($product);
|
||||
|
||||
$requiredGrams = $this->requiredGramsByIngredient($product, $producedQty);
|
||||
if ($product->no_recipe_required) {
|
||||
$ingredientLines = [];
|
||||
} else {
|
||||
$this->assertManufacturerRecipe($product);
|
||||
|
||||
$sums = [];
|
||||
foreach ($ingredientLines as $line) {
|
||||
$iid = (int) $line['ingredient_id'];
|
||||
$used = $this->parseQuantity($line['quantity_used'] ?? null);
|
||||
$sums[$iid] = ($sums[$iid] ?? 0) + $used;
|
||||
}
|
||||
$requiredGrams = $this->requiredGramsByIngredient($product, $producedQty);
|
||||
|
||||
foreach ($requiredGrams as $iid => $req) {
|
||||
$sum = $sums[$iid] ?? 0;
|
||||
if (abs($sum - $req) > 0.02) {
|
||||
throw ValidationException::withMessages([
|
||||
'ingredient_lines' => __('Summe der Chargen-Mengen pro INCI muss dem Soll-Verbrauch entsprechen (INCI :id: Soll :req g, Ist :sum g).', [
|
||||
'id' => $iid,
|
||||
'req' => number_format($req, 2, ',', '.'),
|
||||
'sum' => number_format($sum, 2, ',', '.'),
|
||||
]),
|
||||
]);
|
||||
$this->assertLinesMatchRecipe($requiredGrams, $ingredientLines);
|
||||
|
||||
foreach ($ingredientLines as $line) {
|
||||
$this->assertStockEntryMatchesLine($line, $locationId);
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($ingredientLines as $line) {
|
||||
$this->assertStockEntryMatchesLine($line, $locationId);
|
||||
}
|
||||
|
||||
$mhdWarning = $this->computeMhdWarning($product, $data['produced_at'], $ingredientLines);
|
||||
|
||||
$production->update([
|
||||
|
|
@ -171,33 +133,102 @@ class ProductionService
|
|||
}
|
||||
|
||||
$production->productionPackagings()->delete();
|
||||
foreach ($product->packagings as $bom) {
|
||||
$perUnit = (float) ($bom->pivot->quantity ?? 1);
|
||||
$pieces = (int) round($perUnit * $producedQty);
|
||||
if ($pieces < 1) {
|
||||
$pieces = 1;
|
||||
}
|
||||
$production->productionPackagings()->create([
|
||||
'packaging_item_id' => $bom->id,
|
||||
'quantity_used' => $pieces,
|
||||
]);
|
||||
}
|
||||
$this->syncPackagingSnapshot($production, $product, $producedQty);
|
||||
|
||||
$production->setRelation('product', $product);
|
||||
$this->productStockService->recordProductionStock($production, $userId);
|
||||
|
||||
return $production->fresh(['product', 'location', 'productionIngredients', 'productionPackagings']);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets sind Bündel aus Einzelprodukten und werden nicht produziert.
|
||||
*/
|
||||
public function assertNotASet(Product $product): void
|
||||
{
|
||||
if ($product->is_set) {
|
||||
throw ValidationException::withMessages([
|
||||
'product_id' => __('Sets können nicht produziert werden. Bitte ein Einzelprodukt wählen.'),
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Produktion basiert ausschließlich auf der Hersteller-Rezeptur (kein Fallback auf die Produkt-Rezeptur).
|
||||
*/
|
||||
public function assertManufacturerRecipe(Product $product): void
|
||||
{
|
||||
$product->loadMissing('manufacturer_ingredients');
|
||||
|
||||
if ($product->manufacturer_ingredients->isEmpty()) {
|
||||
throw ValidationException::withMessages([
|
||||
'product_id' => __('Für dieses Produkt ist keine Hersteller-Rezeptur gepflegt. Eine Produktion ist erst möglich, wenn eine Hersteller-Rezeptur hinterlegt wurde.'),
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<int, float> $requiredGrams
|
||||
* @param array<int, array{ingredient_id: int, stock_entry_id: int, quantity_used?: mixed}> $ingredientLines
|
||||
*/
|
||||
private function assertLinesMatchRecipe(array $requiredGrams, array $ingredientLines): void
|
||||
{
|
||||
$sums = [];
|
||||
foreach ($ingredientLines as $line) {
|
||||
$iid = (int) $line['ingredient_id'];
|
||||
$used = $this->parseQuantity($line['quantity_used'] ?? null);
|
||||
$sums[$iid] = ($sums[$iid] ?? 0) + $used;
|
||||
}
|
||||
|
||||
foreach ($requiredGrams as $iid => $req) {
|
||||
$sum = $sums[$iid] ?? 0;
|
||||
if (abs($sum - $req) > 0.02) {
|
||||
throw ValidationException::withMessages([
|
||||
'ingredient_lines' => __('Summe der Chargen-Mengen pro INCI muss dem Soll-Verbrauch entsprechen (INCI :id: Soll :req g, Ist :sum g).', [
|
||||
'id' => $iid,
|
||||
'req' => number_format($req, 2, ',', '.'),
|
||||
'sum' => number_format($sum, 2, ',', '.'),
|
||||
]),
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($sums as $iid => $_sum) {
|
||||
if (! isset($requiredGrams[$iid])) {
|
||||
throw ValidationException::withMessages([
|
||||
'ingredient_lines' => __('Unerwarteter Inhaltsstoff in den Chargen-Zeilen.'),
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private function syncPackagingSnapshot(Production $production, Product $product, int $producedQty): void
|
||||
{
|
||||
foreach ($product->packagings as $bom) {
|
||||
$perUnit = (float) ($bom->pivot->quantity ?? 1);
|
||||
$pieces = (int) round($perUnit * $producedQty);
|
||||
if ($pieces < 1) {
|
||||
$pieces = 1;
|
||||
}
|
||||
$production->productionPackagings()->create([
|
||||
'packaging_item_id' => $bom->id,
|
||||
'quantity_used' => $pieces,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<int, float>
|
||||
*/
|
||||
public function requiredGramsByIngredient(Product $product, int $producedQuantity): array
|
||||
{
|
||||
$required = [];
|
||||
foreach ($product->p_ingredients as $ing) {
|
||||
foreach ($product->manufacturer_ingredients as $ing) {
|
||||
$gram = $ing->pivot->gram;
|
||||
if ($gram === null || $gram === '') {
|
||||
throw ValidationException::withMessages([
|
||||
'product_id' => __('Für „:name“ fehlt der Anteil (%) in der Rezeptur.', ['name' => $ing->name]),
|
||||
'product_id' => __('Für „:name“ fehlt der Anteil (%) in der Hersteller-Rezeptur.', ['name' => $ing->name]),
|
||||
]);
|
||||
}
|
||||
$factor = (float) ($ing->pivot->factor ?? 1.1);
|
||||
|
|
@ -251,52 +282,107 @@ class ProductionService
|
|||
}
|
||||
|
||||
/**
|
||||
* Letzte empfangene Wareneingänge pro Inhaltsstoff am Standort (max. 3).
|
||||
* Verbrauchte Menge je Wareneingang (über alle Produktionen), optional ohne eine bestimmte Produktion.
|
||||
*
|
||||
* @param array<int, int> $stockEntryIds
|
||||
* @return array<int, float>
|
||||
*/
|
||||
public function consumedByStockEntry(array $stockEntryIds, ?int $excludeProductionId = null): array
|
||||
{
|
||||
if ($stockEntryIds === []) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$rows = ProductionIngredient::query()
|
||||
->selectRaw('stock_entry_id, SUM(quantity_used) as used')
|
||||
->whereIn('stock_entry_id', $stockEntryIds)
|
||||
->when($excludeProductionId !== null, fn ($q) => $q->where('production_id', '!=', $excludeProductionId))
|
||||
->groupBy('stock_entry_id')
|
||||
->pluck('used', 'stock_entry_id');
|
||||
|
||||
$result = [];
|
||||
foreach ($rows as $stockEntryId => $used) {
|
||||
$result[(int) $stockEntryId] = (float) $used;
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Wareneingänge eines Inhaltsstoffs am Standort mit Restbestand > 0 (FEFO-Reihenfolge).
|
||||
*
|
||||
* @return Collection<int, StockEntry>
|
||||
*/
|
||||
public function latestStockEntriesForIngredient(int $ingredientId, int $locationId, int $limit = 3)
|
||||
public function availableStockEntriesForIngredient(int $ingredientId, int $locationId, ?int $excludeProductionId = null): Collection
|
||||
{
|
||||
return StockEntry::query()
|
||||
$entries = StockEntry::query()
|
||||
->with('supplier')
|
||||
->where('status', 'received')
|
||||
->where('entry_type', 'ingredient')
|
||||
->where('ingredient_id', $ingredientId)
|
||||
->where('location_id', $locationId)
|
||||
->orderByDesc('received_at')
|
||||
->orderByDesc('id')
|
||||
->limit($limit)
|
||||
->orderByRaw('best_before is null, best_before asc')
|
||||
->orderBy('id')
|
||||
->get();
|
||||
|
||||
$consumed = $this->consumedByStockEntry($entries->pluck('id')->all(), $excludeProductionId);
|
||||
|
||||
return $entries
|
||||
->map(function (StockEntry $entry) use ($consumed) {
|
||||
$received = $entry->received_quantity !== null ? (float) $entry->received_quantity : 0.0;
|
||||
$entry->setAttribute('remaining_quantity', round($received - ($consumed[(int) $entry->id] ?? 0.0), 2));
|
||||
|
||||
return $entry;
|
||||
})
|
||||
->filter(fn (StockEntry $entry) => (float) $entry->getAttribute('remaining_quantity') > 0.0)
|
||||
->values();
|
||||
}
|
||||
|
||||
public function stockEntryLabel(StockEntry $entry): string
|
||||
{
|
||||
$parts = [];
|
||||
$parts[] = $entry->supplier?->name ?: __('Lieferant unbekannt');
|
||||
$parts[] = $entry->batch_number ?: ('#'.$entry->id);
|
||||
if ($entry->best_before) {
|
||||
$parts[] = $entry->best_before->format('d.m.Y');
|
||||
}
|
||||
|
||||
return implode(' - ', $parts);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function buildRecipePayload(Product $product, int $locationId, int $productionQuantity): array
|
||||
public function buildRecipePayload(Product $product, int $locationId, int $productionQuantity, ?int $excludeProductionId = null): array
|
||||
{
|
||||
$product->loadMissing(['p_ingredients', 'packagings.packagingMaterial']);
|
||||
$product->loadMissing(['manufacturer_ingredients', 'packagings.packagingMaterial']);
|
||||
|
||||
$qty = max(1, $productionQuantity);
|
||||
$recipeRequired = ! (bool) $product->no_recipe_required;
|
||||
$hasRecipe = $product->manufacturer_ingredients->isNotEmpty();
|
||||
|
||||
$ingredients = [];
|
||||
foreach ($product->p_ingredients as $ing) {
|
||||
foreach ($product->manufacturer_ingredients as $ing) {
|
||||
$gram = $ing->pivot->gram;
|
||||
$factor = (float) ($ing->pivot->factor ?? 1.1);
|
||||
$req = ($gram !== null && $gram !== '') ? (float) $gram * $factor * $qty : null;
|
||||
$hasGram = $gram !== null && $gram !== '';
|
||||
$req = $hasGram ? (float) $gram * $factor * $qty : null;
|
||||
$ingredients[] = [
|
||||
'id' => $ing->id,
|
||||
'name' => $ing->name,
|
||||
'gram' => $gram !== null && $gram !== '' ? (float) $gram : null,
|
||||
'gram' => $hasGram ? (float) $gram : null,
|
||||
'factor' => $factor,
|
||||
'required_grams_total' => $req,
|
||||
'stock_entries' => $this->latestStockEntriesForIngredient((int) $ing->id, $locationId)->map(function (StockEntry $se) {
|
||||
return [
|
||||
'id' => $se->id,
|
||||
'batch_number' => $se->batch_number,
|
||||
'best_before' => $se->best_before?->format('Y-m-d'),
|
||||
'received_at' => $se->received_at?->format('Y-m-d'),
|
||||
'received_quantity' => $se->received_quantity !== null ? (float) $se->received_quantity : null,
|
||||
];
|
||||
})->values()->all(),
|
||||
'stock_entries' => $this->availableStockEntriesForIngredient((int) $ing->id, $locationId, $excludeProductionId)
|
||||
->map(function (StockEntry $se) {
|
||||
return [
|
||||
'id' => $se->id,
|
||||
'label' => $this->stockEntryLabel($se),
|
||||
'batch_number' => $se->batch_number,
|
||||
'best_before' => $se->best_before?->format('Y-m-d'),
|
||||
'remaining' => (float) $se->getAttribute('remaining_quantity'),
|
||||
];
|
||||
})->values()->all(),
|
||||
];
|
||||
}
|
||||
|
||||
|
|
@ -307,7 +393,7 @@ class ProductionService
|
|||
'id' => $pk->id,
|
||||
'name' => $pk->name,
|
||||
'quantity_per_product' => $perUnit,
|
||||
'total_pieces' => (int) round($perUnit * max(1, $productionQuantity)),
|
||||
'total_pieces' => (int) round($perUnit * $qty),
|
||||
'weight_grams' => $pk->weight_grams !== null ? (float) $pk->weight_grams : null,
|
||||
'material_name' => $pk->packagingMaterial?->name,
|
||||
];
|
||||
|
|
@ -320,8 +406,10 @@ class ProductionService
|
|||
'shelf_life_type' => $product->shelf_life_type,
|
||||
'shelf_life_months' => $product->shelf_life_months,
|
||||
],
|
||||
'recipe_required' => $recipeRequired,
|
||||
'has_recipe' => $hasRecipe,
|
||||
'location_id' => $locationId,
|
||||
'production_quantity' => $productionQuantity,
|
||||
'production_quantity' => $qty,
|
||||
'ingredients' => $ingredients,
|
||||
'packagings' => $packagings,
|
||||
];
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue