diff --git a/backend/app/crud.py b/backend/app/crud.py index 2197aec..d8ef056 100644 --- a/backend/app/crud.py +++ b/backend/app/crud.py @@ -2,10 +2,12 @@ from __future__ import annotations +from datetime import date + from fastapi import HTTPException, status from sqlalchemy.orm import Session -from .models import Product +from .models import Lot, Product from .schemas import ProductOut from .services.stock import current_stock @@ -13,6 +15,16 @@ 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 diff --git a/backend/app/schemas.py b/backend/app/schemas.py index 2182211..ac60ae8 100644 --- a/backend/app/schemas.py +++ b/backend/app/schemas.py @@ -110,6 +110,7 @@ class ProductOut(BaseModel): created_at: datetime # angereichert: stock: float = 0.0 + expired_count: int = 0 class LookupResult(BaseModel): diff --git a/web/src/pages/CheckIn.jsx b/web/src/pages/CheckIn.jsx index 7f43293..78f8d92 100644 --- a/web/src/pages/CheckIn.jsx +++ b/web/src/pages/CheckIn.jsx @@ -4,7 +4,7 @@ import { api } from "../api"; import { useAuth } from "../auth"; import Icon from "../components/Icon"; import { guessGroup, suggestionToProduct } from "../offUtils"; -import { fmt, unitOptions, unitShort } from "../units"; +import { fmt, isExpired, unitOptions, unitShort } from "../units"; const emptyLine = () => ({ quantity: "", best_before: "" }); @@ -251,7 +251,11 @@ export default function CheckIn() {