Merge pull request #17431 from nabinhait/deferred-accounting-long-job-dev

fix: moved deferred accounting monthly job to long job
diff --git a/erpnext/accounts/doctype/payment_entry_reference/payment_entry_reference.json b/erpnext/accounts/doctype/payment_entry_reference/payment_entry_reference.json
index f155399..a752c6f 100644
--- a/erpnext/accounts/doctype/payment_entry_reference/payment_entry_reference.json
+++ b/erpnext/accounts/doctype/payment_entry_reference/payment_entry_reference.json
@@ -20,6 +20,7 @@
    "bold": 0, 
    "collapsible": 0, 
    "columns": 2, 
+   "fetch_if_empty": 0, 
    "fieldname": "reference_doctype", 
    "fieldtype": "Link", 
    "hidden": 0, 
@@ -53,6 +54,7 @@
    "bold": 0, 
    "collapsible": 0, 
    "columns": 2, 
+   "fetch_if_empty": 0, 
    "fieldname": "reference_name", 
    "fieldtype": "Dynamic Link", 
    "hidden": 0, 
@@ -86,6 +88,8 @@
    "bold": 0, 
    "collapsible": 0, 
    "columns": 0, 
+   "fetch_from": "reference_name.due_date", 
+   "fetch_if_empty": 0, 
    "fieldname": "due_date", 
    "fieldtype": "Date", 
    "hidden": 0, 
@@ -119,6 +123,7 @@
    "collapsible": 0, 
    "columns": 0, 
    "depends_on": "", 
+   "fetch_if_empty": 0, 
    "fieldname": "bill_no", 
    "fieldtype": "Data", 
    "hidden": 0, 
@@ -151,6 +156,7 @@
    "bold": 0, 
    "collapsible": 0, 
    "columns": 0, 
+   "fetch_if_empty": 0, 
    "fieldname": "column_break_4", 
    "fieldtype": "Column Break", 
    "hidden": 0, 
@@ -182,6 +188,7 @@
    "bold": 0, 
    "collapsible": 0, 
    "columns": 2, 
+   "fetch_if_empty": 0, 
    "fieldname": "total_amount", 
    "fieldtype": "Float", 
    "hidden": 0, 
@@ -214,6 +221,7 @@
    "bold": 0, 
    "collapsible": 0, 
    "columns": 2, 
+   "fetch_if_empty": 0, 
    "fieldname": "outstanding_amount", 
    "fieldtype": "Float", 
    "hidden": 0, 
@@ -246,6 +254,7 @@
    "bold": 0, 
    "collapsible": 0, 
    "columns": 2, 
+   "fetch_if_empty": 0, 
    "fieldname": "allocated_amount", 
    "fieldtype": "Float", 
    "hidden": 0, 
@@ -279,6 +288,8 @@
    "collapsible": 0, 
    "columns": 0, 
    "depends_on": "eval:(doc.reference_doctype=='Purchase Invoice')", 
+   "fetch_from": "reference_name.conversion_rate", 
+   "fetch_if_empty": 0, 
    "fieldname": "exchange_rate", 
    "fieldtype": "Float", 
    "hidden": 0, 
@@ -315,7 +326,7 @@
  "issingle": 0, 
  "istable": 1, 
  "max_attachments": 0, 
- "modified": "2019-01-07 16:52:06.884796", 
+ "modified": "2019-05-01 13:24:56.586677", 
  "modified_by": "Administrator", 
  "module": "Accounts", 
  "name": "Payment Entry Reference", 
diff --git a/erpnext/assets/doctype/asset_value_adjustment/asset_value_adjustment.py b/erpnext/assets/doctype/asset_value_adjustment/asset_value_adjustment.py
index ac3c350..56425a0 100644
--- a/erpnext/assets/doctype/asset_value_adjustment/asset_value_adjustment.py
+++ b/erpnext/assets/doctype/asset_value_adjustment/asset_value_adjustment.py
@@ -36,7 +36,7 @@
 		fixed_asset_account, accumulated_depreciation_account, depreciation_expense_account = \
 			get_depreciation_accounts(asset)
 
-		depreciation_cost_center, depreciation_series = frappe.get_cached_value('Company',  asset.company, 
+		depreciation_cost_center, depreciation_series = frappe.get_cached_value('Company',  asset.company,
 			["depreciation_cost_center", "series_for_depreciation_entry"])
 
 		je = frappe.new_doc("Journal Entry")
@@ -75,8 +75,8 @@
 				rate_per_day = flt(d.value_after_depreciation) / flt(total_days)
 				from_date = self.date
 			else:
-				no_of_depreciations = len([e.name for e in asset.schedules
-					if (cint(s.finance_book_id) == d.idx and not e.journal_entry)])
+				no_of_depreciations = len([s.name for s in asset.schedules
+					if (cint(s.finance_book_id) == d.idx and not s.journal_entry)])
 
 			value_after_depreciation = d.value_after_depreciation
 			for data in asset.schedules:
diff --git a/erpnext/controllers/taxes_and_totals.py b/erpnext/controllers/taxes_and_totals.py
index b3880be..b75b8b8 100644
--- a/erpnext/controllers/taxes_and_totals.py
+++ b/erpnext/controllers/taxes_and_totals.py
@@ -59,19 +59,21 @@
 
 				if item.discount_percentage == 100:
 					item.rate = 0.0
-				elif (not item.rate or item.discount_percentage > 0) and item.price_list_rate:
-					item.rate = flt(item.price_list_rate *
-						(1.0 - (item.discount_percentage / 100.0)), item.precision("rate"))
-					item.discount_amount = item.price_list_rate * (item.discount_percentage / 100.0)
-				elif item.discount_amount and item.price_list_rate:
-					item.rate =  item.price_list_rate - item.discount_amount
+				elif item.price_list_rate:
+					if not item.rate or (item.pricing_rules and item.discount_percentage > 0):
+						item.rate = flt(item.price_list_rate *
+							(1.0 - (item.discount_percentage / 100.0)), item.precision("rate"))
+						item.discount_amount = item.price_list_rate * (item.discount_percentage / 100.0)
+					elif item.discount_amount and item.pricing_rules:
+						item.rate =  item.price_list_rate - item.discount_amount
 
 				if item.doctype in ['Quotation Item', 'Sales Order Item', 'Delivery Note Item', 'Sales Invoice Item']:
 					item.rate_with_margin, item.base_rate_with_margin = self.calculate_margin(item)
-
 					if flt(item.rate_with_margin) > 0:
 						item.rate = flt(item.rate_with_margin * (1.0 - (item.discount_percentage / 100.0)), item.precision("rate"))
 						item.discount_amount = item.rate_with_margin - item.rate
+					elif flt(item.price_list_rate) > 0:
+						item.discount_amount = item.price_list_rate - item.rate
 				elif flt(item.price_list_rate) > 0 and not item.discount_amount:
 					item.discount_amount = item.price_list_rate - item.rate
 
diff --git a/erpnext/hr/doctype/employee_benefit_application/employee_benefit_application.py b/erpnext/hr/doctype/employee_benefit_application/employee_benefit_application.py
index c613a13..701ae24 100644
--- a/erpnext/hr/doctype/employee_benefit_application/employee_benefit_application.py
+++ b/erpnext/hr/doctype/employee_benefit_application/employee_benefit_application.py
@@ -177,9 +177,12 @@
 
 	# Considering there is only one application for a year
 	benefit_application_name = frappe.db.sql("""
-	select name from `tabEmployee Benefit Application`
-	where payroll_period=%(payroll_period)s and employee=%(employee)s
-	and docstatus = 1
+		select name
+		from `tabEmployee Benefit Application`
+		where
+			payroll_period=%(payroll_period)s
+			and employee=%(employee)s
+			and docstatus = 1
 	""", {
 		'employee': employee,
 		'payroll_period': payroll_period
@@ -209,7 +212,8 @@
 	total_pro_rata_max = 0
 	benefit_amount = 0
 	for sal_struct_row in sal_struct.get("earnings"):
-		pay_against_benefit_claim, max_benefit_amount = frappe.db.get_value("Salary Component", sal_struct_row.salary_component, ["pay_against_benefit_claim", "max_benefit_amount"])
+		pay_against_benefit_claim, max_benefit_amount = frappe.db.get_value("Salary Component",
+			sal_struct_row.salary_component, ["pay_against_benefit_claim", "max_benefit_amount"])
 		if sal_struct_row.is_flexible_benefit == 1 and pay_against_benefit_claim != 1:
 			total_pro_rata_max += max_benefit_amount
 	if total_pro_rata_max > 0:
diff --git a/erpnext/hr/doctype/employee_tax_exemption_declaration_category/employee_tax_exemption_declaration_category.json b/erpnext/hr/doctype/employee_tax_exemption_declaration_category/employee_tax_exemption_declaration_category.json
index 56556c1..7b3b8f5 100644
--- a/erpnext/hr/doctype/employee_tax_exemption_declaration_category/employee_tax_exemption_declaration_category.json
+++ b/erpnext/hr/doctype/employee_tax_exemption_declaration_category/employee_tax_exemption_declaration_category.json
@@ -1,179 +1,179 @@
 {
- "allow_copy": 0,
- "allow_events_in_timeline": 0,
- "allow_guest_to_view": 0,
- "allow_import": 0,
- "allow_rename": 0,
- "beta": 0,
- "creation": "2018-04-13 16:56:23.333041",
- "custom": 0,
- "docstatus": 0,
- "doctype": "DocType",
- "document_type": "",
- "editable_grid": 1,
- "engine": "InnoDB",
+ "allow_copy": 0, 
+ "allow_events_in_timeline": 0, 
+ "allow_guest_to_view": 0, 
+ "allow_import": 0, 
+ "allow_rename": 0, 
+ "beta": 0, 
+ "creation": "2018-04-13 16:56:23.333041", 
+ "custom": 0, 
+ "docstatus": 0, 
+ "doctype": "DocType", 
+ "document_type": "", 
+ "editable_grid": 1, 
+ "engine": "InnoDB", 
  "fields": [
   {
-   "allow_bulk_edit": 0,
-   "allow_in_quick_entry": 0,
-   "allow_on_submit": 0,
-   "bold": 0,
-   "collapsible": 0,
-   "columns": 0,
-   "fetch_if_empty": 0,
-   "fieldname": "exemption_sub_category",
-   "fieldtype": "Link",
-   "hidden": 0,
-   "ignore_user_permissions": 0,
-   "ignore_xss_filter": 0,
-   "in_filter": 0,
-   "in_global_search": 0,
-   "in_list_view": 1,
-   "in_standard_filter": 0,
-   "label": "Exemption Sub Category",
-   "length": 0,
-   "no_copy": 0,
-   "options": "Employee Tax Exemption Sub Category",
-   "permlevel": 0,
-   "precision": "",
-   "print_hide": 0,
-   "print_hide_if_no_value": 0,
-   "read_only": 0,
-   "remember_last_selected_value": 0,
-   "report_hide": 0,
-   "reqd": 1,
-   "search_index": 0,
-   "set_only_once": 0,
-   "translatable": 0,
+   "allow_bulk_edit": 0, 
+   "allow_in_quick_entry": 0, 
+   "allow_on_submit": 0, 
+   "bold": 0, 
+   "collapsible": 0, 
+   "columns": 0, 
+   "fetch_if_empty": 0, 
+   "fieldname": "exemption_sub_category", 
+   "fieldtype": "Link", 
+   "hidden": 0, 
+   "ignore_user_permissions": 0, 
+   "ignore_xss_filter": 0, 
+   "in_filter": 0, 
+   "in_global_search": 0, 
+   "in_list_view": 1, 
+   "in_standard_filter": 0, 
+   "label": "Exemption Sub Category", 
+   "length": 0, 
+   "no_copy": 0, 
+   "options": "Employee Tax Exemption Sub Category", 
+   "permlevel": 0, 
+   "precision": "", 
+   "print_hide": 0, 
+   "print_hide_if_no_value": 0, 
+   "read_only": 0, 
+   "remember_last_selected_value": 0, 
+   "report_hide": 0, 
+   "reqd": 1, 
+   "search_index": 0, 
+   "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0
-  },
+  }, 
   {
-   "allow_bulk_edit": 0,
-   "allow_in_quick_entry": 0,
-   "allow_on_submit": 0,
-   "bold": 0,
-   "collapsible": 0,
-   "columns": 0,
-   "fetch_from": "exemption_sub_category.exemption_category",
-   "fetch_if_empty": 0,
-   "fieldname": "exemption_category",
-   "fieldtype": "Link",
-   "hidden": 0,
-   "ignore_user_permissions": 0,
-   "ignore_xss_filter": 0,
-   "in_filter": 0,
-   "in_global_search": 0,
-   "in_list_view": 1,
-   "in_standard_filter": 0,
-   "label": "Exemption Category",
-   "length": 0,
-   "no_copy": 0,
-   "options": "Employee Tax Exemption Category",
-   "permlevel": 0,
-   "precision": "",
-   "print_hide": 0,
-   "print_hide_if_no_value": 0,
-   "read_only": 1,
-   "remember_last_selected_value": 0,
-   "report_hide": 0,
-   "reqd": 1,
-   "search_index": 0,
-   "set_only_once": 0,
-   "translatable": 0,
+   "allow_bulk_edit": 0, 
+   "allow_in_quick_entry": 0, 
+   "allow_on_submit": 0, 
+   "bold": 0, 
+   "collapsible": 0, 
+   "columns": 0, 
+   "fetch_from": "exemption_sub_category.exemption_category", 
+   "fetch_if_empty": 0, 
+   "fieldname": "exemption_category", 
+   "fieldtype": "Link", 
+   "hidden": 0, 
+   "ignore_user_permissions": 0, 
+   "ignore_xss_filter": 0, 
+   "in_filter": 0, 
+   "in_global_search": 0, 
+   "in_list_view": 1, 
+   "in_standard_filter": 0, 
+   "label": "Exemption Category", 
+   "length": 0, 
+   "no_copy": 0, 
+   "options": "Employee Tax Exemption Category", 
+   "permlevel": 0, 
+   "precision": "", 
+   "print_hide": 0, 
+   "print_hide_if_no_value": 0, 
+   "read_only": 1, 
+   "remember_last_selected_value": 0, 
+   "report_hide": 0, 
+   "reqd": 1, 
+   "search_index": 0, 
+   "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0
-  },
+  }, 
   {
-   "allow_bulk_edit": 0,
-   "allow_in_quick_entry": 0,
-   "allow_on_submit": 0,
-   "bold": 0,
-   "collapsible": 0,
-   "columns": 0,
-   "fetch_from": "exemption_sub_category.max_amount",
-   "fetch_if_empty": 0,
-   "fieldname": "max_amount",
-   "fieldtype": "Currency",
-   "hidden": 0,
-   "ignore_user_permissions": 0,
-   "ignore_xss_filter": 0,
-   "in_filter": 0,
-   "in_global_search": 0,
-   "in_list_view": 1,
-   "in_standard_filter": 0,
-   "label": "Maximum Exemption Amount",
-   "length": 0,
-   "no_copy": 0,
-   "permlevel": 0,
-   "precision": "",
-   "print_hide": 0,
-   "print_hide_if_no_value": 0,
-   "read_only": 1,
-   "remember_last_selected_value": 0,
-   "report_hide": 0,
-   "reqd": 1,
-   "search_index": 0,
-   "set_only_once": 0,
-   "translatable": 0,
+   "allow_bulk_edit": 0, 
+   "allow_in_quick_entry": 0, 
+   "allow_on_submit": 0, 
+   "bold": 0, 
+   "collapsible": 0, 
+   "columns": 0, 
+   "fetch_from": "exemption_sub_category.max_amount", 
+   "fetch_if_empty": 0, 
+   "fieldname": "max_amount", 
+   "fieldtype": "Currency", 
+   "hidden": 0, 
+   "ignore_user_permissions": 0, 
+   "ignore_xss_filter": 0, 
+   "in_filter": 0, 
+   "in_global_search": 0, 
+   "in_list_view": 1, 
+   "in_standard_filter": 0, 
+   "label": "Maximum Exempted Amount", 
+   "length": 0, 
+   "no_copy": 0, 
+   "permlevel": 0, 
+   "precision": "", 
+   "print_hide": 0, 
+   "print_hide_if_no_value": 0, 
+   "read_only": 1, 
+   "remember_last_selected_value": 0, 
+   "report_hide": 0, 
+   "reqd": 1, 
+   "search_index": 0, 
+   "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0
-  },
+  }, 
   {
-   "allow_bulk_edit": 0,
-   "allow_in_quick_entry": 0,
-   "allow_on_submit": 0,
-   "bold": 0,
-   "collapsible": 0,
-   "columns": 0,
-   "fetch_if_empty": 0,
-   "fieldname": "amount",
-   "fieldtype": "Data",
-   "hidden": 0,
-   "ignore_user_permissions": 0,
-   "ignore_xss_filter": 0,
-   "in_filter": 0,
-   "in_global_search": 0,
-   "in_list_view": 1,
-   "in_standard_filter": 0,
-   "label": "Declared Amount",
-   "length": 0,
-   "no_copy": 0,
-   "permlevel": 0,
-   "precision": "",
-   "print_hide": 0,
-   "print_hide_if_no_value": 0,
-   "read_only": 0,
-   "remember_last_selected_value": 0,
-   "report_hide": 0,
-   "reqd": 1,
-   "search_index": 0,
-   "set_only_once": 0,
-   "translatable": 0,
+   "allow_bulk_edit": 0, 
+   "allow_in_quick_entry": 0, 
+   "allow_on_submit": 0, 
+   "bold": 0, 
+   "collapsible": 0, 
+   "columns": 0, 
+   "fetch_if_empty": 0, 
+   "fieldname": "amount", 
+   "fieldtype": "Currency", 
+   "hidden": 0, 
+   "ignore_user_permissions": 0, 
+   "ignore_xss_filter": 0, 
+   "in_filter": 0, 
+   "in_global_search": 0, 
+   "in_list_view": 1, 
+   "in_standard_filter": 0, 
+   "label": "Declared Amount", 
+   "length": 0, 
+   "no_copy": 0, 
+   "permlevel": 0, 
+   "precision": "", 
+   "print_hide": 0, 
+   "print_hide_if_no_value": 0, 
+   "read_only": 0, 
+   "remember_last_selected_value": 0, 
+   "report_hide": 0, 
+   "reqd": 1, 
+   "search_index": 0, 
+   "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0
   }
- ],
- "has_web_view": 0,
- "hide_heading": 0,
- "hide_toolbar": 0,
- "idx": 0,
- "image_view": 0,
- "in_create": 0,
- "is_submittable": 0,
- "issingle": 0,
- "istable": 1,
- "max_attachments": 0,
- "modified": "2019-04-25 15:45:11.279158",
- "modified_by": "Administrator",
- "module": "HR",
- "name": "Employee Tax Exemption Declaration Category",
- "name_case": "",
- "owner": "Administrator",
- "permissions": [],
- "quick_entry": 1,
- "read_only": 0,
- "read_only_onload": 0,
- "show_name_in_global_search": 0,
- "sort_field": "modified",
- "sort_order": "DESC",
- "track_changes": 1,
- "track_seen": 0,
+ ], 
+ "has_web_view": 0, 
+ "hide_heading": 0, 
+ "hide_toolbar": 0, 
+ "idx": 0, 
+ "image_view": 0, 
+ "in_create": 0, 
+ "is_submittable": 0, 
+ "issingle": 0, 
+ "istable": 1, 
+ "max_attachments": 0, 
+ "modified": "2019-04-26 11:28:14.023086", 
+ "modified_by": "Administrator", 
+ "module": "HR", 
+ "name": "Employee Tax Exemption Declaration Category", 
+ "name_case": "", 
+ "owner": "Administrator", 
+ "permissions": [], 
+ "quick_entry": 1, 
+ "read_only": 0, 
+ "read_only_onload": 0, 
+ "show_name_in_global_search": 0, 
+ "sort_field": "modified", 
+ "sort_order": "DESC", 
+ "track_changes": 1, 
+ "track_seen": 0, 
  "track_views": 0
 }
\ No newline at end of file
diff --git a/erpnext/hr/doctype/payroll_period/payroll_period.json b/erpnext/hr/doctype/payroll_period/payroll_period.json
index d3a9612..c9bac09 100644
--- a/erpnext/hr/doctype/payroll_period/payroll_period.json
+++ b/erpnext/hr/doctype/payroll_period/payroll_period.json
@@ -1,5 +1,6 @@
 {
  "allow_copy": 0, 
+ "allow_events_in_timeline": 0, 
  "allow_guest_to_view": 0, 
  "allow_import": 1, 
  "allow_rename": 0, 
@@ -20,6 +21,7 @@
    "bold": 0, 
    "collapsible": 0, 
    "columns": 0, 
+   "fetch_if_empty": 0, 
    "fieldname": "company", 
    "fieldtype": "Link", 
    "hidden": 0, 
@@ -53,6 +55,7 @@
    "bold": 0, 
    "collapsible": 0, 
    "columns": 0, 
+   "fetch_if_empty": 0, 
    "fieldname": "column_break_2", 
    "fieldtype": "Column Break", 
    "hidden": 0, 
@@ -84,6 +87,7 @@
    "bold": 0, 
    "collapsible": 0, 
    "columns": 0, 
+   "fetch_if_empty": 0, 
    "fieldname": "start_date", 
    "fieldtype": "Date", 
    "hidden": 0, 
@@ -116,6 +120,7 @@
    "bold": 0, 
    "collapsible": 0, 
    "columns": 0, 
+   "fetch_if_empty": 0, 
    "fieldname": "end_date", 
    "fieldtype": "Date", 
    "hidden": 0, 
@@ -148,6 +153,7 @@
    "bold": 0, 
    "collapsible": 0, 
    "columns": 0, 
+   "fetch_if_empty": 0, 
    "fieldname": "section_break_5", 
    "fieldtype": "Section Break", 
    "hidden": 1, 
@@ -180,6 +186,7 @@
    "bold": 0, 
    "collapsible": 0, 
    "columns": 0, 
+   "fetch_if_empty": 0, 
    "fieldname": "periods", 
    "fieldtype": "Table", 
    "hidden": 0, 
@@ -213,6 +220,7 @@
    "bold": 0, 
    "collapsible": 0, 
    "columns": 0, 
+   "fetch_if_empty": 0, 
    "fieldname": "section_break_7", 
    "fieldtype": "Section Break", 
    "hidden": 0, 
@@ -245,6 +253,7 @@
    "bold": 0, 
    "collapsible": 0, 
    "columns": 0, 
+   "fetch_if_empty": 0, 
    "fieldname": "taxable_salary_slabs", 
    "fieldtype": "Table", 
    "hidden": 0, 
@@ -270,6 +279,39 @@
    "set_only_once": 0, 
    "translatable": 0, 
    "unique": 0
+  }, 
+  {
+   "allow_bulk_edit": 0, 
+   "allow_in_quick_entry": 0, 
+   "allow_on_submit": 0, 
+   "bold": 0, 
+   "collapsible": 0, 
+   "columns": 0, 
+   "fetch_if_empty": 0, 
+   "fieldname": "standard_tax_exemption_amount", 
+   "fieldtype": "Currency", 
+   "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": "Standard Tax Exemption Amount", 
+   "length": 0, 
+   "no_copy": 0, 
+   "permlevel": 0, 
+   "precision": "", 
+   "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, 
+   "set_only_once": 0, 
+   "translatable": 0, 
+   "unique": 0
   }
  ], 
  "has_web_view": 0, 
@@ -282,7 +324,7 @@
  "issingle": 0, 
  "istable": 0, 
  "max_attachments": 0, 
- "modified": "2018-05-25 12:29:07.207927", 
+ "modified": "2019-04-26 01:45:03.160929", 
  "modified_by": "Administrator", 
  "module": "HR", 
  "name": "Payroll Period", 
@@ -354,5 +396,6 @@
  "sort_field": "modified", 
  "sort_order": "DESC", 
  "track_changes": 1, 
- "track_seen": 0
+ "track_seen": 0, 
+ "track_views": 0
 }
\ No newline at end of file
diff --git a/erpnext/hr/doctype/salary_component/salary_component.js b/erpnext/hr/doctype/salary_component/salary_component.js
index 1d398cc..f6afd5e 100644
--- a/erpnext/hr/doctype/salary_component/salary_component.js
+++ b/erpnext/hr/doctype/salary_component/salary_component.js
@@ -5,10 +5,8 @@
 	setup: function(frm) {
 		frm.set_query("default_account", "accounts", function(doc, cdt, cdn) {
 			var d = locals[cdt][cdn];
-			var root_types = ["Expense", "Liability"];
 			return {
 				filters: {
-					"root_type": ["in", root_types],
 					"is_group": 0,
 					"company": d.company
 				}
diff --git a/erpnext/hr/doctype/salary_slip/salary_slip.py b/erpnext/hr/doctype/salary_slip/salary_slip.py
index 1b487a6..fbfa3af 100644
--- a/erpnext/hr/doctype/salary_slip/salary_slip.py
+++ b/erpnext/hr/doctype/salary_slip/salary_slip.py
@@ -107,8 +107,8 @@
 				for d in self.get("earnings"):
 					if d.is_flexible_benefit == 1:
 						current_flexi_amount += d.amount
-				last_benefits = get_last_payroll_period_benefits(self.employee, self.start_date, self.end_date,\
-				 current_flexi_amount, payroll_period, self._salary_structure_doc)
+				last_benefits = get_last_payroll_period_benefits(self.employee, self.start_date, self.end_date,
+					current_flexi_amount, payroll_period, self._salary_structure_doc)
 				if last_benefits:
 					for last_benefit in last_benefits:
 						last_benefit = frappe._dict(last_benefit)
@@ -118,7 +118,7 @@
 	def add_employee_flexi_benefits(self, struct_row):
 		if frappe.db.get_value("Salary Component", struct_row.salary_component, "pay_against_benefit_claim") != 1:
 			benefit_component_amount = get_benefit_component_amount(self.employee, self.start_date, self.end_date, \
-			struct_row, self._salary_structure_doc, self.total_working_days, self.payroll_frequency)
+				struct_row, self._salary_structure_doc, self.total_working_days, self.payroll_frequency)
 			if benefit_component_amount:
 				self.update_component_row(struct_row, benefit_component_amount, "earnings")
 		else:
@@ -418,7 +418,7 @@
 
 		for d in self.get(component_type):
 			if (self.salary_structure and
-				cint(d.depends_on_payment_days) and
+				cint(d.depends_on_payment_days) and cint(self.total_working_days) and
 				(not
 				    self.salary_slip_based_on_timesheet or
 					getdate(self.start_date) < joining_date or
@@ -577,8 +577,8 @@
 
 	def calculate_variable_tax(self, tax_component, payroll_period):
 		annual_taxable_earning, period_factor = 0, 0
-		pro_rata_tax_paid, additional_tax_paid,  benefit_tax_paid = 0, 0, 0
-		unclaimed_earning, unclaimed_benefit, additional_income = 0, 0, 0
+		pro_rata_tax_paid, additional_tax_paid,  benefit_tax_paid = 0.0, 0.0, 0.0
+		unclaimed_earning, unclaimed_benefit, additional_income = 0.0, 0.0, 0.0
 
 		# get taxable_earning, additional_income in this slip
 		taxable_earning = self.get_taxable_earnings()
@@ -593,7 +593,7 @@
 			unclaimed_earning = self.calculate_unclaimed_taxable_earning(payroll_period, tax_component)
 			earning_in_period = taxable_earning["taxable_earning"] + unclaimed_earning
 			period_factor = self.get_period_factor(payroll_period.start_date, payroll_period.end_date,
-								payroll_period.start_date, self.end_date)
+				payroll_period.start_date, self.end_date)
 			annual_taxable_earning = earning_in_period * period_factor
 			additional_income += self.get_total_additional_income(payroll_period.start_date)
 		else:
@@ -607,6 +607,7 @@
 					{"employee": self.employee, "payroll_period": payroll_period.name, "docstatus": 1},
 					"total_exemption_amount")
 			annual_taxable_earning = annual_earning - exemption_amount
+
 		if self.deduct_tax_for_unclaimed_employee_benefits or self.deduct_tax_for_unsubmitted_tax_exemption_proof:
 			tax_detail = self.get_tax_paid_in_period(payroll_period, tax_component)
 			if tax_detail:
@@ -616,11 +617,17 @@
 
 		# add any additional income in this slip
 		additional_income += taxable_earning["additional_income"]
-		args = {"payroll_period": payroll_period.name, "tax_component": tax_component,
-				"annual_taxable_earning": annual_taxable_earning, "period_factor": period_factor,
-				"unclaimed_benefit": unclaimed_benefit, "additional_income": additional_income,
-				"pro_rata_tax_paid": pro_rata_tax_paid, "benefit_tax_paid": benefit_tax_paid,
-				"additional_tax_paid": additional_tax_paid}
+		args = {
+			"payroll_period": payroll_period.name,
+			"tax_component": tax_component,
+			"period_factor": period_factor,
+			"annual_taxable_earning": annual_taxable_earning,
+			"additional_income": additional_income,
+			"unclaimed_benefit": unclaimed_benefit,
+			"pro_rata_tax_paid": pro_rata_tax_paid,
+			"benefit_tax_paid": benefit_tax_paid,
+			"additional_tax_paid": additional_tax_paid
+		}
 		return self.calculate_tax(args)
 
 	def calculate_unclaimed_taxable_benefit(self, payroll_period):
@@ -667,27 +674,49 @@
 		return total_taxable_earning
 
 	def get_total_additional_income(self, from_date):
-		total_additional_pay = 0
-		sum_additional_earning = frappe.db.sql("""select sum(sd.amount) from `tabSalary Detail` sd join
-					`tabSalary Slip` ss on sd.parent=ss.name where sd.parentfield='earnings'
-					and sd.is_tax_applicable=1 and is_additional_component=1 and is_flexible_benefit=0
-					and ss.docstatus=1 and ss.employee='{0}' and ss.start_date between '{1}' and '{2}'
-					and ss.end_date between '{1}' and '{2}'""".format(self.employee,
-					from_date, self.start_date))
-		if sum_additional_earning and sum_additional_earning[0][0]:
-			total_additional_pay = sum_additional_earning[0][0]
-		return total_additional_pay
+		sum_additional_earning = frappe.db.sql("""
+			select sum(sd.amount)
+			from
+				`tabSalary Detail` sd join `tabSalary Slip` ss on sd.parent=ss.name
+			where
+				sd.parentfield='earnings'
+				and sd.is_tax_applicable=1 and is_additional_component=1
+				and is_flexible_benefit=0 and ss.docstatus=1
+				and ss.employee=%(employee)s
+				and ss.start_date between %(from_date)s and %(to_date)s
+				and ss.end_date between %(from_date)s and %(to_date)s
+			""", {
+				"employee": self.employee,
+				"from_date": from_date,
+				"to_date": self.start_date
+			})
+		return flt(sum_additional_earning[0][0]) if sum_additional_earning else 0
 
 	def get_tax_paid_in_period(self, payroll_period, tax_component, only_total=False):
 		# find total_tax_paid, tax paid for benefit, additional_salary
-		sum_tax_paid = frappe.db.sql("""select sum(sd.amount), sum(tax_on_flexible_benefit),
-			sum(tax_on_additional_salary)  from `tabSalary Detail` sd join `tabSalary Slip`
-			ss on sd.parent=ss.name where sd.parentfield='deductions' and sd.salary_component='{3}'
-			and sd.variable_based_on_taxable_salary=1 and ss.docstatus=1 and ss.employee='{0}'
-			and ss.start_date between '{1}' and '{2}' and ss.end_date between '{1}' and
-			'{2}'""".format(self.employee, payroll_period.start_date, self.start_date, tax_component))
+		sum_tax_paid = frappe.db.sql("""
+			select
+				sum(sd.amount), sum(tax_on_flexible_benefit), sum(tax_on_additional_salary)
+			from
+				`tabSalary Detail` sd join `tabSalary Slip` ss on sd.parent=ss.name
+			where
+				sd.parentfield='deductions' and sd.salary_component=%(salary_component)s
+				and sd.variable_based_on_taxable_salary=1
+				and ss.docstatus=1 and ss.employee=%(employee)s
+				and ss.start_date between %(from_date)s and %(to_date)s
+				and ss.end_date between %(from_date)s and %(to_date)s
+		""", {
+			"salary_component": tax_component,
+			"employee": self.employee,
+			"from_date": payroll_period.start_date,
+			"to_date": self.start_date
+		})
 		if sum_tax_paid and sum_tax_paid[0][0]:
-			return {'total_tax_paid': sum_tax_paid[0][0], 'benefit_tax':sum_tax_paid[0][1], 'additional_tax': sum_tax_paid[0][2]}
+			return {
+				'total_tax_paid': sum_tax_paid[0][0],
+				'benefit_tax':sum_tax_paid[0][1],
+				'additional_tax': sum_tax_paid[0][2]
+			}
 
 	def get_taxable_earnings(self, include_flexi=0, only_flexi=0):
 		taxable_earning = 0
@@ -698,22 +727,22 @@
 					additional_income += earning.amount
 					continue
 				if only_flexi:
-					if earning.is_tax_applicable and earning.is_flexible_benefit:
+					if earning.is_flexible_benefit:
 						taxable_earning += earning.amount
 					continue
-				if include_flexi:
-					if earning.is_tax_applicable or (earning.is_tax_applicable and earning.is_flexible_benefit):
-						taxable_earning += earning.amount
-				else:
-					if earning.is_tax_applicable and not earning.is_flexible_benefit:
-						taxable_earning += earning.amount
-		return {"taxable_earning": taxable_earning, "additional_income": additional_income}
+				if include_flexi or not earning.is_flexible_benefit:
+					taxable_earning += earning.amount
+		return {
+			"taxable_earning": taxable_earning,
+			"additional_income": additional_income
+		}
 
 	def calculate_tax(self, args):
 		tax_amount, benefit_tax, additional_tax = 0, 0, 0
 		annual_taxable_earning = args.get("annual_taxable_earning")
 		benefit_to_tax = args.get("unclaimed_benefit")
 		additional_income = args.get("additional_income")
+
 		# Get tax calc by period
 		annual_tax = self.calculate_tax_by_tax_slab(args.get("payroll_period"), annual_taxable_earning)
 
@@ -744,8 +773,10 @@
 
 	def calculate_tax_by_tax_slab(self, payroll_period, annual_taxable_earning):
 		payroll_period_obj = frappe.get_doc("Payroll Period", payroll_period)
+		annual_taxable_earning -= flt(payroll_period_obj.standard_tax_exemption_amount)
 		data = self.get_data_for_eval()
 		data.update({"annual_taxable_earning": annual_taxable_earning})
+
 		taxable_amount = 0
 		for slab in payroll_period_obj.taxable_salary_slabs:
 			if slab.condition and not self.eval_tax_slab_condition(slab.condition, data):
diff --git a/erpnext/hr/doctype/salary_slip/test_salary_slip.py b/erpnext/hr/doctype/salary_slip/test_salary_slip.py
index 746bf8c..75c1e42 100644
--- a/erpnext/hr/doctype/salary_slip/test_salary_slip.py
+++ b/erpnext/hr/doctype/salary_slip/test_salary_slip.py
@@ -159,21 +159,21 @@
 		month = "%02d" % getdate(nowdate()).month
 		m = get_month_details(fiscal_year, month)
 
-		for payroll_frequncy in ["Monthly", "Bimonthly", "Fortnightly", "Weekly", "Daily"]:
-			make_employee(payroll_frequncy + "_test_employee@salary.com")
-			ss = make_employee_salary_slip(payroll_frequncy + "_test_employee@salary.com", payroll_frequncy)
-			if payroll_frequncy == "Monthly":
+		for payroll_frequency in ["Monthly", "Bimonthly", "Fortnightly", "Weekly", "Daily"]:
+			make_employee(payroll_frequency + "_test_employee@salary.com")
+			ss = make_employee_salary_slip(payroll_frequency + "_test_employee@salary.com", payroll_frequency)
+			if payroll_frequency == "Monthly":
 				self.assertEqual(ss.end_date, m['month_end_date'])
-			elif payroll_frequncy == "Bimonthly":
+			elif payroll_frequency == "Bimonthly":
 				if getdate(ss.start_date).day <= 15:
 					self.assertEqual(ss.end_date, m['month_mid_end_date'])
 				else:
 					self.assertEqual(ss.end_date, m['month_end_date'])
-			elif payroll_frequncy == "Fortnightly":
+			elif payroll_frequency == "Fortnightly":
 				self.assertEqual(ss.end_date, add_days(nowdate(),13))
-			elif payroll_frequncy == "Weekly":
+			elif payroll_frequency == "Weekly":
 				self.assertEqual(ss.end_date, add_days(nowdate(),6))
-			elif payroll_frequncy == "Daily":
+			elif payroll_frequency == "Daily":
 				self.assertEqual(ss.end_date, nowdate())
 
 	def test_tax_for_payroll_period(self):
diff --git a/erpnext/hr/doctype/salary_structure/salary_structure.py b/erpnext/hr/doctype/salary_structure/salary_structure.py
index 26efd00..3431d3d 100644
--- a/erpnext/hr/doctype/salary_structure/salary_structure.py
+++ b/erpnext/hr/doctype/salary_structure/salary_structure.py
@@ -57,11 +57,12 @@
 					have_a_flexi = True
 					max_of_component = frappe.db.get_value("Salary Component", earning_component.salary_component, "max_benefit_amount")
 					flexi_amount += max_of_component
+
 			if have_a_flexi and flt(self.max_benefits) == 0:
 				frappe.throw(_("Max benefits should be greater than zero to dispense benefits"))
-			if have_a_flexi and flt(self.max_benefits) > flexi_amount:
-				frappe.throw(_("Total flexible benefit component amount {0} should not be less \
-				than max benefits {1}").format(flexi_amount, self.max_benefits))
+			if have_a_flexi and flexi_amount and flt(self.max_benefits) > flexi_amount:
+				frappe.throw(_("Total flexible benefit component amount {0} should not be less than max benefits {1}")
+					.format(flexi_amount, self.max_benefits))
 		if not have_a_flexi and flt(self.max_benefits) > 0:
 			frappe.throw(_("Salary Structure should have flexible benefit component(s) to dispense benefit amount"))
 
diff --git a/erpnext/patches.txt b/erpnext/patches.txt
index 6daa85f..9787dc8 100644
--- a/erpnext/patches.txt
+++ b/erpnext/patches.txt
@@ -568,7 +568,7 @@
 execute:frappe.delete_doc_if_exists("Page", "purchase-analytics")
 execute:frappe.delete_doc_if_exists("Page", "stock-analytics")
 execute:frappe.delete_doc_if_exists("Page", "production-analytics")
-erpnext.patches.v11_0.ewaybill_fields_gst_india #2018-11-13 #2019-01-09 #2019-04-01
+erpnext.patches.v11_0.ewaybill_fields_gst_india #2018-11-13 #2019-01-09 #2019-04-01 #2019-04-26
 erpnext.patches.v11_0.drop_column_max_days_allowed
 erpnext.patches.v10_0.update_user_image_in_employee
 erpnext.patches.v10_0.repost_gle_for_purchase_receipts_with_rejected_items
diff --git a/erpnext/public/js/controllers/buying.js b/erpnext/public/js/controllers/buying.js
index 3ec27fc..f2fe3fe 100644
--- a/erpnext/public/js/controllers/buying.js
+++ b/erpnext/public/js/controllers/buying.js
@@ -147,6 +147,8 @@
 	},
 
 	discount_amount: function(doc, cdt, cdn) {
+		var item = frappe.get_doc(cdt, cdn);
+		item.discount_percentage = 0.0;
 		this.price_list_rate(doc, cdt, cdn);
 	},
 
diff --git a/erpnext/public/js/controllers/transaction.js b/erpnext/public/js/controllers/transaction.js
index 343e65e..537ca26 100644
--- a/erpnext/public/js/controllers/transaction.js
+++ b/erpnext/public/js/controllers/transaction.js
@@ -23,6 +23,7 @@
 				} else {
 					item.discount_percentage = flt((1 - item.rate / item.price_list_rate) * 100.0,
 						precision("discount_percentage", item));
+					item.discount_amount = flt(item.price_list_rate) - flt(item.rate);
 					item.margin_type = '';
 					item.margin_rate_or_amount = 0;
 					item.rate_with_margin = 0;
diff --git a/erpnext/selling/sales_common.js b/erpnext/selling/sales_common.js
index 965d2b0..23dcaa1 100644
--- a/erpnext/selling/sales_common.js
+++ b/erpnext/selling/sales_common.js
@@ -145,6 +145,8 @@
 	},
 
 	discount_amount: function(doc, cdt, cdn) {
+		var item = frappe.get_doc(cdt, cdn);
+		item.discount_percentage = 0.0;
 		this.apply_discount_on_item(doc, cdt, cdn, 'discount_amount');
 	},