From 9bc4232af619503028400ffdc446428feb81691d Mon Sep 17 00:00:00 2001 From: Mangesh-Khairnar Date: Thu, 8 Aug 2019 17:11:08 +0530 Subject: [PATCH] feat: calculate remaining leaves both multiple simultaneous allocation --- .../leave_allocation/test_leave_allocation.js | 2 +- .../leave_allocation/test_leave_allocation.py | 10 ++++------ .../leave_application/leave_application.py | 16 ++++++++++------ .../leave_application/test_leave_application.py | 15 ++++++++++++++- 4 files changed, 29 insertions(+), 14 deletions(-) diff --git a/erpnext/hr/doctype/leave_allocation/test_leave_allocation.js b/erpnext/hr/doctype/leave_allocation/test_leave_allocation.js index b8f4fafa6d8..0ef78f2f883 100644 --- a/erpnext/hr/doctype/leave_allocation/test_leave_allocation.js +++ b/erpnext/hr/doctype/leave_allocation/test_leave_allocation.js @@ -34,7 +34,7 @@ QUnit.test("Test: Leave allocation [HR]", function (assert) { () => assert.equal(today_date, cur_frm.doc.from_date, "from date correctly set"), // check for total leaves - () => assert.equal(cur_frm.doc.carry_forwarded_leaves + 2, cur_frm.doc.total_leaves_allocated, + () => assert.equal(cur_frm.doc.unused_leaves + 2, cur_frm.doc.total_leaves_allocated, "total leave calculation is correctly set"), () => done() ]); diff --git a/erpnext/hr/doctype/leave_allocation/test_leave_allocation.py b/erpnext/hr/doctype/leave_allocation/test_leave_allocation.py index 4f4e0ab1fc6..8f876ae7ce4 100644 --- a/erpnext/hr/doctype/leave_allocation/test_leave_allocation.py +++ b/erpnext/hr/doctype/leave_allocation/test_leave_allocation.py @@ -80,7 +80,7 @@ class TestLeaveAllocation(unittest.TestCase): leave_type="_Test_CF_leave", from_date=add_months(nowdate(), -12), to_date=add_months(nowdate(), -1), - carry_forward=1) + carry_forward=0) leave_allocation.submit() # leave allocation with carry forward from previous allocation @@ -89,7 +89,7 @@ class TestLeaveAllocation(unittest.TestCase): carry_forward=1) leave_allocation_1.submit() - self.assertEquals(leave_allocation.total_leaves_allocated, leave_allocation_1.carry_forwarded_leaves) + self.assertEquals(leave_allocation.total_leaves_allocated, leave_allocation_1.unused_leaves) def test_carry_forward_leaves_expiry(self): frappe.db.sql("delete from `tabLeave Allocation`") @@ -105,11 +105,9 @@ class TestLeaveAllocation(unittest.TestCase): leave_type="_Test_CF_leave_expiry", from_date=add_months(nowdate(), -24), to_date=add_months(nowdate(), -12), - carry_forward=1) + carry_forward=0) leave_allocation.submit() - expire_allocation(leave_allocation) - leave_allocation = create_leave_allocation( leave_type="_Test_CF_leave_expiry", from_date=add_days(nowdate(), -90), @@ -128,7 +126,7 @@ class TestLeaveAllocation(unittest.TestCase): to_date=add_months(nowdate(), 12)) leave_allocation_1.submit() - self.assertEquals(leave_allocation_1.carry_forwarded_leaves, leave_allocation.new_leaves_allocated) + self.assertEquals(leave_allocation_1.unused_leaves, leave_allocation.new_leaves_allocated) def test_creation_of_leave_ledger_entry_on_submit(self): frappe.db.sql("delete from `tabLeave Allocation`") diff --git a/erpnext/hr/doctype/leave_application/leave_application.py b/erpnext/hr/doctype/leave_application/leave_application.py index 2e43721a580..0aa8849e879 100755 --- a/erpnext/hr/doctype/leave_application/leave_application.py +++ b/erpnext/hr/doctype/leave_application/leave_application.py @@ -387,10 +387,12 @@ class LeaveApplication(Document): create_leave_ledger_entry(self, args, submit) def get_allocation_expiry(employee, leave_type, to_date, from_date): + ''' Returns expiry of carry forward allocation in leave ledger entry ''' expiry = frappe.get_all("Leave Ledger Entry", filters={ 'employee': employee, 'leave_type': leave_type, + 'is_carry_forward': 1, 'transaction_type': 'Leave Allocation', 'to_date': ['between', (from_date, to_date)] },fields=['to_date']) @@ -487,7 +489,7 @@ def get_leave_allocation_records(employee, date, leave_type=None): "from_date": d.from_date, "to_date": d.to_date, "total_leaves_allocated": flt(d.cf_leaves) + flt(d.new_leaves), - "carry_forwarded_leaves": d.cf_leaves, + "unused_leaves": d.cf_leaves, "new_leaves_allocated": d.new_leaves, "leave_type": d.leave_type })) @@ -515,12 +517,14 @@ def get_remaining_leaves(allocation, leaves_taken, date, expiry): return remaining_leaves - if expiry and allocation.carry_forwarded_leaves: - remaining_leaves = _get_remaining_leaves(allocation.carry_forwarded_leaves, expiry) + total_leaves = allocation.total_leaves_allocated - return flt(allocation.new_leaves_allocated) + flt(remaining_leaves) - else: - return _get_remaining_leaves(allocation.total_leaves_allocated, allocation.to_date) + if expiry and allocation.unused_leaves: + remaining_leaves = _get_remaining_leaves(allocation.unused_leaves, expiry) + + total_leaves = flt(allocation.new_leaves_allocated) + flt(remaining_leaves) + + return _get_remaining_leaves(total_leaves, allocation.to_date) def get_leaves_for_period(employee, leave_type, from_date, to_date): leave_entries = get_leave_entries(employee, leave_type, from_date, to_date) diff --git a/erpnext/hr/doctype/leave_application/test_leave_application.py b/erpnext/hr/doctype/leave_application/test_leave_application.py index 709a2f57dfd..0f7bf86b224 100644 --- a/erpnext/hr/doctype/leave_application/test_leave_application.py +++ b/erpnext/hr/doctype/leave_application/test_leave_application.py @@ -542,6 +542,19 @@ class TestLeaveApplication(unittest.TestCase): self.assertEquals(leave_ledger_entry[0].leaves, -9) self.assertEquals(leave_ledger_entry[1].leaves, -2) + def test_leave_application_creation_after_expiry(self): + # test leave balance for carry forwarded allocation + employee = get_employee() + leave_type = create_leave_type( + leave_type_name="_Test_CF_leave_expiry", + is_carry_forward=1, + expire_carried_forward_leaves=90) + leave_type.submit() + + create_carry_forwarded_allocation(employee, leave_type) + + self.assertEquals(get_leave_balance_on(employee.name, leave_type.name, add_days(nowdate(), -85), add_days(nowdate(), -84)), 0) + def create_carry_forwarded_allocation(employee, leave_type): # initial leave allocation leave_allocation = create_leave_allocation( @@ -550,7 +563,7 @@ def create_carry_forwarded_allocation(employee, leave_type): employee_name=employee.employee_name, from_date=add_months(nowdate(), -24), to_date=add_months(nowdate(), -12), - carry_forward=1) + carry_forward=0) leave_allocation.submit() leave_allocation = create_leave_allocation(