Refactored customer discount and choose discount. They were using the same schema unnecessarily which was leading to confusion.

This commit is contained in:
Amritanshu Agrawal 2023-03-06 21:04:44 +05:30
parent c5eaeb1497
commit 516b22ed1c
15 changed files with 66 additions and 42 deletions

View File

@ -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
],
)

View File

@ -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()

View File

@ -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()
],
)

View File

@ -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

View File

@ -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

View File

@ -1,12 +1,14 @@
export class CustomerDiscount {
id: string;
name: string;
discount: number | null;
discount: number;
limit: number;
public constructor(init?: Partial<CustomerDiscount>) {
this.id = '';
this.name = '';
this.discount = null;
this.discount = 0;
this.limit = 1;
Object.assign(this, init);
}
}

View File

@ -39,6 +39,7 @@
<mat-label>Discount on {{ r.name }}</mat-label>
<input matInput formControlName="discount" />
<span matSuffix>%</span>
<mat-hint>Max {{ r.limit | percent : '1.2-2' }}</mat-hint>
</mat-form-field>
</div>
</div>

View File

@ -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;
}
});
}

View File

@ -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;
}
});
}

View File

@ -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';

View File

@ -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<DiscountItem>) {
this.id = '';
this.name = '';
this.discount = 0;
this.discountLimit = 0;
this.customerDiscount = 0;
this.limit = 0;
this.customer = 0;
Object.assign(this, init);
}
}

View File

@ -22,8 +22,8 @@
<mat-form-field class="flex-auto">
<input matInput type="number" formControlName="discount" autocomplete="off" />
<span matSuffix>%</span>
<mat-hint>Cust: {{ row.customerDiscount | percent : '1.2-2' }}</mat-hint>
<mat-hint align="end">Max: {{ row.discountLimit | percent : '1.2-2' }}</mat-hint>
<mat-hint>Cust: {{ row.customer | percent : '1.2-2' }}</mat-hint>
<mat-hint align="end">Max: {{ row.limit | percent : '1.2-2' }}</mat-hint>
</mat-form-field>
</mat-cell>
</ng-container>

View File

@ -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);

View File

@ -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';