Skip to content

Commit

Permalink
refactor: move get_monthly_earned_leave to server side
Browse files Browse the repository at this point in the history
  • Loading branch information
krantheman committed Jul 10, 2024
1 parent be286fa commit 6c226d2
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 38 deletions.
42 changes: 4 additions & 38 deletions hrms/hr/doctype/leave_allocation/leave_allocation.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down Expand Up @@ -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",
Expand Down
25 changes: 25 additions & 0 deletions hrms/hr/doctype/leave_allocation/leave_allocation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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"""
Expand Down

0 comments on commit 6c226d2

Please sign in to comment.