Files
Vorrania/backend/app/crud.py
Scarriffle d4e7ff8e3d Ablauf-Warnungen + Packungen-Spalte + Farbcodierung
- Backend: ProductOut.expired_count (Anzahl abgelaufener Chargen je Produkt).
- Einlagern: Inline-Warnung pro Charge, wenn MHD in der Vergangenheit liegt.
- Produkte-Liste: Badge "N abgelaufen"; neue Spalte "Packungen" (Bestand/Packungsgroesse,
  bei Stueck-Produkten = Bestand).
- Produktdetail: abgelaufene Chargen rot, Warnfrist gelb (Warnfrist aus Settings),
  Warnbanner bei abgelaufenem Bestand.
- Uebersicht: alle Ablaufzeilen gelb, abgelaufene rot; deutlicher Warnbanner.
- units.js: daysUntil/isExpired/expiryRowClass/amountText.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-22 11:21:14 +02:00

47 lines
1.3 KiB
Python

"""Kleine gemeinsame Helfer für Router."""
from __future__ import annotations
from datetime import date
from fastapi import HTTPException, status
from sqlalchemy.orm import Session
from .models import Lot, Product
from .schemas import ProductOut
from .services.stock import current_stock
def product_to_out(db: Session, product: Product) -> ProductOut:
out = ProductOut.model_validate(product)
out.stock = current_stock(db, product.id)
out.expired_count = (
db.query(Lot)
.filter(
Lot.product_id == product.id,
Lot.best_before.isnot(None),
Lot.best_before < date.today(),
Lot.quantity > 0,
)
.count()
)
return out
def resolve_product(
db: Session, product_id: int | None, barcode: str | None
) -> Product:
"""Findet ein Produkt per ID oder Barcode; wirft 404, wenn keins passt."""
product: Product | None = None
if product_id is not None:
product = db.get(Product, product_id)
elif barcode:
product = db.query(Product).filter(Product.barcode == barcode).first()
else:
raise HTTPException(
status.HTTP_400_BAD_REQUEST, "product_id oder barcode erforderlich"
)
if product is None:
raise HTTPException(status.HTTP_404_NOT_FOUND, "Produkt nicht gefunden")
return product