From 41e395a7e3f5e1327faefe6ea0a56aede01d316f Mon Sep 17 00:00:00 2001 From: David Date: Wed, 12 Jun 2024 10:58:06 +0200 Subject: [PATCH] fix: hot path in page load --- erpnext/accounts/utils.py | 104 ++++++++++++---------- erpnext/public/js/financial_statements.js | 5 +- erpnext/public/js/utils.js | 40 +++++---- erpnext/startup/boot.py | 5 ++ 4 files changed, 90 insertions(+), 64 deletions(-) diff --git a/erpnext/accounts/utils.py b/erpnext/accounts/utils.py index 2c88cb6b2634..6a25ce412378 100644 --- a/erpnext/accounts/utils.py +++ b/erpnext/accounts/utils.py @@ -53,18 +53,28 @@ class PaymentEntryUnlinkError(frappe.ValidationError): @frappe.whitelist() def get_fiscal_year( - date=None, fiscal_year=None, label="Date", verbose=1, company=None, as_dict=False, boolean=False + date=None, + fiscal_year=None, + label="Date", + verbose=1, + company=None, + as_dict=False, + boolean=None, + raise_on_missing=True, ): + if isinstance(raise_on_missing, str): + raise_on_missing = loads(raise_on_missing) + + # backwards compat if isinstance(boolean, str): boolean = loads(boolean) + if boolean is not None: + raise_on_missing = not boolean fiscal_years = get_fiscal_years( - date, fiscal_year, label, verbose, company, as_dict=as_dict, boolean=boolean + date, fiscal_year, label, verbose, company, as_dict=as_dict, raise_on_missing=raise_on_missing ) - if boolean: - return fiscal_years - else: - return fiscal_years[0] + return False if not fiscal_years else fiscal_years[0] def get_fiscal_years( @@ -74,8 +84,48 @@ def get_fiscal_years( verbose=1, company=None, as_dict=False, - boolean=False, + boolean=None, + raise_on_missing=True, ): + if transaction_date: + transaction_date = getdate(transaction_date) + # backwards compat + if boolean is not None: + raise_on_missing = not boolean + + all_fiscal_years = _get_fiscal_years(company=company) + + # No restricting selectors + if not transaction_date and not fiscal_year: + return all_fiscal_years + + for fy in all_fiscal_years: + if (fiscal_year and fy.name == fiscal_year) or ( + transaction_date + and getdate(fy.year_start_date) <= transaction_date + and getdate(fy.year_end_date) >= transaction_date + ): + if as_dict: + return (fy,) + else: + return ((fy.name, fy.year_start_date, fy.year_end_date),) + + # No match for restricting selectors + if raise_on_missing: + error_msg = _("""{0} {1} is not in any active Fiscal Year""").format( + label, formatdate(transaction_date) + ) + if company: + error_msg = _("""{0} for {1}""").format(error_msg, frappe.bold(company)) + + if verbose == 1: + frappe.msgprint(error_msg) + + raise FiscalYearError(error_msg) + return [] + + +def _get_fiscal_years(company=None): fiscal_years = frappe.cache().hget("fiscal_years", company) or [] if not fiscal_years: @@ -86,9 +136,6 @@ def get_fiscal_years( frappe.qb.from_(FY).select(FY.name, FY.year_start_date, FY.year_end_date).where(FY.disabled == 0) ) - if fiscal_year: - query = query.where(FY.name == fiscal_year) - if company: FYC = DocType("Fiscal Year Company") query = query.where( @@ -105,42 +152,7 @@ def get_fiscal_years( fiscal_years = query.run(as_dict=True) frappe.cache().hset("fiscal_years", company, fiscal_years) - - if not transaction_date and not fiscal_year: - return fiscal_years - - if transaction_date: - transaction_date = getdate(transaction_date) - - for fy in fiscal_years: - matched = False - if fiscal_year and fy.name == fiscal_year: - matched = True - - if ( - transaction_date - and getdate(fy.year_start_date) <= transaction_date - and getdate(fy.year_end_date) >= transaction_date - ): - matched = True - - if matched: - if as_dict: - return (fy,) - else: - return ((fy.name, fy.year_start_date, fy.year_end_date),) - - error_msg = _("""{0} {1} is not in any active Fiscal Year""").format(label, formatdate(transaction_date)) - if company: - error_msg = _("""{0} for {1}""").format(error_msg, frappe.bold(company)) - - if boolean: - return False - - if verbose == 1: - frappe.msgprint(error_msg) - - raise FiscalYearError(error_msg) + return fiscal_years @frappe.whitelist() diff --git a/erpnext/public/js/financial_statements.js b/erpnext/public/js/financial_statements.js index bd8b70af02e3..796ecdcaf339 100644 --- a/erpnext/public/js/financial_statements.js +++ b/erpnext/public/js/financial_statements.js @@ -267,11 +267,10 @@ function get_filters() { let fy_filters = filters.filter((x) => { return ["from_fiscal_year", "to_fiscal_year"].includes(x.fieldname); }); - let fiscal_year = erpnext.utils.get_fiscal_year(frappe.datetime.get_today(), false, true); + let fiscal_year = erpnext.utils.get_fiscal_year(frappe.datetime.get_today(), false, false); if (fiscal_year) { - let fy = erpnext.utils.get_fiscal_year(frappe.datetime.get_today(), false, false); fy_filters.forEach((x) => { - x.default = fy; + x.default = fiscal_year; }); } diff --git a/erpnext/public/js/utils.js b/erpnext/public/js/utils.js index 4e89e911132d..165c32549a29 100755 --- a/erpnext/public/js/utils.js +++ b/erpnext/public/js/utils.js @@ -420,7 +420,7 @@ $.extend(erpnext.utils, { }); }, - get_fiscal_year: function (date, with_dates = false, boolean = false) { + get_fiscal_year: function (date, with_dates = false, raise_on_missing = true) { if (!frappe.boot.setup_complete) { return; } @@ -429,20 +429,30 @@ $.extend(erpnext.utils, { } let fiscal_year = ""; - frappe.call({ - method: "erpnext.accounts.utils.get_fiscal_year", - args: { - date: date, - boolean: boolean, - }, - async: false, - callback: function (r) { - if (r.message) { - if (with_dates) fiscal_year = r.message; - else fiscal_year = r.message[0]; - } - }, - }); + if ( + frappe.boot.current_fiscal_year && + date >= frappe.boot.current_fiscal_year[1] && + date <= frappe.boot.current_fiscal_year[2] + ) { + if (with_dates) fiscal_year = frappe.boot.current_fiscal_year; + else fiscal_year = frappe.boot.current_fiscal_year[0]; + } else { + frappe.call({ + method: "erpnext.accounts.utils.get_fiscal_year", + type: "GET", // make it cacheable + args: { + date: date, + raise_on_missing: raise_on_missing, + }, + async: false, + callback: function (r) { + if (r.message) { + if (with_dates) fiscal_year = r.message; + else fiscal_year = r.message[0]; + } + }, + }); + } return fiscal_year; }, }); diff --git a/erpnext/startup/boot.py b/erpnext/startup/boot.py index 3de2be34ebcb..6ef0cdeee38a 100644 --- a/erpnext/startup/boot.py +++ b/erpnext/startup/boot.py @@ -5,6 +5,8 @@ import frappe from frappe.utils import cint +import erpnext.accounts.utils + def boot_session(bootinfo): """boot session - send website info if guest""" @@ -52,6 +54,9 @@ def boot_session(bootinfo): party_account_types = frappe.db.sql(""" select name, ifnull(account_type, '') from `tabParty Type`""") bootinfo.party_account_types = frappe._dict(party_account_types) + fiscal_year = erpnext.accounts.utils.get_fiscal_years(frappe.utils.nowdate(), raise_on_missing=False) + if fiscal_year: + bootinfo.current_fiscal_year = fiscal_year[0] bootinfo.sysdefaults.demo_company = frappe.db.get_single_value("Global Defaults", "demo_company")