Skip to content

Commit b0e4f30

Browse files
authored
feat(Gratuity): allow setting work experience manually (#1541)
1 parent be18fd8 commit b0e4f30

File tree

4 files changed

+39
-30
lines changed

4 files changed

+39
-30
lines changed

hrms/payroll/doctype/gratuity/gratuity.js

+4-8
Original file line numberDiff line numberDiff line change
@@ -48,22 +48,18 @@ frappe.ui.form.on('Gratuity', {
4848
});
4949
}
5050
},
51+
5152
employee: function (frm) {
5253
frm.events.calculate_work_experience_and_amount(frm);
5354
},
55+
5456
gratuity_rule: function (frm) {
5557
frm.events.calculate_work_experience_and_amount(frm);
5658
},
57-
calculate_work_experience_and_amount: function (frm) {
5859

60+
calculate_work_experience_and_amount: function (frm) {
5961
if (frm.doc.employee && frm.doc.gratuity_rule) {
60-
frappe.call({
61-
method: "hrms.payroll.doctype.gratuity.gratuity.calculate_work_experience_and_amount",
62-
args: {
63-
employee: frm.doc.employee,
64-
gratuity_rule: frm.doc.gratuity_rule
65-
}
66-
}).then((r) => {
62+
frm.call("calculate_work_experience_and_amount").then((r) => {
6763
frm.set_value("current_work_experience", r.message['current_work_experience']);
6864
frm.set_value("amount", r.message['amount']);
6965
});

hrms/payroll/doctype/gratuity/gratuity.json

+2-3
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,7 @@
6060
"default": "0",
6161
"fieldname": "current_work_experience",
6262
"fieldtype": "Int",
63-
"label": "Current Work Experience",
64-
"read_only": 1
63+
"label": "Current Work Experience"
6564
},
6665
{
6766
"default": "0",
@@ -200,7 +199,7 @@
200199
"index_web_pages_for_search": 1,
201200
"is_submittable": 1,
202201
"links": [],
203-
"modified": "2022-11-09 15:47:13.353555",
202+
"modified": "2024-03-15 02:50:10.282517",
204203
"modified_by": "Administrator",
205204
"module": "Payroll",
206205
"name": "Gratuity",

hrms/payroll/doctype/gratuity/gratuity.py

+30-16
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,26 @@
1515

1616
class Gratuity(AccountsController):
1717
def validate(self):
18-
data = calculate_work_experience_and_amount(self.employee, self.gratuity_rule)
18+
data = self.calculate_work_experience_and_amount()
1919
self.current_work_experience = data["current_work_experience"]
2020
self.amount = data["amount"]
2121
self.set_status()
2222

23+
@frappe.whitelist()
24+
def calculate_work_experience_and_amount(self):
25+
rule = get_gratuity_rule_config(self.gratuity_rule)
26+
27+
if rule.method == "Manual":
28+
current_work_experience = flt(self.current_work_experience)
29+
else:
30+
current_work_experience = calculate_work_experience(self.employee, self.gratuity_rule) or 0
31+
32+
gratuity_amount = (
33+
calculate_gratuity_amount(self.employee, self.gratuity_rule, current_work_experience) or 0
34+
)
35+
36+
return {"current_work_experience": current_work_experience, "amount": gratuity_amount}
37+
2338
def set_status(self, update=False):
2439
precision = self.precision("paid_amount")
2540
status = None
@@ -130,19 +145,21 @@ def set_total_advance_paid(self):
130145
self.set_status(update=True)
131146

132147

133-
@frappe.whitelist()
134-
def calculate_work_experience_and_amount(employee, gratuity_rule):
135-
current_work_experience = calculate_work_experience(employee, gratuity_rule) or 0
136-
gratuity_amount = calculate_gratuity_amount(employee, gratuity_rule, current_work_experience) or 0
137-
138-
return {"current_work_experience": current_work_experience, "amount": gratuity_amount}
148+
def get_gratuity_rule_config(gratuity_rule: str) -> dict:
149+
return frappe.db.get_value(
150+
"Gratuity Rule",
151+
gratuity_rule,
152+
[
153+
"work_experience_calculation_function as method",
154+
"total_working_days_per_year",
155+
"minimum_year_for_gratuity",
156+
],
157+
as_dict=True,
158+
)
139159

140160

141161
def calculate_work_experience(employee, gratuity_rule):
142-
143-
total_working_days_per_year, minimum_year_for_gratuity = frappe.db.get_value(
144-
"Gratuity Rule", gratuity_rule, ["total_working_days_per_year", "minimum_year_for_gratuity"]
145-
)
162+
rule = get_gratuity_rule_config(gratuity_rule)
146163

147164
date_of_joining, relieving_date = frappe.db.get_value(
148165
"Employee", employee, ["date_of_joining", "relieving_date"]
@@ -154,16 +171,13 @@ def calculate_work_experience(employee, gratuity_rule):
154171
)
155172
)
156173

157-
method = frappe.db.get_value(
158-
"Gratuity Rule", gratuity_rule, "work_experience_calculation_function"
159-
)
160174
employee_total_workings_days = calculate_employee_total_workings_days(
161175
employee, date_of_joining, relieving_date
162176
)
163177

164-
current_work_experience = employee_total_workings_days / total_working_days_per_year or 1
178+
current_work_experience = employee_total_workings_days / rule.total_working_days_per_year or 1
165179
current_work_experience = get_work_experience_using_method(
166-
method, current_work_experience, minimum_year_for_gratuity, employee
180+
rule.method, current_work_experience, rule.minimum_year_for_gratuity, employee
167181
)
168182
return current_work_experience
169183

hrms/payroll/doctype/gratuity_rule/gratuity_rule.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@
6363
"default": "Round off Work Experience",
6464
"fieldname": "work_experience_calculation_function",
6565
"fieldtype": "Select",
66-
"label": "Work Experience Calculation method",
67-
"options": "Round off Work Experience\nTake Exact Completed Years"
66+
"label": "Work Experience Calculation Method",
67+
"options": "Round off Work Experience\nTake Exact Completed Years\nManual"
6868
},
6969
{
7070
"default": "365",
@@ -93,7 +93,7 @@
9393
],
9494
"index_web_pages_for_search": 1,
9595
"links": [],
96-
"modified": "2023-01-05 12:36:32.412409",
96+
"modified": "2024-03-15 01:48:52.295003",
9797
"modified_by": "Administrator",
9898
"module": "Payroll",
9999
"name": "Gratuity Rule",

0 commit comments

Comments
 (0)