82 lines
2.3 KiB
Python
82 lines
2.3 KiB
Python
from typing import List
|
|
|
|
import brewman.schemas.reports as schemas
|
|
|
|
from fastapi import APIRouter, Depends, Request, Security
|
|
from sqlalchemy.orm import Session, joinedload_all
|
|
|
|
from ...core.security import get_current_active_user as get_user
|
|
from ...db.session import SessionLocal
|
|
from ...models.voucher import Journal, Voucher, VoucherType
|
|
from ...schemas.auth import UserToken
|
|
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
# Dependency
|
|
def get_db() -> Session:
|
|
try:
|
|
db = SessionLocal()
|
|
yield db
|
|
finally:
|
|
db.close()
|
|
|
|
|
|
@router.get("", response_model=List[schemas.Unposted])
|
|
def report_data(
|
|
request: Request,
|
|
db: Session = Depends(get_db),
|
|
user: UserToken = Security(get_user, scopes=["post-vouchers"]),
|
|
) -> List[schemas.Unposted]:
|
|
return build_report(db)
|
|
|
|
|
|
def build_report(db: Session) -> List[schemas.Unposted]:
|
|
body = []
|
|
|
|
query = (
|
|
db.query(Voucher)
|
|
.options(joinedload_all(Voucher.journals, Journal.account, innerjoin=True))
|
|
.filter(Voucher.posted == False) # noqa: E712
|
|
.filter(Voucher.type != VoucherType.by_name("Issue").id)
|
|
.order_by(Voucher.date)
|
|
.order_by(Voucher.last_edit_date)
|
|
.all()
|
|
)
|
|
|
|
for voucher in query:
|
|
debit = 0
|
|
credit = 0
|
|
name_debit = ""
|
|
name_credit = ""
|
|
for journal in voucher.journals:
|
|
if journal.debit == 1:
|
|
debit += journal.amount
|
|
name_debit += "{0} / ".format(journal.account.name)
|
|
else:
|
|
credit += journal.amount
|
|
name_credit += "{0} / ".format(journal.account.name)
|
|
name_debit = name_debit[:-3]
|
|
name_credit = name_credit[:-3]
|
|
|
|
body.append(
|
|
schemas.Unposted(
|
|
id=voucher.id,
|
|
date=voucher.date.strftime("%d-%b-%Y"),
|
|
url=[
|
|
"/",
|
|
VoucherType.by_id(voucher.type).name.replace(" ", "-").lower(),
|
|
str(voucher.id),
|
|
],
|
|
type=VoucherType.by_id(voucher.type).name,
|
|
narration=voucher.narration,
|
|
debitName=name_debit,
|
|
debitAmount=debit,
|
|
creditName=name_credit,
|
|
creditAmount=credit,
|
|
)
|
|
)
|
|
|
|
return body
|