2013-10-14 09:33:26 +00:00
|
|
|
import datetime
|
2018-05-25 13:49:00 +00:00
|
|
|
|
2020-05-12 03:53:27 +00:00
|
|
|
from fastapi import APIRouter, Depends, Security, Request
|
|
|
|
from sqlalchemy.orm import Session
|
2018-05-25 13:49:00 +00:00
|
|
|
from sqlalchemy.sql.expression import func
|
2013-10-14 09:33:26 +00:00
|
|
|
|
2020-05-12 03:53:27 +00:00
|
|
|
from ...schemas.auth import UserToken
|
|
|
|
from ...core.security import get_current_active_user as get_user
|
|
|
|
from ...db.session import SessionLocal
|
2015-02-12 12:12:46 +00:00
|
|
|
from brewman.models.master import Product, CostCentre
|
2013-10-14 09:33:26 +00:00
|
|
|
from brewman.models.voucher import Voucher, Journal, VoucherType, Inventory
|
2020-05-08 04:52:25 +00:00
|
|
|
from brewman.routers.services.session import (
|
2019-05-10 03:49:53 +00:00
|
|
|
session_period_set,
|
|
|
|
session_period_start,
|
|
|
|
session_period_finish,
|
|
|
|
)
|
2013-10-14 09:33:26 +00:00
|
|
|
|
2020-05-08 04:52:25 +00:00
|
|
|
router = APIRouter()
|
2013-10-14 09:33:26 +00:00
|
|
|
|
|
|
|
|
2020-05-12 03:53:27 +00:00
|
|
|
# Dependency
|
|
|
|
def get_db() -> Session:
|
|
|
|
try:
|
|
|
|
db = SessionLocal()
|
|
|
|
yield db
|
|
|
|
finally:
|
|
|
|
db.close()
|
|
|
|
|
|
|
|
|
|
|
|
@router.get("")
|
|
|
|
def report_blank(
|
|
|
|
request: Request,
|
|
|
|
user: UserToken = Security(get_user, scopes=["stock-movement"]),
|
|
|
|
):
|
2019-05-10 03:49:53 +00:00
|
|
|
return {
|
2020-05-12 03:53:27 +00:00
|
|
|
"startDate": session_period_start(request.session),
|
|
|
|
"finishDate": session_period_finish(request.session),
|
2019-05-10 03:49:53 +00:00
|
|
|
"body": [],
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-05-12 03:53:27 +00:00
|
|
|
@router.get("/{start}/{finish}")
|
|
|
|
def report_data(
|
|
|
|
start: str,
|
|
|
|
finish: str,
|
|
|
|
request: Request,
|
|
|
|
db: Session = Depends(get_db),
|
|
|
|
user: UserToken = Security(get_user, scopes=["stock-movement"]),
|
|
|
|
):
|
|
|
|
body = build_stock_movement(start, finish, db)
|
|
|
|
session_period_set( start, finish, request.session)
|
|
|
|
return {"startDate": start, "finishDate": finish, "body": body}
|
2013-10-14 09:33:26 +00:00
|
|
|
|
|
|
|
|
2020-05-12 03:53:27 +00:00
|
|
|
def build_stock_movement(start_date, finish_date, db):
|
2019-05-10 03:49:53 +00:00
|
|
|
start_date = datetime.datetime.strptime(start_date, "%d-%b-%Y")
|
|
|
|
finish_date = datetime.datetime.strptime(finish_date, "%d-%b-%Y")
|
|
|
|
dict_ = {}
|
2019-04-06 04:13:12 +00:00
|
|
|
quantity_sum = func.sum(Journal.debit * Inventory.quantity).label("quantity")
|
|
|
|
openings = (
|
2020-05-12 03:53:27 +00:00
|
|
|
db.query(Product, quantity_sum)
|
2019-04-06 04:13:12 +00:00
|
|
|
.join(Product.inventories)
|
|
|
|
.join(Inventory.voucher)
|
|
|
|
.join(Voucher.journals)
|
|
|
|
.filter(Voucher.date < start_date)
|
|
|
|
.filter(Journal.cost_centre_id == CostCentre.cost_centre_purchase())
|
|
|
|
.group_by(Product)
|
|
|
|
.all()
|
|
|
|
)
|
2013-10-14 09:33:26 +00:00
|
|
|
for product, quantity in openings:
|
2019-05-10 03:49:53 +00:00
|
|
|
dict_[product.id] = {
|
2019-04-06 04:13:12 +00:00
|
|
|
"id": product.id,
|
|
|
|
"name": product.full_name,
|
|
|
|
"group": product.product_group.name,
|
|
|
|
"opening": quantity,
|
|
|
|
}
|
|
|
|
purchases = (
|
2020-05-12 03:53:27 +00:00
|
|
|
db.query(Product, quantity_sum)
|
2019-04-06 04:13:12 +00:00
|
|
|
.join(Product.inventories)
|
|
|
|
.join(Inventory.voucher)
|
|
|
|
.join(Voucher.journals)
|
|
|
|
.filter(Voucher.date >= start_date)
|
|
|
|
.filter(Voucher.date <= finish_date)
|
|
|
|
.filter(Voucher.type != VoucherType.by_name("Issue").id)
|
|
|
|
.filter(Journal.cost_centre_id == CostCentre.cost_centre_purchase())
|
|
|
|
.group_by(Product)
|
|
|
|
.all()
|
|
|
|
)
|
2013-10-14 09:33:26 +00:00
|
|
|
for product, quantity in purchases:
|
2019-05-10 03:49:53 +00:00
|
|
|
if product.id in dict_:
|
|
|
|
dict_[product.id]["purchase"] = quantity
|
2013-10-14 09:33:26 +00:00
|
|
|
else:
|
2019-05-10 03:49:53 +00:00
|
|
|
dict_[product.id] = {
|
2019-04-06 04:13:12 +00:00
|
|
|
"id": product.id,
|
|
|
|
"name": product.full_name,
|
|
|
|
"group": product.product_group.name,
|
|
|
|
"purchase": quantity,
|
|
|
|
}
|
|
|
|
issues = (
|
2020-05-12 03:53:27 +00:00
|
|
|
db.query(Product, quantity_sum)
|
2019-04-06 04:13:12 +00:00
|
|
|
.join(Product.inventories)
|
|
|
|
.join(Inventory.voucher)
|
|
|
|
.join(Voucher.journals)
|
|
|
|
.filter(Voucher.date >= start_date)
|
|
|
|
.filter(Voucher.date <= finish_date)
|
|
|
|
.filter(Voucher.type == VoucherType.by_name("Issue").id)
|
|
|
|
.filter(Journal.cost_centre_id == CostCentre.cost_centre_purchase())
|
|
|
|
.group_by(Product)
|
|
|
|
.all()
|
|
|
|
)
|
2013-10-14 09:33:26 +00:00
|
|
|
for product, quantity in issues:
|
2019-05-10 03:49:53 +00:00
|
|
|
if product.id in dict_:
|
|
|
|
dict_[product.id]["issue"] = quantity * -1
|
2013-10-14 09:33:26 +00:00
|
|
|
else:
|
2019-05-10 03:49:53 +00:00
|
|
|
dict_[product.id] = {
|
2019-04-06 04:13:12 +00:00
|
|
|
"id": product.id,
|
|
|
|
"name": product.full_name,
|
|
|
|
"group": product.product_group.name,
|
|
|
|
"issue": quantity * -1,
|
|
|
|
}
|
2013-10-14 09:33:26 +00:00
|
|
|
|
2019-05-10 03:49:53 +00:00
|
|
|
list_ = [value for key, value in dict_.items()]
|
|
|
|
list_ = sorted(list_, key=lambda x: x["name"].lower())
|
|
|
|
list_ = sorted(list_, key=lambda x: x["group"].lower())
|
|
|
|
for i in range(len(list_), 0, -1):
|
|
|
|
item = list_[i - 1]
|
2019-04-06 04:13:12 +00:00
|
|
|
opening = item["opening"] if "opening" in item else 0
|
|
|
|
purchase = item["purchase"] if "purchase" in item else 0
|
|
|
|
issue = item["issue"] if "issue" in item else 0
|
2013-10-14 09:33:26 +00:00
|
|
|
closing = opening + purchase - issue
|
|
|
|
if opening == 0 and purchase == 0 and issue == 0:
|
2019-05-10 03:49:53 +00:00
|
|
|
list_.remove(item)
|
2013-10-14 09:33:26 +00:00
|
|
|
else:
|
2019-04-06 04:13:12 +00:00
|
|
|
item["closing"] = closing
|
2019-05-10 03:49:53 +00:00
|
|
|
return list_
|