From b848c515923386fc119313b0edadc4dd9fb54b44 Mon Sep 17 00:00:00 2001 From: Jamsheer Date: Mon, 14 May 2018 17:56:22 +0530 Subject: [PATCH] Compensatory Leave Request - leave allocation create and update - fix --- .../compensatory_leave_request.py | 40 ++++++++++++++++--- 1 file changed, 34 insertions(+), 6 deletions(-) diff --git a/erpnext/hr/doctype/compensatory_leave_request/compensatory_leave_request.py b/erpnext/hr/doctype/compensatory_leave_request/compensatory_leave_request.py index ec522281c3e..414066838a4 100644 --- a/erpnext/hr/doctype/compensatory_leave_request/compensatory_leave_request.py +++ b/erpnext/hr/doctype/compensatory_leave_request/compensatory_leave_request.py @@ -5,7 +5,7 @@ from __future__ import unicode_literals import frappe from frappe import _ -from frappe.utils import date_diff +from frappe.utils import date_diff, add_days from frappe.model.document import Document from erpnext.hr.utils import validate_dates, validate_overlap, get_leave_period @@ -20,21 +20,49 @@ class CompensatoryLeaveRequest(Document): frappe.throw(_("Please select a leave type to submit the request")) else: company = frappe.db.get_value("Employee", self.employee, "company") + date_difference = date_diff(self.work_end_date, self.work_from_date) + 1 leave_period = get_leave_period(self.work_from_date, self.work_end_date, company) if leave_period: - self.create_leave_allocation(leave_period) + leave_allocation_name = self.exists_allocation_for_period(leave_period) + if leave_allocation_name: + leave_allocation = frappe.get_doc("Leave Allocation", leave_allocation_name) + new_leaves_allocated = leave_allocation.new_leaves_allocated + leave_allocation.new_leaves_allocated += date_difference + leave_allocation.submit() + else: + self.create_leave_allocation(leave_period, date_difference) else: frappe.throw(_("There is no leave period in between {0} and {1}").format(self.work_from_date, self.work_end_date)) - def create_leave_allocation(self, leave_period): - date_difference = date_diff(self.work_end_date, self.work_from_date) + 1 + def exists_allocation_for_period(self, leave_period): + leave_allocation = frappe.db.sql(""" + select name + from `tabLeave Allocation` + where employee=%(employee)s and leave_type=%(leave_type)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)) + """, { + "from_date": leave_period[0].from_date, + "to_date": leave_period[0].to_date, + "employee": self.employee, + "leave_type": self.leave_type + }, as_dict=1) + + if leave_allocation: + return leave_allocation[0].name + else: + False + + def create_leave_allocation(self, leave_period, date_difference): is_carry_forward = frappe.db.get_value("Leave Type", self.leave_type, "is_carry_forward") allocation = frappe.new_doc("Leave Allocation") allocation.employee = self.employee allocation.employee_name = self.employee_name allocation.leave_type = self.leave_type - allocation.from_date = self.work_from_date - allocation.to_date = self.work_end_date + allocation.from_date = add_days(self.work_end_date, 1) + allocation.to_date = leave_period[0].to_date allocation.new_leaves_allocated = date_difference allocation.total_leaves_allocated = date_difference allocation.compensatory_request = self.name