From e96ef38173f5b8f8e18df37a74352c3bc9d3bafd Mon Sep 17 00:00:00 2001 From: tanshu Date: Thu, 30 Sep 2021 17:19:36 +0530 Subject: [PATCH] Fix: Purchase update error. There is no longer product_id in Inventory. Also, now issuing direct updates instead of updating the models. --- brewman/brewman/models/inventory.py | 1 - brewman/brewman/routers/batch_integrity.py | 15 ++++++------- brewman/brewman/routers/issue.py | 25 +++++++++++----------- brewman/brewman/schemas/recipe_item.py | 16 ++++++++++++++ 4 files changed, 35 insertions(+), 22 deletions(-) create mode 100644 brewman/brewman/schemas/recipe_item.py diff --git a/brewman/brewman/models/inventory.py b/brewman/brewman/models/inventory.py index 588f2384..a10000a0 100644 --- a/brewman/brewman/models/inventory.py +++ b/brewman/brewman/models/inventory.py @@ -38,7 +38,6 @@ class Inventory(Base): tax=None, discount=None, batch=None, - product=None, ): self.id = id_ self.voucher_id = voucher_id diff --git a/brewman/brewman/routers/batch_integrity.py b/brewman/brewman/routers/batch_integrity.py index 61dc0d7e..bfad5791 100644 --- a/brewman/brewman/routers/batch_integrity.py +++ b/brewman/brewman/routers/batch_integrity.py @@ -5,7 +5,7 @@ from typing import List import brewman.schemas.batch_integrity as schemas from fastapi import APIRouter, Security -from sqlalchemy import func, select, update +from sqlalchemy import distinct, func, select, update from sqlalchemy.orm import Session, contains_eager from ..core.security import get_current_active_user as get_user @@ -179,16 +179,13 @@ def fix_batch_prices(db: Session) -> None: def fix_single_batch_prices(batch_id: uuid.UUID, db: Session) -> None: list_ = ( db.execute( - select(Batch) - .join(Batch.inventories) - .where(Batch.id == batch_id, Batch.rate != Inventory.rate) - .options(contains_eager(Batch.inventories)) + select(distinct(Inventory.voucher_id)) + .join(Inventory.batch) + .where(Inventory.batch_id == batch_id, Batch.rate != Inventory.rate) ) - .unique() .scalars() .all() ) - for batch in list_: - for inv in batch.inventories: - refresh_voucher(inv.voucher_id, inv.product_id, db) + for voucher_id in list_: + refresh_voucher(voucher_id, batch_id, db) diff --git a/brewman/brewman/routers/issue.py b/brewman/brewman/routers/issue.py index 601982c2..98f18def 100644 --- a/brewman/brewman/routers/issue.py +++ b/brewman/brewman/routers/issue.py @@ -8,7 +8,7 @@ import brewman.schemas.input as schema_in import brewman.schemas.voucher as output from fastapi import APIRouter, Depends, File, HTTPException, Request, Security, status -from sqlalchemy import distinct, or_, select +from sqlalchemy import distinct, func, or_, select, update from sqlalchemy.exc import SQLAlchemyError from sqlalchemy.orm import Session @@ -339,18 +339,19 @@ def update_journals( item.amount = amount -def refresh_voucher(id_: uuid.UUID, product_id: uuid.UUID, db: Session) -> None: +def refresh_voucher(id_: uuid.UUID, batch_id: uuid.UUID, db: Session) -> None: try: - voucher: Voucher = db.execute(select(Voucher).where(Voucher.id == id_)).scalar_one() - voucher.last_edit_date = datetime.utcnow() - inv = next(i for i in voucher.inventories if i.product_id == product_id) - batch = db.execute(select(Batch).where(Batch.id == inv.batch_id)).scalar_one() - inv.rate = batch.rate - inv.tax = batch.tax - inv.discount = batch.discount - amount = sum(i.amount for i in voucher.inventories) - for journal in voucher.journals: - journal.amount = amount + db.execute(update(Voucher).where(Voucher.id == id_).values(last_edit_date=datetime.utcnow())) + batch = db.execute(select(Batch).where(Batch.id == batch_id)).scalar_one() + db.execute( + update(Inventory) + .where(Inventory.voucher_id == id_, Inventory.batch_id == batch_id) + .values(rate=batch.rate, tax=batch.tax, discount=batch.discount) + ) + amount = round( + db.execute(select(func.sum(Inventory.amount)).where(Inventory.voucher_id == id_)).scalar_one(), 2 + ) + db.execute(update(Journal).where(Journal.voucher_id == id_).values(amount=amount)) except SQLAlchemyError as e: raise HTTPException( status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, diff --git a/brewman/brewman/schemas/recipe_item.py b/brewman/brewman/schemas/recipe_item.py new file mode 100644 index 00000000..328506d3 --- /dev/null +++ b/brewman/brewman/schemas/recipe_item.py @@ -0,0 +1,16 @@ +import uuid +from typing import Optional + +from pydantic import BaseModel + +from brewman.schemas.product import ProductLink + + +class RecipeItem(BaseModel): + id_: Optional[uuid.UUID] + product: ProductLink + quantity: int + price: int + + class Config: + fields = {"id_": "id"} \ No newline at end of file