Mehrere EAN-Codes je Produkt und je Gruppe (mit Notiz) + Gruppen umbenennen
Barcodes:
- Neue Tabelle barcodes (code, note, product_id ODER group_id). Damit lassen sich
einem Produkt mehrere Codes geben und einer Gruppe beliebig viele Marken
zuordnen (z.B. alle Mehlmarken zu "Mehl"). Je Code eine Notiz wie
"Mehl bei Aldi".
- Lookup loest jetzt auch Alias-Codes auf; ist ein Code einer Gruppe zugeordnet,
liefert die API group_id/group_name zurueck. Beim Anlegen aus einem Scan wird
diese Gruppe automatisch gesetzt (Vorrang vor dem Kategorie-Rateversuch).
- Endpunkte: POST/DELETE /products/{id}/barcodes und /groups/{id}/barcodes.
- Web: wiederverwendbare BarcodeList-Komponente, eingebunden im Produktformular
und auf der Gruppen-Seite.
Gruppen:
- Namen lassen sich jetzt direkt in der Tabelle aendern (PATCH war vorhanden,
nur die Oberflaeche fehlte).
UI-Fix: Auswahlfelder in Tabellen richten sich nach ihrem Text statt nach der
Zellenbreite (Gruppen-/Benutzer-Dropdowns waren abgeschnitten).
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -3,8 +3,8 @@ from sqlalchemy.orm import Session
|
||||
|
||||
from ..database import get_db
|
||||
from ..deps import get_current_user, require_admin
|
||||
from ..models import Group, Product, User
|
||||
from ..schemas import GroupCreate, GroupOut, GroupUpdate
|
||||
from ..models import Barcode, Group, Product, User
|
||||
from ..schemas import BarcodeCreate, BarcodeOut, GroupCreate, GroupOut, GroupUpdate
|
||||
from ..services.conversion import BASE_OF_KIND
|
||||
from ..services.stock import current_stock
|
||||
|
||||
@@ -15,6 +15,10 @@ def _group_to_out(db: Session, group: Group) -> GroupOut:
|
||||
out = GroupOut.model_validate(group)
|
||||
products = db.query(Product).filter(Product.group_id == group.id).all()
|
||||
out.product_count = len(products)
|
||||
out.barcodes = [
|
||||
BarcodeOut.model_validate(b)
|
||||
for b in db.query(Barcode).filter(Barcode.group_id == group.id).order_by(Barcode.id).all()
|
||||
]
|
||||
unit = group.min_stock_unit
|
||||
if unit is not None:
|
||||
# Bestand nur über Produkte gleicher Art, in der Gruppen-Einheit ausgedrückt.
|
||||
@@ -82,6 +86,44 @@ def update_group(
|
||||
return _group_to_out(db, group)
|
||||
|
||||
|
||||
@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()
|
||||
if db.query(Barcode).filter(Barcode.code == code).first() or (
|
||||
db.query(Product).filter(Product.barcode == code).first()
|
||||
):
|
||||
raise HTTPException(status.HTTP_409_CONFLICT, "Dieser Code ist bereits vergeben")
|
||||
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.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,
|
||||
|
||||
@@ -4,9 +4,9 @@ from sqlalchemy.orm import Session
|
||||
from ..crud import product_to_out
|
||||
from ..database import get_db
|
||||
from ..deps import get_current_user, require_admin
|
||||
from ..models import BaseUnit, Product, User
|
||||
from ..models import Barcode, BaseUnit, Group, Product, User
|
||||
from ..off import lookup_barcode
|
||||
from ..schemas import LookupResult, ProductCreate, ProductOut, ProductUpdate
|
||||
from ..schemas import BarcodeCreate, LookupResult, ProductCreate, ProductOut, ProductUpdate
|
||||
from ..services.conversion import ConversionError, resolve_product_unit
|
||||
|
||||
router = APIRouter(prefix="/products", tags=["products"])
|
||||
@@ -32,15 +32,31 @@ def lookup(
|
||||
db: Session = Depends(get_db),
|
||||
_: User = Depends(get_current_user),
|
||||
) -> LookupResult:
|
||||
"""Barcode: erst lokal, dann Open Food Facts. Nur Vorschlag, legt nichts an."""
|
||||
existing = db.query(Product).filter(Product.barcode == barcode).first()
|
||||
"""Barcode auflösen: Produkt (Haupt- oder Alias-Code), Gruppen-Code, sonst OFF."""
|
||||
code = barcode.strip()
|
||||
|
||||
existing = db.query(Product).filter(Product.barcode == code).first()
|
||||
if existing is None:
|
||||
alias = db.query(Barcode).filter(Barcode.code == code).first()
|
||||
if alias is not None and alias.product_id:
|
||||
existing = db.get(Product, alias.product_id)
|
||||
if existing:
|
||||
return LookupResult(found=True, existing_product=product_to_out(db, existing))
|
||||
|
||||
suggestion = lookup_barcode(barcode)
|
||||
# Code kann einer Gruppe zugeordnet sein (z.B. alle Mehl-Marken in "Mehl").
|
||||
group_id = group_name = None
|
||||
alias = db.query(Barcode).filter(Barcode.code == code).first()
|
||||
if alias is not None and alias.group_id:
|
||||
group = db.get(Group, alias.group_id)
|
||||
if group is not None:
|
||||
group_id, group_name = group.id, group.name
|
||||
|
||||
suggestion = lookup_barcode(code)
|
||||
if suggestion:
|
||||
return LookupResult(found=True, suggestion=suggestion)
|
||||
return LookupResult(found=False)
|
||||
return LookupResult(
|
||||
found=True, suggestion=suggestion, group_id=group_id, group_name=group_name
|
||||
)
|
||||
return LookupResult(found=False, group_id=group_id, group_name=group_name)
|
||||
|
||||
|
||||
@router.get("/{product_id}", response_model=ProductOut)
|
||||
@@ -137,6 +153,46 @@ def update_product(
|
||||
return product_to_out(db, product)
|
||||
|
||||
|
||||
@router.post("/{product_id}/barcodes", response_model=ProductOut, status_code=status.HTTP_201_CREATED)
|
||||
def add_product_barcode(
|
||||
product_id: int,
|
||||
payload: BarcodeCreate,
|
||||
db: Session = Depends(get_db),
|
||||
_: User = Depends(require_admin),
|
||||
) -> ProductOut:
|
||||
"""Weiteren EAN-Code zu einem Produkt hinzufügen."""
|
||||
product = db.get(Product, product_id)
|
||||
if product is None:
|
||||
raise HTTPException(status.HTTP_404_NOT_FOUND, "Produkt nicht gefunden")
|
||||
code = payload.code.strip()
|
||||
if db.query(Barcode).filter(Barcode.code == code).first() or (
|
||||
db.query(Product).filter(Product.barcode == code).first()
|
||||
):
|
||||
raise HTTPException(status.HTTP_409_CONFLICT, "Dieser Code ist bereits vergeben")
|
||||
db.add(Barcode(code=code, note=(payload.note or None), product_id=product.id))
|
||||
db.commit()
|
||||
db.refresh(product)
|
||||
return product_to_out(db, product)
|
||||
|
||||
|
||||
@router.delete("/{product_id}/barcodes/{code}", status_code=status.HTTP_204_NO_CONTENT)
|
||||
def delete_product_barcode(
|
||||
product_id: int,
|
||||
code: str,
|
||||
db: Session = Depends(get_db),
|
||||
_: User = Depends(require_admin),
|
||||
) -> None:
|
||||
entry = (
|
||||
db.query(Barcode)
|
||||
.filter(Barcode.product_id == product_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("/{product_id}", status_code=status.HTTP_204_NO_CONTENT)
|
||||
def delete_product(
|
||||
product_id: int,
|
||||
|
||||
Reference in New Issue
Block a user