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.

This commit is contained in:
Amritanshu Agrawal 2021-07-05 08:28:38 +05:30
parent 8c5d941850
commit c2483ae321
2 changed files with 26 additions and 7 deletions

View File

@ -52,16 +52,24 @@ class Inventory(Base):
tax_id, tax_id,
tax_rate, tax_rate,
sort_order, sort_order,
product=None,
tax=None,
): ):
self.kot_id = kot_id 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.quantity = quantity
self.price = price self.price = price
self.discount = discount self.discount = discount
self.is_happy_hour = is_hh 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.tax_rate = tax_rate
self.sort_order = sort_order self.sort_order = sort_order
if product is not None:
self.product = product
if tax is not None:
self.tax = tax
@hybrid_property @hybrid_property
def effective_price(self): def effective_price(self):

View File

@ -45,9 +45,20 @@ def print_bill(voucher_id: uuid.UUID, db: Session):
) )
if key in items_dict: if key in items_dict:
items_dict[key].quantity += i.quantity items_dict[key].quantity += i.quantity
i.quantity = 0
else: 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]: 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) get_tax_item(i.tax.id, i.tax.name, i.tax_amount, tax)
@ -132,14 +143,14 @@ def design_bill(
s += "\n\r" + "------------------------------------------" s += "\n\r" + "------------------------------------------"
# Totals # 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}" 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: if amount != 0:
s += f"\n\r{'Happy Hour Discount :': >32} {currency_format(amount): >9}" 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: if amount != 0:
s += f"\n\r{'Discount :': >32} {currency_format(amount): >9}" s += f"\n\r{'Discount :': >32} {currency_format(amount): >9}"