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,
discount=None,
batch=None,
product=None,
):
self.id = id_
self.voucher_id = voucher_id

View File

@ -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)

View File

@ -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,

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