Skip to content

Commit

Permalink
fix: compensatory leave request for Half Day & WFH (backport #906) (#907
Browse files Browse the repository at this point in the history
)

Co-authored-by: Rucha Mahabal <[email protected]>
  • Loading branch information
mergify[bot] and ruchamahabal authored Sep 22, 2023
1 parent 5eccb33 commit 037d12e
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,27 @@ def validate(self):
frappe.throw(_("Leave Type is madatory"))

def validate_attendance(self):
attendance = frappe.get_all(
attendance_records = frappe.get_all(
"Attendance",
filters={
"attendance_date": ["between", (self.work_from_date, self.work_end_date)],
"status": "Present",
"status": ("in", ["Present", "Work From Home", "Half Day"]),
"docstatus": 1,
"employee": self.employee,
},
fields=["attendance_date", "status"],
)

if len(attendance) < date_diff(self.work_end_date, self.work_from_date) + 1:
half_days = [entry.attendance_date for entry in attendance_records if entry.status == "Half Day"]

if half_days and (not self.half_day or getdate(self.half_day_date) not in half_days):
frappe.throw(
_(
"You were only present for Half Day on {}. Cannot apply for a full day compensatory leave"
).format(", ".join([frappe.bold(format_date(half_day)) for half_day in half_days]))
)

if len(attendance_records) < date_diff(self.work_end_date, self.work_from_date) + 1:
frappe.throw(_("You are not present all day(s) between compensatory leave request days"))

def validate_holidays(self):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,12 @@

class TestCompensatoryLeaveRequest(FrappeTestCase):
def setUp(self):
frappe.db.sql(""" delete from `tabCompensatory Leave Request`""")
frappe.db.sql(""" delete from `tabLeave Ledger Entry`""")
frappe.db.sql(""" delete from `tabLeave Allocation`""")
frappe.db.sql(
""" delete from `tabAttendance` where attendance_date in {0} """.format(
(today(), add_days(today(), -1))
)
) # nosec
frappe.db.delete("Compensatory Leave Request")
frappe.db.delete("Leave Ledger Entry")
frappe.db.delete("Leave Allocation")
frappe.db.delete("Attendance")
frappe.db.delete("Leave Period")

create_leave_period(add_months(today(), -3), add_months(today(), 3), "_Test Company")
create_holiday_list()

Expand Down Expand Up @@ -99,6 +97,37 @@ def test_creation_of_leave_ledger_entry_on_submit(self):
self.assertEqual(leave_ledger_entry[0].leave_type, compensatory_leave_request.leave_type)
self.assertEqual(leave_ledger_entry[0].leaves, -1)

def test_half_day_compensatory_leave(self):
employee = get_employee()
mark_attendance(employee, status="Half Day")
date = today()
compensatory_leave_request = frappe.new_doc("Compensatory Leave Request")
compensatory_leave_request.update(
dict(
employee=employee.name,
leave_type="Compensatory Off",
work_from_date=date,
work_end_date=date,
reason="test",
)
)

# cannot apply for full day compensatory leave for a half day attendance
self.assertRaises(frappe.ValidationError, compensatory_leave_request.submit)

compensatory_leave_request.half_day = 1
compensatory_leave_request.half_day_date = date
compensatory_leave_request.submit()

# check creation of leave ledger entry on submission of leave request
leave_ledger_entry = frappe.get_all(
"Leave Ledger Entry",
fields="*",
filters={"transaction_name": compensatory_leave_request.leave_allocation},
)

self.assertEqual(leave_ledger_entry[0].leaves, 0.5)


def get_compensatory_leave_request(employee, leave_date=today()):
prev_comp_leave_req = frappe.db.get_value(
Expand Down

0 comments on commit 037d12e

Please sign in to comment.