From 3f160934fd465644ab3b4a340f3f50e9ed6c4d19 Mon Sep 17 00:00:00 2001 From: barredterra <14891507+barredterra@users.noreply.github.com> Date: Mon, 12 Feb 2024 14:09:30 +0100 Subject: [PATCH 01/13] fix(Leave Application): calendar view (cherry picked from commit 19cebe0c8040aa46bde4f948728fddabd5db6d67) --- .../doctype/leave_application/leave_application.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/hrms/hr/doctype/leave_application/leave_application.py b/hrms/hr/doctype/leave_application/leave_application.py index 77f94da99a..0465ad3290 100755 --- a/hrms/hr/doctype/leave_application/leave_application.py +++ b/hrms/hr/doctype/leave_application/leave_application.py @@ -1233,7 +1233,13 @@ def add_leaves(events, start, end, filter_conditions=None): color FROM `tabLeave Application` WHERE - from_date <= %(end)s AND to_date >= %(start)s <= to_date + ( + (%(start)s <= from_date AND from_date <= %(end)s) + OR (%(start)s <= to_date AND to_date <= %(end)s) + OR ( + from_date < %(start)s AND to_date > %(end)s + ) + ) AND docstatus < 2 AND status in ('Approved', 'Open') """ @@ -1244,7 +1250,7 @@ def add_leaves(events, start, end, filter_conditions=None): if filter_conditions: query += filter_conditions - for d in frappe.db.sql(query, {"start": start, "end": end}, as_dict=True): + for d in frappe.db.sql(query, {"start": getdate(start), "end": getdate(end)}, as_dict=True): e = { "name": d.name, "doctype": "Leave Application", @@ -1252,7 +1258,7 @@ def add_leaves(events, start, end, filter_conditions=None): "to_date": d.to_date, "docstatus": d.docstatus, "color": d.color, - "all_day": int(not d.half_day), + "allDay": int(not d.half_day), "title": cstr(d.employee_name) + f" ({cstr(d.leave_type)})" + (" " + _("(Half Day)") if d.half_day else ""), From 867d838ac975843c7688743632b1c97b04e252a7 Mon Sep 17 00:00:00 2001 From: barredterra <14891507+barredterra@users.noreply.github.com> Date: Mon, 12 Feb 2024 14:34:43 +0100 Subject: [PATCH 02/13] fix: show blocked dates as "allDay" (cherry picked from commit 69a759cec9053061f9931f5ce327486cfb361b96) --- hrms/hr/doctype/leave_application/leave_application.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/hrms/hr/doctype/leave_application/leave_application.py b/hrms/hr/doctype/leave_application/leave_application.py index 0465ad3290..2ece51aaea 100755 --- a/hrms/hr/doctype/leave_application/leave_application.py +++ b/hrms/hr/doctype/leave_application/leave_application.py @@ -1268,7 +1268,6 @@ def add_leaves(events, start, end, filter_conditions=None): def add_block_dates(events, start, end, employee, company): - # block days cnt = 0 block_dates = get_applicable_block_dates(start, end, employee, company, all_lists=True) @@ -1280,6 +1279,7 @@ def add_block_dates(events, start, end, employee, company): "to_date": block_date.block_date, "title": _("Leave Blocked") + ": " + block_date.reason, "name": "_" + str(cnt), + "allDay": 1, } ) cnt += 1 @@ -1303,6 +1303,7 @@ def add_holidays(events, start, end, employee, company): "to_date": holiday.holiday_date, "title": _("Holiday") + ": " + cstr(holiday.description), "name": holiday.name, + "allDay": True, } ) From 1e7064ca8b1dbefe0ff65044a88254d728996f6e Mon Sep 17 00:00:00 2001 From: barredterra <14891507+barredterra@users.noreply.github.com> Date: Mon, 12 Feb 2024 14:35:24 +0100 Subject: [PATCH 03/13] fix: remove unused whitelisting from get_applicable_block_dates (cherry picked from commit a6d072b33264d00067928969e8213b1e098a2be6) --- hrms/hr/doctype/leave_block_list/leave_block_list.py | 1 - 1 file changed, 1 deletion(-) diff --git a/hrms/hr/doctype/leave_block_list/leave_block_list.py b/hrms/hr/doctype/leave_block_list/leave_block_list.py index ce7bb453a7..a3571aed81 100644 --- a/hrms/hr/doctype/leave_block_list/leave_block_list.py +++ b/hrms/hr/doctype/leave_block_list/leave_block_list.py @@ -42,7 +42,6 @@ def get_block_dates_from_date(self, start_date, end_date, days): return date_list -@frappe.whitelist() def get_applicable_block_dates( from_date, to_date, employee=None, company=None, all_lists=False, leave_type=None ): From bead61fa01caedfb69246594ad0bfaad9dac0356 Mon Sep 17 00:00:00 2001 From: barredterra <14891507+barredterra@users.noreply.github.com> Date: Mon, 12 Feb 2024 14:37:34 +0100 Subject: [PATCH 04/13] fix: get blocked days in the correct order If company is available and employee is not, we should still return the block lists that apply to all departments. (cherry picked from commit c2699b67308628258e3f97a9c4f8f92dd92fb263) --- .../leave_block_list/leave_block_list.py | 41 ++++++++++--------- 1 file changed, 21 insertions(+), 20 deletions(-) diff --git a/hrms/hr/doctype/leave_block_list/leave_block_list.py b/hrms/hr/doctype/leave_block_list/leave_block_list.py index a3571aed81..875090eb1c 100644 --- a/hrms/hr/doctype/leave_block_list/leave_block_list.py +++ b/hrms/hr/doctype/leave_block_list/leave_block_list.py @@ -58,33 +58,34 @@ def get_applicable_block_dates( def get_applicable_block_lists(employee=None, company=None, all_lists=False, leave_type=None): block_lists = [] - if not employee: - employee = frappe.db.get_value("Employee", {"user_id": frappe.session.user}) - if not employee: - return [] - - if not company: - company = frappe.db.get_value("Employee", employee, "company") - def add_block_list(block_list): for d in block_list: if all_lists or not is_user_in_allow_list(d): block_lists.append(d) - # per department - department = frappe.db.get_value("Employee", employee, "department") - if department: - block_list = frappe.db.get_value("Department", department, "leave_block_list") - block_list_leave_type = frappe.db.get_value("Leave Block List", block_list, "leave_type") - if not block_list_leave_type or not leave_type or block_list_leave_type == leave_type: - add_block_list([block_list]) + if not company: + company = frappe.db.get_value("Employee", employee, "company") + + if company: + # global + conditions = {"applies_to_all_departments": 1, "company": company} + if leave_type: + conditions["leave_type"] = ["IN", (leave_type, "", None)] + + add_block_list(frappe.db.get_all("Leave Block List", filters=conditions, pluck="name")) + + if not employee: + employee = frappe.db.get_value("Employee", {"user_id": frappe.session.user}) - # global - conditions = {"applies_to_all_departments": 1, "company": company} - if leave_type: - conditions["leave_type"] = ["IN", (leave_type, "", None)] + if employee: + # per department + department = frappe.db.get_value("Employee", employee, "department") + if department: + block_list = frappe.db.get_value("Department", department, "leave_block_list") + block_list_leave_type = frappe.db.get_value("Leave Block List", block_list, "leave_type") + if not block_list_leave_type or not leave_type or block_list_leave_type == leave_type: + add_block_list([block_list]) - add_block_list(frappe.db.get_all("Leave Block List", filters=conditions, pluck="name")) return list(set(block_lists)) From 859dd8b3fc35dfc108f3781dfb2c2602df3df8cd Mon Sep 17 00:00:00 2001 From: barredterra <14891507+barredterra@users.noreply.github.com> Date: Mon, 12 Feb 2024 14:37:52 +0100 Subject: [PATCH 05/13] fix: convert datetime to date (cherry picked from commit e6882e47e97ba9d88f31bab3679d04567e2e44cd) --- hrms/hr/doctype/leave_block_list/leave_block_list.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hrms/hr/doctype/leave_block_list/leave_block_list.py b/hrms/hr/doctype/leave_block_list/leave_block_list.py index 875090eb1c..755f3e377d 100644 --- a/hrms/hr/doctype/leave_block_list/leave_block_list.py +++ b/hrms/hr/doctype/leave_block_list/leave_block_list.py @@ -49,7 +49,7 @@ def get_applicable_block_dates( "Leave Block List Date", filters={ "parent": ["IN", get_applicable_block_lists(employee, company, all_lists, leave_type)], - "block_date": ["BETWEEN", [from_date, to_date]], + "block_date": ["BETWEEN", [getdate(from_date), getdate(to_date)]], }, fields=["block_date", "reason"], ) From 3332d54f554dd9ba20de73cd49afaeedb812812a Mon Sep 17 00:00:00 2001 From: barredterra <14891507+barredterra@users.noreply.github.com> Date: Tue, 12 Mar 2024 11:28:37 +0100 Subject: [PATCH 06/13] refactor: use `1` instead of `True` (cherry picked from commit 0ee5767a17d1104fd9467f6135dc19ab33cc1de3) --- hrms/hr/doctype/leave_application/leave_application.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hrms/hr/doctype/leave_application/leave_application.py b/hrms/hr/doctype/leave_application/leave_application.py index 2ece51aaea..36b5e73ecd 100755 --- a/hrms/hr/doctype/leave_application/leave_application.py +++ b/hrms/hr/doctype/leave_application/leave_application.py @@ -1303,7 +1303,7 @@ def add_holidays(events, start, end, employee, company): "to_date": holiday.holiday_date, "title": _("Holiday") + ": " + cstr(holiday.description), "name": holiday.name, - "allDay": True, + "allDay": 1, } ) From 764a1ff7077223c789905a9cd287a1142cc10c6d Mon Sep 17 00:00:00 2001 From: barredterra <14891507+barredterra@users.noreply.github.com> Date: Tue, 12 Mar 2024 11:29:42 +0100 Subject: [PATCH 07/13] fix: set employee before company (cherry picked from commit 9da9fea8c15558d9adb62f98aaa790a59bbeda6f) --- hrms/hr/doctype/leave_block_list/leave_block_list.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/hrms/hr/doctype/leave_block_list/leave_block_list.py b/hrms/hr/doctype/leave_block_list/leave_block_list.py index 755f3e377d..14c94d8500 100644 --- a/hrms/hr/doctype/leave_block_list/leave_block_list.py +++ b/hrms/hr/doctype/leave_block_list/leave_block_list.py @@ -63,9 +63,12 @@ def add_block_list(block_list): if all_lists or not is_user_in_allow_list(d): block_lists.append(d) - if not company: + if not employee: + employee = frappe.db.get_value("Employee", {"user_id": frappe.session.user}) + + if not company and employee: company = frappe.db.get_value("Employee", employee, "company") - + if company: # global conditions = {"applies_to_all_departments": 1, "company": company} @@ -74,9 +77,6 @@ def add_block_list(block_list): add_block_list(frappe.db.get_all("Leave Block List", filters=conditions, pluck="name")) - if not employee: - employee = frappe.db.get_value("Employee", {"user_id": frappe.session.user}) - if employee: # per department department = frappe.db.get_value("Employee", employee, "department") From 8ad3a637168d06ad039a33aca52e6107414775f4 Mon Sep 17 00:00:00 2001 From: barredterra <14891507+barredterra@users.noreply.github.com> Date: Tue, 12 Mar 2024 11:51:50 +0100 Subject: [PATCH 08/13] fix: half day I doesn't make sense to mark the entire leave of, say, 5 days as "not all day" or "half day", just because the first or the last of the 5 days is a half day leave. (cherry picked from commit af8b19cdb15808c51d1739267e074d019b9099f8) --- hrms/hr/doctype/leave_application/leave_application.py | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/hrms/hr/doctype/leave_application/leave_application.py b/hrms/hr/doctype/leave_application/leave_application.py index 36b5e73ecd..1ce0f33211 100755 --- a/hrms/hr/doctype/leave_application/leave_application.py +++ b/hrms/hr/doctype/leave_application/leave_application.py @@ -1228,7 +1228,6 @@ def add_leaves(events, start, end, filter_conditions=None): leave_type, from_date, to_date, - half_day, status, color FROM `tabLeave Application` @@ -1258,10 +1257,8 @@ def add_leaves(events, start, end, filter_conditions=None): "to_date": d.to_date, "docstatus": d.docstatus, "color": d.color, - "allDay": int(not d.half_day), - "title": cstr(d.employee_name) - + f" ({cstr(d.leave_type)})" - + (" " + _("(Half Day)") if d.half_day else ""), + "allDay": 1, + "title": f"{d.employee_name} ({d.leave_type})", } if e not in events: events.append(e) From 680358fed65659e161472390c966ee72070fbe4a Mon Sep 17 00:00:00 2001 From: krantheman Date: Thu, 14 Mar 2024 13:54:13 +0530 Subject: [PATCH 09/13] refactor: use get_list instead of sql and simplify date range condition (cherry picked from commit 83fd0bc29fcb325fe5cf3a48cc43cc04a6d416d5) --- .../leave_application/leave_application.py | 118 +++++++----------- 1 file changed, 47 insertions(+), 71 deletions(-) diff --git a/hrms/hr/doctype/leave_application/leave_application.py b/hrms/hr/doctype/leave_application/leave_application.py index 1ce0f33211..e8181d6ded 100755 --- a/hrms/hr/doctype/leave_application/leave_application.py +++ b/hrms/hr/doctype/leave_application/leave_application.py @@ -1164,7 +1164,11 @@ def is_lwp(leave_type): @frappe.whitelist() def get_events(start, end, filters=None): - from frappe.desk.reportview import get_filters_cond + import json + + filters = json.loads(filters) + for idx, filter in enumerate(filters): + filters[idx] = filter[1:-1] events = [] @@ -1178,12 +1182,11 @@ def get_events(start, end, filters=None): employee = "" company = frappe.db.get_value("Global Defaults", None, "default_company") - conditions = get_filters_cond("Leave Application", filters, []) # show department leaves for employee if "Employee" in frappe.get_roles(): add_department_leaves(events, start, end, employee, company) - add_leaves(events, start, end, conditions) + add_leaves(events, start, end, filters) add_block_dates(events, start, end, employee, company) add_holidays(events, start, end, employee, company) @@ -1191,77 +1194,50 @@ def get_events(start, end, filters=None): def add_department_leaves(events, start, end, employee, company): - department = frappe.db.get_value("Employee", employee, "department") - - if not department: - return - - # department leaves - department_employees = frappe.db.sql_list( - """select name from tabEmployee where department=%s - and company=%s""", - (department, company), + if department := frappe.db.get_value("Employee", employee, "department"): + department_employees = frappe.get_list( + "Employee", filters={"department": department, "company": company}, pluck="name" + ) + filters = [["employee", "in", department_employees]] + add_leaves(events, start, end, filters=filters) + + +def add_leaves(events, start, end, filters=None): + if not filters: + filters = [] + filters.extend( + [ + ["from_date", "<=", getdate(end)], + ["to_date", ">=", getdate(start)], + ["status", "in", ["Approved", "Open"]], + ["docstatus", "<", 2], + ] ) - filter_conditions = ' and employee in ("%s")' % '", "'.join(department_employees) - add_leaves(events, start, end, filter_conditions=filter_conditions) - - -def add_leaves(events, start, end, filter_conditions=None): - from frappe.desk.reportview import build_match_conditions - - conditions = [] - - if not cint( - frappe.db.get_value("HR Settings", None, "show_leaves_of_all_department_members_in_calendar") - ): - match_conditions = build_match_conditions("Leave Application") - - if match_conditions: - conditions.append(match_conditions) - - query = """SELECT - docstatus, - name, - employee, - employee_name, - leave_type, - from_date, - to_date, - status, - color - FROM `tabLeave Application` - WHERE - ( - (%(start)s <= from_date AND from_date <= %(end)s) - OR (%(start)s <= to_date AND to_date <= %(end)s) - OR ( - from_date < %(start)s AND to_date > %(end)s - ) - ) - AND docstatus < 2 - AND status in ('Approved', 'Open') - """ + fields = [ + "name", + "from_date", + "to_date", + "color", + "docstatus", + "employee_name", + "leave_type", + "(1) as allDay", + "'Leave Application' as doctype", + ] + + show_leaves_of_all_members = frappe.db.get_value( + "HR Settings", None, "show_leaves_of_all_department_members_in_calendar" + ) + if cint(show_leaves_of_all_members): + leave_applications = frappe.get_all("Leave Application", filters=filters, fields=fields) + else: + leave_applications = frappe.get_list("Leave Application", filters=filters, fields=fields) - if conditions: - query += " AND " + " AND ".join(conditions) - - if filter_conditions: - query += filter_conditions - - for d in frappe.db.sql(query, {"start": getdate(start), "end": getdate(end)}, as_dict=True): - e = { - "name": d.name, - "doctype": "Leave Application", - "from_date": d.from_date, - "to_date": d.to_date, - "docstatus": d.docstatus, - "color": d.color, - "allDay": 1, - "title": f"{d.employee_name} ({d.leave_type})", - } - if e not in events: - events.append(e) + for d in leave_applications: + if d not in events: + d["title"] = f"{d['employee_name']} ({d['leave_type']})" + events.append(d) def add_block_dates(events, start, end, employee, company): From ad05477cffcac37d74f893314fa92a3f0c39e4fc Mon Sep 17 00:00:00 2001 From: krantheman Date: Thu, 14 Mar 2024 13:59:40 +0530 Subject: [PATCH 10/13] fix: remove employee_name and leave_type before checking in events (cherry picked from commit deda120a7436bc0ae365a20c5afe8da91f7c4c94) --- hrms/hr/doctype/leave_application/leave_application.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/hrms/hr/doctype/leave_application/leave_application.py b/hrms/hr/doctype/leave_application/leave_application.py index e8181d6ded..ccd9795917 100755 --- a/hrms/hr/doctype/leave_application/leave_application.py +++ b/hrms/hr/doctype/leave_application/leave_application.py @@ -1235,8 +1235,10 @@ def add_leaves(events, start, end, filters=None): leave_applications = frappe.get_list("Leave Application", filters=filters, fields=fields) for d in leave_applications: + d["title"] = f"{d['employee_name']} ({d['leave_type']})" + del d["employee_name"] + del d["leave_type"] if d not in events: - d["title"] = f"{d['employee_name']} ({d['leave_type']})" events.append(d) From c2d87432152e048abb88a536ef86ed14c921356f Mon Sep 17 00:00:00 2001 From: krantheman Date: Thu, 14 Mar 2024 14:03:50 +0530 Subject: [PATCH 11/13] fix: use get_single_value instead of get_value (cherry picked from commit 5ec91239d2cfc7e43d6909251b1787bcd3033a6d) --- hrms/hr/doctype/leave_application/leave_application.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/hrms/hr/doctype/leave_application/leave_application.py b/hrms/hr/doctype/leave_application/leave_application.py index ccd9795917..bc5da84999 100755 --- a/hrms/hr/doctype/leave_application/leave_application.py +++ b/hrms/hr/doctype/leave_application/leave_application.py @@ -1226,8 +1226,8 @@ def add_leaves(events, start, end, filters=None): "'Leave Application' as doctype", ] - show_leaves_of_all_members = frappe.db.get_value( - "HR Settings", None, "show_leaves_of_all_department_members_in_calendar" + show_leaves_of_all_members = frappe.db.get_single_value( + "HR Settings", "show_leaves_of_all_department_members_in_calendar" ) if cint(show_leaves_of_all_members): leave_applications = frappe.get_all("Leave Application", filters=filters, fields=fields) From ae8760fec55217251adce7c1a979fad1c167f46a Mon Sep 17 00:00:00 2001 From: krantheman Date: Thu, 14 Mar 2024 14:58:28 +0530 Subject: [PATCH 12/13] chore: add comment (cherry picked from commit 4e5e3e6937b3cfadb072d79d9a010b17f52c3a2a) --- hrms/hr/doctype/leave_application/leave_application.py | 1 + 1 file changed, 1 insertion(+) diff --git a/hrms/hr/doctype/leave_application/leave_application.py b/hrms/hr/doctype/leave_application/leave_application.py index bc5da84999..30c53dc67a 100755 --- a/hrms/hr/doctype/leave_application/leave_application.py +++ b/hrms/hr/doctype/leave_application/leave_application.py @@ -1168,6 +1168,7 @@ def get_events(start, end, filters=None): filters = json.loads(filters) for idx, filter in enumerate(filters): + # taking relevant fields from the list [doctype, fieldname, condition, value, hidden] filters[idx] = filter[1:-1] events = [] From 04f86ab6da0eabe1471cac7258072677d2278952 Mon Sep 17 00:00:00 2001 From: "mergify[bot]" <37929162+mergify[bot]@users.noreply.github.com> Date: Fri, 15 Mar 2024 07:28:02 +0530 Subject: [PATCH 13/13] feat(Gratuity): allow setting work experience manually (#1541) (#1542) (cherry picked from commit b0e4f302fc658c4fbd3487cdd6a58d95eb215cce) Co-authored-by: Rucha Mahabal --- hrms/payroll/doctype/gratuity/gratuity.js | 12 ++--- hrms/payroll/doctype/gratuity/gratuity.json | 5 +- hrms/payroll/doctype/gratuity/gratuity.py | 46 ++++++++++++------- .../doctype/gratuity_rule/gratuity_rule.json | 6 +-- 4 files changed, 39 insertions(+), 30 deletions(-) diff --git a/hrms/payroll/doctype/gratuity/gratuity.js b/hrms/payroll/doctype/gratuity/gratuity.js index 4f311589a3..21d01a24de 100644 --- a/hrms/payroll/doctype/gratuity/gratuity.js +++ b/hrms/payroll/doctype/gratuity/gratuity.js @@ -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']); }); diff --git a/hrms/payroll/doctype/gratuity/gratuity.json b/hrms/payroll/doctype/gratuity/gratuity.json index e4a8d5b8ec..6c1927d029 100644 --- a/hrms/payroll/doctype/gratuity/gratuity.json +++ b/hrms/payroll/doctype/gratuity/gratuity.json @@ -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", @@ -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", diff --git a/hrms/payroll/doctype/gratuity/gratuity.py b/hrms/payroll/doctype/gratuity/gratuity.py index 06e218b513..63085ef010 100644 --- a/hrms/payroll/doctype/gratuity/gratuity.py +++ b/hrms/payroll/doctype/gratuity/gratuity.py @@ -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 @@ -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"] @@ -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 diff --git a/hrms/payroll/doctype/gratuity_rule/gratuity_rule.json b/hrms/payroll/doctype/gratuity_rule/gratuity_rule.json index ed66dc9a1a..0df1cf38e1 100644 --- a/hrms/payroll/doctype/gratuity_rule/gratuity_rule.json +++ b/hrms/payroll/doctype/gratuity_rule/gratuity_rule.json @@ -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", @@ -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",