From e62bb0a82531a212309241251ca5082684f32bef Mon Sep 17 00:00:00 2001 From: tanshu Date: Thu, 14 May 2020 13:49:40 +0530 Subject: [PATCH] Profit & Loss Done!! --- brewman/routers/reports/profit_loss.py | 23 +++++----- brewman/schemas/reports.py | 43 +++++++++++++++++++ .../profit-loss/profit-loss.component.html | 8 ++-- 3 files changed, 58 insertions(+), 16 deletions(-) diff --git a/brewman/routers/reports/profit_loss.py b/brewman/routers/reports/profit_loss.py index fd02124e..da20c441 100644 --- a/brewman/routers/reports/profit_loss.py +++ b/brewman/routers/reports/profit_loss.py @@ -1,4 +1,4 @@ -import datetime +from datetime import datetime, date from fastapi import APIRouter, Depends, Security, Request from sqlalchemy.orm import Session @@ -10,6 +10,7 @@ from ...db.session import SessionLocal 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 +import brewman.schemas.reports as schemas from ...core.session import ( set_period, get_start_date, @@ -28,7 +29,7 @@ def get_db() -> Session: db.close() -@router.get("") +@router.get("", response_model=schemas.ProfitLoss) def report_blank( request: Request, user: UserToken = Security(get_user, scopes=["profit-&-loss"]), @@ -37,11 +38,11 @@ def report_blank( "startDate": get_start_date(request.session), "finishDate": get_finish_date(request.session), "body": [], - "footer": {}, + "footer": None, } -@router.get("/{start}/{finish}") +@router.get("/{start}/{finish}", response_model=schemas.ProfitLoss) def report_data( start: str, finish: str, @@ -49,7 +50,7 @@ def report_data( db: Session = Depends(get_db), user: UserToken = Security(get_user, scopes=["profit-&-loss"]), ): - body, footer = build_profit_loss(start, finish, db) + body, footer = build_profit_loss(datetime.strptime(start, "%d-%b-%Y"), datetime.strptime(finish, "%d-%b-%Y"), db) set_period(start, finish, request.session) return { "startDate": start, @@ -59,9 +60,7 @@ def report_data( } -def build_profit_loss(start_date, finish_date, db): - start_date = datetime.datetime.strptime(start_date, "%d-%b-%Y") - finish_date = datetime.datetime.strptime(finish_date, "%d-%b-%Y") +def build_profit_loss(start_date: date, finish_date: date, db: Session): profit_type_list = [i.id for i in AccountType.list() if i.balance_sheet == False] report = [] groups = {} @@ -87,9 +86,9 @@ def build_profit_loss(start_date, finish_date, db): total_amount = (opening_stock - closing_stock) * -1 report.append( - {"name": "Opening Stock", "amount": opening_stock * -1, "order": 20.0001} + {"name": "Opening Stock", "amount": opening_stock * -1, "order": 200001} ) - report.append({"name": "Closing Stock", "amount": closing_stock, "order": 29}) + report.append({"name": "Closing Stock", "amount": closing_stock, "order": 290000}) purchase_group = AccountType.by_id(2) groups[purchase_group.id] = { "group": purchase_group.name, @@ -105,7 +104,7 @@ def build_profit_loss(start_date, finish_date, db): total_amount += amount if amount != 0: - counter += 0.001 + counter += 10 report.append( { "name": account.name, @@ -130,7 +129,7 @@ def build_profit_loss(start_date, finish_date, db): footer = { "group": "Net Profit" if total_amount > 0 else "Net Loss", "total": total_amount, - "order": 100, + "order": 1000000, } return sorted(report, key=lambda d: d["order"]), footer diff --git a/brewman/schemas/reports.py b/brewman/schemas/reports.py index be31b9d6..720fc91c 100644 --- a/brewman/schemas/reports.py +++ b/brewman/schemas/reports.py @@ -328,3 +328,46 @@ class ProductLedger(BaseModel): value, "%d-%b-%Y" ).date() + + +class ProfitLossItem(BaseModel): + group: Optional[str] + name: Optional[str] + amount: Optional[Decimal] + total: Optional[Decimal] + order: int + + class Config: + anystr_strip_whitespace = True + alias_generator = to_camel + json_encoders = { + date: lambda v: v.strftime("%d-%b-%Y") + } + + +class ProfitLoss(BaseModel): + start_date: date + finish_date: date + body: List[ProfitLossItem] + footer: Optional[ProfitLossItem] + + class Config: + anystr_strip_whitespace = True + alias_generator = to_camel + json_encoders = { + date: lambda v: v.strftime("%d-%b-%Y") + } + + @validator("start_date", pre=True) + def parse_start_date(cls, value): + return datetime.strptime( + value, + "%d-%b-%Y" + ).date() + + @validator("finish_date", pre=True) + def parse_finish_date(cls, value): + return datetime.strptime( + value, + "%d-%b-%Y" + ).date() diff --git a/overlord/src/app/profit-loss/profit-loss.component.html b/overlord/src/app/profit-loss/profit-loss.component.html index 8c15e27f..75697e2f 100644 --- a/overlord/src/app/profit-loss/profit-loss.component.html +++ b/overlord/src/app/profit-loss/profit-loss.component.html @@ -27,14 +27,14 @@ Group {{row.group}} - {{info.footer.group}} + {{info.footer?.group}} Name {{row.name}} - {{info.footer.name}} + {{info.footer?.name}} @@ -42,7 +42,7 @@ Amount {{row.amount | currency:'INR'}} - {{info.footer.amount | currency:'INR'}} + {{info.footer?.amount | currency:'INR'}} @@ -51,7 +51,7 @@ Total {{row.total | currency:'INR'}} - {{info.footer.total | currency:'INR'}} + {{info.footer?.total | currency:'INR'}}