Purchases done!!

This commit is contained in:
tanshu
2020-05-14 14:59:16 +05:30
parent da58528768
commit 2462818abf
4 changed files with 76 additions and 30 deletions

View File

@ -10,6 +10,7 @@ from ...db.session import SessionLocal
from brewman.models.master import CostCentre, Product from brewman.models.master import CostCentre, Product
from brewman.models.voucher import Voucher, Journal, Inventory, VoucherType from brewman.models.voucher import Voucher, Journal, Inventory, VoucherType
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,20 +29,19 @@ def get_db() -> Session:
db.close() db.close()
@router.get("/api/purchases") @router.get("", response_model=schemas.Purchases)
def report_blank( def report_blank(
request: Request, request: Request, user: UserToken = Security(get_user, scopes=["purchases"]),
user: UserToken = Security(get_user, scopes=["purchases"]),
): ):
return { return schemas.Purchases(
"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.Purchases)
def report_data( def report_data(
start: str, start: str,
finish: str, finish: str,
@ -51,12 +51,9 @@ def report_data(
): ):
body, footer = build_report(start, finish, db) body, footer = build_report(start, finish, db)
set_period(start, finish, request.session) set_period(start, finish, request.session)
return { return schemas.Purchases(
"startDate": start, startDate=start, finishDate=finish, body=body, footer=footer,
"finishDate": finish, )
"body": body,
"footer": footer,
}
def build_report(start_date, finish_date, db): 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: for product, quantity, amount in query:
rate = amount / quantity if quantity != 0 else 0 rate = amount / quantity if quantity != 0 else 0
total_amount += amount total_amount += amount
row = { row = schemas.PurchasesItem(
"name": product.full_name, name=product.full_name,
"quantity": quantity, quantity=quantity,
"rate": rate, rate=rate,
"amount": amount, amount=amount,
"url": "", # request.route_url("product_ledger_id", id=product.id, _query={"startDate": start_date, "finishDate": finish_date},), url=["/", "product-ledger", str(product.id)],
} )
body.append(row) body.append(row)
return body, {"name": "Total", "amount": total_amount} return (
body,
schemas.PurchasesItem(
name="Total", quantity=0, rate=0, url=[], amount=total_amount
),
)

View File

@ -421,3 +421,47 @@ class PurchaseEntries(BaseModel):
value, value,
"%d-%b-%Y" "%d-%b-%Y"
).date() ).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()

View File

@ -26,9 +26,9 @@
<!-- Product Column --> <!-- Product Column -->
<ng-container matColumnDef="product"> <ng-container matColumnDef="product">
<mat-header-cell *matHeaderCellDef>Product</mat-header-cell> <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> <mat-footer-cell *matFooterCellDef>
{{info.footer.name}} {{info.footer?.name}}
</mat-footer-cell> </mat-footer-cell>
</ng-container> </ng-container>
@ -37,7 +37,7 @@
<mat-header-cell *matHeaderCellDef class="right">Quantity</mat-header-cell> <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-cell *matCellDef="let row" class="right">{{row.quantity | number:'1.2-2'}}</mat-cell>
<mat-footer-cell *matFooterCellDef class="right"> <mat-footer-cell *matFooterCellDef class="right">
{{info.footer.quantity | number:'1.2-2'}} {{info.footer?.quantity | number:'1.2-2'}}
</mat-footer-cell> </mat-footer-cell>
</ng-container> </ng-container>
@ -46,7 +46,7 @@
<mat-header-cell *matHeaderCellDef class="right">Rate</mat-header-cell> <mat-header-cell *matHeaderCellDef class="right">Rate</mat-header-cell>
<mat-cell *matCellDef="let row" class="right">{{row.rate | currency:'INR'}}</mat-cell> <mat-cell *matCellDef="let row" class="right">{{row.rate | currency:'INR'}}</mat-cell>
<mat-footer-cell *matFooterCellDef class="right"> <mat-footer-cell *matFooterCellDef class="right">
{{info.footer.rate | currency:'INR'}} {{info.footer?.rate | currency:'INR'}}
</mat-footer-cell> </mat-footer-cell>
</ng-container> </ng-container>
@ -55,7 +55,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>

View File

@ -4,7 +4,7 @@ export class PurchasesItem {
quantity: number; quantity: number;
rate: number; rate: number;
amount: number; amount: number;
url: string; url: string[];
} }
export class Purchases { export class Purchases {