fix: use doc_before_save and other changes

This commit is contained in:
Sagar Vora
2022-11-18 01:53:24 +05:30
parent fdfe5cbf93
commit 8ae58ed427
2 changed files with 37 additions and 40 deletions

View File

@@ -92,9 +92,7 @@ class Account(NestedSet):
self.root_type = par.root_type self.root_type = par.root_type
if self.is_group: if self.is_group:
db_value = frappe.get_cached_value( db_value = self.get_doc_before_save()
"Account", self.name, ["report_type", "root_type"], as_dict=1
)
if db_value: if db_value:
if self.report_type != db_value.report_type: if self.report_type != db_value.report_type:
frappe.db.sql( frappe.db.sql(
@@ -113,14 +111,13 @@ class Account(NestedSet):
) )
def validate_root_details(self): def validate_root_details(self):
# does not exists parent doc_before_save = self.get_doc_before_save()
account_values = frappe.get_cached_value("Account", self.name, ["parent_account"], as_dict=1)
if account_values: if doc_before_save and not doc_before_save.parent_account:
if not account_values.parent_account:
throw(_("Root cannot be edited."), RootNotEditable) throw(_("Root cannot be edited."), RootNotEditable)
if not self.parent_account and not self.is_group: if not self.parent_account and not self.is_group:
frappe.throw(_("The root account {0} must be a group").format(frappe.bold(self.name))) throw(_("The root account {0} must be a group").format(frappe.bold(self.name)))
def validate_root_company_and_sync_account_to_children(self): def validate_root_company_and_sync_account_to_children(self):
# ignore validation while creating new compnay or while syncing to child companies # ignore validation while creating new compnay or while syncing to child companies
@@ -164,11 +161,10 @@ class Account(NestedSet):
self.create_account_for_child_company(parent_acc_name_map, descendants, parent_acc_name) self.create_account_for_child_company(parent_acc_name_map, descendants, parent_acc_name)
def validate_group_or_ledger(self): def validate_group_or_ledger(self):
if self.get("__islocal"): doc_before_save = self.get_doc_before_save()
if not doc_before_save or cint(doc_before_save.is_group) == cint(self.is_group):
return return
existing_is_group = frappe.get_cached_value("Account", self.name, "is_group")
if cint(self.is_group) != cint(existing_is_group):
if self.check_gle_exists(): if self.check_gle_exists():
throw(_("Account with existing transaction cannot be converted to ledger")) throw(_("Account with existing transaction cannot be converted to ledger"))
elif self.is_group: elif self.is_group:
@@ -178,10 +174,12 @@ class Account(NestedSet):
throw(_("Account with child nodes cannot be set as ledger")) throw(_("Account with child nodes cannot be set as ledger"))
def validate_frozen_accounts_modifier(self): def validate_frozen_accounts_modifier(self):
old_value = frappe.get_cached_value("Account", self.name, "freeze_account") doc_before_save = self.get_doc_before_save()
if old_value and old_value != self.freeze_account: if not doc_before_save or doc_before_save.freeze_account == self.freeze_account:
frozen_accounts_modifier = frappe.db.get_value( return
"Accounts Settings", None, "frozen_accounts_modifier"
frozen_accounts_modifier = frappe.get_cached_value(
"Accounts Settings", "Accounts Settings", "frozen_accounts_modifier"
) )
if not frozen_accounts_modifier or frozen_accounts_modifier not in frappe.get_roles(): if not frozen_accounts_modifier or frozen_accounts_modifier not in frappe.get_roles():
throw(_("You are not authorized to set Frozen value")) throw(_("You are not authorized to set Frozen value"))
@@ -440,25 +438,24 @@ def update_account_number(name, account_name, account_number=None, from_descenda
@frappe.whitelist() @frappe.whitelist()
def merge_account(old, new, is_group, root_type, company): def merge_account(old, new, is_group, root_type, company):
# Validate properties before merging # Validate properties before merging
account_data = frappe.get_cached_value( new_account = frappe.get_cached_doc("Account", new)
"Account", new, ["is_group", "root_type", "company", "parent_account"], as_dict=True
) if not new_account:
if not account_data:
throw(_("Account {0} does not exist").format(new)) throw(_("Account {0} does not exist").format(new))
val = list(account_data.values())[:3] # is_group, root_type, company if (new_account.is_group, new_account.root_type, new_account.company) != (
cint(is_group),
if val != [cint(is_group), root_type, company]: root_type,
company,
):
throw( throw(
_( _(
"""Merging is only possible if following properties are same in both records. Is Group, Root Type, Company""" """Merging is only possible if following properties are same in both records. Is Group, Root Type, Company"""
) )
) )
if is_group and account_data.parent_account == old: if is_group and new_account.parent_account == old:
frappe.db.set_value( new_account.db_set("parent_account", frappe.get_cached_value("Account", old, "parent_account"))
"Account", new, "parent_account", frappe.get_cached_value("Account", old, "parent_account")
)
frappe.rename_doc("Account", old, new, merge=1, force=1) frappe.rename_doc("Account", old, new, merge=1, force=1)

View File

@@ -286,10 +286,10 @@ def get_accounts_with_children(accounts):
all_accounts = [] all_accounts = []
for d in accounts: for d in accounts:
account_data = frappe.get_cached_value("Account", d, ["lft", "rgt"], as_dict=1) account = frappe.get_cached_doc("Account", d)
if account_data: if account:
children = frappe.get_all( children = frappe.get_all(
"Account", filters={"lft": [">=", account_data.lft], "rgt": ["<=", account_data.rgt]} "Account", filters={"lft": [">=", account.lft], "rgt": ["<=", account.rgt]}
) )
all_accounts += [c.name for c in children] all_accounts += [c.name for c in children]
else: else: