Tree fix (#14425)

* 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
diff --git a/erpnext/accounts/doctype/account/account.js b/erpnext/accounts/doctype/account/account.js
index 50fc9ca..1a23d5f 100644
--- a/erpnext/accounts/doctype/account/account.js
+++ b/erpnext/accounts/doctype/account/account.js
@@ -111,10 +111,13 @@
 				}
 
 				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) {
diff --git a/erpnext/accounts/doctype/account/account.py b/erpnext/accounts/doctype/account/account.py
index 3d03d9d..dcdf02c 100644
--- a/erpnext/accounts/doctype/account/account.py
+++ b/erpnext/accounts/doctype/account/account.py
@@ -232,44 +232,6 @@
 
 	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
diff --git a/erpnext/accounts/doctype/cost_center/cost_center.js b/erpnext/accounts/doctype/cost_center/cost_center.js
index a5bcaf4..8f3ae19 100644
--- a/erpnext/accounts/doctype/cost_center/cost_center.js
+++ b/erpnext/accounts/doctype/cost_center/cost_center.js
@@ -41,7 +41,7 @@
 					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 @@
 							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();
 						}
diff --git a/erpnext/accounts/doctype/cost_center/cost_center.py b/erpnext/accounts/doctype/cost_center/cost_center.py
index 24af0ce..1200902 100644
--- a/erpnext/accounts/doctype/cost_center/cost_center.py
+++ b/erpnext/accounts/doctype/cost_center/cost_center.py
@@ -56,6 +56,9 @@
 
 		# 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 @@
 
 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		
\ No newline at end of file
diff --git a/erpnext/accounts/utils.py b/erpnext/accounts/utils.py
index cd63518..b1c1972 100644
--- a/erpnext/accounts/utils.py
+++ b/erpnext/accounts/utils.py
@@ -758,3 +758,58 @@
 	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)