Fix: Product sale report was aggregating on the product_version_id
This commit is contained in:
@ -53,13 +53,11 @@ def product_sale_report(
|
||||
start_date: date, finish_date: date, id_: uuid.UUID | None, db: Session
|
||||
) -> list[ProductSaleReportItem]:
|
||||
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")
|
||||
product_version_onclause = _pv_onclause(day)
|
||||
sku_version_onclause = _sv_onclause(day)
|
||||
query = (
|
||||
select(
|
||||
ProductVersion.id,
|
||||
StockKeepingUnit.id,
|
||||
ProductVersion.name,
|
||||
SkuVersion.units,
|
||||
Voucher.voucher_type,
|
||||
@ -69,10 +67,10 @@ def product_sale_report(
|
||||
.join(Inventory.kot)
|
||||
.join(Kot.voucher)
|
||||
.join(Inventory.sku)
|
||||
.join(SkuVersion, onclause=sku_version_onclause)
|
||||
.join(SkuVersion, onclause=_sv_onclause(day))
|
||||
.join(SkuVersion.menu_category)
|
||||
.join(StockKeepingUnit.product)
|
||||
.join(ProductVersion, onclause=product_version_onclause)
|
||||
.join(ProductVersion, onclause=_pv_onclause(day))
|
||||
.join(ProductVersion.sale_category)
|
||||
.join(Voucher.food_table)
|
||||
.where(
|
||||
@ -85,7 +83,7 @@ def product_sale_report(
|
||||
query = query.group_by(
|
||||
SaleCategory.name,
|
||||
MenuCategory.name,
|
||||
ProductVersion.id,
|
||||
StockKeepingUnit.id,
|
||||
ProductVersion.name,
|
||||
SkuVersion.units,
|
||||
Voucher.voucher_type,
|
||||
@ -93,14 +91,14 @@ def product_sale_report(
|
||||
).order_by(SaleCategory.name, MenuCategory.name, ProductVersion.name, SkuVersion.units)
|
||||
list_ = db.execute(query).all()
|
||||
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
|
||||
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:
|
||||
old[type_] = old[type_] + quantity
|
||||
else:
|
||||
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
|
||||
info.append(item)
|
||||
|
||||
@ -19,7 +19,7 @@ from .user import UserLink
|
||||
|
||||
|
||||
class ProductSaleReportItem(BaseModel):
|
||||
product_version_id: uuid.UUID
|
||||
sku_id: uuid.UUID
|
||||
name: str
|
||||
is_happy_hour: bool
|
||||
|
||||
@ -40,7 +40,7 @@ class ProductSaleReportItem(BaseModel):
|
||||
@model_serializer(mode="plain")
|
||||
def custom_dump(self) -> dict: # type: ignore
|
||||
base = {
|
||||
"productVersionId": str(self.product_version_id),
|
||||
"skuId": str(self.sku_id),
|
||||
"name": self.name,
|
||||
"isHappyHour": self.is_happy_hour,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user