2020-05-14 11:03:08 +00:00
|
|
|
from typing import List
|
|
|
|
|
2020-10-07 15:18:43 +00:00
|
|
|
import brewman.schemas.reports as schemas
|
|
|
|
|
|
|
|
from fastapi import APIRouter, Depends, Request, Security
|
|
|
|
from sqlalchemy.orm import Session, joinedload_all
|
2012-05-01 21:40:01 +00:00
|
|
|
|
2020-05-12 03:53:27 +00:00
|
|
|
from ...core.security import get_current_active_user as get_user
|
|
|
|
from ...db.session import SessionLocal
|
2020-10-07 15:18:43 +00:00
|
|
|
from ...models.voucher import Journal, Voucher, VoucherType
|
|
|
|
from ...schemas.auth import UserToken
|
|
|
|
|
2016-12-24 11:41:01 +00:00
|
|
|
|
2020-05-08 04:52:25 +00:00
|
|
|
router = APIRouter()
|
2012-05-01 21:40:01 +00:00
|
|
|
|
|
|
|
|
2020-05-12 03:53:27 +00:00
|
|
|
# Dependency
|
|
|
|
def get_db() -> Session:
|
|
|
|
try:
|
|
|
|
db = SessionLocal()
|
|
|
|
yield db
|
|
|
|
finally:
|
|
|
|
db.close()
|
|
|
|
|
|
|
|
|
2020-05-14 11:03:08 +00:00
|
|
|
@router.get("", response_model=List[schemas.Unposted])
|
2020-05-12 03:53:27 +00:00
|
|
|
def report_data(
|
2020-10-07 15:18:43 +00:00
|
|
|
request: Request,
|
|
|
|
db: Session = Depends(get_db),
|
|
|
|
user: UserToken = Security(get_user, scopes=["post-vouchers"]),
|
2020-05-12 03:53:27 +00:00
|
|
|
):
|
|
|
|
return build_report(db)
|
2012-05-01 21:40:01 +00:00
|
|
|
|
2016-12-24 11:41:01 +00:00
|
|
|
|
2020-05-12 03:53:27 +00:00
|
|
|
def build_report(db: Session):
|
2012-10-16 20:02:01 +00:00
|
|
|
body = []
|
2012-05-01 21:40:01 +00:00
|
|
|
|
2019-04-06 04:13:12 +00:00
|
|
|
query = (
|
2020-05-12 03:53:27 +00:00
|
|
|
db.query(Voucher)
|
2019-04-06 04:13:12 +00:00
|
|
|
.options(joinedload_all(Voucher.journals, Journal.account, innerjoin=True))
|
|
|
|
.filter(Voucher.posted == False)
|
|
|
|
.filter(Voucher.type != VoucherType.by_name("Issue").id)
|
|
|
|
.order_by(Voucher.date)
|
|
|
|
.order_by(Voucher.last_edit_date)
|
|
|
|
.all()
|
|
|
|
)
|
2012-05-01 21:40:01 +00:00
|
|
|
|
|
|
|
for voucher in query:
|
|
|
|
debit = 0
|
|
|
|
credit = 0
|
2016-12-24 11:41:01 +00:00
|
|
|
name_debit = ""
|
|
|
|
name_credit = ""
|
2012-05-01 21:40:01 +00:00
|
|
|
for journal in voucher.journals:
|
|
|
|
if journal.debit == 1:
|
|
|
|
debit += journal.amount
|
2018-07-07 11:01:44 +00:00
|
|
|
name_debit += "{0} / ".format(journal.account.name)
|
2012-05-01 21:40:01 +00:00
|
|
|
else:
|
|
|
|
credit += journal.amount
|
2018-07-07 11:01:44 +00:00
|
|
|
name_credit += "{0} / ".format(journal.account.name)
|
2016-12-24 11:41:01 +00:00
|
|
|
name_debit = name_debit[:-3]
|
|
|
|
name_credit = name_credit[:-3]
|
2012-05-01 21:40:01 +00:00
|
|
|
|
2019-04-06 04:13:12 +00:00
|
|
|
body.append(
|
|
|
|
{
|
2020-05-12 17:51:12 +00:00
|
|
|
"id": voucher.id,
|
2019-04-06 04:13:12 +00:00
|
|
|
"date": voucher.date.strftime("%d-%b-%Y"),
|
2020-10-07 15:18:43 +00:00
|
|
|
"url": [
|
|
|
|
"/",
|
|
|
|
VoucherType.by_id(voucher.type).name.replace(" ", "-").lower(),
|
|
|
|
str(voucher.id),
|
|
|
|
],
|
2020-05-14 11:03:08 +00:00
|
|
|
"type": VoucherType.by_id(voucher.type).name,
|
2019-04-06 04:13:12 +00:00
|
|
|
"narration": voucher.narration,
|
|
|
|
"debitName": name_debit,
|
|
|
|
"debitAmount": debit,
|
|
|
|
"creditName": name_credit,
|
|
|
|
"creditAmount": credit,
|
|
|
|
}
|
|
|
|
)
|
2012-05-01 21:40:01 +00:00
|
|
|
|
2016-12-24 11:41:01 +00:00
|
|
|
return body
|