2013-10-30 07:20:32 +00:00
|
|
|
import 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 22:22:25 +00:00
|
|
|
from sqlalchemy.orm import Session
|
2020-10-07 15:18:43 +00:00
|
|
|
from sqlalchemy.sql.expression import desc, func
|
2013-10-30 07:20:32 +00:00
|
|
|
|
2020-05-11 22:22:25 +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 22:22:25 +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, VoucherType
|
|
|
|
from ...schemas.auth import UserToken
|
2020-05-11 22:22:25 +00:00
|
|
|
|
2013-10-30 07:20:32 +00:00
|
|
|
|
2020-05-08 04:52:25 +00:00
|
|
|
router = APIRouter()
|
2013-10-30 07:20:32 +00:00
|
|
|
|
|
|
|
|
2020-05-11 22:22:25 +00:00
|
|
|
# Dependency
|
|
|
|
def get_db() -> Session:
|
|
|
|
try:
|
|
|
|
db = SessionLocal()
|
|
|
|
yield db
|
|
|
|
finally:
|
|
|
|
db.close()
|
|
|
|
|
|
|
|
|
2020-05-14 09:29:16 +00:00
|
|
|
@router.get("", response_model=schemas.Purchases)
|
2020-05-11 22:22:25 +00:00
|
|
|
def report_blank(
|
2020-10-07 15:18:43 +00:00
|
|
|
request: Request,
|
|
|
|
user: UserToken = Security(get_user, scopes=["purchases"]),
|
2020-05-11 22:22:25 +00:00
|
|
|
):
|
2020-05-14 09:29:16 +00:00
|
|
|
return schemas.Purchases(
|
2020-10-07 15:18:43 +00:00
|
|
|
startDate=get_start_date(request.session),
|
|
|
|
finishDate=get_finish_date(request.session),
|
|
|
|
body=[],
|
|
|
|
footer=None,
|
2020-05-14 09:29:16 +00:00
|
|
|
)
|
2013-10-30 07:20:32 +00:00
|
|
|
|
|
|
|
|
2020-05-14 09:29:16 +00:00
|
|
|
@router.get("/{start}/{finish}", response_model=schemas.Purchases)
|
2020-05-11 22:22:25 +00:00
|
|
|
def report_data(
|
|
|
|
start: str,
|
|
|
|
finish: str,
|
|
|
|
request: Request,
|
|
|
|
db: Session = Depends(get_db),
|
|
|
|
user: UserToken = Security(get_user, scopes=["purchases"]),
|
|
|
|
):
|
|
|
|
body, footer = build_report(start, finish, db)
|
2020-05-12 04:25:33 +00:00
|
|
|
set_period(start, finish, request.session)
|
2020-10-07 15:18:43 +00:00
|
|
|
return schemas.Purchases(
|
|
|
|
startDate=start,
|
|
|
|
finishDate=finish,
|
|
|
|
body=body,
|
|
|
|
footer=footer,
|
|
|
|
)
|
2019-04-06 04:13:12 +00:00
|
|
|
|
2019-05-10 03:49:53 +00:00
|
|
|
|
2020-05-11 22:22:25 +00:00
|
|
|
def build_report(start_date, finish_date, db):
|
2019-05-10 03:49:53 +00:00
|
|
|
body = []
|
2019-04-06 04:13:12 +00:00
|
|
|
quantity_sum = func.sum(Journal.debit * Inventory.quantity).label("quantity")
|
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
|
|
|
query = (
|
2020-05-11 22:22:25 +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)
|
|
|
|
.filter(Voucher.date >= datetime.datetime.strptime(start_date, "%d-%b-%Y"))
|
|
|
|
.filter(Voucher.date <= datetime.datetime.strptime(finish_date, "%d-%b-%Y"))
|
|
|
|
.filter(Voucher.type != VoucherType.by_name("Issue").id)
|
|
|
|
.filter(Journal.cost_centre_id == CostCentre.cost_centre_purchase())
|
|
|
|
.group_by(Product)
|
|
|
|
.order_by(desc(amount_sum))
|
|
|
|
.all()
|
|
|
|
)
|
2013-10-30 07:20:32 +00:00
|
|
|
|
|
|
|
total_amount = 0
|
|
|
|
for product, quantity, amount in query:
|
|
|
|
rate = amount / quantity if quantity != 0 else 0
|
|
|
|
total_amount += amount
|
2020-05-14 09:29:16 +00:00
|
|
|
row = schemas.PurchasesItem(
|
|
|
|
name=product.full_name,
|
|
|
|
quantity=quantity,
|
|
|
|
rate=rate,
|
|
|
|
amount=amount,
|
|
|
|
url=["/", "product-ledger", str(product.id)],
|
|
|
|
)
|
2019-05-10 03:49:53 +00:00
|
|
|
body.append(row)
|
2020-05-14 09:29:16 +00:00
|
|
|
return (
|
|
|
|
body,
|
2020-10-07 16:59:24 +00:00
|
|
|
schemas.PurchasesItem(
|
|
|
|
name="Total", quantity=0, rate=0, url=[], amount=total_amount
|
|
|
|
),
|
2020-05-14 09:29:16 +00:00
|
|
|
)
|