Skip to content

Commit

Permalink
Merge pull request #43314 from frappe/mergify/bp/version-15-hotfix/pr…
Browse files Browse the repository at this point in the history
…-42842

refactor: use common functionality to validate account number (backport #42842)
  • Loading branch information
vorasmit committed Sep 21, 2024
2 parents f6725e2 + 86ae644 commit d1c72dc
Showing 1 changed file with 18 additions and 17 deletions.
35 changes: 18 additions & 17 deletions erpnext/accounts/doctype/account/account.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,14 +103,12 @@ def autoname(self):
self.name = get_autoname_with_number(self.account_number, self.account_name, self.company)

def validate(self):
from erpnext.accounts.utils import validate_field_number

if frappe.local.flags.allow_unverified_charts:
return
self.validate_parent()
self.validate_parent_child_account_type()
self.validate_root_details()
validate_field_number("Account", self.name, self.account_number, self.company, "account_number")
self.validate_account_number()
self.validate_group_or_ledger()
self.set_root_and_report_type()
self.validate_mandatory()
Expand Down Expand Up @@ -311,6 +309,22 @@ def validate_account_currency(self):
if frappe.db.get_value("GL Entry", {"account": self.name}):
frappe.throw(_("Currency can not be changed after making entries using some other currency"))

def validate_account_number(self, account_number=None):
if not account_number:
account_number = self.account_number

if account_number:
account_with_same_number = frappe.db.get_value(
"Account",
{"account_number": account_number, "company": self.company, "name": ["!=", self.name]},
)
if account_with_same_number:
frappe.throw(
_("Account Number {0} already used in account {1}").format(
account_number, account_with_same_number
)
)

def create_account_for_child_company(self, parent_acc_name_map, descendants, parent_acc_name):
for company in descendants:
company_bold = frappe.bold(company)
Expand Down Expand Up @@ -464,19 +478,6 @@ def get_account_autoname(account_number, account_name, company):
return " - ".join(parts)


def validate_account_number(name, account_number, company):
if account_number:
account_with_same_number = frappe.db.get_value(
"Account", {"account_number": account_number, "company": company, "name": ["!=", name]}
)
if account_with_same_number:
frappe.throw(
_("Account Number {0} already used in account {1}").format(
account_number, account_with_same_number
)
)


@frappe.whitelist()
def update_account_number(name, account_name, account_number=None, from_descendant=False):
account = frappe.get_cached_doc("Account", name)
Expand Down Expand Up @@ -517,7 +518,7 @@ def update_account_number(name, account_name, account_number=None, from_descenda

frappe.throw(message, title=_("Rename Not Allowed"))

validate_account_number(name, account_number, account.company)
account.validate_account_number(account_number)
if account_number:
frappe.db.set_value("Account", name, "account_number", account_number.strip())
else:
Expand Down

0 comments on commit d1c72dc

Please sign in to comment.