fix: Some enhancements and better validation

This commit is contained in:
Anurag Mishra
2020-08-17 14:51:25 +05:30
parent 0761301c2e
commit 34a7250a2d
6 changed files with 21 additions and 19 deletions

View File

@@ -6,7 +6,7 @@ from __future__ import unicode_literals
import frappe import frappe
from frappe import _, bold from frappe import _, bold
from frappe.model.document import Document from frappe.model.document import Document
from frappe.utils import flt, get_datetime from frappe.utils import flt, get_datetime, get_link_to_form
from math import floor from math import floor
from frappe.utils import get_datetime from frappe.utils import get_datetime
@@ -43,8 +43,7 @@ class Gratuity(Document):
""", (self.name, self.employee), as_dict=1)[0].paid_amount """, (self.name, self.employee), as_dict=1)[0].paid_amount
if flt(paid_amount) > self.amount: if flt(paid_amount) > self.amount:
frappe.throw(_("Row {0}# Paid Amount cannot be greater than Total amount"), frappe.throw(_("Row {0}# Paid Amount cannot be greater than Total amount"))
EmployeeAdvanceOverPayment)
self.db_set("paid_amount", paid_amount) self.db_set("paid_amount", paid_amount)
@@ -65,7 +64,7 @@ def calculate_work_experience(employee, gratuity_rule):
date_of_joining, relieving_date = frappe.db.get_value('Employee', employee, ['date_of_joining', 'relieving_date']) date_of_joining, relieving_date = frappe.db.get_value('Employee', employee, ['date_of_joining', 'relieving_date'])
if not relieving_date: if not relieving_date:
frappe.throw(_("Please set Relieving Date for employee: {0}").format(bold(employee))) 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") method = frappe.db.get_value("Gratuity Rule", gratuity_rule, "work_experience_calculation_function")
@@ -108,11 +107,13 @@ def get_non_working_days(employee, relieving_date, status):
filters["leave_type"] = ("IN", lwp_leave_types) filters["leave_type"] = ("IN", lwp_leave_types)
record = frappe.get_all("Attendance", filters=filters, fields = ["COUNT(name) as total_lwp"], debug = 1) record = frappe.get_all("Attendance", filters=filters, fields = ["COUNT(name) as total_lwp"])
return record[0].total_lwp if len(record) else 0 return record[0].total_lwp if len(record) else 0
def calculate_gratuity_amount(employee, gratuity_rule, experience): def calculate_gratuity_amount(employee, gratuity_rule, experience):
applicable_earnings_component = frappe.get_all("Gratuity Applicable Component", filters= {'parent': gratuity_rule}, fields=["salary_component"]) 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] applicable_earnings_component = [component.salary_component for component in applicable_earnings_component]
slabs = get_gratuity_rule_slabs(gratuity_rule) slabs = get_gratuity_rule_slabs(gratuity_rule)
@@ -137,16 +138,16 @@ def calculate_gratuity_amount(employee, gratuity_rule, experience):
slab_found = True slab_found = True
break break
if experience > slab.to_year and experience > slab.from_year: if experience > slab.to_year and experience > slab.from_year and slab.to_year !=0:
gratuity_amount += (slab.to_year - slab.from_year) * total_applicable_components_amount * slab.fraction_of_applicable_earnings gratuity_amount += (slab.to_year - slab.from_year) * total_applicable_components_amount * slab.fraction_of_applicable_earnings
year_left -= (slab.to_year - slab.from_year) year_left -= (slab.to_year - slab.from_year)
slab_found = True slab_found = True
elif slab.from_year <= experience < slab.to_year: elif slab.from_year <= experience and (experience < slab.to_year or slab.to_year == 0):
gratuity_amount += year_left * total_applicable_components_amount * slab.fraction_of_applicable_earnings gratuity_amount += year_left * total_applicable_components_amount * slab.fraction_of_applicable_earnings
slab_found = True slab_found = True
if not slab_found: if not slab_found:
frappe.throw(_("No Suitable Slab found for Calculation of gratuity amount in Gratuity Rule: {0}").format(bold(gratuity_rule))) frappe.throw(_("No Suitable Slab found for Calculation of gratuity amount in Gratuity Rule: {0}").format(bold(gratuity_rule)))
return gratuity_amount return gratuity_amount
@@ -174,7 +175,7 @@ def get_total_applicable_component_amount(employee, applicable_earnings_componen
return total_applicable_components_amount return total_applicable_components_amount
def get_gratuity_rule_slabs(gratuity_rule): def get_gratuity_rule_slabs(gratuity_rule):
return frappe.get_all("Gratuity Rule Slab", filters= {'parent': gratuity_rule}, fields = ["*"]) return frappe.get_all("Gratuity Rule Slab", filters= {'parent': gratuity_rule}, fields = ["*"], order_by="idx")
def get_salary_structure(employee): def get_salary_structure(employee):
return frappe.get_list("Salary Structure Assignment", filters = {"employee": employee, 'docstatus': 1}, fields=["from_date", "salary_structure"], order_by = "from_date desc")[0].salary_structure return frappe.get_list("Salary Structure Assignment", filters = {"employee": employee, 'docstatus': 1}, fields=["from_date", "salary_structure"], order_by = "from_date desc")[0].salary_structure

View File

@@ -19,6 +19,7 @@ frappe.ui.form.on('Gratuity Rule Slab', {
So, on row addition setting current_row.from = previous row.to. So, on row addition setting current_row.from = previous row.to.
On to_year insert we have to check that it is not less than from_year On to_year insert we have to check that it is not less than from_year
Wrong order may lead to Wrong Calculation
*/ */
@@ -33,7 +34,7 @@ frappe.ui.form.on('Gratuity Rule Slab', {
to_year(frm, cdt, cdn) { to_year(frm, cdt, cdn) {
let row = locals[cdt][cdn]; let row = locals[cdt][cdn];
if (row.to_year <= row.from_year){ if (row.to_year <= row.from_year && row.to_year === 0){
frappe.throw(__("To(Year) year can not be less than From(year) ")); frappe.throw(__("To(Year) year can not be less than From(year) "));
} }
} }

View File

@@ -28,7 +28,7 @@
"fieldtype": "Select", "fieldtype": "Select",
"in_list_view": 1, "in_list_view": 1,
"label": "Calculate Gratuity Amount Based on", "label": "Calculate Gratuity Amount Based on",
"options": "Current slab\nSum of all previous slabs", "options": "Current Slab\nSum of all previous slabs",
"reqd": 1 "reqd": 1
}, },
{ {
@@ -76,7 +76,7 @@
} }
], ],
"links": [], "links": [],
"modified": "2020-08-14 16:23:05.287545", "modified": "2020-08-17 14:17:02.594665",
"modified_by": "Administrator", "modified_by": "Administrator",
"module": "Payroll", "module": "Payroll",
"name": "Gratuity Rule", "name": "Gratuity Rule",

View File

@@ -11,8 +11,8 @@ class GratuityRule(Document):
def validate(self): def validate(self):
for current_slab in self.gratuity_rule_slabs: for current_slab in self.gratuity_rule_slabs:
if current_slab.from_year > current_slab.to_year: if (current_slab.from_year > current_slab.to_year) and current_slab.to_year != 0:
frappe(_("Row {0}: From (Year) can not be greater than To (Year)").format(slab.idx)) frappe(_("Row {0}: From (Year) can not be greater than To (Year)").format(current_slab.idx))
if current_slab.to_year == 0 and current_slab.from_year == 0 and len(self.gratuity_rule_slabs) > 1: if current_slab.to_year == 0 and current_slab.from_year == 0 and len(self.gratuity_rule_slabs) > 1:
frappe.throw(_("You can not define multiple slabs if you have a slab with no lower and upper limits.")) frappe.throw(_("You can not define multiple slabs if you have a slab with no lower and upper limits."))

View File

@@ -37,7 +37,7 @@
], ],
"istable": 1, "istable": 1,
"links": [], "links": [],
"modified": "2020-08-14 15:23:12.041375", "modified": "2020-08-17 14:09:56.781712",
"modified_by": "Administrator", "modified_by": "Administrator",
"module": "Payroll", "module": "Payroll",
"name": "Gratuity Rule Slab", "name": "Gratuity Rule Slab",

View File

@@ -161,7 +161,7 @@ def create_standard_documents():
# Rule Under Limited Contract # Rule Under Limited Contract
rule_1 = frappe.new_doc("Gratuity Rule") rule_1 = frappe.new_doc("Gratuity Rule")
rule_1.name = "Rule Under Limited Contract" rule_1.name = "Rule Under Limited Contract (UAE)"
rule_1.calculate_gratuity_amount_based_on = "Sum of all previous slabs" rule_1.calculate_gratuity_amount_based_on = "Sum of all previous slabs"
rule_1.work_experience_calculation_method = "Take Exact Completed Years" rule_1.work_experience_calculation_method = "Take Exact Completed Years"
rule_1.minimum_year_for_gratuity = 1 rule_1.minimum_year_for_gratuity = 1
@@ -186,7 +186,7 @@ def create_standard_documents():
# Rule Under Unlimited Contract on termination # Rule Under Unlimited Contract on termination
rule_2 = frappe.new_doc("Gratuity Rule") rule_2 = frappe.new_doc("Gratuity Rule")
rule_2.name = "Rule Under Unlimited Contract on termination" rule_2.name = "Rule Under Unlimited Contract on termination (UAE)"
rule_2.calculate_gratuity_amount_based_on = "Current Slab" rule_2.calculate_gratuity_amount_based_on = "Current Slab"
rule_2.work_experience_calculation_method = "Take Exact Completed Years" rule_2.work_experience_calculation_method = "Take Exact Completed Years"
rule_2.minimum_year_for_gratuity = 1 rule_2.minimum_year_for_gratuity = 1
@@ -211,7 +211,7 @@ def create_standard_documents():
# Rule Under Unlimited Contract # Rule Under Unlimited Contract
rule_3 = frappe.new_doc("Gratuity Rule") rule_3 = frappe.new_doc("Gratuity Rule")
rule_3.name = "Rule Under Unlimited Contract on resignation" rule_3.name = "Rule Under Unlimited Contract on resignation (UAE)"
rule_3.calculate_gratuity_amount_based_on = "Current Slab" rule_3.calculate_gratuity_amount_based_on = "Current Slab"
rule_3.work_experience_calculation_method = "Take Exact Completed Years" rule_3.work_experience_calculation_method = "Take Exact Completed Years"
rule_3.minimum_year_for_gratuity = 1 rule_3.minimum_year_for_gratuity = 1