feat: force cost center renaming from cost center form (#21503)
diff --git a/erpnext/accounts/doctype/cost_center/cost_center.js b/erpnext/accounts/doctype/cost_center/cost_center.js
index 96ec57d..9e2f6ee 100644
--- a/erpnext/accounts/doctype/cost_center/cost_center.js
+++ b/erpnext/accounts/doctype/cost_center/cost_center.js
@@ -18,7 +18,7 @@
},
refresh: function(frm) {
if (!frm.is_new()) {
- frm.add_custom_button(__('Update Cost Center Number'), function () {
+ frm.add_custom_button(__('Update Cost Center Name / Number'), function () {
frm.trigger("update_cost_center_number");
});
}
@@ -47,35 +47,45 @@
},
update_cost_center_number: function(frm) {
var d = new frappe.ui.Dialog({
- title: __('Update Cost Center Number'),
+ title: __('Update Cost Center Name / Number'),
fields: [
{
- "label": 'Cost Center Number',
+ "label": "Cost Center Name",
+ "fieldname": "cost_center_name",
+ "fieldtype": "Data",
+ "reqd": 1,
+ "default": frm.doc.cost_center_name
+ },
+ {
+ "label": "Cost Center Number",
"fieldname": "cost_center_number",
"fieldtype": "Data",
- "reqd": 1
+ "reqd": 1,
+ "default": frm.doc.cost_center_number
}
],
primary_action: function() {
var data = d.get_values();
- if(data.cost_center_number === frm.doc.cost_center_number) {
+ if(data.cost_center_name === frm.doc.cost_center_name && data.cost_center_number === frm.doc.cost_center_number) {
d.hide();
return;
}
+ frappe.dom.freeze();
frappe.call({
- method: "erpnext.accounts.utils.update_number_field",
+ method: "erpnext.accounts.utils.update_cost_center",
args: {
- doctype_name: frm.doc.doctype,
- name: frm.doc.name,
- field_name: d.fields[0].fieldname,
- number_value: data.cost_center_number,
+ docname: frm.doc.name,
+ cost_center_name: data.cost_center_name,
+ cost_center_number: data.cost_center_number,
company: frm.doc.company
},
callback: function(r) {
+ frappe.dom.unfreeze();
if(!r.exc) {
if(r.message) {
frappe.set_route("Form", "Cost Center", r.message);
} else {
+ me.frm.set_value("cost_center_name", data.cost_center_name);
me.frm.set_value("cost_center_number", data.cost_center_number);
}
d.hide();
diff --git a/erpnext/accounts/doctype/cost_center/cost_center.json b/erpnext/accounts/doctype/cost_center/cost_center.json
index 99b89d1..5013c92 100644
--- a/erpnext/accounts/doctype/cost_center/cost_center.json
+++ b/erpnext/accounts/doctype/cost_center/cost_center.json
@@ -2,7 +2,6 @@
"actions": [],
"allow_copy": 1,
"allow_import": 1,
- "allow_rename": 1,
"creation": "2013-01-23 19:57:17",
"description": "Track separate Income and Expense for product verticals or divisions.",
"doctype": "DocType",
@@ -126,7 +125,7 @@
"idx": 1,
"is_tree": 1,
"links": [],
- "modified": "2020-03-18 17:59:04.321637",
+ "modified": "2020-04-29 16:09:30.025214",
"modified_by": "Administrator",
"module": "Accounts",
"name": "Cost Center",
diff --git a/erpnext/accounts/utils.py b/erpnext/accounts/utils.py
index b5d6ca9..5165495 100644
--- a/erpnext/accounts/utils.py
+++ b/erpnext/accounts/utils.py
@@ -817,48 +817,37 @@
pass
@frappe.whitelist()
-def update_number_field(doctype_name, name, field_name, number_value, company):
+def update_cost_center(docname, cost_center_name, cost_center_number, company):
'''
- doctype_name = Name of the DocType
- name = Docname being referred
- field_name = Name of the field thats holding the 'number' attribute
- number_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("Cost Center", docname, cost_center_number, company, "cost_center_number")
- validate_field_number(doctype_name, name, number_value, company, field_name)
+ if cost_center_number:
+ frappe.db.set_value("Cost Center", docname, "cost_center_number", cost_center_number.strip())
+ else:
+ frappe.db.set_value("Cost Center", docname, "cost_center_number", "")
- frappe.db.set_value(doctype_name, name, field_name, number_value)
+ frappe.db.set_value("Cost Center", docname, "cost_center_name", cost_center_name.strip())
- 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_autoname_with_number(number_value, doc_title, name, company)
-
- if name != new_name:
- frappe.rename_doc(doctype_name, name, new_name)
+ new_name = get_autoname_with_number(cost_center_number, cost_center_name, docname, company)
+ if docname != new_name:
+ frappe.rename_doc("Cost Center", docname, new_name, force=1)
return new_name
-def validate_field_number(doctype_name, name, number_value, company, field_name):
+def validate_field_number(doctype_name, docname, number_value, company, field_name):
''' Validate if the number entered isn't already assigned to some other document. '''
if number_value:
+ filters = {field_name: number_value, "name": ["!=", docname]}
if company:
- doctype_with_same_number = frappe.db.get_value(doctype_name,
- {field_name: number_value, "company": company, "name": ["!=", name]})
- else:
- doctype_with_same_number = frappe.db.get_value(doctype_name,
- {field_name: number_value, "name": ["!=", name]})
+ filters["company"] = company
+
+ doctype_with_same_number = frappe.db.get_value(doctype_name, filters)
+
if doctype_with_same_number:
- frappe.throw(_("{0} Number {1} already used in account {2}")
- .format(doctype_name, number_value, doctype_with_same_number))
+ frappe.throw(_("{0} Number {1} is already used in {2} {3}")
+ .format(doctype_name, number_value, doctype_name.lower(), doctype_with_same_number))
def get_autoname_with_number(number_value, doc_title, name, company):
''' append title with prefix as number and suffix as company's abbreviation separated by '-' '''