From 0727d1d99b237780f6d9a4d66fa9c5ffd3a991ea Mon Sep 17 00:00:00 2001 From: Gavin D'souza Date: Mon, 13 Jun 2022 12:39:28 +0530 Subject: [PATCH] refactor: get_fiscal_years API * Optimize fiscal year options generation * Don't pass unrequired criterions / values to prepared query * Use QB notation for raw query --- erpnext/accounts/utils.py | 54 +++++++++++++++++++-------------------- 1 file changed, 26 insertions(+), 28 deletions(-) diff --git a/erpnext/accounts/utils.py b/erpnext/accounts/utils.py index 8711395d55..65e05410aa 100644 --- a/erpnext/accounts/utils.py +++ b/erpnext/accounts/utils.py @@ -9,7 +9,10 @@ import frappe import frappe.defaults from frappe import _, qb, throw from frappe.model.meta import get_field_precision +from frappe.query_builder.utils import DocType from frappe.utils import cint, cstr, flt, formatdate, get_number_format_info, getdate, now, nowdate +from pypika import Order +from pypika.terms import ExistsCriterion import erpnext @@ -42,37 +45,32 @@ def get_fiscal_years( if not fiscal_years: # if year start date is 2012-04-01, year end date should be 2013-03-31 (hence subdate) - cond = "" - if fiscal_year: - cond += " and fy.name = {0}".format(frappe.db.escape(fiscal_year)) - if company: - cond += """ - and (not exists (select name - from `tabFiscal Year Company` fyc - where fyc.parent = fy.name) - or exists(select company - from `tabFiscal Year Company` fyc - where fyc.parent = fy.name - and fyc.company=%(company)s) - ) - """ + FY = DocType("Fiscal Year") - fiscal_years = frappe.db.sql( - """ - select - fy.name, fy.year_start_date, fy.year_end_date - from - `tabFiscal Year` fy - where - disabled = 0 {0} - order by - fy.year_start_date desc""".format( - cond - ), - {"company": company}, - as_dict=True, + query = ( + 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( + ExistsCriterion(frappe.qb.from_(FYC).select(FYC.name).where(FYC.parent == FY.name)).negate() + | ExistsCriterion( + frappe.qb.from_(FYC) + .select(FYC.company) + .where(FYC.parent == FY.name) + .where(FYC.company == company) + ) + ) + + query = query.orderby(FY.year_start_date, Order.desc) + fiscal_years = query.run(as_dict=True) + frappe.cache().hset("fiscal_years", company, fiscal_years) if not transaction_date and not fiscal_year: