From 9bd3d7a020ad8ce4e90da1ffdfddfc71510aab74 Mon Sep 17 00:00:00 2001 From: venkat102 Date: Thu, 5 Sep 2024 19:13:31 +0530 Subject: [PATCH 1/2] fix: cancel common party advance jv while canceling the invoice (cherry picked from commit 6a928b92dfa90de8290a3e5829deb69a80c6ccdb) --- erpnext/accounts/utils.py | 40 ++++++++++++++++++++++ erpnext/controllers/accounts_controller.py | 3 ++ 2 files changed, 43 insertions(+) diff --git a/erpnext/accounts/utils.py b/erpnext/accounts/utils.py index 0450221222d..e462f749b54 100644 --- a/erpnext/accounts/utils.py +++ b/erpnext/accounts/utils.py @@ -781,6 +781,46 @@ def cancel_exchange_gain_loss_journal( gain_loss_je.cancel() +def cancel_common_party_journal(self): + if self.doctype not in ["Sales Invoice", "Purchase Invoice"]: + return + + if not frappe.db.get_single_value("Accounts Settings", "enable_common_party_accounting"): + return + + party_link = self.get_common_party_link() + if not party_link: + return + + journal_entry = frappe.db.get_value( + "Journal Entry Account", + filters={ + "reference_type": self.doctype, + "reference_name": self.name, + "docstatus": 1, + }, + fieldname="parent", + ) + + if not journal_entry: + return + + common_party_journal = frappe.db.get_value( + "Journal Entry", + filters={ + "name": journal_entry, + "is_system_generated": True, + "docstatus": 1, + }, + ) + + if not common_party_journal: + return + + common_party_je = frappe.get_doc("Journal Entry", common_party_journal) + common_party_je.cancel() + + def update_accounting_ledgers_after_reference_removal( ref_type: str | None = None, ref_no: str | None = None, payment_name: str | None = None ): diff --git a/erpnext/controllers/accounts_controller.py b/erpnext/controllers/accounts_controller.py index ce5d813b801..fbd717db153 100644 --- a/erpnext/controllers/accounts_controller.py +++ b/erpnext/controllers/accounts_controller.py @@ -1580,6 +1580,7 @@ class AccountsController(TransactionBase): remove_from_bank_transaction, ) from erpnext.accounts.utils import ( + cancel_common_party_journal, cancel_exchange_gain_loss_journal, unlink_ref_doc_from_payment_entries, ) @@ -1591,6 +1592,7 @@ class AccountsController(TransactionBase): # Cancel Exchange Gain/Loss Journal before unlinking cancel_exchange_gain_loss_journal(self) + cancel_common_party_journal(self) if frappe.db.get_single_value("Accounts Settings", "unlink_payment_on_cancellation_of_invoice"): unlink_ref_doc_from_payment_entries(self) @@ -2424,6 +2426,7 @@ class AccountsController(TransactionBase): jv.posting_date = self.posting_date jv.company = self.company jv.remark = f"Adjustment for {self.doctype} {self.name}" + jv.is_system_generated = True reconcilation_entry = frappe._dict() advance_entry = frappe._dict() From 6c74180e1c2fae184ae3300d0bfb9b932e8b9a13 Mon Sep 17 00:00:00 2001 From: venkat102 Date: Thu, 5 Sep 2024 23:30:35 +0530 Subject: [PATCH 2/2] test: add unit test for canceling the common party advance jv created from sales invoice (cherry picked from commit 8c6e3f3c12164f30f044b3bf1fa0b71b66ff837d) --- .../sales_invoice/test_sales_invoice.py | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/erpnext/accounts/doctype/sales_invoice/test_sales_invoice.py b/erpnext/accounts/doctype/sales_invoice/test_sales_invoice.py index fef30bdfecd..ce8627be27f 100644 --- a/erpnext/accounts/doctype/sales_invoice/test_sales_invoice.py +++ b/erpnext/accounts/doctype/sales_invoice/test_sales_invoice.py @@ -3162,6 +3162,50 @@ class TestSalesInvoice(FrappeTestCase): party_link.delete() frappe.db.set_single_value("Accounts Settings", "enable_common_party_accounting", 0) + def test_sales_invoice_cancel_with_common_party_advance_jv(self): + from erpnext.accounts.doctype.opening_invoice_creation_tool.test_opening_invoice_creation_tool import ( + make_customer, + ) + from erpnext.accounts.doctype.party_link.party_link import create_party_link + from erpnext.buying.doctype.supplier.test_supplier import create_supplier + + # create a customer + customer = make_customer(customer="_Test Common Supplier") + # create a supplier + supplier = create_supplier(supplier_name="_Test Common Supplier").name + + # create a party link between customer & supplier + party_link = create_party_link("Supplier", supplier, customer) + + # enable common party accounting + frappe.db.set_single_value("Accounts Settings", "enable_common_party_accounting", 1) + + # create a sales invoice + si = create_sales_invoice(customer=customer) + + # check creation of journal entry + jv = frappe.db.get_value( + "Journal Entry Account", + filters={ + "reference_type": si.doctype, + "reference_name": si.name, + "docstatus": 1, + }, + fieldname="parent", + ) + + self.assertTrue(jv) + + # cancel sales invoice + si.cancel() + + # check cancellation of journal entry + jv_status = frappe.db.get_value("Journal Entry", jv, "docstatus") + self.assertEqual(jv_status, 2) + + party_link.delete() + frappe.db.set_single_value("Accounts Settings", "enable_common_party_accounting", 0) + def test_payment_statuses(self): from erpnext.accounts.doctype.payment_entry.test_payment_entry import get_payment_entry