2021-06-24 07:30:50 +00:00
|
|
|
# Copyright (c) 2021, Frappe Technologies Pvt. Ltd. and Contributors
|
|
|
|
# License: GNU General Public License v3. See license.txt
|
|
|
|
|
|
|
|
import frappe
|
|
|
|
from frappe import _
|
|
|
|
from frappe.desk.form import assign_to
|
|
|
|
from frappe.model.document import Document
|
2021-09-02 11:14:59 +00:00
|
|
|
from frappe.utils import add_days, flt, unique
|
|
|
|
|
2021-08-27 05:42:24 +00:00
|
|
|
from erpnext.hr.doctype.employee.employee import get_holiday_list_for_employee
|
2021-09-02 11:14:59 +00:00
|
|
|
from erpnext.hr.doctype.holiday_list.holiday_list import is_holiday
|
|
|
|
|
2021-06-24 07:30:50 +00:00
|
|
|
|
|
|
|
class EmployeeBoardingController(Document):
|
2022-03-28 13:22:46 +00:00
|
|
|
"""
|
|
|
|
Create the project and the task for the boarding process
|
|
|
|
Assign to the concerned person and roles as per the onboarding/separation template
|
|
|
|
"""
|
|
|
|
|
2021-06-24 07:30:50 +00:00
|
|
|
def validate(self):
|
|
|
|
# remove the task if linked before submitting the form
|
|
|
|
if self.amended_from:
|
|
|
|
for activity in self.activities:
|
2022-03-28 13:22:46 +00:00
|
|
|
activity.task = ""
|
2021-06-24 07:30:50 +00:00
|
|
|
|
|
|
|
def on_submit(self):
|
|
|
|
# create the project for the given employee onboarding
|
2022-03-28 13:22:46 +00:00
|
|
|
project_name = _(self.doctype) + " : "
|
|
|
|
if self.doctype == "Employee Onboarding":
|
2021-06-24 07:30:50 +00:00
|
|
|
project_name += self.job_applicant
|
|
|
|
else:
|
|
|
|
project_name += self.employee
|
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
project = frappe.get_doc(
|
|
|
|
{
|
|
|
|
"doctype": "Project",
|
|
|
|
"project_name": project_name,
|
|
|
|
"expected_start_date": self.date_of_joining
|
|
|
|
if self.doctype == "Employee Onboarding"
|
|
|
|
else self.resignation_letter_date,
|
|
|
|
"department": self.department,
|
|
|
|
"company": self.company,
|
|
|
|
}
|
|
|
|
).insert(ignore_permissions=True, ignore_mandatory=True)
|
2021-06-24 07:30:50 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
self.db_set("project", project.name)
|
|
|
|
self.db_set("boarding_status", "Pending")
|
2021-06-24 07:30:50 +00:00
|
|
|
self.reload()
|
|
|
|
self.create_task_and_notify_user()
|
|
|
|
|
|
|
|
def create_task_and_notify_user(self):
|
|
|
|
# create the task for the given project and assign to the concerned person
|
2021-08-27 05:42:24 +00:00
|
|
|
holiday_list = self.get_holiday_list()
|
|
|
|
|
2021-06-24 07:30:50 +00:00
|
|
|
for activity in self.activities:
|
|
|
|
if activity.task:
|
|
|
|
continue
|
|
|
|
|
2021-08-27 05:42:24 +00:00
|
|
|
dates = self.get_task_dates(activity, holiday_list)
|
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
task = frappe.get_doc(
|
|
|
|
{
|
|
|
|
"doctype": "Task",
|
|
|
|
"project": self.project,
|
|
|
|
"subject": activity.activity_name + " : " + self.employee_name,
|
|
|
|
"description": activity.description,
|
|
|
|
"department": self.department,
|
|
|
|
"company": self.company,
|
|
|
|
"task_weight": activity.task_weight,
|
|
|
|
"exp_start_date": dates[0],
|
|
|
|
"exp_end_date": dates[1],
|
|
|
|
}
|
|
|
|
).insert(ignore_permissions=True)
|
|
|
|
activity.db_set("task", task.name)
|
2021-06-24 07:30:50 +00:00
|
|
|
|
|
|
|
users = [activity.user] if activity.user else []
|
|
|
|
if activity.role:
|
2022-03-28 13:22:46 +00:00
|
|
|
user_list = frappe.db.sql_list(
|
|
|
|
"""
|
2021-06-24 07:30:50 +00:00
|
|
|
SELECT
|
|
|
|
DISTINCT(has_role.parent)
|
|
|
|
FROM
|
|
|
|
`tabHas Role` has_role
|
|
|
|
LEFT JOIN `tabUser` user
|
|
|
|
ON has_role.parent = user.name
|
|
|
|
WHERE
|
|
|
|
has_role.parenttype = 'User'
|
|
|
|
AND user.enabled = 1
|
|
|
|
AND has_role.role = %s
|
2022-03-28 13:22:46 +00:00
|
|
|
""",
|
|
|
|
activity.role,
|
|
|
|
)
|
2021-06-24 07:30:50 +00:00
|
|
|
users = unique(users + user_list)
|
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
if "Administrator" in users:
|
|
|
|
users.remove("Administrator")
|
2021-06-24 07:30:50 +00:00
|
|
|
|
|
|
|
# assign the task the users
|
|
|
|
if users:
|
|
|
|
self.assign_task_to_users(task, users)
|
|
|
|
|
2021-08-27 05:42:24 +00:00
|
|
|
def get_holiday_list(self):
|
2022-03-28 13:22:46 +00:00
|
|
|
if self.doctype == "Employee Separation":
|
2021-08-27 05:42:24 +00:00
|
|
|
return get_holiday_list_for_employee(self.employee)
|
|
|
|
else:
|
|
|
|
if self.employee:
|
|
|
|
return get_holiday_list_for_employee(self.employee)
|
|
|
|
else:
|
|
|
|
if not self.holiday_list:
|
2022-03-28 13:22:46 +00:00
|
|
|
frappe.throw(_("Please set the Holiday List."), frappe.MandatoryError)
|
2021-08-27 05:42:24 +00:00
|
|
|
else:
|
|
|
|
return self.holiday_list
|
|
|
|
|
|
|
|
def get_task_dates(self, activity, holiday_list):
|
|
|
|
start_date = end_date = None
|
|
|
|
|
2022-02-21 17:24:46 +00:00
|
|
|
if activity.begin_on is not None:
|
2021-08-27 05:42:24 +00:00
|
|
|
start_date = add_days(self.boarding_begins_on, activity.begin_on)
|
|
|
|
start_date = self.update_if_holiday(start_date, holiday_list)
|
|
|
|
|
2022-02-21 17:24:46 +00:00
|
|
|
if activity.duration is not None:
|
2021-08-27 05:42:24 +00:00
|
|
|
end_date = add_days(self.boarding_begins_on, activity.begin_on + activity.duration)
|
|
|
|
end_date = self.update_if_holiday(end_date, holiday_list)
|
|
|
|
|
|
|
|
return [start_date, end_date]
|
|
|
|
|
|
|
|
def update_if_holiday(self, date, holiday_list):
|
|
|
|
while is_holiday(holiday_list, date):
|
|
|
|
date = add_days(date, 1)
|
|
|
|
return date
|
|
|
|
|
2021-06-24 07:30:50 +00:00
|
|
|
def assign_task_to_users(self, task, users):
|
|
|
|
for user in users:
|
|
|
|
args = {
|
2022-03-28 13:22:46 +00:00
|
|
|
"assign_to": [user],
|
|
|
|
"doctype": task.doctype,
|
|
|
|
"name": task.name,
|
|
|
|
"description": task.description or task.subject,
|
|
|
|
"notify": self.notify_users_by_email,
|
2021-06-24 07:30:50 +00:00
|
|
|
}
|
|
|
|
assign_to.add(args)
|
|
|
|
|
|
|
|
def on_cancel(self):
|
|
|
|
# delete task project
|
2022-01-29 09:57:36 +00:00
|
|
|
project = self.project
|
2022-03-28 13:22:46 +00:00
|
|
|
for task in frappe.get_all("Task", filters={"project": project}):
|
|
|
|
frappe.delete_doc("Task", task.name, force=1)
|
|
|
|
frappe.delete_doc("Project", project, force=1)
|
|
|
|
self.db_set("project", "")
|
2021-06-24 07:30:50 +00:00
|
|
|
for activity in self.activities:
|
2022-03-28 13:22:46 +00:00
|
|
|
activity.db_set("task", "")
|
2021-06-24 07:30:50 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
frappe.msgprint(
|
|
|
|
_("Linked Project {} and Tasks deleted.").format(project), alert=True, indicator="blue"
|
|
|
|
)
|
2022-01-29 09:57:36 +00:00
|
|
|
|
2021-06-24 07:30:50 +00:00
|
|
|
|
|
|
|
@frappe.whitelist()
|
|
|
|
def get_onboarding_details(parent, parenttype):
|
2022-03-28 13:22:46 +00:00
|
|
|
return frappe.get_all(
|
|
|
|
"Employee Boarding Activity",
|
|
|
|
fields=[
|
|
|
|
"activity_name",
|
|
|
|
"role",
|
|
|
|
"user",
|
|
|
|
"required_for_employee_creation",
|
|
|
|
"description",
|
|
|
|
"task_weight",
|
|
|
|
"begin_on",
|
|
|
|
"duration",
|
|
|
|
],
|
|
|
|
filters={"parent": parent, "parenttype": parenttype},
|
|
|
|
order_by="idx",
|
|
|
|
)
|
2021-06-24 07:30:50 +00:00
|
|
|
|
|
|
|
|
|
|
|
def update_employee_boarding_status(project):
|
2022-03-28 13:22:46 +00:00
|
|
|
employee_onboarding = frappe.db.exists("Employee Onboarding", {"project": project.name})
|
|
|
|
employee_separation = frappe.db.exists("Employee Separation", {"project": project.name})
|
2021-06-24 07:30:50 +00:00
|
|
|
|
|
|
|
if not (employee_onboarding or employee_separation):
|
|
|
|
return
|
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
status = "Pending"
|
2021-06-24 07:30:50 +00:00
|
|
|
if flt(project.percent_complete) > 0.0 and flt(project.percent_complete) < 100.0:
|
2022-03-28 13:22:46 +00:00
|
|
|
status = "In Process"
|
2021-06-24 07:30:50 +00:00
|
|
|
elif flt(project.percent_complete) == 100.0:
|
2022-03-28 13:22:46 +00:00
|
|
|
status = "Completed"
|
2021-06-24 07:30:50 +00:00
|
|
|
|
|
|
|
if employee_onboarding:
|
2022-03-28 13:22:46 +00:00
|
|
|
frappe.db.set_value("Employee Onboarding", employee_onboarding, "boarding_status", status)
|
2021-06-24 07:30:50 +00:00
|
|
|
elif employee_separation:
|
2022-03-28 13:22:46 +00:00
|
|
|
frappe.db.set_value("Employee Separation", employee_separation, "boarding_status", status)
|