Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(Gratuity): allow setting work experience manually (backport #1541) #1542

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 4 additions & 8 deletions hrms/payroll/doctype/gratuity/gratuity.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,22 +48,18 @@ frappe.ui.form.on('Gratuity', {
});
}
},

employee: function (frm) {
frm.events.calculate_work_experience_and_amount(frm);
},

gratuity_rule: function (frm) {
frm.events.calculate_work_experience_and_amount(frm);
},
calculate_work_experience_and_amount: function (frm) {

calculate_work_experience_and_amount: function (frm) {
if (frm.doc.employee && frm.doc.gratuity_rule) {
frappe.call({
method: "hrms.payroll.doctype.gratuity.gratuity.calculate_work_experience_and_amount",
args: {
employee: frm.doc.employee,
gratuity_rule: frm.doc.gratuity_rule
}
}).then((r) => {
frm.call("calculate_work_experience_and_amount").then((r) => {
frm.set_value("current_work_experience", r.message['current_work_experience']);
frm.set_value("amount", r.message['amount']);
});
Expand Down
5 changes: 2 additions & 3 deletions hrms/payroll/doctype/gratuity/gratuity.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,7 @@
"default": "0",
"fieldname": "current_work_experience",
"fieldtype": "Int",
"label": "Current Work Experience",
"read_only": 1
"label": "Current Work Experience"
},
{
"default": "0",
Expand Down Expand Up @@ -200,7 +199,7 @@
"index_web_pages_for_search": 1,
"is_submittable": 1,
"links": [],
"modified": "2022-11-09 15:47:13.353555",
"modified": "2024-03-15 02:50:10.282517",
"modified_by": "Administrator",
"module": "Payroll",
"name": "Gratuity",
Expand Down
46 changes: 30 additions & 16 deletions hrms/payroll/doctype/gratuity/gratuity.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,26 @@

class Gratuity(AccountsController):
def validate(self):
data = calculate_work_experience_and_amount(self.employee, self.gratuity_rule)
data = self.calculate_work_experience_and_amount()
self.current_work_experience = data["current_work_experience"]
self.amount = data["amount"]
self.set_status()

@frappe.whitelist()
def calculate_work_experience_and_amount(self):
rule = get_gratuity_rule_config(self.gratuity_rule)

if rule.method == "Manual":
current_work_experience = flt(self.current_work_experience)
else:
current_work_experience = calculate_work_experience(self.employee, self.gratuity_rule) or 0

gratuity_amount = (
calculate_gratuity_amount(self.employee, self.gratuity_rule, current_work_experience) or 0
)

return {"current_work_experience": current_work_experience, "amount": gratuity_amount}

def set_status(self, update=False):
precision = self.precision("paid_amount")
status = None
Expand Down Expand Up @@ -130,19 +145,21 @@ def set_total_advance_paid(self):
self.set_status(update=True)


@frappe.whitelist()
def calculate_work_experience_and_amount(employee, gratuity_rule):
current_work_experience = calculate_work_experience(employee, gratuity_rule) or 0
gratuity_amount = calculate_gratuity_amount(employee, gratuity_rule, current_work_experience) or 0

return {"current_work_experience": current_work_experience, "amount": gratuity_amount}
def get_gratuity_rule_config(gratuity_rule: str) -> dict:
return frappe.db.get_value(
"Gratuity Rule",
gratuity_rule,
[
"work_experience_calculation_function as method",
"total_working_days_per_year",
"minimum_year_for_gratuity",
],
as_dict=True,
)


def calculate_work_experience(employee, gratuity_rule):

total_working_days_per_year, minimum_year_for_gratuity = frappe.db.get_value(
"Gratuity Rule", gratuity_rule, ["total_working_days_per_year", "minimum_year_for_gratuity"]
)
rule = get_gratuity_rule_config(gratuity_rule)

date_of_joining, relieving_date = frappe.db.get_value(
"Employee", employee, ["date_of_joining", "relieving_date"]
Expand All @@ -154,16 +171,13 @@ def calculate_work_experience(employee, gratuity_rule):
)
)

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 = employee_total_workings_days / rule.total_working_days_per_year or 1
current_work_experience = get_work_experience_using_method(
method, current_work_experience, minimum_year_for_gratuity, employee
rule.method, current_work_experience, rule.minimum_year_for_gratuity, employee
)
return current_work_experience

Expand Down
6 changes: 3 additions & 3 deletions hrms/payroll/doctype/gratuity_rule/gratuity_rule.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@
"default": "Round off Work Experience",
"fieldname": "work_experience_calculation_function",
"fieldtype": "Select",
"label": "Work Experience Calculation method",
"options": "Round off Work Experience\nTake Exact Completed Years"
"label": "Work Experience Calculation Method",
"options": "Round off Work Experience\nTake Exact Completed Years\nManual"
},
{
"default": "365",
Expand Down Expand Up @@ -93,7 +93,7 @@
],
"index_web_pages_for_search": 1,
"links": [],
"modified": "2023-01-05 12:36:32.412409",
"modified": "2024-03-15 01:48:52.295003",
"modified_by": "Administrator",
"module": "Payroll",
"name": "Gratuity Rule",
Expand Down
Loading