diff --git a/erpnext/accounts/doctype/payment_entry/payment_entry.py b/erpnext/accounts/doctype/payment_entry/payment_entry.py index eb1673a8798..350e8b700a9 100644 --- a/erpnext/accounts/doctype/payment_entry/payment_entry.py +++ b/erpnext/accounts/doctype/payment_entry/payment_entry.py @@ -1285,8 +1285,11 @@ class PaymentEntry(AccountsController): def make_gl_entries(self, cancel=0, adv_adj=0): gl_entries = self.build_gl_map() - gl_entries = process_gl_map(gl_entries) - make_gl_entries(gl_entries, cancel=cancel, adv_adj=adv_adj) + + merge_entries = frappe.get_single_value("Accounts Settings", "merge_similar_account_heads") + + gl_entries = process_gl_map(gl_entries, merge_entries=merge_entries) + make_gl_entries(gl_entries, cancel=cancel, adv_adj=adv_adj, merge_entries=merge_entries) if cancel: cancel_exchange_gain_loss_journal(frappe._dict(doctype=self.doctype, name=self.name)) else: diff --git a/erpnext/accounts/doctype/payment_entry/test_payment_entry.py b/erpnext/accounts/doctype/payment_entry/test_payment_entry.py index 355aa46ea05..f6c240c0714 100644 --- a/erpnext/accounts/doctype/payment_entry/test_payment_entry.py +++ b/erpnext/accounts/doctype/payment_entry/test_payment_entry.py @@ -1045,6 +1045,7 @@ class TestPaymentEntry(IntegrationTestCase): ) def test_gl_of_multi_currency_payment_with_taxes(self): + frappe.db.set_single_value("Accounts Settings", "merge_similar_account_heads", 1) payment_entry = create_payment_entry( party="_Test Supplier USD", paid_to="_Test Payable USD - _TC", save=True ) @@ -1606,6 +1607,96 @@ class TestPaymentEntry(IntegrationTestCase): self.voucher_no = pe.name self.check_gl_entries() + def test_payment_entry_merges_gl_entries_with_same_account_head(self): + """ + Test that Payment Entry merges GL entries with same account head + when 'Merge Similar Account Heads' setting is enabled. + """ + frappe.db.set_single_value("Accounts Settings", "merge_similar_account_heads", 1) + + pe = create_payment_entry( + party_type="Supplier", + party="_Test Supplier", + paid_from="_Test Bank - _TC", + paid_to="Creditors - _TC", + ) + + pe.append( + "deductions", + { + "account": "Write Off - _TC", + "cost_center": "_Test Cost Center - _TC", + "amount": 50, + }, + ) + + pe.append( + "deductions", + { + "account": "Write Off - _TC", + "cost_center": "_Test Cost Center - _TC", + "amount": 30, + }, + ) + + pe.save() + pe.submit() + + gl_entries = frappe.db.get_all( + "GL Entry", + filters={"voucher_no": pe.name, "account": "Write Off - _TC", "is_cancelled": 0}, + fields=["debit", "credit"], + ) + + self.assertEqual(len(gl_entries), 1) + self.assertEqual(gl_entries[0].debit, 80) + + def test_payment_entry_does_not_merge_gl_entries_when_setting_disabled(self): + """ + Test that Payment Entry does NOT merge GL entries + when 'Merge Similar Account Heads' is disabled. + """ + + frappe.db.set_single_value("Accounts Settings", "merge_similar_account_heads", 0) + + pe = create_payment_entry( + party_type="Supplier", + party="_Test Supplier", + paid_from="_Test Bank - _TC", + paid_to="Creditors - _TC", + ) + + pe.append( + "deductions", + { + "account": "Write Off - _TC", + "cost_center": "_Test Cost Center - _TC", + "amount": 50, + }, + ) + + pe.append( + "deductions", + { + "account": "Write Off - _TC", + "cost_center": "_Test Cost Center - _TC", + "amount": 30, + }, + ) + + pe.save() + pe.submit() + + gl_entries = frappe.db.get_all( + "GL Entry", + filters={"voucher_no": pe.name, "account": "Write Off - _TC", "is_cancelled": 0}, + fields=["debit", "credit"], + ) + + self.assertEqual(len(gl_entries), 2) + + frappe.db.set_single_value("Accounts Settings", "merge_similar_account_heads", 1) + def check_pl_entries(self): ple = frappe.qb.DocType("Payment Ledger Entry") pl_entries = ( diff --git a/erpnext/accounts/doctype/purchase_invoice/test_purchase_invoice.py b/erpnext/accounts/doctype/purchase_invoice/test_purchase_invoice.py index adb11f8d615..cb771bdc35a 100644 --- a/erpnext/accounts/doctype/purchase_invoice/test_purchase_invoice.py +++ b/erpnext/accounts/doctype/purchase_invoice/test_purchase_invoice.py @@ -1498,6 +1498,8 @@ class TestPurchaseInvoice(IntegrationTestCase, StockTestMixin): def test_purchase_invoice_advance_taxes(self): from erpnext.accounts.doctype.payment_entry.payment_entry import get_payment_entry + frappe.db.set_single_value("Accounts Settings", "merge_similar_account_heads", 1) + company = "_Test Company" tds_account_args = {