From 96674254fa64a3343df3426a2f1227d5b7ec1b0f Mon Sep 17 00:00:00 2001 From: Charles-Henri Decultot Date: Fri, 24 Nov 2023 21:45:04 +0100 Subject: [PATCH 1/2] fix: If not explicitely provided, all entries in a single posting should have the same journal --- erpnext/accounts/general_ledger.py | 9 ++++++- erpnext/patches.txt | 1 + ...ccounting_journal_for_bank_transactions.py | 27 +++++++++++++++++++ 3 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 erpnext/patches/dokos/v3_0/single_accounting_journal_for_bank_transactions.py diff --git a/erpnext/accounts/general_ledger.py b/erpnext/accounts/general_ledger.py index 8b84ae1fec7..42a14402591 100644 --- a/erpnext/accounts/general_ledger.py +++ b/erpnext/accounts/general_ledger.py @@ -307,10 +307,10 @@ def save_entries(gl_map, adv_adj, update_outstanding, from_repost=False): accounting_number = get_accounting_number(gl_map[0]) for entry in gl_map: + entry["accounting_entry_number"] = accounting_number if not entry.get("accounting_journal"): get_accounting_journal(entry) - entry["accounting_entry_number"] = accounting_number make_entry(entry, adv_adj, update_outstanding, from_repost) @@ -372,6 +372,13 @@ def get_accounting_journal(entry): ]: entry["accounting_journal"] = [rule for rule in applicable_rules if not rule.condition][0].name + if not entry.get("accounting_journal"): + entry["accounting_journal"] = frappe.db.get_value( + "GL Entry", + dict(accounting_entry_number=entry.get("accounting_entry_number")), + "accounting_journal", + ) + if not entry.get("accounting_journal") and cint( frappe.db.get_single_value("Accounts Settings", "mandatory_accounting_journal") ): diff --git a/erpnext/patches.txt b/erpnext/patches.txt index b4e84c80252..29eaeb3bb67 100644 --- a/erpnext/patches.txt +++ b/erpnext/patches.txt @@ -284,6 +284,7 @@ erpnext.patches.dokos.v3_0.migrate_ecommerce_settings execute:frappe.delete_doc_if_exists("Report", "Eway Bill") erpnext.patches.dokos.v3_0.set_all_leads_as_draft erpnext.patches.dokos.v3_0.update_remarks +erpnext.patches.dokos.v3_0.single_accounting_journal_for_bank_transactions [post_model_sync] execute:frappe.delete_doc_if_exists('Workspace', 'ERPNext Integrations Settings') diff --git a/erpnext/patches/dokos/v3_0/single_accounting_journal_for_bank_transactions.py b/erpnext/patches/dokos/v3_0/single_accounting_journal_for_bank_transactions.py new file mode 100644 index 00000000000..2487f7373ac --- /dev/null +++ b/erpnext/patches/dokos/v3_0/single_accounting_journal_for_bank_transactions.py @@ -0,0 +1,27 @@ +from itertools import groupby + +import frappe +from frappe.utils import nowdate + +from erpnext.accounts.utils import get_fiscal_year + + +def execute(): + payment_entries = frappe.get_all( + "GL Entry", + filters={"voucher_type": "Payment Entry", "fiscal_year": get_fiscal_year(nowdate())[0]}, + fields=["accounting_entry_number", "name", "account", "accounting_journal"], + ) + party_accounts = frappe.get_all( + "Account", filters={"account_type": ("in", ("Payable", "Receivable"))}, pluck="name" + ) + for _, group in groupby(payment_entries, lambda x: x["accounting_entry_number"]): + entries = list(group) + if len(set([g.accounting_journal for g in entries])) == 1: + continue + + if party_entries := [g.accounting_journal for g in entries if g.account in party_accounts]: + party_journal = party_entries[0] + for g in entries: + if g.accounting_journal != party_journal: + frappe.db.set_value("GL Entry", g.name, "accounting_journal", party_journal) -- GitLab From 08f00d1519630d61d9c2b0a1e351d8affe37e20b Mon Sep 17 00:00:00 2001 From: Charles-Henri Decultot Date: Mon, 27 Nov 2023 09:31:56 +0100 Subject: [PATCH 2/2] fix: Post deductions if missing --- ...ccounting_journal_for_bank_transactions.py | 46 ++++++++++++++++++- 1 file changed, 45 insertions(+), 1 deletion(-) diff --git a/erpnext/patches/dokos/v3_0/single_accounting_journal_for_bank_transactions.py b/erpnext/patches/dokos/v3_0/single_accounting_journal_for_bank_transactions.py index 2487f7373ac..11421f207dc 100644 --- a/erpnext/patches/dokos/v3_0/single_accounting_journal_for_bank_transactions.py +++ b/erpnext/patches/dokos/v3_0/single_accounting_journal_for_bank_transactions.py @@ -3,6 +3,7 @@ from itertools import groupby import frappe from frappe.utils import nowdate +from erpnext.accounts.general_ledger import make_entry, process_gl_map from erpnext.accounts.utils import get_fiscal_year @@ -10,7 +11,15 @@ def execute(): payment_entries = frappe.get_all( "GL Entry", filters={"voucher_type": "Payment Entry", "fiscal_year": get_fiscal_year(nowdate())[0]}, - fields=["accounting_entry_number", "name", "account", "accounting_journal"], + fields=[ + "accounting_entry_number", + "name", + "account", + "accounting_journal", + "credit", + "debit", + "voucher_no", + ], ) party_accounts = frappe.get_all( "Account", filters={"account_type": ("in", ("Payable", "Receivable"))}, pluck="name" @@ -25,3 +34,38 @@ def execute(): for g in entries: if g.accounting_journal != party_journal: frappe.db.set_value("GL Entry", g.name, "accounting_journal", party_journal) + + for cancelled_state in [0, 1]: + payment_entries = frappe.get_all( + "GL Entry", + filters={ + "voucher_type": "Payment Entry", + "fiscal_year": get_fiscal_year(nowdate())[0], + "is_cancelled": cancelled_state, + }, + fields=[ + "accounting_entry_number", + "name", + "account", + "accounting_journal", + "credit", + "debit", + "voucher_no", + ], + ) + for _, group in groupby( + sorted(payment_entries, key=lambda x: x["accounting_entry_number"]), + lambda x: x["accounting_entry_number"], + ): + entries = list(group) + debit = sum(x.debit for x in entries) + credit = sum(x.credit for x in entries) + if frappe.utils.flt(debit, 2) != frappe.utils.flt(credit, 2): + doc = frappe.get_doc("Payment Entry", entries[0].voucher_no) + gl_entries = [] + doc.add_deductions_gl_entries(gl_entries) + gl_entries = process_gl_map(gl_entries) + for gl_entry in gl_entries: + gl_entry["accounting_entry_number"] = entries[0].accounting_entry_number + gl_entry["accounting_journal"] = entries[0].accounting_journal + make_entry(gl_entry, False, "False") -- GitLab