From c2483ae321f284ae2e23100a81350d652178e587 Mon Sep 17 00:00:00 2001 From: tanshu Date: Mon, 5 Jul 2021 08:28:38 +0530 Subject: [PATCH] Fix: Edge cases where rounding due to changed quantities in printing was altering the final amount in decimals, at times resulted in final rounding to change up or down. --- barker/barker/models/inventory.py | 12 ++++++++++-- barker/barker/printing/bill.py | 21 ++++++++++++++++----- 2 files changed, 26 insertions(+), 7 deletions(-) diff --git a/barker/barker/models/inventory.py b/barker/barker/models/inventory.py index ccfb41d..287734e 100644 --- a/barker/barker/models/inventory.py +++ b/barker/barker/models/inventory.py @@ -52,16 +52,24 @@ class Inventory(Base): tax_id, tax_rate, sort_order, + product=None, + tax=None, ): self.kot_id = kot_id - self.product_id = product_id + if product_id is not None: + self.product_id = product_id self.quantity = quantity self.price = price self.discount = discount self.is_happy_hour = is_hh - self.tax_id = tax_id + if tax_id is not None: + self.tax_id = tax_id self.tax_rate = tax_rate self.sort_order = sort_order + if product is not None: + self.product = product + if tax is not None: + self.tax = tax @hybrid_property def effective_price(self): diff --git a/barker/barker/printing/bill.py b/barker/barker/printing/bill.py index da5e6ed..e276556 100644 --- a/barker/barker/printing/bill.py +++ b/barker/barker/printing/bill.py @@ -45,9 +45,20 @@ def print_bill(voucher_id: uuid.UUID, db: Session): ) if key in items_dict: items_dict[key].quantity += i.quantity - i.quantity = 0 else: - items_dict[key] = i + items_dict[key] = Inventory( + None, + i.product_id, + i.quantity, + i.price, + i.discount, + i.is_happy_hour, + None, + i.tax_rate, + 0, + i.product, + i.tax, + ) for i in [it for it in items_dict.values() if it.quantity != 0]: get_tax_item(i.tax.id, i.tax.name, i.tax_amount, tax) @@ -132,14 +143,14 @@ def design_bill( s += "\n\r" + "------------------------------------------" # Totals - amount = sum(i.quantity * i.price for i in items) + amount = sum(round(i.quantity * i.price, 2) for i in items) s += f"\n\r{'Subtotal :': >32} {currency_format(amount): >9}" - amount = sum(i.quantity * i.price for i in items if i.is_happy_hour) + amount = sum(round(i.quantity * i.price, 2) for i in items if i.is_happy_hour) if amount != 0: s += f"\n\r{'Happy Hour Discount :': >32} {currency_format(amount): >9}" - amount = sum(i.quantity * i.effective_price * i.discount for i in items) + amount = sum(round(i.quantity * i.effective_price * i.discount, 2) for i in items) if amount != 0: s += f"\n\r{'Discount :': >32} {currency_format(amount): >9}"