feat: calculate remaining leaves both multiple simultaneous allocation
This commit is contained in:
		
							parent
							
								
									5cbe6160ca
								
							
						
					
					
						commit
						9bc4232af6
					
				| @ -34,7 +34,7 @@ QUnit.test("Test: Leave allocation [HR]", function (assert) { | |||||||
| 		() => assert.equal(today_date, cur_frm.doc.from_date, | 		() => assert.equal(today_date, cur_frm.doc.from_date, | ||||||
| 			"from date correctly set"), | 			"from date correctly set"), | ||||||
| 		// check for total leaves
 | 		// 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"), | 			"total leave calculation is correctly set"), | ||||||
| 		() => done() | 		() => done() | ||||||
| 	]); | 	]); | ||||||
|  | |||||||
| @ -80,7 +80,7 @@ class TestLeaveAllocation(unittest.TestCase): | |||||||
| 			leave_type="_Test_CF_leave", | 			leave_type="_Test_CF_leave", | ||||||
| 			from_date=add_months(nowdate(), -12), | 			from_date=add_months(nowdate(), -12), | ||||||
| 			to_date=add_months(nowdate(), -1), | 			to_date=add_months(nowdate(), -1), | ||||||
| 			carry_forward=1) | 			carry_forward=0) | ||||||
| 		leave_allocation.submit() | 		leave_allocation.submit() | ||||||
| 
 | 
 | ||||||
| 		# leave allocation with carry forward from previous allocation | 		# leave allocation with carry forward from previous allocation | ||||||
| @ -89,7 +89,7 @@ class TestLeaveAllocation(unittest.TestCase): | |||||||
| 			carry_forward=1) | 			carry_forward=1) | ||||||
| 		leave_allocation_1.submit() | 		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): | 	def test_carry_forward_leaves_expiry(self): | ||||||
| 		frappe.db.sql("delete from `tabLeave Allocation`") | 		frappe.db.sql("delete from `tabLeave Allocation`") | ||||||
| @ -105,11 +105,9 @@ class TestLeaveAllocation(unittest.TestCase): | |||||||
| 			leave_type="_Test_CF_leave_expiry", | 			leave_type="_Test_CF_leave_expiry", | ||||||
| 			from_date=add_months(nowdate(), -24), | 			from_date=add_months(nowdate(), -24), | ||||||
| 			to_date=add_months(nowdate(), -12), | 			to_date=add_months(nowdate(), -12), | ||||||
| 			carry_forward=1) | 			carry_forward=0) | ||||||
| 		leave_allocation.submit() | 		leave_allocation.submit() | ||||||
| 
 | 
 | ||||||
| 		expire_allocation(leave_allocation) |  | ||||||
| 
 |  | ||||||
| 		leave_allocation = create_leave_allocation( | 		leave_allocation = create_leave_allocation( | ||||||
| 			leave_type="_Test_CF_leave_expiry", | 			leave_type="_Test_CF_leave_expiry", | ||||||
| 			from_date=add_days(nowdate(), -90), | 			from_date=add_days(nowdate(), -90), | ||||||
| @ -128,7 +126,7 @@ class TestLeaveAllocation(unittest.TestCase): | |||||||
| 			to_date=add_months(nowdate(), 12)) | 			to_date=add_months(nowdate(), 12)) | ||||||
| 		leave_allocation_1.submit() | 		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): | 	def test_creation_of_leave_ledger_entry_on_submit(self): | ||||||
| 		frappe.db.sql("delete from `tabLeave Allocation`") | 		frappe.db.sql("delete from `tabLeave Allocation`") | ||||||
|  | |||||||
| @ -387,10 +387,12 @@ class LeaveApplication(Document): | |||||||
| 			create_leave_ledger_entry(self, args, submit) | 			create_leave_ledger_entry(self, args, submit) | ||||||
| 
 | 
 | ||||||
| def get_allocation_expiry(employee, leave_type, to_date, from_date): | 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", | 	expiry =  frappe.get_all("Leave Ledger Entry", | ||||||
| 		filters={ | 		filters={ | ||||||
| 			'employee': employee, | 			'employee': employee, | ||||||
| 			'leave_type': leave_type, | 			'leave_type': leave_type, | ||||||
|  | 			'is_carry_forward': 1, | ||||||
| 			'transaction_type': 'Leave Allocation', | 			'transaction_type': 'Leave Allocation', | ||||||
| 			'to_date': ['between', (from_date, to_date)] | 			'to_date': ['between', (from_date, to_date)] | ||||||
| 		},fields=['to_date']) | 		},fields=['to_date']) | ||||||
| @ -487,7 +489,7 @@ def get_leave_allocation_records(employee, date, leave_type=None): | |||||||
| 			"from_date": d.from_date, | 			"from_date": d.from_date, | ||||||
| 			"to_date": d.to_date, | 			"to_date": d.to_date, | ||||||
| 			"total_leaves_allocated": flt(d.cf_leaves) + flt(d.new_leaves), | 			"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, | 			"new_leaves_allocated": d.new_leaves, | ||||||
| 			"leave_type": d.leave_type | 			"leave_type": d.leave_type | ||||||
| 		})) | 		})) | ||||||
| @ -515,12 +517,14 @@ def get_remaining_leaves(allocation, leaves_taken, date, expiry): | |||||||
| 
 | 
 | ||||||
| 		return remaining_leaves | 		return remaining_leaves | ||||||
| 
 | 
 | ||||||
| 	if expiry and allocation.carry_forwarded_leaves: | 	total_leaves = allocation.total_leaves_allocated | ||||||
| 		remaining_leaves = _get_remaining_leaves(allocation.carry_forwarded_leaves, expiry) |  | ||||||
| 
 | 
 | ||||||
| 		return flt(allocation.new_leaves_allocated) + flt(remaining_leaves) | 	if expiry and allocation.unused_leaves: | ||||||
| 	else: | 		remaining_leaves = _get_remaining_leaves(allocation.unused_leaves, expiry) | ||||||
| 		return _get_remaining_leaves(allocation.total_leaves_allocated, allocation.to_date) | 
 | ||||||
|  | 		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): | def get_leaves_for_period(employee, leave_type, from_date, to_date): | ||||||
| 	leave_entries = get_leave_entries(employee, leave_type, from_date, to_date) | 	leave_entries = get_leave_entries(employee, leave_type, from_date, to_date) | ||||||
|  | |||||||
| @ -542,6 +542,19 @@ class TestLeaveApplication(unittest.TestCase): | |||||||
| 		self.assertEquals(leave_ledger_entry[0].leaves, -9) | 		self.assertEquals(leave_ledger_entry[0].leaves, -9) | ||||||
| 		self.assertEquals(leave_ledger_entry[1].leaves, -2) | 		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): | def create_carry_forwarded_allocation(employee, leave_type): | ||||||
| 		# initial leave allocation | 		# initial leave allocation | ||||||
| 		leave_allocation = create_leave_allocation( | 		leave_allocation = create_leave_allocation( | ||||||
| @ -550,7 +563,7 @@ def create_carry_forwarded_allocation(employee, leave_type): | |||||||
| 			employee_name=employee.employee_name, | 			employee_name=employee.employee_name, | ||||||
| 			from_date=add_months(nowdate(), -24), | 			from_date=add_months(nowdate(), -24), | ||||||
| 			to_date=add_months(nowdate(), -12), | 			to_date=add_months(nowdate(), -12), | ||||||
| 			carry_forward=1) | 			carry_forward=0) | ||||||
| 		leave_allocation.submit() | 		leave_allocation.submit() | ||||||
| 
 | 
 | ||||||
| 		leave_allocation = create_leave_allocation( | 		leave_allocation = create_leave_allocation( | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user