From eebd058891b60d71423bbe642bc13ba4873cfd7f Mon Sep 17 00:00:00 2001 From: ruthra kumar Date: Mon, 25 Nov 2024 15:56:41 +0530 Subject: [PATCH] refactor: clear unallocated payments from POS Invoice --- .../test_pos_closing_entry.py | 18 ++++++++++++++++-- .../doctype/pos_invoice/pos_invoice.py | 7 +++++++ .../test_pos_invoice_merge_log.py | 2 ++ 3 files changed, 25 insertions(+), 2 deletions(-) diff --git a/erpnext/accounts/doctype/pos_closing_entry/test_pos_closing_entry.py b/erpnext/accounts/doctype/pos_closing_entry/test_pos_closing_entry.py index aaf46b337e9..7ce87af95c5 100644 --- a/erpnext/accounts/doctype/pos_closing_entry/test_pos_closing_entry.py +++ b/erpnext/accounts/doctype/pos_closing_entry/test_pos_closing_entry.py @@ -218,11 +218,25 @@ class TestPOSClosingEntry(IntegrationTestCase): opening_entry = create_opening_entry(pos_profile, test_user.name) pos_inv = create_pos_invoice( - item_code=item_code, qty=5, rate=300, use_serial_batch_fields=1, batch_no=batch_no + item_code=item_code, + qty=5, + rate=300, + use_serial_batch_fields=1, + batch_no=batch_no, + do_not_submit=True, ) + pos_inv.payments[0].amount = pos_inv.grand_total + pos_inv.submit() pos_inv2 = create_pos_invoice( - item_code=item_code, qty=5, rate=300, use_serial_batch_fields=1, batch_no=batch_no + item_code=item_code, + qty=5, + rate=300, + use_serial_batch_fields=1, + batch_no=batch_no, + do_not_submit=True, ) + pos_inv2.payments[0].amount = pos_inv2.grand_total + pos_inv2.submit() batch_qty = frappe.db.get_value("Batch", batch_no, "batch_qty") self.assertEqual(batch_qty, 10) diff --git a/erpnext/accounts/doctype/pos_invoice/pos_invoice.py b/erpnext/accounts/doctype/pos_invoice/pos_invoice.py index 6e499f647c1..347f9b6e6ec 100644 --- a/erpnext/accounts/doctype/pos_invoice/pos_invoice.py +++ b/erpnext/accounts/doctype/pos_invoice/pos_invoice.py @@ -240,6 +240,7 @@ class POSInvoice(SalesInvoice): from erpnext.accounts.doctype.pricing_rule.utils import update_coupon_code_count update_coupon_code_count(self.coupon_code, "used") + self.clear_unallocated_mode_of_payments() def before_cancel(self): if ( @@ -278,6 +279,12 @@ class POSInvoice(SalesInvoice): self.delink_serial_and_batch_bundle() + def clear_unallocated_mode_of_payments(self): + self.set("payments", self.get("payments", {"amount": ["not in", [0, None, ""]]})) + + sip = frappe.qb.DocType("Sales Invoice Payment") + frappe.qb.from_(sip).delete().where(sip.parent == self.name).where(sip.amount == 0).run() + def delink_serial_and_batch_bundle(self): for row in self.items: if row.serial_and_batch_bundle: 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 e64c9e979b3..5ccefda16f6 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 @@ -134,6 +134,7 @@ class TestPOSInvoiceMergeLog(IntegrationTestCase): }, ) inv.insert() + inv.payments[0].amount = inv.grand_total inv.submit() inv2 = create_pos_invoice(qty=1, rate=100, do_not_save=True) @@ -150,6 +151,7 @@ class TestPOSInvoiceMergeLog(IntegrationTestCase): }, ) inv2.insert() + inv2.payments[0].amount = inv.grand_total inv2.submit() consolidate_pos_invoices()