Skip to content

Commit

Permalink
refactor: use common functionality to validate account number (#42842)
Browse files Browse the repository at this point in the history
feat: Allow unique Account number by root type (not unique for accros all Accounts)
  • Loading branch information
FHenry authored Sep 21, 2024
1 parent b127a0c commit 40d97f4
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 @@ -102,14 +102,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 @@ -310,6 +308,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 @@ -463,19 +477,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 @@ -516,7 +517,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 40d97f4

Please sign in to comment.