Merge pull request #6683 from KanchanChauhan/multiple-active-salary-slio

Warning if there are mutliple active salary structures
diff --git a/erpnext/hr/doctype/leave_application/leave_application.py b/erpnext/hr/doctype/leave_application/leave_application.py
index c1a9a06..a51e2b5 100755
--- a/erpnext/hr/doctype/leave_application/leave_application.py
+++ b/erpnext/hr/doctype/leave_application/leave_application.py
@@ -193,7 +193,7 @@
 
 	def validate_max_days(self):
 		max_days = frappe.db.get_value("Leave Type", self.leave_type, "max_days_allowed")
-		if max_days and self.total_leave_days > max_days:
+		if max_days and self.total_leave_days > cint(max_days):
 			frappe.throw(_("Leave of type {0} cannot be longer than {1}").format(self.leave_type, max_days))
 
 	def validate_leave_approver(self):
@@ -279,7 +279,7 @@
 
 @frappe.whitelist()
 def get_number_of_leave_days(employee, leave_type, from_date, to_date, half_day=None):
-	if half_day:
+	if half_day==1:
 		return 0.5
 	number_of_days = date_diff(to_date, from_date) + 1
 	if not frappe.db.get_value("Leave Type", leave_type, "include_holiday"):
diff --git a/erpnext/hr/doctype/process_payroll/process_payroll.json b/erpnext/hr/doctype/process_payroll/process_payroll.json
index 13d3191..546da97 100644
--- a/erpnext/hr/doctype/process_payroll/process_payroll.json
+++ b/erpnext/hr/doctype/process_payroll/process_payroll.json
@@ -609,7 +609,7 @@
    "bold": 0, 
    "collapsible": 0, 
    "columns": 0, 
-   "depends_on": "payment_account", 
+   "depends_on": "eval:doc.payment_account", 
    "description": "Create Bank Entry for the total salary paid for the above selected criteria", 
    "fieldname": "make_bank_entry", 
    "fieldtype": "Button", 
@@ -692,7 +692,7 @@
  "issingle": 1, 
  "istable": 0, 
  "max_attachments": 0, 
- "modified": "2016-09-28 05:43:26.472928", 
+ "modified": "2016-10-22 08:10:44.293748", 
  "modified_by": "Administrator", 
  "module": "HR", 
  "name": "Process Payroll", 
@@ -708,6 +708,7 @@
    "export": 0, 
    "if_owner": 0, 
    "import": 0, 
+   "is_custom": 0, 
    "permlevel": 0, 
    "print": 0, 
    "read": 1, 
diff --git a/erpnext/hr/doctype/salary_slip/salary_slip.py b/erpnext/hr/doctype/salary_slip/salary_slip.py
index 35564ce..e101c3d 100644
--- a/erpnext/hr/doctype/salary_slip/salary_slip.py
+++ b/erpnext/hr/doctype/salary_slip/salary_slip.py
@@ -277,24 +277,26 @@
 		holidays = [cstr(i) for i in holidays]
 
 		return holidays
-
+	
 	def calculate_lwp(self, holidays, working_days):
 		lwp = 0
+		holidays = "','".join(holidays)
 		for d in range(working_days):
 			dt = add_days(cstr(getdate(self.start_date)), d)
-			if dt not in holidays:
-				leave = frappe.db.sql("""
-					select t1.name, t1.half_day
-					from `tabLeave Application` t1, `tabLeave Type` t2
-					where t2.name = t1.leave_type
-					and t2.is_lwp = 1
-					and t1.docstatus = 1
-					and t1.employee = %s
-					and %s between from_date and to_date
-				""", (self.employee, dt))
-				if leave:
-					lwp = cint(leave[0][1]) and (lwp + 0.5) or (lwp + 1)
-		return lwp
+			leave = frappe.db.sql("""
+				select t1.name, t1.half_day
+				from `tabLeave Application` t1, `tabLeave Type` t2
+				where t2.name = t1.leave_type
+				and t2.is_lwp = 1
+				and t1.docstatus = 1
+				and t1.employee = %(employee)s
+				and CASE WHEN t2.include_holiday != 1 THEN %(dt)s not in ('{0}') and %(dt)s between from_date and to_date
+				WHEN t2.include_holiday THEN %(dt)s between from_date and to_date
+				END
+				""".format(holidays), {"employee": self.employee, "dt": dt})
+			if leave:
+				lwp = cint(leave[0][1]) and (lwp + 0.5) or (lwp + 1)
+		return lwp	
 
 	def check_existing(self):
 		if not self.salary_slip_based_on_timesheet:
diff --git a/erpnext/hr/doctype/salary_structure/salary_structure.js b/erpnext/hr/doctype/salary_structure/salary_structure.js
index d3f362e..334e8a5 100755
--- a/erpnext/hr/doctype/salary_structure/salary_structure.js
+++ b/erpnext/hr/doctype/salary_structure/salary_structure.js
@@ -21,7 +21,7 @@
 					type: "earning"
 				}
 			}
-		})
+		});
 		frm.set_query("salary_component", "deductions", function() {
 			return {
 				filters: {
@@ -32,14 +32,68 @@
 	},
 	
 	refresh: function(frm) {
-		frm.trigger("toggle_fields")
+		frm.trigger("toggle_fields");
 		frm.fields_dict['earnings'].grid.set_column_disp("default_amount", false);
 		frm.fields_dict['deductions'].grid.set_column_disp("default_amount", false);
 		
 		frm.add_custom_button(__("Preview Salary Slip"),
 			function() { frm.trigger('preview_salary_slip'); }, "icon-sitemap", "btn-default");
+
+		frm.add_custom_button(__("Add Employees"),function () {
+			frm.trigger('add_employees')
+		})
 		
-	},	
+	},
+
+	add_employees:function (frm) {
+		frm.$emp_dialog = new frappe.ui.Dialog({
+			title: __("Add Employees"),
+			fields: [
+				{fieldname:'company', fieldtype:'Link', options: 'Company', label: __('Company')},
+				{fieldname:'branch', fieldtype:'Link', options: 'Branch', label: __('Branch')},
+				{fieldname:'department', fieldtype:'Link', options: 'Department', label: __('Department')},
+				{fieldname:'designation', fieldtype:'Link', options: 'Designation', label: __('Designation')},
+				{fieldname:'base_variable', fieldtype:'Section Break'},
+				{fieldname:'base', fieldtype:'Currency', label: __('Base')},
+				{fieldname:'base_col_br', fieldtype:'Column Break'},
+				{fieldname:'variable', fieldtype:'Currency', label: __('Variable')}
+			]
+		});
+		frm.$emp_dialog.set_primary_action(__("Add"), function() {
+			frm.trigger('get_employees');
+		});
+		frm.$emp_dialog.show();
+	},
+
+	get_employees:function (frm) {
+		var filters = frm.$emp_dialog.get_values();
+		if ('variable' in filters) {
+			delete filters.variable
+		}
+		if ('base' in filters) {
+			delete filters.base
+		}
+		frappe.call({
+			method:'erpnext.hr.doctype.salary_structure.salary_structure.get_employees',
+			args:{
+				filters: filters
+			},
+			callback:function (r) {
+				var employees = $.map(frm.doc.employees, function(d) { return d.employee });
+				for (var i=0; i< r.message.length; i++) {
+					if (employees.indexOf(r.message[i].name) === -1) {
+						var row = frappe.model.add_child(frm.doc, frm.fields_dict.employees.df.options, frm.fields_dict.employees.df.fieldname);
+						row.employee = r.message[i].name;
+						row.employee_name = r.message[i].employee_name;
+						row.base = frm.$emp_dialog.get_value('base');
+						row.variable = frm.$emp_dialog.get_value('variable');
+					}
+				}
+				frm.refresh_field('employees');
+				frm.$emp_dialog.hide()
+			}
+		})
+	},
 
 	salary_slip_based_on_timesheet: function(frm) {
 		frm.trigger("toggle_fields")
@@ -81,12 +135,12 @@
 		frm.toggle_display(['salary_component', 'hour_rate'], frm.doc.salary_slip_based_on_timesheet);
 		frm.toggle_reqd(['salary_component', 'hour_rate'], frm.doc.salary_slip_based_on_timesheet);
 	}
-})
+});
 
 
 cur_frm.cscript.amount = function(doc, cdt, cdn){
 	calculate_totals(doc, cdt, cdn);
-}
+};
 
 var calculate_totals = function(doc) {
 	var tbl1 = doc.earnings || [];
diff --git a/erpnext/hr/doctype/salary_structure/salary_structure.py b/erpnext/hr/doctype/salary_structure/salary_structure.py
index d4bc6e3..13622c3 100644
--- a/erpnext/hr/doctype/salary_structure/salary_structure.py
+++ b/erpnext/hr/doctype/salary_structure/salary_structure.py
@@ -58,4 +58,9 @@
 		doc.name = 'Preview for {0}'.format(employee)
 		return frappe.get_print(doc.doctype, doc.name, doc = doc, print_format = print_format)
 	else:
-		return doc
\ No newline at end of file
+		return doc
+
+
+@frappe.whitelist()
+def get_employees(**args):
+	return frappe.get_list('Employee',filters=args['filters'], fields=['name', 'employee_name'])
diff --git a/erpnext/setup/doctype/company/delete_company_transactions.py b/erpnext/setup/doctype/company/delete_company_transactions.py
index 8f058e8..eb5c043 100644
--- a/erpnext/setup/doctype/company/delete_company_transactions.py
+++ b/erpnext/setup/doctype/company/delete_company_transactions.py
@@ -14,7 +14,7 @@
 	doc = frappe.get_doc("Company", company_name)
 
 	if frappe.session.user != doc.owner:
-		frappe.throw(_("Transactions can only be deleted by the creator of the Company"), 
+		frappe.throw(_("Transactions can only be deleted by the creator of the Company"),
 			frappe.PermissionError)
 
 	delete_bins(company_name)
@@ -37,6 +37,9 @@
 
 	if not meta.issingle:
 		if not meta.istable:
+			# delete communication
+			delete_communications(doctype, company_name, company_fieldname)
+
 			# delete children
 			for df in meta.get_table_fields():
 				frappe.db.sql("""delete from `tab{0}` where parent in
@@ -64,7 +67,6 @@
 					frappe.db.sql("""update tabSeries set current = %s
 						where name=%s""", (last, prefix))
 
-
 def delete_bins(company_name):
 	frappe.db.sql("""delete from tabBin where warehouse in
 			(select name from tabWarehouse where company=%s)""", company_name)
@@ -76,3 +78,9 @@
 			where lead=%s and (customer='' or customer is null) and (supplier='' or supplier is null)""", lead.name)
 
 		frappe.db.sql("""update `tabAddress` set lead=null, lead_name=null where lead=%s""", lead.name)
+
+def delete_communications(doctype, company_name, company_fieldname):
+		frappe.db.sql("""
+			DELETE FROM `tabCommunication` WHERE reference_doctype = %s AND
+			EXISTS (SELECT name FROM `tab{0}` WHERE {1} = %s AND `tabCommunication`.reference_name = name)
+			""".format(doctype, company_fieldname), (doctype, company_name))
diff --git a/erpnext/stock/doctype/material_request/material_request.js b/erpnext/stock/doctype/material_request/material_request.js
index 78f17d3..44b7dea 100644
--- a/erpnext/stock/doctype/material_request/material_request.js
+++ b/erpnext/stock/doctype/material_request/material_request.js
@@ -7,7 +7,13 @@
 	onload: function(frm) {
 		// formatter for material request item
 		frm.set_indicator_formatter('item_code',
-			function(doc) { return (doc.qty<=doc.ordered_qty) ? "green" : "orange" })
+			function(doc) { return (doc.qty<=doc.ordered_qty) ? "green" : "orange" }),
+
+		frm.fields_dict["items"].grid.get_field("warehouse").get_query = function(doc, cdt, cdn){
+			return{
+				filters: {'company': doc.company}
+			}
+		}
 	}
 });