From 42e4c37f15e2a0613be9104d390bfdc3032f85b7 Mon Sep 17 00:00:00 2001 From: Deepesh Garg Date: Mon, 17 Oct 2022 20:09:07 +0530 Subject: [PATCH] chore: Break into smaller functions --- .../doctype/sales_invoice/sales_invoice.py | 47 +++++++++++-------- 1 file changed, 27 insertions(+), 20 deletions(-) diff --git a/erpnext/accounts/doctype/sales_invoice/sales_invoice.py b/erpnext/accounts/doctype/sales_invoice/sales_invoice.py index 891ae1c4b2..1047e88606 100644 --- a/erpnext/accounts/doctype/sales_invoice/sales_invoice.py +++ b/erpnext/accounts/doctype/sales_invoice/sales_invoice.py @@ -551,31 +551,38 @@ class SalesInvoice(SellingController): needs_repost = 1 break - # Check for parent level - for index, item in enumerate(self.get("items")): - for field in ("income_account", "expense_account", "discount_account"): - if doc_before_update.get("items")[index].get(field) != item.get(field): - needs_repost = 1 - break + # Check for child tables + if self.check_if_child_table_updated( + "items", + doc_before_update, + ("income_account", "expense_account", "discount_account"), + accounting_dimensions, + ): + needs_repost = 1 - for dimension in accounting_dimensions: - if doc_before_update.get("items")[index].get(dimension) != item.get(dimension): - needs_repost = 1 - break - - for index, tax in enumerate(self.get("taxes")): - if doc_before_update.get("taxes")[index].get("account_head") != tax.get("account_head"): - needs_repost = 1 - break - - for dimension in accounting_dimensions: - if doc_before_update.get("taxes")[index].get(dimension) != tax.get(dimension): - needs_repost = 1 - break + if self.check_if_child_table_updated( + "taxes", doc_before_update, ("account_head",), accounting_dimensions + ): + needs_repost = 1 self.validate_accounts() self.db_set("repost_required", needs_repost) + def check_if_child_table_updated( + self, child_table, doc_before_update, fields_to_check, accounting_dimensions + ): + # Check if any field affecting accounting entry is altered + for index, item in enumerate(self.get(child_table)): + for field in fields_to_check: + if doc_before_update.get(child_table)[index].get(field) != item.get(field): + return True + + for dimension in accounting_dimensions: + if doc_before_update.get(child_table)[index].get(dimension) != item.get(dimension): + return True + + return False + @frappe.whitelist() def repost_accounting_entries(self): self.docstatus = 2