Fix: Product sale report was aggregating on the product_version_id

This commit is contained in:
2026-02-14 12:20:09 +00:00
parent 4cc2ff2229
commit 9b0da9cb65
2 changed files with 10 additions and 12 deletions

View File

@ -53,13 +53,11 @@ def product_sale_report(
start_date: date, finish_date: date, id_: uuid.UUID | None, db: Session start_date: date, finish_date: date, id_: uuid.UUID | None, db: Session
) -> list[ProductSaleReportItem]: ) -> list[ProductSaleReportItem]:
day = func.cast( day = func.cast(
Voucher.date + timedelta(minutes=settings.TIMEZONE_OFFSET_MINUTES - settings.NEW_DAY_OFFSET_MINUTES), Date Kot.date + timedelta(minutes=settings.TIMEZONE_OFFSET_MINUTES - settings.NEW_DAY_OFFSET_MINUTES), Date
).label("day") ).label("day")
product_version_onclause = _pv_onclause(day)
sku_version_onclause = _sv_onclause(day)
query = ( query = (
select( select(
ProductVersion.id, StockKeepingUnit.id,
ProductVersion.name, ProductVersion.name,
SkuVersion.units, SkuVersion.units,
Voucher.voucher_type, Voucher.voucher_type,
@ -69,10 +67,10 @@ def product_sale_report(
.join(Inventory.kot) .join(Inventory.kot)
.join(Kot.voucher) .join(Kot.voucher)
.join(Inventory.sku) .join(Inventory.sku)
.join(SkuVersion, onclause=sku_version_onclause) .join(SkuVersion, onclause=_sv_onclause(day))
.join(SkuVersion.menu_category) .join(SkuVersion.menu_category)
.join(StockKeepingUnit.product) .join(StockKeepingUnit.product)
.join(ProductVersion, onclause=product_version_onclause) .join(ProductVersion, onclause=_pv_onclause(day))
.join(ProductVersion.sale_category) .join(ProductVersion.sale_category)
.join(Voucher.food_table) .join(Voucher.food_table)
.where( .where(
@ -85,7 +83,7 @@ def product_sale_report(
query = query.group_by( query = query.group_by(
SaleCategory.name, SaleCategory.name,
MenuCategory.name, MenuCategory.name,
ProductVersion.id, StockKeepingUnit.id,
ProductVersion.name, ProductVersion.name,
SkuVersion.units, SkuVersion.units,
Voucher.voucher_type, Voucher.voucher_type,
@ -93,14 +91,14 @@ def product_sale_report(
).order_by(SaleCategory.name, MenuCategory.name, ProductVersion.name, SkuVersion.units) ).order_by(SaleCategory.name, MenuCategory.name, ProductVersion.name, SkuVersion.units)
list_ = db.execute(query).all() list_ = db.execute(query).all()
info: list[ProductSaleReportItem] = [] info: list[ProductSaleReportItem] = []
for product_version_id, name, units, v_type, hh, quantity in list_: for sku_id, name, units, v_type, hh, quantity in list_:
type_ = VoucherType(v_type).name type_ = VoucherType(v_type).name
old = next((i for i in info if i.product_version_id == product_version_id and i.is_happy_hour == hh), None) old = next((i for i in info if i.sku_id == sku_id and i.is_happy_hour == hh), None)
if old: if old:
old[type_] = old[type_] + quantity old[type_] = old[type_] + quantity
else: else:
item = ProductSaleReportItem( item = ProductSaleReportItem(
product_version_id=product_version_id, name=f"{'H H ' if hh else ''}{name} ({units})", is_happy_hour=hh sku_id=sku_id, name=f"{'H H ' if hh else ''}{name} ({units})", is_happy_hour=hh
) )
item[type_] = quantity item[type_] = quantity
info.append(item) info.append(item)

View File

@ -19,7 +19,7 @@ from .user import UserLink
class ProductSaleReportItem(BaseModel): class ProductSaleReportItem(BaseModel):
product_version_id: uuid.UUID sku_id: uuid.UUID
name: str name: str
is_happy_hour: bool is_happy_hour: bool
@ -40,7 +40,7 @@ class ProductSaleReportItem(BaseModel):
@model_serializer(mode="plain") @model_serializer(mode="plain")
def custom_dump(self) -> dict: # type: ignore def custom_dump(self) -> dict: # type: ignore
base = { base = {
"productVersionId": str(self.product_version_id), "skuId": str(self.sku_id),
"name": self.name, "name": self.name,
"isHappyHour": self.is_happy_hour, "isHappyHour": self.is_happy_hour,
} }