Fixes in user permissions patches
diff --git a/erpnext/accounts/doctype/purchase_taxes_and_charges/purchase_taxes_and_charges.json b/erpnext/accounts/doctype/purchase_taxes_and_charges/purchase_taxes_and_charges.json
index 147f2f5..6079fa2 100644
--- a/erpnext/accounts/doctype/purchase_taxes_and_charges/purchase_taxes_and_charges.json
+++ b/erpnext/accounts/doctype/purchase_taxes_and_charges/purchase_taxes_and_charges.json
@@ -1,417 +1,164 @@
 {
- "_last_update": null,
- "_user_tags": null,
- "allow_attach": null,
- "allow_copy": null,
- "allow_email": null,
- "allow_import": null,
- "allow_print": null,
- "allow_rename": null,
- "allow_trash": null,
  "autoname": "PVTD.######",
- "change_log": null,
- "client_script": null,
- "client_script_core": null,
- "client_string": null,
- "colour": null,
  "creation": "2013-05-21 16:16:04",
- "custom": null,
- "default_print_format": null,
- "description": null,
  "docstatus": 0,
  "doctype": "DocType",
- "document_type": null,
- "dt_template": null,
  "fields": [
   {
-   "allow_on_submit": null,
    "default": "Valuation and Total",
-   "depends_on": null,
-   "description": null,
    "fieldname": "category",
    "fieldtype": "Select",
-   "hidden": null,
-   "in_filter": null,
    "in_list_view": 0,
    "label": "Consider Tax or Charge for",
-   "no_column": null,
-   "no_copy": null,
    "oldfieldname": "category",
    "oldfieldtype": "Select",
    "options": "Valuation and Total\nValuation\nTotal",
    "permlevel": 0,
-   "print_hide": null,
-   "print_width": null,
    "read_only": 0,
-   "report_hide": null,
-   "reqd": 1,
-   "search_index": null,
-   "set_only_once": null,
-   "trigger": null,
-   "width": null
+   "reqd": 1
   },
   {
-   "allow_on_submit": null,
    "default": "Add",
-   "depends_on": null,
-   "description": null,
    "fieldname": "add_deduct_tax",
    "fieldtype": "Select",
-   "hidden": null,
-   "in_filter": null,
-   "in_list_view": null,
    "label": "Add or Deduct",
-   "no_column": null,
-   "no_copy": null,
    "oldfieldname": "add_deduct_tax",
    "oldfieldtype": "Select",
    "options": "Add\nDeduct",
    "permlevel": 0,
-   "print_hide": null,
-   "print_width": null,
    "read_only": 0,
-   "report_hide": null,
-   "reqd": 1,
-   "search_index": null,
-   "set_only_once": null,
-   "trigger": null,
-   "width": null
+   "reqd": 1
   },
   {
-   "allow_on_submit": null,
-   "default": null,
-   "depends_on": null,
-   "description": null,
    "fieldname": "charge_type",
    "fieldtype": "Select",
-   "hidden": null,
-   "in_filter": null,
    "in_list_view": 1,
    "label": "Type",
-   "no_column": null,
-   "no_copy": null,
    "oldfieldname": "charge_type",
    "oldfieldtype": "Select",
    "options": "\nActual\nOn Net Total\nOn Previous Row Amount\nOn Previous Row Total",
    "permlevel": 0,
-   "print_hide": null,
-   "print_width": null,
    "read_only": 0,
-   "report_hide": null,
-   "reqd": 1,
-   "search_index": null,
-   "set_only_once": null,
-   "trigger": null,
-   "width": null
+   "reqd": 1
   },
   {
-   "allow_on_submit": null,
-   "default": null,
    "depends_on": "eval:[\"On Previous Row Amount\", \"On Previous Row Total\"].indexOf(doc.charge_type)!==-1",
-   "description": null,
    "fieldname": "row_id",
    "fieldtype": "Data",
    "hidden": 0,
-   "in_filter": null,
-   "in_list_view": null,
    "label": "Reference Row #",
-   "no_column": null,
-   "no_copy": null,
    "oldfieldname": "row_id",
    "oldfieldtype": "Data",
-   "options": null,
    "permlevel": 0,
-   "print_hide": null,
-   "print_width": null,
-   "read_only": 0,
-   "report_hide": null,
-   "reqd": null,
-   "search_index": null,
-   "set_only_once": null,
-   "trigger": null,
-   "width": null
+   "read_only": 0
   },
   {
-   "allow_on_submit": null,
-   "default": null,
-   "depends_on": null,
-   "description": null,
    "fieldname": "description",
    "fieldtype": "Small Text",
-   "hidden": null,
-   "in_filter": null,
    "in_list_view": 1,
    "label": "Description",
-   "no_column": null,
-   "no_copy": null,
    "oldfieldname": "description",
    "oldfieldtype": "Small Text",
-   "options": null,
    "permlevel": 0,
-   "print_hide": null,
    "print_width": "300px",
    "read_only": 0,
-   "report_hide": null,
    "reqd": 1,
-   "search_index": null,
-   "set_only_once": null,
-   "trigger": null,
    "width": "300px"
   },
   {
-   "allow_on_submit": null,
-   "default": null,
-   "depends_on": null,
-   "description": null,
    "fieldname": "col_break1",
    "fieldtype": "Column Break",
-   "hidden": null,
-   "in_filter": null,
-   "in_list_view": null,
-   "label": null,
-   "no_column": null,
-   "no_copy": null,
-   "oldfieldname": null,
-   "oldfieldtype": null,
-   "options": null,
-   "permlevel": 0,
-   "print_hide": null,
-   "print_width": null,
-   "read_only": null,
-   "report_hide": null,
-   "reqd": null,
-   "search_index": null,
-   "set_only_once": null,
-   "trigger": null,
-   "width": null
+   "permlevel": 0
   },
   {
-   "allow_on_submit": null,
-   "default": null,
-   "depends_on": null,
-   "description": null,
    "fieldname": "account_head",
    "fieldtype": "Link",
-   "hidden": null,
-   "in_filter": null,
    "in_list_view": 0,
    "label": "Account Head",
-   "no_column": null,
-   "no_copy": null,
    "oldfieldname": "account_head",
    "oldfieldtype": "Link",
    "options": "Account",
    "permlevel": 0,
-   "print_hide": null,
-   "print_width": null,
    "read_only": 0,
-   "report_hide": null,
-   "reqd": 1,
-   "search_index": null,
-   "set_only_once": null,
-   "trigger": null,
-   "width": null
+   "reqd": 1
   },
   {
-   "allow_on_submit": null,
    "default": ":Company",
-   "depends_on": null,
-   "description": null,
    "fieldname": "cost_center",
    "fieldtype": "Link",
-   "hidden": null,
-   "in_filter": null,
    "in_list_view": 0,
    "label": "Cost Center",
-   "no_column": null,
-   "no_copy": null,
    "oldfieldname": "cost_center",
    "oldfieldtype": "Link",
    "options": "Cost Center",
    "permlevel": 0,
-   "print_hide": null,
-   "print_width": null,
-   "read_only": 0,
-   "report_hide": null,
-   "reqd": null,
-   "search_index": null,
-   "set_only_once": null,
-   "trigger": null,
-   "width": null
+   "read_only": 0
   },
   {
-   "allow_on_submit": null,
-   "default": null,
-   "depends_on": null,
-   "description": null,
    "fieldname": "rate",
    "fieldtype": "Float",
-   "hidden": null,
-   "in_filter": null,
    "in_list_view": 1,
    "label": "Rate",
-   "no_column": null,
-   "no_copy": null,
    "oldfieldname": "rate",
    "oldfieldtype": "Currency",
-   "options": null,
    "permlevel": 0,
-   "print_hide": null,
-   "print_width": null,
    "read_only": 0,
-   "report_hide": null,
-   "reqd": 0,
-   "search_index": null,
-   "set_only_once": null,
-   "trigger": null,
-   "width": null
+   "reqd": 0
   },
   {
-   "allow_on_submit": null,
-   "default": null,
-   "depends_on": null,
-   "description": null,
    "fieldname": "tax_amount",
    "fieldtype": "Currency",
-   "hidden": null,
-   "in_filter": null,
    "in_list_view": 1,
    "label": "Amount",
-   "no_column": null,
-   "no_copy": null,
    "oldfieldname": "tax_amount",
    "oldfieldtype": "Currency",
    "options": "Company:company:default_currency",
    "permlevel": 0,
-   "print_hide": null,
-   "print_width": null,
    "read_only": 1,
-   "report_hide": null,
-   "reqd": 0,
-   "search_index": null,
-   "set_only_once": null,
-   "trigger": null,
-   "width": null
+   "reqd": 0
   },
   {
-   "allow_on_submit": null,
-   "default": null,
-   "depends_on": null,
-   "description": null,
    "fieldname": "total",
    "fieldtype": "Currency",
-   "hidden": null,
-   "in_filter": null,
-   "in_list_view": null,
    "label": "Total",
-   "no_column": null,
-   "no_copy": null,
    "oldfieldname": "total",
    "oldfieldtype": "Currency",
    "options": "Company:company:default_currency",
    "permlevel": 0,
-   "print_hide": null,
-   "print_width": null,
-   "read_only": 1,
-   "report_hide": null,
-   "reqd": null,
-   "search_index": null,
-   "set_only_once": null,
-   "trigger": null,
-   "width": null
+   "read_only": 1
   },
   {
-   "allow_on_submit": null,
-   "default": null,
-   "depends_on": null,
-   "description": null,
    "fieldname": "item_wise_tax_detail",
    "fieldtype": "Small Text",
    "hidden": 1,
-   "in_filter": null,
-   "in_list_view": null,
    "label": "Item Wise Tax Detail ",
-   "no_column": null,
-   "no_copy": null,
    "oldfieldname": "item_wise_tax_detail",
    "oldfieldtype": "Small Text",
-   "options": null,
    "permlevel": 0,
    "print_hide": 1,
-   "print_width": null,
-   "read_only": 1,
-   "report_hide": null,
-   "reqd": null,
-   "search_index": null,
-   "set_only_once": null,
-   "trigger": null,
-   "width": null
+   "read_only": 1
   },
   {
-   "allow_on_submit": null,
-   "default": null,
-   "depends_on": null,
-   "description": null,
    "fieldname": "parenttype",
    "fieldtype": "Data",
    "hidden": 1,
    "in_filter": 1,
-   "in_list_view": null,
    "label": "Parenttype",
-   "no_column": null,
-   "no_copy": null,
    "oldfieldname": "parenttype",
    "oldfieldtype": "Data",
-   "options": null,
    "permlevel": 0,
    "print_hide": 1,
-   "print_width": null,
    "read_only": 0,
-   "report_hide": null,
-   "reqd": null,
-   "search_index": 0,
-   "set_only_once": null,
-   "trigger": null,
-   "width": null
+   "search_index": 0
   }
  ],
  "hide_heading": 1,
- "hide_toolbar": null,
- "icon": null,
  "idx": 1,
- "in_create": null,
- "in_dialog": null,
- "is_submittable": null,
- "is_transaction_doc": null,
- "issingle": null,
  "istable": 1,
- "max_attachments": null,
- "menu_index": null,
- "modified": "2014-04-15 09:48:45.892548",
+ "modified": "2014-05-30 03:43:32.494112",
  "modified_by": "Administrator",
  "module": "Accounts",
  "name": "Purchase Taxes and Charges",
- "name_case": null,
  "owner": "Administrator",
- "parent": null,
- "parent_node": null,
- "parentfield": null,
- "parenttype": null,
- "permissions": [],
- "plugin": null,
- "print_outline": null,
- "read_only": null,
- "read_only_onload": null,
- "search_fields": null,
- "server_code": null,
- "server_code_compiled": null,
- "server_code_core": null,
- "server_code_error": null,
- "show_in_menu": null,
- "smallicon": null,
- "subject": null,
- "tag_fields": null,
- "title_field": null,
- "use_template": null,
- "version": null
+ "permissions": []
 }
diff --git a/erpnext/accounts/doctype/sales_taxes_and_charges/sales_taxes_and_charges.json b/erpnext/accounts/doctype/sales_taxes_and_charges/sales_taxes_and_charges.json
index bba8f7e..7bde84f 100644
--- a/erpnext/accounts/doctype/sales_taxes_and_charges/sales_taxes_and_charges.json
+++ b/erpnext/accounts/doctype/sales_taxes_and_charges/sales_taxes_and_charges.json
@@ -1,417 +1,155 @@
 {
- "_last_update": null,
- "_user_tags": null,
- "allow_attach": null,
- "allow_copy": null,
- "allow_email": null,
- "allow_import": null,
- "allow_print": null,
- "allow_rename": null,
- "allow_trash": null,
  "autoname": "INVTD.######",
- "change_log": null,
- "client_script": null,
- "client_script_core": null,
- "client_string": null,
- "colour": null,
  "creation": "2013-04-24 11:39:32",
- "custom": null,
- "default_print_format": null,
- "description": null,
  "docstatus": 0,
  "doctype": "DocType",
- "document_type": null,
- "dt_template": null,
  "fields": [
   {
-   "allow_on_submit": null,
-   "default": null,
-   "depends_on": null,
-   "description": null,
    "fieldname": "charge_type",
    "fieldtype": "Select",
-   "hidden": null,
-   "in_filter": null,
    "in_list_view": 1,
    "label": "Type",
-   "no_column": null,
-   "no_copy": null,
    "oldfieldname": "charge_type",
    "oldfieldtype": "Select",
    "options": "\nActual\nOn Net Total\nOn Previous Row Amount\nOn Previous Row Total",
    "permlevel": 0,
-   "print_hide": null,
-   "print_width": null,
-   "read_only": null,
-   "report_hide": null,
-   "reqd": 1,
-   "search_index": null,
-   "set_only_once": null,
-   "trigger": null,
-   "width": null
+   "reqd": 1
   },
   {
-   "allow_on_submit": null,
-   "default": null,
    "depends_on": "eval:[\"On Previous Row Amount\", \"On Previous Row Total\"].indexOf(doc.charge_type)!==-1",
-   "description": null,
    "fieldname": "row_id",
    "fieldtype": "Data",
    "hidden": 0,
-   "in_filter": null,
-   "in_list_view": null,
    "label": "Reference Row #",
-   "no_column": null,
-   "no_copy": null,
    "oldfieldname": "row_id",
    "oldfieldtype": "Data",
-   "options": null,
-   "permlevel": 0,
-   "print_hide": null,
-   "print_width": null,
-   "read_only": null,
-   "report_hide": null,
-   "reqd": null,
-   "search_index": null,
-   "set_only_once": null,
-   "trigger": null,
-   "width": null
+   "permlevel": 0
   },
   {
-   "allow_on_submit": null,
-   "default": null,
-   "depends_on": null,
-   "description": null,
    "fieldname": "description",
    "fieldtype": "Small Text",
-   "hidden": null,
-   "in_filter": null,
    "in_list_view": 1,
    "label": "Description",
-   "no_column": null,
-   "no_copy": null,
    "oldfieldname": "description",
    "oldfieldtype": "Small Text",
-   "options": null,
    "permlevel": 0,
-   "print_hide": null,
    "print_width": "300px",
-   "read_only": null,
-   "report_hide": null,
    "reqd": 1,
-   "search_index": null,
-   "set_only_once": null,
-   "trigger": null,
    "width": "300px"
   },
   {
-   "allow_on_submit": null,
-   "default": null,
-   "depends_on": null,
-   "description": null,
    "fieldname": "col_break_1",
    "fieldtype": "Column Break",
-   "hidden": null,
-   "in_filter": null,
-   "in_list_view": null,
-   "label": null,
-   "no_column": null,
-   "no_copy": null,
-   "oldfieldname": null,
-   "oldfieldtype": null,
-   "options": null,
    "permlevel": 0,
-   "print_hide": null,
-   "print_width": null,
-   "read_only": null,
-   "report_hide": null,
-   "reqd": null,
-   "search_index": null,
-   "set_only_once": null,
-   "trigger": null,
    "width": "50%"
   },
   {
-   "allow_on_submit": null,
-   "default": null,
-   "depends_on": null,
-   "description": null,
    "fieldname": "account_head",
    "fieldtype": "Link",
-   "hidden": null,
-   "in_filter": null,
    "in_list_view": 0,
    "label": "Account Head",
-   "no_column": null,
-   "no_copy": null,
    "oldfieldname": "account_head",
    "oldfieldtype": "Link",
    "options": "Account",
    "permlevel": 0,
-   "print_hide": null,
-   "print_width": null,
-   "read_only": null,
-   "report_hide": null,
    "reqd": 1,
-   "search_index": 1,
-   "set_only_once": null,
-   "trigger": null,
-   "width": null
+   "search_index": 1
   },
   {
-   "allow_on_submit": null,
    "default": ":Company",
-   "depends_on": null,
-   "description": null,
    "fieldname": "cost_center",
    "fieldtype": "Link",
-   "hidden": null,
-   "in_filter": null,
    "in_list_view": 0,
    "label": "Cost Center",
-   "no_column": null,
-   "no_copy": null,
    "oldfieldname": "cost_center_other_charges",
    "oldfieldtype": "Link",
    "options": "Cost Center",
-   "permlevel": 0,
-   "print_hide": null,
-   "print_width": null,
-   "read_only": null,
-   "report_hide": null,
-   "reqd": null,
-   "search_index": null,
-   "set_only_once": null,
-   "trigger": null,
-   "width": null
+   "permlevel": 0
   },
   {
-   "allow_on_submit": null,
-   "default": null,
-   "depends_on": null,
-   "description": null,
    "fieldname": "rate",
    "fieldtype": "Float",
-   "hidden": null,
-   "in_filter": null,
    "in_list_view": 1,
    "label": "Rate",
-   "no_column": null,
-   "no_copy": null,
    "oldfieldname": "rate",
    "oldfieldtype": "Currency",
-   "options": null,
    "permlevel": 0,
-   "print_hide": null,
-   "print_width": null,
-   "read_only": null,
-   "report_hide": null,
-   "reqd": 1,
-   "search_index": null,
-   "set_only_once": null,
-   "trigger": null,
-   "width": null
+   "reqd": 1
   },
   {
-   "allow_on_submit": null,
-   "default": null,
-   "depends_on": null,
-   "description": null,
    "fieldname": "tax_amount",
    "fieldtype": "Currency",
-   "hidden": null,
-   "in_filter": null,
    "in_list_view": 1,
    "label": "Amount",
-   "no_column": null,
-   "no_copy": null,
    "oldfieldname": "tax_amount",
    "oldfieldtype": "Currency",
    "options": "Company:company:default_currency",
    "permlevel": 0,
-   "print_hide": null,
-   "print_width": null,
    "read_only": 1,
-   "report_hide": null,
-   "reqd": 0,
-   "search_index": null,
-   "set_only_once": null,
-   "trigger": null,
-   "width": null
+   "reqd": 0
   },
   {
-   "allow_on_submit": null,
-   "default": null,
-   "depends_on": null,
-   "description": null,
    "fieldname": "total",
    "fieldtype": "Currency",
-   "hidden": null,
-   "in_filter": null,
-   "in_list_view": null,
    "label": "Total",
-   "no_column": null,
-   "no_copy": null,
    "oldfieldname": "total",
    "oldfieldtype": "Currency",
    "options": "Company:company:default_currency",
    "permlevel": 0,
-   "print_hide": null,
-   "print_width": null,
-   "read_only": 1,
-   "report_hide": null,
-   "reqd": null,
-   "search_index": null,
-   "set_only_once": null,
-   "trigger": null,
-   "width": null
+   "read_only": 1
   },
   {
    "allow_on_submit": 0,
-   "default": null,
-   "depends_on": null,
    "description": "If checked, the tax amount will be considered as already included in the Print Rate / Print Amount",
    "fieldname": "included_in_print_rate",
    "fieldtype": "Check",
-   "hidden": null,
-   "in_filter": null,
-   "in_list_view": null,
    "label": "Is this Tax included in Basic Rate?",
-   "no_column": null,
    "no_copy": 0,
-   "oldfieldname": null,
-   "oldfieldtype": null,
-   "options": null,
    "permlevel": 0,
    "print_hide": 1,
    "print_width": "150px",
-   "read_only": null,
    "report_hide": 1,
-   "reqd": null,
-   "search_index": null,
-   "set_only_once": null,
-   "trigger": null,
    "width": "150px"
   },
   {
-   "allow_on_submit": null,
-   "default": null,
-   "depends_on": null,
-   "description": null,
    "fieldname": "tax_amount_after_discount_amount",
    "fieldtype": "Currency",
    "hidden": 1,
-   "in_filter": null,
-   "in_list_view": null,
    "label": "Tax Amount After Discount Amount",
-   "no_column": null,
-   "no_copy": null,
-   "oldfieldname": null,
-   "oldfieldtype": null,
    "options": "Company:company:default_currency",
    "permlevel": 0,
-   "print_hide": null,
-   "print_width": null,
-   "read_only": 1,
-   "report_hide": null,
-   "reqd": null,
-   "search_index": null,
-   "set_only_once": null,
-   "trigger": null,
-   "width": null
+   "read_only": 1
   },
   {
-   "allow_on_submit": null,
-   "default": null,
-   "depends_on": null,
-   "description": null,
    "fieldname": "item_wise_tax_detail",
    "fieldtype": "Small Text",
    "hidden": 1,
-   "in_filter": null,
-   "in_list_view": null,
    "label": "Item Wise Tax Detail",
-   "no_column": null,
-   "no_copy": null,
    "oldfieldname": "item_wise_tax_detail",
    "oldfieldtype": "Small Text",
-   "options": null,
    "permlevel": 0,
-   "print_hide": null,
-   "print_width": null,
-   "read_only": 1,
-   "report_hide": null,
-   "reqd": null,
-   "search_index": null,
-   "set_only_once": null,
-   "trigger": null,
-   "width": null
+   "read_only": 1
   },
   {
-   "allow_on_submit": null,
-   "default": null,
-   "depends_on": null,
-   "description": null,
    "fieldname": "parenttype",
    "fieldtype": "Data",
    "hidden": 1,
    "in_filter": 1,
-   "in_list_view": null,
    "label": "Parenttype",
-   "no_column": null,
-   "no_copy": null,
    "oldfieldname": "parenttype",
    "oldfieldtype": "Data",
-   "options": null,
    "permlevel": 0,
    "print_hide": 1,
-   "print_width": null,
-   "read_only": null,
-   "report_hide": null,
-   "reqd": null,
-   "search_index": 1,
-   "set_only_once": null,
-   "trigger": null,
-   "width": null
+   "search_index": 1
   }
  ],
  "hide_heading": 1,
- "hide_toolbar": null,
- "icon": null,
  "idx": 1,
- "in_create": null,
- "in_dialog": null,
- "is_submittable": null,
- "is_transaction_doc": null,
- "issingle": null,
  "istable": 1,
- "max_attachments": null,
- "menu_index": null,
- "modified": "2014-04-14 18:40:48.450796",
+ "modified": "2014-05-30 03:43:39.740638",
  "modified_by": "Administrator",
  "module": "Accounts",
  "name": "Sales Taxes and Charges",
- "name_case": null,
  "owner": "Administrator",
- "parent": null,
- "parent_node": null,
- "parentfield": null,
- "parenttype": null,
- "permissions": [],
- "plugin": null,
- "print_outline": null,
- "read_only": null,
- "read_only_onload": null,
- "search_fields": null,
- "server_code": null,
- "server_code_compiled": null,
- "server_code_core": null,
- "server_code_error": null,
- "show_in_menu": null,
- "smallicon": null,
- "subject": null,
- "tag_fields": null,
- "title_field": null,
- "use_template": null,
- "version": null
+ "permissions": []
 }
diff --git a/erpnext/hr/doctype/employee/employee.py b/erpnext/hr/doctype/employee/employee.py
index b58a300..4e0c268 100644
--- a/erpnext/hr/doctype/employee/employee.py
+++ b/erpnext/hr/doctype/employee/employee.py
@@ -11,6 +11,8 @@
 from frappe.model.document import Document
 from frappe.model.mapper import get_mapped_doc
 
+class EmployeeUserDisabledError(frappe.ValidationError): pass
+
 class Employee(Document):
 	def onload(self):
 		self.get("__onload").salary_structure_exists = frappe.db.get_value("Salary Structure",
@@ -133,7 +135,7 @@
 		enabled = frappe.db.sql("""select name from `tabUser` where
 			name=%s and enabled=1""", self.user_id)
 		if not enabled:
-			throw(_("User {0} is disabled").format(self.user_id))
+			throw(_("User {0} is disabled").format(self.user_id), EmployeeUserDisabledError)
 
 	def validate_duplicate_user_id(self):
 		employee = frappe.db.sql_list("""select name from `tabEmployee` where
diff --git a/erpnext/patches/v4_0/apply_user_permissions.py b/erpnext/patches/v4_0/apply_user_permissions.py
index 7f5b951..e32e3e1 100644
--- a/erpnext/patches/v4_0/apply_user_permissions.py
+++ b/erpnext/patches/v4_0/apply_user_permissions.py
@@ -3,6 +3,7 @@
 
 from __future__ import unicode_literals
 import frappe
+from erpnext.hr.doctype.employee.employee import EmployeeUserDisabledError
 
 def execute():
 	update_hr_permissions()
@@ -25,7 +26,10 @@
 
 	# save employees to run on_update events
 	for employee in frappe.db.sql_list("""select name from `tabEmployee`"""):
-		frappe.get_doc("Employee", employee).save()
+		try:
+			frappe.get_doc("Employee", employee).save()
+		except EmployeeUserDisabledError:
+			pass
 
 def update_permissions():
 	# clear match conditions other than owner
diff --git a/erpnext/patches/v4_0/customer_discount_to_pricing_rule.py b/erpnext/patches/v4_0/customer_discount_to_pricing_rule.py
index 87aa792..5f9fc23 100644
--- a/erpnext/patches/v4_0/customer_discount_to_pricing_rule.py
+++ b/erpnext/patches/v4_0/customer_discount_to_pricing_rule.py
@@ -3,30 +3,27 @@
 
 from __future__ import unicode_literals
 import frappe
+from frappe.utils.nestedset import get_root_of
 
 def execute():
 	frappe.reload_doc("accounts", "doctype", "pricing_rule")
-	
+
 	frappe.db.auto_commit_on_many_writes = True
-	
-	for d in frappe.db.sql("""select * from `tabCustomer Discount` 
-		where ifnull(parent, '') != '' and docstatus < 2""", as_dict=1):
-			if not d.item_group:
-				item_group = frappe.db.sql("""select name from `tabItem Group` 
-					where ifnull(parent_item_group, '') = ''""")[0][0]
-			else:
-				item_group = d.item_group
-				
+
+	default_item_group = get_root_of("Item Group")
+
+	for d in frappe.db.sql("""select * from `tabCustomer Discount`
+		where ifnull(parent, '') != ''""", as_dict=1):
 			frappe.get_doc({
 				"doctype": "Pricing Rule",
 				"apply_on": "Item Group",
-				"item_group": item_group,
+				"item_group": d.item_group or default_item_group,
 				"applicable_for": "Customer",
 				"customer": d.parent,
 				"price_or_discount": "Discount Percentage",
 				"discount_percentage": d.discount
 			}).insert()
-	
-	frappe.db.auto_commit_on_many_writes = False		
-	
+
+	frappe.db.auto_commit_on_many_writes = False
+
 	frappe.delete_doc("DocType", "Customer Discount")
diff --git a/erpnext/patches/v4_0/fix_employee_user_id.py b/erpnext/patches/v4_0/fix_employee_user_id.py
index 71107f8..d366fa4 100644
--- a/erpnext/patches/v4_0/fix_employee_user_id.py
+++ b/erpnext/patches/v4_0/fix_employee_user_id.py
@@ -1,3 +1,8 @@
+# Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors
+# License: GNU General Public License v3. See license.txt
+
+from __future__ import unicode_literals
+
 import frappe
 from frappe.utils import get_fullname
 
@@ -5,9 +10,14 @@
 	for user_id in frappe.db.sql_list("""select distinct user_id from `tabEmployee`
 		where ifnull(user_id, '')!=''
 		group by user_id having count(name) > 1"""):
-		
+
 		fullname = get_fullname(user_id)
 		employee = frappe.db.get_value("Employee", {"employee_name": fullname, "user_id": user_id})
+
 		if employee:
 			frappe.db.sql("""update `tabEmployee` set user_id=null
-				where user_id=%s and name!=%s""", (user_id, employee))
\ No newline at end of file
+				where user_id=%s and name!=%s""", (user_id, employee))
+		else:
+			count = frappe.db.sql("""select count(*) from `tabEmployee` where user_id=%s""", user_id)[0][0]
+			frappe.db.sql("""update `tabEmployee` set user_id=null
+				where user_id=%s limit %s""", (user_id, count - 1))
diff --git a/erpnext/patches/v4_0/split_email_settings.py b/erpnext/patches/v4_0/split_email_settings.py
index 630e954..05d9bc5 100644
--- a/erpnext/patches/v4_0/split_email_settings.py
+++ b/erpnext/patches/v4_0/split_email_settings.py
@@ -7,15 +7,16 @@
 def execute():
 	frappe.reload_doc("core", "doctype", "outgoing_email_settings")
 	frappe.reload_doc("support", "doctype", "support_email_settings")
-	
+
 	email_settings = get_email_settings()
 	map_outgoing_email_settings(email_settings)
 	map_support_email_settings(email_settings)
-	frappe.delete_doc("Doctype", "Email Settings")
-	
+
+	frappe.delete_doc("DocType", "Email Settings")
+
 def map_outgoing_email_settings(email_settings):
 	outgoing_email_settings = frappe.get_doc("Outgoing Email Settings")
-	for fieldname in (("outgoing_mail_server", "mail_server"), 
+	for fieldname in (("outgoing_mail_server", "mail_server"),
 		"use_ssl", "mail_port", "mail_login", "mail_password",
 		"always_use_login_id_as_sender",
 		"auto_email_id", "send_print_in_body_and_attachment"):
@@ -28,26 +29,26 @@
 		outgoing_email_settings.set(to_fieldname, email_settings.get(from_fieldname))
 
 	outgoing_email_settings.save()
-	
+
 def map_support_email_settings(email_settings):
 	support_email_settings = frappe.get_doc("Support Email Settings")
-	
-	for fieldname in ("sync_support_mails", "support_email", 
-		("support_host", "mail_server"), 
-		("support_use_ssl", "use_ssl"), 
-		("support_username", "mail_login"), 
-		("support_password", "mail_password"), 
+
+	for fieldname in ("sync_support_mails", "support_email",
+		("support_host", "mail_server"),
+		("support_use_ssl", "use_ssl"),
+		("support_username", "mail_login"),
+		("support_password", "mail_password"),
 		"support_signature", "send_autoreply", "support_autoreply"):
-		
+
 		if isinstance(fieldname, tuple):
 			from_fieldname, to_fieldname = fieldname
 		else:
 			from_fieldname = to_fieldname = fieldname
-	
+
 		support_email_settings.set(to_fieldname, email_settings.get(from_fieldname))
-	
+
 	support_email_settings.save()
-	
+
 def get_email_settings():
 	ret = {}
 	for field, value in frappe.db.sql("select field, value from tabSingles where doctype='Email Settings'"):