Purchases done!!
This commit is contained in:
parent
da58528768
commit
2462818abf
brewman
overlord/src/app/purchases
@ -10,6 +10,7 @@ from ...db.session import SessionLocal
|
||||
|
||||
from brewman.models.master import CostCentre, Product
|
||||
from brewman.models.voucher import Voucher, Journal, Inventory, VoucherType
|
||||
import brewman.schemas.reports as schemas
|
||||
from ...core.session import (
|
||||
set_period,
|
||||
get_start_date,
|
||||
@ -28,20 +29,19 @@ def get_db() -> Session:
|
||||
db.close()
|
||||
|
||||
|
||||
@router.get("/api/purchases")
|
||||
@router.get("", response_model=schemas.Purchases)
|
||||
def report_blank(
|
||||
request: Request,
|
||||
user: UserToken = Security(get_user, scopes=["purchases"]),
|
||||
request: Request, user: UserToken = Security(get_user, scopes=["purchases"]),
|
||||
):
|
||||
return {
|
||||
"startDate": get_start_date(request.session),
|
||||
"finishDate": get_finish_date(request.session),
|
||||
"body": [],
|
||||
"footer": {},
|
||||
}
|
||||
return schemas.Purchases(
|
||||
startDate=get_start_date(request.session),
|
||||
finishDate=get_finish_date(request.session),
|
||||
body=[],
|
||||
footer=None,
|
||||
)
|
||||
|
||||
|
||||
@router.get("/{start}/{finish}")
|
||||
@router.get("/{start}/{finish}", response_model=schemas.Purchases)
|
||||
def report_data(
|
||||
start: str,
|
||||
finish: str,
|
||||
@ -51,12 +51,9 @@ def report_data(
|
||||
):
|
||||
body, footer = build_report(start, finish, db)
|
||||
set_period(start, finish, request.session)
|
||||
return {
|
||||
"startDate": start,
|
||||
"finishDate": finish,
|
||||
"body": body,
|
||||
"footer": footer,
|
||||
}
|
||||
return schemas.Purchases(
|
||||
startDate=start, finishDate=finish, body=body, footer=footer,
|
||||
)
|
||||
|
||||
|
||||
def build_report(start_date, finish_date, db):
|
||||
@ -83,12 +80,17 @@ def build_report(start_date, finish_date, db):
|
||||
for product, quantity, amount in query:
|
||||
rate = amount / quantity if quantity != 0 else 0
|
||||
total_amount += amount
|
||||
row = {
|
||||
"name": product.full_name,
|
||||
"quantity": quantity,
|
||||
"rate": rate,
|
||||
"amount": amount,
|
||||
"url": "", # request.route_url("product_ledger_id", id=product.id, _query={"startDate": start_date, "finishDate": finish_date},),
|
||||
}
|
||||
row = schemas.PurchasesItem(
|
||||
name=product.full_name,
|
||||
quantity=quantity,
|
||||
rate=rate,
|
||||
amount=amount,
|
||||
url=["/", "product-ledger", str(product.id)],
|
||||
)
|
||||
body.append(row)
|
||||
return body, {"name": "Total", "amount": total_amount}
|
||||
return (
|
||||
body,
|
||||
schemas.PurchasesItem(
|
||||
name="Total", quantity=0, rate=0, url=[], amount=total_amount
|
||||
),
|
||||
)
|
||||
|
@ -421,3 +421,47 @@ class PurchaseEntries(BaseModel):
|
||||
value,
|
||||
"%d-%b-%Y"
|
||||
).date()
|
||||
|
||||
|
||||
class PurchasesItem(BaseModel):
|
||||
name: str
|
||||
quantity: Decimal
|
||||
rate: Decimal
|
||||
amount: Decimal
|
||||
url: List[str]
|
||||
|
||||
class Config:
|
||||
anystr_strip_whitespace = True
|
||||
alias_generator = to_camel
|
||||
|
||||
|
||||
class Purchases(BaseModel):
|
||||
start_date: date
|
||||
finish_date: date
|
||||
body: List[PurchasesItem]
|
||||
footer: Optional[PurchasesItem]
|
||||
|
||||
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):
|
||||
if isinstance(value, date):
|
||||
return value
|
||||
return datetime.strptime(
|
||||
value,
|
||||
"%d-%b-%Y"
|
||||
).date()
|
||||
|
||||
@validator("finish_date", pre=True)
|
||||
def parse_finish_date(cls, value):
|
||||
if isinstance(value, date):
|
||||
return value
|
||||
return datetime.strptime(
|
||||
value,
|
||||
"%d-%b-%Y"
|
||||
).date()
|
||||
|
@ -26,9 +26,9 @@
|
||||
<!-- Product Column -->
|
||||
<ng-container matColumnDef="product">
|
||||
<mat-header-cell *matHeaderCellDef>Product</mat-header-cell>
|
||||
<mat-cell *matCellDef="let row">{{row.name}}</mat-cell>
|
||||
<mat-cell *matCellDef="let row"><a [routerLink]="row.url" [queryParams]="{startDate: info.startDate, finishDate: info.finishDate}">{{row.name}}</a></mat-cell>
|
||||
<mat-footer-cell *matFooterCellDef>
|
||||
{{info.footer.name}}
|
||||
{{info.footer?.name}}
|
||||
</mat-footer-cell>
|
||||
</ng-container>
|
||||
|
||||
@ -37,7 +37,7 @@
|
||||
<mat-header-cell *matHeaderCellDef class="right">Quantity</mat-header-cell>
|
||||
<mat-cell *matCellDef="let row" class="right">{{row.quantity | number:'1.2-2'}}</mat-cell>
|
||||
<mat-footer-cell *matFooterCellDef class="right">
|
||||
{{info.footer.quantity | number:'1.2-2'}}
|
||||
{{info.footer?.quantity | number:'1.2-2'}}
|
||||
</mat-footer-cell>
|
||||
</ng-container>
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
<mat-header-cell *matHeaderCellDef class="right">Rate</mat-header-cell>
|
||||
<mat-cell *matCellDef="let row" class="right">{{row.rate | currency:'INR'}}</mat-cell>
|
||||
<mat-footer-cell *matFooterCellDef class="right">
|
||||
{{info.footer.rate | currency:'INR'}}
|
||||
{{info.footer?.rate | currency:'INR'}}
|
||||
</mat-footer-cell>
|
||||
</ng-container>
|
||||
|
||||
@ -55,7 +55,7 @@
|
||||
<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-footer-cell *matFooterCellDef class="right">
|
||||
{{info.footer.amount | currency:'INR'}}
|
||||
{{info.footer?.amount | currency:'INR'}}
|
||||
</mat-footer-cell>
|
||||
</ng-container>
|
||||
|
||||
|
@ -4,7 +4,7 @@ export class PurchasesItem {
|
||||
quantity: number;
|
||||
rate: number;
|
||||
amount: number;
|
||||
url: string;
|
||||
url: string[];
|
||||
}
|
||||
|
||||
export class Purchases {
|
||||
|
Loading…
x
Reference in New Issue
Block a user