From 6c226d2e28aa8d5228b75454a7906ba2cf2e27c7 Mon Sep 17 00:00:00 2001 From: krantheman Date: Wed, 10 Jul 2024 11:34:58 +0530 Subject: [PATCH] refactor: move get_monthly_earned_leave to server side --- .../leave_allocation/leave_allocation.js | 42 ++----------------- .../leave_allocation/leave_allocation.py | 25 +++++++++++ 2 files changed, 29 insertions(+), 38 deletions(-) diff --git a/hrms/hr/doctype/leave_allocation/leave_allocation.js b/hrms/hr/doctype/leave_allocation/leave_allocation.js index 6ca85faa2d..9be93bb2a1 100755 --- a/hrms/hr/doctype/leave_allocation/leave_allocation.js +++ b/hrms/hr/doctype/leave_allocation/leave_allocation.js @@ -51,7 +51,10 @@ frappe.ui.form.on("Leave Allocation", { }, add_allocate_leaves_button: async function (frm) { - const monthly_earned_leave = await frm.trigger("get_monthly_earned_leave"); + const { message: monthly_earned_leave } = await frappe.call({ + method: "get_monthly_earned_leave", + doc: frm.doc, + }); frm.add_custom_button(__("Allocate Leaves Manually"), function () { const dialog = new frappe.ui.Dialog({ @@ -81,43 +84,6 @@ frappe.ui.form.on("Leave Allocation", { }); }, - get_monthly_earned_leave: async function (frm) { - const { - message: { date_of_joining }, - } = await frappe.db.get_value("Employee", frm.doc.employee, "date_of_joining"); - - const { - message: { annual_allocation }, - } = await frappe.db.get_value( - "Leave Policy Detail", - { - parent: frm.doc.leave_policy, - leave_type: frm.doc.leave_type, - }, - "annual_allocation", - () => {}, - "Leave Policy", - ); - - const { - message: { earned_leave_frequency, rounding }, - } = await frappe.db.get_value("Leave Type", frm.doc.leave_type, [ - "earned_leave_frequency", - "rounding", - ]); - - const { message } = await frappe.call({ - method: "hrms.hr.utils.get_monthly_earned_leave", - args: { - date_of_joining: date_of_joining, - annual_leaves: annual_allocation, - frequency: earned_leave_frequency, - rounding: rounding, - }, - }); - return message; - }, - expire_allocation: function (frm) { frappe.call({ method: "hrms.hr.doctype.leave_ledger_entry.leave_ledger_entry.expire_allocation", diff --git a/hrms/hr/doctype/leave_allocation/leave_allocation.py b/hrms/hr/doctype/leave_allocation/leave_allocation.py index 000d3b9ba5..99a80ff30d 100755 --- a/hrms/hr/doctype/leave_allocation/leave_allocation.py +++ b/hrms/hr/doctype/leave_allocation/leave_allocation.py @@ -13,6 +13,7 @@ expire_allocation, ) from hrms.hr.utils import create_additional_leave_ledger_entry, get_leave_period, set_employee_name +from hrms.hr.utils import get_monthly_earned_leave as _get_monthly_earned_leave class OverlapError(frappe.ValidationError): @@ -346,6 +347,30 @@ def allocate_leaves_manually(self, new_leaves): ) self.add_comment(comment_type="Info", text=text) + @frappe.whitelist() + def get_monthly_earned_leave(self): + doj = frappe.db.get_value("Employee", self.employee, "date_of_joining") + + annual_allocation = frappe.db.get_value( + "Leave Policy Detail", + { + "parent": self.leave_policy, + "leave_type": self.leave_type, + }, + "annual_allocation", + ) + + frequency, rounding = frappe.db.get_value( + "Leave Type", + self.leave_type, + [ + "earned_leave_frequency", + "rounding", + ], + ) + + return _get_monthly_earned_leave(doj, annual_allocation, frequency, rounding) + def get_previous_allocation(from_date, leave_type, employee): """Returns document properties of previous allocation"""