2020-10-07 15:18:43 +00:00
|
|
|
from datetime import date, datetime
|
2018-05-25 13:49:00 +00:00
|
|
|
|
2020-10-07 15:18:43 +00:00
|
|
|
import brewman.schemas.reports as schemas
|
|
|
|
|
|
|
|
from fastapi import APIRouter, Depends, Request, Security
|
2020-05-11 21:27:48 +00:00
|
|
|
from sqlalchemy.orm import Session
|
2018-05-25 13:49:00 +00:00
|
|
|
from sqlalchemy.sql.expression import func
|
2012-10-22 13:53:58 +00:00
|
|
|
|
2020-05-11 21:27:48 +00:00
|
|
|
from ...core.security import get_current_active_user as get_user
|
2020-10-07 15:18:43 +00:00
|
|
|
from ...core.session import get_finish_date, get_start_date, set_period
|
2020-05-11 21:27:48 +00:00
|
|
|
from ...db.session import SessionLocal
|
2020-10-07 15:18:43 +00:00
|
|
|
from ...models.master import CostCentre, Product
|
|
|
|
from ...models.voucher import Inventory, Journal, Voucher
|
|
|
|
from ...schemas.auth import UserToken
|
|
|
|
|
2013-09-27 22:11:57 +00:00
|
|
|
|
2020-05-08 04:52:25 +00:00
|
|
|
router = APIRouter()
|
2012-10-22 13:53:58 +00:00
|
|
|
|
|
|
|
|
2020-05-11 21:27:48 +00:00
|
|
|
# Dependency
|
|
|
|
def get_db() -> Session:
|
|
|
|
try:
|
|
|
|
db = SessionLocal()
|
|
|
|
yield db
|
|
|
|
finally:
|
|
|
|
db.close()
|
|
|
|
|
|
|
|
|
2020-05-14 06:48:45 +00:00
|
|
|
@router.get("", response_model=schemas.ClosingStock)
|
2020-05-11 21:27:48 +00:00
|
|
|
def report_blank(
|
2020-10-07 15:18:43 +00:00
|
|
|
request: Request,
|
|
|
|
user: UserToken = Security(get_user, scopes=["closing-stock"]),
|
2020-05-11 21:27:48 +00:00
|
|
|
):
|
2020-05-12 04:25:33 +00:00
|
|
|
return {"date": get_finish_date(request.session), "body": []}
|
2012-10-22 13:53:58 +00:00
|
|
|
|
|
|
|
|
2020-05-14 06:48:45 +00:00
|
|
|
@router.get("/{date_}", response_model=schemas.ClosingStock)
|
2020-05-11 21:27:48 +00:00
|
|
|
def report_data(
|
2020-05-14 06:48:45 +00:00
|
|
|
date_: str,
|
2020-05-11 21:27:48 +00:00
|
|
|
request: Request,
|
|
|
|
db: Session = Depends(get_db),
|
|
|
|
user: UserToken = Security(get_user, scopes=["closing-stock"]),
|
|
|
|
):
|
2020-05-14 06:48:45 +00:00
|
|
|
set_period(get_start_date(request.session), date_, request.session)
|
2020-05-31 09:11:11 +00:00
|
|
|
return {
|
|
|
|
"date": date_,
|
|
|
|
"body": build_report(datetime.strptime(date_, "%d-%b-%Y").date(), db),
|
|
|
|
}
|
2012-10-22 13:53:58 +00:00
|
|
|
|
|
|
|
|
2020-05-14 06:48:45 +00:00
|
|
|
def build_report(date_: date, db: Session):
|
2020-10-07 16:59:24 +00:00
|
|
|
amount_sum = func.sum(
|
|
|
|
Journal.debit * Inventory.quantity * Inventory.rate * (1 + Inventory.tax)
|
|
|
|
).label("amount")
|
2019-04-06 04:13:12 +00:00
|
|
|
quantity_sum = func.sum(Journal.debit * Inventory.quantity).label("quantity")
|
|
|
|
query = (
|
2020-05-11 21:27:48 +00:00
|
|
|
db.query(Product, quantity_sum, amount_sum)
|
2019-04-06 04:13:12 +00:00
|
|
|
.join(Product.inventories)
|
|
|
|
.join(Inventory.voucher)
|
|
|
|
.join(Voucher.journals)
|
2020-05-14 06:48:45 +00:00
|
|
|
.filter(Voucher.date <= date_)
|
2019-04-06 04:13:12 +00:00
|
|
|
.filter(Journal.cost_centre_id == CostCentre.cost_centre_purchase())
|
|
|
|
.group_by(Product)
|
|
|
|
.order_by(amount_sum.desc())
|
|
|
|
.all()
|
|
|
|
)
|
2012-10-22 13:53:58 +00:00
|
|
|
|
|
|
|
body = []
|
|
|
|
for product, quantity, amount in query:
|
|
|
|
if quantity != 0 and amount != 0:
|
2019-04-06 04:13:12 +00:00
|
|
|
body.append(
|
|
|
|
{
|
|
|
|
"product": product.full_name,
|
|
|
|
"group": product.product_group.name,
|
|
|
|
"quantity": quantity,
|
|
|
|
"amount": amount,
|
|
|
|
}
|
|
|
|
)
|
2013-09-27 22:11:57 +00:00
|
|
|
return body
|
|
|
|
|
|
|
|
|
2020-05-14 06:48:45 +00:00
|
|
|
def get_opening_stock(date_: date, db: Session):
|
2019-04-06 04:13:12 +00:00
|
|
|
opening_stock = (
|
2020-10-07 16:59:24 +00:00
|
|
|
db.query(
|
|
|
|
func.sum(
|
|
|
|
Inventory.quantity
|
|
|
|
* Inventory.rate
|
|
|
|
* (1 + Inventory.tax)
|
|
|
|
* Journal.debit
|
|
|
|
)
|
|
|
|
)
|
2019-04-06 04:13:12 +00:00
|
|
|
.join(Journal.voucher)
|
|
|
|
.join(Journal.account)
|
|
|
|
.join(Voucher.inventories)
|
2020-05-14 06:48:45 +00:00
|
|
|
.filter(Voucher.date < date_)
|
2019-04-06 04:13:12 +00:00
|
|
|
.filter(Journal.cost_centre_id == CostCentre.cost_centre_purchase())
|
|
|
|
.scalar()
|
|
|
|
)
|
2013-09-27 22:11:57 +00:00
|
|
|
return 0 if opening_stock is None else opening_stock
|
|
|
|
|
|
|
|
|
2020-05-14 06:48:45 +00:00
|
|
|
def get_closing_stock(date_, db: Session):
|
2019-04-06 04:13:12 +00:00
|
|
|
closing_stock = (
|
2020-10-07 16:59:24 +00:00
|
|
|
db.query(
|
|
|
|
func.sum(
|
|
|
|
Inventory.quantity
|
|
|
|
* Inventory.rate
|
|
|
|
* (1 + Inventory.tax)
|
|
|
|
* Journal.debit
|
|
|
|
)
|
|
|
|
)
|
2019-04-06 04:13:12 +00:00
|
|
|
.join(Journal.voucher)
|
|
|
|
.join(Journal.account)
|
|
|
|
.join(Voucher.inventories)
|
2020-05-14 06:48:45 +00:00
|
|
|
.filter(Voucher.date <= date_)
|
2019-04-06 04:13:12 +00:00
|
|
|
.filter(Journal.cost_centre_id == CostCentre.cost_centre_purchase())
|
|
|
|
.scalar()
|
|
|
|
)
|
2013-09-27 22:11:57 +00:00
|
|
|
return 0 if closing_stock is None else closing_stock
|