From 3cdbb65b5a886550411659e3cac05287cb090293 Mon Sep 17 00:00:00 2001 From: Rucha Mahabal Date: Wed, 27 Apr 2022 14:44:25 +0530 Subject: [PATCH] fix: filters not working in Shift Assignment Calendar view (#30822) --- .../shift_assignment/shift_assignment.py | 20 +++++---- .../shift_assignment/test_shift_assignment.py | 45 ++++++++++++++++++- 2 files changed, 54 insertions(+), 11 deletions(-) diff --git a/erpnext/hr/doctype/shift_assignment/shift_assignment.py b/erpnext/hr/doctype/shift_assignment/shift_assignment.py index 5a1248698c23..868be6ef7193 100644 --- a/erpnext/hr/doctype/shift_assignment/shift_assignment.py +++ b/erpnext/hr/doctype/shift_assignment/shift_assignment.py @@ -84,7 +84,7 @@ def throw_overlap_error(self, shift_details): @frappe.whitelist() def get_events(start, end, filters=None): - events = [] + from frappe.desk.calendar import get_event_conditions employee = frappe.db.get_value( "Employee", {"user_id": frappe.session.user}, ["name", "company"], as_dict=True @@ -95,20 +95,22 @@ def get_events(start, end, filters=None): employee = "" company = frappe.db.get_value("Global Defaults", None, "default_company") - from frappe.desk.reportview import get_filters_cond - - conditions = get_filters_cond("Shift Assignment", filters, []) - add_assignments(events, start, end, conditions=conditions) + conditions = get_event_conditions("Shift Assignment", filters) + events = add_assignments(start, end, conditions=conditions) return events -def add_assignments(events, start, end, conditions=None): +def add_assignments(start, end, conditions=None): + events = [] + query = """select name, start_date, end_date, employee_name, employee, docstatus, shift_type from `tabShift Assignment` where - start_date >= %(start_date)s - or end_date <= %(end_date)s - or (%(start_date)s between start_date and end_date and %(end_date)s between start_date and end_date) + ( + start_date >= %(start_date)s + or end_date <= %(end_date)s + or (%(start_date)s between start_date and end_date and %(end_date)s between start_date and end_date) + ) and docstatus = 1""" if conditions: query += conditions diff --git a/erpnext/hr/doctype/shift_assignment/test_shift_assignment.py b/erpnext/hr/doctype/shift_assignment/test_shift_assignment.py index 4a1ec293bded..ea37424762b1 100644 --- a/erpnext/hr/doctype/shift_assignment/test_shift_assignment.py +++ b/erpnext/hr/doctype/shift_assignment/test_shift_assignment.py @@ -4,14 +4,22 @@ import unittest import frappe +from frappe.tests.utils import FrappeTestCase from frappe.utils import add_days, nowdate +from erpnext.hr.doctype.employee.test_employee import make_employee +from erpnext.hr.doctype.shift_assignment.shift_assignment import get_events + test_dependencies = ["Shift Type"] -class TestShiftAssignment(unittest.TestCase): +class TestShiftAssignment(FrappeTestCase): def setUp(self): - frappe.db.sql("delete from `tabShift Assignment`") + frappe.db.delete("Shift Assignment") + if not frappe.db.exists("Shift Type", "Day Shift"): + frappe.get_doc( + {"doctype": "Shift Type", "name": "Day Shift", "start_time": "9:00:00", "end_time": "18:00:00"} + ).insert() def test_make_shift_assignment(self): shift_assignment = frappe.get_doc( @@ -86,3 +94,36 @@ def test_overlapping_for_fixed_period_shift(self): ) self.assertRaises(frappe.ValidationError, shift_assignment_3.save) + + def test_shift_assignment_calendar(self): + employee1 = make_employee("test_shift_assignment1@example.com", company="_Test Company") + employee2 = make_employee("test_shift_assignment2@example.com", company="_Test Company") + date = nowdate() + + shift_1 = frappe.get_doc( + { + "doctype": "Shift Assignment", + "shift_type": "Day Shift", + "company": "_Test Company", + "employee": employee1, + "start_date": date, + "status": "Active", + } + ).submit() + + frappe.get_doc( + { + "doctype": "Shift Assignment", + "shift_type": "Day Shift", + "company": "_Test Company", + "employee": employee2, + "start_date": date, + "status": "Active", + } + ).submit() + + events = get_events( + start=date, end=date, filters=[["Shift Assignment", "employee", "=", employee1, False]] + ) + self.assertEqual(len(events), 1) + self.assertEqual(events[0]["name"], shift_1.name)