* use frm.set_value instead of this.set_value

* generic methods moved to accounts/utils

* clean up code, and make use of methods added in utils
This commit is contained in:
Zarrar 2018-06-08 16:35:21 +05:30 committed by Nabin Hait
parent f553b53fbf
commit 44175904c6
5 changed files with 66 additions and 86 deletions

View File

@ -111,10 +111,13 @@ frappe.ui.form.on('Account', {
}
frappe.call({
method: "erpnext.accounts.doctype.account.account.update_account_number",
method: "erpnext.accounts.utils.update_number_field",
args: {
account_number: data.account_number,
name: frm.doc.name
doctype_name: frm.doc.doctype,
name: frm.doc.name,
field_name: d.fields[0].fieldname,
field_value: data.account_number,
company: frm.doc.company,
},
callback: function(r) {
if(!r.exc) {

View File

@ -232,44 +232,6 @@ def get_account_currency(account):
return frappe.local_cache("account_currency", account, generator)
def get_account_autoname(account_number, account_name, company):
# first validate if company exists
company = frappe.db.get_value("Company", company, ["abbr", "name"], as_dict=True)
if not company:
frappe.throw(_('Company {0} does not exist').format(company))
parts = [account_name.strip(), company.abbr]
if cstr(account_number).strip():
parts.insert(0, cstr(account_number).strip())
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_number):
account = frappe.db.get_value("Account", name, ["account_name", "company"], as_dict=True)
validate_account_number(name, account_number, account.company)
frappe.db.set_value("Account", name, "account_number", account_number)
account_name = account.account_name
if account_name[0].isdigit():
separator = " - " if " - " in account_name else " "
account_name = account_name.split(separator, 1)[1]
frappe.db.set_value("Account", name, "account_name", account_name)
new_name = get_account_autoname(account_number, account_name, account.company)
if name != new_name:
frappe.rename_doc("Account", name, new_name)
return new_name
def get_name_with_number(new_account, account_number):
if account_number and not new_account[0].isdigit():
new_account = account_number + " - " + new_account

View File

@ -41,7 +41,7 @@ frappe.ui.form.on('Cost Center', {
return;
}
frappe.call({
method: "erpnext.accounts.doctype.cost_center.cost_center.update_number_field",
method: "erpnext.accounts.utils.update_number_field",
args: {
doctype_name: frm.doc.doctype,
name: frm.doc.name,
@ -54,7 +54,7 @@ frappe.ui.form.on('Cost Center', {
if(r.message) {
frappe.set_route("Form", "Cost Center", r.message);
} else {
me.set_value("cost_center_number", data.cost_center_number);
me.frm.set_value("cost_center_number", data.cost_center_number);
}
d.hide();
}

View File

@ -56,6 +56,9 @@ class CostCenter(NestedSet):
# Validate properties before merging
super(CostCenter, self).before_rename(olddn, new_cost_center, merge, "is_group")
if not merge:
from erpnext.accounts.doctype.account.account import get_name_with_number
new_cost_center = get_name_with_number(new_cost_center, self.cost_center_number)
return new_cost_center
@ -85,46 +88,3 @@ class CostCenter(NestedSet):
def on_doctype_update():
frappe.db.add_index("Cost Center", ["lft", "rgt"])
def get_doc_name_autoname(field_value, doc_title, name, company):
if company:
name_split=name.split("-")
parts = [doc_title.strip(), name_split[len(name_split)-1].strip()]
else:
parts = [doc_title.strip()]
if cstr(field_value).strip():
parts.insert(0, cstr(field_value).strip())
return ' - '.join(parts)
def validate_field_number(doctype_name, name, field_value, company, field_name):
if field_value:
if company:
doctype_with_same_number = frappe.db.get_value(doctype_name,
{field_name: field_value, "company": company, "name": ["!=", name]})
else:
doctype_with_same_number = frappe.db.get_value(doctype_name,
{field_name: field_value, "name": ["!=", name]})
if doctype_with_same_number:
frappe.throw(_("{0} Number {1} already used in account {2}")
.format(doctype_name, field_value, doctype_with_same_number))
@frappe.whitelist()
def update_number_field(doctype_name, name, field_name, field_value, company):
doc_title = frappe.db.get_value(doctype_name, name, frappe.scrub(doctype_name)+"_name")
validate_field_number(doctype_name, name, field_value, company, field_name)
frappe.db.set_value(doctype_name, name, field_name, field_value)
if doc_title[0].isdigit():
separator = " - " if " - " in doc_title else " "
doc_title = doc_title.split(separator, 1)[1]
frappe.db.set_value(doctype_name, name, frappe.scrub(doctype_name)+"_name", doc_title)
new_name = get_doc_name_autoname(field_value, doc_title, name, company)
if name != new_name:
frappe.rename_doc(doctype_name, name, new_name)
return new_name

View File

@ -758,3 +758,58 @@ def create_payment_gateway_account(gateway):
except frappe.DuplicateEntryError:
# already exists, due to a reinstall?
pass
@frappe.whitelist()
def update_number_field(doctype_name, name, field_name, field_value, company):
'''
doctype_name = Name of the DocType
name = Docname being referred
field_name = Name of the field thats holding the 'number' attribute
field_value = Numeric value entered in field_name
Stores the number entered in the dialog to the DocType's field.
Renames the document by adding the number as a prefix to the current name and updates
all transaction where it was present.
'''
doc_title = frappe.db.get_value(doctype_name, name, frappe.scrub(doctype_name)+"_name")
validate_field_number(doctype_name, name, field_value, company, field_name)
frappe.db.set_value(doctype_name, name, field_name, field_value)
if doc_title[0].isdigit():
separator = " - " if " - " in doc_title else " "
doc_title = doc_title.split(separator, 1)[1]
frappe.db.set_value(doctype_name, name, frappe.scrub(doctype_name)+"_name", doc_title)
new_name = get_doc_name_autoname(field_value, doc_title, name, company)
if name != new_name:
frappe.rename_doc(doctype_name, name, new_name)
return new_name
def validate_field_number(doctype_name, name, field_value, company, field_name):
''' Validate if the number entered isn't already assigned to some other document. '''
if field_value:
if company:
doctype_with_same_number = frappe.db.get_value(doctype_name,
{field_name: field_value, "company": company, "name": ["!=", name]})
else:
doctype_with_same_number = frappe.db.get_value(doctype_name,
{field_name: field_value, "name": ["!=", name]})
if doctype_with_same_number:
frappe.throw(_("{0} Number {1} already used in account {2}")
.format(doctype_name, field_value, doctype_with_same_number))
def get_doc_name_autoname(field_value, doc_title, name, company):
''' append title with prefix as number and suffix as company's abbreviation separated by '-' '''
if company:
name_split=name.split("-")
parts = [doc_title.strip(), name_split[len(name_split)-1].strip()]
else:
parts = [doc_title.strip()]
if cstr(field_value).strip():
parts.insert(0, cstr(field_value).strip())
return ' - '.join(parts)