Skip to content

Commit

Permalink
feat: better error msg
Browse files Browse the repository at this point in the history
  • Loading branch information
krantheman committed Nov 22, 2023
1 parent 4ae0270 commit 085b56e
Showing 1 changed file with 25 additions and 6 deletions.
31 changes: 25 additions & 6 deletions hrms/hr/doctype/leave_application/leave_application.py
Original file line number Diff line number Diff line change
Expand Up @@ -491,8 +491,8 @@ def get_first_from_date(self, to_date, holiday_list):
if from_date:
return self.get_first_from_date(add_to_date(from_date, days=-1), holiday_list)
elif frappe.db.exists("Holiday", {"parent": holiday_list, "holiday_date": to_date}):
return self.get_first_from_date(add_to_date(to_date, days=-1), holiday_list)
return add_to_date(to_date, days=1)
return self.get_first_from_date(add_to_date(to_date, days=-1), holiday_list) # ignore holiday
return add_to_date(to_date, days=1) # to_date is actually from_date

# gets to_date of last leave application from following consecutive leave applications
def get_last_to_date(self, from_date, holiday_list):
Expand All @@ -504,8 +504,8 @@ def get_last_to_date(self, from_date, holiday_list):
if to_date:
return self.get_last_to_date(add_to_date(to_date, days=1), holiday_list)
elif frappe.db.exists("Holiday", {"parent": holiday_list, "holiday_date": from_date}):
return self.get_last_to_date(add_to_date(from_date, days=1), holiday_list)
return add_to_date(from_date, days=-1)
return self.get_last_to_date(add_to_date(from_date, days=1), holiday_list) # ignore holiday
return add_to_date(from_date, days=-1) # from_date is actually to_date

def validate_max_days(self):
max_days = frappe.db.get_value("Leave Type", self.leave_type, "max_continuous_days_allowed")
Expand All @@ -521,9 +521,28 @@ def validate_max_days(self):
self.employee, self.leave_type, first_from_date, last_to_date
)
if total_consecutive_leaves > cint(max_days):
leave_applications = frappe.get_list(
"Leave Application",
filters={
"employee": self.employee,
"leave_type": self.leave_type,
"from_date": [">=", first_from_date],
"to_date": ["<=", last_to_date],
},
pluck="name",
)
if len(leave_applications) > 0:
leave_applications = ", ".join(
list(map(lambda x: get_link_to_form("Leave Application", x), leave_applications))
)
frappe.throw(
_("Cannot have more that {0} consecutive leaves of type {1}. Reference: {2}").format(
max_days, get_link_to_form("Leave Type", self.leave_type), leave_applications
)
)
frappe.throw(
_("Cannot have more that {0} consecutive leaves of Leave Type <b>{1}</b>").format(
max_days, self.leave_type, max_days
_("Cannot have more that {0} consecutive leaves of type {1}").format(
max_days, get_link_to_form("Leave Type", self.leave_type)
)
)

Expand Down

0 comments on commit 085b56e

Please sign in to comment.