Rucha Mahabal | 357657f | 2021-06-24 13:00:50 +0530 | [diff] [blame] | 1 | # Copyright (c) 2021, Frappe Technologies Pvt. Ltd. and Contributors |
| 2 | # License: GNU General Public License v3. See license.txt |
| 3 | |
Rucha Mahabal | 357657f | 2021-06-24 13:00:50 +0530 | [diff] [blame] | 4 | import frappe |
| 5 | from frappe import _ |
| 6 | from frappe.desk.form import assign_to |
| 7 | from frappe.model.document import Document |
| 8 | from frappe.utils import flt, unique |
| 9 | |
| 10 | class EmployeeBoardingController(Document): |
| 11 | ''' |
| 12 | Create the project and the task for the boarding process |
| 13 | Assign to the concerned person and roles as per the onboarding/separation template |
| 14 | ''' |
| 15 | def validate(self): |
| 16 | # remove the task if linked before submitting the form |
| 17 | if self.amended_from: |
| 18 | for activity in self.activities: |
| 19 | activity.task = '' |
| 20 | |
| 21 | def on_submit(self): |
| 22 | # create the project for the given employee onboarding |
| 23 | project_name = _(self.doctype) + ' : ' |
| 24 | if self.doctype == 'Employee Onboarding': |
| 25 | project_name += self.job_applicant |
| 26 | else: |
| 27 | project_name += self.employee |
| 28 | |
| 29 | project = frappe.get_doc({ |
| 30 | 'doctype': 'Project', |
| 31 | 'project_name': project_name, |
| 32 | 'expected_start_date': self.date_of_joining if self.doctype == 'Employee Onboarding' else self.resignation_letter_date, |
| 33 | 'department': self.department, |
| 34 | 'company': self.company |
| 35 | }).insert(ignore_permissions=True, ignore_mandatory=True) |
| 36 | |
| 37 | self.db_set('project', project.name) |
| 38 | self.db_set('boarding_status', 'Pending') |
| 39 | self.reload() |
| 40 | self.create_task_and_notify_user() |
| 41 | |
| 42 | def create_task_and_notify_user(self): |
| 43 | # create the task for the given project and assign to the concerned person |
| 44 | for activity in self.activities: |
| 45 | if activity.task: |
| 46 | continue |
| 47 | |
| 48 | task = frappe.get_doc({ |
| 49 | 'doctype': 'Task', |
| 50 | 'project': self.project, |
| 51 | 'subject': activity.activity_name + ' : ' + self.employee_name, |
| 52 | 'description': activity.description, |
| 53 | 'department': self.department, |
| 54 | 'company': self.company, |
| 55 | 'task_weight': activity.task_weight |
| 56 | }).insert(ignore_permissions=True) |
| 57 | activity.db_set('task', task.name) |
| 58 | |
| 59 | users = [activity.user] if activity.user else [] |
| 60 | if activity.role: |
| 61 | user_list = frappe.db.sql_list(''' |
| 62 | SELECT |
| 63 | DISTINCT(has_role.parent) |
| 64 | FROM |
| 65 | `tabHas Role` has_role |
| 66 | LEFT JOIN `tabUser` user |
| 67 | ON has_role.parent = user.name |
| 68 | WHERE |
| 69 | has_role.parenttype = 'User' |
| 70 | AND user.enabled = 1 |
| 71 | AND has_role.role = %s |
| 72 | ''', activity.role) |
| 73 | users = unique(users + user_list) |
| 74 | |
| 75 | if 'Administrator' in users: |
| 76 | users.remove('Administrator') |
| 77 | |
| 78 | # assign the task the users |
| 79 | if users: |
| 80 | self.assign_task_to_users(task, users) |
| 81 | |
| 82 | def assign_task_to_users(self, task, users): |
| 83 | for user in users: |
| 84 | args = { |
| 85 | 'assign_to': [user], |
| 86 | 'doctype': task.doctype, |
| 87 | 'name': task.name, |
| 88 | 'description': task.description or task.subject, |
| 89 | 'notify': self.notify_users_by_email |
| 90 | } |
| 91 | assign_to.add(args) |
| 92 | |
| 93 | def on_cancel(self): |
| 94 | # delete task project |
| 95 | for task in frappe.get_all('Task', filters={'project': self.project}): |
| 96 | frappe.delete_doc('Task', task.name, force=1) |
| 97 | frappe.delete_doc('Project', self.project, force=1) |
| 98 | self.db_set('project', '') |
| 99 | for activity in self.activities: |
| 100 | activity.db_set('task', '') |
| 101 | |
| 102 | |
| 103 | @frappe.whitelist() |
| 104 | def get_onboarding_details(parent, parenttype): |
| 105 | return frappe.get_all('Employee Boarding Activity', |
| 106 | fields=['activity_name', 'role', 'user', 'required_for_employee_creation', 'description', 'task_weight'], |
| 107 | filters={'parent': parent, 'parenttype': parenttype}, |
| 108 | order_by= 'idx') |
| 109 | |
| 110 | |
| 111 | def update_employee_boarding_status(project): |
| 112 | employee_onboarding = frappe.db.exists('Employee Onboarding', {'project': project.name}) |
| 113 | employee_separation = frappe.db.exists('Employee Separation', {'project': project.name}) |
| 114 | |
| 115 | if not (employee_onboarding or employee_separation): |
| 116 | return |
| 117 | |
| 118 | status = 'Pending' |
| 119 | if flt(project.percent_complete) > 0.0 and flt(project.percent_complete) < 100.0: |
| 120 | status = 'In Process' |
| 121 | elif flt(project.percent_complete) == 100.0: |
| 122 | status = 'Completed' |
| 123 | |
| 124 | if employee_onboarding: |
| 125 | frappe.db.set_value('Employee Onboarding', employee_onboarding, 'boarding_status', status) |
| 126 | elif employee_separation: |
| 127 | frappe.db.set_value('Employee Separation', employee_separation, 'boarding_status', status) |