From 99c9cfaaedd069c4053a190a37a681ca2f79eea9 Mon Sep 17 00:00:00 2001 From: Mangesh-Khairnar Date: Tue, 23 Apr 2019 18:28:28 +0530 Subject: [PATCH] feat: generate leave allocation for carry forwarded leaves --- .../hr/doctype/leave_period/leave_period.py | 38 ++++++++++++++----- 1 file changed, 28 insertions(+), 10 deletions(-) diff --git a/erpnext/hr/doctype/leave_period/leave_period.py b/erpnext/hr/doctype/leave_period/leave_period.py index 15fa8d6f8c..1e2884d202 100644 --- a/erpnext/hr/doctype/leave_period/leave_period.py +++ b/erpnext/hr/doctype/leave_period/leave_period.py @@ -5,9 +5,10 @@ from __future__ import unicode_literals import frappe from frappe import _ -from frappe.utils import getdate, cstr +from frappe.utils import getdate, cstr, add_days from frappe.model.document import Document from erpnext.hr.utils import validate_overlap, get_employee_leave_policy +from erpnext.hr.doctype.leave_allocation.leave_allocation import get_carry_forwarded_leaves from frappe.utils.background_jobs import enqueue from six import iteritems @@ -39,8 +40,8 @@ class LeavePeriod(Document): employee=None, carry_forward_leaves=0): employees = self.get_employees({ "grade": grade, - "department": department, - "designation": designation, + "department": department, + "designation": designation, "name": employee }) @@ -57,7 +58,7 @@ def grant_leave_alloc_for_employees(employees, leave_period, carry_forward_leave leave_allocations = [] existing_allocations_for = get_existing_allocations(employees, leave_period.name) leave_type_details = get_leave_type_details() - count=0 + count = 0 for employee in employees: if employee in existing_allocations_for: continue @@ -77,8 +78,14 @@ def grant_leave_alloc_for_employees(employees, leave_period, carry_forward_leave def get_existing_allocations(employees, leave_period): leave_allocations = frappe.db.sql_list(""" - select distinct employee from `tabLeave Allocation` - where leave_period=%s and employee in (%s) and docstatus=1 + SELECT DISTINCT + employee + FROM `tabLeave Allocation` + WHERE + leave_period=%s + AND employee in (%s) + AND carry_forward=0 + AND docstatus=1 """ % ('%s', ', '.join(['%s']*len(employees))), [leave_period] + employees) if leave_allocations: frappe.msgprint(_("Skipping Leave Allocation for the following employees, as Leave Allocation records already exists against them. {0}") @@ -87,7 +94,8 @@ def get_existing_allocations(employees, leave_period): def get_leave_type_details(): leave_type_details = frappe._dict() - leave_types = frappe.get_all("Leave Type", fields=["name", "is_lwp", "is_earned_leave", "is_compensatory", "is_carry_forward"]) + leave_types = frappe.get_all("Leave Type", + fields=["name", "is_lwp", "is_earned_leave", "is_compensatory", "is_carry_forward", "carry_forward_leave_expiry"]) for d in leave_types: leave_type_details.setdefault(d.name, d) return leave_type_details @@ -106,9 +114,19 @@ def create_leave_allocation(employee, leave_type, new_leaves_allocated, leave_ty allocation.leave_period = leave_period.name if carry_forward_leaves: if leave_type_details.get(leave_type).is_carry_forward: - allocation.carry_forward = carry_forward_leaves - allocation.save(ignore_permissions = True) + create_carry_forward_leaves_allocation(employee, leave_type, leave_type_details, leave_period, carry_forward_leaves) + allocation.save(ignore_permissions=True) allocation.submit() return allocation.name - +def create_carry_forward_leaves_allocation(employee, leave_type, leave_type_details, leave_period, carry_forward_leaves): + allocation = frappe.new_doc("Leave Allocation") + allocation.employee = employee + allocation.leave_type = leave_type + allocation.from_date = leave_period.from_date + allocation.to_date = add_days(leave_period.from_date, leave_type_details.carry_forward_leave_expiry) if not leave_type_details.carry_forward_leave_expiry else leave_period.to_date + allocation.leave_period = leave_period.name + allocation.carry_forward = carry_forward_leaves + allocation.carry_forwarded_leaves = get_carry_forwarded_leaves(employee, leave_type, leave_period.from_date, leave_type_details.carry_forward_leave_expiry) + allocation.save(ignore_permissions=True) + allocation.submit() \ No newline at end of file