Die drei Einheiten-Arten waren bisher strikt getrennt: BASE_OF_KIND bildet count/weight/volume 1:1 auf Stueck/Gramm/Milliliter ab, ohne jeden Faktor dazwischen. Zwei Stellen setzten das durch - to_base lehnte artfremde Einheiten beim Ein-/Auslagern ab, und group_min_context filterte stueckweise gefuehrte Artikel aus einer Kilogramm-Gruppe stillschweigend heraus. Letzteres war der Anlass: eine Gruppe "Wurst" in kg sah Bratwuerste in Stueck gar nicht. Ein Artikel darf jetzt eine Zweiteinheit tragen: "3 Stueck ≙ 250 g". Gespeichert wird das eingegebene PAAR, nicht der Faktor - wer 3 und 250 eintippt, sieht beim naechsten Oeffnen genau das wieder. Das hat auch einen rechnerischen Grund: 250 * 3 / 250 ist exakt 3, der Umweg ueber 250/3 ergibt 3,0000000000000004 und liefe damit gegen die Bestandspruefung beim Auslagern. Der Artikel bleibt in seiner Basiseinheit gefuehrt; die Bruecke ist reine Rechnung. Gruppen zaehlen artfremde Artikel jetzt mit ihrem Faktor mit (GroupMinContext.faktoren), Bestandssummen laufen dafuer je Artikel gewichtet - weiterhin zwei Abfragen, nur mit GROUP BY. Ein-/Auslagern in der Fremdeinheit geht, krumme Mengen werden bewusst gebucht statt gerundet: 100 g sind 1,2 Stueck, und Runden wuerde stumm etwas anderes buchen als angegeben. WICHTIGE KORREKTUR am urspruenglichen Plan: die Teilmengen-Bedingung in _gruppen_bedarfe konnte NICHT bleiben. Sie war bisher zugleich ein Einheiten-Schutz, weil Artikel verschiedener Arten zwangslaeufig disjunkt waren. Mit der Bruecke gilt sie ploetzlich auch zwischen einer Stueck- und einer Gramm-Gruppe - und _netted_topups haette einen Bedarf in Stueck von einem in Gramm abgezogen. Jetzt wird nur noch zwischen Gruppen derselben Basiseinheit verrechnet. Open Food Facts: "3 x 80 g" verlor bisher den Multiplikator, weil der Regex den ersten Zahl-Einheit-Treffer nahm. parse_gebinde liefert jetzt Gesamtmenge UND Stueckzahl und belegt die Zweiteinheit vor; parse_quantity behaelt seinen schmalen Vertrag. 18 neue Tests. Dass test_wrong_kind_rejected und test_einheitenfilter_gilt_auch_fuer_untergruppen unveraendert gruen bleiben, ist selbst der Beleg: ohne Bruecke aendert sich nichts. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
389 lines
15 KiB
Python
389 lines
15 KiB
Python
from fastapi import APIRouter, Depends, HTTPException, status
|
||
from sqlalchemy.orm import Session
|
||
|
||
from ..database import get_db
|
||
from ..deps import get_current_user, require_admin
|
||
from ..models import Barcode, Group, GroupLocationMinStock, Location, Product, User
|
||
from ..schemas import (
|
||
BarcodeCreate,
|
||
BarcodeNoteUpdate,
|
||
BarcodeOut,
|
||
GroupCreate,
|
||
GroupOut,
|
||
GroupUpdate,
|
||
LocationMinStockIn,
|
||
LocationMinStockOut,
|
||
ProductBarcodeOut,
|
||
)
|
||
from ..services import gruppen as gruppen_graph
|
||
from ..services.conversion import group_min_context
|
||
from ..services.min_stock import UEBERALL_NAME, lies_ueberall, schreibe_ueberall
|
||
from ..services.stock import (
|
||
summe_bestand_gewichtet,
|
||
summe_bestand_im_subtree_gewichtet,
|
||
)
|
||
|
||
router = APIRouter(prefix="/groups", tags=["groups"])
|
||
|
||
|
||
def _obergruppen_setzen(db: Session, group: Group, parent_ids: list[int]) -> None:
|
||
"""Obergruppen einer Gruppe ersetzen – mit Ringschutz.
|
||
|
||
Ohne die Pruefung entstuende ein Ring, und jede Bestands- oder
|
||
Bedarfsrechnung liefe im Kreis. Gleiche Absicherung wie beim Umhaengen einer
|
||
Kategorie (routers/categories.py), nur ueber eine Nachfahren-MENGE, weil
|
||
Gruppen ein Graph und kein Baum sind.
|
||
"""
|
||
# Die Nachfahren aendern sich durch das Setzen von OBERgruppen nicht –
|
||
# deshalb einmal vor der Schleife bestimmen.
|
||
verboten = gruppen_graph.nachfahren_ids(group) | {group.id}
|
||
gewuenscht: list[Group] = []
|
||
for pid in dict.fromkeys(parent_ids): # Reihenfolge halten, Dubletten raus
|
||
if pid in verboten:
|
||
raise HTTPException(
|
||
status.HTTP_409_CONFLICT,
|
||
"Eine Gruppe kann nicht sich selbst oder einer ihrer "
|
||
"Untergruppen untergeordnet werden.",
|
||
)
|
||
eltern = db.get(Group, pid)
|
||
if eltern is None:
|
||
raise HTTPException(status.HTTP_404_NOT_FOUND, "Obergruppe nicht gefunden")
|
||
gewuenscht.append(eltern)
|
||
group.parents = gewuenscht
|
||
|
||
|
||
def _group_to_out(db: Session, group: Group) -> GroupOut:
|
||
out = GroupOut.model_validate(group)
|
||
# ``min_stock`` bleibt nach aussen in der Erfassungseinheit der Gruppe,
|
||
# gespeichert ist die „Ueberall"-Zeile aber in Basiseinheiten.
|
||
ueberall = lies_ueberall(group.location_min_stocks)
|
||
products = db.query(Product).filter(Product.group_id == group.id).all()
|
||
out.parent_ids = [p.id for p in group.parents]
|
||
out.child_ids = sorted(c.id for c in group.children)
|
||
out.direct_product_count = len(products)
|
||
out.product_count = len(gruppen_graph.produkte(group))
|
||
# Zu welchem Artikel gehoert ein Code? Der Gruppen-Code entsteht beim
|
||
# Zuordnen automatisch; die Herkunft soll trotzdem sichtbar bleiben.
|
||
artikel_zu_code: dict[str, Product] = {}
|
||
for product in products:
|
||
if product.barcode:
|
||
artikel_zu_code[product.barcode] = product
|
||
for alias in db.query(Barcode).filter(Barcode.product_id == product.id):
|
||
artikel_zu_code[alias.code] = product
|
||
|
||
out.barcodes = []
|
||
for b in db.query(Barcode).filter(Barcode.group_id == group.id).order_by(Barcode.id).all():
|
||
eintrag = BarcodeOut.model_validate(b)
|
||
besitzer = artikel_zu_code.get(b.code)
|
||
if besitzer is not None:
|
||
eintrag.product_name = besitzer.name
|
||
eintrag.product_brand = besitzer.brand
|
||
out.barcodes.append(eintrag)
|
||
# Codes der Artikel in dieser Gruppe mitliefern. Sie waren bisher nirgends
|
||
# sichtbar, wodurch eine Gruppe mit Artikeln "0 EANs" anzeigte.
|
||
# Was schon als Gruppen-Code gefuehrt wird, hier ueberspringen - sonst
|
||
# steht derselbe Code zweimal in der Liste, einmal davon schreibgeschuetzt.
|
||
schon_da = {b.code for b in out.barcodes}
|
||
product_codes: list[ProductBarcodeOut] = []
|
||
for product in products:
|
||
if product.barcode and product.barcode not in schon_da:
|
||
product_codes.append(
|
||
ProductBarcodeOut(
|
||
code=product.barcode, product_id=product.id,
|
||
product_name=product.name, product_brand=product.brand,
|
||
)
|
||
)
|
||
for alias in db.query(Barcode).filter(Barcode.product_id == product.id).order_by(Barcode.id):
|
||
if alias.code in schon_da:
|
||
continue
|
||
product_codes.append(
|
||
ProductBarcodeOut(
|
||
code=alias.code, product_id=product.id,
|
||
product_name=product.name, product_brand=product.brand,
|
||
)
|
||
)
|
||
out.product_barcodes = product_codes
|
||
|
||
# Bestand in derselben Einheit, in der auch der Mindestbestand erfasst ist
|
||
# (Gruppen-Gebinde ODER verwaltete Einheit) – so ist beides vergleichbar.
|
||
ctx = group_min_context(group)
|
||
# Gewichtet: artfremde Artikel zaehlen ueber ihre Zweiteinheit mit.
|
||
stock_base = summe_bestand_gewichtet(db, ctx.matching, ctx.faktoren)
|
||
out.stock = round(stock_base / ctx.divisor, 3)
|
||
out.min_stock = None if ueberall is None else round(ueberall / ctx.divisor, 3)
|
||
unit = group.min_stock_unit
|
||
if unit is not None:
|
||
out.min_stock_unit_name = unit.name
|
||
out.min_stock_unit_factor = unit.factor
|
||
out.kind = unit.kind.value
|
||
|
||
# Bestand je Lagerort (inkl. Unterorte) in derselben Einheit wie out.stock.
|
||
# Bestand je Lagerort in BASISEINHEITEN – wie ``min_stock`` in denselben
|
||
# Zeilen. Nur ``out.stock`` oben rechnet in der Erfassungseinheit.
|
||
def _loc_stock(loc_id: str | None) -> float:
|
||
# „Ueberall" (Ort NULL) zaehlt den Gesamtbestand – inkl. Chargen, die
|
||
# (noch) an keinem Lagerort liegen.
|
||
total = (
|
||
stock_base
|
||
if loc_id is None
|
||
else summe_bestand_im_subtree_gewichtet(db, ctx.matching, loc_id, ctx.faktoren)
|
||
)
|
||
return round(total, 3)
|
||
|
||
out.location_min_stocks = [
|
||
LocationMinStockOut(
|
||
location_id=e.location_id,
|
||
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=_loc_stock(e.location_id),
|
||
)
|
||
# „Ueberall" zuerst, danach nach Anlagereihenfolge.
|
||
for e in sorted(group.location_min_stocks, key=lambda x: (x.location_id is not None, x.id))
|
||
]
|
||
return out
|
||
|
||
|
||
@router.get("", response_model=list[GroupOut])
|
||
def list_groups(
|
||
db: Session = Depends(get_db), _: User = Depends(get_current_user)
|
||
) -> list[GroupOut]:
|
||
groups = db.query(Group).order_by(Group.name).all()
|
||
return [_group_to_out(db, g) for g in groups]
|
||
|
||
|
||
@router.put("/{group_id}/location-min-stock", response_model=GroupOut)
|
||
def set_group_location_min_stock(
|
||
group_id: int,
|
||
payload: list[LocationMinStockIn],
|
||
db: Session = Depends(get_db),
|
||
_: User = Depends(require_admin),
|
||
) -> GroupOut:
|
||
"""Gruppen-Mindestbestände je Lagerort ersetzen (Menge 0 = Eintrag entfällt).
|
||
|
||
``location_id = null`` ist „Überall" und damit ein Ort wie jeder andere –
|
||
er ersetzt den frueheren Gesamt-Mindestbestand. Mengen in Basiseinheiten.
|
||
"""
|
||
group = db.get(Group, group_id)
|
||
if group is None:
|
||
raise HTTPException(status.HTTP_404_NOT_FOUND, "Gruppe nicht gefunden")
|
||
|
||
db.query(GroupLocationMinStock).filter(
|
||
GroupLocationMinStock.group_id == group_id
|
||
).delete()
|
||
# None (= Überall) ist ein eigener Schluessel in der Dublettenpruefung.
|
||
gesehen: set[str | None] = set()
|
||
for eintrag in payload:
|
||
if eintrag.min_stock <= 0 or eintrag.location_id in gesehen:
|
||
continue
|
||
if eintrag.location_id is not None and db.get(Location, eintrag.location_id) is None:
|
||
raise HTTPException(status.HTTP_404_NOT_FOUND, "Lagerort nicht gefunden")
|
||
gesehen.add(eintrag.location_id)
|
||
db.add(GroupLocationMinStock(
|
||
group_id=group_id,
|
||
location_id=eintrag.location_id,
|
||
min_stock=eintrag.min_stock,
|
||
))
|
||
db.commit()
|
||
db.refresh(group)
|
||
return _group_to_out(db, group)
|
||
|
||
|
||
@router.post("", response_model=GroupOut, status_code=status.HTTP_201_CREATED)
|
||
def create_group(
|
||
payload: GroupCreate,
|
||
db: Session = Depends(get_db),
|
||
_: User = Depends(require_admin),
|
||
) -> GroupOut:
|
||
if db.query(Group).filter(Group.name == payload.name).first():
|
||
raise HTTPException(status.HTTP_409_CONFLICT, "Gruppe existiert bereits")
|
||
group = Group(
|
||
name=payload.name,
|
||
min_stock_unit_id=payload.min_stock_unit_id,
|
||
package_size=payload.package_size,
|
||
package_label=payload.package_label,
|
||
min_stock_in_packages=payload.min_stock_in_packages,
|
||
)
|
||
db.add(group)
|
||
# Erst flushen: die Ringpruefung in _obergruppen_setzen braucht group.id.
|
||
db.flush()
|
||
_obergruppen_setzen(db, group, payload.parent_ids)
|
||
# ``min_stock`` kommt in der Erfassungseinheit der Gruppe, gespeichert wird
|
||
# in Basiseinheiten – deshalb ueber den Divisor.
|
||
if payload.min_stock is not None:
|
||
schreibe_ueberall(
|
||
db, group, payload.min_stock * (group_min_context(group).divisor or 1.0)
|
||
)
|
||
db.commit()
|
||
db.refresh(group)
|
||
return _group_to_out(db, group)
|
||
|
||
|
||
@router.patch("/{group_id}", response_model=GroupOut)
|
||
def update_group(
|
||
group_id: int,
|
||
payload: GroupUpdate,
|
||
db: Session = Depends(get_db),
|
||
_: User = Depends(require_admin),
|
||
) -> GroupOut:
|
||
group = db.get(Group, group_id)
|
||
if group is None:
|
||
raise HTTPException(status.HTTP_404_NOT_FOUND, "Gruppe nicht gefunden")
|
||
data = payload.model_dump(exclude_unset=True)
|
||
# Muss RAUS, bevor unten stumpf jedes Feld per setattr gesetzt wird –
|
||
# ``parent_ids`` ist kein Modellattribut, sondern eine Beziehung.
|
||
obergruppen = data.pop("parent_ids", None)
|
||
# Ebenfalls kein Modellfeld mehr: der Mindestbestand ist die „Ueberall"-Zeile.
|
||
min_gesetzt = "min_stock" in data
|
||
min_wert = data.pop("min_stock", None)
|
||
if "name" in data and data["name"]:
|
||
clash = (
|
||
db.query(Group)
|
||
.filter(Group.name == data["name"], Group.id != group_id)
|
||
.first()
|
||
)
|
||
if clash:
|
||
raise HTTPException(status.HTTP_409_CONFLICT, "Name bereits vergeben")
|
||
|
||
for field, value in data.items():
|
||
setattr(group, field, value)
|
||
if obergruppen is not None:
|
||
_obergruppen_setzen(db, group, obergruppen)
|
||
# Die Erfassungseinheit umzustellen (Gebinde <-> verwaltete Einheit) braucht
|
||
# keine Umrechnung mehr: gespeichert wird in Basiseinheiten, der physische
|
||
# Bedarf bleibt dadurch von selbst gleich.
|
||
if min_gesetzt:
|
||
divisor = group_min_context(group).divisor or 1.0
|
||
schreibe_ueberall(db, group, None if min_wert is None else min_wert * divisor)
|
||
db.commit()
|
||
db.refresh(group)
|
||
return _group_to_out(db, group)
|
||
|
||
|
||
def _describe_conflict(db: Session, code: str, group_id: int) -> str | None:
|
||
"""Sagt, wem ein Code schon gehört – "bereits vergeben" allein half nicht weiter."""
|
||
product = db.query(Product).filter(Product.barcode == code).first()
|
||
existing = db.query(Barcode).filter(Barcode.code == code).first()
|
||
|
||
if existing is not None and existing.product_id:
|
||
product = db.get(Product, existing.product_id) or product
|
||
|
||
if product is not None:
|
||
if product.group_id == group_id:
|
||
# Der Code steht durch den Artikel ohnehin schon in dieser Gruppe.
|
||
return (
|
||
f"Dieser Code gehört zum Artikel \"{product.name}\" und ist über ihn "
|
||
"bereits in dieser Gruppe."
|
||
)
|
||
if product.group_id is not None:
|
||
other = db.get(Group, product.group_id)
|
||
name = other.name if other else "einer anderen Gruppe"
|
||
return (
|
||
f"Dieser Code gehört zum Artikel \"{product.name}\", der in der Gruppe "
|
||
f"\"{name}\" liegt. Ändere die Gruppe des Artikels, statt den Code hier "
|
||
"einzutragen."
|
||
)
|
||
return (
|
||
f"Dieser Code gehört bereits zum Artikel \"{product.name}\". Ordne den "
|
||
"Artikel dieser Gruppe zu – sein Code erscheint dann automatisch hier."
|
||
)
|
||
|
||
if existing is not None and existing.group_id == group_id:
|
||
return "Dieser Code ist in dieser Gruppe bereits hinterlegt."
|
||
if existing is not None and existing.group_id:
|
||
other = db.get(Group, existing.group_id)
|
||
name = other.name if other else "einer anderen Gruppe"
|
||
return f"Dieser Code ist bereits der Gruppe \"{name}\" zugeordnet."
|
||
if existing is not None:
|
||
return "Dieser Code ist bereits vergeben."
|
||
return None
|
||
|
||
|
||
@router.post("/{group_id}/barcodes", response_model=GroupOut, status_code=status.HTTP_201_CREATED)
|
||
def add_group_barcode(
|
||
group_id: int,
|
||
payload: BarcodeCreate,
|
||
db: Session = Depends(get_db),
|
||
_: User = Depends(require_admin),
|
||
) -> GroupOut:
|
||
"""EAN einer Gruppe zuordnen (z.B. alle Mehl-Marken zur Gruppe "Mehl")."""
|
||
group = db.get(Group, group_id)
|
||
if group is None:
|
||
raise HTTPException(status.HTTP_404_NOT_FOUND, "Gruppe nicht gefunden")
|
||
code = payload.code.strip()
|
||
conflict = _describe_conflict(db, code, group.id)
|
||
if conflict:
|
||
raise HTTPException(status.HTTP_409_CONFLICT, conflict)
|
||
db.add(Barcode(code=code, note=(payload.note or None), group_id=group.id))
|
||
db.commit()
|
||
db.refresh(group)
|
||
return _group_to_out(db, group)
|
||
|
||
|
||
@router.patch("/{group_id}/barcodes/{code}", response_model=GroupOut)
|
||
def update_group_barcode_note(
|
||
group_id: int,
|
||
code: str,
|
||
payload: BarcodeNoteUpdate,
|
||
db: Session = Depends(get_db),
|
||
_: User = Depends(require_admin),
|
||
) -> GroupOut:
|
||
"""Notiz zu einem Gruppen-Code setzen.
|
||
|
||
Betrifft ausdrücklich auch Codes, die beim Zuordnen eines Artikels
|
||
automatisch entstanden sind – die hatten bisher gar keine Möglichkeit,
|
||
eine Notiz zu bekommen.
|
||
"""
|
||
group = db.get(Group, group_id)
|
||
if group is None:
|
||
raise HTTPException(status.HTTP_404_NOT_FOUND, "Gruppe nicht gefunden")
|
||
entry = (
|
||
db.query(Barcode).filter(Barcode.group_id == group_id, Barcode.code == code).first()
|
||
)
|
||
if entry is None:
|
||
raise HTTPException(status.HTTP_404_NOT_FOUND, "Code nicht gefunden")
|
||
|
||
note = (payload.note or "").strip()
|
||
entry.note = note or None
|
||
db.commit()
|
||
db.refresh(group)
|
||
return _group_to_out(db, group)
|
||
|
||
|
||
@router.delete("/{group_id}/barcodes/{code}", status_code=status.HTTP_204_NO_CONTENT)
|
||
def delete_group_barcode(
|
||
group_id: int,
|
||
code: str,
|
||
db: Session = Depends(get_db),
|
||
_: User = Depends(require_admin),
|
||
) -> None:
|
||
entry = (
|
||
db.query(Barcode).filter(Barcode.group_id == group_id, Barcode.code == code).first()
|
||
)
|
||
if entry is None:
|
||
raise HTTPException(status.HTTP_404_NOT_FOUND, "Code nicht gefunden")
|
||
db.delete(entry)
|
||
db.commit()
|
||
|
||
|
||
@router.delete("/{group_id}", status_code=status.HTTP_204_NO_CONTENT)
|
||
def delete_group(
|
||
group_id: int,
|
||
db: Session = Depends(get_db),
|
||
_: User = Depends(require_admin),
|
||
) -> None:
|
||
group = db.get(Group, group_id)
|
||
if group is None:
|
||
raise HTTPException(status.HTTP_404_NOT_FOUND, "Gruppe nicht gefunden")
|
||
# Untergruppen bleiben bestehen und ruecken NICHT nach oben: im Graphen
|
||
# waere unklar, an welchen der moeglicherweise mehreren Grosseltern sie
|
||
# sollten – ein automatisches Umhaengen wuerde stillschweigend neue
|
||
# Bestandssummen erzeugen. Sie verlieren nur die Verbindung. Kanten
|
||
# ausdruecklich loesen, weil SQLite Fremdschluessel nicht erzwingt.
|
||
group.parents.clear()
|
||
group.children.clear()
|
||
db.flush()
|
||
db.delete(group)
|
||
db.commit()
|