Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: rewrite attendance query in qb & add test for half-day attendance overwritten by leave application #1205

Merged
merged 2 commits into from
Dec 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 18 additions & 12 deletions hrms/hr/doctype/attendance/attendance.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,18 +145,24 @@ def validate_employee_status(self):
frappe.throw(_("Cannot mark attendance for an Inactive employee {0}").format(self.employee))

def check_leave_record(self):
leave_record = frappe.db.sql(
"""
select leave_type, half_day, half_day_date, name
from `tabLeave Application`
where employee = %s
and %s between from_date and to_date
and status = 'Approved'
and docstatus = 1
""",
(self.employee, self.attendance_date),
as_dict=True,
)
LeaveApplication = frappe.qb.DocType("Leave Application")
leave_record = (
frappe.qb.from_(LeaveApplication)
.select(
LeaveApplication.leave_type,
LeaveApplication.half_day,
LeaveApplication.half_day_date,
LeaveApplication.name,
)
.where(
(LeaveApplication.employee == self.employee)
& (self.attendance_date >= LeaveApplication.from_date)
& (self.attendance_date <= LeaveApplication.to_date)
& (LeaveApplication.status == "Approved")
& (LeaveApplication.docstatus == 1)
)
).run(as_dict=True)

if leave_record:
for d in leave_record:
self.leave_type = d.leave_type
Expand Down
24 changes: 24 additions & 0 deletions hrms/hr/doctype/leave_application/test_leave_application.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from erpnext.setup.doctype.employee.test_employee import make_employee
from erpnext.setup.doctype.holiday_list.test_holiday_list import set_holiday_list

from hrms.hr.doctype.attendance.attendance import mark_attendance
from hrms.hr.doctype.leave_allocation.test_leave_allocation import create_leave_allocation
from hrms.hr.doctype.leave_application.leave_application import (
InsufficientLeaveBalanceError,
Expand Down Expand Up @@ -306,6 +307,29 @@ def test_overwrite_attendance(self):
for d in ("2018-01-01", "2018-01-02", "2018-01-03"):
self.assertTrue(getdate(d) in dates)

def test_overwrite_half_day_attendance(self):
mark_attendance("_T-Employee-00001", "2023-01-02", "Absent")

make_allocation_record(from_date="2023-01-01", to_date="2023-12-31")
application = self.get_application(_test_records[0])
application.status = "Approved"
application.from_date = "2023-01-02"
application.to_date = "2023-01-02"
application.half_day = 1
application.half_day_date = "2023-01-02"
application.submit()

attendance = frappe.db.get_value(
"Attendance",
{"attendance_date": "2023-01-02"},
["status", "leave_type", "leave_application"],
as_dict=True,
)

self.assertEqual(attendance.status, "Half Day")
self.assertEqual(attendance.leave_type, "_Test Leave Type")
self.assertEqual(attendance.leave_application, application.name)

@set_holiday_list("Salary Slip Test Holiday List", "_Test Company")
def test_attendance_for_include_holidays(self):
# Case 1: leave type with 'Include holidays within leaves as leaves' enabled
Expand Down
Loading