diff --git a/barker/barker/main.py b/barker/barker/main.py index de2af0a..92c1b8d 100644 --- a/barker/barker/main.py +++ b/barker/barker/main.py @@ -29,6 +29,7 @@ from .routers.reports import ( cashier_report, discount_report, product_sale_report, + product_updates_report, sale_report, tax_report, ) @@ -65,6 +66,7 @@ app.include_router(product.router, prefix="/api/products", tags=["products"]) app.include_router(device.router, prefix="/api/devices", tags=["devices"]) app.include_router(sale_category.router, prefix="/api/sale-categories", tags=["products"]) app.include_router(header_footer.router, prefix="/api/header-footer", tags=["products"]) +app.include_router(product_updates_report.router, prefix="/api/product-updates-report", tags=["products"]) app.include_router(section.router, prefix="/api/sections", tags=["sections"]) app.include_router(section_printer.router, prefix="/api/section-printers", tags=["section-printers"]) diff --git a/barker/barker/routers/product.py b/barker/barker/routers/product.py index 493680b..36a4865 100644 --- a/barker/barker/routers/product.py +++ b/barker/barker/routers/product.py @@ -174,6 +174,7 @@ def update( quantity=data.quantity, valid_from=date_, valid_till=None, + sort_order=item.sort_order, ) db.add(product_version) db.commit() diff --git a/barker/barker/routers/reports/product_updates_report.py b/barker/barker/routers/reports/product_updates_report.py new file mode 100644 index 0000000..0693c65 --- /dev/null +++ b/barker/barker/routers/reports/product_updates_report.py @@ -0,0 +1,83 @@ +from datetime import date, timedelta +from typing import List + +from fastapi import APIRouter, Depends, Security +from sqlalchemy import and_, or_ +from sqlalchemy.orm import Session, contains_eager, joinedload + +from ...core.security import get_current_active_user as get_user +from ...db.session import SessionLocal +from ...models import MenuCategory, ProductVersion +from ...schemas.auth import UserToken +from . import report_finish_date, report_start_date + + +router = APIRouter() + + +# Dependency +def get_db() -> Session: + try: + db = SessionLocal() + yield db + finally: + db.close() + + +@router.get("") +def product_updates_report_view( + start_date: date = Depends(report_start_date), + finish_date: date = Depends(report_finish_date), + db: Session = Depends(get_db), + user: UserToken = Security(get_user, scopes=["product-sale-report"]), +): + return { + "startDate": start_date.strftime("%d-%b-%Y"), + "finishDate": finish_date.strftime("%d-%b-%Y"), + "report": product_updates_report(start_date, finish_date, db), + } + + +def product_updates_report(s: date, f: date, db: Session) -> List[str]: + list_: List[ProductVersion] = ( + db.query(ProductVersion) + .join(ProductVersion.menu_category) + .filter( + or_( + and_( + ProductVersion.valid_from >= s, + ProductVersion.valid_from <= f, + ), + and_( + ProductVersion.valid_till >= s - timedelta(days=1), + ProductVersion.valid_till <= f, + ), + ) + ) + .order_by( + MenuCategory.sort_order, + MenuCategory.name, + ProductVersion.sort_order, + ProductVersion.name, + ProductVersion.valid_from.nullsfirst(), + ) + .options( + joinedload(ProductVersion.menu_category, innerjoin=True), + contains_eager(ProductVersion.menu_category), + ) + .all() + ) + report = {} + for item in list_: + if item.product_id not in report: + report[item.product_id] = "" + report[item.product_id] += ( + "From: " + + (f"{item.valid_from:%d-%b-%Y}" if item.valid_from is not None else "\u221e") + + " " + + "Till: " + + (f"{item.valid_till:%d-%b-%Y}" if item.valid_till is not None else "\u221e") + + "\n" + + f"{item.full_name} @ {item.price: .2f} - {'Happy Hour' if item.has_happy_hour else 'No Happy Hour'}\n" + ) + return list(report.values()) diff --git a/bookie/src/app/app-routing.module.ts b/bookie/src/app/app-routing.module.ts index f244c1f..5f55546 100644 --- a/bookie/src/app/app-routing.module.ts +++ b/bookie/src/app/app-routing.module.ts @@ -68,6 +68,13 @@ const routes: Routes = [ (mod) => mod.ProductSaleReportModule, ), }, + { + path: 'product-updates-report', + loadChildren: () => + import('./product-updates-report/product-updates-report.module').then( + (mod) => mod.ProductUpdatesReportModule, + ), + }, { path: 'menu-categories', loadChildren: () => diff --git a/bookie/src/app/home/home.component.html b/bookie/src/app/home/home.component.html index 90843c4..4f24d42 100644 --- a/bookie/src/app/home/home.component.html +++ b/bookie/src/app/home/home.component.html @@ -206,6 +206,15 @@ >

Header / Footer

+ +

Product Updates Report

+