Skip to content

Commit

Permalink
test(Leave Application): attendance update for including and excludin…
Browse files Browse the repository at this point in the history
…g holidays
  • Loading branch information
ruchamahabal committed Jan 24, 2022
1 parent 2e3ea45 commit 31ff0d3
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 8 deletions.
5 changes: 5 additions & 0 deletions erpnext/hr/doctype/leave_application/leave_application.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,11 @@ def update_attendance(self):
# if leave type does not include holidays within leaves as leaves
if date in holiday_dates:
if attendance_name:
# cancel and delete existing attendance for holidays
attendance = frappe.get_doc("Attendance", attendance_name)
attendance.flags.ignore_permissions = True
if attendance.docstatus == 1:
attendance.cancel()
frappe.delete_doc("Attendance", attendance_name, force=1)
continue

Expand Down
116 changes: 108 additions & 8 deletions erpnext/hr/doctype/leave_application/test_leave_application.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,19 @@

import frappe
from frappe.permissions import clear_user_permissions_for_doctype
from frappe.utils import add_days, add_months, getdate, nowdate
from frappe.utils import (
add_days,
add_months,
get_first_day,
get_last_day,
get_year_ending,
get_year_start,
getdate,
nowdate,
)

import erpnext
from erpnext.hr.doctype.employee.employee import get_holiday_list_for_employee
from erpnext.hr.doctype.employee.test_employee import make_employee
from erpnext.hr.doctype.leave_allocation.test_leave_allocation import create_leave_allocation
from erpnext.hr.doctype.leave_application.leave_application import (
Expand All @@ -19,6 +30,10 @@
create_assignment_for_multiple_employees,
)
from erpnext.hr.doctype.leave_type.test_leave_type import create_leave_type
from erpnext.payroll.doctype.salary_slip.test_salary_slip import (
make_holiday_list,
make_leave_application,
)

test_dependencies = ["Leave Allocation", "Leave Block List", "Employee"]

Expand Down Expand Up @@ -61,13 +76,15 @@ def setUp(self):
for dt in ["Leave Application", "Leave Allocation", "Salary Slip", "Leave Ledger Entry"]:
frappe.db.sql("DELETE FROM `tab%s`" % dt) #nosec

frappe.set_user("Administrator")

@classmethod
def setUpClass(cls):
set_leave_approver()
frappe.db.sql("delete from tabAttendance where employee='_T-Employee-00001'")

def tearDown(self):
frappe.set_user("Administrator")
frappe.db.rollback()

def _clear_roles(self):
frappe.db.sql("""delete from `tabHas Role` where parent in
Expand Down Expand Up @@ -106,6 +123,72 @@ 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_attendance_for_include_holidays(self):
# Case 1: leave type with 'Include holidays within leaves as leaves' enabled
frappe.delete_doc_if_exists("Leave Type", "Test Include Holidays", force=1)
leave_type = frappe.get_doc(dict(
leave_type_name="Test Include Holidays",
doctype="Leave Type",
include_holiday=True
)).insert()

date = getdate()
make_allocation_record(leave_type=leave_type.name, from_date=get_year_start(date), to_date=get_year_ending(date))

holiday_list = make_holiday_list()
frappe.db.set_value("Company", erpnext.get_default_company(), "default_holiday_list", holiday_list)
first_sunday = get_first_sunday(holiday_list)

leave_application = make_leave_application("_T-Employee-00001", first_sunday, add_days(first_sunday, 3), leave_type.name)
leave_application.reload()
self.assertEqual(leave_application.total_leave_days, 4)
self.assertEqual(frappe.db.count('Attendance', {'leave_application': leave_application.name}), 4)

leave_application.cancel()

def test_attendance_update_for_exclude_holidays(self):
# Case 2: leave type with 'Include holidays within leaves as leaves' disabled
frappe.delete_doc_if_exists("Leave Type", "Test Do Not Include Holidays", force=1)
leave_type = frappe.get_doc(dict(
leave_type_name="Test Do Not Include Holidays",
doctype="Leave Type",
include_holiday=False
)).insert()

date = getdate()
make_allocation_record(leave_type=leave_type.name, from_date=get_year_start(date), to_date=get_year_ending(date))

holiday_list = make_holiday_list()
frappe.db.set_value("Company", erpnext.get_default_company(), "default_holiday_list", holiday_list)
first_sunday = get_first_sunday(holiday_list)

# already marked attendance on a holiday should be deleted in this case
config = {
"doctype": "Attendance",
"employee": "_T-Employee-00001",
"status": "Present"
}
attendance_on_holiday = frappe.get_doc(config)
attendance_on_holiday.attendance_date = first_sunday
attendance_on_holiday.save()

# already marked attendance on a non-holiday should be updated
attendance = frappe.get_doc(config)
attendance.attendance_date = add_days(first_sunday, 3)
attendance.save()

leave_application = make_leave_application("_T-Employee-00001", first_sunday, add_days(first_sunday, 3), leave_type.name)
leave_application.reload()
# holiday should be excluded while marking attendance
self.assertEqual(leave_application.total_leave_days, 3)
self.assertEqual(frappe.db.count("Attendance", {"leave_application": leave_application.name}), 3)

# attendance on holiday deleted
self.assertFalse(frappe.db.exists("Attendance", attendance_on_holiday.name))

# attendance on non-holiday updated
self.assertEqual(frappe.db.get_value("Attendance", attendance.name, "status"), "On Leave")

def test_block_list(self):
self._clear_roles()

Expand Down Expand Up @@ -241,7 +324,12 @@ def test_optional_leave(self):
leave_period = get_leave_period()
today = nowdate()
holiday_list = 'Test Holiday List for Optional Holiday'
optional_leave_date = add_days(today, 7)
employee = get_employee()

default_holiday_list = get_holiday_list_for_employee(employee.name)
first_sunday = get_first_sunday(default_holiday_list)

optional_leave_date = add_days(first_sunday, 1)

if not frappe.db.exists('Holiday List', holiday_list):
frappe.get_doc(dict(
Expand All @@ -253,7 +341,6 @@ def test_optional_leave(self):
dict(holiday_date = optional_leave_date, description = 'Test')
]
)).insert()
employee = get_employee()

frappe.db.set_value('Leave Period', leave_period.name, 'optional_holiday_list', holiday_list)
leave_type = 'Test Optional Type'
Expand All @@ -266,7 +353,7 @@ def test_optional_leave(self):

allocate_leaves(employee, leave_period, leave_type, 10)

date = add_days(today, 6)
date = add_days(first_sunday, 2)

leave_application = frappe.get_doc(dict(
doctype = 'Leave Application',
Expand Down Expand Up @@ -637,13 +724,13 @@ def create_carry_forwarded_allocation(employee, leave_type):
carry_forward=1)
leave_allocation.submit()

def make_allocation_record(employee=None, leave_type=None):
def make_allocation_record(employee=None, leave_type=None, from_date=None, to_date=None):
allocation = frappe.get_doc({
"doctype": "Leave Allocation",
"employee": employee or "_T-Employee-00001",
"leave_type": leave_type or "_Test Leave Type",
"from_date": "2013-01-01",
"to_date": "2019-12-31",
"from_date": from_date or "2013-01-01",
"to_date": to_date or "2019-12-31",
"new_leaves_allocated": 30
})

Expand Down Expand Up @@ -692,3 +779,16 @@ def allocate_leaves(employee, leave_period, leave_type, new_leaves_allocated, el
}).insert()

allocate_leave.submit()


def get_first_sunday(holiday_list):
month_start_date = get_first_day(nowdate())
month_end_date = get_last_day(nowdate())
first_sunday = frappe.db.sql("""
select holiday_date from `tabHoliday`
where parent = %s
and holiday_date between %s and %s
order by holiday_date
""", (holiday_list, month_start_date, month_end_date))[0][0]

return first_sunday
2 changes: 2 additions & 0 deletions erpnext/payroll/doctype/salary_slip/test_salary_slip.py
Original file line number Diff line number Diff line change
Expand Up @@ -994,6 +994,8 @@ def make_leave_application(employee, from_date, to_date, leave_type, company=Non
))
leave_application.submit()

return leave_application

def setup_test():
make_earning_salary_component(setup=True, company_list=["_Test Company"])
make_deduction_salary_component(setup=True, company_list=["_Test Company"])
Expand Down

0 comments on commit 31ff0d3

Please sign in to comment.