Merge pull request #4311 from rmehta/auth-rule-cleanup

Auth rule cleanup
diff --git a/erpnext/setup/doctype/authorization_rule/authorization_rule.js b/erpnext/setup/doctype/authorization_rule/authorization_rule.js
index 336e709..6b606e1 100644
--- a/erpnext/setup/doctype/authorization_rule/authorization_rule.js
+++ b/erpnext/setup/doctype/authorization_rule/authorization_rule.js
@@ -1,20 +1,59 @@
 // Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
 // License: GNU General Public License v3. See license.txt
 
+frappe.ui.form.on("Authorization Rule", {
+	refresh: function(frm) {
+		frm.events.set_master_type(frm);
+	},
+	set_master_type: function(frm) {
+		if(frm.doc.based_on==="Customerwise Discount") {
+			unhide_field("master_name");
+			frm.set_value("customer_or_item", "Customer");
+		} else if(frm.doc.based_on==="Itemwise Discount") {
+			unhide_field("master_name");
+			frm.set_value("customer_or_item", "Item");
+		} else {
+			frm.set_value("customer_or_item", "");
+			frm.set_value("master_name", "");
+			hide_field("master_name");
+		}
+	},
+	based_on: function(frm) {
+		frm.events.set_master_type(frm);
+		if (frm.doc.based_on === 'Not Applicable') {
+			frm.set_value("value", 0);
+			hide_field('value');
+		} else {
+			unhide_field('value');
+		}
+	},
+	transaction: function(frm) {
+		if (frm.doc.transaction == 'Appraisal') {
+			frm.set_value("based_on", "Not Applicable");
+			frm.set_value("master_name", "");
+			frm.set_value("system_role", "");
+			frm.set_value("system_user", "");
+			frm.set_value("value", 0);
+			hide_field(['based_on', 'system_role', 'system_user', 'value']);
+			unhide_field(['to_emp','to_designation']);
+		}
+		else {
+			unhide_field(['system_role', 'system_user','value', 'based_on']);
+			hide_field(['to_emp','to_designation']);
+		}
+	}
+})
+
+
 // Settings Module
 cur_frm.cscript.refresh = function(doc, cdt, cdn) {
-	if (doc.based_on == 'Grand Total' || doc.based_on == 'Average Discount' || doc.based_on == 'Not Applicable')
-		hide_field('master_name');
-	else
-		unhide_field('master_name');
-
 	if (doc.based_on == 'Not Applicable')
 		hide_field('value');
 	else
 		unhide_field('value');
 
 	if (doc.transaction == 'Appraisal') {
-		hide_field(['master_name','system_role', 'system_user']);
+		hide_field(['system_role', 'system_user']);
 		unhide_field(['to_emp','to_designation']);
 
 		if (doc.transaction == 'Appraisal')
@@ -23,47 +62,11 @@
 			unhide_field('value');
 	}
 	else {
-		unhide_field(['master_name','system_role', 'system_user','value']);
+		unhide_field(['system_role', 'system_user','value']);
 		hide_field(['to_emp','to_designation']);
 	}
 }
 
-cur_frm.cscript.based_on = function(doc) {
-	if (doc.based_on == 'Grand Total' || doc.based_on == 'Average Discount' || doc.based_on == 'Not Applicable') {
-		doc.master_name = '';
-		refresh_field('master_name');
-		hide_field('master_name');
-	}
-	else
-		unhide_field('master_name');
-
-	if (doc.based_on == 'Not Applicable') {
-		doc.value =0;
-		refresh_field('value');
-		hide_field('value');
-	}
-	else
-		unhide_field('value');
-}
-
-cur_frm.cscript.transaction = function(doc, cdt, cdn){
-	if (doc.transaction == 'Appraisal') {
-		doc.based_on == 'Not Applicable';
-		doc.master_name = doc.system_role = doc.system_user = '';
-		refresh_many(['master_name','system_role', 'system_user', 'based_on']);
-		hide_field(['master_name','system_role', 'system_user']);
-		unhide_field(['to_emp','to_designation']);
-		doc.value = 0;
-		refresh_many('value');
-		hide_field('value');
-	}
-	else {
-		unhide_field(['master_name','system_role', 'system_user','value']);
-		hide_field(['to_emp','to_designation']);
-	}
-
-}
-
 cur_frm.fields_dict.system_user.get_query = function(doc, cdt, cdn) {
 	return { query:"frappe.core.doctype.user.user.user_query" }
 }
diff --git a/erpnext/setup/doctype/authorization_rule/authorization_rule.json b/erpnext/setup/doctype/authorization_rule/authorization_rule.json
index f6a74ef..45c290e 100644
--- a/erpnext/setup/doctype/authorization_rule/authorization_rule.json
+++ b/erpnext/setup/doctype/authorization_rule/authorization_rule.json
@@ -13,6 +13,122 @@
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
+   "fieldname": "transaction", 
+   "fieldtype": "Select", 
+   "hidden": 0, 
+   "ignore_user_permissions": 0, 
+   "in_filter": 0, 
+   "in_list_view": 1, 
+   "label": "Transaction", 
+   "no_copy": 0, 
+   "oldfieldname": "transaction", 
+   "oldfieldtype": "Select", 
+   "options": "\nSales Order\nPurchase Order\nQuotation\nDelivery Note\nSales Invoice\nPurchase Invoice\nPurchase Receipt\nAppraisal", 
+   "permlevel": 0, 
+   "print_hide": 0, 
+   "read_only": 0, 
+   "report_hide": 0, 
+   "reqd": 1, 
+   "search_index": 0, 
+   "set_only_once": 0, 
+   "unique": 0
+  }, 
+  {
+   "allow_on_submit": 0, 
+   "bold": 0, 
+   "collapsible": 0, 
+   "fieldname": "based_on", 
+   "fieldtype": "Select", 
+   "hidden": 0, 
+   "ignore_user_permissions": 0, 
+   "in_filter": 0, 
+   "in_list_view": 1, 
+   "label": "Based On", 
+   "no_copy": 0, 
+   "oldfieldname": "based_on", 
+   "oldfieldtype": "Select", 
+   "options": "\nGrand Total\nAverage Discount\nCustomerwise Discount\nItemwise Discount\nNot Applicable", 
+   "permlevel": 0, 
+   "print_hide": 0, 
+   "read_only": 0, 
+   "report_hide": 0, 
+   "reqd": 1, 
+   "search_index": 0, 
+   "set_only_once": 0, 
+   "unique": 0
+  }, 
+  {
+   "allow_on_submit": 0, 
+   "bold": 0, 
+   "collapsible": 0, 
+   "fieldname": "customer_or_item", 
+   "fieldtype": "Select", 
+   "hidden": 1, 
+   "ignore_user_permissions": 0, 
+   "in_filter": 0, 
+   "in_list_view": 0, 
+   "label": "Customer or Item", 
+   "no_copy": 0, 
+   "options": "Customer\nItem", 
+   "permlevel": 0, 
+   "precision": "", 
+   "print_hide": 0, 
+   "read_only": 1, 
+   "report_hide": 0, 
+   "reqd": 0, 
+   "search_index": 0, 
+   "set_only_once": 0, 
+   "unique": 0
+  }, 
+  {
+   "allow_on_submit": 0, 
+   "bold": 0, 
+   "collapsible": 0, 
+   "fieldname": "master_name", 
+   "fieldtype": "Dynamic Link", 
+   "hidden": 0, 
+   "ignore_user_permissions": 0, 
+   "in_filter": 0, 
+   "in_list_view": 1, 
+   "label": "Customer / Item Name", 
+   "no_copy": 0, 
+   "oldfieldname": "master_name", 
+   "oldfieldtype": "Link", 
+   "options": "customer_or_item", 
+   "permlevel": 0, 
+   "print_hide": 0, 
+   "read_only": 0, 
+   "report_hide": 0, 
+   "reqd": 0, 
+   "search_index": 0, 
+   "set_only_once": 0, 
+   "unique": 0
+  }, 
+  {
+   "allow_on_submit": 0, 
+   "bold": 0, 
+   "collapsible": 0, 
+   "fieldname": "column_break_3", 
+   "fieldtype": "Column Break", 
+   "hidden": 0, 
+   "ignore_user_permissions": 0, 
+   "in_filter": 0, 
+   "in_list_view": 0, 
+   "no_copy": 0, 
+   "permlevel": 0, 
+   "precision": "", 
+   "print_hide": 0, 
+   "read_only": 0, 
+   "report_hide": 0, 
+   "reqd": 0, 
+   "search_index": 0, 
+   "set_only_once": 0, 
+   "unique": 0
+  }, 
+  {
+   "allow_on_submit": 0, 
+   "bold": 0, 
+   "collapsible": 0, 
    "fieldname": "company", 
    "fieldtype": "Link", 
    "hidden": 0, 
@@ -37,22 +153,19 @@
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
-   "fieldname": "transaction", 
-   "fieldtype": "Select", 
+   "fieldname": "section_break_17", 
+   "fieldtype": "Section Break", 
    "hidden": 0, 
    "ignore_user_permissions": 0, 
    "in_filter": 0, 
-   "in_list_view": 1, 
-   "label": "Transaction",  
+   "in_list_view": 0, 
    "no_copy": 0, 
-   "oldfieldname": "transaction", 
-   "oldfieldtype": "Select", 
-   "options": "\nDelivery Note\nPurchase Invoice\nPurchase Order\nPurchase Receipt\nQuotation\nSales Invoice\nSales Order\nAppraisal", 
    "permlevel": 0, 
+   "precision": "", 
    "print_hide": 0, 
    "read_only": 0, 
    "report_hide": 0, 
-   "reqd": 1, 
+   "reqd": 0, 
    "search_index": 0, 
    "set_only_once": 0, 
    "unique": 0
@@ -61,22 +174,21 @@
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
-   "fieldname": "based_on", 
-   "fieldtype": "Select", 
+   "fieldname": "value", 
+   "fieldtype": "Float", 
    "hidden": 0, 
    "ignore_user_permissions": 0, 
    "in_filter": 0, 
-   "in_list_view": 1, 
-   "label": "Based On",  
+   "in_list_view": 0, 
+   "label": "Authorized Value", 
    "no_copy": 0, 
-   "oldfieldname": "based_on", 
-   "oldfieldtype": "Select", 
-   "options": "\nGrand Total\nAverage Discount\nCustomerwise Discount\nItemwise Discount\nNot Applicable", 
+   "oldfieldname": "value", 
+   "oldfieldtype": "Currency", 
    "permlevel": 0, 
    "print_hide": 0, 
    "read_only": 0, 
    "report_hide": 0, 
-   "reqd": 1, 
+   "reqd": 0, 
    "search_index": 0, 
    "set_only_once": 0, 
    "unique": 0
@@ -85,18 +197,15 @@
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
-   "fieldname": "master_name", 
-   "fieldtype": "Link", 
+   "fieldname": "section_break_7", 
+   "fieldtype": "Section Break", 
    "hidden": 0, 
    "ignore_user_permissions": 0, 
    "in_filter": 0, 
-   "in_list_view": 1, 
-   "label": "Customer / Item Name", 
+   "in_list_view": 0, 
    "no_copy": 0, 
-   "oldfieldname": "master_name", 
-   "oldfieldtype": "Link", 
-   "options": "[Select]", 
    "permlevel": 0, 
+   "precision": "", 
    "print_hide": 0, 
    "read_only": 0, 
    "report_hide": 0, 
@@ -133,31 +242,7 @@
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
-   "fieldname": "system_user", 
-   "fieldtype": "Link", 
-   "hidden": 0, 
-   "ignore_user_permissions": 0, 
-   "in_filter": 0, 
-   "in_list_view": 0, 
-   "label": "Applicable To (User)", 
-   "no_copy": 0, 
-   "oldfieldname": "system_user", 
-   "oldfieldtype": "Link", 
-   "options": "User", 
-   "permlevel": 0, 
-   "print_hide": 0, 
-   "read_only": 0, 
-   "report_hide": 0, 
-   "reqd": 0, 
-   "search_index": 0, 
-   "set_only_once": 0, 
-   "unique": 0
-  }, 
-  {
-   "allow_on_submit": 0, 
-   "bold": 0, 
-   "collapsible": 0, 
-   "description": "This will be used for setting rule in HR module", 
+   "description": "", 
    "fieldname": "to_emp", 
    "fieldtype": "Link", 
    "hidden": 0, 
@@ -182,7 +267,52 @@
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
-   "description": "This will be used for setting rule in HR module", 
+   "fieldname": "column_break_10", 
+   "fieldtype": "Column Break", 
+   "hidden": 0, 
+   "ignore_user_permissions": 0, 
+   "in_filter": 0, 
+   "in_list_view": 0, 
+   "no_copy": 0, 
+   "permlevel": 0, 
+   "precision": "", 
+   "print_hide": 0, 
+   "read_only": 0, 
+   "report_hide": 0, 
+   "reqd": 0, 
+   "search_index": 0, 
+   "set_only_once": 0, 
+   "unique": 0
+  }, 
+  {
+   "allow_on_submit": 0, 
+   "bold": 0, 
+   "collapsible": 0, 
+   "fieldname": "system_user", 
+   "fieldtype": "Link", 
+   "hidden": 0, 
+   "ignore_user_permissions": 0, 
+   "in_filter": 0, 
+   "in_list_view": 0, 
+   "label": "Applicable To (User)", 
+   "no_copy": 0, 
+   "oldfieldname": "system_user", 
+   "oldfieldtype": "Link", 
+   "options": "User", 
+   "permlevel": 0, 
+   "print_hide": 0, 
+   "read_only": 0, 
+   "report_hide": 0, 
+   "reqd": 0, 
+   "search_index": 0, 
+   "set_only_once": 0, 
+   "unique": 0
+  }, 
+  {
+   "allow_on_submit": 0, 
+   "bold": 0, 
+   "collapsible": 0, 
+   "description": "", 
    "fieldname": "to_designation", 
    "fieldtype": "Link", 
    "hidden": 0, 
@@ -207,13 +337,34 @@
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
+   "fieldname": "section_break_13", 
+   "fieldtype": "Section Break", 
+   "hidden": 0, 
+   "ignore_user_permissions": 0, 
+   "in_filter": 0, 
+   "in_list_view": 0, 
+   "no_copy": 0, 
+   "permlevel": 0, 
+   "precision": "", 
+   "print_hide": 0, 
+   "read_only": 0, 
+   "report_hide": 0, 
+   "reqd": 0, 
+   "search_index": 0, 
+   "set_only_once": 0, 
+   "unique": 0
+  }, 
+  {
+   "allow_on_submit": 0, 
+   "bold": 0, 
+   "collapsible": 0, 
    "fieldname": "approving_role", 
    "fieldtype": "Link", 
    "hidden": 0, 
    "ignore_user_permissions": 0, 
    "in_filter": 0, 
    "in_list_view": 0, 
-   "label": "Approving Role", 
+   "label": "Approving Role (above authorized value)", 
    "no_copy": 0, 
    "oldfieldname": "approving_role", 
    "oldfieldtype": "Link", 
@@ -231,13 +382,34 @@
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
+   "fieldname": "column_break_15", 
+   "fieldtype": "Column Break", 
+   "hidden": 0, 
+   "ignore_user_permissions": 0, 
+   "in_filter": 0, 
+   "in_list_view": 0, 
+   "no_copy": 0, 
+   "permlevel": 0, 
+   "precision": "", 
+   "print_hide": 0, 
+   "read_only": 0, 
+   "report_hide": 0, 
+   "reqd": 0, 
+   "search_index": 0, 
+   "set_only_once": 0, 
+   "unique": 0
+  }, 
+  {
+   "allow_on_submit": 0, 
+   "bold": 0, 
+   "collapsible": 0, 
    "fieldname": "approving_user", 
    "fieldtype": "Link", 
    "hidden": 0, 
    "ignore_user_permissions": 0, 
    "in_filter": 0, 
    "in_list_view": 0, 
-   "label": "Approving User", 
+   "label": "Approving User  (above authorized value)", 
    "no_copy": 0, 
    "oldfieldname": "approving_user", 
    "oldfieldtype": "Link", 
@@ -250,29 +422,6 @@
    "search_index": 0, 
    "set_only_once": 0, 
    "unique": 0
-  }, 
-  {
-   "allow_on_submit": 0, 
-   "bold": 0, 
-   "collapsible": 0, 
-   "fieldname": "value", 
-   "fieldtype": "Float", 
-   "hidden": 0, 
-   "ignore_user_permissions": 0, 
-   "in_filter": 0, 
-   "in_list_view": 0, 
-   "label": "Above Value", 
-   "no_copy": 0, 
-   "oldfieldname": "value", 
-   "oldfieldtype": "Currency", 
-   "permlevel": 0, 
-   "print_hide": 0, 
-   "read_only": 0, 
-   "report_hide": 0, 
-   "reqd": 0, 
-   "search_index": 0, 
-   "set_only_once": 0, 
-   "unique": 0
   }
  ], 
  "hide_heading": 0, 
@@ -284,7 +433,7 @@
  "is_submittable": 0, 
  "issingle": 0, 
  "istable": 0, 
- "modified": "2015-10-02 07:38:38.997664", 
+ "modified": "2015-11-09 00:57:05.515494", 
  "modified_by": "Administrator", 
  "module": "Setup", 
  "name": "Authorization Rule",