Skip to content

Commit

Permalink
refactor: use single monthly_earned_leave
Browse files Browse the repository at this point in the history
  • Loading branch information
krantheman committed Dec 12, 2023
1 parent 36585a2 commit 9f4eb11
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 22 deletions.
9 changes: 5 additions & 4 deletions hrms/hr/doctype/leave_allocation/leave_allocation.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ frappe.ui.form.on("Leave Allocation", {
rounding: frm.rounding,
},
callback: function (r) {
frm.monthly_leaves = r.message;
frm.monthly_earned_leave = r.message;
frm.new_leaves = r.message;
frm.trigger("make_dashboard");
},
Expand Down Expand Up @@ -230,18 +230,19 @@ frappe.ui.form.on("Leave Allocation", {
$("div").remove(".form-dashboard-section.custom");

frappe.call({
method: "hrms.hr.utils.get_monthly_allocations",
method: "hrms.hr.utils.get_monthly_allocation_dates",
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,
monthly_earned_leave: frm.monthly_earned_leave,
},
callback: function (r) {
frm.dashboard.add_section(
frappe.render_template("leave_allocation_dashboard", {
allocations: r.message
allocation_dates: r.message,
monthly_earned_leave: frm.monthly_earned_leave,
}),
__("Leaves Allocated")
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{% if allocations.length %}
{% if allocation_dates.length %}
<table class="table table-bordered small">
<thead>
<tr>
Expand All @@ -7,10 +7,10 @@
</tr>
</thead>
<tbody>
{% for(const i of allocations) { %} {% %}
{% for(const date of allocation_dates) { %} {% %}
<tr>
<td>{%= i.date %}</td>
<td>{%= i.leaves %}</td>
<td>{%= date %}</td>
<td>{%= monthly_earned_leave %}</td>
</tr>
{% } %}
</tbody>
Expand Down
25 changes: 11 additions & 14 deletions hrms/hr/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -523,33 +523,30 @@ def allocate_leaves_manually(allocation_name, new_leaves):


@frappe.whitelist()
def get_monthly_allocations(employee, leave_type, from_date, to_date, leave_policy):
def get_monthly_allocation_dates(
leave_type, from_date, to_date, leave_policy, monthly_earned_leave
):
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
)
allocate_on_day = frappe.db.get_value("Leave Type", leave_type, "allocate_on_day")
date = get_month_allocation_date(from_date, allocate_on_day)

allocations = []
date = get_monthly_allocation_date(from_date, allocate_on_day)
total_leaves = 0
monthly_earned_leave = float(monthly_earned_leave)
total_leaves = monthly_earned_leave
if date < from_date:
date = add_months(date, 1)
date = get_monthly_allocation_date(date, allocate_on_day)
date = get_month_allocation_date(date, allocate_on_day)
while date <= to_date and total_leaves <= annual_allocation:
allocations.append({"date": date, "leaves": monthly_earned_leave})
allocations.append(date)
date = add_months(date, 1)
date = get_monthly_allocation_date(date, allocate_on_day)
date = get_month_allocation_date(date, allocate_on_day)
total_leaves += monthly_earned_leave
return allocations


def get_monthly_allocation_date(date, allocate_on_day):
def get_month_allocation_date(date, allocate_on_day):
allocation_date = {
"First Day": get_first_day(date),
"Last Day": get_last_day(date),
Expand Down

0 comments on commit 9f4eb11

Please sign in to comment.