Moved the batch integrity report from settings to its own report in products with permission of product ledger.
It also automatically fixes the issue prices.
This commit is contained in:
@ -13,6 +13,7 @@ from .routers import (
|
||||
attendance_report,
|
||||
attendance_types,
|
||||
batch,
|
||||
batch_integrity,
|
||||
client,
|
||||
cost_centre,
|
||||
credit_salary,
|
||||
@ -47,6 +48,7 @@ from .routers.reports import (
|
||||
cash_flow,
|
||||
closing_stock,
|
||||
daybook,
|
||||
entries,
|
||||
ledger,
|
||||
net_transactions,
|
||||
product_ledger,
|
||||
@ -57,7 +59,6 @@ from .routers.reports import (
|
||||
reconcile,
|
||||
stock_movement,
|
||||
trial_balance,
|
||||
entries,
|
||||
)
|
||||
|
||||
|
||||
@ -105,6 +106,7 @@ app.include_router(reconcile.router, prefix="/api/reconcile", tags=["reports"])
|
||||
app.include_router(stock_movement.router, prefix="/api/stock-movement", tags=["reports"])
|
||||
app.include_router(trial_balance.router, prefix="/api/trial-balance", tags=["reports"])
|
||||
app.include_router(entries.router, prefix="/api/entries", tags=["reports"])
|
||||
app.include_router(batch_integrity.router, prefix="/api/batch-integrity", tags=["reports"])
|
||||
|
||||
app.include_router(issue_grid.router, prefix="/api/issue-grid", tags=["vouchers"])
|
||||
app.include_router(batch.router, prefix="/api/batch", tags=["vouchers"])
|
||||
|
||||
@ -21,7 +21,7 @@ class Batch(Base):
|
||||
tax = Column("tax", Numeric(precision=15, scale=5), nullable=False)
|
||||
discount = Column("discount", Numeric(precision=15, scale=5), nullable=False)
|
||||
|
||||
inventories = relationship("Inventory", backref="batch", cascade=None, cascade_backrefs=False)
|
||||
inventories = relationship("Inventory", back_populates="batch")
|
||||
product = relationship("Product", back_populates="batches")
|
||||
|
||||
def __init__(
|
||||
|
||||
@ -28,6 +28,7 @@ class Inventory(Base):
|
||||
|
||||
voucher = relationship("Voucher", back_populates="inventories")
|
||||
product = relationship("Product", back_populates="inventories")
|
||||
batch = relationship("Batch", back_populates="inventories")
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
|
||||
172
brewman/brewman/routers/batch_integrity.py
Normal file
172
brewman/brewman/routers/batch_integrity.py
Normal file
@ -0,0 +1,172 @@
|
||||
import uuid
|
||||
|
||||
from typing import List
|
||||
|
||||
import brewman.schemas.batch_integrity as schemas
|
||||
|
||||
from fastapi import APIRouter, Security
|
||||
from sqlalchemy import func, select, update
|
||||
from sqlalchemy.orm import Session, contains_eager
|
||||
|
||||
from ..core.security import get_current_active_user as get_user
|
||||
from ..db.session import SessionFuture
|
||||
from ..models.batch import Batch
|
||||
from ..models.cost_centre import CostCentre
|
||||
from ..models.inventory import Inventory
|
||||
from ..models.journal import Journal
|
||||
from ..models.product import Product
|
||||
from ..models.voucher import Voucher
|
||||
from ..models.voucher_type import VoucherType
|
||||
from ..schemas.user import UserToken
|
||||
from .issue import refresh_voucher
|
||||
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
|
||||
@router.post("", response_model=List[schemas.BatchIntegrity])
|
||||
def post_check_batch_integrity(
|
||||
user: UserToken = Security(get_user, scopes=["product-ledger"])
|
||||
) -> List[schemas.BatchIntegrity]:
|
||||
|
||||
with SessionFuture() as db:
|
||||
info = batches(db) + batch_dates(db)
|
||||
fix_batch_prices(db)
|
||||
db.commit()
|
||||
return info
|
||||
|
||||
|
||||
def batches(db: Session) -> List[schemas.BatchIntegrity]:
|
||||
inv_sum = func.sum(Inventory.quantity * Journal.debit).label("quantity")
|
||||
list_ = db.execute(
|
||||
select(Batch, Product.full_name, inv_sum)
|
||||
.join(Batch.product)
|
||||
.join(Batch.inventories)
|
||||
.join(Inventory.voucher)
|
||||
.join(Voucher.journals)
|
||||
.where(
|
||||
Voucher.type.in_(
|
||||
[
|
||||
VoucherType.by_name("Purchase").id,
|
||||
VoucherType.by_name("Purchase Return").id,
|
||||
VoucherType.by_name("Issue").id,
|
||||
VoucherType.by_name("Opening Batches").id,
|
||||
]
|
||||
),
|
||||
Journal.cost_centre_id == CostCentre.cost_centre_purchase(),
|
||||
)
|
||||
.group_by(Batch, Product.full_name)
|
||||
.having(Batch.quantity_remaining != inv_sum)
|
||||
).all()
|
||||
|
||||
issue = []
|
||||
for batch, product, quantity in list_:
|
||||
if quantity >= 0:
|
||||
db.execute(update(Batch).where(Batch.id == batch.id).values(quantity_remaining=quantity))
|
||||
else:
|
||||
issue.append(
|
||||
schemas.BatchIntegrity(
|
||||
id=batch.id,
|
||||
product=product,
|
||||
date=batch.name,
|
||||
showing=batch.quantity_remaining,
|
||||
actual=quantity,
|
||||
price=batch.rate,
|
||||
details=batch_details(batch.id, db),
|
||||
)
|
||||
)
|
||||
return issue
|
||||
|
||||
|
||||
def batch_details(batch_id: uuid.UUID, db: Session) -> List[schemas.BatchIntegrityItem]:
|
||||
list_ = db.execute(
|
||||
select(Voucher.id, Voucher.date, Voucher.type, Inventory.quantity, Inventory.rate)
|
||||
.join(Inventory.voucher)
|
||||
.join(Voucher.journals)
|
||||
.where(
|
||||
Inventory.batch_id == batch_id,
|
||||
Voucher.type.in_(
|
||||
[
|
||||
VoucherType.by_name("Purchase").id,
|
||||
VoucherType.by_name("Purchase Return").id,
|
||||
VoucherType.by_name("Issue").id,
|
||||
VoucherType.by_name("Opening Batches").id,
|
||||
]
|
||||
),
|
||||
Journal.cost_centre_id == CostCentre.cost_centre_purchase(),
|
||||
)
|
||||
).all()
|
||||
return [
|
||||
schemas.BatchIntegrityItem(
|
||||
date=date_,
|
||||
type=VoucherType.by_id(type_).name,
|
||||
url=["/", VoucherType.by_id(type_).name.replace(" ", "-").lower(), str(id_)],
|
||||
quantity=quantity,
|
||||
price=rate,
|
||||
)
|
||||
for id_, date_, type_, quantity, rate in list_
|
||||
]
|
||||
|
||||
|
||||
def batch_dates(db: Session) -> List[schemas.BatchIntegrity]:
|
||||
list_ = (
|
||||
db.execute(
|
||||
select(Batch)
|
||||
.join(Batch.product)
|
||||
.join(Batch.inventories)
|
||||
.join(Inventory.voucher)
|
||||
.where(Voucher.date < Batch.name)
|
||||
.options(contains_eager(Batch.product), contains_eager(Batch.inventories).contains_eager(Inventory.voucher))
|
||||
)
|
||||
.unique()
|
||||
.scalars()
|
||||
.all()
|
||||
)
|
||||
|
||||
issue = []
|
||||
for batch in list_:
|
||||
issue.append(
|
||||
schemas.BatchIntegrity(
|
||||
id=batch.id,
|
||||
product=batch.product.full_name,
|
||||
date=batch.name,
|
||||
showing=batch.quantity_remaining,
|
||||
actual=0,
|
||||
price=batch.rate,
|
||||
details=[
|
||||
schemas.BatchIntegrityItem(
|
||||
date=inv.voucher.date,
|
||||
type=VoucherType.by_id(inv.voucher.type).name,
|
||||
quantity=inv.quantity,
|
||||
price=inv.rate,
|
||||
url=[
|
||||
"/",
|
||||
VoucherType.by_id(inv.voucher.type).name.replace(" ", "-").lower(),
|
||||
str(inv.voucher.id),
|
||||
],
|
||||
)
|
||||
for inv in batch.inventories
|
||||
],
|
||||
)
|
||||
)
|
||||
return issue
|
||||
|
||||
|
||||
def fix_batch_prices(db: Session) -> None:
|
||||
quantities = (
|
||||
db.execute(
|
||||
select(Batch)
|
||||
.join(Batch.product)
|
||||
.join(Batch.inventories)
|
||||
.join(Inventory.voucher)
|
||||
.where(Batch.rate != Inventory.rate)
|
||||
.options(contains_eager(Batch.product), contains_eager(Batch.inventories).contains_eager(Inventory.voucher))
|
||||
)
|
||||
.unique()
|
||||
.scalars()
|
||||
.all()
|
||||
)
|
||||
|
||||
for batch in quantities:
|
||||
for inv in batch.inventories:
|
||||
refresh_voucher(inv.voucher.id, batch.product_id, db)
|
||||
@ -1,18 +1,10 @@
|
||||
import uuid
|
||||
|
||||
from fastapi import APIRouter, Security
|
||||
from sqlalchemy import delete, desc, distinct, func, over, select, update
|
||||
from sqlalchemy import delete, desc, distinct, func, over, select
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from ..core.security import get_current_active_user as get_user
|
||||
from ..db.session import SessionFuture
|
||||
from ..models.attendance import Attendance
|
||||
from ..models.batch import Batch
|
||||
from ..models.cost_centre import CostCentre
|
||||
from ..models.inventory import Inventory
|
||||
from ..models.journal import Journal
|
||||
from ..models.voucher import Voucher
|
||||
from ..models.voucher_type import VoucherType
|
||||
from ..schemas.user import UserToken
|
||||
|
||||
|
||||
@ -24,9 +16,6 @@ def post_check_db(user: UserToken = Security(get_user)):
|
||||
info = {}
|
||||
|
||||
with SessionFuture() as db:
|
||||
info["batches"] = batches(db)
|
||||
info["batchDates"] = batch_dates(db)
|
||||
#TODO: Also check the prices in the batches to errors
|
||||
duplicate_attendances = get_duplicate_attendances(db)
|
||||
if duplicate_attendances > 0:
|
||||
fix_duplicate_attendances(db)
|
||||
@ -65,85 +54,3 @@ def fix_duplicate_attendances(db: Session) -> None:
|
||||
.subquery()
|
||||
)
|
||||
db.execute(delete(Attendance).where(~Attendance.id.in_(sub), Attendance.is_valid == True)) # noqa: E712
|
||||
|
||||
|
||||
def batches(db: Session):
|
||||
quantities = db.execute(
|
||||
select(Inventory.batch_id, func.sum(Inventory.quantity * Journal.debit))
|
||||
.join(Inventory.voucher)
|
||||
.join(Voucher.journals)
|
||||
.where(
|
||||
Voucher.type.in_(
|
||||
[
|
||||
VoucherType.by_name("Purchase").id,
|
||||
VoucherType.by_name("Purchase Return").id,
|
||||
VoucherType.by_name("Issue").id,
|
||||
VoucherType.by_name("Opening Batches").id,
|
||||
]
|
||||
),
|
||||
Journal.cost_centre_id == CostCentre.cost_centre_purchase(),
|
||||
)
|
||||
.group_by(Inventory.batch_id)
|
||||
).all()
|
||||
|
||||
issue = []
|
||||
for batch_id, quantity in quantities:
|
||||
batch = db.execute(select(Batch).where(Batch.id == batch_id)).scalar_one()
|
||||
if batch.quantity_remaining == quantity:
|
||||
continue
|
||||
if quantity >= 0:
|
||||
db.execute(update(Batch).where(Batch.id == batch_id).values(quantity_remaining=quantity))
|
||||
else:
|
||||
issue.append(
|
||||
{
|
||||
"product": batch.product.full_name,
|
||||
"date": batch.name,
|
||||
"showing": batch.quantity_remaining,
|
||||
"actual": quantity,
|
||||
"details": batch_details(batch_id, db),
|
||||
}
|
||||
)
|
||||
return issue
|
||||
|
||||
|
||||
def batch_details(batch_id: uuid.UUID, db: Session):
|
||||
details = db.execute(
|
||||
select(Voucher.date, Voucher.type, Inventory.quantity)
|
||||
.join(Inventory.voucher)
|
||||
.join(Voucher.journals)
|
||||
.where(
|
||||
Inventory.batch_id == batch_id,
|
||||
Voucher.type.in_(
|
||||
[
|
||||
VoucherType.by_name("Purchase").id,
|
||||
VoucherType.by_name("Purchase Return").id,
|
||||
VoucherType.by_name("Issue").id,
|
||||
VoucherType.by_name("Opening Batches").id,
|
||||
]
|
||||
),
|
||||
Journal.cost_centre_id == CostCentre.cost_centre_purchase(),
|
||||
)
|
||||
).all()
|
||||
return [{"date": x.date, "type": VoucherType.by_id(x.type).name, "quantity": x.quantity} for x in details]
|
||||
|
||||
|
||||
def batch_dates(db: Session):
|
||||
quantities = db.execute(
|
||||
select(Batch, Voucher.date, Voucher.type)
|
||||
.join(Batch.product)
|
||||
.join(Batch.inventories)
|
||||
.join(Inventory.voucher)
|
||||
.where(Voucher.date < Batch.name)
|
||||
).all()
|
||||
|
||||
issue = []
|
||||
for batch, date_, type_ in quantities:
|
||||
issue.append(
|
||||
{
|
||||
"product": batch.product.full_name,
|
||||
"batchDate": batch.name,
|
||||
"voucherDate": date_,
|
||||
"type": VoucherType.by_id(type_).name,
|
||||
}
|
||||
)
|
||||
return issue
|
||||
|
||||
@ -327,6 +327,25 @@ def update_journals(
|
||||
item.amount = amount
|
||||
|
||||
|
||||
def refresh_voucher(id_: uuid.UUID, product_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
|
||||
except SQLAlchemyError as e:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
||||
detail=str(e),
|
||||
)
|
||||
|
||||
|
||||
@router.get("/{id_}", response_model=output.Voucher)
|
||||
def get_id(
|
||||
id_: uuid.UUID,
|
||||
|
||||
50
brewman/brewman/schemas/batch_integrity.py
Normal file
50
brewman/brewman/schemas/batch_integrity.py
Normal file
@ -0,0 +1,50 @@
|
||||
import uuid
|
||||
|
||||
from datetime import date, datetime
|
||||
from decimal import Decimal
|
||||
from typing import List
|
||||
|
||||
from pydantic import validator
|
||||
from pydantic.main import BaseModel
|
||||
|
||||
from . import to_camel
|
||||
|
||||
|
||||
class BatchIntegrityItem(BaseModel):
|
||||
date_: date
|
||||
type_: str
|
||||
url: List[str]
|
||||
quantity: Decimal
|
||||
price: Decimal
|
||||
|
||||
class Config:
|
||||
anystr_strip_whitespace = True
|
||||
alias_generator = to_camel
|
||||
json_encoders = {date: lambda v: v.strftime("%d-%b-%Y")}
|
||||
|
||||
@validator("date_", pre=True)
|
||||
def parse_start_date(cls, value):
|
||||
if isinstance(value, date):
|
||||
return value
|
||||
return datetime.strptime(value, "%d-%b-%Y").date()
|
||||
|
||||
|
||||
class BatchIntegrity(BaseModel):
|
||||
id_: uuid.UUID
|
||||
product: str
|
||||
date_: date
|
||||
price: Decimal
|
||||
showing: Decimal
|
||||
actual: Decimal
|
||||
details: List[BatchIntegrityItem]
|
||||
|
||||
class Config:
|
||||
anystr_strip_whitespace = True
|
||||
alias_generator = to_camel
|
||||
json_encoders = {date: lambda v: v.strftime("%d-%b-%Y")}
|
||||
|
||||
@validator("date_", pre=True)
|
||||
def parse_start_date(cls, value):
|
||||
if isinstance(value, date):
|
||||
return value
|
||||
return datetime.strptime(value, "%d-%b-%Y").date()
|
||||
Reference in New Issue
Block a user