Moving to FastAPI

Config is not properly working with .env file as of now
Cost Centre is mostly working, going to work on Accounts now
This commit is contained in:
tanshu committed 2020-05-08 10:22:25 +05:30
1 parent 72044476a8
commit fe74ef44bf
89 files changed
+1516 -2421

No files matched your search

+132
View File
@@ -0,0 +1,132 @@
import datetime
from sqlalchemy.sql.expression import func, desc
from brewman.models.master import AccountType, AccountBase
from brewman.models.voucher import Voucher, Journal, VoucherType
from brewman.routers.reports.closing_stock import get_opening_stock, get_closing_stock
from brewman.routers.services.session import (
session_period_set,
session_period_start,
session_period_finish,
)
from fastapi import APIRouter
router = APIRouter()
@router.get("/api/profit-loss", permission="Profit & Loss")
def report_blank(request):
return {
"startDate": session_period_start(request),
"finishDate": session_period_finish(request),
"body": [],
"footer": {},
}
@router.get("/api/profit-loss", request_param=["s", "f"], permission="Profit & Loss")
def report_data(request):
start_date = request.GET["s"]
finish_date = request.GET["f"]
body, footer = build_profit_loss(start_date, finish_date, request.dbsession)
session_period_set(start_date, finish_date, request)
return {
"startDate": start_date,
"finishDate": finish_date,
"body": body,
"footer": footer,
}
def build_profit_loss(start_date, finish_date, dbsession):
start_date = datetime.datetime.strptime(start_date, "%d-%b-%Y")
finish_date = datetime.datetime.strptime(finish_date, "%d-%b-%Y")
profit_type_list = [i.id for i in AccountType.list() if i.balance_sheet == False]
report = []
groups = {}
amount_sum = func.sum(Journal.amount * Journal.debit)
query = (
dbsession.query(AccountBase, amount_sum)
.join(Journal.voucher)
.join(Journal.account)
.filter(Voucher.date >= start_date)
.filter(Voucher.date <= finish_date)
.filter(Voucher.type != VoucherType.by_name("Issue").id)
.filter(AccountBase.type.in_(profit_type_list))
.group_by(AccountBase)
.order_by(AccountBase.type)
.order_by(desc(func.abs(amount_sum)))
.all()
)
# Get opening / closing stock
opening_stock = get_opening_stock(start_date, dbsession)
closing_stock = get_closing_stock(finish_date, dbsession)
total_amount = (opening_stock - closing_stock) * -1
report.append(
{"name": "Opening Stock", "amount": opening_stock * -1, "order": 20.0001}
)
report.append({"name": "Closing Stock", "amount": closing_stock, "order": 29})
purchase_group = AccountType.by_id(2)
groups[purchase_group.id] = {
"group": purchase_group.name,
"total": total_amount,
"order": purchase_group.order,
}
counter = 0
for account, amount in query:
# Add Items
amount *= -1
account_type = AccountType.by_id(account.type)
total_amount += amount
if amount != 0:
counter += 0.001
report.append(
{
"name": account.name,
"amount": amount,
"order": account_type.order + counter,
}
)
if account_type.id in groups:
groups[account_type.id]["total"] += amount
else:
groups[account_type.id] = {
"group": account_type.name,
"total": amount,
"order": account_type.order,
}
# Add Subtotals
for item in groups.values():
report.append(item)
# Add Net
footer = {
"group": "Net Profit" if total_amount > 0 else "Net Loss",
"total": total_amount,
"order": 100,
}
return sorted(report, key=lambda d: d["order"]), footer
def get_accumulated_profit(finish_date, dbsession):
type_list = [i.id for i in AccountType.list() if i.balance_sheet is False]
accumulated_profit = (
dbsession.query(func.sum(Journal.amount * Journal.debit))
.join(Journal.voucher)
.join(Journal.account)
.filter(Voucher.date <= finish_date)
.filter(Voucher.type != VoucherType.by_name("Issue").id)
.filter(AccountBase.type.in_(type_list))
.scalar()
)
return 0 if accumulated_profit is None else accumulated_profit