From c2b83a02837e8ab9c2e23596f22b7f75e420003f Mon Sep 17 00:00:00 2001 From: Saqib Ansari Date: Tue, 8 Feb 2022 17:07:51 +0530 Subject: [PATCH] fix(test): case if write off is calculated as negative amount --- .../pos_invoice_merge_log/pos_invoice_merge_log.py | 1 - .../test_pos_invoice_merge_log.py | 11 ++++++----- erpnext/controllers/taxes_and_totals.py | 6 +++--- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/erpnext/accounts/doctype/pos_invoice_merge_log/pos_invoice_merge_log.py b/erpnext/accounts/doctype/pos_invoice_merge_log/pos_invoice_merge_log.py index d98dec8b92..ddca68a57b 100644 --- a/erpnext/accounts/doctype/pos_invoice_merge_log/pos_invoice_merge_log.py +++ b/erpnext/accounts/doctype/pos_invoice_merge_log/pos_invoice_merge_log.py @@ -95,7 +95,6 @@ class POSInvoiceMergeLog(Document): pos_invoice_grand_total = sum(d.grand_total for d in data) if abs(pos_invoice_grand_total - invoice.grand_total) < 1: - invoice.write_off_amount += -1 * (pos_invoice_grand_total - invoice.grand_total) invoice.save() diff --git a/erpnext/accounts/doctype/pos_invoice_merge_log/test_pos_invoice_merge_log.py b/erpnext/accounts/doctype/pos_invoice_merge_log/test_pos_invoice_merge_log.py index fd1aaab264..fc14161456 100644 --- a/erpnext/accounts/doctype/pos_invoice_merge_log/test_pos_invoice_merge_log.py +++ b/erpnext/accounts/doctype/pos_invoice_merge_log/test_pos_invoice_merge_log.py @@ -154,10 +154,10 @@ class TestPOSInvoiceMergeLog(unittest.TestCase): def test_consolidation_round_off_error_1(self): ''' - Test case for bug: - Round off error in consolidated invoice creation if POS Invoice has inclusive tax + Test round off error in consolidated invoice creation if POS Invoice has inclusive tax ''' frappe.db.sql("delete from `tabPOS Invoice`") + allow_negative_stock = frappe.db.get_value('Stock Settings', None, 'allow_negative_stock') frappe.db.set_value('Stock Settings', None, 'allow_negative_stock', 1) try: @@ -206,13 +206,14 @@ class TestPOSInvoiceMergeLog(unittest.TestCase): frappe.set_user("Administrator") frappe.db.sql("delete from `tabPOS Profile`") frappe.db.sql("delete from `tabPOS Invoice`") - frappe.db.set_value('Stock Settings', None, 'allow_negative_stock', 0) + frappe.db.set_value('Stock Settings', None, 'allow_negative_stock', allow_negative_stock) def test_consolidation_round_off_error_2(self): ''' Test the same case as above but with an Unpaid POS Invoice ''' frappe.db.sql("delete from `tabPOS Invoice`") + allow_negative_stock = frappe.db.get_value('Stock Settings', None, 'allow_negative_stock') frappe.db.set_value('Stock Settings', None, 'allow_negative_stock', 1) try: @@ -262,10 +263,10 @@ class TestPOSInvoiceMergeLog(unittest.TestCase): inv.load_from_db() consolidated_invoice = frappe.get_doc('Sales Invoice', inv.consolidated_invoice) self.assertEqual(consolidated_invoice.outstanding_amount, 800) - self.assertEqual(consolidated_invoice.status, 'Unpaid') + self.assertNotEqual(consolidated_invoice.status, 'Paid') finally: frappe.set_user("Administrator") frappe.db.sql("delete from `tabPOS Profile`") frappe.db.sql("delete from `tabPOS Invoice`") - frappe.db.set_value('Stock Settings', None, 'allow_negative_stock', 0) + frappe.db.set_value('Stock Settings', None, 'allow_negative_stock', allow_negative_stock) diff --git a/erpnext/controllers/taxes_and_totals.py b/erpnext/controllers/taxes_and_totals.py index 5d1856cfa9..de1099ee28 100644 --- a/erpnext/controllers/taxes_and_totals.py +++ b/erpnext/controllers/taxes_and_totals.py @@ -650,12 +650,12 @@ class calculate_taxes_and_totals(object): def calculate_change_amount(self): self.doc.change_amount = 0.0 self.doc.base_change_amount = 0.0 + grand_total = self.doc.rounded_total or self.doc.grand_total + base_grand_total = self.doc.base_rounded_total or self.doc.base_grand_total if self.doc.doctype == "Sales Invoice" \ - and self.doc.paid_amount > self.doc.grand_total and not self.doc.is_return \ + and self.doc.paid_amount > grand_total and not self.doc.is_return \ and any(d.type == "Cash" for d in self.doc.payments): - grand_total = self.doc.rounded_total or self.doc.grand_total - base_grand_total = self.doc.base_rounded_total or self.doc.base_grand_total self.doc.change_amount = flt(self.doc.paid_amount - grand_total + self.doc.write_off_amount, self.doc.precision("change_amount"))