Fix Decimal precision issue

This commit is contained in:
tcprod 2025-02-04 19:05:19 +01:00
parent 43ce42052e
commit 3655e8eb53
4 changed files with 30 additions and 19 deletions

View File

@ -28,7 +28,7 @@ CREATE TABLE `catering_menu_items` (
`catering_menu_item_id` int(11) NOT NULL AUTO_INCREMENT, `catering_menu_item_id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(45) NOT NULL, `name` varchar(45) NOT NULL,
`additional_info` varchar(300) DEFAULT '', `additional_info` varchar(300) DEFAULT '',
`price` int(11) NOT NULL DEFAULT 0, `price` varchar(45) NOT NULL DEFAULT '0',
`category` varchar(80) NOT NULL, `category` varchar(80) NOT NULL,
`is_disabled` tinyint(4) DEFAULT 0, `is_disabled` tinyint(4) DEFAULT 0,
PRIMARY KEY (`catering_menu_item_id`) PRIMARY KEY (`catering_menu_item_id`)

View File

@ -13,6 +13,7 @@ from src.ez_lan_manager.types.Seat import Seat
logger = logging.getLogger(__name__.split(".")[-1]) logger = logging.getLogger(__name__.split(".")[-1])
class CateringOrderInfoPopup(Component): class CateringOrderInfoPopup(Component):
order: Optional[CateringOrder] = None order: Optional[CateringOrder] = None
close_cb: Optional[Callable] = None close_cb: Optional[Callable] = None
@ -20,17 +21,18 @@ class CateringOrderInfoPopup(Component):
def build(self) -> Component: def build(self) -> Component:
if not self.order: if not self.order:
return Card( return Card(
content=Text(""), content=Text(""),
margin=1, margin=1,
color=self.session.theme.hud_color, color=self.session.theme.hud_color,
min_width=40, min_width=40,
min_height=40, min_height=40,
on_press=self.close_cb on_press=self.close_cb
) )
rows = [] rows = []
is_contrast_line = True is_contrast_line = True
for item, amount in self.order.items.items(): for item, amount in self.order.items.items():
style = TextStyle(fill=self.session.theme.secondary_color if is_contrast_line else self.session.theme.neutral_color) style = TextStyle(
fill=self.session.theme.secondary_color if is_contrast_line else self.session.theme.neutral_color)
is_contrast_line = not is_contrast_line is_contrast_line = not is_contrast_line
rows.append( rows.append(
Row( Row(
@ -44,7 +46,8 @@ class CateringOrderInfoPopup(Component):
Text(f"Bestellung {self.order.order_id}", style=TextStyle(font_size=1.2), margin_bottom=1), Text(f"Bestellung {self.order.order_id}", style=TextStyle(font_size=1.2), margin_bottom=1),
*rows, *rows,
Spacer(), Spacer(),
Row(Text("Gesamtpreis:"), Spacer(), Text(self.session[AccountingService].make_euro_string_from_int(self.order.price))) Row(Text("Gesamtpreis:"), Spacer(),
Text(self.session[AccountingService].make_euro_string_from_decimal(self.order.price)))
), ),
margin=1, margin=1,
color=self.session.theme.hud_color, color=self.session.theme.hud_color,
@ -56,11 +59,13 @@ class CateringOrderInfoPopup(Component):
colorize_on_hover=False colorize_on_hover=False
) )
@dataclass @dataclass
class CateringOrderWithSeat: class CateringOrderWithSeat:
catering_order: CateringOrder catering_order: CateringOrder
seat: Optional[Seat] seat: Optional[Seat]
class ManageCateringPage(Component): class ManageCateringPage(Component):
all_orders: list[CateringOrderWithSeat] = field(default_factory=list) all_orders: list[CateringOrderWithSeat] = field(default_factory=list)
last_updated: Optional[datetime] = None last_updated: Optional[datetime] = None
@ -73,7 +78,6 @@ class ManageCateringPage(Component):
self.all_orders = await self.populate_seating(await self.session[CateringService].get_orders()) self.all_orders = await self.populate_seating(await self.session[CateringService].get_orders())
self.last_updated = datetime.now() self.last_updated = datetime.now()
@event.periodic(30) @event.periodic(30)
async def update_orders(self) -> None: async def update_orders(self) -> None:
polled_orders = await self.session[CateringService].get_orders() polled_orders = await self.session[CateringService].get_orders()
@ -88,12 +92,16 @@ class ManageCateringPage(Component):
return result return result
def get_all_pending_orders(self) -> list[CateringOrderWithSeat]: def get_all_pending_orders(self) -> list[CateringOrderWithSeat]:
filtered_list = list(filter(lambda o: o.catering_order.status != CateringOrderStatus.COMPLETED and o.catering_order.status != CateringOrderStatus.CANCELED, self.all_orders)) filtered_list = list(filter(lambda
o: o.catering_order.status != CateringOrderStatus.COMPLETED and o.catering_order.status != CateringOrderStatus.CANCELED,
self.all_orders))
sorted_list = sorted(filtered_list, key=lambda o: o.catering_order.order_date) sorted_list = sorted(filtered_list, key=lambda o: o.catering_order.order_date)
return sorted_list return sorted_list
def get_all_completed_orders(self) -> list[CateringOrderWithSeat]: def get_all_completed_orders(self) -> list[CateringOrderWithSeat]:
filtered_list = list(filter(lambda o: o.catering_order.status == CateringOrderStatus.COMPLETED or o.catering_order.status == CateringOrderStatus.CANCELED, self.all_orders)) filtered_list = list(filter(lambda
o: o.catering_order.status == CateringOrderStatus.COMPLETED or o.catering_order.status == CateringOrderStatus.CANCELED,
self.all_orders))
sorted_list = sorted(filtered_list, key=lambda o: o.catering_order.order_date) sorted_list = sorted(filtered_list, key=lambda o: o.catering_order.order_date)
return sorted_list return sorted_list
@ -154,7 +162,8 @@ class ManageCateringPage(Component):
margin_bottom=1, margin_bottom=1,
on_press=self.update_orders on_press=self.update_orders
), ),
*[CateringManagementOrderDisplay(v.catering_order, v.seat, self.order_clicked) for v in self.get_all_pending_orders()], *[CateringManagementOrderDisplay(v.catering_order, v.seat, self.order_clicked) for v in
self.get_all_pending_orders()],
) )
), ),
MainViewContentBox( MainViewContentBox(
@ -169,7 +178,8 @@ class ManageCateringPage(Component):
margin_bottom=0.2, margin_bottom=0.2,
align_x=0.5 align_x=0.5
), ),
*[CateringManagementOrderDisplay(v.catering_order, v.seat, self.order_clicked) for v in self.get_all_completed_orders()], *[CateringManagementOrderDisplay(v.catering_order, v.seat, self.order_clicked) for v in
self.get_all_completed_orders()],
) )
), ),
Spacer() Spacer()

View File

@ -485,7 +485,7 @@ class DatabaseService:
item_id=menu_item_raw[0], item_id=menu_item_raw[0],
name=menu_item_raw[1], name=menu_item_raw[1],
additional_info=menu_item_raw[2], additional_info=menu_item_raw[2],
price=menu_item_raw[3], price=Decimal(menu_item_raw[3]),
category=CateringMenuItemCategory(menu_item_raw[4]), category=CateringMenuItemCategory(menu_item_raw[4]),
is_disabled=bool(menu_item_raw[5]) is_disabled=bool(menu_item_raw[5])
)) ))
@ -515,7 +515,7 @@ class DatabaseService:
item_id=raw_data[0], item_id=raw_data[0],
name=raw_data[1], name=raw_data[1],
additional_info=raw_data[2], additional_info=raw_data[2],
price=raw_data[3], price=Decimal(raw_data[3]),
category=CateringMenuItemCategory(raw_data[4]), category=CateringMenuItemCategory(raw_data[4]),
is_disabled=bool(raw_data[5]) is_disabled=bool(raw_data[5])
) )
@ -707,7 +707,7 @@ class DatabaseService:
item_id=order_catering_menu_item_raw[1], item_id=order_catering_menu_item_raw[1],
name=order_catering_menu_item_raw[4], name=order_catering_menu_item_raw[4],
additional_info=order_catering_menu_item_raw[5], additional_info=order_catering_menu_item_raw[5],
price=order_catering_menu_item_raw[6], price=Decimal(order_catering_menu_item_raw[6]),
category=CateringMenuItemCategory(order_catering_menu_item_raw[7]), category=CateringMenuItemCategory(order_catering_menu_item_raw[7]),
is_disabled=bool(order_catering_menu_item_raw[8]) is_disabled=bool(order_catering_menu_item_raw[8])
)] = order_catering_menu_item_raw[2] )] = order_catering_menu_item_raw[2]

View File

@ -18,6 +18,7 @@ class CateringOrderStatus(StrEnum):
COMPLETED = "COMPLETED" COMPLETED = "COMPLETED"
CANCELED = "CANCELED" CANCELED = "CANCELED"
@dataclass(frozen=True) @dataclass(frozen=True)
class CateringOrder: class CateringOrder:
order_id: int order_id: int
@ -31,7 +32,7 @@ class CateringOrder:
def price(self) -> Decimal: def price(self) -> Decimal:
total = Decimal("0") total = Decimal("0")
for item, amount in self.items.items(): for item, amount in self.items.items():
total += (item.price * Decimal(amount)) total += (item.price * amount)
return total return total
@staticmethod @staticmethod