From de6c8632a1e888dea617fac566f9b5ca15bdc5da Mon Sep 17 00:00:00 2001 From: Scarriffle Date: Wed, 22 Jul 2026 14:48:14 +0200 Subject: [PATCH] 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 --- backend/app/crud.py | 8 +- backend/app/models.py | 22 ++++++ backend/app/routers/groups.py | 46 ++++++++++- backend/app/routers/products.py | 70 +++++++++++++++-- backend/app/schemas.py | 19 +++++ web/src/api.js | 6 ++ web/src/components/BarcodeList.jsx | 60 +++++++++++++++ web/src/pages/CheckIn.jsx | 13 +++- web/src/pages/Groups.jsx | 118 ++++++++++++++++++++--------- web/src/pages/ProductForm.jsx | 23 ++++++ web/src/pages/Users.jsx | 2 +- web/src/styles.css | 2 + 12 files changed, 341 insertions(+), 48 deletions(-) create mode 100644 web/src/components/BarcodeList.jsx diff --git a/backend/app/crud.py b/backend/app/crud.py index 09cde4c..487d9e6 100644 --- a/backend/app/crud.py +++ b/backend/app/crud.py @@ -7,8 +7,8 @@ from datetime import date from fastapi import HTTPException, status from sqlalchemy.orm import Session -from .models import Lot, Product -from .schemas import ProductOut +from .models import Barcode, Lot, Product +from .schemas import BarcodeOut, ProductOut from .services.conversion import KIND_OF_BASE, display_unit_info from .services.stock import current_stock @@ -26,6 +26,10 @@ def product_to_out(db: Session, product: Product) -> ProductOut: ) .count() ) + out.barcodes = [ + BarcodeOut.model_validate(b) + for b in db.query(Barcode).filter(Barcode.product_id == product.id).order_by(Barcode.id).all() + ] out.kind = KIND_OF_BASE[product.base_unit].value name, factor = display_unit_info(product) out.unit_name = name diff --git a/backend/app/models.py b/backend/app/models.py index 9f277ab..c8f7793 100644 --- a/backend/app/models.py +++ b/backend/app/models.py @@ -146,6 +146,28 @@ class Product(Base): ) +class Barcode(Base): + """Zusätzliche EAN-Codes für ein Produkt ODER eine Gruppe. + + Beispiel Gruppe "Mehl": alle Mehl-Marken einscannen; ein Scan ordnet das + Produkt dann automatisch dieser Gruppe zu. + """ + + __tablename__ = "barcodes" + + id: Mapped[int] = mapped_column(Integer, primary_key=True) + code: Mapped[str] = mapped_column(String(64), unique=True, index=True, nullable=False) + # Freitext zur Einordnung, z.B. "Mehl bei Aldi" + note: Mapped[str | None] = mapped_column(String(120), nullable=True) + product_id: Mapped[int | None] = mapped_column( + ForeignKey("products.id", ondelete="CASCADE"), nullable=True + ) + group_id: Mapped[int | None] = mapped_column( + ForeignKey("groups.id", ondelete="CASCADE"), nullable=True + ) + created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), default=_now) + + class Lot(Base): __tablename__ = "lots" diff --git a/backend/app/routers/groups.py b/backend/app/routers/groups.py index 5e28e44..d961873 100644 --- a/backend/app/routers/groups.py +++ b/backend/app/routers/groups.py @@ -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, diff --git a/backend/app/routers/products.py b/backend/app/routers/products.py index ef990a6..5f70cd5 100644 --- a/backend/app/routers/products.py +++ b/backend/app/routers/products.py @@ -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, diff --git a/backend/app/schemas.py b/backend/app/schemas.py index c7904db..f88b4de 100644 --- a/backend/app/schemas.py +++ b/backend/app/schemas.py @@ -23,6 +23,14 @@ class UnitCreate(BaseModel): factor: float = Field(gt=0) +# ---- Barcodes ---- +class BarcodeOut(BaseModel): + model_config = ConfigDict(from_attributes=True) + id: int + code: str + note: str | None = None + + # ---- Auth / Users ---- class Token(BaseModel): access_token: str @@ -62,6 +70,7 @@ class GroupOut(BaseModel): stock: float = 0.0 # Bestand in Basiseinheiten min_stock_unit_name: str | None = None kind: str | None = None # Art der Mindestbestand-Einheit + barcodes: list[BarcodeOut] = [] # EANs, die dieser Gruppe zugeordnet sind class GroupCreate(BaseModel): @@ -153,12 +162,22 @@ class ProductOut(BaseModel): # Mindestbestand in der erfassten Einheit (für die Anzeige): min_stock_display: float | None = None min_stock_unit_label: str = "" + # Zusätzliche EAN-Codes (neben dem Haupt-Barcode): + barcodes: list[BarcodeOut] = [] class LookupResult(BaseModel): found: bool existing_product: ProductOut | None = None suggestion: dict | None = None + # Ist der Code einer Gruppe zugeordnet (z.B. "Mehl"), wird sie hier gemeldet. + group_id: int | None = None + group_name: str | None = None + + +class BarcodeCreate(BaseModel): + code: str = Field(min_length=4, max_length=64) + note: str | None = Field(default=None, max_length=120) # ---- Stock movements ---- diff --git a/web/src/api.js b/web/src/api.js index fd061cd..04932f6 100644 --- a/web/src/api.js +++ b/web/src/api.js @@ -104,6 +104,9 @@ export const api = { createProduct: (body) => request("/products", { method: "POST", body }), updateProduct: (id, body) => request(`/products/${id}`, { method: "PATCH", body }), deleteProduct: (id) => request(`/products/${id}`, { method: "DELETE" }), + addProductBarcode: (id, body) => request(`/products/${id}/barcodes`, { method: "POST", body }), + deleteProductBarcode: (id, code) => + request(`/products/${id}/barcodes/${encodeURIComponent(code)}`, { method: "DELETE" }), // Bestand checkIn: (body) => request("/stock/checkin", { method: "POST", body }), @@ -129,6 +132,9 @@ export const api = { createGroup: (body) => request("/groups", { method: "POST", body }), updateGroup: (id, body) => request(`/groups/${id}`, { method: "PATCH", body }), deleteGroup: (id) => request(`/groups/${id}`, { method: "DELETE" }), + addGroupBarcode: (id, body) => request(`/groups/${id}/barcodes`, { method: "POST", body }), + deleteGroupBarcode: (id, code) => + request(`/groups/${id}/barcodes/${encodeURIComponent(code)}`, { method: "DELETE" }), // Export / Import exportCsv: () => downloadFile("/export/stock.csv", "bestand.csv"), diff --git a/web/src/components/BarcodeList.jsx b/web/src/components/BarcodeList.jsx new file mode 100644 index 0000000..214e870 --- /dev/null +++ b/web/src/components/BarcodeList.jsx @@ -0,0 +1,60 @@ +import { useState } from "react"; +import Icon from "./Icon"; + +/** + * Liste zusätzlicher EAN-Codes mit Notiz ("Mehl bei Aldi"). + * onAdd({ code, note }) und onDelete(code) werden vom Aufrufer bereitgestellt. + */ +export default function BarcodeList({ barcodes = [], onAdd, onDelete, disabled = false, hint }) { + const [code, setCode] = useState(""); + const [note, setNote] = useState(""); + const [busy, setBusy] = useState(false); + + async function add(e) { + e.preventDefault(); + if (!code.trim()) return; + setBusy(true); + try { + await onAdd({ code: code.trim(), note: note.trim() || null }); + setCode(""); + setNote(""); + } finally { + setBusy(false); + } + } + + return ( +
+
    + {barcodes.map((b) => ( +
  • + + {b.code} + {b.note && {b.note}} + + {!disabled && ( + + )} +
  • + ))} + {barcodes.length === 0 &&
  • Noch keine zusätzlichen Codes.
  • } +
+ + {!disabled && ( +
+ setCode(e.target.value)} /> + setNote(e.target.value)} /> + +
+ )} + {hint &&

{hint}

} +
+ ); +} diff --git a/web/src/pages/CheckIn.jsx b/web/src/pages/CheckIn.jsx index 4e88dcb..1db1d89 100644 --- a/web/src/pages/CheckIn.jsx +++ b/web/src/pages/CheckIn.jsx @@ -13,6 +13,8 @@ export default function CheckIn() { const [barcode, setBarcode] = useState(""); const [product, setProduct] = useState(null); const [suggestion, setSuggestion] = useState(null); + // Gruppe, die diesem Code zugeordnet ist (z.B. alle Mehl-Marken in "Mehl"). + const [suggestionGroup, setSuggestionGroup] = useState({ id: "", name: "" }); const [unknownBarcode, setUnknownBarcode] = useState(null); const [results, setResults] = useState([]); const [search, setSearch] = useState(""); @@ -60,8 +62,10 @@ export default function CheckIn() { setInfo(`Produkt erkannt: ${res.existing_product.name}`); } else if (res.found && res.suggestion) { setSuggestion(res.suggestion); + setSuggestionGroup({ id: res.group_id ? String(res.group_id) : "", name: res.group_name || "" }); } else { setUnknownBarcode(barcode.trim()); + setSuggestionGroup({ id: res.group_id ? String(res.group_id) : "", name: res.group_name || "" }); } } catch (err) { setError(err.message); @@ -71,7 +75,9 @@ export default function CheckIn() { async function createFromSuggestion() { setError(null); setBusy(true); try { - const payload = suggestionToProduct(suggestion, guessGroup(groups, suggestion)); + // Ist der Code einer Gruppe zugeordnet, hat das Vorrang vor dem Kategorie-Rateversuch. + const groupId = suggestionGroup.id || guessGroup(groups, suggestion); + const payload = suggestionToProduct(suggestion, groupId); const created = await api.createProduct(payload); selectProduct(created); setInfo(`Produkt "${created.name}" angelegt.`); @@ -163,7 +169,10 @@ export default function CheckIn() { {suggestion.brand ? `${suggestion.brand} · ` : ""}auf Open Food Facts gefunden {suggestion.quantity_text ? ` · ${suggestion.quantity_text}` : ""} -
Noch nicht im Katalog.
+
+ Noch nicht im Katalog. + {suggestionGroup.name && ` Gruppe: ${suggestionGroup.name}`} +
{isAdmin ? ( + {isAdmin && ( + + )} + + {isAdmin && ( + +

Neue Gruppe

+ - - - -

- Der Gruppen-Bestand summiert nur Produkte, die zur gewählten Einheit passen - (Gewicht/Volumen/Stück). Produkte ordnest du im Produkt-Formular einer Gruppe zu. -

- - )} +
+ + +
+ +

+ Der Gruppen-Bestand summiert nur Produkte, die zur gewählten Einheit passen. + Namen lassen sich in der Tabelle direkt ändern. +

+ + )} + ); diff --git a/web/src/pages/ProductForm.jsx b/web/src/pages/ProductForm.jsx index 1d799ac..d5b2e22 100644 --- a/web/src/pages/ProductForm.jsx +++ b/web/src/pages/ProductForm.jsx @@ -2,6 +2,7 @@ import { useEffect, useState } from "react"; import { useNavigate, useParams, useSearchParams } from "react-router-dom"; import { api } from "../api"; import { useAuth } from "../auth"; +import BarcodeList from "../components/BarcodeList"; import Icon from "../components/Icon"; import { guessGroup } from "../offUtils"; import { daysUntil, expiryRowClass, fmt, isExpired, unitShort } from "../units"; @@ -332,6 +333,28 @@ export default function ProductForm() { {readOnly &&

Nur Administratoren können Produkte bearbeiten.

} + {!isNew && product && ( +
+

Weitere EAN-Codes

+ { + try { + setProduct(await api.addProductBarcode(id, body)); + } catch (err) { setError(err.message); } + }} + onDelete={async (code) => { + try { + await api.deleteProductBarcode(id, code); + setProduct(await api.getProduct(id)); + } catch (err) { setError(err.message); } + }} + /> +
+ )} + {!isNew && ( diff --git a/web/src/styles.css b/web/src/styles.css index f23f272..feff37d 100644 --- a/web/src/styles.css +++ b/web/src/styles.css @@ -160,6 +160,8 @@ td .field-inline { margin-bottom: 0; flex-wrap: wrap; } Zahlenfelder duerfen wachsen, Einheiten-Dropdowns behalten ihre Textbreite. */ .field-inline > input { flex: 1 1 auto; min-width: 78px; } .field-inline > select { flex: 0 0 auto; width: auto; min-width: 132px; } +/* Auswahlfelder in Tabellen richten sich nach ihrem Text statt nach der Zelle. */ +td select { width: auto; min-width: 132px; max-width: 100%; } /* Button-Paare (z.B. Speichern/Abbrechen) gleich breit halten. */ .btn-pair { display: flex; gap: var(--sp-2); flex-wrap: wrap; justify-content: flex-end; } .btn-pair .btn { flex: 1 1 auto; min-width: 104px; justify-content: center; }