From 73e9b38cda37b52f917f9d781a11d3035f0e055e Mon Sep 17 00:00:00 2001 From: Sagar Vora Date: Mon, 24 Jul 2023 18:37:36 +0530 Subject: [PATCH] fix(regional): set `frappe.flags.company` temporarily, where required (cherry picked from commit 4205f564a06e8b074f1b0a2f4dda38bcef3967f2) --- erpnext/accounts/party.py | 6 +++--- erpnext/controllers/accounts_controller.py | 5 ++++- erpnext/controllers/taxes_and_totals.py | 22 ++++++++-------------- erpnext/utilities/regional.py | 13 +++++++++++++ 4 files changed, 28 insertions(+), 18 deletions(-) create mode 100644 erpnext/utilities/regional.py diff --git a/erpnext/accounts/party.py b/erpnext/accounts/party.py index 7583a60d63e9..56d33b758b7f 100644 --- a/erpnext/accounts/party.py +++ b/erpnext/accounts/party.py @@ -33,6 +33,7 @@ from erpnext import get_company_currency from erpnext.accounts.utils import get_fiscal_year from erpnext.exceptions import InvalidAccountCurrency, PartyDisabled, PartyFrozen +from erpnext.utilities.regional import temporary_flag PURCHASE_TRANSACTION_TYPES = {"Purchase Order", "Purchase Receipt", "Purchase Invoice"} SALES_TRANSACTION_TYPES = { @@ -261,9 +262,8 @@ def set_address_details( ) if doctype in TRANSACTION_TYPES: - # required to set correct region - frappe.flags.company = company - get_regional_address_details(party_details, doctype, company) + with temporary_flag("company", company): + get_regional_address_details(party_details, doctype, company) return party_address, shipping_address diff --git a/erpnext/controllers/accounts_controller.py b/erpnext/controllers/accounts_controller.py index a1eba4ae0c34..b70ea36dcde8 100644 --- a/erpnext/controllers/accounts_controller.py +++ b/erpnext/controllers/accounts_controller.py @@ -55,6 +55,7 @@ get_item_tax_map, get_item_warehouse, ) +from erpnext.utilities.regional import temporary_flag from erpnext.utilities.transaction_base import TransactionBase @@ -758,7 +759,9 @@ def get_gl_dict(self, args, account_currency=None, item=None): } ) - update_gl_dict_with_regional_fields(self, gl_dict) + with temporary_flag("company", self.company): + update_gl_dict_with_regional_fields(self, gl_dict) + accounting_dimensions = get_accounting_dimensions() dimension_dict = frappe._dict() diff --git a/erpnext/controllers/taxes_and_totals.py b/erpnext/controllers/taxes_and_totals.py index 4661c5ca7e8f..77006b905cb7 100644 --- a/erpnext/controllers/taxes_and_totals.py +++ b/erpnext/controllers/taxes_and_totals.py @@ -18,6 +18,7 @@ validate_taxes_and_charges, ) from erpnext.stock.get_item_details import _get_item_tax_template +from erpnext.utilities.regional import temporary_flag class calculate_taxes_and_totals(object): @@ -942,7 +943,6 @@ def set_total_amount_to_default_mop(self, total_amount_to_pay): def get_itemised_tax_breakup_html(doc): if not doc.taxes: return - frappe.flags.company = doc.company # get headers tax_accounts = [] @@ -952,15 +952,11 @@ def get_itemised_tax_breakup_html(doc): if tax.description not in tax_accounts: tax_accounts.append(tax.description) - headers = get_itemised_tax_breakup_header(doc.doctype + " Item", tax_accounts) - - # get tax breakup data - itemised_tax, itemised_taxable_amount = get_itemised_tax_breakup_data(doc) - - get_rounded_tax_amount(itemised_tax, doc.precision("tax_amount", "taxes")) - - update_itemised_tax_data(doc) - frappe.flags.company = None + with temporary_flag("company", doc.company): + headers = get_itemised_tax_breakup_header(doc.doctype + " Item", tax_accounts) + itemised_tax, itemised_taxable_amount = get_itemised_tax_breakup_data(doc) + get_rounded_tax_amount(itemised_tax, doc.precision("tax_amount", "taxes")) + update_itemised_tax_data(doc) return frappe.render_template( "templates/includes/itemised_tax_breakup.html", @@ -977,10 +973,8 @@ def get_itemised_tax_breakup_html(doc): @frappe.whitelist() def get_round_off_applicable_accounts(company, account_list): # required to set correct region - frappe.flags.company = company - account_list = get_regional_round_off_accounts(company, account_list) - - return account_list + with temporary_flag("company", company): + return get_regional_round_off_accounts(company, account_list) @erpnext.allow_regional diff --git a/erpnext/utilities/regional.py b/erpnext/utilities/regional.py new file mode 100644 index 000000000000..858976f85572 --- /dev/null +++ b/erpnext/utilities/regional.py @@ -0,0 +1,13 @@ +from contextlib import contextmanager + +import frappe + + +@contextmanager +def temporary_flag(flag_name, value): + flags = frappe.local.flags + flags[flag_name] = value + try: + yield + finally: + flags.pop(flag_name, None)