Refactored customer discount and choose discount. They were using the same schema unnecessarily which was leading to confusion.
This commit is contained in:
@ -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
|
||||
],
|
||||
)
|
||||
|
||||
@ -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()
|
||||
|
||||
@ -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()
|
||||
],
|
||||
)
|
||||
|
||||
@ -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
|
||||
|
||||
17
barker/barker/schemas/discount_item.py
Normal file
17
barker/barker/schemas/discount_item.py
Normal 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
|
||||
Reference in New Issue
Block a user