Profit & Loss Done!!
This commit is contained in:
@ -1,4 +1,4 @@
|
|||||||
import datetime
|
from datetime import datetime, date
|
||||||
|
|
||||||
from fastapi import APIRouter, Depends, Security, Request
|
from fastapi import APIRouter, Depends, Security, Request
|
||||||
from sqlalchemy.orm import Session
|
from sqlalchemy.orm import Session
|
||||||
@ -10,6 +10,7 @@ from ...db.session import SessionLocal
|
|||||||
from brewman.models.master import AccountType, AccountBase
|
from brewman.models.master import AccountType, AccountBase
|
||||||
from brewman.models.voucher import Voucher, Journal, VoucherType
|
from brewman.models.voucher import Voucher, Journal, VoucherType
|
||||||
from brewman.routers.reports.closing_stock import get_opening_stock, get_closing_stock
|
from brewman.routers.reports.closing_stock import get_opening_stock, get_closing_stock
|
||||||
|
import brewman.schemas.reports as schemas
|
||||||
from ...core.session import (
|
from ...core.session import (
|
||||||
set_period,
|
set_period,
|
||||||
get_start_date,
|
get_start_date,
|
||||||
@ -28,7 +29,7 @@ def get_db() -> Session:
|
|||||||
db.close()
|
db.close()
|
||||||
|
|
||||||
|
|
||||||
@router.get("")
|
@router.get("", response_model=schemas.ProfitLoss)
|
||||||
def report_blank(
|
def report_blank(
|
||||||
request: Request,
|
request: Request,
|
||||||
user: UserToken = Security(get_user, scopes=["profit-&-loss"]),
|
user: UserToken = Security(get_user, scopes=["profit-&-loss"]),
|
||||||
@ -37,11 +38,11 @@ def report_blank(
|
|||||||
"startDate": get_start_date(request.session),
|
"startDate": get_start_date(request.session),
|
||||||
"finishDate": get_finish_date(request.session),
|
"finishDate": get_finish_date(request.session),
|
||||||
"body": [],
|
"body": [],
|
||||||
"footer": {},
|
"footer": None,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@router.get("/{start}/{finish}")
|
@router.get("/{start}/{finish}", response_model=schemas.ProfitLoss)
|
||||||
def report_data(
|
def report_data(
|
||||||
start: str,
|
start: str,
|
||||||
finish: str,
|
finish: str,
|
||||||
@ -49,7 +50,7 @@ def report_data(
|
|||||||
db: Session = Depends(get_db),
|
db: Session = Depends(get_db),
|
||||||
user: UserToken = Security(get_user, scopes=["profit-&-loss"]),
|
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)
|
set_period(start, finish, request.session)
|
||||||
return {
|
return {
|
||||||
"startDate": start,
|
"startDate": start,
|
||||||
@ -59,9 +60,7 @@ def report_data(
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
def build_profit_loss(start_date, finish_date, db):
|
def build_profit_loss(start_date: date, finish_date: date, db: Session):
|
||||||
start_date = datetime.datetime.strptime(start_date, "%d-%b-%Y")
|
|
||||||
finish_date = datetime.datetime.strptime(finish_date, "%d-%b-%Y")
|
|
||||||
profit_type_list = [i.id for i in AccountType.list() if i.balance_sheet == False]
|
profit_type_list = [i.id for i in AccountType.list() if i.balance_sheet == False]
|
||||||
report = []
|
report = []
|
||||||
groups = {}
|
groups = {}
|
||||||
@ -87,9 +86,9 @@ def build_profit_loss(start_date, finish_date, db):
|
|||||||
total_amount = (opening_stock - closing_stock) * -1
|
total_amount = (opening_stock - closing_stock) * -1
|
||||||
|
|
||||||
report.append(
|
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)
|
purchase_group = AccountType.by_id(2)
|
||||||
groups[purchase_group.id] = {
|
groups[purchase_group.id] = {
|
||||||
"group": purchase_group.name,
|
"group": purchase_group.name,
|
||||||
@ -105,7 +104,7 @@ def build_profit_loss(start_date, finish_date, db):
|
|||||||
total_amount += amount
|
total_amount += amount
|
||||||
|
|
||||||
if amount != 0:
|
if amount != 0:
|
||||||
counter += 0.001
|
counter += 10
|
||||||
report.append(
|
report.append(
|
||||||
{
|
{
|
||||||
"name": account.name,
|
"name": account.name,
|
||||||
@ -130,7 +129,7 @@ def build_profit_loss(start_date, finish_date, db):
|
|||||||
footer = {
|
footer = {
|
||||||
"group": "Net Profit" if total_amount > 0 else "Net Loss",
|
"group": "Net Profit" if total_amount > 0 else "Net Loss",
|
||||||
"total": total_amount,
|
"total": total_amount,
|
||||||
"order": 100,
|
"order": 1000000,
|
||||||
}
|
}
|
||||||
|
|
||||||
return sorted(report, key=lambda d: d["order"]), footer
|
return sorted(report, key=lambda d: d["order"]), footer
|
||||||
|
|||||||
@ -328,3 +328,46 @@ class ProductLedger(BaseModel):
|
|||||||
value,
|
value,
|
||||||
"%d-%b-%Y"
|
"%d-%b-%Y"
|
||||||
).date()
|
).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()
|
||||||
|
|||||||
@ -27,14 +27,14 @@
|
|||||||
<ng-container matColumnDef="group">
|
<ng-container matColumnDef="group">
|
||||||
<mat-header-cell *matHeaderCellDef mat-sort-header>Group</mat-header-cell>
|
<mat-header-cell *matHeaderCellDef mat-sort-header>Group</mat-header-cell>
|
||||||
<mat-cell *matCellDef="let row">{{row.group}}</mat-cell>
|
<mat-cell *matCellDef="let row">{{row.group}}</mat-cell>
|
||||||
<mat-footer-cell *matFooterCellDef>{{info.footer.group}}</mat-footer-cell>
|
<mat-footer-cell *matFooterCellDef>{{info.footer?.group}}</mat-footer-cell>
|
||||||
</ng-container>
|
</ng-container>
|
||||||
|
|
||||||
<!-- Name Column -->
|
<!-- Name Column -->
|
||||||
<ng-container matColumnDef="name">
|
<ng-container matColumnDef="name">
|
||||||
<mat-header-cell *matHeaderCellDef mat-sort-header>Name</mat-header-cell>
|
<mat-header-cell *matHeaderCellDef mat-sort-header>Name</mat-header-cell>
|
||||||
<mat-cell *matCellDef="let row">{{row.name}}</mat-cell>
|
<mat-cell *matCellDef="let row">{{row.name}}</mat-cell>
|
||||||
<mat-footer-cell *matFooterCellDef>{{info.footer.name}}</mat-footer-cell>
|
<mat-footer-cell *matFooterCellDef>{{info.footer?.name}}</mat-footer-cell>
|
||||||
</ng-container>
|
</ng-container>
|
||||||
|
|
||||||
<!-- Amount Column -->
|
<!-- Amount Column -->
|
||||||
@ -42,7 +42,7 @@
|
|||||||
<mat-header-cell *matHeaderCellDef mat-sort-header class="right">Amount</mat-header-cell>
|
<mat-header-cell *matHeaderCellDef mat-sort-header class="right">Amount</mat-header-cell>
|
||||||
<mat-cell *matCellDef="let row" class="right">{{row.amount | currency:'INR'}}</mat-cell>
|
<mat-cell *matCellDef="let row" class="right">{{row.amount | currency:'INR'}}</mat-cell>
|
||||||
<mat-footer-cell *matFooterCellDef class="right">
|
<mat-footer-cell *matFooterCellDef class="right">
|
||||||
{{info.footer.amount | currency:'INR'}}
|
{{info.footer?.amount | currency:'INR'}}
|
||||||
</mat-footer-cell>
|
</mat-footer-cell>
|
||||||
</ng-container>
|
</ng-container>
|
||||||
|
|
||||||
@ -51,7 +51,7 @@
|
|||||||
<mat-header-cell *matHeaderCellDef mat-sort-header class="right">Total</mat-header-cell>
|
<mat-header-cell *matHeaderCellDef mat-sort-header class="right">Total</mat-header-cell>
|
||||||
<mat-cell *matCellDef="let row" class="right">{{row.total | currency:'INR'}}</mat-cell>
|
<mat-cell *matCellDef="let row" class="right">{{row.total | currency:'INR'}}</mat-cell>
|
||||||
<mat-footer-cell *matFooterCellDef class="right">
|
<mat-footer-cell *matFooterCellDef class="right">
|
||||||
{{info.footer.total | currency:'INR'}}
|
{{info.footer?.total | currency:'INR'}}
|
||||||
</mat-footer-cell>
|
</mat-footer-cell>
|
||||||
</ng-container>
|
</ng-container>
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user