fix: add type hints for employee leave balance report
This commit is contained in:
parent
3f3b1766c2
commit
430bf00458
@ -3,6 +3,7 @@
|
|||||||
|
|
||||||
|
|
||||||
from itertools import groupby
|
from itertools import groupby
|
||||||
|
from typing import Dict, List, Tuple
|
||||||
|
|
||||||
import frappe
|
import frappe
|
||||||
from frappe import _
|
from frappe import _
|
||||||
@ -14,10 +15,9 @@ from erpnext.hr.doctype.leave_application.leave_application import (
|
|||||||
get_leaves_for_period,
|
get_leaves_for_period,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
Filters = frappe._dict
|
||||||
|
|
||||||
def execute(filters=None):
|
def execute(filters: Filters = None) -> Tuple:
|
||||||
filters = frappe._dict(filters or {})
|
|
||||||
|
|
||||||
if filters.to_date <= filters.from_date:
|
if filters.to_date <= filters.from_date:
|
||||||
frappe.throw(_('"From Date" can not be greater than or equal to "To Date"'))
|
frappe.throw(_('"From Date" can not be greater than or equal to "To Date"'))
|
||||||
|
|
||||||
@ -26,8 +26,9 @@ def execute(filters=None):
|
|||||||
charts = get_chart_data(data)
|
charts = get_chart_data(data)
|
||||||
return columns, data, None, charts
|
return columns, data, None, charts
|
||||||
|
|
||||||
def get_columns():
|
|
||||||
columns = [{
|
def get_columns() -> List[Dict]:
|
||||||
|
return [{
|
||||||
'label': _('Leave Type'),
|
'label': _('Leave Type'),
|
||||||
'fieldtype': 'Link',
|
'fieldtype': 'Link',
|
||||||
'fieldname': 'leave_type',
|
'fieldname': 'leave_type',
|
||||||
@ -72,9 +73,8 @@ def get_columns():
|
|||||||
'width': 150,
|
'width': 150,
|
||||||
}]
|
}]
|
||||||
|
|
||||||
return columns
|
|
||||||
|
|
||||||
def get_data(filters):
|
def get_data(filters: Filters) -> List:
|
||||||
leave_types = frappe.db.get_list('Leave Type', pluck='name', order_by='name')
|
leave_types = frappe.db.get_list('Leave Type', pluck='name', order_by='name')
|
||||||
conditions = get_conditions(filters)
|
conditions = get_conditions(filters)
|
||||||
|
|
||||||
@ -128,7 +128,7 @@ def get_data(filters):
|
|||||||
return data
|
return data
|
||||||
|
|
||||||
|
|
||||||
def get_opening_balance(employee, leave_type, filters, carry_forwarded_leaves):
|
def get_opening_balance(employee: str, leave_type: str, filters: Filters, carry_forwarded_leaves: float) -> float:
|
||||||
# allocation boundary condition
|
# allocation boundary condition
|
||||||
# opening balance is the closing leave balance 1 day before the filter start date
|
# opening balance is the closing leave balance 1 day before the filter start date
|
||||||
opening_balance_date = add_days(filters.from_date, -1)
|
opening_balance_date = add_days(filters.from_date, -1)
|
||||||
@ -146,7 +146,7 @@ def get_opening_balance(employee, leave_type, filters, carry_forwarded_leaves):
|
|||||||
return opening_balance
|
return opening_balance
|
||||||
|
|
||||||
|
|
||||||
def get_conditions(filters):
|
def get_conditions(filters: Filters) -> Dict:
|
||||||
conditions={
|
conditions={
|
||||||
'status': 'Active',
|
'status': 'Active',
|
||||||
}
|
}
|
||||||
@ -162,7 +162,7 @@ def get_conditions(filters):
|
|||||||
return conditions
|
return conditions
|
||||||
|
|
||||||
|
|
||||||
def get_department_leave_approver_map(department=None):
|
def get_department_leave_approver_map(department: str = None):
|
||||||
# get current department and all its child
|
# get current department and all its child
|
||||||
department_list = frappe.get_list('Department',
|
department_list = frappe.get_list('Department',
|
||||||
filters={'disabled': 0},
|
filters={'disabled': 0},
|
||||||
@ -190,7 +190,7 @@ def get_department_leave_approver_map(department=None):
|
|||||||
return approvers
|
return approvers
|
||||||
|
|
||||||
|
|
||||||
def get_allocated_and_expired_leaves(from_date, to_date, employee, leave_type):
|
def get_allocated_and_expired_leaves(from_date: str, to_date: str, employee: str, leave_type: str) -> Tuple[float, float, float]:
|
||||||
new_allocation = 0
|
new_allocation = 0
|
||||||
expired_leaves = 0
|
expired_leaves = 0
|
||||||
carry_forwarded_leaves = 0
|
carry_forwarded_leaves = 0
|
||||||
@ -219,7 +219,7 @@ def get_allocated_and_expired_leaves(from_date, to_date, employee, leave_type):
|
|||||||
return new_allocation, expired_leaves, carry_forwarded_leaves
|
return new_allocation, expired_leaves, carry_forwarded_leaves
|
||||||
|
|
||||||
|
|
||||||
def get_leave_ledger_entries(from_date, to_date, employee, leave_type):
|
def get_leave_ledger_entries(from_date: str, to_date: str, employee: str, leave_type: str) -> List[Dict]:
|
||||||
ledger = frappe.qb.DocType('Leave Ledger Entry')
|
ledger = frappe.qb.DocType('Leave Ledger Entry')
|
||||||
records = (
|
records = (
|
||||||
frappe.qb.from_(ledger)
|
frappe.qb.from_(ledger)
|
||||||
@ -243,7 +243,7 @@ def get_leave_ledger_entries(from_date, to_date, employee, leave_type):
|
|||||||
return records
|
return records
|
||||||
|
|
||||||
|
|
||||||
def get_chart_data(data):
|
def get_chart_data(data: List) -> Dict:
|
||||||
labels = []
|
labels = []
|
||||||
datasets = []
|
datasets = []
|
||||||
employee_data = data
|
employee_data = data
|
||||||
@ -263,7 +263,7 @@ def get_chart_data(data):
|
|||||||
return chart
|
return chart
|
||||||
|
|
||||||
|
|
||||||
def get_dataset_for_chart(employee_data, datasets, labels):
|
def get_dataset_for_chart(employee_data: List, datasets: List, labels: List) -> List:
|
||||||
leaves = []
|
leaves = []
|
||||||
employee_data = sorted(employee_data, key=lambda k: k['employee_name'])
|
employee_data = sorted(employee_data, key=lambda k: k['employee_name'])
|
||||||
|
|
||||||
|
@ -59,11 +59,11 @@ class TestEmployeeLeaveBalance(unittest.TestCase):
|
|||||||
leave_application = make_leave_application(self.employee_id, add_days(first_sunday, 1), add_days(first_sunday, 4), '_Test Leave Type')
|
leave_application = make_leave_application(self.employee_id, add_days(first_sunday, 1), add_days(first_sunday, 4), '_Test Leave Type')
|
||||||
leave_application.reload()
|
leave_application.reload()
|
||||||
|
|
||||||
filters = {
|
filters = frappe._dict({
|
||||||
'from_date': allocation1.from_date,
|
'from_date': allocation1.from_date,
|
||||||
'to_date': allocation2.to_date,
|
'to_date': allocation2.to_date,
|
||||||
'employee': self.employee_id
|
'employee': self.employee_id
|
||||||
}
|
})
|
||||||
|
|
||||||
report = execute(filters)
|
report = execute(filters)
|
||||||
|
|
||||||
@ -93,30 +93,30 @@ class TestEmployeeLeaveBalance(unittest.TestCase):
|
|||||||
leave_application.reload()
|
leave_application.reload()
|
||||||
|
|
||||||
# Case 1: opening balance for first alloc boundary
|
# Case 1: opening balance for first alloc boundary
|
||||||
filters = {
|
filters = frappe._dict({
|
||||||
'from_date': self.year_start,
|
'from_date': self.year_start,
|
||||||
'to_date': self.year_end,
|
'to_date': self.year_end,
|
||||||
'employee': self.employee_id
|
'employee': self.employee_id
|
||||||
}
|
})
|
||||||
report = execute(filters)
|
report = execute(filters)
|
||||||
self.assertEqual(report[1][0].opening_balance, 0)
|
self.assertEqual(report[1][0].opening_balance, 0)
|
||||||
|
|
||||||
# Case 2: opening balance after leave application date
|
# Case 2: opening balance after leave application date
|
||||||
filters = {
|
filters = frappe._dict({
|
||||||
'from_date': add_days(leave_application.to_date, 1),
|
'from_date': add_days(leave_application.to_date, 1),
|
||||||
'to_date': self.year_end,
|
'to_date': self.year_end,
|
||||||
'employee': self.employee_id
|
'employee': self.employee_id
|
||||||
}
|
})
|
||||||
report = execute(filters)
|
report = execute(filters)
|
||||||
self.assertEqual(report[1][0].opening_balance, (allocation1.new_leaves_allocated - leave_application.total_leave_days))
|
self.assertEqual(report[1][0].opening_balance, (allocation1.new_leaves_allocated - leave_application.total_leave_days))
|
||||||
|
|
||||||
# Case 3: leave balance shows actual balance and not consumption balance as per remaining days near alloc end date
|
# Case 3: leave balance shows actual balance and not consumption balance as per remaining days near alloc end date
|
||||||
# eg: 3 days left for alloc to end, leave balance should still be 26 and not 3
|
# eg: 3 days left for alloc to end, leave balance should still be 26 and not 3
|
||||||
filters = {
|
filters = frappe._dict({
|
||||||
'from_date': add_days(self.year_end, -3),
|
'from_date': add_days(self.year_end, -3),
|
||||||
'to_date': self.year_end,
|
'to_date': self.year_end,
|
||||||
'employee': self.employee_id
|
'employee': self.employee_id
|
||||||
}
|
})
|
||||||
report = execute(filters)
|
report = execute(filters)
|
||||||
self.assertEqual(report[1][0].opening_balance, (allocation1.new_leaves_allocated - leave_application.total_leave_days))
|
self.assertEqual(report[1][0].opening_balance, (allocation1.new_leaves_allocated - leave_application.total_leave_days))
|
||||||
|
|
||||||
@ -139,22 +139,22 @@ class TestEmployeeLeaveBalance(unittest.TestCase):
|
|||||||
carry_forward=True, leave_type=leave_type.name)
|
carry_forward=True, leave_type=leave_type.name)
|
||||||
|
|
||||||
# Case 1: carry forwarded leaves considered in opening balance for second alloc
|
# Case 1: carry forwarded leaves considered in opening balance for second alloc
|
||||||
filters = {
|
filters = frappe._dict({
|
||||||
'from_date': add_days(self.mid_year, 1),
|
'from_date': add_days(self.mid_year, 1),
|
||||||
'to_date': self.year_end,
|
'to_date': self.year_end,
|
||||||
'employee': self.employee_id
|
'employee': self.employee_id
|
||||||
}
|
})
|
||||||
report = execute(filters)
|
report = execute(filters)
|
||||||
# available leaves from old alloc
|
# available leaves from old alloc
|
||||||
opening_balance = allocation1.new_leaves_allocated - leave_application.total_leave_days
|
opening_balance = allocation1.new_leaves_allocated - leave_application.total_leave_days
|
||||||
self.assertEqual(report[1][0].opening_balance, opening_balance)
|
self.assertEqual(report[1][0].opening_balance, opening_balance)
|
||||||
|
|
||||||
# Case 2: opening balance one day after alloc boundary = carry forwarded leaves + new leaves alloc
|
# Case 2: opening balance one day after alloc boundary = carry forwarded leaves + new leaves alloc
|
||||||
filters = {
|
filters = frappe._dict({
|
||||||
'from_date': add_days(self.mid_year, 2),
|
'from_date': add_days(self.mid_year, 2),
|
||||||
'to_date': self.year_end,
|
'to_date': self.year_end,
|
||||||
'employee': self.employee_id
|
'employee': self.employee_id
|
||||||
}
|
})
|
||||||
report = execute(filters)
|
report = execute(filters)
|
||||||
# available leaves from old alloc
|
# available leaves from old alloc
|
||||||
opening_balance = allocation2.new_leaves_allocated + (allocation1.new_leaves_allocated - leave_application.total_leave_days)
|
opening_balance = allocation2.new_leaves_allocated + (allocation1.new_leaves_allocated - leave_application.total_leave_days)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user