diff --git a/brewman/brewman/routers/product.py b/brewman/brewman/routers/product.py index 6fb3cb69..6ea7f585 100644 --- a/brewman/brewman/routers/product.py +++ b/brewman/brewman/routers/product.py @@ -511,7 +511,7 @@ async def show_term_product( for item in db.execute(query_).unique().scalars().all(): list_.append( ProductSku( - id_=item.id, + id_=item.product_id, name=item.name, fraction_units=item.fraction_units, cost_price=Decimal(0), diff --git a/brewman/brewman/routers/reports/product_ledger.py b/brewman/brewman/routers/reports/product_ledger.py index 1cb43742..165e92d3 100644 --- a/brewman/brewman/routers/reports/product_ledger.py +++ b/brewman/brewman/routers/reports/product_ledger.py @@ -86,29 +86,33 @@ def build_report( SkuVersion.valid_till >= Voucher.date_, ), ) - query: list[tuple[Voucher, Inventory, Journal, SkuVersion]] = db.execute( # type: ignore[assignment] - select(Voucher, Inventory, Journal, SkuVersion) - .select_from(Voucher) - .join(Voucher.journals) - .join(Journal.account) - .join(Journal.cost_centre) - .join(Voucher.inventories) - .join(Inventory.batch) - .join(Batch.sku) - .join(SkuVersion, onclause=sku_version_onclause) - .join(StockKeepingUnit.product) - .options( - contains_eager(Voucher.journals).contains_eager(Journal.account), - contains_eager(Voucher.journals).contains_eager(Journal.cost_centre), + query: list[tuple[Voucher, Inventory, Journal, SkuVersion]] = ( + db.execute( # type: ignore[assignment] + select(Voucher, Inventory, Journal, SkuVersion) + .select_from(Voucher) + .join(Voucher.journals) + .join(Journal.account) + .join(Journal.cost_centre) + .join(Voucher.inventories) + .join(Inventory.batch) + .join(Batch.sku) + .join(SkuVersion, onclause=sku_version_onclause) + .join(StockKeepingUnit.product) + .options( + contains_eager(Voucher.journals).contains_eager(Journal.account), + contains_eager(Voucher.journals).contains_eager(Journal.cost_centre), + ) + .where( + StockKeepingUnit.product_id == product_id, + Journal.cost_centre_id != CostCentre.cost_centre_purchase(), + Voucher.date_ >= start_date, + Voucher.date_ <= finish_date, + ) + .order_by(Voucher.date_, Voucher.last_edit_date) ) - .where( - StockKeepingUnit.product_id == product_id, - Journal.cost_centre_id != CostCentre.cost_centre_purchase(), - Voucher.date_ >= start_date, - Voucher.date_ <= finish_date, - ) - .order_by(Voucher.date_, Voucher.last_edit_date) - ).all() + .unique() + .all() + ) for voucher, inventory, journal, stockKeepingUnit in query: journal_debit = journal.debit * -1