From 516b22ed1c683376130e13c3329e58fe81a10baf Mon Sep 17 00:00:00 2001 From: Amritanshu Date: Mon, 6 Mar 2023 21:04:44 +0530 Subject: [PATCH] Refactored customer discount and choose discount. They were using the same schema unnecessarily which was leading to confusion. --- barker/barker/routers/customer.py | 28 +++++++++++-------- barker/barker/routers/customer_discount.py | 10 +++---- barker/barker/routers/user.py | 12 ++++---- barker/barker/schemas/customer.py | 7 ++--- barker/barker/schemas/discount_item.py | 17 +++++++++++ bookie/src/app/core/customer-discount.ts | 6 ++-- .../customer-detail.component.html | 1 + .../customer-detail.component.ts | 2 +- .../choose-customer.component.ts | 2 +- .../customer-discounts.service.spec.ts | 0 .../discount}/customer-discounts.service.ts | 5 ++-- .../src/app/sales/discount/discount-item.ts | 8 +++--- .../sales/discount/discount.component.html | 4 +-- .../app/sales/discount/discount.component.ts | 4 +-- .../app/sales/home/sales-home.component.ts | 2 +- 15 files changed, 66 insertions(+), 42 deletions(-) create mode 100644 barker/barker/schemas/discount_item.py rename bookie/src/app/{customers => sales/discount}/customer-discounts.service.spec.ts (100%) rename bookie/src/app/{customers => sales/discount}/customer-discounts.service.ts (89%) diff --git a/barker/barker/routers/customer.py b/barker/barker/routers/customer.py index 1ab4e92..89c466d 100644 --- a/barker/barker/routers/customer.py +++ b/barker/barker/routers/customer.py @@ -64,11 +64,13 @@ def update_route( ) -def add_discounts(customer: Customer, discounts: list[schemas.DiscountItem], db: Session) -> None: +def add_discounts(customer: Customer, discounts: list[schemas.CustomerDiscount], db: Session) -> None: for discount in discounts: cd: CustomerDiscount | None = next((d for d in customer.discounts if d.sale_category_id == discount.id_), None) if cd is None: - cd = CustomerDiscount(discount.id_, round(discount.discount, 5), customer=customer) + cd = CustomerDiscount( + sale_category_id=discount.id_, discount=round(discount.discount, 5), customer=customer + ) customer.discounts.append(cd) db.add(cd) else: @@ -139,11 +141,12 @@ def customer_info(item: Customer, sale_categories: Sequence[SaleCategory]) -> sc phone=item.phone, printInBill=item.print_in_bill, discounts=[ - { - "id": sc.id, - "name": sc.name, - "discount": next((d.discount for d in item.discounts if d.sale_category_id == sc.id), None), - } + schemas.CustomerDiscount( + id=sc.id, + name=sc.name, + discount=next((d.discount for d in item.discounts if d.sale_category_id == sc.id), 0), + limit=sc.discount_limit, + ) for sc in sale_categories ], ) @@ -156,11 +159,12 @@ def blank_customer_info(sale_categories: Sequence[SaleCategory]) -> schemas.Cust phone="", printInBill=False, discounts=[ - { - "id": sc.id, - "name": sc.name, - "discount": None, - } + schemas.CustomerDiscount( + id=sc.id, + name=sc.name, + discount=0, + limit=sc.discount_limit, + ) for sc in sale_categories ], ) diff --git a/barker/barker/routers/customer_discount.py b/barker/barker/routers/customer_discount.py index 8e1c2cb..6c1dad3 100644 --- a/barker/barker/routers/customer_discount.py +++ b/barker/barker/routers/customer_discount.py @@ -2,7 +2,7 @@ import uuid from decimal import Decimal -import barker.schemas.customer as schemas +import barker.schemas.discount_item as schemas from fastapi import APIRouter, Security from sqlalchemy import select @@ -49,8 +49,8 @@ def customer_discounts(item: Customer, db: Session) -> list[schemas.DiscountItem discount=Decimal(0) if not default_discount else next((d.discount for d in item.discounts if d.sale_category_id == sc.id), Decimal(0)), - discountLimit=sc.discount_limit, - customerDiscount=next((d.discount for d in item.discounts if d.sale_category_id == sc.id), Decimal(0)), + limit=sc.discount_limit, + customer=next((d.discount for d in item.discounts if d.sale_category_id == sc.id), Decimal(0)), ) for sc in db.execute(select(SaleCategory).where(SaleCategory.discount_limit != 0).order_by(SaleCategory.name)) .scalars() @@ -64,8 +64,8 @@ def customer_discounts_blank(db: Session) -> list[schemas.DiscountItem]: id=sc.id, name=sc.name, discount=0, - discountLimit=sc.discount_limit, - customerDiscount=0, + limit=sc.discount_limit, + customer=0, ) for sc in db.execute(select(SaleCategory).where(SaleCategory.discount_limit != 0).order_by(SaleCategory.name)) .scalars() diff --git a/barker/barker/routers/user.py b/barker/barker/routers/user.py index bdaa73b..76e1867 100644 --- a/barker/barker/routers/user.py +++ b/barker/barker/routers/user.py @@ -165,11 +165,11 @@ def user_info(item: User, db: Session, user: UserToken) -> schemas.User: password="", lockedOut=item.locked_out, roles=[ - { - "id": r.id, - "name": r.name, - "enabled": True if r in item.roles else False, - } + schemas.RoleItem( + id=r.id, + name=r.name, + enabled=True if r in item.roles else False, + ) for r in db.execute(select(Role).order_by(Role.name)).scalars().all() ] if "users" in user.permissions @@ -193,7 +193,7 @@ def blank_user_info(db: Session) -> schemas.UserIn: password="", lockedOut=False, roles=[ - {"id": r.id, "name": r.name, "enabled": False} + schemas.RoleItem(id=r.id, name=r.name, enabled=False) for r in db.execute(select(Role).order_by(Role.name)).scalars().all() ], ) diff --git a/barker/barker/schemas/customer.py b/barker/barker/schemas/customer.py index 9d9aa20..0b8cf61 100644 --- a/barker/barker/schemas/customer.py +++ b/barker/barker/schemas/customer.py @@ -7,12 +7,11 @@ from pydantic import BaseModel, Field from . import to_camel -class DiscountItem(BaseModel): +class CustomerDiscount(BaseModel): id_: uuid.UUID name: str discount: Decimal = Field(ge=0, multiple_of=0.0001, default=0, le=1) - discount_limit: Decimal - customer_discount: Decimal + limit: Decimal = Field(ge=0, multiple_of=0.0001, default=0, le=1) class Config: alias_generator = to_camel @@ -25,7 +24,7 @@ class CustomerIn(BaseModel): address: str | None print_in_bill: bool - discounts: list[DiscountItem] + discounts: list[CustomerDiscount] class Config: anystr_strip_whitespace = True diff --git a/barker/barker/schemas/discount_item.py b/barker/barker/schemas/discount_item.py new file mode 100644 index 0000000..99e044e --- /dev/null +++ b/barker/barker/schemas/discount_item.py @@ -0,0 +1,17 @@ +import uuid + +from decimal import Decimal + +from barker.schemas import to_camel +from pydantic import BaseModel, Field + + +class DiscountItem(BaseModel): + id_: uuid.UUID + name: str + discount: Decimal | None = Field(ge=0, multiple_of=0.0001, default=0, le=1) + limit: Decimal + customer: Decimal + + class Config: + alias_generator = to_camel diff --git a/bookie/src/app/core/customer-discount.ts b/bookie/src/app/core/customer-discount.ts index 728d521..dfb396c 100644 --- a/bookie/src/app/core/customer-discount.ts +++ b/bookie/src/app/core/customer-discount.ts @@ -1,12 +1,14 @@ export class CustomerDiscount { id: string; name: string; - discount: number | null; + discount: number; + limit: number; public constructor(init?: Partial) { this.id = ''; this.name = ''; - this.discount = null; + this.discount = 0; + this.limit = 1; Object.assign(this, init); } } diff --git a/bookie/src/app/customers/customer-detail/customer-detail.component.html b/bookie/src/app/customers/customer-detail/customer-detail.component.html index 3fdebe0..bc4d42c 100644 --- a/bookie/src/app/customers/customer-detail/customer-detail.component.html +++ b/bookie/src/app/customers/customer-detail/customer-detail.component.html @@ -39,6 +39,7 @@ Discount on {{ r.name }} % + Max {{ r.limit | percent : '1.2-2' }} diff --git a/bookie/src/app/customers/customer-detail/customer-detail.component.ts b/bookie/src/app/customers/customer-detail/customer-detail.component.ts index f1f151e..03a326a 100644 --- a/bookie/src/app/customers/customer-detail/customer-detail.component.ts +++ b/bookie/src/app/customers/customer-detail/customer-detail.component.ts @@ -131,7 +131,7 @@ export class CustomerDetailComponent implements OnInit, AfterViewInit { if (array_item && array_item?.discount) { item.discount = Math.max(Math.min(round(array_item.discount / 100, 5), 100), 0); } else { - item.discount = null; + item.discount = 0; } }); } diff --git a/bookie/src/app/sales/choose-customer/choose-customer.component.ts b/bookie/src/app/sales/choose-customer/choose-customer.component.ts index bb80aae..415c74e 100644 --- a/bookie/src/app/sales/choose-customer/choose-customer.component.ts +++ b/bookie/src/app/sales/choose-customer/choose-customer.component.ts @@ -106,7 +106,7 @@ export class ChooseCustomerComponent { if (array_item && array_item?.discount) { item.discount = Math.max(Math.min(round(array_item.discount / 100, 5), 100), 0); } else { - item.discount = null; + item.discount = 0; } }); } diff --git a/bookie/src/app/customers/customer-discounts.service.spec.ts b/bookie/src/app/sales/discount/customer-discounts.service.spec.ts similarity index 100% rename from bookie/src/app/customers/customer-discounts.service.spec.ts rename to bookie/src/app/sales/discount/customer-discounts.service.spec.ts diff --git a/bookie/src/app/customers/customer-discounts.service.ts b/bookie/src/app/sales/discount/customer-discounts.service.ts similarity index 89% rename from bookie/src/app/customers/customer-discounts.service.ts rename to bookie/src/app/sales/discount/customer-discounts.service.ts index ee99053..b9377b5 100644 --- a/bookie/src/app/customers/customer-discounts.service.ts +++ b/bookie/src/app/sales/discount/customer-discounts.service.ts @@ -3,8 +3,9 @@ import { Injectable } from '@angular/core'; import { Observable } from 'rxjs'; import { catchError } from 'rxjs/operators'; -import { ErrorLoggerService } from '../core/error-logger.service'; -import { DiscountItem } from '../sales/discount/discount-item'; +import { ErrorLoggerService } from '../../core/error-logger.service'; + +import { DiscountItem } from './discount-item'; const url = '/api/customer-discounts'; const serviceName = 'CustomerService'; diff --git a/bookie/src/app/sales/discount/discount-item.ts b/bookie/src/app/sales/discount/discount-item.ts index 0565d27..f8f4350 100644 --- a/bookie/src/app/sales/discount/discount-item.ts +++ b/bookie/src/app/sales/discount/discount-item.ts @@ -2,15 +2,15 @@ export class DiscountItem { id: string; name: string; discount: number; - discountLimit: number; - customerDiscount: number; + limit: number; + customer: number; public constructor(init?: Partial) { this.id = ''; this.name = ''; this.discount = 0; - this.discountLimit = 0; - this.customerDiscount = 0; + this.limit = 0; + this.customer = 0; Object.assign(this, init); } } diff --git a/bookie/src/app/sales/discount/discount.component.html b/bookie/src/app/sales/discount/discount.component.html index f32609b..ce00cf5 100644 --- a/bookie/src/app/sales/discount/discount.component.html +++ b/bookie/src/app/sales/discount/discount.component.html @@ -22,8 +22,8 @@ % - Cust: {{ row.customerDiscount | percent : '1.2-2' }} - Max: {{ row.discountLimit | percent : '1.2-2' }} + Cust: {{ row.customer | percent : '1.2-2' }} + Max: {{ row.limit | percent : '1.2-2' }} diff --git a/bookie/src/app/sales/discount/discount.component.ts b/bookie/src/app/sales/discount/discount.component.ts index 8d04917..b4c3f34 100644 --- a/bookie/src/app/sales/discount/discount.component.ts +++ b/bookie/src/app/sales/discount/discount.component.ts @@ -50,7 +50,7 @@ export class DiscountComponent { new FormGroup({ name: new FormControl(x.name, { nonNullable: true }), discount: new FormControl(x.discount * 100, { - validators: [Validators.min(0), Validators.max(x.discountLimit * 100)], + validators: [Validators.min(0), Validators.max(x.limit * 100)], nonNullable: true, }), }), @@ -68,7 +68,7 @@ export class DiscountComponent { if (control.pristine && control.value === 0) { this.list.splice(i, 1); } else { - item.discount = Math.max(Math.min(round(control.value / 100, 5), item.discountLimit), 0); + item.discount = Math.max(Math.min(round(control.value / 100, 5), item.limit), 0); } } this.dialogRef.close(this.list); diff --git a/bookie/src/app/sales/home/sales-home.component.ts b/bookie/src/app/sales/home/sales-home.component.ts index 9ef7c21..2ee6940 100644 --- a/bookie/src/app/sales/home/sales-home.component.ts +++ b/bookie/src/app/sales/home/sales-home.component.ts @@ -8,7 +8,6 @@ import { AuthService } from '../../auth/auth.service'; import { ReceivePaymentItem } from '../../core/receive-payment-item'; import { Table } from '../../core/table'; import { ToasterService } from '../../core/toaster.service'; -import { CustomerDiscountsService } from '../../customers/customer-discounts.service'; import { SaleCategoryService } from '../../sale-category/sale-category.service'; import { ConfirmDialogComponent } from '../../shared/confirm-dialog/confirm-dialog.component'; import { TableService } from '../../tables/table.service'; @@ -16,6 +15,7 @@ import { BillTypeComponent } from '../bill-type/bill-type.component'; import { BillService } from '../bill.service'; import { BillSelectionItem } from '../bills/bill-selection-item'; import { VoucherType } from '../bills/voucher-type'; +import { CustomerDiscountsService } from '../discount/customer-discounts.service'; import { DiscountComponent } from '../discount/discount.component'; import { ReasonComponent } from '../reason/reason.component'; import { ReceivePaymentComponent } from '../receive-payment/receive-payment.component';