Warenwirtschaft: AP-27 Wareneingang gleiche Charge zusammenführen
- Chargen-Existenz-Check beim Buchen (JSON-Endpoint batch-check): Hinweis „Charge bereits im Haus" + Vorbelegung MHD/Qualität aus dem letzten Eingang - Rohstoffbestand-Detail: Chargen je batch_number summiert (InventoryService::chargeGroupsForIngredient), Einkäufe als Unterzeilen - Produktion: Charge-Dropdown je Charge mit Summen-Rest (batches im Recipe-Payload), Verbrauch wird per expandBatchLines FEFO auf die Einzel-Einkäufe der Charge verteilt; Service-API bleibt je stock_entry - Jeder Einkauf bleibt eigener stock_entry (Preis/Datum/Audit) - Hinweise-Doku + Plan V6.0 §6 fortgeschrieben Tests: StockEntryChargeMergeTest (7 neu); Suite 193 grün, 698 Assertions Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
parent
e47d848d3d
commit
7a5b5e0f7b
16 changed files with 673 additions and 34 deletions
|
|
@ -310,6 +310,34 @@ class InventoryService
|
|||
->values();
|
||||
}
|
||||
|
||||
/**
|
||||
* AP-27: Verfügbare Chargen eines Rohstoffs je Chargennummer zusammengeführt.
|
||||
* Jeder Einkauf bleibt ein eigener stock_entry (in `entries` enthalten),
|
||||
* Restbestand/Anzeige werden je batch_number summiert. FEFO-Reihenfolge bleibt erhalten.
|
||||
*
|
||||
* @return \Illuminate\Support\Collection<int, array{key: string, batch_number: ?string, entries: Collection<int, StockEntry>, remaining: float, best_before: ?Carbon, suppliers: \Illuminate\Support\Collection<int, string>, locations: \Illuminate\Support\Collection<int, string>}>
|
||||
*/
|
||||
public function chargeGroupsForIngredient(int $ingredientId): \Illuminate\Support\Collection
|
||||
{
|
||||
return $this->chargesWithRemainingForIngredient($ingredientId)
|
||||
->groupBy(fn (StockEntry $entry) => $entry->batchGroupKey())
|
||||
->map(function (Collection $entries, string $key) {
|
||||
/** @var StockEntry $first */
|
||||
$first = $entries->first();
|
||||
|
||||
return [
|
||||
'key' => $key,
|
||||
'batch_number' => $first->batch_number,
|
||||
'entries' => $entries->values(),
|
||||
'remaining' => round($entries->sum(fn (StockEntry $e) => (float) $e->getAttribute('remaining_quantity')), 2),
|
||||
'best_before' => $entries->pluck('best_before')->filter()->min(),
|
||||
'suppliers' => $entries->map(fn (StockEntry $e) => $e->supplier?->name)->filter()->unique()->values(),
|
||||
'locations' => $entries->map(fn (StockEntry $e) => $e->location?->name)->filter()->unique()->values(),
|
||||
];
|
||||
})
|
||||
->values();
|
||||
}
|
||||
|
||||
/**
|
||||
* Offene (bestellte, noch nicht eingegangene) Einkäufe eines Rohstoffs.
|
||||
*
|
||||
|
|
|
|||
|
|
@ -350,6 +350,104 @@ class ProductionService
|
|||
return implode(' - ', $parts);
|
||||
}
|
||||
|
||||
/**
|
||||
* AP-27: Verfügbare Chargen je batch_number zusammengeführt (FEFO-Reihenfolge bleibt erhalten).
|
||||
* Grundlage für das Charge-Dropdown der Produktion: eine Option je Charge mit Summen-Rest.
|
||||
*
|
||||
* @param Collection<int, StockEntry> $entries FEFO-sortierte Einträge mit remaining_quantity
|
||||
* @return array<int, array{key: string, label: string, batch_number: ?string, best_before: ?string, remaining: float, purchases: int}>
|
||||
*/
|
||||
public function batchOptionsForEntries(Collection $entries): array
|
||||
{
|
||||
return $entries
|
||||
->groupBy(fn (StockEntry $entry) => $entry->batchGroupKey())
|
||||
->map(function ($group, string $key) {
|
||||
/** @var StockEntry $first */
|
||||
$first = $group->first();
|
||||
$suppliers = $group->map(fn (StockEntry $e) => $e->supplier?->name)->filter()->unique();
|
||||
$bestBefore = $group->pluck('best_before')->filter()->min();
|
||||
|
||||
$labelParts = [];
|
||||
$labelParts[] = $suppliers->isNotEmpty() ? $suppliers->implode(', ') : __('Lieferant unbekannt');
|
||||
$labelParts[] = $first->batch_number ?: ('#'.$first->id);
|
||||
if ($bestBefore) {
|
||||
$labelParts[] = $bestBefore->format('d.m.Y');
|
||||
}
|
||||
|
||||
return [
|
||||
'key' => $key,
|
||||
'label' => implode(' - ', $labelParts),
|
||||
'batch_number' => $first->batch_number,
|
||||
'best_before' => $bestBefore?->format('Y-m-d'),
|
||||
'remaining' => round($group->sum(fn (StockEntry $e) => (float) $e->getAttribute('remaining_quantity')), 2),
|
||||
'purchases' => $group->count(),
|
||||
];
|
||||
})
|
||||
->values()
|
||||
->all();
|
||||
}
|
||||
|
||||
/**
|
||||
* AP-27: Löst Chargen-Zeilen (batch_key) in Einzel-Wareneingänge auf.
|
||||
* Der Verbrauch wird FEFO auf die Einträge der Charge verteilt; ein etwaiger
|
||||
* Überhang wird dem letzten Eintrag zugerechnet (kein Restbestands-Hardstop,
|
||||
* konsistent zum bisherigen Verhalten je Einzel-Eintrag).
|
||||
* Zeilen im alten Format (stock_entry_id) werden unverändert durchgereicht.
|
||||
*
|
||||
* @param array<int, array{ingredient_id: int, stock_entry_id?: int, batch_key?: string, quantity_used?: mixed}> $ingredientLines
|
||||
* @return array<int, array{ingredient_id: int, stock_entry_id: int, quantity_used: float|int|string}>
|
||||
*/
|
||||
public function expandBatchLines(array $ingredientLines, int $locationId, ?int $excludeProductionId = null): array
|
||||
{
|
||||
$expanded = [];
|
||||
$entriesByIngredient = [];
|
||||
|
||||
foreach ($ingredientLines as $line) {
|
||||
$batchKey = trim((string) ($line['batch_key'] ?? ''));
|
||||
if ($batchKey === '') {
|
||||
$expanded[] = $line;
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
$ingredientId = (int) $line['ingredient_id'];
|
||||
$entriesByIngredient[$ingredientId] ??= $this->availableStockEntriesForIngredient($ingredientId, $locationId, $excludeProductionId);
|
||||
|
||||
$batchEntries = $entriesByIngredient[$ingredientId]
|
||||
->filter(fn (StockEntry $entry) => $entry->batchGroupKey() === $batchKey)
|
||||
->values();
|
||||
|
||||
if ($batchEntries->isEmpty()) {
|
||||
throw ValidationException::withMessages([
|
||||
'ingredient_lines' => __('Charge :key ist am gewählten Lagerort nicht (mehr) verfügbar.', [
|
||||
'key' => substr($batchKey, 2),
|
||||
]),
|
||||
]);
|
||||
}
|
||||
|
||||
$needed = round($this->parseQuantity($line['quantity_used'] ?? null), 2);
|
||||
$lastIndex = $batchEntries->count() - 1;
|
||||
|
||||
foreach ($batchEntries as $index => $entry) {
|
||||
$capacity = (float) $entry->getAttribute('remaining_quantity');
|
||||
$take = $index === $lastIndex ? $needed : round(min($needed, $capacity), 2);
|
||||
if ($take > 0) {
|
||||
$expanded[] = [
|
||||
'ingredient_id' => $ingredientId,
|
||||
'stock_entry_id' => (int) $entry->id,
|
||||
'quantity_used' => $take,
|
||||
];
|
||||
}
|
||||
$needed = round($needed - $take, 2);
|
||||
if ($needed <= 0) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $expanded;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
|
|
@ -367,13 +465,14 @@ class ProductionService
|
|||
$factor = (float) ($ing->pivot->factor ?? 1.1);
|
||||
$hasGram = $gram !== null && $gram !== '';
|
||||
$req = $hasGram ? (float) $gram * $factor * $qty : null;
|
||||
$available = $this->availableStockEntriesForIngredient((int) $ing->id, $locationId, $excludeProductionId);
|
||||
$ingredients[] = [
|
||||
'id' => $ing->id,
|
||||
'name' => $ing->name,
|
||||
'gram' => $hasGram ? (float) $gram : null,
|
||||
'factor' => $factor,
|
||||
'required_grams_total' => $req,
|
||||
'stock_entries' => $this->availableStockEntriesForIngredient((int) $ing->id, $locationId, $excludeProductionId)
|
||||
'stock_entries' => $available
|
||||
->map(function (StockEntry $se) {
|
||||
return [
|
||||
'id' => $se->id,
|
||||
|
|
@ -383,6 +482,8 @@ class ProductionService
|
|||
'remaining' => (float) $se->getAttribute('remaining_quantity'),
|
||||
];
|
||||
})->values()->all(),
|
||||
// AP-27: Dropdown-Optionen je Charge zusammengeführt (Summen-Rest, FEFO-Reihenfolge).
|
||||
'batches' => $this->batchOptionsForEntries($available),
|
||||
];
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue