Fix: Customers with no discount wouldn't show properly in sale / select customer
This commit is contained in:
parent
b70a296d8b
commit
5ff954deb6
|
@ -31,7 +31,8 @@ def save(
|
||||||
db.add(item)
|
db.add(item)
|
||||||
add_discounts(item, data.discounts, db)
|
add_discounts(item, data.discounts, db)
|
||||||
db.commit()
|
db.commit()
|
||||||
return customer_info(item, db)
|
sc: List[SaleCategory] = db.execute(select(SaleCategory).order_by(SaleCategory.name)).scalars().all()
|
||||||
|
return customer_info(item, sc)
|
||||||
except SQLAlchemyError as e:
|
except SQLAlchemyError as e:
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
||||||
|
@ -54,7 +55,8 @@ def update_route(
|
||||||
item.print_in_bill = data.print_in_bill
|
item.print_in_bill = data.print_in_bill
|
||||||
add_discounts(item, data.discounts, db)
|
add_discounts(item, data.discounts, db)
|
||||||
db.commit()
|
db.commit()
|
||||||
return customer_info(item, db)
|
sc: List[SaleCategory] = db.execute(select(SaleCategory).order_by(SaleCategory.name)).scalars().all()
|
||||||
|
return customer_info(item, sc)
|
||||||
except SQLAlchemyError as e:
|
except SQLAlchemyError as e:
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
||||||
|
@ -89,15 +91,16 @@ def show_blank(
|
||||||
user: UserToken = Security(get_user, scopes=["customers"]),
|
user: UserToken = Security(get_user, scopes=["customers"]),
|
||||||
) -> schemas.CustomerBlank:
|
) -> schemas.CustomerBlank:
|
||||||
with SessionFuture() as db:
|
with SessionFuture() as db:
|
||||||
return blank_customer_info(db)
|
sc: List[SaleCategory] = db.execute(select(SaleCategory).order_by(SaleCategory.name)).scalars().all()
|
||||||
|
return blank_customer_info(sc)
|
||||||
|
|
||||||
|
|
||||||
@router.get("/list", response_model=List[schemas.Customer])
|
@router.get("/list", response_model=List[schemas.Customer])
|
||||||
def show_list(user: UserToken = Depends(get_user)) -> List[schemas.Customer]:
|
def show_list(user: UserToken = Depends(get_user)) -> List[schemas.Customer]:
|
||||||
with SessionFuture() as db:
|
with SessionFuture() as db:
|
||||||
|
sc: List[SaleCategory] = db.execute(select(SaleCategory).order_by(SaleCategory.name)).scalars().all()
|
||||||
return [
|
return [
|
||||||
customer_info_for_list(item)
|
customer_info(item, sc) for item in db.execute(select(Customer).order_by(Customer.name)).scalars().all()
|
||||||
for item in db.execute(select(Customer).order_by(Customer.name)).scalars().all()
|
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
|
@ -113,7 +116,8 @@ async def show_term(
|
||||||
|
|
||||||
query = query.order_by(Customer.name)
|
query = query.order_by(Customer.name)
|
||||||
with SessionFuture() as db:
|
with SessionFuture() as db:
|
||||||
return [customer_info_for_list(item) for item in db.execute(query).scalars().all()]
|
sc: List[SaleCategory] = db.execute(select(SaleCategory).order_by(SaleCategory.name)).scalars().all()
|
||||||
|
return [customer_info(item, sc) for item in db.execute(query).scalars().all()]
|
||||||
|
|
||||||
|
|
||||||
@router.get("/{id_}", response_model=schemas.Customer)
|
@router.get("/{id_}", response_model=schemas.Customer)
|
||||||
|
@ -122,11 +126,12 @@ def show_id(
|
||||||
user: UserToken = Security(get_user, scopes=["customers"]),
|
user: UserToken = Security(get_user, scopes=["customers"]),
|
||||||
) -> schemas.Customer:
|
) -> schemas.Customer:
|
||||||
with SessionFuture() as db:
|
with SessionFuture() as db:
|
||||||
|
sc: List[SaleCategory] = db.execute(select(SaleCategory).order_by(SaleCategory.name)).scalars().all()
|
||||||
item: Customer = db.execute(select(Customer).where(Customer.id == id_)).scalar_one()
|
item: Customer = db.execute(select(Customer).where(Customer.id == id_)).scalar_one()
|
||||||
return customer_info(item, db)
|
return customer_info(item, sc)
|
||||||
|
|
||||||
|
|
||||||
def customer_info(item: Customer, db: Session) -> schemas.Customer:
|
def customer_info(item: Customer, sale_categories: List[SaleCategory]) -> schemas.Customer:
|
||||||
return schemas.Customer(
|
return schemas.Customer(
|
||||||
id=item.id,
|
id=item.id,
|
||||||
name=item.name,
|
name=item.name,
|
||||||
|
@ -139,31 +144,12 @@ def customer_info(item: Customer, db: Session) -> schemas.Customer:
|
||||||
"name": sc.name,
|
"name": sc.name,
|
||||||
"discount": next((d.discount for d in item.discounts if d.sale_category_id == sc.id), 0),
|
"discount": next((d.discount for d in item.discounts if d.sale_category_id == sc.id), 0),
|
||||||
}
|
}
|
||||||
for sc in db.execute(select(SaleCategory).order_by(SaleCategory.name)).scalars().all()
|
for sc in sale_categories
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def customer_info_for_list(item: Customer) -> schemas.Customer:
|
def blank_customer_info(sale_categories: List[SaleCategory]) -> schemas.CustomerBlank:
|
||||||
return schemas.Customer(
|
|
||||||
id=item.id,
|
|
||||||
name=item.name,
|
|
||||||
address=item.address,
|
|
||||||
phone=item.phone,
|
|
||||||
printInBill=item.print_in_bill,
|
|
||||||
discounts=[
|
|
||||||
{
|
|
||||||
"id": d.sale_category_id,
|
|
||||||
"name": d.sale_category.name,
|
|
||||||
"discount": d.discount,
|
|
||||||
}
|
|
||||||
for d in item.discounts
|
|
||||||
if d.discount != 0
|
|
||||||
],
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def blank_customer_info(db: Session) -> schemas.CustomerBlank:
|
|
||||||
return schemas.CustomerBlank(
|
return schemas.CustomerBlank(
|
||||||
name="",
|
name="",
|
||||||
address="",
|
address="",
|
||||||
|
@ -175,6 +161,6 @@ def blank_customer_info(db: Session) -> schemas.CustomerBlank:
|
||||||
"name": sc.name,
|
"name": sc.name,
|
||||||
"discount": 0,
|
"discount": 0,
|
||||||
}
|
}
|
||||||
for sc in db.execute(select(SaleCategory).order_by(SaleCategory.name)).scalars().all()
|
for sc in sale_categories
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
|
|
|
@ -21,6 +21,7 @@ export class CustomerListComponent implements OnInit {
|
||||||
ngOnInit() {
|
ngOnInit() {
|
||||||
this.route.data.subscribe((value) => {
|
this.route.data.subscribe((value) => {
|
||||||
const data = value as { list: Customer[] };
|
const data = value as { list: Customer[] };
|
||||||
|
data.list.forEach((c) => (c.discounts = c.discounts.filter((d) => d.discount !== 0)));
|
||||||
this.list = data.list;
|
this.list = data.list;
|
||||||
});
|
});
|
||||||
this.dataSource = new CustomerListDatasource(this.list);
|
this.dataSource = new CustomerListDatasource(this.list);
|
||||||
|
|
Loading…
Reference in New Issue