Fixed merge conflict
diff --git a/erpnext/hr/doctype/additional_salary/additional_salary.js b/erpnext/hr/doctype/additional_salary/additional_salary.js
index 957b979..94e06ad 100644
--- a/erpnext/hr/doctype/additional_salary/additional_salary.js
+++ b/erpnext/hr/doctype/additional_salary/additional_salary.js
@@ -6,7 +6,8 @@
 		frm.set_query("salary_component", function() {
 			return {
 				filters: {
-					type: "earning"
+					type: "earning",
+					is_additional_component: true
 				}
 			};
 		});
diff --git a/erpnext/hr/doctype/additional_salary/additional_salary.json b/erpnext/hr/doctype/additional_salary/additional_salary.json
index 0235f49..420d599 100644
--- a/erpnext/hr/doctype/additional_salary/additional_salary.json
+++ b/erpnext/hr/doctype/additional_salary/additional_salary.json
@@ -438,4 +438,4 @@
  "title_field": "employee", 
  "track_changes": 1, 
  "track_seen": 0
-}
\ No newline at end of file
+}
diff --git a/erpnext/hr/doctype/attendance_request/attendance_request.json b/erpnext/hr/doctype/attendance_request/attendance_request.json
index d152a70..c626941 100644
--- a/erpnext/hr/doctype/attendance_request/attendance_request.json
+++ b/erpnext/hr/doctype/attendance_request/attendance_request.json
@@ -15,6 +15,7 @@
  "fields": [
   {
    "allow_bulk_edit": 0, 
+   "allow_in_quick_entry": 0, 
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
@@ -47,11 +48,12 @@
   }, 
   {
    "allow_bulk_edit": 0, 
+   "allow_in_quick_entry": 0, 
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
    "columns": 0, 
-   "fetch_from": "employee.employee_name",
+   "fetch_from": "employee.employee_name", 
    "fieldname": "employee_name", 
    "fieldtype": "Data", 
    "hidden": 0, 
@@ -80,6 +82,7 @@
   }, 
   {
    "allow_bulk_edit": 0, 
+   "allow_in_quick_entry": 0, 
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
@@ -144,6 +147,7 @@
   }, 
   {
    "allow_bulk_edit": 0, 
+   "allow_in_quick_entry": 0, 
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
@@ -175,6 +179,7 @@
   }, 
   {
    "allow_bulk_edit": 0, 
+   "allow_in_quick_entry": 0, 
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
@@ -206,6 +211,7 @@
   }, 
   {
    "allow_bulk_edit": 0, 
+   "allow_in_quick_entry": 0, 
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
@@ -238,6 +244,7 @@
   }, 
   {
    "allow_bulk_edit": 0, 
+   "allow_in_quick_entry": 0, 
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
@@ -268,6 +275,7 @@
   }, 
   {
    "allow_bulk_edit": 0, 
+   "allow_in_quick_entry": 0, 
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
@@ -299,6 +307,7 @@
   }, 
   {
    "allow_bulk_edit": 0, 
+   "allow_in_quick_entry": 0, 
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
@@ -331,6 +340,7 @@
   }, 
   {
    "allow_bulk_edit": 0, 
+   "allow_in_quick_entry": 0, 
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
@@ -363,6 +373,7 @@
   }, 
   {
    "allow_bulk_edit": 0, 
+   "allow_in_quick_entry": 0, 
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
@@ -403,7 +414,7 @@
  "issingle": 0, 
  "istable": 0, 
  "max_attachments": 0, 
- "modified": "2018-05-17 12:04:20.334541",
+ "modified": "2018-05-25 12:02:39.993839", 
  "modified_by": "Administrator", 
  "module": "HR", 
  "name": "Attendance Request", 
@@ -496,4 +507,4 @@
  "title_field": "employee_name", 
  "track_changes": 1, 
  "track_seen": 0
-}
\ No newline at end of file
+}
diff --git a/erpnext/hr/doctype/compensatory_leave_request/compensatory_leave_request.json b/erpnext/hr/doctype/compensatory_leave_request/compensatory_leave_request.json
index 44c76c5..9d2a966 100644
--- a/erpnext/hr/doctype/compensatory_leave_request/compensatory_leave_request.json
+++ b/erpnext/hr/doctype/compensatory_leave_request/compensatory_leave_request.json
@@ -15,6 +15,7 @@
  "fields": [
   {
    "allow_bulk_edit": 0, 
+   "allow_in_quick_entry": 0, 
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
@@ -47,11 +48,12 @@
   }, 
   {
    "allow_bulk_edit": 0, 
+   "allow_in_quick_entry": 0, 
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
    "columns": 0, 
-   "fetch_from": "employee.employee_name",
+   "fetch_from": "employee.employee_name", 
    "fieldname": "employee_name", 
    "fieldtype": "Data", 
    "hidden": 0, 
@@ -80,6 +82,7 @@
   }, 
   {
    "allow_bulk_edit": 0, 
+   "allow_in_quick_entry": 0, 
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
@@ -144,6 +147,7 @@
   }, 
   {
    "allow_bulk_edit": 0, 
+   "allow_in_quick_entry": 0, 
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
@@ -175,6 +179,7 @@
   }, 
   {
    "allow_bulk_edit": 0, 
+   "allow_in_quick_entry": 0, 
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
@@ -205,6 +210,7 @@
   }, 
   {
    "allow_bulk_edit": 0, 
+   "allow_in_quick_entry": 0, 
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
@@ -237,6 +243,7 @@
   }, 
   {
    "allow_bulk_edit": 0, 
+   "allow_in_quick_entry": 0, 
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
@@ -268,6 +275,7 @@
   }, 
   {
    "allow_bulk_edit": 0, 
+   "allow_in_quick_entry": 0, 
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
@@ -300,6 +308,7 @@
   }, 
   {
    "allow_bulk_edit": 0, 
+   "allow_in_quick_entry": 0, 
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
@@ -340,7 +349,7 @@
  "issingle": 0, 
  "istable": 0, 
  "max_attachments": 0, 
- "modified": "2018-05-17 10:09:21.089982",
+ "modified": "2018-05-25 12:02:05.585184", 
  "modified_by": "Administrator", 
  "module": "HR", 
  "name": "Compensatory Leave Request", 
@@ -433,4 +442,4 @@
  "title_field": "employee_name", 
  "track_changes": 1, 
  "track_seen": 0
-}
\ No newline at end of file
+}
diff --git a/erpnext/hr/doctype/employee_benefit_application/employee_benefit_application.js b/erpnext/hr/doctype/employee_benefit_application/employee_benefit_application.js
index 7859a47..f96f262 100644
--- a/erpnext/hr/doctype/employee_benefit_application/employee_benefit_application.js
+++ b/erpnext/hr/doctype/employee_benefit_application/employee_benefit_application.js
@@ -5,39 +5,41 @@
 	setup: function(frm) {
 		frm.set_query("earning_component", "employee_benefits", function() {
 			return {
-				filters: {
-					type: "Earning",
-					is_flexible_benefit: true,
-					disabled: false
-				}
+				query : "erpnext.hr.doctype.employee_benefit_application.employee_benefit_application.get_earning_components",
+				filters: {date: frm.doc.date, employee: frm.doc.employee}
 			};
 		});
 	},
 	employee: function(frm) {
-		frappe.call({
-			method: "erpnext.hr.doctype.employee_benefit_application.employee_benefit_application.get_max_benefits",
-			args:{
-				employee: frm.doc.employee,
-				on_date: frm.doc.date
-			},
-			callback: function (data) {
-				if(!data.exc){
-					if(data.message){
-						frm.set_value("max_benefits", data.message);
+		if(frm.doc.employee && frm.doc.date){
+			frappe.call({
+				method: "erpnext.hr.doctype.employee_benefit_application.employee_benefit_application.get_max_benefits",
+				args:{
+					employee: frm.doc.employee,
+					on_date: frm.doc.date
+				},
+				callback: function (data) {
+					if(!data.exc){
+						if(data.message){
+							frm.set_value("max_benefits", data.message);
+						}
 					}
 				}
-			}
-		});
+			});
+		}
 	}
 });
 
 frappe.ui.form.on("Employee Benefit Application Detail",{
-	amount:  function(frm, cdt, cdn) {
-		calculate_all(frm.doc, cdt, cdn);
+	amount:  function(frm) {
+		calculate_all(frm.doc);
+	},
+	employee_benefits_remove: function(frm) {
+		calculate_all(frm.doc);
 	}
 });
 
-var calculate_all = function(doc, dt, dn) {
+var calculate_all = function(doc) {
 	var tbl = doc.employee_benefits || [];
 	var pro_rata_dispensed_amount = 0;
 	var total_amount = 0;
@@ -46,7 +48,7 @@
 			total_amount += flt(tbl[i].amount);
 		}
 		if(tbl[i].is_pro_rata_applicable == 1){
-			pro_rata_dispensed_amount += flt(tbl[i].amount)
+			pro_rata_dispensed_amount += flt(tbl[i].amount);
 		}
 	}
 	doc.total_amount = total_amount;
diff --git a/erpnext/hr/doctype/employee_benefit_application/employee_benefit_application.json b/erpnext/hr/doctype/employee_benefit_application/employee_benefit_application.json
index cf17854..7a8803a 100644
--- a/erpnext/hr/doctype/employee_benefit_application/employee_benefit_application.json
+++ b/erpnext/hr/doctype/employee_benefit_application/employee_benefit_application.json
@@ -15,6 +15,7 @@
  "fields": [
   {
    "allow_bulk_edit": 0, 
+   "allow_in_quick_entry": 0, 
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
@@ -47,11 +48,12 @@
   }, 
   {
    "allow_bulk_edit": 0, 
+   "allow_in_quick_entry": 0, 
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
    "columns": 0, 
-   "fetch_from": "employee.employee_name",
+   "fetch_from": "employee.employee_name", 
    "fieldname": "employee_name", 
    "fieldtype": "Data", 
    "hidden": 0, 
@@ -80,6 +82,7 @@
   }, 
   {
    "allow_bulk_edit": 0, 
+   "allow_in_quick_entry": 0, 
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
@@ -111,6 +114,7 @@
   }, 
   {
    "allow_bulk_edit": 0, 
+   "allow_in_quick_entry": 0, 
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
@@ -142,6 +146,7 @@
   }, 
   {
    "allow_bulk_edit": 0, 
+   "allow_in_quick_entry": 0, 
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
@@ -172,6 +177,7 @@
   }, 
   {
    "allow_bulk_edit": 0, 
+   "allow_in_quick_entry": 0, 
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
@@ -204,6 +210,7 @@
   }, 
   {
    "allow_bulk_edit": 0, 
+   "allow_in_quick_entry": 0, 
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
@@ -236,6 +243,7 @@
   }, 
   {
    "allow_bulk_edit": 0, 
+   "allow_in_quick_entry": 0, 
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
@@ -267,6 +275,7 @@
   }, 
   {
    "allow_bulk_edit": 0, 
+   "allow_in_quick_entry": 0, 
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
@@ -298,6 +307,7 @@
   }, 
   {
    "allow_bulk_edit": 0, 
+   "allow_in_quick_entry": 0, 
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
@@ -362,6 +372,7 @@
   }, 
   {
    "allow_bulk_edit": 0, 
+   "allow_in_quick_entry": 0, 
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
@@ -392,6 +403,7 @@
   }, 
   {
    "allow_bulk_edit": 0, 
+   "allow_in_quick_entry": 0, 
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
@@ -433,7 +445,7 @@
  "issingle": 0, 
  "istable": 0, 
  "max_attachments": 0, 
- "modified": "2018-05-17 12:43:32.220595",
+ "modified": "2018-05-25 12:01:01.490375", 
  "modified_by": "Administrator", 
  "module": "HR", 
  "name": "Employee Benefit Application", 
@@ -526,4 +538,4 @@
  "title_field": "employee_name", 
  "track_changes": 1, 
  "track_seen": 0
-}
\ No newline at end of file
+}
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 8e59bf5..2d33ce8 100644
--- a/erpnext/hr/doctype/employee_benefit_application/employee_benefit_application.py
+++ b/erpnext/hr/doctype/employee_benefit_application/employee_benefit_application.py
@@ -5,18 +5,50 @@
 from __future__ import unicode_literals
 import frappe
 from frappe import _
-from frappe.utils import nowdate, date_diff, getdate
+from frappe.utils import date_diff, getdate
 from frappe.model.document import Document
 from erpnext.hr.doctype.payroll_period.payroll_period import get_payroll_period_days
+from erpnext.hr.doctype.salary_structure_assignment.salary_structure_assignment import get_assigned_salary_structure
 
 class EmployeeBenefitApplication(Document):
 	def validate(self):
+		self.validate_duplicate_on_payroll_period()
 		if self.max_benefits <= 0:
 			frappe.throw(_("Employee {0} has no maximum benefit amount").format(self.employee))
 		self.validate_max_benefit_for_component()
+		if self.remainig_benefits > 0:
+			self.validate_remaining_benefit_amount()
 
-	def before_submit(self):
-		self.validate_duplicate_on_payroll_period()
+	def validate_remaining_benefit_amount(self):
+		# check salary structure earnings have flexi component (sum of max_benefit_amount)
+		# without pro-rata which satisfy the remainig_benefits
+		# else pro-rata component for the amount
+		# again comes the same validation and satisfy or throw
+		benefit_components = []
+		if self.employee_benefits:
+			for employee_benefit in self.employee_benefits:
+				benefit_components.append(employee_benefit.earning_component)
+		salary_struct_name = get_assigned_salary_structure(self.employee, self.date)
+		if salary_struct_name:
+			non_pro_rata_amount = 0
+			pro_rata_amount = 0
+			salary_structure = frappe.get_doc("Salary Structure", salary_struct_name)
+			if salary_structure.earnings:
+				for earnings in salary_structure.earnings:
+					if earnings.is_flexible_benefit == 1 and earnings.salary_component not in benefit_components:
+						is_pro_rata_applicable, max_benefit_amount = frappe.db.get_value("Salary Component", earnings.salary_component, ["is_pro_rata_applicable", "max_benefit_amount"])
+						if is_pro_rata_applicable == 1:
+							pro_rata_amount += max_benefit_amount
+						else:
+							non_pro_rata_amount += max_benefit_amount
+			if pro_rata_amount == 0  and non_pro_rata_amount == 0:
+				frappe.throw(_("Please add the remainig benefits {0} to any of the existing component").format(self.remainig_benefits))
+			elif non_pro_rata_amount > 0 and non_pro_rata_amount < self.remainig_benefits:
+				frappe.throw(_("You can claim only an amount of {0}, the rest amount {1} should be in the application \
+				as pro-rata component").format(non_pro_rata_amount, self.remainig_benefits - non_pro_rata_amount))
+			elif non_pro_rata_amount == 0:
+				frappe.throw(_("Please add the remainig benefits {0} to the application as \
+				pro-rata component").format(self.remainig_benefits))
 
 	def validate_max_benefit_for_component(self):
 		if self.employee_benefits:
@@ -50,9 +82,9 @@
 
 @frappe.whitelist()
 def get_max_benefits(employee, on_date):
-	sal_struct = get_assigned_salary_sturecture(employee, on_date)
+	sal_struct = get_assigned_salary_structure(employee, on_date)
 	if sal_struct:
-		max_benefits = frappe.db.get_value("Salary Structure", sal_struct[0][0], "max_benefits")
+		max_benefits = frappe.db.get_value("Salary Structure", sal_struct, "max_benefits")
 		if max_benefits > 0:
 			return max_benefits
 		else:
@@ -60,76 +92,74 @@
 	else:
 		frappe.throw(_("Employee {0} has no salary structure assigned").format(employee))
 
-
-@frappe.whitelist()
-def get_assigned_salary_sturecture(employee, _date):
-	if not _date:
-		_date = nowdate()
-	salary_structure = frappe.db.sql("""
-		select salary_structure from `tabSalary Structure Assignment`
-		where employee=%(employee)s
-		and docstatus = 1
-		and (
-			(%(_date)s between from_date and ifnull(to_date, '2199-12-31'))
-		)""", {
-			'employee': employee,
-			'_date': _date,
-		})
-	if salary_structure:
-		return salary_structure
-
-def get_employee_benefit_application(salary_slip):
-	employee_benefits = frappe.db.sql("""
+def get_benefit_component_amount(employee, start_date, end_date, struct_row, sal_struct):
+	# Considering there is only one application for an year
+	benefit_application_name = frappe.db.sql("""
 	select name from `tabEmployee Benefit Application`
 	where employee=%(employee)s
 	and docstatus = 1
 	and (date between %(start_date)s and %(end_date)s)
 	""", {
-		'employee': salary_slip.employee,
-		'start_date': salary_slip.start_date,
-		'end_date': salary_slip.end_date
+		'employee': employee,
+		'start_date': start_date,
+		'end_date': end_date
 	})
 
-	if employee_benefits:
-		for employee_benefit in employee_benefits:
-			employee_benefit_obj = frappe.get_doc("Employee Benefit Application", employee_benefit[0])
-			return get_components(employee_benefit_obj, salary_slip)
+	payroll_period_days = get_payroll_period_days(start_date, end_date, frappe.db.get_value("Employee", employee, "company"))
+	if payroll_period_days:
+		# If there is application for benefit claim then fetch the amount from it.
+		if benefit_application_name:
+			benefit_application = frappe.get_doc("Employee Benefit Application", benefit_application_name[0][0])
+			return get_benefit_amount(benefit_application, start_date, end_date, struct_row, payroll_period_days)
 
-def get_components(employee_benefit_application, salary_slip):
-	salary_components_array = []
-	group_component_amount = {}
-	payroll_period_days = get_payroll_period_days(salary_slip.start_date, salary_slip.end_date, salary_slip.company)
-	for employee_benefit in employee_benefit_application.employee_benefits:
-		if employee_benefit.is_pro_rata_applicable == 1:
-			struct_row = {}
-			salary_components_dict = {}
-			amount = get_amount(payroll_period_days, salary_slip.start_date, salary_slip.end_date, employee_benefit.amount)
-			sc = frappe.get_doc("Salary Component", employee_benefit.earning_component)
-			salary_component = sc
-			if sc.earning_component_group and not sc.is_group and not sc.flexi_default:
-				salary_component = frappe.get_doc("Salary Component", sc.earning_component_group)
-				if group_component_amount and group_component_amount.has_key(sc.earning_component_group):
-					group_component_amount[sc.earning_component_group] += amount
-				else:
-					group_component_amount[sc.earning_component_group] = amount
-				amount = group_component_amount[sc.earning_component_group]
-			struct_row['depends_on_lwp'] = salary_component.depends_on_lwp
-			struct_row['salary_component'] = salary_component.name
-			struct_row['abbr'] = salary_component.salary_component_abbr
-			struct_row['do_not_include_in_total'] = salary_component.do_not_include_in_total
-			salary_components_dict['amount'] = amount
-			salary_components_dict['struct_row'] = struct_row
-			salary_components_array.append(salary_components_dict)
-
-	if len(salary_components_array) > 0:
-		return salary_components_array
+		# TODO: Check if there is benefit claim for employee then pro-rata devid the rest of amount (Late Benefit Application)
+		# else Split the max benefits to the pro-rata components with the ratio of thier max_benefit_amount
+		else:
+			component_max = frappe.db.get_value("Salary Component", struct_row.salary_component, "max_benefit_amount")
+			if component_max > 0:
+				return get_benefit_pro_rata_ratio_amount(sal_struct, component_max, payroll_period_days, start_date, end_date)
 	return False
 
+def get_benefit_pro_rata_ratio_amount(sal_struct, component_max, payroll_period_days, start_date, end_date):
+	total_pro_rata_max = 0
+	for sal_struct_row in sal_struct.get("earnings"):
+		is_pro_rata_applicable, max_benefit_amount = frappe.db.get_value("Salary Component", sal_struct_row.salary_component, ["is_pro_rata_applicable", "max_benefit_amount"])
+		if sal_struct_row.is_flexible_benefit == 1 and is_pro_rata_applicable == 1:
+			total_pro_rata_max += max_benefit_amount
+	if total_pro_rata_max > 0:
+		benefit_amount = component_max * sal_struct.max_benefits / total_pro_rata_max
+		if benefit_amount > component_max:
+			benefit_amount = component_max
+		return get_amount(payroll_period_days, start_date, end_date, benefit_amount)
+	return False
+
+def get_benefit_amount(application, start_date, end_date, struct_row, payroll_period_days):
+	amount = 0
+	for employee_benefit in application.employee_benefits:
+		if employee_benefit.earning_component == struct_row.salary_component:
+			amount += get_amount(payroll_period_days, start_date, end_date, employee_benefit.amount)
+	return amount if amount > 0 else False
+
 def get_amount(payroll_period_days, start_date, end_date, amount):
 	salary_slip_days = date_diff(getdate(end_date), getdate(start_date)) + 1
 	amount_per_day = amount / payroll_period_days
 	total_amount = amount_per_day * salary_slip_days
-	if total_amount > amount:
-		return amount
-	else:
-		return total_amount
+	return total_amount
+
+def get_earning_components(doctype, txt, searchfield, start, page_len, filters):
+	if len(filters) < 2:
+		return {}
+	employee = filters['employee']
+	date = filters['date']
+	salary_structure = get_assigned_salary_structure(employee, date)
+
+	if salary_structure:
+		query = """select salary_component from `tabSalary Detail` where parent = '{salary_structure}'
+		and is_flexible_benefit = 1
+		order by name"""
+
+		return frappe.db.sql(query.format(**{
+			"salary_structure": salary_structure
+		}))
+
+	return {}
diff --git a/erpnext/hr/doctype/employee_benefit_application_detail/employee_benefit_application_detail.json b/erpnext/hr/doctype/employee_benefit_application_detail/employee_benefit_application_detail.json
index d2b1d2a..eba9b19 100644
--- a/erpnext/hr/doctype/employee_benefit_application_detail/employee_benefit_application_detail.json
+++ b/erpnext/hr/doctype/employee_benefit_application_detail/employee_benefit_application_detail.json
@@ -15,6 +15,7 @@
  "fields": [
   {
    "allow_bulk_edit": 0, 
+   "allow_in_quick_entry": 0, 
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
@@ -47,6 +48,7 @@
   }, 
   {
    "allow_bulk_edit": 0, 
+   "allow_in_quick_entry": 0, 
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
@@ -80,6 +82,41 @@
   }, 
   {
    "allow_bulk_edit": 0, 
+   "allow_in_quick_entry": 0, 
+   "allow_on_submit": 0, 
+   "bold": 0, 
+   "collapsible": 0, 
+   "columns": 0, 
+   "fetch_from": "earning_component.max_benefit_amount", 
+   "fieldname": "max_benefit_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": "Max Benefit Amount", 
+   "length": 0, 
+   "no_copy": 0, 
+   "options": "", 
+   "permlevel": 0, 
+   "precision": "", 
+   "print_hide": 0, 
+   "print_hide_if_no_value": 0, 
+   "read_only": 1, 
+   "remember_last_selected_value": 0, 
+   "report_hide": 0, 
+   "reqd": 0, 
+   "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, 
@@ -120,7 +157,7 @@
  "issingle": 0, 
  "istable": 1, 
  "max_attachments": 0, 
- "modified": "2018-05-25 12:03:25.545041", 
+ "modified": "2018-05-26 12:00:37.588822", 
  "modified_by": "Administrator", 
  "module": "HR", 
  "name": "Employee Benefit Application Detail", 
@@ -130,6 +167,7 @@
  "quick_entry": 1, 
  "read_only": 0, 
  "read_only_onload": 0, 
+ "search_fields": "", 
  "show_name_in_global_search": 0, 
  "sort_field": "modified", 
  "sort_order": "DESC", 
diff --git a/erpnext/hr/doctype/employee_benefit_claim/employee_benefit_claim.js b/erpnext/hr/doctype/employee_benefit_claim/employee_benefit_claim.js
index 64fde03..5e12828 100644
--- a/erpnext/hr/doctype/employee_benefit_claim/employee_benefit_claim.js
+++ b/erpnext/hr/doctype/employee_benefit_claim/employee_benefit_claim.js
@@ -5,12 +5,12 @@
 	setup: function(frm) {
 		frm.set_query("earning_component", function() {
 			return {
-				filters: {
-					type: "Earning",
-					is_flexible_benefit: true,
-					disabled: false
-				}
+				query : "erpnext.hr.doctype.employee_benefit_application.employee_benefit_application.get_earning_components",
+				filters: {date: frm.doc.claim_date, employee: frm.doc.employee}
 			};
 		});
+	},
+	employee: function(frm) {
+		frm.set_value("earning_component", null);
 	}
 });
diff --git a/erpnext/hr/doctype/employee_benefit_claim/employee_benefit_claim.json b/erpnext/hr/doctype/employee_benefit_claim/employee_benefit_claim.json
index a5b707f..a59aaea 100644
--- a/erpnext/hr/doctype/employee_benefit_claim/employee_benefit_claim.json
+++ b/erpnext/hr/doctype/employee_benefit_claim/employee_benefit_claim.json
@@ -15,6 +15,7 @@
  "fields": [
   {
    "allow_bulk_edit": 0, 
+   "allow_in_quick_entry": 0, 
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
@@ -47,11 +48,12 @@
   }, 
   {
    "allow_bulk_edit": 0, 
+   "allow_in_quick_entry": 0, 
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
    "columns": 0, 
-   "fetch_from": "employee.employee_name",
+   "fetch_from": "employee.employee_name", 
    "fieldname": "employee_name", 
    "fieldtype": "Data", 
    "hidden": 0, 
@@ -80,6 +82,7 @@
   }, 
   {
    "allow_bulk_edit": 0, 
+   "allow_in_quick_entry": 0, 
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
@@ -116,7 +119,8 @@
    "allow_on_submit": 0,
    "bold": 0,
    "collapsible": 0,
-   "columns": 0,
+   "columns": 0, 
+   "default": "Today", 
    "fieldname": "claim_date", 
    "fieldtype": "Date", 
    "hidden": 0, 
@@ -144,6 +148,7 @@
   }, 
   {
    "allow_bulk_edit": 0, 
+   "allow_in_quick_entry": 0, 
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
@@ -174,6 +179,7 @@
   }, 
   {
    "allow_bulk_edit": 0, 
+   "allow_in_quick_entry": 0, 
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
@@ -206,6 +212,7 @@
   }, 
   {
    "allow_bulk_edit": 0, 
+   "allow_in_quick_entry": 0, 
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
@@ -239,6 +246,7 @@
   }, 
   {
    "allow_bulk_edit": 0, 
+   "allow_in_quick_entry": 0, 
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
@@ -272,6 +280,7 @@
   }, 
   {
    "allow_bulk_edit": 0, 
+   "allow_in_quick_entry": 0, 
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
@@ -303,6 +312,7 @@
   }, 
   {
    "allow_bulk_edit": 0, 
+   "allow_in_quick_entry": 0, 
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
@@ -335,6 +345,7 @@
   }, 
   {
    "allow_bulk_edit": 0, 
+   "allow_in_quick_entry": 0, 
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
@@ -366,6 +377,7 @@
   }, 
   {
    "allow_bulk_edit": 0, 
+   "allow_in_quick_entry": 0, 
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
@@ -396,6 +408,7 @@
   }, 
   {
    "allow_bulk_edit": 0, 
+   "allow_in_quick_entry": 0, 
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
@@ -436,7 +449,7 @@
  "issingle": 0, 
  "istable": 0, 
  "max_attachments": 0, 
- "modified": "2018-05-25 12:43:21.082282",
+ "modified": "2018-05-26 12:43:21.082282",
  "modified_by": "Administrator", 
  "module": "HR", 
  "name": "Employee Benefit Claim", 
@@ -529,4 +542,4 @@
  "title_field": "employee_name", 
  "track_changes": 1, 
  "track_seen": 0
-}
\ No newline at end of file
+}
diff --git a/erpnext/hr/doctype/employee_benefit_claim/employee_benefit_claim.py b/erpnext/hr/doctype/employee_benefit_claim/employee_benefit_claim.py
index 39b3540..1aed7ce 100644
--- a/erpnext/hr/doctype/employee_benefit_claim/employee_benefit_claim.py
+++ b/erpnext/hr/doctype/employee_benefit_claim/employee_benefit_claim.py
@@ -7,59 +7,127 @@
 from frappe import _
 from frappe.model.document import Document
 from erpnext.hr.doctype.employee_benefit_application.employee_benefit_application import get_max_benefits
+from erpnext.hr.utils import get_payroll_period
+from erpnext.hr.doctype.salary_structure_assignment.salary_structure_assignment import get_assigned_salary_structure
 
 class EmployeeBenefitClaim(Document):
 	def validate(self):
-		if not self.is_pro_rata_applicable:
-			self.validate_max_benefit_for_sal_struct()
-		# TODO: Validate all cases
-
-	def validate_max_benefit_for_sal_struct(self):
 		max_benefits = get_max_benefits(self.employee, self.claim_date)
+		payroll_period = get_payroll_period(self.claim_date, self.claim_date, frappe.db.get_value("Employee", self.employee, "company"))
+		self.validate_max_benefit_for_component(payroll_period)
+		self.validate_max_benefit_for_sal_struct(max_benefits)
+		self.validate_benefit_claim_amount(max_benefits, payroll_period)
+		if not self.is_pro_rata_applicable:
+			self.validate_non_pro_rata_benefit_claim(max_benefits, payroll_period)
+
+	def validate_benefit_claim_amount(self, max_benefits, payroll_period):
+		claimed_amount = self.claimed_amount
+		claimed_amount += self.get_previous_claimed_amount(payroll_period)
+		if max_benefits < claimed_amount:
+			frappe.throw(_("Maximum benefit of employee {0} exceeds {1} by the sum {2} of previous claimed\
+			amount").format(self.employee, max_benefits, claimed_amount-max_benefits))
+
+	def validate_max_benefit_for_sal_struct(self, max_benefits):
 		if self.claimed_amount > max_benefits:
 			frappe.throw(_("Maximum benefit amount of employee {0} exceeds {1}").format(self.employee, max_benefits))
 
+	def validate_max_benefit_for_component(self, payroll_period):
+		claimed_amount = self.claimed_amount
+		claimed_amount += self.get_previous_claimed_amount(payroll_period, self.earning_component)
+		if claimed_amount > self.max_amount_eligible:
+			frappe.throw(_("Maximum amount eligible for the component {0} exceeds {1}").format(self.earning_component, self.max_amount_eligible))
 
-def get_employee_benefit_claim(salary_slip):
-	employee_benefits = frappe.db.sql("""
-	select name from `tabEmployee Benefit Claim`
+	def validate_non_pro_rata_benefit_claim(self, max_benefits, payroll_period):
+		claimed_amount = self.claimed_amount
+		pro_rata_amount = self.get_pro_rata_amount_in_application(payroll_period.name)
+		if not pro_rata_amount:
+			# Get pro_rata_amount if there is no application,
+			# get salary structure for the date and calculate pro-rata amount
+			pro_rata_amount = self.get_benefit_pro_rata_ratio_amount()
+		if not pro_rata_amount:
+			pro_rata_amount = 0
+
+		claimed_amount += self.get_previous_claimed_amount(payroll_period, True)
+		if max_benefits < pro_rata_amount + claimed_amount:
+			frappe.throw(_("Maximum benefit of employee {0} exceeds {1} by the sum {2} of benefit application pro-rata component\
+			amount and previous claimed amount").format(self.employee, max_benefits, pro_rata_amount+claimed_amount-max_benefits))
+
+	def get_benefit_pro_rata_ratio_amount(self):
+		sal_struct_name = get_assigned_salary_structure(self.employee, self.claim_date)
+		if sal_struct_name:
+			sal_struct = frappe.get_doc("Salary Structure", sal_struct_name)
+			total_pro_rata_max = 0
+			benefit_amount_total = 0
+			for sal_struct_row in sal_struct.get("earnings"):
+				is_pro_rata_applicable, max_benefit_amount = frappe.db.get_value("Salary Component", sal_struct_row.salary_component, ["is_pro_rata_applicable", "max_benefit_amount"])
+				if sal_struct_row.is_flexible_benefit == 1 and is_pro_rata_applicable == 1:
+					total_pro_rata_max += max_benefit_amount
+			if total_pro_rata_max > 0:
+				for sal_struct_row in sal_struct.get("earnings"):
+					is_pro_rata_applicable, max_benefit_amount = frappe.db.get_value("Salary Component", sal_struct_row.salary_component, ["is_pro_rata_applicable", "max_benefit_amount"])
+					if sal_struct_row.is_flexible_benefit == 1 and is_pro_rata_applicable == 1:
+						component_max = max_benefit_amount
+						benefit_amount = component_max * sal_struct.max_benefits / total_pro_rata_max
+						if benefit_amount > component_max:
+							benefit_amount = component_max
+						benefit_amount_total += benefit_amount
+				return benefit_amount_total
+		return False
+
+	def get_pro_rata_amount_in_application(self, payroll_period):
+		application = frappe.db.exists(
+			"Employee Benefit Application",
+			{
+				'employee': self.employee,
+				'payroll_period': payroll_period,
+				'docstatus': 1
+			}
+		)
+		if application:
+			return frappe.db.get_value("Employee Benefit Application", application, "pro_rata_dispensed_amount")
+		return False
+
+	def get_previous_claimed_amount(self, payroll_period, non_pro_rata=False, component=False):
+		total_claimed_amount = 0
+		query = """
+		select sum(claimed_amount) as 'total_amount'
+		from `tabEmployee Benefit Claim`
+		where employee=%(employee)s
+		and docstatus = 1
+		and (claim_date between %(start_date)s and %(end_date)s)
+		"""
+		if non_pro_rata:
+			query += "and is_pro_rata_applicable = 0"
+		if component:
+			query += "and earning_component = %(component)s"
+
+		sum_of_claimed_amount = frappe.db.sql(query, {
+			'employee': self.employee,
+			'start_date': payroll_period.start_date,
+			'end_date': payroll_period.end_date,
+			'component': component
+		}, as_dict=True)
+		if sum_of_claimed_amount and sum_of_claimed_amount[0].total_amount > 0:
+			total_claimed_amount = sum_of_claimed_amount[0].total_amount
+		return total_claimed_amount
+
+def get_benefit_claim_amount(employee, start_date, end_date, struct_row):
+	benefit_claim_details = frappe.db.sql("""
+	select claimed_amount from `tabEmployee Benefit Claim`
 	where employee=%(employee)s
 	and docstatus = 1 and is_pro_rata_applicable = 0
+	and earning_component = %(earning_component)s
 	and (claim_date between %(start_date)s and %(end_date)s)
 	""", {
-		'employee': salary_slip.employee,
-		'start_date': salary_slip.start_date,
-		'end_date': salary_slip.end_date
-	})
+		'employee': employee,
+		'start_date': start_date,
+		'end_date': end_date,
+		'earning_component': struct_row.salary_component
+	}, as_dict = True)
 
-	if employee_benefits:
-		salary_components_array = []
-		for employee_benefit in employee_benefits:
-			struct_row = {}
-			salary_components_dict = {}
-			group_component_amount = {}
-
-			employee_benefit_claim = frappe.get_doc("Employee Benefit Claim", employee_benefit[0])
-			amount = employee_benefit_claim.claimed_amount
-			sc = frappe.get_doc("Salary Component", employee_benefit_claim.earning_component)
-
-			salary_component = sc
-			if sc.earning_component_group and not sc.is_group and not sc.flexi_default:
-				salary_component = frappe.get_doc("Salary Component", sc.earning_component_group)
-				if group_component_amount and group_component_amount.has_key(sc.earning_component_group):
-					group_component_amount[sc.earning_component_group] += amount
-				else:
-					group_component_amount[sc.earning_component_group] = amount
-				amount = group_component_amount[sc.earning_component_group]
-
-			struct_row['depends_on_lwp'] = salary_component.depends_on_lwp
-			struct_row['salary_component'] = salary_component.name
-			struct_row['abbr'] = salary_component.salary_component_abbr
-			struct_row['do_not_include_in_total'] = salary_component.do_not_include_in_total
-			salary_components_dict['amount'] = amount
-			salary_components_dict['struct_row'] = struct_row
-			salary_components_array.append(salary_components_dict)
-
-		if len(salary_components_array) > 0:
-			return salary_components_array
+	if benefit_claim_details:
+		claimed_amount = 0
+		for claim_detail in benefit_claim_details:
+			claimed_amount += claim_detail.claimed_amount
+		return claimed_amount
 	return False
diff --git a/erpnext/hr/doctype/employee_tax_exemption_declaration/test_employee_tax_exemption_declaration.py b/erpnext/hr/doctype/employee_tax_exemption_declaration/test_employee_tax_exemption_declaration.py
index 84970d8..dff02ea 100644
--- a/erpnext/hr/doctype/employee_tax_exemption_declaration/test_employee_tax_exemption_declaration.py
+++ b/erpnext/hr/doctype/employee_tax_exemption_declaration/test_employee_tax_exemption_declaration.py
@@ -84,10 +84,8 @@
 			doctype = 'Payroll Period',
 			name = "_Test Payroll Period",
 			company = "_Test Company",
-			periods = [
-				dict(start_date = date(date.today().year, 1, 1),
-				end_date = date(date.today().year, 12, 31))
-			]
+			start_date = date(date.today().year, 1, 1),
+			end_date = date(date.today().year, 12, 31)
 		)).insert()
 
 def create_exemption_category():
diff --git a/erpnext/hr/doctype/leave_period/leave_period.json b/erpnext/hr/doctype/leave_period/leave_period.json
index 0f3cad4..91f97e7 100644
--- a/erpnext/hr/doctype/leave_period/leave_period.json
+++ b/erpnext/hr/doctype/leave_period/leave_period.json
@@ -15,6 +15,7 @@
  "fields": [
   {
    "allow_bulk_edit": 0, 
+   "allow_in_quick_entry": 0, 
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
@@ -41,10 +42,12 @@
    "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, 
@@ -71,10 +74,12 @@
    "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, 
@@ -100,10 +105,12 @@
    "reqd": 0, 
    "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, 
@@ -131,10 +138,12 @@
    "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, 
@@ -161,10 +170,12 @@
    "reqd": 0, 
    "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, 
@@ -192,10 +203,12 @@
    "reqd": 0, 
    "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": 1, 
@@ -222,10 +235,12 @@
    "reqd": 0, 
    "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, 
@@ -252,15 +267,17 @@
    "reqd": 0, 
    "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, 
-   "fieldname": "employee_grade", 
+   "fieldname": "grade", 
    "fieldtype": "Link", 
    "hidden": 0, 
    "ignore_user_permissions": 0, 
@@ -283,10 +300,12 @@
    "reqd": 0, 
    "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, 
@@ -314,10 +333,12 @@
    "reqd": 0, 
    "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, 
@@ -345,10 +366,12 @@
    "reqd": 0, 
    "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, 
@@ -376,10 +399,12 @@
    "reqd": 0, 
    "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, 
@@ -406,10 +431,12 @@
    "reqd": 0, 
    "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, 
@@ -435,10 +462,12 @@
    "reqd": 0, 
    "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, 
@@ -465,6 +494,7 @@
    "reqd": 0, 
    "search_index": 0, 
    "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0
   }
  ], 
@@ -478,7 +508,7 @@
  "issingle": 0, 
  "istable": 0, 
  "max_attachments": 0, 
- "modified": "2018-05-07 18:25:06.719932", 
+ "modified": "2018-05-25 14:43:02.871669", 
  "modified_by": "Administrator", 
  "module": "HR", 
  "name": "Leave Period", 
@@ -487,7 +517,6 @@
  "permissions": [
   {
    "amend": 0, 
-   "apply_user_permissions": 0, 
    "cancel": 0, 
    "create": 1, 
    "delete": 1, 
@@ -507,7 +536,6 @@
   }, 
   {
    "amend": 0, 
-   "apply_user_permissions": 0, 
    "cancel": 0, 
    "create": 1, 
    "delete": 1, 
@@ -527,7 +555,6 @@
   }, 
   {
    "amend": 0, 
-   "apply_user_permissions": 0, 
    "cancel": 0, 
    "create": 1, 
    "delete": 1, 
diff --git a/erpnext/hr/doctype/payroll_period/payroll_period.json b/erpnext/hr/doctype/payroll_period/payroll_period.json
index 44ead2b..d3a9612 100644
--- a/erpnext/hr/doctype/payroll_period/payroll_period.json
+++ b/erpnext/hr/doctype/payroll_period/payroll_period.json
@@ -15,6 +15,7 @@
  "fields": [
   {
    "allow_bulk_edit": 0, 
+   "allow_in_quick_entry": 0, 
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
@@ -47,6 +48,134 @@
   }, 
   {
    "allow_bulk_edit": 0, 
+   "allow_in_quick_entry": 0, 
+   "allow_on_submit": 0, 
+   "bold": 0, 
+   "collapsible": 0, 
+   "columns": 0, 
+   "fieldname": "column_break_2", 
+   "fieldtype": "Column 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, 
+   "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
+  }, 
+  {
+   "allow_bulk_edit": 0, 
+   "allow_in_quick_entry": 0, 
+   "allow_on_submit": 0, 
+   "bold": 0, 
+   "collapsible": 0, 
+   "columns": 0, 
+   "fieldname": "start_date", 
+   "fieldtype": "Date", 
+   "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": "Start Date", 
+   "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
+  }, 
+  {
+   "allow_bulk_edit": 0, 
+   "allow_in_quick_entry": 0, 
+   "allow_on_submit": 0, 
+   "bold": 0, 
+   "collapsible": 0, 
+   "columns": 0, 
+   "fieldname": "end_date", 
+   "fieldtype": "Date", 
+   "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": "End Date", 
+   "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
+  }, 
+  {
+   "allow_bulk_edit": 0, 
+   "allow_in_quick_entry": 0, 
+   "allow_on_submit": 0, 
+   "bold": 0, 
+   "collapsible": 0, 
+   "columns": 0, 
+   "fieldname": "section_break_5", 
+   "fieldtype": "Section Break", 
+   "hidden": 1, 
+   "ignore_user_permissions": 0, 
+   "ignore_xss_filter": 0, 
+   "in_filter": 0, 
+   "in_global_search": 0, 
+   "in_list_view": 0, 
+   "in_standard_filter": 0, 
+   "label": "Payroll Periods", 
+   "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
+  }, 
+  {
+   "allow_bulk_edit": 0, 
+   "allow_in_quick_entry": 0, 
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
@@ -71,7 +200,72 @@
    "read_only": 0, 
    "remember_last_selected_value": 0, 
    "report_hide": 0, 
-   "reqd": 1, 
+   "reqd": 0, 
+   "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, 
+   "fieldname": "section_break_7", 
+   "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": "Taxable Salary Slabs", 
+   "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
+  }, 
+  {
+   "allow_bulk_edit": 0, 
+   "allow_in_quick_entry": 0, 
+   "allow_on_submit": 0, 
+   "bold": 0, 
+   "collapsible": 0, 
+   "columns": 0, 
+   "fieldname": "taxable_salary_slabs", 
+   "fieldtype": "Table", 
+   "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": "Taxable Salary Slabs", 
+   "length": 0, 
+   "no_copy": 0, 
+   "options": "Taxable Salary Slab", 
+   "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, 
@@ -88,7 +282,7 @@
  "issingle": 0, 
  "istable": 0, 
  "max_attachments": 0, 
- "modified": "2018-04-13 19:38:28.013664", 
+ "modified": "2018-05-25 12:29:07.207927", 
  "modified_by": "Administrator", 
  "module": "HR", 
  "name": "Payroll Period", 
diff --git a/erpnext/hr/doctype/payroll_period/payroll_period.py b/erpnext/hr/doctype/payroll_period/payroll_period.py
index a818bc4..66d6a45 100644
--- a/erpnext/hr/doctype/payroll_period/payroll_period.py
+++ b/erpnext/hr/doctype/payroll_period/payroll_period.py
@@ -4,21 +4,54 @@
 
 from __future__ import unicode_literals
 import frappe
-from frappe.utils import date_diff, getdate
+from frappe import _
+from frappe.utils import date_diff, getdate, formatdate
 from frappe.model.document import Document
 
 class PayrollPeriod(Document):
-	pass
+	def validate(self):
+		self.validate_dates()
+		self.validate_overlap()
+
+	def validate_dates(self):
+		if getdate(self.start_date) > getdate(self.end_date):
+			frappe.throw(_("End date can not be less than start date"))
+
+	def validate_overlap(self):
+		query = """
+			select name
+			from `tab{0}`
+			where name != %(name)s
+			and company = %(company)s and (start_date between %(start_date)s and %(end_date)s \
+				or end_date between %(start_date)s and %(end_date)s \
+				or (start_date < %(start_date)s and end_date > %(end_date)s))
+			"""
+		if not self.name:
+			# hack! if name is null, it could cause problems with !=
+			self.name = "New "+self.doctype
+
+		overlap_doc = frappe.db.sql(query.format(self.doctype),{
+				"start_date": self.start_date,
+				"end_date": self.end_date,
+				"name": self.name,
+				"company": self.company
+			}, as_dict = 1)
+
+		if overlap_doc:
+			msg = _("A {0} exists between {1} and {2} (").format(self.doctype,
+				formatdate(self.start_date), formatdate(self.end_date)) \
+				+ """ <b><a href="#Form/{0}/{1}">{1}</a></b>""".format(self.doctype, overlap_doc[0].name) \
+				+ _(") for {0}").format(self.company)
+			frappe.throw(msg)
 
 def get_payroll_period_days(start_date, end_date, company):
 	payroll_period_dates = frappe.db.sql("""
-	select ppd.start_date, ppd.end_date from `tabPayroll Period Date` ppd, `tabPayroll Period` pp
-	where pp.company=%(company)s
-	and ppd.parent = pp.name
+	select start_date, end_date from `tabPayroll Period`
+	where company=%(company)s
 	and (
-		(%(start_date)s between ppd.start_date and ppd.end_date)
-		or (%(end_date)s between ppd.start_date and ppd.end_date)
-		or (ppd.start_date between %(start_date)s and %(end_date)s)
+		(%(start_date)s between start_date and end_date)
+		or (%(end_date)s between start_date and end_date)
+		or (start_date between %(start_date)s and %(end_date)s)
 	)""", {
 		'company': company,
 		'start_date': start_date,
diff --git a/erpnext/hr/doctype/salary_component/salary_component.js b/erpnext/hr/doctype/salary_component/salary_component.js
index e58a05e..0b8bd12 100644
--- a/erpnext/hr/doctype/salary_component/salary_component.js
+++ b/erpnext/hr/doctype/salary_component/salary_component.js
@@ -14,7 +14,7 @@
 				}
 			};
 		});
-		frm.set_query("earning_component_group", function(frm) {
+		frm.set_query("earning_component_group", function() {
 			return {
 				filters: {
 					"is_group": 1,
@@ -22,5 +22,40 @@
 				}
 			};
 		});
+	},
+	is_flexible_benefit: function(frm) {
+		if(frm.doc.is_flexible_benefit){
+			set_value_for_condition_and_formula(frm);
+		}
+	},
+	type: function(frm) {
+		if(frm.doc.type=="Earning"){
+			frm.set_value("variable_based_on_taxable_salary", 0);
+		}
+		if(frm.doc.type=="Deduction"){
+			frm.set_value("is_flexible_benefit", 0);
+		}
+	},
+	variable_based_on_taxable_salary: function(frm) {
+		if(frm.doc.variable_based_on_taxable_salary){
+			set_value_for_condition_and_formula(frm);
+		}
+	},
+	create_separate_payment_entry_against_benefit_claim: function(frm) {
+		if(frm.doc.create_separate_payment_entry_against_benefit_claim){
+			frm.set_df_property("accounts", "reqd", 1);
+		}
+		else{
+			frm.set_df_property("accounts", "reqd", 0);
+		}
 	}
 });
+
+var set_value_for_condition_and_formula = function(frm) {
+	frm.set_value("formula", null);
+	frm.set_value("condition", null);
+	frm.set_value("amount_based_on_formula", 0);
+	frm.set_value("statistical_component", 0);
+	frm.set_value("do_not_include_in_total", 0);
+	frm.set_value("depends_on_lwp", 0);
+};
diff --git a/erpnext/hr/doctype/salary_component/salary_component.json b/erpnext/hr/doctype/salary_component/salary_component.json
index 6764e0c..f22bfc3 100644
--- a/erpnext/hr/doctype/salary_component/salary_component.json
+++ b/erpnext/hr/doctype/salary_component/salary_component.json
@@ -14,6 +14,7 @@
  "fields": [
   {
    "allow_bulk_edit": 0, 
+   "allow_in_quick_entry": 0, 
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
@@ -45,6 +46,7 @@
   }, 
   {
    "allow_bulk_edit": 0, 
+   "allow_in_quick_entry": 0, 
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
@@ -78,6 +80,7 @@
   }, 
   {
    "allow_bulk_edit": 0, 
+   "allow_in_quick_entry": 0, 
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
@@ -110,6 +113,106 @@
   }, 
   {
    "allow_bulk_edit": 0, 
+   "allow_in_quick_entry": 0, 
+   "allow_on_submit": 0, 
+   "bold": 0, 
+   "collapsible": 0, 
+   "columns": 0, 
+   "depends_on": "eval:doc.type == \"Earning\"", 
+   "fieldname": "is_additional_component", 
+   "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": "Is Additional Component", 
+   "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
+  }, 
+  {
+   "allow_bulk_edit": 0, 
+   "allow_in_quick_entry": 0, 
+   "allow_on_submit": 0, 
+   "bold": 0, 
+   "collapsible": 0, 
+   "columns": 0, 
+   "depends_on": "eval:doc.type == \"Earning\"", 
+   "fieldname": "is_tax_applicable", 
+   "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": "Is Tax Applicable", 
+   "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
+  }, 
+  {
+   "allow_bulk_edit": 0, 
+   "allow_in_quick_entry": 0, 
+   "allow_on_submit": 0, 
+   "bold": 0, 
+   "collapsible": 0, 
+   "columns": 0, 
+   "default": "1", 
+   "fieldname": "is_payable", 
+   "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": "Is Payable", 
+   "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
+  }, 
+  {
+   "allow_bulk_edit": 0, 
+   "allow_in_quick_entry": 0, 
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
@@ -140,6 +243,7 @@
   }, 
   {
    "allow_bulk_edit": 0, 
+   "allow_in_quick_entry": 0, 
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
@@ -171,6 +275,7 @@
   }, 
   {
    "allow_bulk_edit": 0, 
+   "allow_in_quick_entry": 0, 
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
@@ -202,6 +307,7 @@
   }, 
   {
    "allow_bulk_edit": 0, 
+   "allow_in_quick_entry": 0, 
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
@@ -234,6 +340,7 @@
   }, 
   {
    "allow_bulk_edit": 0, 
+   "allow_in_quick_entry": 0, 
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
@@ -265,6 +372,7 @@
   }, 
   {
    "allow_bulk_edit": 0, 
+   "allow_in_quick_entry": 0, 
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
@@ -297,6 +405,7 @@
   }, 
   {
    "allow_bulk_edit": 0, 
+   "allow_in_quick_entry": 0, 
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
@@ -329,11 +438,12 @@
   }, 
   {
    "allow_bulk_edit": 0, 
+   "allow_in_quick_entry": 0, 
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
    "columns": 0, 
-   "depends_on": "eval:doc.is_flexible_benefit && !doc.is_group && !doc.flexi_default", 
+   "depends_on": "eval:doc.is_flexible_benefit && !doc.is_group", 
    "fieldname": "earning_component_group", 
    "fieldtype": "Link", 
    "hidden": 0, 
@@ -362,6 +472,7 @@
   }, 
   {
    "allow_bulk_edit": 0, 
+   "allow_in_quick_entry": 0, 
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
@@ -392,6 +503,7 @@
   }, 
   {
    "allow_bulk_edit": 0, 
+   "allow_in_quick_entry": 0, 
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
@@ -424,38 +536,7 @@
   }, 
   {
    "allow_bulk_edit": 0, 
-   "allow_on_submit": 0, 
-   "bold": 0, 
-   "collapsible": 0, 
-   "columns": 0, 
-   "depends_on": "eval:doc.is_flexible_benefit && doc.is_pro_rata_applicable", 
-   "fieldname": "flexi_default", 
-   "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": "Default Flexible Component", 
-   "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
-  }, 
-  {
-   "allow_bulk_edit": 0, 
+   "allow_in_quick_entry": 0, 
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
@@ -488,10 +569,12 @@
   }, 
   {
    "allow_bulk_edit": 0, 
+   "allow_in_quick_entry": 0, 
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
    "columns": 0, 
+   "depends_on": "is_flexible_benefit", 
    "fieldname": "create_separate_payment_entry_against_benefit_claim", 
    "fieldtype": "Check", 
    "hidden": 0, 
@@ -519,6 +602,7 @@
   }, 
   {
    "allow_bulk_edit": 0, 
+   "allow_in_quick_entry": 0, 
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
@@ -550,6 +634,7 @@
   }, 
   {
    "allow_bulk_edit": 0, 
+   "allow_in_quick_entry": 0, 
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
@@ -581,43 +666,12 @@
   }, 
   {
    "allow_bulk_edit": 0, 
+   "allow_in_quick_entry": 0, 
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
    "columns": 0, 
-   "depends_on": "variable_based_on_taxable_salary", 
-   "fieldname": "taxable_salary_slabs", 
-   "fieldtype": "Table", 
-   "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": "Taxable Salary Slabs", 
-   "length": 0, 
-   "no_copy": 0, 
-   "options": "Taxable Salary Slab", 
-   "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
-  }, 
-  {
-   "allow_bulk_edit": 0, 
-   "allow_on_submit": 0, 
-   "bold": 0, 
-   "collapsible": 0, 
-   "columns": 0, 
+   "depends_on": "eval:doc.is_payable == 1", 
    "fieldname": "section_break_5", 
    "fieldtype": "Section Break", 
    "hidden": 0, 
@@ -645,6 +699,7 @@
   }, 
   {
    "allow_bulk_edit": 0, 
+   "allow_in_quick_entry": 0, 
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
@@ -677,10 +732,13 @@
   }, 
   {
    "allow_bulk_edit": 0, 
+   "allow_in_quick_entry": 0, 
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 1, 
+   "collapsible_depends_on": "", 
    "columns": 0, 
+   "depends_on": "eval:doc.is_flexible_benefit != 1 && doc.variable_based_on_taxable_salary != 1", 
    "fieldname": "condition_and_formula", 
    "fieldtype": "Section Break", 
    "hidden": 0, 
@@ -708,6 +766,7 @@
   }, 
   {
    "allow_bulk_edit": 0, 
+   "allow_in_quick_entry": 0, 
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
@@ -739,6 +798,7 @@
   }, 
   {
    "allow_bulk_edit": 0, 
+   "allow_in_quick_entry": 0, 
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
@@ -771,6 +831,7 @@
   }, 
   {
    "allow_bulk_edit": 0, 
+   "allow_in_quick_entry": 0, 
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
@@ -802,6 +863,7 @@
   }, 
   {
    "allow_bulk_edit": 0, 
+   "allow_in_quick_entry": 0, 
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
@@ -833,6 +895,7 @@
   }, 
   {
    "allow_bulk_edit": 0, 
+   "allow_in_quick_entry": 0, 
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
@@ -865,6 +928,7 @@
   }, 
   {
    "allow_bulk_edit": 0, 
+   "allow_in_quick_entry": 0, 
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
@@ -897,6 +961,7 @@
   }, 
   {
    "allow_bulk_edit": 0, 
+   "allow_in_quick_entry": 0, 
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
@@ -929,6 +994,7 @@
   }, 
   {
    "allow_bulk_edit": 0, 
+   "allow_in_quick_entry": 0, 
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
@@ -959,6 +1025,7 @@
   }, 
   {
    "allow_bulk_edit": 0, 
+   "allow_in_quick_entry": 0, 
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
@@ -1001,7 +1068,7 @@
  "issingle": 0, 
  "istable": 0, 
  "max_attachments": 0, 
- "modified": "2018-05-16 12:27:03.005070", 
+ "modified": "2018-05-25 12:28:03.454487", 
  "modified_by": "Administrator", 
  "module": "HR", 
  "name": "Salary Component", 
@@ -1036,4 +1103,4 @@
  "sort_order": "DESC", 
  "track_changes": 0, 
  "track_seen": 0
-}
+}
\ No newline at end of file
diff --git a/erpnext/hr/doctype/salary_component/salary_component.py b/erpnext/hr/doctype/salary_component/salary_component.py
index 132930f..7c92631 100644
--- a/erpnext/hr/doctype/salary_component/salary_component.py
+++ b/erpnext/hr/doctype/salary_component/salary_component.py
@@ -3,28 +3,12 @@
 # For license information, please see license.txt
 
 from __future__ import unicode_literals
-import frappe
-from frappe import _
 from frappe.model.document import Document
 from frappe.model.naming import append_number_if_name_exists
 
 class SalaryComponent(Document):
 	def validate(self):
 		self.validate_abbr()
-		self.validate_flexi_default()
-
-	def validate_flexi_default(self):
-		if self.is_flexible_benefit and self.is_pro_rata_applicable and self.flexi_default:
-			salary_component = frappe.db.exists(
-				'Salary Component',
-				{
-					'is_flexible_benefit': 1,
-					'is_pro_rata_applicable': 1,
-					'flexi_default': 1
-				}
-			)
-			if salary_component and salary_component != self.name:
-				frappe.throw(_("{0} is already marked as default flexible component").format(salary_component))
 
 	def validate_abbr(self):
 		if not self.salary_component_abbr:
@@ -34,12 +18,3 @@
 		self.salary_component_abbr = self.salary_component_abbr.strip()
 		self.salary_component_abbr = append_number_if_name_exists('Salary Component', self.salary_component_abbr,
 			'salary_component_abbr', separator='_', filters={"name": ["!=", self.name]})
-
-	def calculate_tax(self, annual_earning):
-		taxable_amount = 0
-		for slab in self.taxable_salary_slabs:
-			if annual_earning > slab.from_amount and annual_earning < slab.to_amount:
-				taxable_amount += (annual_earning - slab.from_amount) * slab.percent_deduction *.01
-			elif annual_earning > slab.from_amount and annual_earning > slab.to_amount:
-				taxable_amount += (slab.to_amount - slab.from_amount) * slab.percent_deduction * .01
-		return taxable_amount
diff --git a/erpnext/hr/doctype/salary_detail/salary_detail.json b/erpnext/hr/doctype/salary_detail/salary_detail.json
index 82b0af2..a0d699a 100644
--- a/erpnext/hr/doctype/salary_detail/salary_detail.json
+++ b/erpnext/hr/doctype/salary_detail/salary_detail.json
@@ -238,6 +238,7 @@
    "bold": 0, 
    "collapsible": 0, 
    "columns": 0, 
+   "depends_on": "eval:doc.is_flexible_benefit != 1", 
    "fieldname": "section_break_2", 
    "fieldtype": "Section Break", 
    "hidden": 0, 
@@ -566,7 +567,7 @@
  "issingle": 0, 
  "istable": 1, 
  "max_attachments": 0, 
- "modified": "2018-05-17 12:43:22.606298",
+ "modified": "2018-05-22 15:11:02.341840", 
  "modified_by": "Administrator", 
  "module": "HR", 
  "name": "Salary Detail", 
@@ -581,4 +582,4 @@
  "sort_order": "DESC", 
  "track_changes": 0, 
  "track_seen": 0
-}
\ No newline at end of file
+}
diff --git a/erpnext/hr/doctype/salary_slip/salary_slip.json b/erpnext/hr/doctype/salary_slip/salary_slip.json
index e19f722..15a4653 100644
--- a/erpnext/hr/doctype/salary_slip/salary_slip.json
+++ b/erpnext/hr/doctype/salary_slip/salary_slip.json
@@ -14,6 +14,7 @@
  "fields": [
   {
    "allow_bulk_edit": 0, 
+   "allow_in_quick_entry": 0, 
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
@@ -46,6 +47,7 @@
   }, 
   {
    "allow_bulk_edit": 0, 
+   "allow_in_quick_entry": 0, 
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
@@ -79,11 +81,12 @@
   }, 
   {
    "allow_bulk_edit": 0, 
+   "allow_in_quick_entry": 0, 
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
    "columns": 0, 
-   "fetch_from": "employee.employee_name",
+   "fetch_from": "employee.employee_name", 
    "fieldname": "employee_name", 
    "fieldtype": "Read Only", 
    "hidden": 0, 
@@ -113,11 +116,12 @@
   }, 
   {
    "allow_bulk_edit": 0, 
+   "allow_in_quick_entry": 0, 
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
    "columns": 0, 
-   "fetch_from": "employee.department",
+   "fetch_from": "employee.department", 
    "fieldname": "department", 
    "fieldtype": "Link",
    "hidden": 0, 
@@ -132,7 +136,7 @@
    "no_copy": 0, 
    "oldfieldname": "department", 
    "oldfieldtype": "Link", 
-   "options": "Department",
+   "options": "Department", 
    "permlevel": 0, 
    "print_hide": 0, 
    "print_hide_if_no_value": 0, 
@@ -147,12 +151,13 @@
   }, 
   {
    "allow_bulk_edit": 0, 
+   "allow_in_quick_entry": 0, 
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
    "columns": 0, 
    "depends_on": "eval:doc.designation", 
-   "fetch_from": "employee.designation",
+   "fetch_from": "employee.designation", 
    "fieldname": "designation", 
    "fieldtype": "Read Only", 
    "hidden": 0, 
@@ -182,11 +187,12 @@
   }, 
   {
    "allow_bulk_edit": 0, 
+   "allow_in_quick_entry": 0, 
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
    "columns": 0, 
-   "fetch_from": "employee.branch",
+   "fetch_from": "employee.branch", 
    "fieldname": "branch", 
    "fieldtype": "Read Only", 
    "hidden": 0, 
@@ -216,6 +222,7 @@
   }, 
   {
    "allow_bulk_edit": 0, 
+   "allow_in_quick_entry": 0, 
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
@@ -247,6 +254,7 @@
   }, 
   {
    "allow_bulk_edit": 0, 
+   "allow_in_quick_entry": 0, 
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
@@ -279,6 +287,7 @@
   }, 
   {
    "allow_bulk_edit": 0, 
+   "allow_in_quick_entry": 0, 
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
@@ -311,6 +320,7 @@
   }, 
   {
    "allow_bulk_edit": 0, 
+   "allow_in_quick_entry": 0, 
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
@@ -342,6 +352,7 @@
   }, 
   {
    "allow_bulk_edit": 0, 
+   "allow_in_quick_entry": 0, 
    "allow_on_submit": 1, 
    "bold": 0, 
    "collapsible": 0, 
@@ -373,6 +384,7 @@
   }, 
   {
    "allow_bulk_edit": 0, 
+   "allow_in_quick_entry": 0, 
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
@@ -403,6 +415,7 @@
   }, 
   {
    "allow_bulk_edit": 0, 
+   "allow_in_quick_entry": 0, 
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
@@ -436,6 +449,7 @@
   }, 
   {
    "allow_bulk_edit": 0, 
+   "allow_in_quick_entry": 0, 
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
@@ -468,6 +482,7 @@
   }, 
   {
    "allow_bulk_edit": 0, 
+   "allow_in_quick_entry": 0, 
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
@@ -501,6 +516,7 @@
   }, 
   {
    "allow_bulk_edit": 0, 
+   "allow_in_quick_entry": 0, 
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
@@ -531,6 +547,7 @@
   }, 
   {
    "allow_bulk_edit": 0, 
+   "allow_in_quick_entry": 0, 
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
@@ -564,6 +581,7 @@
   }, 
   {
    "allow_bulk_edit": 0, 
+   "allow_in_quick_entry": 0, 
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
@@ -598,6 +616,7 @@
   }, 
   {
    "allow_bulk_edit": 0, 
+   "allow_in_quick_entry": 0, 
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
@@ -631,6 +650,7 @@
   }, 
   {
    "allow_bulk_edit": 0, 
+   "allow_in_quick_entry": 0, 
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
@@ -664,6 +684,7 @@
   }, 
   {
    "allow_bulk_edit": 0, 
+   "allow_in_quick_entry": 0, 
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
@@ -697,6 +718,7 @@
   }, 
   {
    "allow_bulk_edit": 0, 
+   "allow_in_quick_entry": 0, 
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
@@ -729,6 +751,7 @@
   }, 
   {
    "allow_bulk_edit": 0, 
+   "allow_in_quick_entry": 0, 
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
@@ -762,6 +785,7 @@
   }, 
   {
    "allow_bulk_edit": 0, 
+   "allow_in_quick_entry": 0, 
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
@@ -792,6 +816,7 @@
   }, 
   {
    "allow_bulk_edit": 0, 
+   "allow_in_quick_entry": 0, 
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
@@ -823,6 +848,7 @@
   }, 
   {
    "allow_bulk_edit": 0, 
+   "allow_in_quick_entry": 0, 
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
@@ -855,6 +881,7 @@
   }, 
   {
    "allow_bulk_edit": 0, 
+   "allow_in_quick_entry": 0, 
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
@@ -887,6 +914,7 @@
   }, 
   {
    "allow_bulk_edit": 0, 
+   "allow_in_quick_entry": 0, 
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
@@ -919,6 +947,7 @@
   }, 
   {
    "allow_bulk_edit": 0, 
+   "allow_in_quick_entry": 0, 
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
@@ -951,6 +980,7 @@
   }, 
   {
    "allow_bulk_edit": 0, 
+   "allow_in_quick_entry": 0, 
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
@@ -981,6 +1011,7 @@
   }, 
   {
    "allow_bulk_edit": 0, 
+   "allow_in_quick_entry": 0, 
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
@@ -1014,6 +1045,7 @@
   }, 
   {
    "allow_bulk_edit": 0, 
+   "allow_in_quick_entry": 0, 
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
@@ -1044,6 +1076,7 @@
   }, 
   {
    "allow_bulk_edit": 0, 
+   "allow_in_quick_entry": 0, 
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
@@ -1075,6 +1108,7 @@
   }, 
   {
    "allow_bulk_edit": 0, 
+   "allow_in_quick_entry": 0, 
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
@@ -1106,6 +1140,7 @@
   }, 
   {
    "allow_bulk_edit": 0, 
+   "allow_in_quick_entry": 0, 
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
@@ -1137,6 +1172,7 @@
   }, 
   {
    "allow_bulk_edit": 0, 
+   "allow_in_quick_entry": 0, 
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
@@ -1169,6 +1205,7 @@
   }, 
   {
    "allow_bulk_edit": 0, 
+   "allow_in_quick_entry": 0, 
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
@@ -1203,6 +1240,7 @@
   }, 
   {
    "allow_bulk_edit": 0, 
+   "allow_in_quick_entry": 0, 
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
@@ -1235,6 +1273,7 @@
   }, 
   {
    "allow_bulk_edit": 0, 
+   "allow_in_quick_entry": 0, 
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
@@ -1268,6 +1307,7 @@
   }, 
   {
    "allow_bulk_edit": 0, 
+   "allow_in_quick_entry": 0, 
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
@@ -1299,6 +1339,7 @@
   }, 
   {
    "allow_bulk_edit": 0, 
+   "allow_in_quick_entry": 0, 
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
@@ -1332,6 +1373,7 @@
   }, 
   {
    "allow_bulk_edit": 0, 
+   "allow_in_quick_entry": 0, 
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
@@ -1361,6 +1403,7 @@
   }, 
   {
    "allow_bulk_edit": 0, 
+   "allow_in_quick_entry": 0, 
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
@@ -1394,6 +1437,7 @@
   }, 
   {
    "allow_bulk_edit": 0, 
+   "allow_in_quick_entry": 0, 
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
@@ -1426,6 +1470,7 @@
   }, 
   {
    "allow_bulk_edit": 0, 
+   "allow_in_quick_entry": 0, 
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
@@ -1458,6 +1503,7 @@
   }, 
   {
    "allow_bulk_edit": 0, 
+   "allow_in_quick_entry": 0, 
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
@@ -1488,6 +1534,7 @@
   }, 
   {
    "allow_bulk_edit": 0, 
+   "allow_in_quick_entry": 0, 
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
@@ -1521,6 +1568,7 @@
   }, 
   {
    "allow_bulk_edit": 0, 
+   "allow_in_quick_entry": 0, 
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
@@ -1554,6 +1602,7 @@
   }, 
   {
    "allow_bulk_edit": 0, 
+   "allow_in_quick_entry": 0, 
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
@@ -1584,6 +1633,7 @@
   }, 
   {
    "allow_bulk_edit": 0, 
+   "allow_in_quick_entry": 0, 
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
@@ -1617,6 +1667,7 @@
   }, 
   {
    "allow_bulk_edit": 0, 
+   "allow_in_quick_entry": 0, 
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
@@ -1648,6 +1699,7 @@
   }, 
   {
    "allow_bulk_edit": 0, 
+   "allow_in_quick_entry": 0, 
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
@@ -1682,6 +1734,7 @@
   }, 
   {
    "allow_bulk_edit": 0, 
+   "allow_in_quick_entry": 0, 
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
@@ -1712,6 +1765,7 @@
   }, 
   {
    "allow_bulk_edit": 0, 
+   "allow_in_quick_entry": 0, 
    "allow_on_submit": 0, 
    "bold": 1, 
    "collapsible": 0, 
@@ -1743,6 +1797,7 @@
   }, 
   {
    "allow_bulk_edit": 0, 
+   "allow_in_quick_entry": 0, 
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
@@ -1773,6 +1828,7 @@
   }, 
   {
    "allow_bulk_edit": 0, 
+   "allow_in_quick_entry": 0, 
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
@@ -1816,7 +1872,7 @@
  "issingle": 0, 
  "istable": 0, 
  "max_attachments": 0, 
- "modified": "2018-05-17 10:30:09.007581",
+ "modified": "2018-05-25 12:33:27.147636", 
  "modified_by": "Administrator", 
  "module": "HR", 
  "name": "Salary Slip", 
@@ -1890,4 +1946,4 @@
  "title_field": "employee_name", 
  "track_changes": 0, 
  "track_seen": 0
-}
\ No newline at end of file
+}
diff --git a/erpnext/hr/doctype/salary_slip/salary_slip.py b/erpnext/hr/doctype/salary_slip/salary_slip.py
index f74ef00..bb55756 100644
--- a/erpnext/hr/doctype/salary_slip/salary_slip.py
+++ b/erpnext/hr/doctype/salary_slip/salary_slip.py
@@ -13,10 +13,9 @@
 from erpnext.utilities.transaction_base import TransactionBase
 from frappe.utils.background_jobs import enqueue
 from erpnext.hr.doctype.additional_salary.additional_salary import get_additional_salary_component
-from erpnext.hr.doctype.employee_benefit_application.employee_benefit_application import get_employee_benefit_application, get_amount
-from erpnext.hr.doctype.payroll_period.payroll_period import get_payroll_period_days
-from erpnext.hr.doctype.employee_benefit_claim.employee_benefit_claim import get_employee_benefit_claim
 from erpnext.hr.utils import get_payroll_period
+from erpnext.hr.doctype.employee_benefit_application.employee_benefit_application import get_benefit_component_amount
+from erpnext.hr.doctype.employee_benefit_claim.employee_benefit_claim import get_benefit_claim_amount
 
 class SalarySlip(TransactionBase):
 	def autoname(self):
@@ -62,6 +61,10 @@
 				amount = self.eval_condition_and_formula(struct_row, data)
 				if amount and struct_row.statistical_component == 0:
 					self.update_component_row(struct_row, amount, key)
+
+				if key=="earnings" and struct_row.is_flexible_benefit == 1:
+					self.add_employee_flexi_benefits(struct_row)
+
 				if key=="deductions" and struct_row.variable_based_on_taxable_salary:
 					tax_row, amount = self.calculate_pro_rata_tax(struct_row.salary_component)
 					if tax_row and amount:
@@ -71,58 +74,26 @@
 		if additional_components:
 			for additional_component in additional_components:
 				additional_component = frappe._dict(additional_component)
-				amount = self.update_amount_for_other_component(frappe._dict(additional_component.struct_row).salary_component, additional_component.amount)
+				amount = additional_component.amount + self.get_amount_from_exisiting_component(frappe._dict(additional_component.struct_row).salary_component)
 				self.update_component_row(frappe._dict(additional_component.struct_row), amount, "earnings")
 
-		max_benefits = self._salary_structure_doc.get("max_benefits")
-		if max_benefits > 0:
-			employee_benefits = get_employee_benefit_application(self)
-			if employee_benefits:
-				for employee_benefit in employee_benefits:
-					benefit_component = frappe._dict(employee_benefit)
-					amount = self.update_amount_for_other_component(frappe._dict(benefit_component.struct_row).salary_component, benefit_component.amount)
-					self.update_component_row(frappe._dict(benefit_component.struct_row), amount, "earnings")
-			else:
-				default_flexi_compenent = frappe.db.exists(
-					'Salary Component',
-					{
-						'is_flexible_benefit': 1,
-						'is_pro_rata_applicable': 1,
-						'flexi_default': 1
-					}
-				)
-				if default_flexi_compenent:
-					flexi_struct_row = self.create_flexi_struct_row(default_flexi_compenent)
-					payroll_period_days = get_payroll_period_days(self.start_date, self.end_date, self.company)
-					amount = self.update_amount_for_other_component(default_flexi_compenent, get_amount(payroll_period_days, self.start_date, self.end_date, max_benefits))
-					self.update_component_row(flexi_struct_row, amount, "earnings")
-				else:
-					frappe.throw(_("Configure default flexible benefit salary component for apply pro-rata benefit"))
+	def add_employee_flexi_benefits(self, struct_row):
+		if frappe.db.get_value("Salary Component", struct_row.salary_component, "is_pro_rata_applicable") == 1:
+			benefit_component_amount = get_benefit_component_amount(self.employee, self.start_date, self.end_date, struct_row, self._salary_structure_doc)
+			if benefit_component_amount:
+				self.update_component_row(struct_row, benefit_component_amount, "earnings")
+		else:
+			benefit_claim_amount = get_benefit_claim_amount(self.employee, self.start_date, self.end_date, struct_row)
+			if benefit_claim_amount:
+				self.update_component_row(struct_row, benefit_claim_amount, "earnings")
 
-			benefit_claims = get_employee_benefit_claim(self)
-			if benefit_claims:
-				for benefit_claim in benefit_claims:
-					benefit_component = frappe._dict(benefit_claim)
-					amount = self.update_amount_for_other_component(frappe._dict(benefit_component.struct_row).salary_component, benefit_component.amount)
-					self.update_component_row(frappe._dict(benefit_component.struct_row), amount, "earnings")
-
-	def update_amount_for_other_component(self, salary_component, new_amount):
-		amount = new_amount
+	def get_amount_from_exisiting_component(self, salary_component):
+		amount = 0
 		for d in self.get("earnings"):
 			if d.salary_component == salary_component:
-				d.amount += new_amount
 				amount = d.amount
 		return amount
 
-	def create_flexi_struct_row(self, default_flexi_compenent):
-		salary_component = frappe.get_doc("Salary Component", default_flexi_compenent)
-		flexi_struct_row = {}
-		flexi_struct_row['depends_on_lwp'] = salary_component.depends_on_lwp
-		flexi_struct_row['salary_component'] = salary_component.name
-		flexi_struct_row['abbr'] = salary_component.salary_component_abbr
-		flexi_struct_row['do_not_include_in_total'] = salary_component.do_not_include_in_total
-		return frappe._dict(flexi_struct_row)
-
 	def update_component_row(self, struct_row, amount, key):
 		component_row = None
 		for d in self.get(key):
diff --git a/erpnext/hr/doctype/salary_structure/salary_structure.js b/erpnext/hr/doctype/salary_structure/salary_structure.js
index 9e62b66..56f5992 100755
--- a/erpnext/hr/doctype/salary_structure/salary_structure.js
+++ b/erpnext/hr/doctype/salary_structure/salary_structure.js
@@ -19,7 +19,9 @@
 		frm.set_query("salary_component", "earnings", function() {
 			return {
 				filters: {
-					type: "earning"
+					type: "earning",
+					is_additional_component: 0,
+					is_group: 0
 				}
 			}
 		});
diff --git a/erpnext/hr/doctype/salary_structure/salary_structure.py b/erpnext/hr/doctype/salary_structure/salary_structure.py
index 23c57de..12e3445 100644
--- a/erpnext/hr/doctype/salary_structure/salary_structure.py
+++ b/erpnext/hr/doctype/salary_structure/salary_structure.py
@@ -13,6 +13,7 @@
 	def validate(self):
 		self.validate_amount()
 		self.strip_condition_and_formula_fields()
+		self.validate_max_benefits_with_flexi()
 
 	def validate_amount(self):
 		if flt(self.net_pay) < 0 and self.salary_slip_based_on_timesheet:
@@ -28,11 +29,29 @@
 			row.condition = row.condition.strip() if row.condition else ""
 			row.formula = row.formula.strip() if row.formula else ""
 
+	def validate_max_benefits_with_flexi(self):
+		have_a_flexi = False
+		if self.earnings:
+			flexi_amount = 0
+			for earning_component in self.earnings:
+				if earning_component.is_flexible_benefit == 1:
+					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 self.max_benefits == 0:
+				frappe.throw(_("Max benefits should be greater than zero to despense flexi"))
+			if self.max_benefits > flexi_amount:
+				frappe.throw(_("Total flexi component amount {0} should not be less \
+				than max benefits {1}").format(flexi_amount, self.max_benefits))
+		if not have_a_flexi and self.max_benefits > 0:
+			frappe.throw(_("Flexi component require to add max benefit"))
+
+
 @frappe.whitelist()
 def make_salary_slip(source_name, target_doc = None, employee = None, as_print = False, print_format = None):
 	def postprocess(source, target):
 		if employee:
-			employee_details = frappe.db.get_value("Employee", employee, 
+			employee_details = frappe.db.get_value("Employee", employee,
 				["employee_name", "branch", "designation", "department"], as_dict=1)
 			target.employee = employee
 			target.employee_name = employee_details.employee_name
@@ -62,4 +81,4 @@
 def get_employees(salary_structure):
 	employees = frappe.get_list('Salary Structure Assignment',
 		filters={'salary_structure': salary_structure}, fields=['employee'])
-	return list(set([d.employee for d in employees]))
\ No newline at end of file
+	return list(set([d.employee for d in employees]))
diff --git a/erpnext/hr/utils.py b/erpnext/hr/utils.py
index 4e937c6..a5819ff 100644
--- a/erpnext/hr/utils.py
+++ b/erpnext/hr/utils.py
@@ -236,10 +236,9 @@
 		return leave_period
 
 def get_payroll_period(from_date, to_date, company):
-	payroll_period = frappe.db.sql("""select pp.name, pd.start_date, pd.end_date from
-		`tabPayroll Period Date` pd join `tabPayroll Period` pp on
-		pd.parent=pp.name where pd.start_date<=%s and pd.end_date>= %s
-		and pp.company=%s""", (from_date, to_date, company), as_dict=1)
+	payroll_period = frappe.db.sql("""select name, start_date, end_date from
+		`tabPayroll Period`
+		where start_date<=%s and end_date>= %s and company=%s""", (from_date, to_date, company), as_dict=1)
 	return payroll_period[0] if payroll_period else None