fix: filters not working in Shift Assignment Calendar view (#30822)

This commit is contained in:
Rucha Mahabal
2022-04-27 14:44:25 +05:30
committed by GitHub
parent f693d43cda
commit 3cdbb65b5a
2 changed files with 54 additions and 11 deletions

View File

@@ -84,7 +84,7 @@ class ShiftAssignment(Document):
@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

View File

@@ -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 @@ class TestShiftAssignment(unittest.TestCase):
)
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)