diff --git a/erpnext/hr/doctype/leave_application/leave_application.py b/erpnext/hr/doctype/leave_application/leave_application.py index 3f7fd4cc78..a8ced43c85 100755 --- a/erpnext/hr/doctype/leave_application/leave_application.py +++ b/erpnext/hr/doctype/leave_application/leave_application.py @@ -538,4 +538,39 @@ def get_mandatory_approval(doctype): 'expense_approver_mandatory_in_expense_claim') return mandatory + +def get_approved_leaves_for_period(employee, leave_type, from_date, to_date): + query = """ + select employee, leave_type, from_date, to_date, total_leave_days + from `tabLeave Application` + where employee=%(employee)s + and docstatus=1 + and (from_date between %(from_date)s and %(to_date)s + or to_date between %(from_date)s and %(to_date)s + or (from_date < %(from_date)s and to_date > %(to_date)s)) + """ + if leave_type: + query += "and leave_type=%(leave_type)s" + + leave_applications = frappe.db.sql(query,{ + "from_date": from_date, + "to_date": to_date, + "employee": employee, + "leave_type": leave_type + }, as_dict=1) + + leave_days = 0 + for leave_app in leave_applications: + if leave_app.from_date >= getdate(from_date) and leave_app.to_date <= getdate(to_date): + leave_days += leave_app.total_leave_days + else: + if leave_app.from_date < getdate(from_date): + leave_app.from_date = from_date + if leave_app.to_date > getdate(to_date): + leave_app.to_date = to_date + + leave_days += get_number_of_leave_days(employee, leave_type, + leave_app.from_date, leave_app.to_date) + + return leave_days \ No newline at end of file