Fixes in the naming series (#10480)

* rewrite the js

* fix the space issue in the naming series
diff --git a/erpnext/setup/doctype/naming_series/naming_series.js b/erpnext/setup/doctype/naming_series/naming_series.js
index e2584bf..7c76d9c 100644
--- a/erpnext/setup/doctype/naming_series/naming_series.js
+++ b/erpnext/setup/doctype/naming_series/naming_series.js
@@ -1,47 +1,55 @@
-// Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
-// License: GNU General Public License v3. See license.txt
+// Copyright (c) 2017, Frappe Technologies Pvt. Ltd. and contributors
+// For license information, please see license.txt
 
 
-cur_frm.cscript.onload_post_render = function(doc, cdt, cdn) {
-	cur_frm.disable_save();
-	cur_frm.toolbar.print_icon.addClass("hide");
-	return cur_frm.call({
-		doc: cur_frm.doc,
-		method: 'get_transactions',
-		callback: function(r) {
-			cur_frm.cscript.update_selects(r);
-			cur_frm.cscript.select_doc_for_series(doc, cdt, cdn);
-		}
-	});
-}
+frappe.ui.form.on("Naming Series", {
+	onload: function(frm) {
+		frm.disable_save();
+		frm.events.get_doc_and_prefix(frm);
+	},
 
-cur_frm.cscript.update_selects = function(r) {
-	set_field_options('select_doc_for_series', r.message.transactions);
-	set_field_options('prefix', r.message.prefixes);
-}
+	get_doc_and_prefix: function(frm) {
+		frappe.call({
+			method: "get_transactions",
+			doc: frm.doc,
+			callback: function(r) {
+				frm.set_df_property("select_doc_for_series", "options", r.message.transactions);
+				frm.set_df_property("prefix", "options", r.message.prefixes);
+			}
+		});
+	},
 
-cur_frm.cscript.select_doc_for_series = function(doc, cdt, cdn) {
-	cur_frm.set_value('user_must_always_select', 0);
-	cur_frm.toggle_display(['help_html','set_options', 'user_must_always_select', 'update'],
-		doc.select_doc_for_series);
+	select_doc_for_series: function(frm) {
+		frm.set_value("user_must_always_select", 0);
+		frappe.call({
+			method: "get_options",
+			doc: frm.doc,
+			callback: function(r) {
+				frm.set_value("set_options", r.message);
+				if(r.message && r.message.split('\n')[0]=='')
+					frm.set_value('user_must_always_select', 1);
+				frm.refresh();
+			}
+		});
+	},
 
-	var callback = function(r, rt){
-		locals[cdt][cdn].set_options = r.message;
-		refresh_field('set_options');
-		if(r.message && r.message.split('\n')[0]=='')
-			cur_frm.set_value('user_must_always_select', 1);
+	prefix: function(frm) {
+		frappe.call({
+			method: "get_current",
+			doc: frm.doc,
+			callback: function(r) {
+				frm.refresh_field("current_value");
+			}
+		});
+	},
+
+	update: function(frm) {
+		frappe.call({
+			method: "update_series",
+			doc: frm.doc,
+			callback: function(r) {
+				frm.events.get_doc_and_prefix(frm);
+			}
+		});
 	}
-
-	if(doc.select_doc_for_series)
-		return $c_obj(doc,'get_options','',callback);
-}
-
-cur_frm.cscript.update = function() {
-	return cur_frm.call_server('update_series', '', cur_frm.cscript.update_selects);
-}
-
-cur_frm.cscript.prefix = function(doc, dt, dn) {
-	return cur_frm.call_server('get_current', '', function(r) {
-		refresh_field('current_value');
-	});
-}
+});
diff --git a/erpnext/setup/doctype/naming_series/naming_series.json b/erpnext/setup/doctype/naming_series/naming_series.json
index 0daf1a1..f936dcf 100644
--- a/erpnext/setup/doctype/naming_series/naming_series.json
+++ b/erpnext/setup/doctype/naming_series/naming_series.json
@@ -1,29 +1,40 @@
 {
  "allow_copy": 0, 
+ "allow_guest_to_view": 0, 
  "allow_import": 0, 
  "allow_rename": 0, 
+ "beta": 0, 
  "creation": "2013-01-25 11:35:08", 
  "custom": 0, 
  "description": "Set prefix for numbering series on your transactions", 
  "docstatus": 0, 
  "doctype": "DocType", 
+ "editable_grid": 0, 
  "fields": [
   {
+   "allow_bulk_edit": 0, 
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
+   "columns": 0, 
    "description": "Set prefix for numbering series on your transactions", 
    "fieldname": "setup_series", 
    "fieldtype": "Section Break", 
    "hidden": 0, 
    "ignore_user_permissions": 0, 
+   "ignore_xss_filter": 0, 
    "in_filter": 0, 
+   "in_global_search": 0, 
    "in_list_view": 0, 
+   "in_standard_filter": 0, 
    "label": "Setup Series", 
+   "length": 0, 
    "no_copy": 0, 
    "permlevel": 0, 
    "print_hide": 0, 
+   "print_hide_if_no_value": 0, 
    "read_only": 0, 
+   "remember_last_selected_value": 0, 
    "report_hide": 0, 
    "reqd": 0, 
    "search_index": 0, 
@@ -31,20 +42,28 @@
    "unique": 0
   }, 
   {
+   "allow_bulk_edit": 0, 
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
+   "columns": 0, 
    "fieldname": "select_doc_for_series", 
    "fieldtype": "Select", 
    "hidden": 0, 
    "ignore_user_permissions": 0, 
+   "ignore_xss_filter": 0, 
    "in_filter": 0, 
+   "in_global_search": 0, 
    "in_list_view": 0, 
+   "in_standard_filter": 0, 
    "label": "Select Transaction", 
+   "length": 0, 
    "no_copy": 0, 
    "permlevel": 0, 
    "print_hide": 0, 
+   "print_hide_if_no_value": 0, 
    "read_only": 0, 
+   "remember_last_selected_value": 0, 
    "report_hide": 0, 
    "reqd": 0, 
    "search_index": 0, 
@@ -52,21 +71,30 @@
    "unique": 0
   }, 
   {
+   "allow_bulk_edit": 0, 
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
+   "columns": 0, 
+   "depends_on": "select_doc_for_series", 
    "fieldname": "help_html", 
    "fieldtype": "HTML", 
    "hidden": 0, 
    "ignore_user_permissions": 0, 
+   "ignore_xss_filter": 0, 
    "in_filter": 0, 
+   "in_global_search": 0, 
    "in_list_view": 0, 
+   "in_standard_filter": 0, 
    "label": "Help HTML", 
+   "length": 0, 
    "no_copy": 0, 
    "options": "<div class=\"well\">\nEdit list of Series in the box below. Rules:\n<ul>\n<li>Each Series Prefix on a new line.</li>\n<li>Allowed special characters are \"/\" and \"-\"</li>\n<li>Optionally, set the number of digits in the series using dot (.) followed by hashes (#). For example, \".####\" means that the series will have four digits. Default is five digits.</li>\n</ul>\nExamples:<br>\nINV-<br>\nINV-10-<br>\nINVK-<br>\nINV-.####<br>\n</div>", 
    "permlevel": 0, 
    "print_hide": 0, 
+   "print_hide_if_no_value": 0, 
    "read_only": 0, 
+   "remember_last_selected_value": 0, 
    "report_hide": 0, 
    "reqd": 0, 
    "search_index": 0, 
@@ -74,20 +102,29 @@
    "unique": 0
   }, 
   {
+   "allow_bulk_edit": 0, 
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
+   "columns": 0, 
+   "depends_on": "select_doc_for_series", 
    "fieldname": "set_options", 
    "fieldtype": "Text", 
    "hidden": 0, 
    "ignore_user_permissions": 0, 
+   "ignore_xss_filter": 0, 
    "in_filter": 0, 
+   "in_global_search": 0, 
    "in_list_view": 0, 
+   "in_standard_filter": 0, 
    "label": "Series List for this Transaction", 
+   "length": 0, 
    "no_copy": 0, 
    "permlevel": 0, 
    "print_hide": 0, 
+   "print_hide_if_no_value": 0, 
    "read_only": 0, 
+   "remember_last_selected_value": 0, 
    "report_hide": 0, 
    "reqd": 0, 
    "search_index": 0, 
@@ -95,21 +132,30 @@
    "unique": 0
   }, 
   {
+   "allow_bulk_edit": 0, 
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
+   "columns": 0, 
+   "depends_on": "select_doc_for_series", 
    "description": "Check this if you want to force the user to select a series before saving. There will be no default if you check this.", 
    "fieldname": "user_must_always_select", 
    "fieldtype": "Check", 
    "hidden": 0, 
    "ignore_user_permissions": 0, 
+   "ignore_xss_filter": 0, 
    "in_filter": 0, 
+   "in_global_search": 0, 
    "in_list_view": 0, 
+   "in_standard_filter": 0, 
    "label": "User must always select", 
+   "length": 0, 
    "no_copy": 0, 
    "permlevel": 0, 
    "print_hide": 0, 
+   "print_hide_if_no_value": 0, 
    "read_only": 0, 
+   "remember_last_selected_value": 0, 
    "report_hide": 0, 
    "reqd": 0, 
    "search_index": 0, 
@@ -117,20 +163,30 @@
    "unique": 0
   }, 
   {
+   "allow_bulk_edit": 0, 
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
+   "columns": 0, 
+   "depends_on": "select_doc_for_series", 
    "fieldname": "update", 
    "fieldtype": "Button", 
    "hidden": 0, 
    "ignore_user_permissions": 0, 
+   "ignore_xss_filter": 0, 
    "in_filter": 0, 
+   "in_global_search": 0, 
    "in_list_view": 0, 
+   "in_standard_filter": 0, 
    "label": "Update", 
+   "length": 0, 
    "no_copy": 0, 
+   "options": "", 
    "permlevel": 0, 
    "print_hide": 0, 
+   "print_hide_if_no_value": 0, 
    "read_only": 0, 
+   "remember_last_selected_value": 0, 
    "report_hide": 0, 
    "reqd": 0, 
    "search_index": 0, 
@@ -138,21 +194,29 @@
    "unique": 0
   }, 
   {
+   "allow_bulk_edit": 0, 
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
+   "columns": 0, 
    "description": "Change the starting / current sequence number of an existing series.", 
    "fieldname": "update_series", 
    "fieldtype": "Section Break", 
    "hidden": 0, 
    "ignore_user_permissions": 0, 
+   "ignore_xss_filter": 0, 
    "in_filter": 0, 
+   "in_global_search": 0, 
    "in_list_view": 0, 
+   "in_standard_filter": 0, 
    "label": "Update Series", 
+   "length": 0, 
    "no_copy": 0, 
    "permlevel": 0, 
    "print_hide": 0, 
+   "print_hide_if_no_value": 0, 
    "read_only": 0, 
+   "remember_last_selected_value": 0, 
    "report_hide": 0, 
    "reqd": 0, 
    "search_index": 0, 
@@ -160,20 +224,28 @@
    "unique": 0
   }, 
   {
+   "allow_bulk_edit": 0, 
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
+   "columns": 0, 
    "fieldname": "prefix", 
    "fieldtype": "Select", 
    "hidden": 0, 
    "ignore_user_permissions": 0, 
+   "ignore_xss_filter": 0, 
    "in_filter": 0, 
+   "in_global_search": 0, 
    "in_list_view": 0, 
+   "in_standard_filter": 0, 
    "label": "Prefix", 
+   "length": 0, 
    "no_copy": 0, 
    "permlevel": 0, 
    "print_hide": 0, 
+   "print_hide_if_no_value": 0, 
    "read_only": 0, 
+   "remember_last_selected_value": 0, 
    "report_hide": 0, 
    "reqd": 0, 
    "search_index": 0, 
@@ -181,21 +253,29 @@
    "unique": 0
   }, 
   {
+   "allow_bulk_edit": 0, 
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
+   "columns": 0, 
    "description": "This is the number of the last created transaction with this prefix", 
    "fieldname": "current_value", 
    "fieldtype": "Int", 
    "hidden": 0, 
    "ignore_user_permissions": 0, 
+   "ignore_xss_filter": 0, 
    "in_filter": 0, 
+   "in_global_search": 0, 
    "in_list_view": 0, 
+   "in_standard_filter": 0, 
    "label": "Current Value", 
+   "length": 0, 
    "no_copy": 0, 
    "permlevel": 0, 
    "print_hide": 0, 
+   "print_hide_if_no_value": 0, 
    "read_only": 0, 
+   "remember_last_selected_value": 0, 
    "report_hide": 0, 
    "reqd": 0, 
    "search_index": 0, 
@@ -203,21 +283,29 @@
    "unique": 0
   }, 
   {
+   "allow_bulk_edit": 0, 
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
+   "columns": 0, 
    "fieldname": "update_series_start", 
    "fieldtype": "Button", 
    "hidden": 0, 
    "ignore_user_permissions": 0, 
+   "ignore_xss_filter": 0, 
    "in_filter": 0, 
+   "in_global_search": 0, 
    "in_list_view": 0, 
+   "in_standard_filter": 0, 
    "label": "Update Series Number", 
+   "length": 0, 
    "no_copy": 0, 
    "options": "update_series_start", 
    "permlevel": 0, 
    "print_hide": 0, 
+   "print_hide_if_no_value": 0, 
    "read_only": 0, 
+   "remember_last_selected_value": 0, 
    "report_hide": 0, 
    "reqd": 0, 
    "search_index": 0, 
@@ -225,16 +313,18 @@
    "unique": 0
   }
  ], 
+ "has_web_view": 0, 
  "hide_heading": 0, 
  "hide_toolbar": 1, 
  "icon": "fa fa-sort-by-order", 
  "idx": 1, 
+ "image_view": 0, 
  "in_create": 0, 
- "in_dialog": 0, 
  "is_submittable": 0, 
  "issingle": 1, 
  "istable": 0, 
- "modified": "2015-02-05 05:11:41.473793", 
+ "max_attachments": 0, 
+ "modified": "2017-08-17 03:41:37.685910", 
  "modified_by": "Administrator", 
  "module": "Setup", 
  "name": "Naming Series", 
@@ -261,6 +351,10 @@
    "write": 1
   }
  ], 
+ "quick_entry": 0, 
  "read_only": 1, 
- "read_only_onload": 0
+ "read_only_onload": 0, 
+ "show_name_in_global_search": 0, 
+ "track_changes": 0, 
+ "track_seen": 0
 }
\ No newline at end of file
diff --git a/erpnext/setup/doctype/naming_series/naming_series.py b/erpnext/setup/doctype/naming_series/naming_series.py
index 536b72f..4534584 100644
--- a/erpnext/setup/doctype/naming_series/naming_series.py
+++ b/erpnext/setup/doctype/naming_series/naming_series.py
@@ -21,7 +21,6 @@
 				where fieldname='naming_series'""")))
 
 		doctypes = list(set(get_doctypes_with_read()) | set(doctypes))
-
 		prefixes = ""
 		for d in doctypes:
 			options = ""
@@ -34,9 +33,8 @@
 
 			if options:
 				prefixes = prefixes + "\n" + options
-
 		prefixes.replace("\n\n", "\n")
-		prefixes = "\n".join(sorted(prefixes.split()))
+		prefixes = "\n".join(sorted(prefixes.split("\n")))
 
 		return {
 			"transactions": "\n".join([''] + sorted(doctypes)),
@@ -112,9 +110,7 @@
 				where dt.name = df.dt and df.fieldname='naming_series' and dt.name != %s""",
 				self.select_doc_for_series)
 			))
-		sr = [[frappe.get_meta(p).get_field("naming_series").options, p]
-			for p in parent]
-
+		sr = [[frappe.get_meta(p).get_field("naming_series").options, p] for p in parent]
 		dt = frappe.get_doc("DocType", self.select_doc_for_series)
 		options = self.scrub_options_list(self.set_options.split("\n"))
 		for series in options:
diff --git a/erpnext/setup/doctype/naming_series/test_naming_series.js b/erpnext/setup/doctype/naming_series/test_naming_series.js
new file mode 100644
index 0000000..22b664b
--- /dev/null
+++ b/erpnext/setup/doctype/naming_series/test_naming_series.js
@@ -0,0 +1,23 @@
+/* eslint-disable */
+// rename this file from _test_[name] to test_[name] to activate
+// and remove above this line
+
+QUnit.test("test: Naming Series", function (assert) {
+	let done = assert.async();
+
+	// number of asserts
+	assert.expect(1);
+
+	frappe.run_serially([
+		// insert a new Naming Series
+		() => frappe.tests.make('Naming Series', [
+			// values to be set
+			{key: 'value'}
+		]),
+		() => {
+			assert.equal(cur_frm.doc.key, 'value');
+		},
+		() => done()
+	]);
+
+});