Merge pull request #4763 from rmehta/leave-app-cleanup
[cleanup] leave application
This commit is contained in:
commit
c403ce3b4f
@ -145,28 +145,31 @@ class LeaveApplication(Document):
|
|||||||
}, as_dict = 1):
|
}, as_dict = 1):
|
||||||
|
|
||||||
if d['total_leave_days']==0.5 and cint(self.half_day)==1:
|
if d['total_leave_days']==0.5 and cint(self.half_day)==1:
|
||||||
sum_leave_days=frappe.db.sql("""select sum(total_leave_days) from `tabLeave Application`
|
sum_leave_days = self.get_total_leaves_on_half_day()
|
||||||
|
if sum_leave_days==1:
|
||||||
|
self.throw_overlap_error(d)
|
||||||
|
else:
|
||||||
|
self.throw_overlap_error(d)
|
||||||
|
|
||||||
|
def throw_overlap_error(self, d):
|
||||||
|
msg = _("Employee {0} has already applied for {1} between {2} and {3}").format(self.employee,
|
||||||
|
d['leave_type'], formatdate(d['from_date']), formatdate(d['to_date'])) \
|
||||||
|
+ """ <br><b><a href="#Form/Leave Application/{0}">{0}</a></b>""".format(d["name"])
|
||||||
|
frappe.throw(msg, OverlapError)
|
||||||
|
|
||||||
|
def get_total_leaves_on_half_day(self):
|
||||||
|
return frappe.db.sql("""select sum(total_leave_days) from `tabLeave Application`
|
||||||
where employee = %(employee)s
|
where employee = %(employee)s
|
||||||
and docstatus < 2
|
and docstatus < 2
|
||||||
and status in ("Open", "Approved")
|
and status in ("Open", "Approved")
|
||||||
and (from_date between %(from_date)s and %(to_date)s
|
and from_date = %(from_date)s
|
||||||
or to_date between %(from_date)s and %(to_date)s
|
and to_date = %(to_date)s
|
||||||
or %(from_date)s between from_date and to_date)
|
|
||||||
and name != %(name)s""", {
|
and name != %(name)s""", {
|
||||||
"employee": self.employee,
|
"employee": self.employee,
|
||||||
"from_date": self.from_date,
|
"from_date": self.from_date,
|
||||||
"to_date": self.to_date,
|
"to_date": self.to_date,
|
||||||
"name": self.name
|
"name": self.name
|
||||||
})[0][0]
|
})[0][0]
|
||||||
if sum_leave_days==1:
|
|
||||||
frappe.msgprint(_("Employee {0} has already applied this day").format(self.employee))
|
|
||||||
frappe.throw('<a href="#Form/Leave Application/{0}">{0}</a>'.format(d["name"]), OverlapError)
|
|
||||||
else:
|
|
||||||
frappe.msgprint(_("Employee {0} has already applied for {1} between {2} and {3}")
|
|
||||||
.format(self.employee, cstr(d['leave_type']),formatdate(d['from_date']), formatdate(d['to_date'])))
|
|
||||||
|
|
||||||
frappe.throw("""Exising Application: <a href="#Form/Leave Application/{0}">{0}</a>""".format(d["name"]), OverlapError)
|
|
||||||
|
|
||||||
|
|
||||||
def validate_max_days(self):
|
def validate_max_days(self):
|
||||||
max_days = frappe.db.get_value("Leave Type", self.leave_type, "max_days_allowed")
|
max_days = frappe.db.get_value("Leave Type", self.leave_type, "max_days_allowed")
|
||||||
|
@ -137,6 +137,60 @@ class TestLeaveApplication(unittest.TestCase):
|
|||||||
application.leave_approver = "test2@example.com"
|
application.leave_approver = "test2@example.com"
|
||||||
self.assertRaises(OverlapError, application.insert)
|
self.assertRaises(OverlapError, application.insert)
|
||||||
|
|
||||||
|
def test_overlap_with_half_day(self):
|
||||||
|
self._clear_roles()
|
||||||
|
self._clear_applications()
|
||||||
|
|
||||||
|
from frappe.utils.user import add_role
|
||||||
|
add_role("test@example.com", "Employee")
|
||||||
|
add_role("test2@example.com", "Leave Approver")
|
||||||
|
|
||||||
|
frappe.set_user("test@example.com")
|
||||||
|
|
||||||
|
make_allocation_record()
|
||||||
|
|
||||||
|
# allow second half-day on the same day if available
|
||||||
|
application = self.get_application(_test_records[0])
|
||||||
|
application.leave_approver = "test2@example.com"
|
||||||
|
application.half_day = 1
|
||||||
|
application.insert()
|
||||||
|
|
||||||
|
# allow second half-day on the same day if available
|
||||||
|
application = self.get_application(_test_records[0])
|
||||||
|
application.leave_approver = "test2@example.com"
|
||||||
|
application.half_day = 1
|
||||||
|
application.insert()
|
||||||
|
|
||||||
|
application = self.get_application(_test_records[0])
|
||||||
|
application.leave_approver = "test2@example.com"
|
||||||
|
application.half_day = 1
|
||||||
|
|
||||||
|
self.assertRaises(OverlapError, application.insert)
|
||||||
|
|
||||||
|
def test_overlap_with_half_day_not_applicable(self):
|
||||||
|
self._clear_roles()
|
||||||
|
self._clear_applications()
|
||||||
|
|
||||||
|
from frappe.utils.user import add_role
|
||||||
|
add_role("test@example.com", "Employee")
|
||||||
|
add_role("test2@example.com", "Leave Approver")
|
||||||
|
|
||||||
|
frappe.set_user("test@example.com")
|
||||||
|
|
||||||
|
make_allocation_record()
|
||||||
|
|
||||||
|
# allow second half-day on the same day if available
|
||||||
|
application = self.get_application(_test_records[0])
|
||||||
|
application.leave_approver = "test2@example.com"
|
||||||
|
application.insert()
|
||||||
|
|
||||||
|
# allow second half-day on the same day if available
|
||||||
|
application = self.get_application(_test_records[0])
|
||||||
|
application.leave_approver = "test2@example.com"
|
||||||
|
application.half_day = 1
|
||||||
|
|
||||||
|
self.assertRaises(OverlapError, application.insert)
|
||||||
|
|
||||||
def test_global_block_list(self):
|
def test_global_block_list(self):
|
||||||
self._clear_roles()
|
self._clear_roles()
|
||||||
|
|
||||||
|
8
erpnext/hr/doctype/leave_type/leave_type.js
Normal file
8
erpnext/hr/doctype/leave_type/leave_type.js
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
frappe.ui.form.on("Leave Type", {
|
||||||
|
refresh: function(frm) {
|
||||||
|
frm.add_custom_button(__("Allocations"), function() {
|
||||||
|
frappe.set_route("List", "Leave Allocation",
|
||||||
|
{"leave_type": frm.doc.name});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
Loading…
x
Reference in New Issue
Block a user