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: convert dates to datetime before comparing in leave days calculation and fix half day edge case #30538

Merged
merged 1 commit into from
Apr 1, 2022
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
4 changes: 2 additions & 2 deletions erpnext/hr/doctype/leave_application/leave_application.py
Original file line number Diff line number Diff line change
Expand Up @@ -735,9 +735,9 @@ def get_number_of_leave_days(
(Based on the include_holiday setting in Leave Type)"""
number_of_days = 0
if cint(half_day) == 1:
if from_date == to_date:
if getdate(from_date) == getdate(to_date):
number_of_days = 0.5
elif half_day_date and half_day_date <= to_date:
elif half_day_date and getdate(from_date) <= getdate(half_day_date) <= getdate(to_date):
number_of_days = date_diff(to_date, from_date) + 0.5
else:
number_of_days = date_diff(to_date, from_date) + 1
Expand Down
20 changes: 17 additions & 3 deletions erpnext/hr/doctype/leave_application/test_leave_application.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,12 @@ def test_separate_leave_ledger_entry_for_boundary_applications(self):
# creates separate leave ledger entries
frappe.delete_doc_if_exists("Leave Type", "Test Leave Validation", force=1)
leave_type = frappe.get_doc(
dict(leave_type_name="Test Leave Validation", doctype="Leave Type", allow_negative=True)
dict(
leave_type_name="Test Leave Validation",
doctype="Leave Type",
allow_negative=True,
include_holiday=True,
)
).insert()

employee = get_employee()
Expand All @@ -217,8 +222,14 @@ def test_separate_leave_ledger_entry_for_boundary_applications(self):
# application across allocations

# CASE 1: from date has no allocation, to date has an allocation / both dates have allocation
start_date = add_days(year_start, -10)
application = make_leave_application(
employee.name, add_days(year_start, -10), add_days(year_start, 3), leave_type.name
employee.name,
start_date,
add_days(year_start, 3),
leave_type.name,
half_day=1,
half_day_date=start_date,
)

# 2 separate leave ledger entries
Expand Down Expand Up @@ -828,6 +839,7 @@ def test_ledger_entry_creation_on_intermediate_allocation_expiry(self):
leave_type_name="_Test_CF_leave_expiry",
is_carry_forward=1,
expire_carry_forwarded_leaves_after_days=90,
include_holiday=True,
)
leave_type.submit()

Expand All @@ -840,6 +852,8 @@ def test_ledger_entry_creation_on_intermediate_allocation_expiry(self):
leave_type=leave_type.name,
from_date=add_days(nowdate(), -3),
to_date=add_days(nowdate(), 7),
half_day=1,
half_day_date=add_days(nowdate(), -3),
description="_Test Reason",
company="_Test Company",
docstatus=1,
Expand All @@ -855,7 +869,7 @@ def test_ledger_entry_creation_on_intermediate_allocation_expiry(self):
self.assertEqual(len(leave_ledger_entry), 2)
self.assertEqual(leave_ledger_entry[0].employee, leave_application.employee)
self.assertEqual(leave_ledger_entry[0].leave_type, leave_application.leave_type)
self.assertEqual(leave_ledger_entry[0].leaves, -9)
self.assertEqual(leave_ledger_entry[0].leaves, -8.5)
self.assertEqual(leave_ledger_entry[1].leaves, -2)

def test_leave_application_creation_after_expiry(self):
Expand Down
13 changes: 12 additions & 1 deletion erpnext/payroll/doctype/salary_slip/test_salary_slip.py
Original file line number Diff line number Diff line change
Expand Up @@ -1290,14 +1290,25 @@ def create_additional_salary(employee, payroll_period, amount):
return salary_date


def make_leave_application(employee, from_date, to_date, leave_type, company=None, submit=True):
def make_leave_application(
employee,
from_date,
to_date,
leave_type,
company=None,
half_day=False,
half_day_date=None,
submit=True,
):
leave_application = frappe.get_doc(
dict(
doctype="Leave Application",
employee=employee,
leave_type=leave_type,
from_date=from_date,
to_date=to_date,
half_day=half_day,
half_day_date=half_day_date,
company=company or erpnext.get_default_company() or "_Test Company",
status="Approved",
leave_approver="test@example.com",
Expand Down