Trial Balance Done!!

This commit is contained in:
tanshu 2020-05-14 16:24:03 +05:30
parent 6a044f827b
commit 0df442e541
2 changed files with 36 additions and 9 deletions

View File

@ -1,4 +1,4 @@
import datetime
from datetime import datetime, date
from fastapi import APIRouter, Depends, Security, Request
from sqlalchemy.orm import Session
@ -9,6 +9,7 @@ from ...core.security import get_current_active_user as get_user
from ...db.session import SessionLocal
from brewman.models.master import AccountBase
from brewman.models.voucher import Voucher, Journal, VoucherType
import brewman.schemas.reports as schemas
from ...core.session import (
set_period,
get_start_date,
@ -27,7 +28,7 @@ def get_db() -> Session:
db.close()
@router.get("")
@router.get("", response_model=schemas.TrialBalance)
def report_blank(
request: Request,
user: UserToken = Security(get_user, scopes=["trial-balance"]),
@ -35,25 +36,24 @@ def report_blank(
return {"date": get_finish_date(request.session), "body": []}
@router.get("/{date}")
@router.get("/{date_}", response_model=schemas.TrialBalance)
def report_data(
date: str,
date_: str,
request: Request,
db: Session = Depends(get_db),
user: UserToken = Security(get_user, scopes=["trial-balance"]),
):
set_period(get_start_date(request.session), date, request.session)
return {"date": date, "body": build_report(date, db)}
set_period(get_start_date(request.session), date_, request.session)
return {"date": date_, "body": build_report(datetime.strptime(date_, "%d-%b-%Y"), db)}
def build_report(date, db):
date = datetime.datetime.strptime(date, "%d-%b-%Y")
def build_report(date_: date, db: Session):
amount_sum = func.sum(Journal.amount * Journal.debit).label("amount")
query = (
db.query(AccountBase, amount_sum)
.join(Journal.voucher)
.join(Journal.account)
.filter(Voucher.date <= date)
.filter(Voucher.date <= date_)
.filter(Voucher.type != VoucherType.by_name("Issue").id)
.group_by(AccountBase)
.order_by(AccountBase.type)

View File

@ -451,3 +451,30 @@ class StockMovement(BaseModel):
if isinstance(value, date):
return value
return datetime.strptime(value, "%d-%b-%Y").date()
class TrialBalanceItem(BaseModel):
type_: Optional[str]
name: Optional[str]
debit: Optional[Decimal]
credit: Optional[Decimal]
class Config:
anystr_strip_whitespace = True
alias_generator = to_camel
class TrialBalance(BaseModel):
date_: date
body: List[TrialBalanceItem]
class Config:
anystr_strip_whitespace = True
alias_generator = to_camel
json_encoders = {date: lambda v: v.strftime("%d-%b-%Y")}
@validator("date_", pre=True)
def parse_date(cls, value):
if isinstance(value, date):
return value
return datetime.strptime(value, "%d-%b-%Y").date()