From 4708f5cd3d0ec021f46619ce8f859cf5a6700514 Mon Sep 17 00:00:00 2001 From: Charles-Henri Decultot Date: Fri, 6 Oct 2023 12:09:12 +0200 Subject: [PATCH] fix: Don't cancel payment ledger entries when adjusting accounting journal --- .../accounting_journal/accounting_journal.py | 5 +- .../test_accounting_journal.py | 84 ++++++++++++++++++- erpnext/accounts/general_ledger.py | 14 +++- 3 files changed, 94 insertions(+), 9 deletions(-) diff --git a/erpnext/accounts/doctype/accounting_journal/accounting_journal.py b/erpnext/accounts/doctype/accounting_journal/accounting_journal.py index 2f32244dd9e..1870d1debd6 100644 --- a/erpnext/accounts/doctype/accounting_journal/accounting_journal.py +++ b/erpnext/accounts/doctype/accounting_journal/accounting_journal.py @@ -45,6 +45,7 @@ def get_entries(doctype, docnames): ) +# @dokos @frappe.whitelist() def accounting_journal_adjustment(doctype, docnames, accounting_journal): for docname in frappe.parse_json(docnames): @@ -54,7 +55,9 @@ def accounting_journal_adjustment(doctype, docnames, accounting_journal): filters={"voucher_type": doctype, "voucher_no": docname, "is_cancelled": 0}, ) - make_reverse_gl_entries(voucher_type=doctype, voucher_no=docname) + make_reverse_gl_entries( + voucher_type=doctype, voucher_no=docname, cancel_payment_ledger_entries=False + ) for gl_entry in original_entries: gl_entry["name"] = None diff --git a/erpnext/accounts/doctype/accounting_journal/test_accounting_journal.py b/erpnext/accounts/doctype/accounting_journal/test_accounting_journal.py index eb9f326fa86..bef61e936d0 100644 --- a/erpnext/accounts/doctype/accounting_journal/test_accounting_journal.py +++ b/erpnext/accounts/doctype/accounting_journal/test_accounting_journal.py @@ -2,10 +2,86 @@ # See license.txt -import unittest - import frappe +from frappe.tests.utils import FrappeTestCase +from frappe.utils import nowdate + +from erpnext.accounts.doctype.accounting_journal.accounting_journal import ( + accounting_journal_adjustment, +) +from erpnext.accounts.doctype.payment_entry.test_payment_entry import get_payment_entry + +test_records = frappe.get_test_records("Sales Invoice") + + +class TestAccountingJournal(FrappeTestCase): + @classmethod + def setUpClass(cls): + frappe.get_doc( + { + "doctype": "Accounting Journal", + "journal_code": "BQ", + "journal_name": "Banque", + "type": "Bank", + "account": "_Test Bank - _TC", + "company": "_Test Company", + "conditions": [ + {"document_type": "Payment Entry"}, + ], + } + ).insert(ignore_if_duplicate=True) + + frappe.get_doc( + { + "doctype": "Accounting Journal", + "journal_code": "MD", + "journal_name": "Miscellaneous Operations", + "type": "Miscellaneous", + "company": "_Test Company", + } + ).insert(ignore_if_duplicate=True) + + def make(self): + si = frappe.copy_doc(test_records[0]) + si.is_pos = 0 + si.insert() + si.submit() + return si + + def test_payment_entry_journal_adjustment(self): + sales_invoice = self.make() + + pe = get_payment_entry("Sales Invoice", sales_invoice.name, bank_account="_Test Bank - _TC") + pe.reference_no = "1" + pe.reference_date = nowdate() + pe.paid_from_account_currency = sales_invoice.currency + pe.paid_to_account_currency = sales_invoice.currency + pe.source_exchange_rate = 1 + pe.target_exchange_rate = 1 + pe.paid_amount = sales_invoice.outstanding_amount + pe.insert() + pe.submit() + + si_status = frappe.db.get_value("Sales Invoice", sales_invoice.name, "status") + self.assertEqual(si_status, "Paid") + + pe_gl_entries = frappe.get_all( + "GL Entry", + filters={"voucher_type": "Payment Entry", "voucher_no": pe.name}, + fields=["name", "accounting_journal"], + ) + accounting_journal = list(set(gl.accounting_journal for gl in pe_gl_entries))[0] + self.assertEqual(accounting_journal, "BQ") + + accounting_journal_adjustment("Payment Entry", [pe.name], "MD") + pe_gl_entries = frappe.get_all( + "GL Entry", + filters={"voucher_type": "Payment Entry", "voucher_no": pe.name, "is_cancelled": 0}, + fields=["name", "accounting_journal"], + ) + accounting_journal = list(set(gl.accounting_journal for gl in pe_gl_entries))[0] + self.assertEqual(accounting_journal, "MD") -class TestAccountingJournal(unittest.TestCase): - pass + si_status = frappe.db.get_value("Sales Invoice", sales_invoice.name, "status") + self.assertEqual(si_status, "Paid") diff --git a/erpnext/accounts/general_ledger.py b/erpnext/accounts/general_ledger.py index 9df2021d7f2..8b84ae1fec7 100644 --- a/erpnext/accounts/general_ledger.py +++ b/erpnext/accounts/general_ledger.py @@ -559,7 +559,12 @@ def get_round_off_account_and_cost_center(company, voucher_type, voucher_no): def make_reverse_gl_entries( - gl_entries=None, voucher_type=None, voucher_no=None, adv_adj=False, update_outstanding="Yes" + gl_entries=None, + voucher_type=None, + voucher_no=None, + adv_adj=False, + update_outstanding="Yes", + cancel_payment_ledger_entries=True, # @dokos ): """ Get original gl entries of the voucher @@ -578,9 +583,10 @@ def make_reverse_gl_entries( ).run(as_dict=1) if gl_entries: - create_payment_ledger_entry( - gl_entries, cancel=1, adv_adj=adv_adj, update_outstanding=update_outstanding - ) + if cancel_payment_ledger_entries: # @dokos + create_payment_ledger_entry( + gl_entries, cancel=1, adv_adj=adv_adj, update_outstanding=update_outstanding + ) validate_accounting_period(gl_entries) check_freezing_date(gl_entries[0]["posting_date"], adv_adj) set_as_cancel(gl_entries[0]["voucher_type"], gl_entries[0]["voucher_no"]) -- GitLab