Gruppen: Obergruppen + Mindestbestaende nur noch je Lagerort (Backend)

Zwei zusammenhaengende Umbauten, weil sie dieselben Stellen betreffen.

Obergruppen: Gruppen bilden jetzt einen gerichteten azyklischen Graphen statt
einer flachen Liste. Eine Gruppe darf unter MEHREREN Obergruppen haengen -
"Grillwurst" unter "Wurst" UND unter "Grillgut"; mit einem einzelnen parent_id
waere genau das nicht abbildbar. Bestand und Mindestbestand einer Gruppe zaehlen
den gesamten Untergraphen, wobei eine ueber zwei Wege erreichbare Untergruppe
nur einmal zaehlt (services/gruppen.py arbeitet durchgaengig mit Mengen).
Product.group_id bleibt unveraendert - ein Artikel haengt weiter an genau einer
Gruppe.

Mindestbestaende: der separate Gesamt-Mindestbestand entfaellt. Er wird zur
Zeile mit location_id NULL ("Ueberall") und ist damit die Wurzel ueber allen
Lagerorten - dieselbe Verrechnung wie bei verschachtelten Orten greift jetzt
auch zwischen Ueberall und Kueche, wodurch derselbe Artikel nicht mehr doppelt
in der Einkaufsliste steht. Alle Werte liegen einheitlich in Basiseinheiten
statt in drei verschiedenen Einheiten nebeneinander; das Umrechnen beim
Umschalten der Erfassungseinheit entfaellt dadurch ersatzlos.

_netted_topups nimmt die Hierarchie jetzt als Parameter und faltet damit
Lagerort-Baum und Gruppen-Graph. Verrechnet wird zwischen zwei Gruppen nur,
wenn die zaehlenden Artikel der Untergruppe eine Teilmenge der Obergruppe sind -
zaehlt die Obergruppe in Kilogramm und die Untergruppe in Stueck, kommt ein Kauf
dort oben nicht an.

Die vierfach kopierte Bestandssumme wandert in Sammelabfragen
(summe_bestand_base), sonst vervielfacht der transitive Teilgraph die Abfragen.

Einmalige Datenwanderung beim Start (Merker in den Einstellungen), 18 neue
Tests - darunter Doppelzaehlung ueber zwei Wege, Ringschutz und die bewusst
offene Grenze bei zwei Obergruppen mit gemeinsamer Untergruppe.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
Scarriffle
2026-08-15 21:48:14 +02:00
parent df65d9583c
commit eaacfd03e5
16 changed files with 1327 additions and 238 deletions

View File

@@ -12,6 +12,7 @@ from sqlalchemy.orm import Session
from .models import Barcode, Category, CategoryTracking, Item, Lot, Product, ProductImage
from .schemas import BarcodeOut, LocationMinStockOut, ProductOut
from .services.conversion import KIND_OF_BASE, display_unit_info
from .services.min_stock import UEBERALL_NAME, lies_ueberall
from .services.stock import current_stock, location_subtree_stock_base
@@ -68,26 +69,38 @@ def product_to_out(db: Session, product: Product) -> ProductOut:
out.unit_name = name
out.unit_factor = factor
# Mindestbestand in der Einheit anzeigen, in der er erfasst wurde.
if product.min_stock is not None:
# „Ueberall" (Ort NULL) ist der frueher separate Gesamt-Mindestbestand.
# ``out.min_stock`` bleibt im Vertrag Dashboards, Einkaufsliste und App
# lesen es unveraendert weiter, es kommt nur aus einer anderen Quelle.
out.min_stock = lies_ueberall(product.location_min_stocks)
if out.min_stock is not None:
if product.min_stock_in_packages and product.package_size:
out.min_stock_display = product.min_stock / product.package_size
out.min_stock_display = out.min_stock / product.package_size
out.min_stock_unit_label = "Pkg"
elif product.min_stock_unit is not None:
out.min_stock_display = product.min_stock / product.min_stock_unit.factor
out.min_stock_display = out.min_stock / product.min_stock_unit.factor
out.min_stock_unit_label = product.min_stock_unit.name
else:
out.min_stock_display = product.min_stock / factor
out.min_stock_display = out.min_stock / factor
out.min_stock_unit_label = name
out.location_min_stocks = [
LocationMinStockOut(
location_id=e.location_id,
location_name=e.location.name if e.location else None,
location_name=UEBERALL_NAME if e.location_id is None else (
e.location.name if e.location else None
),
min_stock=e.min_stock,
stock=location_subtree_stock_base(db, product, e.location_id),
stock=(
out.stock
if e.location_id is None
else location_subtree_stock_base(db, product, e.location_id)
),
)
# „Ueberall" zuerst, danach nach Anlagereihenfolge.
for e in sorted(
product.location_min_stocks, key=lambda x: (x.location_id is not None, x.id)
)
for e in sorted(product.location_min_stocks, key=lambda x: x.id)
]
return out
@@ -151,24 +164,33 @@ def products_to_out_bulk(db: Session, products: list[Product]) -> list[ProductOu
name, factor = display_unit_info(product)
o.unit_name = name
o.unit_factor = factor
if product.min_stock is not None:
o.min_stock = lies_ueberall(product.location_min_stocks)
if o.min_stock is not None:
if product.min_stock_in_packages and product.package_size:
o.min_stock_display = product.min_stock / product.package_size
o.min_stock_display = o.min_stock / product.package_size
o.min_stock_unit_label = "Pkg"
elif product.min_stock_unit is not None:
o.min_stock_display = product.min_stock / product.min_stock_unit.factor
o.min_stock_display = o.min_stock / product.min_stock_unit.factor
o.min_stock_unit_label = product.min_stock_unit.name
else:
o.min_stock_display = product.min_stock / factor
o.min_stock_display = o.min_stock / factor
o.min_stock_unit_label = name
o.location_min_stocks = [
LocationMinStockOut(
location_id=e.location_id,
location_name=e.location.name if e.location else None,
location_name=UEBERALL_NAME if e.location_id is None else (
e.location.name if e.location else None
),
min_stock=e.min_stock,
stock=location_subtree_stock_base(db, product, e.location_id),
stock=(
o.stock
if e.location_id is None
else location_subtree_stock_base(db, product, e.location_id)
),
)
for e in sorted(
product.location_min_stocks, key=lambda x: (x.location_id is not None, x.id)
)
for e in sorted(product.location_min_stocks, key=lambda x: x.id)
]
out.append(o)
return out