From aeae8d646a1efdc88866d7f08518d57bcf78b1b6 Mon Sep 17 00:00:00 2001 From: marination Date: Thu, 20 Jul 2023 19:00:39 +0530 Subject: [PATCH 1/2] fix: Patch dunnings made after accounts were frozen - Consider "Accounts Frozen Until" and Period Closing Voucher --- .../patches/v14_0/single_to_multi_dunning.py | 29 ++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/erpnext/patches/v14_0/single_to_multi_dunning.py b/erpnext/patches/v14_0/single_to_multi_dunning.py index 7a8e591798..3567522eb3 100644 --- a/erpnext/patches/v14_0/single_to_multi_dunning.py +++ b/erpnext/patches/v14_0/single_to_multi_dunning.py @@ -7,7 +7,15 @@ def execute(): frappe.reload_doc("accounts", "doctype", "overdue_payment") frappe.reload_doc("accounts", "doctype", "dunning") - all_dunnings = frappe.get_all("Dunning", filters={"docstatus": ("!=", 2)}, pluck="name") + filters = {"docstatus": ("!=", 2)} + + can_edit_accounts_after = get_accounts_closing_date() + if can_edit_accounts_after: + # Get dunnings after the date when accounts were frozen/closed + filters["posting_date"] = (">", can_edit_accounts_after) + + all_dunnings = frappe.get_all("Dunning", filters=filters, pluck="name") + for dunning_name in all_dunnings: dunning = frappe.get_doc("Dunning", dunning_name) if not dunning.sales_invoice: @@ -47,3 +55,22 @@ def execute(): # so we reverse previous GL Entries that recorded the dunning amount at # time of submission of the Dunning. make_reverse_gl_entries(voucher_type="Dunning", voucher_no=dunning.name) + + +def get_accounts_closing_date(): + """Get the date when accounts were frozen/closed""" + accounts_frozen_till = frappe.db.get_single_value( + "Accounts Settings", "acc_frozen_upto" + ) # always returns datetime.date + + period_closing_date = frappe.db.get_value( + "Period Closing Voucher", {"docstatus": 1}, "posting_date", order_by="posting_date desc" + ) + + # Set most recent frozen/closing date as filter + if accounts_frozen_till and period_closing_date: + can_edit_accounts_after = max(accounts_frozen_till, period_closing_date) + else: + can_edit_accounts_after = accounts_frozen_till or period_closing_date + + return can_edit_accounts_after From 17ff395f9a230a8ffbb3c821fb5c357d9261daba Mon Sep 17 00:00:00 2001 From: marination Date: Thu, 20 Jul 2023 20:54:43 +0530 Subject: [PATCH 2/2] fix: Reverse GL entries only for submitted Dunnings --- erpnext/patches/v14_0/single_to_multi_dunning.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/erpnext/patches/v14_0/single_to_multi_dunning.py b/erpnext/patches/v14_0/single_to_multi_dunning.py index 3567522eb3..3b01871d43 100644 --- a/erpnext/patches/v14_0/single_to_multi_dunning.py +++ b/erpnext/patches/v14_0/single_to_multi_dunning.py @@ -7,6 +7,7 @@ def execute(): frappe.reload_doc("accounts", "doctype", "overdue_payment") frappe.reload_doc("accounts", "doctype", "dunning") + # Migrate schema of all uncancelled dunnings filters = {"docstatus": ("!=", 2)} can_edit_accounts_after = get_accounts_closing_date() @@ -49,7 +50,8 @@ def execute(): dunning.flags.ignore_validate_update_after_submit = True dunning.save() - if dunning.status != "Resolved": + # Reverse entries only if dunning is submitted and not resolved + if dunning.docstatus == 1 and dunning.status != "Resolved": # With the new logic, dunning amount gets recorded as additional income # at time of payment. We don't want to record the dunning amount twice, # so we reverse previous GL Entries that recorded the dunning amount at