iOS (neu, ios/):
- SwiftUI-App: Login (Server + Keychain-Token), Kamera-Scanner (EAN/UPC/Code128),
Einlagern mit mehreren Chargen und eigenem MHD, Auslagern mit Chargenauswahl
oder FEFO, Artikelsuche, Anlegen mit Open-Food-Facts-Vorbefuellung.
- Home-Screen-Shortcuts: Schnellaktionen (langer Druck) und URL-Schema
projectgood://checkin | ://checkout fuer eigene Symbole via Kurzbefehle.
- project.yml (XcodeGen) + README mit Build-Anleitung. NICHT kompiliert - auf
diesem Rechner ist kein Xcode vorhanden.
Import-Modi (wie besprochen sinnvoll):
- add (Standard, nichts loeschen), replace_listed (Bestaende der in der Datei
genannten Produkte ersetzen - fuer Inventur), replace_all (alles ersetzen).
Geleerte Bestaende werden als Korrektur-Bewegung protokolliert; die Oberflaeche
fragt bei den zerstoerenden Modi nach.
Anzeige:
- "Bald ablaufend"/"Abgelaufen" zeigen jetzt das Gebinde ("1 Glas") mit der
Basiseinheit klein darunter (ExpiringItem liefert die Einheiten mit).
- MHD als Zeitspanne mit korrektem Numerus: "in 3 Tagen", "in 1 Woche",
"vor 2 Wochen", dazu heute/morgen/gestern.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
501 lines
18 KiB
Python
501 lines
18 KiB
Python
"""Export und Import von Beständen (CSV für Tabellen, JSON für Backups).
|
||
|
||
Der Import arbeitet ausschließlich additiv: Unbekanntes wird angelegt, Chargen
|
||
werden ergänzt – es wird nie etwas gelöscht oder überschrieben.
|
||
"""
|
||
|
||
from __future__ import annotations
|
||
|
||
import csv
|
||
import io
|
||
import json
|
||
from datetime import date, datetime, timezone
|
||
|
||
from fastapi import APIRouter, Depends, File, HTTPException, UploadFile, status
|
||
from fastapi.responses import Response
|
||
from sqlalchemy.orm import Session
|
||
|
||
from ..database import get_db
|
||
from ..deps import get_current_user, require_admin
|
||
from ..models import (
|
||
BaseUnit,
|
||
Group,
|
||
Location,
|
||
Lot,
|
||
Movement,
|
||
MovementType,
|
||
Product,
|
||
Unit,
|
||
UnitKind,
|
||
User,
|
||
)
|
||
from ..services.conversion import BASE_OF_KIND, display_unit_info, find_unit
|
||
|
||
router = APIRouter(tags=["transfer"])
|
||
|
||
CSV_FIELDS = [
|
||
"barcode", "name", "marke", "einheit", "packungsgroesse", "gebinde",
|
||
"gruppe", "mindestbestand", "menge", "menge_einheit", "mhd", "lagerort",
|
||
]
|
||
PACKAGE_TOKENS = {"packung", "package", "pkg", "pack"}
|
||
|
||
|
||
# --------------------------------------------------------------------------
|
||
# Export
|
||
# --------------------------------------------------------------------------
|
||
def _article_unit(product: Product) -> tuple[float, str]:
|
||
"""Faktor und Bezeichnung der Artikeleinheit (Gebinde, sonst Produkteinheit)."""
|
||
unit_name, unit_factor = display_unit_info(product)
|
||
if product.package_size and product.package_size > 0:
|
||
return product.package_size, (product.package_label or "Packung")
|
||
return unit_factor, unit_name
|
||
|
||
|
||
@router.get("/export/stock.csv")
|
||
def export_stock_csv(
|
||
db: Session = Depends(get_db), _: User = Depends(get_current_user)
|
||
) -> Response:
|
||
"""Eine Zeile je Charge – direkt in Excel/LibreOffice bearbeitbar."""
|
||
buf = io.StringIO()
|
||
writer = csv.writer(buf, delimiter=";", lineterminator="\r\n")
|
||
writer.writerow(CSV_FIELDS)
|
||
|
||
for product in db.query(Product).order_by(Product.name).all():
|
||
unit_name, _ = display_unit_info(product)
|
||
factor, amount_label = _article_unit(product)
|
||
base = [
|
||
product.barcode or "",
|
||
product.name,
|
||
product.brand or "",
|
||
unit_name,
|
||
product.package_size if product.package_size else "",
|
||
product.package_label or "",
|
||
product.group.name if product.group else "",
|
||
product.min_stock if product.min_stock is not None else "",
|
||
]
|
||
lots = (
|
||
db.query(Lot)
|
||
.filter(Lot.product_id == product.id)
|
||
.order_by(Lot.best_before.is_(None), Lot.best_before)
|
||
.all()
|
||
)
|
||
if not lots:
|
||
# Produkt ohne Bestand: Stammdaten trotzdem mitnehmen.
|
||
writer.writerow(base + ["", "", "", ""])
|
||
continue
|
||
for lot in lots:
|
||
location = db.get(Location, lot.location_id) if lot.location_id else None
|
||
writer.writerow(
|
||
base
|
||
+ [
|
||
round(lot.quantity / factor, 6),
|
||
amount_label,
|
||
lot.best_before.isoformat() if lot.best_before else "",
|
||
location.name if location else "",
|
||
]
|
||
)
|
||
|
||
# BOM, damit Excel die Umlaute korrekt erkennt.
|
||
return Response(
|
||
content="" + buf.getvalue(),
|
||
media_type="text/csv; charset=utf-8",
|
||
headers={"Content-Disposition": 'attachment; filename="bestand.csv"'},
|
||
)
|
||
|
||
|
||
@router.get("/export/backup.json")
|
||
def export_backup_json(
|
||
db: Session = Depends(get_db), _: User = Depends(get_current_user)
|
||
) -> Response:
|
||
"""Vollständiges Backup: Einheiten, Gruppen, Lagerorte, Produkte, Chargen."""
|
||
locations = db.query(Location).order_by(Location.id).all()
|
||
loc_name = {loc.id: loc.name for loc in locations}
|
||
|
||
data = {
|
||
"version": 1,
|
||
"exported_at": datetime.now(timezone.utc).isoformat(),
|
||
"units": [
|
||
{"name": u.name, "kind": u.kind.value, "factor": u.factor}
|
||
for u in db.query(Unit).order_by(Unit.id).all()
|
||
],
|
||
"groups": [
|
||
{
|
||
"name": g.name,
|
||
"min_stock": g.min_stock,
|
||
"min_stock_unit": g.min_stock_unit.name if g.min_stock_unit else None,
|
||
}
|
||
for g in db.query(Group).order_by(Group.id).all()
|
||
],
|
||
"locations": [
|
||
{"name": loc.name, "parent": loc_name.get(loc.parent_id)}
|
||
for loc in locations
|
||
],
|
||
"products": [],
|
||
}
|
||
|
||
for p in db.query(Product).order_by(Product.id).all():
|
||
unit_name, _ = display_unit_info(p)
|
||
lots = db.query(Lot).filter(Lot.product_id == p.id).all()
|
||
data["products"].append(
|
||
{
|
||
"barcode": p.barcode,
|
||
"name": p.name,
|
||
"brand": p.brand,
|
||
"image_url": p.image_url,
|
||
"unit": unit_name,
|
||
"package_size": p.package_size,
|
||
"package_label": p.package_label,
|
||
"group": p.group.name if p.group else None,
|
||
"min_stock": p.min_stock,
|
||
"min_stock_unit": p.min_stock_unit.name if p.min_stock_unit else None,
|
||
"min_stock_in_packages": bool(p.min_stock_in_packages),
|
||
"lots": [
|
||
{
|
||
"quantity": lot.quantity,
|
||
"best_before": lot.best_before.isoformat() if lot.best_before else None,
|
||
"location": loc_name.get(lot.location_id),
|
||
}
|
||
for lot in lots
|
||
],
|
||
}
|
||
)
|
||
|
||
return Response(
|
||
content=json.dumps(data, ensure_ascii=False, indent=2),
|
||
media_type="application/json; charset=utf-8",
|
||
headers={"Content-Disposition": 'attachment; filename="project-good-backup.json"'},
|
||
)
|
||
|
||
|
||
# --------------------------------------------------------------------------
|
||
# Import (nur additiv)
|
||
# --------------------------------------------------------------------------
|
||
def _num(value) -> float | None:
|
||
if value is None:
|
||
return None
|
||
text = str(value).strip().replace(",", ".")
|
||
if text == "":
|
||
return None
|
||
return float(text)
|
||
|
||
|
||
def _parse_date(value) -> date | None:
|
||
text = (str(value) if value is not None else "").strip()
|
||
if not text:
|
||
return None
|
||
for fmt in ("%Y-%m-%d", "%d.%m.%Y", "%d.%m.%y"):
|
||
try:
|
||
return datetime.strptime(text, fmt).date()
|
||
except ValueError:
|
||
continue
|
||
raise ValueError(f"Datum nicht lesbar: {text}")
|
||
|
||
|
||
IMPORT_MODES = {"add", "replace_listed", "replace_all"}
|
||
|
||
|
||
def _clear_lots(db: Session, product: Product, user: User) -> None:
|
||
"""Entfernt alle Chargen eines Produkts und protokolliert das als Korrektur."""
|
||
lots = db.query(Lot).filter(Lot.product_id == product.id).all()
|
||
total = float(sum(lot.quantity for lot in lots))
|
||
for lot in lots:
|
||
db.delete(lot)
|
||
if total > 0:
|
||
db.add(
|
||
Movement(
|
||
product_id=product.id,
|
||
lot_id=None,
|
||
user_id=user.id,
|
||
type=MovementType.adjust,
|
||
quantity=-total,
|
||
unit_used="base",
|
||
note="Import: Bestand ersetzt",
|
||
)
|
||
)
|
||
db.flush()
|
||
|
||
|
||
def _clear_all_lots(db: Session, user: User) -> None:
|
||
for product in db.query(Product).all():
|
||
_clear_lots(db, product, user)
|
||
|
||
|
||
def _get_or_create_location(db: Session, name: str | None) -> Location | None:
|
||
name = (name or "").strip()
|
||
if not name:
|
||
return None
|
||
loc = db.query(Location).filter(Location.name == name).first()
|
||
if loc is None:
|
||
loc = Location(name=name)
|
||
db.add(loc)
|
||
db.flush()
|
||
return loc
|
||
|
||
|
||
def _get_or_create_group(db: Session, name: str | None) -> Group | None:
|
||
name = (name or "").strip()
|
||
if not name:
|
||
return None
|
||
group = db.query(Group).filter(Group.name == name).first()
|
||
if group is None:
|
||
group = Group(name=name)
|
||
db.add(group)
|
||
db.flush()
|
||
return group
|
||
|
||
|
||
def _get_or_create_product(db: Session, row: dict, created: list[str]) -> Product:
|
||
"""Sucht per Barcode, sonst per Name; legt das Produkt sonst an."""
|
||
barcode = (row.get("barcode") or "").strip() or None
|
||
name = (row.get("name") or "").strip()
|
||
if barcode:
|
||
product = db.query(Product).filter(Product.barcode == barcode).first()
|
||
if product:
|
||
return product
|
||
if name:
|
||
product = db.query(Product).filter(Product.name == name).first()
|
||
if product:
|
||
return product
|
||
if not name:
|
||
raise ValueError("Produktname fehlt")
|
||
|
||
unit = find_unit(db, (row.get("einheit") or "Stück"))
|
||
if unit is None:
|
||
raise ValueError(f"Unbekannte Einheit: {row.get('einheit')}")
|
||
group = _get_or_create_group(db, row.get("gruppe"))
|
||
product = Product(
|
||
barcode=barcode,
|
||
name=name,
|
||
brand=(row.get("marke") or "").strip() or None,
|
||
base_unit=BASE_OF_KIND[unit.kind],
|
||
display_unit_id=unit.id,
|
||
package_size=_num(row.get("packungsgroesse")),
|
||
package_label=(row.get("gebinde") or "").strip() or None,
|
||
group_id=group.id if group else None,
|
||
min_stock=_num(row.get("mindestbestand")),
|
||
source="import",
|
||
)
|
||
db.add(product)
|
||
db.flush()
|
||
created.append(product.name)
|
||
return product
|
||
|
||
|
||
def _quantity_to_base(db: Session, product: Product, amount: float, token: str) -> float:
|
||
"""Rechnet eine Importmenge in Basiseinheiten um (kennt auch das Gebinde)."""
|
||
text = (token or "").strip().lower()
|
||
package_label = (product.package_label or "Packung").strip().lower()
|
||
if text in PACKAGE_TOKENS or (text and text == package_label):
|
||
if not product.package_size:
|
||
raise ValueError(f"'{token}': keine Packungsgröße für {product.name} hinterlegt")
|
||
return amount * product.package_size
|
||
if not text:
|
||
return amount # ohne Angabe: Basiseinheiten
|
||
unit = find_unit(db, text)
|
||
if unit is None:
|
||
raise ValueError(f"Unbekannte Einheit: {token}")
|
||
return amount * unit.factor
|
||
|
||
|
||
def _import_csv(db: Session, content: bytes, user: User, mode: str) -> dict:
|
||
text = content.decode("utf-8-sig", errors="replace")
|
||
sample = text[:2048]
|
||
delimiter = ";" if sample.count(";") >= sample.count(",") else ","
|
||
reader = csv.DictReader(io.StringIO(text), delimiter=delimiter)
|
||
|
||
created_products: list[str] = []
|
||
lots_added = 0
|
||
errors: list[str] = []
|
||
cleared: set[int] = set()
|
||
|
||
if mode == "replace_all":
|
||
_clear_all_lots(db, user)
|
||
|
||
for index, raw in enumerate(reader, start=2): # Zeile 1 = Kopfzeile
|
||
row = {(k or "").strip().lower(): (v if v is not None else "") for k, v in raw.items()}
|
||
if not any(str(v).strip() for v in row.values()):
|
||
continue
|
||
try:
|
||
product = _get_or_create_product(db, row, created_products)
|
||
# Beim Ersetzen: Bestand des Produkts einmalig leeren, bevor die
|
||
# Zeilen dieser Datei dazukommen.
|
||
if mode == "replace_listed" and product.id not in cleared:
|
||
_clear_lots(db, product, user)
|
||
cleared.add(product.id)
|
||
amount = _num(row.get("menge"))
|
||
if amount is None or amount <= 0:
|
||
continue # Zeile ohne Bestand: nur Stammdaten
|
||
quantity = _quantity_to_base(db, product, amount, row.get("menge_einheit", ""))
|
||
location = _get_or_create_location(db, row.get("lagerort"))
|
||
lot = Lot(
|
||
product_id=product.id,
|
||
quantity=quantity,
|
||
best_before=_parse_date(row.get("mhd")),
|
||
location_id=location.id if location else None,
|
||
)
|
||
db.add(lot)
|
||
db.flush()
|
||
db.add(
|
||
Movement(
|
||
product_id=product.id,
|
||
lot_id=lot.id,
|
||
user_id=user.id,
|
||
type=MovementType.in_,
|
||
quantity=quantity,
|
||
unit_used=row.get("menge_einheit") or "base",
|
||
note="Import",
|
||
)
|
||
)
|
||
lots_added += 1
|
||
except Exception as exc: # eine fehlerhafte Zeile darf den Rest nicht stoppen
|
||
errors.append(f"Zeile {index}: {exc}")
|
||
|
||
return {
|
||
"format": "csv",
|
||
"mode": mode,
|
||
"products_created": len(created_products),
|
||
"products_cleared": len(cleared),
|
||
"lots_added": lots_added,
|
||
"errors": errors,
|
||
}
|
||
|
||
|
||
def _import_json(db: Session, content: bytes, user: User, mode: str) -> dict:
|
||
data = json.loads(content.decode("utf-8-sig", errors="replace"))
|
||
created_products: list[str] = []
|
||
lots_added = 0
|
||
units_created = 0
|
||
errors: list[str] = []
|
||
cleared: set[int] = set()
|
||
|
||
if mode == "replace_all":
|
||
_clear_all_lots(db, user)
|
||
|
||
for entry in data.get("units", []):
|
||
try:
|
||
if db.query(Unit).filter(Unit.name == entry["name"]).first():
|
||
continue
|
||
db.add(
|
||
Unit(
|
||
name=entry["name"],
|
||
kind=UnitKind(entry.get("kind", "count")),
|
||
factor=float(entry.get("factor", 1)),
|
||
is_builtin=False,
|
||
)
|
||
)
|
||
units_created += 1
|
||
except Exception as exc:
|
||
errors.append(f"Einheit {entry!r}: {exc}")
|
||
db.flush()
|
||
|
||
for entry in data.get("groups", []):
|
||
_get_or_create_group(db, entry.get("name"))
|
||
for entry in data.get("locations", []):
|
||
_get_or_create_location(db, entry.get("name"))
|
||
db.flush()
|
||
# Übergeordnete Lagerorte nachziehen
|
||
for entry in data.get("locations", []):
|
||
parent_name = entry.get("parent")
|
||
if not parent_name:
|
||
continue
|
||
child = db.query(Location).filter(Location.name == entry.get("name")).first()
|
||
parent = db.query(Location).filter(Location.name == parent_name).first()
|
||
if child and parent and child.parent_id is None and child.id != parent.id:
|
||
child.parent_id = parent.id
|
||
|
||
for entry in data.get("products", []):
|
||
try:
|
||
row = {
|
||
"barcode": entry.get("barcode") or "",
|
||
"name": entry.get("name") or "",
|
||
"marke": entry.get("brand") or "",
|
||
"einheit": entry.get("unit") or "Stück",
|
||
"packungsgroesse": entry.get("package_size") or "",
|
||
"gebinde": entry.get("package_label") or "",
|
||
"gruppe": entry.get("group") or "",
|
||
"mindestbestand": entry.get("min_stock") if entry.get("min_stock") is not None else "",
|
||
}
|
||
product = _get_or_create_product(db, row, created_products)
|
||
if mode == "replace_listed" and product.id not in cleared:
|
||
_clear_lots(db, product, user)
|
||
cleared.add(product.id)
|
||
for lot_entry in entry.get("lots", []):
|
||
quantity = float(lot_entry.get("quantity") or 0)
|
||
if quantity <= 0:
|
||
continue
|
||
location = _get_or_create_location(db, lot_entry.get("location"))
|
||
lot = Lot(
|
||
product_id=product.id,
|
||
quantity=quantity,
|
||
best_before=_parse_date(lot_entry.get("best_before")),
|
||
location_id=location.id if location else None,
|
||
)
|
||
db.add(lot)
|
||
db.flush()
|
||
db.add(
|
||
Movement(
|
||
product_id=product.id,
|
||
lot_id=lot.id,
|
||
user_id=user.id,
|
||
type=MovementType.in_,
|
||
quantity=quantity,
|
||
unit_used="base",
|
||
note="Import",
|
||
)
|
||
)
|
||
lots_added += 1
|
||
except Exception as exc:
|
||
errors.append(f"Produkt {entry.get('name')!r}: {exc}")
|
||
|
||
return {
|
||
"format": "json",
|
||
"mode": mode,
|
||
"units_created": units_created,
|
||
"products_created": len(created_products),
|
||
"products_cleared": len(cleared),
|
||
"lots_added": lots_added,
|
||
"errors": errors,
|
||
}
|
||
|
||
|
||
@router.post("/import/stock")
|
||
def import_stock(
|
||
mode: str = "add",
|
||
file: UploadFile = File(...),
|
||
db: Session = Depends(get_db),
|
||
user: User = Depends(require_admin),
|
||
) -> dict:
|
||
"""Importiert CSV oder JSON.
|
||
|
||
mode:
|
||
add – nur ergänzen (Standard, löscht nie etwas)
|
||
replace_listed – Bestand der in der Datei genannten Produkte ersetzen
|
||
replace_all – alle Bestände vorher leeren (vollständige Wiederherstellung)
|
||
"""
|
||
if mode not in IMPORT_MODES:
|
||
raise HTTPException(status.HTTP_400_BAD_REQUEST, f"Unbekannter Modus: {mode}")
|
||
|
||
content = file.file.read()
|
||
if not content:
|
||
raise HTTPException(status.HTTP_400_BAD_REQUEST, "Datei ist leer")
|
||
|
||
filename = (file.filename or "").lower()
|
||
stripped = content.lstrip()[:1]
|
||
is_json = filename.endswith(".json") or stripped in (b"{", b"[")
|
||
|
||
try:
|
||
result = (
|
||
_import_json(db, content, user, mode)
|
||
if is_json
|
||
else _import_csv(db, content, user, mode)
|
||
)
|
||
except json.JSONDecodeError as exc:
|
||
db.rollback()
|
||
raise HTTPException(status.HTTP_400_BAD_REQUEST, f"JSON nicht lesbar: {exc}") from exc
|
||
except Exception as exc:
|
||
db.rollback()
|
||
raise HTTPException(status.HTTP_400_BAD_REQUEST, f"Import fehlgeschlagen: {exc}") from exc
|
||
|
||
db.commit()
|
||
return result
|