Fix: Purchase update error. There is no longer product_id in Inventory. Also, now issuing direct updates instead of updating the models.

This commit is contained in:
Amritanshu Agrawal 2021-09-30 17:19:36 +05:30
parent 1647d356c9
commit e96ef38173
4 changed files with 35 additions and 22 deletions

View File

@ -38,7 +38,6 @@ class Inventory(Base):
tax=None, tax=None,
discount=None, discount=None,
batch=None, batch=None,
product=None,
): ):
self.id = id_ self.id = id_
self.voucher_id = voucher_id self.voucher_id = voucher_id

View File

@ -5,7 +5,7 @@ from typing import List
import brewman.schemas.batch_integrity as schemas import brewman.schemas.batch_integrity as schemas
from fastapi import APIRouter, Security 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 sqlalchemy.orm import Session, contains_eager
from ..core.security import get_current_active_user as get_user 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: def fix_single_batch_prices(batch_id: uuid.UUID, db: Session) -> None:
list_ = ( list_ = (
db.execute( db.execute(
select(Batch) select(distinct(Inventory.voucher_id))
.join(Batch.inventories) .join(Inventory.batch)
.where(Batch.id == batch_id, Batch.rate != Inventory.rate) .where(Inventory.batch_id == batch_id, Batch.rate != Inventory.rate)
.options(contains_eager(Batch.inventories))
) )
.unique()
.scalars() .scalars()
.all() .all()
) )
for batch in list_: for voucher_id in list_:
for inv in batch.inventories: refresh_voucher(voucher_id, batch_id, db)
refresh_voucher(inv.voucher_id, inv.product_id, db)

View File

@ -8,7 +8,7 @@ import brewman.schemas.input as schema_in
import brewman.schemas.voucher as output import brewman.schemas.voucher as output
from fastapi import APIRouter, Depends, File, HTTPException, Request, Security, status 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.exc import SQLAlchemyError
from sqlalchemy.orm import Session from sqlalchemy.orm import Session
@ -339,18 +339,19 @@ def update_journals(
item.amount = amount 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: try:
voucher: Voucher = db.execute(select(Voucher).where(Voucher.id == id_)).scalar_one() db.execute(update(Voucher).where(Voucher.id == id_).values(last_edit_date=datetime.utcnow()))
voucher.last_edit_date = datetime.utcnow() batch = db.execute(select(Batch).where(Batch.id == batch_id)).scalar_one()
inv = next(i for i in voucher.inventories if i.product_id == product_id) db.execute(
batch = db.execute(select(Batch).where(Batch.id == inv.batch_id)).scalar_one() update(Inventory)
inv.rate = batch.rate .where(Inventory.voucher_id == id_, Inventory.batch_id == batch_id)
inv.tax = batch.tax .values(rate=batch.rate, tax=batch.tax, discount=batch.discount)
inv.discount = batch.discount )
amount = sum(i.amount for i in voucher.inventories) amount = round(
for journal in voucher.journals: db.execute(select(func.sum(Inventory.amount)).where(Inventory.voucher_id == id_)).scalar_one(), 2
journal.amount = amount )
db.execute(update(Journal).where(Journal.voucher_id == id_).values(amount=amount))
except SQLAlchemyError as e: except SQLAlchemyError as e:
raise HTTPException( raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,

View File

@ -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"}