Skip to content

Commit

Permalink
fix: filters not working in Shift Assignment Calendar view (frappe#30822
Browse files Browse the repository at this point in the history
)
  • Loading branch information
ruchamahabal authored Apr 27, 2022
1 parent f693d43 commit 3cdbb65
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 11 deletions.
20 changes: 11 additions & 9 deletions erpnext/hr/doctype/shift_assignment/shift_assignment.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
45 changes: 43 additions & 2 deletions erpnext/hr/doctype/shift_assignment/test_shift_assignment.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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)

0 comments on commit 3cdbb65

Please sign in to comment.