From 36585a2fa43e51774537cf9b6204610ce691d222 Mon Sep 17 00:00:00 2001 From: krantheman Date: Tue, 12 Dec 2023 17:03:48 +0530 Subject: [PATCH] refactor: date instead of month in dashboard table --- .../leave_allocation/leave_allocation.js | 43 ++++++++++--------- .../leave_allocation_dashboard.html | 10 ++--- hrms/hr/utils.py | 39 +++++++++++++++++ 3 files changed, 66 insertions(+), 26 deletions(-) diff --git a/hrms/hr/doctype/leave_allocation/leave_allocation.js b/hrms/hr/doctype/leave_allocation/leave_allocation.js index 14832f49c3..d90f946938 100755 --- a/hrms/hr/doctype/leave_allocation/leave_allocation.js +++ b/hrms/hr/doctype/leave_allocation/leave_allocation.js @@ -1,9 +1,10 @@ // Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors // License: GNU General Public License v3. See license.txt -frm.add_fetch("employee", "employee_name", "employee_name"); - frappe.ui.form.on("Leave Allocation", { + setup: function (frm) { + frm.add_fetch("employee", "employee_name", "employee_name"); + }, onload: function (frm) { // Ignore cancellation of doctype on cancel all. frm.ignore_doctypes_on_cancel_all = ["Leave Ledger Entry"]; @@ -228,25 +229,25 @@ frappe.ui.form.on("Leave Allocation", { return; $("div").remove(".form-dashboard-section.custom"); - const from_date_array = frm.doc.from_date.split("-"); - const from_month = from_date_array[1] - 1; - const to_date_array = frm.doc.to_date.split("-"); - let to_month = to_date_array[1] - 1; - if (to_date_array[0] > from_date_array[0]) to_month += 12; - - const months = []; - for (let i = from_month; i <= to_month; i++) { - months.push(moment().month(i).format("MMMM")); - } - - frm.dashboard.add_section( - frappe.render_template("leave_allocation_dashboard", { - months: months, - monthly_leaves: frm.monthly_leaves, - }), - __("Allocated Leaves") - ); - frm.dashboard.show(); + frappe.call({ + method: "hrms.hr.utils.get_monthly_allocations", + args: { + employee: frm.doc.employee, + leave_type: frm.doc.leave_type, + from_date: frm.doc.from_date, + to_date: frm.doc.to_date, + leave_policy: frm.doc.leave_policy, + }, + callback: function (r) { + frm.dashboard.add_section( + frappe.render_template("leave_allocation_dashboard", { + allocations: r.message + }), + __("Leaves Allocated") + ); + frm.dashboard.show(); + }, + }); }, }); diff --git a/hrms/hr/doctype/leave_allocation/leave_allocation_dashboard.html b/hrms/hr/doctype/leave_allocation/leave_allocation_dashboard.html index 9798e7b1e4..4baed7ebf8 100644 --- a/hrms/hr/doctype/leave_allocation/leave_allocation_dashboard.html +++ b/hrms/hr/doctype/leave_allocation/leave_allocation_dashboard.html @@ -1,16 +1,16 @@ -{% if monthly_leaves %} +{% if allocations.length %} - + - {% for(const value of months) { %} {% %} + {% for(const i of allocations) { %} {% %} - - + + {% } %} diff --git a/hrms/hr/utils.py b/hrms/hr/utils.py index a179fa045b..7b727c7b95 100644 --- a/hrms/hr/utils.py +++ b/hrms/hr/utils.py @@ -7,6 +7,7 @@ from frappe.query_builder import DocType from frappe.utils import ( add_days, + add_months, comma_and, cstr, flt, @@ -521,6 +522,44 @@ def allocate_leaves_manually(allocation_name, new_leaves): allocation.add_comment(comment_type="Info", text=text) +@frappe.whitelist() +def get_monthly_allocations(employee, leave_type, from_date, to_date, leave_policy): + annual_allocation = frappe.db.get_value( + "Leave Policy Detail", {"parent": leave_policy, "leave_type": leave_type}, "annual_allocation" + ) + date_of_joining = frappe.db.get_value("Employee", employee, "date_of_joining") + allocate_on_day, rounding = frappe.db.get_value( + "Leave Type", leave_type, ["allocate_on_day", "rounding"] + ) + monthly_earned_leave = get_monthly_earned_leave( + date_of_joining, annual_allocation, "Monthly", rounding, pro_rated=False + ) + + allocations = [] + date = get_monthly_allocation_date(from_date, allocate_on_day) + total_leaves = 0 + if date < from_date: + date = add_months(date, 1) + date = get_monthly_allocation_date(date, allocate_on_day) + while date <= to_date and total_leaves <= annual_allocation: + allocations.append({"date": date, "leaves": monthly_earned_leave}) + date = add_months(date, 1) + date = get_monthly_allocation_date(date, allocate_on_day) + total_leaves += monthly_earned_leave + return allocations + + +def get_monthly_allocation_date(date, allocate_on_day): + allocation_date = { + "First Day": get_first_day(date), + "Last Day": get_last_day(date), + "Date of Joining": date[:-2] + "15", + }[allocate_on_day] + return ( + allocation_date if allocate_on_day == "Date of Joining" else allocation_date.strftime("%Y-%m-%d") + ) + + def create_additional_leave_ledger_entry(allocation, leaves, date): """Create leave ledger entry for leave types""" allocation.new_leaves_allocated = leaves
{{ __("Month") }}{{ __("Date") }} {{ __("No. of Leaves") }}
{%= value %}{%= monthly_leaves %}{%= i.date %}{%= i.leaves %}