fix: leave allocation records query (#30118)

This commit is contained in:
Rucha Mahabal 2022-03-08 23:16:40 +05:30 committed by GitHub
parent ce27cdb121
commit 096092f173
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -4,6 +4,7 @@
import frappe import frappe
from frappe import _ from frappe import _
from frappe.query_builder.functions import Max, Min, Sum
from frappe.utils import ( from frappe.utils import (
add_days, add_days,
cint, cint,
@ -567,28 +568,39 @@ def get_leave_balance_on(employee, leave_type, date, to_date=None, consider_all_
return get_remaining_leaves(allocation, leaves_taken, date, expiry) return get_remaining_leaves(allocation, leaves_taken, date, expiry)
def get_leave_allocation_records(employee, date, leave_type=None): def get_leave_allocation_records(employee, date, leave_type=None):
''' returns the total allocated leaves and carry forwarded leaves based on ledger entries ''' """Returns the total allocated leaves and carry forwarded leaves based on ledger entries"""
Ledger = frappe.qb.DocType("Leave Ledger Entry")
conditions = ("and leave_type='%s'" % leave_type) if leave_type else "" cf_leave_case = frappe.qb.terms.Case().when(Ledger.is_carry_forward == "1", Ledger.leaves).else_(0)
allocation_details = frappe.db.sql(""" sum_cf_leaves = Sum(cf_leave_case).as_("cf_leaves")
SELECT
SUM(CASE WHEN is_carry_forward = 1 THEN leaves ELSE 0 END) as cf_leaves, new_leaves_case = frappe.qb.terms.Case().when(Ledger.is_carry_forward == "0", Ledger.leaves).else_(0)
SUM(CASE WHEN is_carry_forward = 0 THEN leaves ELSE 0 END) as new_leaves, sum_new_leaves = Sum(new_leaves_case).as_("new_leaves")
MIN(from_date) as from_date,
MAX(to_date) as to_date, query = (
leave_type frappe.qb.from_(Ledger)
FROM `tabLeave Ledger Entry` .select(
WHERE sum_cf_leaves,
from_date <= %(date)s sum_new_leaves,
AND to_date >= %(date)s Min(Ledger.from_date).as_("from_date"),
AND docstatus=1 Max(Ledger.to_date).as_("to_date"),
AND transaction_type="Leave Allocation" Ledger.leave_type
AND employee=%(employee)s ).where(
AND is_expired=0 (Ledger.from_date <= date)
AND is_lwp=0 & (Ledger.to_date >= date)
{0} & (Ledger.docstatus == 1)
GROUP BY employee, leave_type & (Ledger.transaction_type == "Leave Allocation")
""".format(conditions), dict(date=date, employee=employee), as_dict=1) #nosec & (Ledger.employee == employee)
& (Ledger.is_expired == 0)
& (Ledger.is_lwp == 0)
)
)
if leave_type:
query = query.where((Ledger.leave_type == leave_type))
query = query.groupby(Ledger.employee, Ledger.leave_type)
allocation_details = query.run(as_dict=True)
allocated_leaves = frappe._dict() allocated_leaves = frappe._dict()
for d in allocation_details: for d in allocation_details: