mirror of
https://github.com/frappe/erpnext.git
synced 2026-05-24 23:49:19 +00:00
Merge pull request #15724 from shreyashah115/fix-leave
Do not consider current leave application while submitting leave
This commit is contained in:
@@ -191,9 +191,8 @@ class LeaveApplication(Document):
|
|||||||
frappe.throw(_("The day(s) on which you are applying for leave are holidays. You need not apply for leave."))
|
frappe.throw(_("The day(s) on which you are applying for leave are holidays. You need not apply for leave."))
|
||||||
|
|
||||||
if not is_lwp(self.leave_type):
|
if not is_lwp(self.leave_type):
|
||||||
self.leave_balance = get_leave_balance_on(self.employee, self.leave_type, self.from_date,
|
self.leave_balance = get_leave_balance_on(self.employee, self.leave_type, self.from_date, docname=self.name,
|
||||||
consider_all_leaves_in_the_allocation_period=True)
|
consider_all_leaves_in_the_allocation_period=True)
|
||||||
|
|
||||||
if self.status != "Rejected" and self.leave_balance < self.total_leave_days:
|
if self.status != "Rejected" and self.leave_balance < self.total_leave_days:
|
||||||
if frappe.db.get_value("Leave Type", self.leave_type, "allow_negative"):
|
if frappe.db.get_value("Leave Type", self.leave_type, "allow_negative"):
|
||||||
frappe.msgprint(_("Note: There is not enough leave balance for Leave Type {0}")
|
frappe.msgprint(_("Note: There is not enough leave balance for Leave Type {0}")
|
||||||
@@ -385,26 +384,24 @@ def get_leave_details(employee, date):
|
|||||||
return ret
|
return ret
|
||||||
|
|
||||||
@frappe.whitelist()
|
@frappe.whitelist()
|
||||||
def get_leave_balance_on(employee, leave_type, date, allocation_records=None,
|
def get_leave_balance_on(employee, leave_type, date, allocation_records=None, docname=None,
|
||||||
consider_all_leaves_in_the_allocation_period=False, consider_encashed_leaves=True):
|
consider_all_leaves_in_the_allocation_period=False, consider_encashed_leaves=True):
|
||||||
|
|
||||||
if allocation_records == None:
|
if allocation_records == None:
|
||||||
allocation_records = get_leave_allocation_records(date, employee).get(employee, frappe._dict())
|
allocation_records = get_leave_allocation_records(date, employee).get(employee, frappe._dict())
|
||||||
|
|
||||||
allocation = allocation_records.get(leave_type, frappe._dict())
|
allocation = allocation_records.get(leave_type, frappe._dict())
|
||||||
|
|
||||||
if consider_all_leaves_in_the_allocation_period:
|
if consider_all_leaves_in_the_allocation_period:
|
||||||
date = allocation.to_date
|
date = allocation.to_date
|
||||||
leaves_taken = get_leaves_for_period(employee, leave_type, allocation.from_date, date, status="Approved")
|
leaves_taken = get_leaves_for_period(employee, leave_type, allocation.from_date, date, status="Approved", docname=docname)
|
||||||
|
|
||||||
leaves_encashed = 0
|
leaves_encashed = 0
|
||||||
if frappe.db.get_value("Leave Type", leave_type, 'allow_encashment') and consider_encashed_leaves:
|
if frappe.db.get_value("Leave Type", leave_type, 'allow_encashment') and consider_encashed_leaves:
|
||||||
leaves_encashed = flt(allocation.total_leaves_encashed)
|
leaves_encashed = flt(allocation.total_leaves_encashed)
|
||||||
|
|
||||||
return flt(allocation.total_leaves_allocated) - (flt(leaves_taken) + flt(leaves_encashed))
|
return flt(allocation.total_leaves_allocated) - (flt(leaves_taken) + flt(leaves_encashed))
|
||||||
|
|
||||||
def get_leaves_for_period(employee, leave_type, from_date, to_date, status):
|
def get_leaves_for_period(employee, leave_type, from_date, to_date, status, docname=None):
|
||||||
leave_applications = frappe.db.sql("""
|
leave_applications = frappe.db.sql("""
|
||||||
select employee, leave_type, from_date, to_date, total_leave_days
|
select name, employee, leave_type, from_date, to_date, total_leave_days
|
||||||
from `tabLeave Application`
|
from `tabLeave Application`
|
||||||
where employee=%(employee)s and leave_type=%(leave_type)s
|
where employee=%(employee)s and leave_type=%(leave_type)s
|
||||||
and status = %(status)s and docstatus != 2
|
and status = %(status)s and docstatus != 2
|
||||||
@@ -418,9 +415,10 @@ def get_leaves_for_period(employee, leave_type, from_date, to_date, status):
|
|||||||
"status": status,
|
"status": status,
|
||||||
"leave_type": leave_type
|
"leave_type": leave_type
|
||||||
}, as_dict=1)
|
}, as_dict=1)
|
||||||
|
|
||||||
leave_days = 0
|
leave_days = 0
|
||||||
for leave_app in leave_applications:
|
for leave_app in leave_applications:
|
||||||
|
if docname and leave_app.name == docname:
|
||||||
|
continue
|
||||||
if leave_app.from_date >= getdate(from_date) and leave_app.to_date <= getdate(to_date):
|
if leave_app.from_date >= getdate(from_date) and leave_app.to_date <= getdate(to_date):
|
||||||
leave_days += leave_app.total_leave_days
|
leave_days += leave_app.total_leave_days
|
||||||
else:
|
else:
|
||||||
@@ -450,7 +448,6 @@ def get_leave_allocation_records(date, employee=None):
|
|||||||
"total_leaves_allocated": d.total_leaves_allocated,
|
"total_leaves_allocated": d.total_leaves_allocated,
|
||||||
"total_leaves_encashed":d.total_leaves_encashed
|
"total_leaves_encashed":d.total_leaves_encashed
|
||||||
}))
|
}))
|
||||||
|
|
||||||
return allocated_leaves
|
return allocated_leaves
|
||||||
|
|
||||||
@frappe.whitelist()
|
@frappe.whitelist()
|
||||||
|
|||||||
@@ -392,6 +392,34 @@ class TestLeaveApplication(unittest.TestCase):
|
|||||||
i += 1
|
i += 1
|
||||||
self.assertEqual(get_leave_balance_on(employee.name, leave_type, nowdate()), 6)
|
self.assertEqual(get_leave_balance_on(employee.name, leave_type, nowdate()), 6)
|
||||||
|
|
||||||
|
# test to not consider current leave in leave balance while submitting
|
||||||
|
def test_current_leave_on_submit(self):
|
||||||
|
employee = get_employee()
|
||||||
|
leave_type = 'Sick leave'
|
||||||
|
allocation = frappe.get_doc(dict(
|
||||||
|
doctype = 'Leave Allocation',
|
||||||
|
employee = employee.name,
|
||||||
|
leave_type = leave_type,
|
||||||
|
from_date = '2018-10-01',
|
||||||
|
to_date = '2018-10-10',
|
||||||
|
new_leaves_allocated = 1
|
||||||
|
))
|
||||||
|
allocation.insert(ignore_permissions=True)
|
||||||
|
allocation.submit()
|
||||||
|
leave_application = frappe.get_doc(dict(
|
||||||
|
doctype = 'Leave Application',
|
||||||
|
employee = employee.name,
|
||||||
|
leave_type = leave_type,
|
||||||
|
from_date = '2018-10-02',
|
||||||
|
to_date = '2018-10-02',
|
||||||
|
company = '_Test Company',
|
||||||
|
status = 'Approved',
|
||||||
|
leave_approver = 'test@example.com'
|
||||||
|
))
|
||||||
|
self.assertTrue(leave_application.insert())
|
||||||
|
leave_application.submit()
|
||||||
|
self.assertEqual(leave_application.docstatus, 1)
|
||||||
|
|
||||||
def make_allocation_record(employee=None, leave_type=None):
|
def make_allocation_record(employee=None, leave_type=None):
|
||||||
frappe.db.sql("delete from `tabLeave Allocation`")
|
frappe.db.sql("delete from `tabLeave Allocation`")
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user