fix: fetch query for employee (#24529)

diff --git a/erpnext/payroll/doctype/payroll_entry/payroll_entry.js b/erpnext/payroll/doctype/payroll_entry/payroll_entry.js
index 94f3f30..0dcea88 100644
--- a/erpnext/payroll/doctype/payroll_entry/payroll_entry.js
+++ b/erpnext/payroll/doctype/payroll_entry/payroll_entry.js
@@ -133,6 +133,31 @@
 				}
 			};
 		});
+
+		frm.set_query('employee', 'employees', () => {
+			if (!frm.doc.company) {
+				frappe.msgprint(__("Please set a Company"));
+				return []
+			}
+			let filters = {};
+			filters['company'] = frm.doc.company;
+			filters['start_date'] = frm.doc.start_date;
+			filters['end_date'] = frm.doc.end_date;
+
+			if (frm.doc.department) {
+				filters['department'] = frm.doc.department;
+			}
+			if (frm.doc.branch) {
+				filters['branch'] = frm.doc.branch;
+			}
+			if (frm.doc.designation) {
+				filters['designation'] = frm.doc.designation;
+			}
+			return {
+				query: "erpnext.payroll.doctype.payroll_entry.payroll_entry.employee_query",
+				filters: filters
+			}
+		});
 	},
 
 	payroll_frequency: function (frm) {
@@ -141,40 +166,8 @@
 		});
 	},
 
-	employee_filters: function (frm, emp_list) {
-		frm.set_query('employee', 'employees', () => {
-			return {
-				filters: {
-					name: ["not in", emp_list],
-					company: frm.doc.company
-				}
-			};
-		});
-	},
-
-	get_employee_with_salary_slip_and_set_query: function (frm) {
-		if (!frm.doc.company) {
-			frappe.throw(__("Please set a Company"));
-		}
-		frappe.db.get_list('Salary Slip', {
-			filters: {
-				start_date: frm.doc.start_date,
-				end_date: frm.doc.end_date,
-				docstatus: 1,
-			},
-			fields: ['employee']
-		}).then((emp) => {
-			var emp_list = [];
-			emp.forEach((employee_data) => {
-				emp_list.push(Object.values(employee_data)[0]);
-			});
-			frm.events.employee_filters(frm, emp_list);
-		});
-	},
-
 	company: function (frm) {
 		frm.events.clear_employee_table(frm);
-		frm.events.get_employee_with_salary_slip_and_set_query(frm);
 		erpnext.accounts.dimensions.update_dimension(frm, frm.doctype);
 	},
 
@@ -349,14 +342,3 @@
 		})
 	);
 };
-
-frappe.ui.form.on('Payroll Employee Detail', {
-	employee: function(frm) {
-		if (!frm.doc.company) {
-			frappe.throw(__("Please set a Company"));
-		}
-		if (!frm.doc.payroll_frequency) {
-			frappe.throw(__("Please set a Payroll Frequency"));
-		}
-	}
-});
diff --git a/erpnext/payroll/doctype/payroll_entry/payroll_entry.py b/erpnext/payroll/doctype/payroll_entry/payroll_entry.py
index 1ed3332..b520cda 100644
--- a/erpnext/payroll/doctype/payroll_entry/payroll_entry.py
+++ b/erpnext/payroll/doctype/payroll_entry/payroll_entry.py
@@ -10,6 +10,7 @@
 from frappe import _
 from erpnext.accounts.utils import get_fiscal_year
 from erpnext.hr.doctype.employee.employee import get_holiday_list_for_employee
+from frappe.desk.reportview import get_match_cond, get_filters_cond
 
 class PayrollEntry(Document):
 	def onload(self):
@@ -632,3 +633,57 @@
 			'txt': "%%%s%%" % frappe.db.escape(txt),
 			'start': start, 'page_len': page_len
 		})
+
+def get_employee_with_existing_salary_slip(start_date, end_date):
+
+	return frappe.db.sql_list("""
+		select employee from `tabSalary Slip` 
+		where 
+			(start_date between %(start_date)s and %(end_date)s 
+		or 
+			end_date between %(start_date)s and %(end_date)s 
+		or 
+			%(start_date)s between start_date and end_date)
+		and docstatus = 1
+	""", {'start_date': start_date, 'end_date': end_date})
+
+@frappe.whitelist()
+@frappe.validate_and_sanitize_search_inputs
+def employee_query(doctype, txt, searchfield, start, page_len, filters):
+	filters = frappe._dict(filters)
+	conditions = []
+	emp_cond = ''
+	if filters.start_date and filters.end_date:
+		employee_list = get_employee_with_existing_salary_slip(filters.start_date, filters.end_date)
+		filters.pop('start_date')
+		filters.pop('end_date')
+		if employee_list:
+			emp_cond += 'and employee not in %(employee_list)s'
+	else:
+		employee_list = []
+	
+
+	return frappe.db.sql("""select name, employee_name from `tabEmployee`
+		where status = 'Active'
+			and docstatus < 2
+			and ({key} like %(txt)s
+				or employee_name like %(txt)s)
+			{emp_cond}
+			{fcond} {mcond}
+		order by
+			if(locate(%(_txt)s, name), locate(%(_txt)s, name), 99999),
+			if(locate(%(_txt)s, employee_name), locate(%(_txt)s, employee_name), 99999),
+			idx desc,
+			name, employee_name
+		limit %(start)s, %(page_len)s""".format(**{
+			'key': searchfield,
+			'fcond': get_filters_cond(doctype, filters, conditions),
+			'mcond': get_match_cond(doctype),
+			'emp_cond': emp_cond
+		}), {
+			'txt': "%%%s%%" % txt,
+			'_txt': txt.replace("%", ""),
+			'start': start,
+			'page_len': page_len,
+			'employee_list': employee_list
+		})