From 4efc947f1494b1b0984009eb81f47f56bd31d952 Mon Sep 17 00:00:00 2001 From: Daizy Modi Date: Mon, 7 Nov 2022 09:21:03 +0530 Subject: [PATCH] perf: use `get_cached_value` instead of `db.get_value` in controllers (#32776) --- erpnext/controllers/accounts_controller.py | 22 +++++++++++-------- .../controllers/sales_and_purchase_return.py | 6 ++--- erpnext/controllers/stock_controller.py | 20 ++++++++--------- erpnext/controllers/taxes_and_totals.py | 2 +- erpnext/controllers/trends.py | 4 ++-- 5 files changed, 29 insertions(+), 25 deletions(-) diff --git a/erpnext/controllers/accounts_controller.py b/erpnext/controllers/accounts_controller.py index 756fc6cc3d..216c9f45d3 100644 --- a/erpnext/controllers/accounts_controller.py +++ b/erpnext/controllers/accounts_controller.py @@ -227,7 +227,7 @@ class AccountsController(TransactionBase): for item in self.get("items"): if item.get("enable_deferred_revenue") or item.get("enable_deferred_expense"): if not item.get(field_map.get(self.doctype)): - default_deferred_account = frappe.db.get_value( + default_deferred_account = frappe.get_cached_value( "Company", self.company, "default_" + field_map.get(self.doctype) ) if not default_deferred_account: @@ -676,7 +676,7 @@ class AccountsController(TransactionBase): def validate_enabled_taxes_and_charges(self): taxes_and_charges_doctype = self.meta.get_options("taxes_and_charges") - if frappe.db.get_value(taxes_and_charges_doctype, self.taxes_and_charges, "disabled"): + if frappe.get_cached_value(taxes_and_charges_doctype, self.taxes_and_charges, "disabled"): frappe.throw( _("{0} '{1}' is disabled").format(taxes_and_charges_doctype, self.taxes_and_charges) ) @@ -684,7 +684,7 @@ class AccountsController(TransactionBase): def validate_tax_account_company(self): for d in self.get("taxes"): if d.account_head: - tax_account_company = frappe.db.get_value("Account", d.account_head, "company") + tax_account_company = frappe.get_cached_value("Account", d.account_head, "company") if tax_account_company != self.company: frappe.throw( _("Row #{0}: Account {1} does not belong to company {2}").format( @@ -929,7 +929,9 @@ class AccountsController(TransactionBase): party_account = self.credit_to if is_purchase_invoice else self.debit_to party_type = "Supplier" if is_purchase_invoice else "Customer" - gain_loss_account = frappe.db.get_value("Company", self.company, "exchange_gain_loss_account") + gain_loss_account = frappe.get_cached_value( + "Company", self.company, "exchange_gain_loss_account" + ) if not gain_loss_account: frappe.throw( _("Please set default Exchange Gain/Loss Account in Company {}").format(self.get("company")) @@ -1026,7 +1028,7 @@ class AccountsController(TransactionBase): else self.grand_total ), "outstanding_amount": self.outstanding_amount, - "difference_account": frappe.db.get_value( + "difference_account": frappe.get_cached_value( "Company", self.company, "exchange_gain_loss_account" ), "exchange_gain_loss": flt(d.get("exchange_gain_loss")), @@ -1394,7 +1396,7 @@ class AccountsController(TransactionBase): @property def company_abbr(self): if not hasattr(self, "_abbr"): - self._abbr = frappe.db.get_value("Company", self.company, "abbr") + self._abbr = frappe.get_cached_value("Company", self.company, "abbr") return self._abbr @@ -1780,7 +1782,7 @@ class AccountsController(TransactionBase): """ if self.is_internal_transfer() and not self.unrealized_profit_loss_account: - unrealized_profit_loss_account = frappe.db.get_value( + unrealized_profit_loss_account = frappe.get_cached_value( "Company", self.company, "unrealized_profit_loss_account" ) @@ -1895,7 +1897,9 @@ class AccountsController(TransactionBase): @frappe.whitelist() def get_tax_rate(account_head): - return frappe.db.get_value("Account", account_head, ["tax_rate", "account_name"], as_dict=True) + return frappe.get_cached_value( + "Account", account_head, ["tax_rate", "account_name"], as_dict=True + ) @frappe.whitelist() @@ -1904,7 +1908,7 @@ def get_default_taxes_and_charges(master_doctype, tax_template=None, company=Non return {} if tax_template and company: - tax_template_company = frappe.db.get_value(master_doctype, tax_template, "company") + tax_template_company = frappe.get_cached_value(master_doctype, tax_template, "company") if tax_template_company == company: return diff --git a/erpnext/controllers/sales_and_purchase_return.py b/erpnext/controllers/sales_and_purchase_return.py index 0054edbbd5..39ef68a9fb 100644 --- a/erpnext/controllers/sales_and_purchase_return.py +++ b/erpnext/controllers/sales_and_purchase_return.py @@ -326,7 +326,7 @@ def make_return_doc(doctype: str, source_name: str, target_doc=None): from erpnext.stock.doctype.serial_no.serial_no import get_serial_nos company = frappe.db.get_value("Delivery Note", source_name, "company") - default_warehouse_for_sales_return = frappe.db.get_value( + default_warehouse_for_sales_return = frappe.get_cached_value( "Company", company, "default_warehouse_for_sales_return" ) @@ -340,11 +340,11 @@ def make_return_doc(doctype: str, source_name: str, target_doc=None): # look for Print Heading "Credit Note" if not doc.select_print_heading: - doc.select_print_heading = frappe.db.get_value("Print Heading", _("Credit Note")) + doc.select_print_heading = frappe.get_cached_value("Print Heading", _("Credit Note")) elif doctype == "Purchase Invoice": # look for Print Heading "Debit Note" - doc.select_print_heading = frappe.db.get_value("Print Heading", _("Debit Note")) + doc.select_print_heading = frappe.get_cached_value("Print Heading", _("Debit Note")) for tax in doc.get("taxes") or []: if tax.charge_type == "Actual": diff --git a/erpnext/controllers/stock_controller.py b/erpnext/controllers/stock_controller.py index 98dc58677b..1e4fabe0d2 100644 --- a/erpnext/controllers/stock_controller.py +++ b/erpnext/controllers/stock_controller.py @@ -57,7 +57,7 @@ class StockController(AccountsController): make_reverse_gl_entries(voucher_type=self.doctype, voucher_no=self.name) provisional_accounting_for_non_stock_items = cint( - frappe.db.get_value( + frappe.get_cached_value( "Company", self.company, "enable_provisional_accounting_for_non_stock_items" ) ) @@ -200,7 +200,7 @@ class StockController(AccountsController): elif self.get("is_internal_supplier"): warehouse_asset_account = warehouse_account[item_row.get("warehouse")]["account"] - expense_account = frappe.db.get_value("Company", self.company, "default_expense_account") + expense_account = frappe.get_cached_value("Company", self.company, "default_expense_account") gl_list.append( self.get_gl_dict( @@ -235,7 +235,7 @@ class StockController(AccountsController): if warehouse_with_no_account: for wh in warehouse_with_no_account: - if frappe.db.get_value("Warehouse", wh, "company"): + if frappe.get_cached_value("Warehouse", wh, "company"): frappe.throw( _( "Warehouse {0} is not linked to any account, please mention the account in the warehouse record or set default inventory account in company {1}." @@ -449,15 +449,15 @@ class StockController(AccountsController): # Get value based on doctype name if not sl_dict.get(dimension.target_fieldname): - fieldname = frappe.get_cached_value( - "DocField", {"parent": self.doctype, "options": dimension.fetch_from_parent}, "fieldname" + fieldname = next( + ( + field.fieldname + for field in frappe.get_meta(self.doctype).fields + if field.options == dimension.fetch_from_parent + ), + None, ) - if not fieldname: - fieldname = frappe.get_cached_value( - "Custom Field", {"dt": self.doctype, "options": dimension.fetch_from_parent}, "fieldname" - ) - if fieldname and self.get(fieldname): sl_dict[dimension.target_fieldname] = self.get(fieldname) diff --git a/erpnext/controllers/taxes_and_totals.py b/erpnext/controllers/taxes_and_totals.py index 81de682378..c6a634ba80 100644 --- a/erpnext/controllers/taxes_and_totals.py +++ b/erpnext/controllers/taxes_and_totals.py @@ -1056,7 +1056,7 @@ class init_landed_taxes_and_totals(object): company_currency = erpnext.get_company_currency(self.doc.company) for d in self.doc.get(self.tax_field): if not d.account_currency: - account_currency = frappe.db.get_value("Account", d.expense_account, "account_currency") + account_currency = frappe.get_cached_value("Account", d.expense_account, "account_currency") d.account_currency = account_currency or company_currency def set_exchange_rate(self): diff --git a/erpnext/controllers/trends.py b/erpnext/controllers/trends.py index 1d6c5dc0be..1fb722e112 100644 --- a/erpnext/controllers/trends.py +++ b/erpnext/controllers/trends.py @@ -80,7 +80,7 @@ def get_data(filters, conditions): if conditions.get("trans") == "Quotation" and filters.get("group_by") == "Customer": cond += " and t1.quotation_to = 'Customer'" - year_start_date, year_end_date = frappe.db.get_value( + year_start_date, year_end_date = frappe.get_cached_value( "Fiscal Year", filters.get("fiscal_year"), ["year_start_date", "year_end_date"] ) @@ -275,7 +275,7 @@ def get_period_date_ranges(period, fiscal_year=None, year_start_date=None): from dateutil.relativedelta import relativedelta if not year_start_date: - year_start_date, year_end_date = frappe.db.get_value( + year_start_date, year_end_date = frappe.get_cached_value( "Fiscal Year", fiscal_year, ["year_start_date", "year_end_date"] )