fix: use `get_cached_value` to avoid db call with `db.exists`
diff --git a/erpnext/accounts/doctype/account/account.py b/erpnext/accounts/doctype/account/account.py
index 8beb01b..17c96b6 100644
--- a/erpnext/accounts/doctype/account/account.py
+++ b/erpnext/accounts/doctype/account/account.py
@@ -114,8 +114,9 @@
def validate_root_details(self):
# does not exists parent
- if frappe.db.exists("Account", self.name):
- if not frappe.get_cached_value("Account", self.name, "parent_account"):
+ account_values = frappe.get_cached_value("Account", self.name, ["parent_account"], as_dict=1)
+ if account_values:
+ if not account_values.parent_account:
throw(_("Root cannot be edited."), RootNotEditable)
if not self.parent_account and not self.is_group:
@@ -439,10 +440,13 @@
@frappe.whitelist()
def merge_account(old, new, is_group, root_type, company):
# Validate properties before merging
- if not frappe.db.exists("Account", new):
+ account_data = frappe.get_cached_value(
+ "Account", new, ["is_group", "root_type", "company", "parent_account"], as_dict=True
+ )
+ if not account_data:
throw(_("Account {0} does not exist").format(new))
- val = list(frappe.get_cached_value("Account", new, ["is_group", "root_type", "company"]))
+ val = list(account_data.values())[:3] # is_group, root_type, company
if val != [cint(is_group), root_type, company]:
throw(
@@ -451,7 +455,7 @@
)
)
- if is_group and frappe.get_cached_value("Account", new, "parent_account") == old:
+ if is_group and account_data.parent_account == old:
frappe.db.set_value(
"Account", new, "parent_account", frappe.get_cached_value("Account", old, "parent_account")
)
diff --git a/erpnext/accounts/report/general_ledger/general_ledger.py b/erpnext/accounts/report/general_ledger/general_ledger.py
index 615db29..2dfb5d4 100644
--- a/erpnext/accounts/report/general_ledger/general_ledger.py
+++ b/erpnext/accounts/report/general_ledger/general_ledger.py
@@ -286,9 +286,11 @@
all_accounts = []
for d in accounts:
- if frappe.db.exists("Account", d):
- lft, rgt = frappe.get_cached_value("Account", d, ["lft", "rgt"])
- children = frappe.get_all("Account", filters={"lft": [">=", lft], "rgt": ["<=", rgt]})
+ account_data = frappe.get_cached_value("Account", d, ["lft", "rgt"], as_dict=1)
+ if account_data:
+ children = frappe.get_all(
+ "Account", filters={"lft": [">=", account_data.lft], "rgt": ["<=", account_data.rgt]}
+ )
all_accounts += [c.name for c in children]
else:
frappe.throw(_("Account: {0} does not exist").format(d))
diff --git a/erpnext/accounts/utils.py b/erpnext/accounts/utils.py
index 4aed7ce..41702d6 100644
--- a/erpnext/accounts/utils.py
+++ b/erpnext/accounts/utils.py
@@ -975,7 +975,7 @@
def create_payment_gateway_account(gateway, payment_channel="Email"):
from erpnext.setup.setup_wizard.operations.install_fixtures import create_bank_account
- company = frappe.get_cached_value("Global Defaults", None, "default_company")
+ company = frappe.get_cached_value("Global Defaults", "Global Defaults", "default_company")
if not company:
return