Merge pull request #41871 from blaggacao/perf/reduce-critical-path
perf: hot path in page load
This commit is contained in:
@@ -53,18 +53,28 @@ GL_REPOSTING_CHUNK = 100
|
|||||||
|
|
||||||
@frappe.whitelist()
|
@frappe.whitelist()
|
||||||
def get_fiscal_year(
|
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):
|
if isinstance(boolean, str):
|
||||||
boolean = loads(boolean)
|
boolean = loads(boolean)
|
||||||
|
if boolean is not None:
|
||||||
|
raise_on_missing = not boolean
|
||||||
|
|
||||||
fiscal_years = get_fiscal_years(
|
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 False if not fiscal_years else fiscal_years[0]
|
||||||
return fiscal_years
|
|
||||||
else:
|
|
||||||
return fiscal_years[0]
|
|
||||||
|
|
||||||
|
|
||||||
def get_fiscal_years(
|
def get_fiscal_years(
|
||||||
@@ -74,8 +84,48 @@ def get_fiscal_years(
|
|||||||
verbose=1,
|
verbose=1,
|
||||||
company=None,
|
company=None,
|
||||||
as_dict=False,
|
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 []
|
fiscal_years = frappe.cache().hget("fiscal_years", company) or []
|
||||||
|
|
||||||
if not fiscal_years:
|
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)
|
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:
|
if company:
|
||||||
FYC = DocType("Fiscal Year Company")
|
FYC = DocType("Fiscal Year Company")
|
||||||
query = query.where(
|
query = query.where(
|
||||||
@@ -105,42 +152,7 @@ def get_fiscal_years(
|
|||||||
fiscal_years = query.run(as_dict=True)
|
fiscal_years = query.run(as_dict=True)
|
||||||
|
|
||||||
frappe.cache().hset("fiscal_years", company, fiscal_years)
|
frappe.cache().hset("fiscal_years", company, fiscal_years)
|
||||||
|
return 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)
|
|
||||||
|
|
||||||
|
|
||||||
@frappe.whitelist()
|
@frappe.whitelist()
|
||||||
|
|||||||
@@ -271,11 +271,10 @@ function get_filters() {
|
|||||||
let fy_filters = filters.filter((x) => {
|
let fy_filters = filters.filter((x) => {
|
||||||
return ["from_fiscal_year", "to_fiscal_year"].includes(x.fieldname);
|
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) {
|
if (fiscal_year) {
|
||||||
let fy = erpnext.utils.get_fiscal_year(frappe.datetime.get_today(), false, false);
|
|
||||||
fy_filters.forEach((x) => {
|
fy_filters.forEach((x) => {
|
||||||
x.default = fy;
|
x.default = fiscal_year;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -424,7 +424,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) {
|
if (!frappe.boot.setup_complete) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -433,20 +433,30 @@ $.extend(erpnext.utils, {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let fiscal_year = "";
|
let fiscal_year = "";
|
||||||
frappe.call({
|
if (
|
||||||
method: "erpnext.accounts.utils.get_fiscal_year",
|
frappe.boot.current_fiscal_year &&
|
||||||
args: {
|
date >= frappe.boot.current_fiscal_year[1] &&
|
||||||
date: date,
|
date <= frappe.boot.current_fiscal_year[2]
|
||||||
boolean: boolean,
|
) {
|
||||||
},
|
if (with_dates) fiscal_year = frappe.boot.current_fiscal_year;
|
||||||
async: false,
|
else fiscal_year = frappe.boot.current_fiscal_year[0];
|
||||||
callback: function (r) {
|
} else {
|
||||||
if (r.message) {
|
frappe.call({
|
||||||
if (with_dates) fiscal_year = r.message;
|
method: "erpnext.accounts.utils.get_fiscal_year",
|
||||||
else fiscal_year = r.message[0];
|
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;
|
return fiscal_year;
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -5,6 +5,8 @@
|
|||||||
import frappe
|
import frappe
|
||||||
from frappe.utils import cint
|
from frappe.utils import cint
|
||||||
|
|
||||||
|
import erpnext.accounts.utils
|
||||||
|
|
||||||
|
|
||||||
def boot_session(bootinfo):
|
def boot_session(bootinfo):
|
||||||
"""boot session - send website info if guest"""
|
"""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`""")
|
party_account_types = frappe.db.sql(""" select name, ifnull(account_type, '') from `tabParty Type`""")
|
||||||
bootinfo.party_account_types = frappe._dict(party_account_types)
|
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")
|
bootinfo.sysdefaults.demo_company = frappe.db.get_single_value("Global Defaults", "demo_company")
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user