From 1446749b63be791d309de2ecb29208135e2db2f7 Mon Sep 17 00:00:00 2001 From: Anurag Mishra Date: Thu, 17 Dec 2020 14:24:05 +0530 Subject: [PATCH] fix: changes requested --- erpnext/payroll/doctype/gratuity/gratuity.py | 53 ++++++++++++-------- 1 file changed, 32 insertions(+), 21 deletions(-) diff --git a/erpnext/payroll/doctype/gratuity/gratuity.py b/erpnext/payroll/doctype/gratuity/gratuity.py index 5a08f6a24c..7160bf0ef6 100644 --- a/erpnext/payroll/doctype/gratuity/gratuity.py +++ b/erpnext/payroll/doctype/gratuity/gratuity.py @@ -113,7 +113,13 @@ def calculate_work_experience(employee, gratuity_rule): frappe.throw(_("Please set Relieving Date for employee: {0}").format(bold(get_link_to_form("Employee", employee)))) method = frappe.db.get_value("Gratuity Rule", gratuity_rule, "work_experience_calculation_function") + employee_total_workings_days = calculate_employee_total_workings_days(employee, date_of_joining, relieving_date) + current_work_experience = employee_total_workings_days/total_working_days_per_year or 1 + current_work_experience = get_work_experience_using_method(method, current_work_experience, minimum_year_for_gratuity) + return current_work_experience + +def calculate_employee_total_workings_days(employee, date_of_joining, relieving_date ): employee_total_workings_days = (get_datetime(relieving_date) - get_datetime(date_of_joining)).days payroll_based_on = frappe.db.get_value("Payroll Settings", None, "payroll_based_on") or "Leave" @@ -124,10 +130,9 @@ def calculate_work_experience(employee, gratuity_rule): total_absents = get_non_working_days(employee, relieving_date, "Absent") employee_total_workings_days -= total_absents - # current_work_experience = time_difference.years - - current_work_experience = employee_total_workings_days/total_working_days_per_year or 1 + return employee_total_workings_days +def get_work_experience_using_method(method, current_work_experience, minimum_year_for_gratuity): if method == "Round off Work Experience": current_work_experience = round(current_work_experience) else: @@ -135,7 +140,6 @@ def calculate_work_experience(employee, gratuity_rule): if current_work_experience < minimum_year_for_gratuity: frappe.throw(_("Employee: {0} have to complete minimum {1} years for gratuity").format(bold(employee), minimum_year_for_gratuity)) - return current_work_experience def get_non_working_days(employee, relieving_date, status): @@ -157,27 +161,22 @@ def get_non_working_days(employee, relieving_date, status): return record[0].total_lwp if len(record) else 0 def calculate_gratuity_amount(employee, gratuity_rule, experience): - applicable_earnings_component = frappe.get_all("Gratuity Applicable Component", filters= {'parent': gratuity_rule}, fields=["salary_component"]) - if len(applicable_earnings_component) == 0: - frappe.throw(_("No Applicable Earnings Component found for Gratuity Rule: {0}").format(bold(get_link_to_form("Gratuity Rule",gratuity_rule)))) - applicable_earnings_component = [component.salary_component for component in applicable_earnings_component] - - slabs = get_gratuity_rule_slabs(gratuity_rule) - + applicable_earnings_component = get_applicable_components(gratuity_rule) total_applicable_components_amount = get_total_applicable_component_amount(employee, applicable_earnings_component, gratuity_rule) calculate_gratuity_amount_based_on = frappe.db.get_value("Gratuity Rule", gratuity_rule, "calculate_gratuity_amount_based_on") - gratuity_amount = 0 + slabs = get_gratuity_rule_slabs(gratuity_rule) slab_found = False year_left = experience + for slab in slabs: if calculate_gratuity_amount_based_on == "Current Slab": - if experience >= slab.from_year and (slab.to_year == 0 or experience < slab.to_year): - gratuity_amount = total_applicable_components_amount * experience * slab.fraction_of_applicable_earnings - if slab.fraction_of_applicable_earnings: - slab_found = True + slab_found, gratuity_amount = calculate_amount_based_on_current_slab(slab.from_year, slab.to_year, + experience, total_applicable_components_amount, slab.fraction_of_applicable_earnings) + if slab_found == True: break + elif calculate_gratuity_amount_based_on == "Sum of all previous slabs": if slab.to_year == 0 and slab.from_year == 0: gratuity_amount += year_left * total_applicable_components_amount * slab.fraction_of_applicable_earnings @@ -194,16 +193,20 @@ def calculate_gratuity_amount(employee, gratuity_rule, experience): if not slab_found: frappe.throw(_("No Suitable Slab found for Calculation of gratuity amount in Gratuity Rule: {0}").format(bold(gratuity_rule))) - - return gratuity_amount +def get_applicable_components(gratuity_rule): + applicable_earnings_component = frappe.get_all("Gratuity Applicable Component", filters= {'parent': gratuity_rule}, fields=["salary_component"]) + if len(applicable_earnings_component) == 0: + frappe.throw(_("No Applicable Earnings Component found for Gratuity Rule: {0}").format(bold(get_link_to_form("Gratuity Rule",gratuity_rule)))) + applicable_earnings_component = [component.salary_component for component in applicable_earnings_component] + + return applicable_earnings_component + def get_total_applicable_component_amount(employee, applicable_earnings_component, gratuity_rule): sal_slip = get_last_salary_slip(employee) - if not sal_slip: frappe.throw(_("No Salary Slip is found for Employee: {0}").format(bold(employee))) - component_and_amounts = frappe.get_list("Salary Detail", filters={ "docstatus": 1, @@ -217,9 +220,17 @@ def get_total_applicable_component_amount(employee, applicable_earnings_componen frappe.throw(_("No Applicable Component is present in last month salary slip")) for data in component_and_amounts: total_applicable_components_amount += data.amount - return total_applicable_components_amount +def calculate_amount_based_on_current_slab(from_year, to_year, experience, total_applicable_components_amount, fraction_of_applicable_earnings): + slab_found = False; gratuity_amount = 0 + if experience >= from_year and (to_year == 0 or experience < to_year): + gratuity_amount = total_applicable_components_amount * experience * fraction_of_applicable_earnings + if fraction_of_applicable_earnings: + slab_found = True + + return slab_found, gratuity_amount + def get_gratuity_rule_slabs(gratuity_rule): return frappe.get_all("Gratuity Rule Slab", filters= {'parent': gratuity_rule}, fields = ["*"], order_by="idx")