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-08-27 05:42:24 +00:00
|
|
|
from frappe.utils import add_days, flt, unique
|
2021-09-02 11:14:59 +00:00
|
|
|
|
2021-08-27 05:42:24 +00:00
|
|
|
from erpnext.hr.doctype.employee.employee import get_holiday_list_for_employee
|
|
|
|
from erpnext.hr.doctype.holiday_list.holiday_list import is_holiday
|
2021-09-02 11:14:59 +00:00
|
|
|
|
2021-06-24 07:30:50 +00:00
|
|
|
|
|
|
|
class EmployeeBoardingController(Document):
|
2022-03-28 13:22:46 +00:00
|
|
|
"""
|
2021-06-24 07:30:50 +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
|
2022-03-28 13:22:46 +00:00
|
|
|
"""
|
|
|
|
|
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:
|
|
|
|
activity.task = ""
|
|
|
|
|
|
|
|
def on_submit(self):
|
|
|
|
# create the project for the given employee onboarding
|
|
|
|
project_name = _(self.doctype) + " : "
|
|
|
|
if self.doctype == "Employee Onboarding":
|
|
|
|
project_name += self.job_applicant
|
|
|
|
else:
|
|
|
|
project_name += self.employee
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
self.db_set("project", project.name)
|
|
|
|
self.db_set("boarding_status", "Pending")
|
|
|
|
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)
|
|
|
|
|
2021-06-24 07:30:50 +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,
|
2021-08-27 05:42:24 +00:00
|
|
|
"task_weight": activity.task_weight,
|
|
|
|
"exp_start_date": dates[0],
|
|
|
|
"exp_end_date": dates[1],
|
2021-06-24 07:30:50 +00:00
|
|
|
}
|
|
|
|
).insert(ignore_permissions=True)
|
|
|
|
activity.db_set("task", task.name)
|
|
|
|
|
|
|
|
users = [activity.user] if activity.user else []
|
|
|
|
if activity.role:
|
|
|
|
user_list = frappe.db.sql_list(
|
2022-03-28 13:22:46 +00:00
|
|
|
"""
|
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
|
|
|
""",
|
2021-06-24 07:30:50 +00:00
|
|
|
activity.role,
|
|
|
|
)
|
|
|
|
users = unique(users + user_list)
|
|
|
|
|
|
|
|
if "Administrator" in users:
|
|
|
|
users.remove("Administrator")
|
|
|
|
|
|
|
|
# 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):
|
|
|
|
if self.doctype == "Employee Separation":
|
|
|
|
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:
|
|
|
|
frappe.throw(_("Please set the Holiday List."), frappe.MandatoryError)
|
|
|
|
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 = {
|
|
|
|
"assign_to": [user],
|
|
|
|
"doctype": task.doctype,
|
|
|
|
"name": task.name,
|
|
|
|
"description": task.description or task.subject,
|
|
|
|
"notify": self.notify_users_by_email,
|
|
|
|
}
|
|
|
|
assign_to.add(args)
|
|
|
|
|
|
|
|
def on_cancel(self):
|
|
|
|
# delete task project
|
2022-01-29 09:57:36 +00:00
|
|
|
project = self.project
|
|
|
|
for task in frappe.get_all("Task", filters={"project": project}):
|
2021-06-24 07:30:50 +00:00
|
|
|
frappe.delete_doc("Task", task.name, force=1)
|
2022-01-29 09:57:36 +00:00
|
|
|
frappe.delete_doc("Project", project, force=1)
|
2021-06-24 07:30:50 +00:00
|
|
|
self.db_set("project", "")
|
|
|
|
for activity in self.activities:
|
|
|
|
activity.db_set("task", "")
|
|
|
|
|
2022-01-29 09:57:36 +00:00
|
|
|
frappe.msgprint(
|
|
|
|
_("Linked Project {} and Tasks deleted.").format(project), alert=True, indicator="blue"
|
|
|
|
)
|
|
|
|
|
2021-06-24 07:30:50 +00:00
|
|
|
|
|
|
|
@frappe.whitelist()
|
|
|
|
def get_onboarding_details(parent, parenttype):
|
|
|
|
return frappe.get_all(
|
|
|
|
"Employee Boarding Activity",
|
2021-08-27 05:42:24 +00:00
|
|
|
fields=[
|
|
|
|
"activity_name",
|
|
|
|
"role",
|
|
|
|
"user",
|
|
|
|
"required_for_employee_creation",
|
|
|
|
"description",
|
|
|
|
"task_weight",
|
|
|
|
"begin_on",
|
|
|
|
"duration",
|
|
|
|
],
|
2021-06-24 07:30:50 +00:00
|
|
|
filters={"parent": parent, "parenttype": parenttype},
|
|
|
|
order_by="idx",
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def update_employee_boarding_status(project):
|
|
|
|
employee_onboarding = frappe.db.exists("Employee Onboarding", {"project": project.name})
|
|
|
|
employee_separation = frappe.db.exists("Employee Separation", {"project": project.name})
|
|
|
|
|
|
|
|
if not (employee_onboarding or employee_separation):
|
|
|
|
return
|
|
|
|
|
|
|
|
status = "Pending"
|
|
|
|
if flt(project.percent_complete) > 0.0 and flt(project.percent_complete) < 100.0:
|
|
|
|
status = "In Process"
|
|
|
|
elif flt(project.percent_complete) == 100.0:
|
|
|
|
status = "Completed"
|
|
|
|
|
|
|
|
if employee_onboarding:
|
|
|
|
frappe.db.set_value("Employee Onboarding", employee_onboarding, "boarding_status", status)
|
|
|
|
elif employee_separation:
|
|
|
|
frappe.db.set_value("Employee Separation", employee_separation, "boarding_status", status)
|