2012-05-01 21:40:01 +00:00
|
|
|
import datetime
|
2017-02-09 18:39:32 +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
|
2012-05-01 21:40:01 +00:00
|
|
|
from sqlalchemy.orm.util import aliased
|
2020-10-07 15:18:43 +00:00
|
|
|
from sqlalchemy.sql.expression import desc, func
|
2012-05-01 21:40:01 +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 AccountBase, AccountType
|
|
|
|
from ...models.voucher import Journal, Voucher, VoucherType
|
|
|
|
from ...schemas.auth import UserToken
|
|
|
|
|
2012-10-16 18:51:43 +00:00
|
|
|
|
2020-05-08 04:52:25 +00:00
|
|
|
router = APIRouter()
|
2012-10-16 18:51:43 +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 05:56:28 +00:00
|
|
|
@router.get("", response_model=schemas.CashFlow)
|
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=["cash-flow"]),
|
2020-05-11 22:22:25 +00:00
|
|
|
):
|
2019-05-10 03:49:53 +00:00
|
|
|
return {
|
2020-05-12 04:25:33 +00:00
|
|
|
"startDate": get_start_date(request.session),
|
|
|
|
"finishDate": get_finish_date(request.session),
|
2020-05-14 05:56:28 +00:00
|
|
|
"body": {"operating": [], "investing": [], "financing": [], "details": []},
|
|
|
|
"footer": None,
|
2019-05-10 03:49:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-05-11 22:22:25 +00:00
|
|
|
@router.get("/data")
|
|
|
|
def report_data(
|
|
|
|
request: Request,
|
|
|
|
s: str = None,
|
|
|
|
f: str = None,
|
|
|
|
db: Session = Depends(get_db),
|
|
|
|
user: UserToken = Security(get_user, scopes=["cash-flow"]),
|
|
|
|
):
|
|
|
|
body, footer = build_report(s, f, db)
|
2020-05-12 04:25:33 +00:00
|
|
|
set_period(s, f, request.session)
|
2019-05-10 03:49:53 +00:00
|
|
|
return {
|
2020-05-11 22:22:25 +00:00
|
|
|
"startDate": s,
|
|
|
|
"finishDate": f,
|
2019-05-10 03:49:53 +00:00
|
|
|
"body": body,
|
|
|
|
"footer": footer,
|
|
|
|
}
|
2019-04-06 04:13:12 +00:00
|
|
|
|
|
|
|
|
2020-05-11 22:22:25 +00:00
|
|
|
@router.get("/{id_}")
|
|
|
|
def report_id(
|
|
|
|
id_: int,
|
|
|
|
request: Request,
|
|
|
|
s: str = None,
|
|
|
|
f: str = None,
|
|
|
|
db: Session = Depends(get_db),
|
|
|
|
user: UserToken = Security(get_user, scopes=["cash-flow"]),
|
|
|
|
):
|
|
|
|
details, footer = build_report_id(id_, s, f, db)
|
2020-05-12 04:25:33 +00:00
|
|
|
set_period(s, f, request.session)
|
2019-05-10 03:49:53 +00:00
|
|
|
return {
|
2020-05-11 22:22:25 +00:00
|
|
|
"startDate": s,
|
|
|
|
"finishDate": f,
|
2019-05-10 03:49:53 +00:00
|
|
|
"body": {"details": details},
|
|
|
|
"footer": footer,
|
|
|
|
}
|
2012-05-01 21:40:01 +00:00
|
|
|
|
|
|
|
|
2020-05-11 22:22:25 +00:00
|
|
|
def build_report(start_date, finish_date, db):
|
2012-05-01 21:40:01 +00:00
|
|
|
sub_voucher = aliased(Voucher)
|
|
|
|
sub_journal = aliased(Journal)
|
2018-07-07 11:01:44 +00:00
|
|
|
sub_account = aliased(AccountBase)
|
2012-05-01 21:40:01 +00:00
|
|
|
|
2019-04-06 04:13:12 +00:00
|
|
|
sub_query = (
|
2020-05-11 22:22:25 +00:00
|
|
|
db.query(sub_voucher.id)
|
2019-04-06 04:13:12 +00:00
|
|
|
.join(sub_journal, sub_voucher.journals)
|
|
|
|
.join(sub_account, sub_journal.account)
|
|
|
|
.filter(sub_account.type == AccountType.by_name("Cash").id)
|
|
|
|
.filter(sub_voucher.date >= datetime.datetime.strptime(start_date, "%d-%b-%Y"))
|
|
|
|
.filter(sub_voucher.date <= datetime.datetime.strptime(finish_date, "%d-%b-%Y"))
|
|
|
|
.subquery()
|
|
|
|
)
|
|
|
|
|
|
|
|
query = (
|
2020-05-11 22:22:25 +00:00
|
|
|
db.query(AccountBase.type, func.sum(Journal.signed_amount))
|
2019-04-06 04:13:12 +00:00
|
|
|
.join(Journal, Voucher.journals)
|
|
|
|
.join(AccountBase, Journal.account)
|
|
|
|
.filter(Voucher.id.in_(sub_query))
|
|
|
|
.filter(AccountBase.type != AccountType.by_name("Cash").id)
|
|
|
|
.group_by(AccountBase.type)
|
|
|
|
.order_by(func.sum(Journal.signed_amount))
|
|
|
|
.all()
|
|
|
|
)
|
2017-02-06 07:39:15 +00:00
|
|
|
|
|
|
|
total_amount = 0
|
2019-04-06 04:13:12 +00:00
|
|
|
cf = {"operating": [], "investing": [], "financing": []}
|
2018-07-07 11:01:44 +00:00
|
|
|
for account_type, amount in query:
|
|
|
|
lt = AccountType.by_id(account_type)
|
2019-04-06 04:13:12 +00:00
|
|
|
total_amount += amount * -1
|
2018-05-25 13:49:00 +00:00
|
|
|
cf[lt.cash_flow_classification.lower()].append(
|
2020-10-07 15:18:43 +00:00
|
|
|
{
|
|
|
|
"name": lt.name,
|
|
|
|
"url": ["/", "cash-flow", str(lt.id)],
|
|
|
|
"amount": amount * -1,
|
|
|
|
}
|
2017-02-06 07:39:15 +00:00
|
|
|
)
|
2019-04-06 04:13:12 +00:00
|
|
|
|
|
|
|
opening = (
|
2020-05-11 22:22:25 +00:00
|
|
|
db.query(func.sum(Journal.amount * Journal.debit))
|
2019-04-06 04:13:12 +00:00
|
|
|
.join(Journal.voucher)
|
|
|
|
.join(Journal.account)
|
|
|
|
.filter(Voucher.date < start_date)
|
|
|
|
.filter(Voucher.type != VoucherType.by_name("Issue").id)
|
|
|
|
.filter(AccountBase.type == AccountType.by_name("Cash").id)
|
|
|
|
.scalar()
|
|
|
|
)
|
|
|
|
|
|
|
|
closing = (
|
2020-05-11 22:22:25 +00:00
|
|
|
db.query(func.sum(Journal.amount * Journal.debit))
|
2019-04-06 04:13:12 +00:00
|
|
|
.join(Journal.voucher)
|
|
|
|
.join(Journal.account)
|
|
|
|
.filter(Voucher.date <= finish_date)
|
|
|
|
.filter(Voucher.type != VoucherType.by_name("Issue").id)
|
|
|
|
.filter(AccountBase.type == AccountType.by_name("Cash").id)
|
|
|
|
.scalar()
|
|
|
|
)
|
|
|
|
|
2019-05-10 03:49:53 +00:00
|
|
|
return (
|
|
|
|
cf,
|
|
|
|
[
|
2020-10-07 15:18:43 +00:00
|
|
|
{
|
|
|
|
"name": "Net increase in cash and cash equivalents",
|
|
|
|
"amount": total_amount,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "Cash and cash equivalents at beginning of period",
|
|
|
|
"amount": opening,
|
|
|
|
},
|
2019-05-10 03:49:53 +00:00
|
|
|
{"name": "Cash and cash equivalents at end of period", "amount": closing},
|
|
|
|
],
|
|
|
|
)
|
2012-05-01 21:40:01 +00:00
|
|
|
|
|
|
|
|
2020-05-11 22:22:25 +00:00
|
|
|
def build_report_id(account_type, start_date, finish_date, db):
|
2019-05-10 03:49:53 +00:00
|
|
|
details = []
|
2012-05-01 21:40:01 +00:00
|
|
|
sub_voucher = aliased(Voucher)
|
|
|
|
sub_journal = aliased(Journal)
|
2018-07-07 11:01:44 +00:00
|
|
|
sub_account = aliased(AccountBase)
|
2012-05-01 21:40:01 +00:00
|
|
|
|
2019-04-06 04:13:12 +00:00
|
|
|
sub_query = (
|
2020-05-11 22:22:25 +00:00
|
|
|
db.query(sub_voucher.id)
|
2019-04-06 04:13:12 +00:00
|
|
|
.join(sub_journal, sub_voucher.journals)
|
|
|
|
.join(sub_account, sub_journal.account)
|
|
|
|
.filter(sub_account.type == AccountType.by_name("Cash").id)
|
|
|
|
.filter(sub_voucher.date >= datetime.datetime.strptime(start_date, "%d-%b-%Y"))
|
|
|
|
.filter(sub_voucher.date <= datetime.datetime.strptime(finish_date, "%d-%b-%Y"))
|
|
|
|
.subquery()
|
|
|
|
)
|
|
|
|
|
|
|
|
query = (
|
2020-05-11 22:22:25 +00:00
|
|
|
db.query(AccountBase, func.sum(Journal.signed_amount))
|
2019-04-06 04:13:12 +00:00
|
|
|
.join(Journal, Voucher.journals)
|
|
|
|
.join(AccountBase, Journal.account)
|
|
|
|
.filter(Voucher.id.in_(sub_query))
|
|
|
|
.filter(AccountBase.type == account_type)
|
|
|
|
.group_by(AccountBase)
|
|
|
|
.order_by(desc(func.sum(Journal.amount)))
|
|
|
|
.all()
|
|
|
|
)
|
2016-12-24 11:41:01 +00:00
|
|
|
|
2017-02-06 07:39:15 +00:00
|
|
|
total_amount = 0
|
2018-07-07 11:01:44 +00:00
|
|
|
for account, amount in query:
|
2019-04-06 04:13:12 +00:00
|
|
|
total_amount += amount * -1
|
2019-05-10 03:49:53 +00:00
|
|
|
details.append(
|
2020-10-07 15:18:43 +00:00
|
|
|
{
|
|
|
|
"name": account.name,
|
|
|
|
"url": ["/", "ledger", str(account.id)],
|
|
|
|
"amount": amount * -1,
|
|
|
|
}
|
2017-02-06 07:39:15 +00:00
|
|
|
)
|
|
|
|
|
2019-05-10 03:49:53 +00:00
|
|
|
return details, [{"name": "total", "amount": total_amount}]
|