Skip to content

Commit

Permalink
refactor: date instead of month in dashboard table
Browse files Browse the repository at this point in the history
  • Loading branch information
krantheman committed Dec 12, 2023
1 parent 378f4be commit 36585a2
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 26 deletions.
43 changes: 22 additions & 21 deletions hrms/hr/doctype/leave_allocation/leave_allocation.js
Original file line number Diff line number Diff line change
@@ -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"];
Expand Down Expand Up @@ -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();
},
});
},
});

Expand Down
10 changes: 5 additions & 5 deletions hrms/hr/doctype/leave_allocation/leave_allocation_dashboard.html
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
{% if monthly_leaves %}
{% if allocations.length %}
<table class="table table-bordered small">
<thead>
<tr>
<th style="width: 50%">{{ __("Month") }}</th>
<th style="width: 50%">{{ __("Date") }}</th>
<th style="width: 50%">{{ __("No. of Leaves") }}</th>
</tr>
</thead>
<tbody>
{% for(const value of months) { %} {% %}
{% for(const i of allocations) { %} {% %}
<tr>
<td>{%= value %}</td>
<td>{%= monthly_leaves %}</td>
<td>{%= i.date %}</td>
<td>{%= i.leaves %}</td>
</tr>
{% } %}
</tbody>
Expand Down
39 changes: 39 additions & 0 deletions hrms/hr/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from frappe.query_builder import DocType
from frappe.utils import (
add_days,
add_months,
comma_and,
cstr,
flt,
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 36585a2

Please sign in to comment.