Anand Doshi | 885e074 | 2015-03-03 14:55:30 +0530 | [diff] [blame] | 1 | # Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors |
Rushabh Mehta | e67d1fb | 2013-08-05 14:59:54 +0530 | [diff] [blame] | 2 | # License: GNU General Public License v3. See license.txt |
Anand Doshi | 60666a2 | 2013-04-12 20:19:53 +0530 | [diff] [blame] | 3 | |
Rohan | db2d196 | 2021-03-09 21:03:45 +0530 | [diff] [blame] | 4 | import erpnext |
| 5 | import frappe |
Jamsheer | cc25eb0 | 2018-06-13 15:14:24 +0530 | [diff] [blame] | 6 | from erpnext.hr.doctype.employee.employee import get_holiday_list_for_employee |
Rohan | db2d196 | 2021-03-09 21:03:45 +0530 | [diff] [blame] | 7 | from frappe import _ |
| 8 | from frappe.desk.form import assign_to |
| 9 | from frappe.model.document import Document |
| 10 | from frappe.utils import (add_days, cstr, flt, format_datetime, formatdate, |
| 11 | get_datetime, getdate, nowdate, today, unique) |
| 12 | |
Manas Solanki | b698846 | 2018-05-10 18:07:20 +0530 | [diff] [blame] | 13 | |
Nabin Hait | 58ee6c1 | 2020-04-26 17:45:57 +0530 | [diff] [blame] | 14 | class DuplicateDeclarationError(frappe.ValidationError): pass |
| 15 | |
Rohan | db2d196 | 2021-03-09 21:03:45 +0530 | [diff] [blame] | 16 | |
Manas Solanki | b698846 | 2018-05-10 18:07:20 +0530 | [diff] [blame] | 17 | class EmployeeBoardingController(Document): |
| 18 | ''' |
| 19 | Create the project and the task for the boarding process |
| 20 | Assign to the concerned person and roles as per the onboarding/separation template |
| 21 | ''' |
| 22 | def validate(self): |
| 23 | # remove the task if linked before submitting the form |
| 24 | if self.amended_from: |
| 25 | for activity in self.activities: |
| 26 | activity.task = '' |
| 27 | |
| 28 | def on_submit(self): |
| 29 | # create the project for the given employee onboarding |
Manas Solanki | 70899f5 | 2018-05-15 18:52:14 +0530 | [diff] [blame] | 30 | project_name = _(self.doctype) + " : " |
Manas Solanki | b698846 | 2018-05-10 18:07:20 +0530 | [diff] [blame] | 31 | if self.doctype == "Employee Onboarding": |
Manas Solanki | 70899f5 | 2018-05-15 18:52:14 +0530 | [diff] [blame] | 32 | project_name += self.job_applicant |
Manas Solanki | b698846 | 2018-05-10 18:07:20 +0530 | [diff] [blame] | 33 | else: |
Manas Solanki | 70899f5 | 2018-05-15 18:52:14 +0530 | [diff] [blame] | 34 | project_name += self.employee |
Manas Solanki | b698846 | 2018-05-10 18:07:20 +0530 | [diff] [blame] | 35 | project = frappe.get_doc({ |
| 36 | "doctype": "Project", |
| 37 | "project_name": project_name, |
| 38 | "expected_start_date": self.date_of_joining if self.doctype == "Employee Onboarding" else self.resignation_letter_date, |
| 39 | "department": self.department, |
| 40 | "company": self.company |
| 41 | }).insert(ignore_permissions=True) |
| 42 | self.db_set("project", project.name) |
Shreya | 5c6ade4 | 2018-06-20 15:48:27 +0530 | [diff] [blame] | 43 | self.db_set("boarding_status", "Pending") |
Mangesh-Khairnar | 06a0afa | 2019-08-05 10:07:05 +0530 | [diff] [blame] | 44 | self.reload() |
| 45 | self.create_task_and_notify_user() |
Manas Solanki | b698846 | 2018-05-10 18:07:20 +0530 | [diff] [blame] | 46 | |
Mangesh-Khairnar | 06a0afa | 2019-08-05 10:07:05 +0530 | [diff] [blame] | 47 | def create_task_and_notify_user(self): |
Manas Solanki | b698846 | 2018-05-10 18:07:20 +0530 | [diff] [blame] | 48 | # create the task for the given project and assign to the concerned person |
| 49 | for activity in self.activities: |
Mangesh-Khairnar | 06a0afa | 2019-08-05 10:07:05 +0530 | [diff] [blame] | 50 | if activity.task: |
| 51 | continue |
| 52 | |
Manas Solanki | b698846 | 2018-05-10 18:07:20 +0530 | [diff] [blame] | 53 | task = frappe.get_doc({ |
Rohan | db2d196 | 2021-03-09 21:03:45 +0530 | [diff] [blame] | 54 | "doctype": "Task", |
| 55 | "project": self.project, |
| 56 | "subject": activity.activity_name + " : " + self.employee_name, |
| 57 | "description": activity.description, |
| 58 | "department": self.department, |
| 59 | "company": self.company, |
| 60 | "task_weight": activity.task_weight |
| 61 | }).insert(ignore_permissions=True) |
Manas Solanki | b698846 | 2018-05-10 18:07:20 +0530 | [diff] [blame] | 62 | activity.db_set("task", task.name) |
Rohan | db2d196 | 2021-03-09 21:03:45 +0530 | [diff] [blame] | 63 | |
Manas Solanki | b698846 | 2018-05-10 18:07:20 +0530 | [diff] [blame] | 64 | users = [activity.user] if activity.user else [] |
| 65 | if activity.role: |
Rohan | db2d196 | 2021-03-09 21:03:45 +0530 | [diff] [blame] | 66 | user_list = frappe.db.sql_list(''' |
| 67 | SELECT |
| 68 | DISTINCT(has_role.parent) |
| 69 | FROM |
| 70 | `tabHas Role` has_role |
| 71 | LEFT JOIN `tabUser` user |
| 72 | ON has_role.parent = user.name |
| 73 | WHERE |
| 74 | has_role.parenttype = 'User' |
| 75 | AND user.enabled = 1 |
| 76 | AND has_role.role = %s |
| 77 | ''', activity.role) |
| 78 | users = unique(users + user_list) |
Manas Solanki | b698846 | 2018-05-10 18:07:20 +0530 | [diff] [blame] | 79 | |
Anurag Mishra | add6bf3 | 2019-01-04 11:36:30 +0530 | [diff] [blame] | 80 | if "Administrator" in users: |
| 81 | users.remove("Administrator") |
| 82 | |
Manas Solanki | b698846 | 2018-05-10 18:07:20 +0530 | [diff] [blame] | 83 | # assign the task the users |
| 84 | if users: |
Rohan | db2d196 | 2021-03-09 21:03:45 +0530 | [diff] [blame] | 85 | self.assign_task_to_users(task, users) |
Manas Solanki | b698846 | 2018-05-10 18:07:20 +0530 | [diff] [blame] | 86 | |
| 87 | def assign_task_to_users(self, task, users): |
| 88 | for user in users: |
| 89 | args = { |
Anurag Mishra | 225802e | 2020-06-04 14:11:18 +0530 | [diff] [blame] | 90 | 'assign_to': [user], |
| 91 | 'doctype': task.doctype, |
| 92 | 'name': task.name, |
| 93 | 'description': task.description or task.subject, |
| 94 | 'notify': self.notify_users_by_email |
Manas Solanki | b698846 | 2018-05-10 18:07:20 +0530 | [diff] [blame] | 95 | } |
| 96 | assign_to.add(args) |
| 97 | |
| 98 | def on_cancel(self): |
| 99 | # delete task project |
| 100 | for task in frappe.get_all("Task", filters={"project": self.project}): |
Zarrar | 9a3b785 | 2018-07-11 14:34:55 +0530 | [diff] [blame] | 101 | frappe.delete_doc("Task", task.name, force=1) |
| 102 | frappe.delete_doc("Project", self.project, force=1) |
Manas Solanki | b698846 | 2018-05-10 18:07:20 +0530 | [diff] [blame] | 103 | self.db_set('project', '') |
| 104 | for activity in self.activities: |
| 105 | activity.db_set("task", "") |
| 106 | |
| 107 | |
| 108 | @frappe.whitelist() |
| 109 | def get_onboarding_details(parent, parenttype): |
Nabin Hait | 01b2a65 | 2018-08-28 14:09:27 +0530 | [diff] [blame] | 110 | return frappe.get_all("Employee Boarding Activity", |
Himanshu | 4cb1a1e | 2019-06-05 10:26:01 +0530 | [diff] [blame] | 111 | fields=["activity_name", "role", "user", "required_for_employee_creation", "description", "task_weight"], |
Manas Solanki | b698846 | 2018-05-10 18:07:20 +0530 | [diff] [blame] | 112 | filters={"parent": parent, "parenttype": parenttype}, |
| 113 | order_by= "idx") |
Anand Doshi | 60666a2 | 2013-04-12 20:19:53 +0530 | [diff] [blame] | 114 | |
Shreya | 5c6ade4 | 2018-06-20 15:48:27 +0530 | [diff] [blame] | 115 | @frappe.whitelist() |
| 116 | def get_boarding_status(project): |
| 117 | status = 'Pending' |
| 118 | if project: |
| 119 | doc = frappe.get_doc('Project', project) |
| 120 | if flt(doc.percent_complete) > 0.0 and flt(doc.percent_complete) < 100.0: |
| 121 | status = 'In Process' |
| 122 | elif flt(doc.percent_complete) == 100.0: |
| 123 | status = 'Completed' |
| 124 | return status |
| 125 | |
Anand Doshi | c280d06 | 2014-05-30 14:43:36 +0530 | [diff] [blame] | 126 | def set_employee_name(doc): |
| 127 | if doc.employee and not doc.employee_name: |
| 128 | doc.employee_name = frappe.db.get_value("Employee", doc.employee, "employee_name") |
Ranjith | fddfffd | 2018-05-05 13:27:26 +0530 | [diff] [blame] | 129 | |
Ranjith Kurungadam | e46639f | 2018-06-11 11:24:44 +0530 | [diff] [blame] | 130 | def update_employee(employee, details, date=None, cancel=False): |
| 131 | internal_work_history = {} |
Manas Solanki | b698846 | 2018-05-10 18:07:20 +0530 | [diff] [blame] | 132 | for item in details: |
| 133 | fieldtype = frappe.get_meta("Employee").get_field(item.fieldname).fieldtype |
| 134 | new_data = item.new if not cancel else item.current |
| 135 | if fieldtype == "Date" and new_data: |
| 136 | new_data = getdate(new_data) |
| 137 | elif fieldtype =="Datetime" and new_data: |
| 138 | new_data = get_datetime(new_data) |
| 139 | setattr(employee, item.fieldname, new_data) |
Ranjith Kurungadam | e46639f | 2018-06-11 11:24:44 +0530 | [diff] [blame] | 140 | if item.fieldname in ["department", "designation", "branch"]: |
| 141 | internal_work_history[item.fieldname] = item.new |
| 142 | if internal_work_history and not cancel: |
| 143 | internal_work_history["from_date"] = date |
| 144 | employee.append("internal_work_history", internal_work_history) |
Manas Solanki | b698846 | 2018-05-10 18:07:20 +0530 | [diff] [blame] | 145 | return employee |
| 146 | |
Ranjith | fddfffd | 2018-05-05 13:27:26 +0530 | [diff] [blame] | 147 | @frappe.whitelist() |
| 148 | def get_employee_fields_label(): |
| 149 | fields = [] |
| 150 | for df in frappe.get_meta("Employee").get("fields"): |
Ranjith Kurungadam | c1030a3 | 2018-06-20 12:42:58 +0530 | [diff] [blame] | 151 | if df.fieldname in ["salutation", "user_id", "employee_number", "employment_type", |
Nabin Hait | 6b9d64c | 2019-05-16 11:23:04 +0530 | [diff] [blame] | 152 | "holiday_list", "branch", "department", "designation", "grade", |
| 153 | "notice_number_of_days", "reports_to", "leave_policy", "company_email"]: |
| 154 | fields.append({"value": df.fieldname, "label": df.label}) |
Ranjith | fddfffd | 2018-05-05 13:27:26 +0530 | [diff] [blame] | 155 | return fields |
| 156 | |
| 157 | @frappe.whitelist() |
| 158 | def get_employee_field_property(employee, fieldname): |
| 159 | if employee and fieldname: |
| 160 | field = frappe.get_meta("Employee").get_field(fieldname) |
| 161 | value = frappe.db.get_value("Employee", employee, fieldname) |
| 162 | options = field.options |
| 163 | if field.fieldtype == "Date": |
| 164 | value = formatdate(value) |
| 165 | elif field.fieldtype == "Datetime": |
| 166 | value = format_datetime(value) |
| 167 | return { |
| 168 | "value" : value, |
| 169 | "datatype" : field.fieldtype, |
| 170 | "label" : field.label, |
| 171 | "options" : options |
| 172 | } |
| 173 | else: |
| 174 | return False |
| 175 | |
Jamsheer | 0e2cc55 | 2018-05-08 11:48:25 +0530 | [diff] [blame] | 176 | def validate_dates(doc, from_date, to_date): |
| 177 | date_of_joining, relieving_date = frappe.db.get_value("Employee", doc.employee, ["date_of_joining", "relieving_date"]) |
| 178 | if getdate(from_date) > getdate(to_date): |
| 179 | frappe.throw(_("To date can not be less than from date")) |
| 180 | elif getdate(from_date) > getdate(nowdate()): |
| 181 | frappe.throw(_("Future dates not allowed")) |
| 182 | elif date_of_joining and getdate(from_date) < getdate(date_of_joining): |
| 183 | frappe.throw(_("From date can not be less than employee's joining date")) |
| 184 | elif relieving_date and getdate(to_date) > getdate(relieving_date): |
| 185 | frappe.throw(_("To date can not greater than employee's relieving date")) |
| 186 | |
| 187 | def validate_overlap(doc, from_date, to_date, company = None): |
| 188 | query = """ |
| 189 | select name |
| 190 | from `tab{0}` |
| 191 | where name != %(name)s |
| 192 | """ |
| 193 | query += get_doc_condition(doc.doctype) |
| 194 | |
| 195 | if not doc.name: |
| 196 | # hack! if name is null, it could cause problems with != |
| 197 | doc.name = "New "+doc.doctype |
| 198 | |
| 199 | overlap_doc = frappe.db.sql(query.format(doc.doctype),{ |
Nabin Hait | d53c2c0 | 2018-07-30 20:16:48 +0530 | [diff] [blame] | 200 | "employee": doc.get("employee"), |
Jamsheer | 0e2cc55 | 2018-05-08 11:48:25 +0530 | [diff] [blame] | 201 | "from_date": from_date, |
| 202 | "to_date": to_date, |
| 203 | "name": doc.name, |
| 204 | "company": company |
| 205 | }, as_dict = 1) |
| 206 | |
| 207 | if overlap_doc: |
deepeshgarg007 | 78b273a | 2018-10-31 18:12:03 +0530 | [diff] [blame] | 208 | if doc.get("employee"): |
| 209 | exists_for = doc.employee |
Jamsheer | 0e2cc55 | 2018-05-08 11:48:25 +0530 | [diff] [blame] | 210 | if company: |
| 211 | exists_for = company |
| 212 | throw_overlap_error(doc, exists_for, overlap_doc[0].name, from_date, to_date) |
| 213 | |
| 214 | def get_doc_condition(doctype): |
| 215 | if doctype == "Compensatory Leave Request": |
| 216 | return "and employee = %(employee)s and docstatus < 2 \ |
| 217 | and (work_from_date between %(from_date)s and %(to_date)s \ |
| 218 | or work_end_date between %(from_date)s and %(to_date)s \ |
| 219 | or (work_from_date < %(from_date)s and work_end_date > %(to_date)s))" |
| 220 | elif doctype == "Leave Period": |
| 221 | return "and company = %(company)s and (from_date between %(from_date)s and %(to_date)s \ |
| 222 | or to_date between %(from_date)s and %(to_date)s \ |
| 223 | or (from_date < %(from_date)s and to_date > %(to_date)s))" |
| 224 | |
| 225 | def throw_overlap_error(doc, exists_for, overlap_doc, from_date, to_date): |
| 226 | msg = _("A {0} exists between {1} and {2} (").format(doc.doctype, |
| 227 | formatdate(from_date), formatdate(to_date)) \ |
Rushabh Mehta | 542bc01 | 2020-11-18 15:00:34 +0530 | [diff] [blame] | 228 | + """ <b><a href="/app/Form/{0}/{1}">{1}</a></b>""".format(doc.doctype, overlap_doc) \ |
Jamsheer | 0e2cc55 | 2018-05-08 11:48:25 +0530 | [diff] [blame] | 229 | + _(") for {0}").format(exists_for) |
| 230 | frappe.throw(msg) |
| 231 | |
Nabin Hait | 58ee6c1 | 2020-04-26 17:45:57 +0530 | [diff] [blame] | 232 | def validate_duplicate_exemption_for_payroll_period(doctype, docname, payroll_period, employee): |
| 233 | existing_record = frappe.db.exists(doctype, { |
| 234 | "payroll_period": payroll_period, |
| 235 | "employee": employee, |
| 236 | 'docstatus': ['<', 2], |
| 237 | 'name': ['!=', docname] |
| 238 | }) |
| 239 | if existing_record: |
| 240 | frappe.throw(_("{0} already exists for employee {1} and period {2}") |
| 241 | .format(doctype, employee, payroll_period), DuplicateDeclarationError) |
| 242 | |
Ranjith | 5a8e642 | 2018-05-10 15:06:49 +0530 | [diff] [blame] | 243 | def validate_tax_declaration(declarations): |
| 244 | subcategories = [] |
Nabin Hait | 04e7bf4 | 2019-04-25 18:44:10 +0530 | [diff] [blame] | 245 | for d in declarations: |
| 246 | if d.exemption_sub_category in subcategories: |
| 247 | frappe.throw(_("More than one selection for {0} not allowed").format(d.exemption_sub_category)) |
| 248 | subcategories.append(d.exemption_sub_category) |
| 249 | |
| 250 | def get_total_exemption_amount(declarations): |
Nabin Hait | 04e7bf4 | 2019-04-25 18:44:10 +0530 | [diff] [blame] | 251 | exemptions = frappe._dict() |
| 252 | for d in declarations: |
| 253 | exemptions.setdefault(d.exemption_category, frappe._dict()) |
| 254 | category_max_amount = exemptions.get(d.exemption_category).max_amount |
| 255 | if not category_max_amount: |
| 256 | category_max_amount = frappe.db.get_value("Employee Tax Exemption Category", d.exemption_category, "max_amount") |
| 257 | exemptions.get(d.exemption_category).max_amount = category_max_amount |
| 258 | sub_category_exemption_amount = d.max_amount \ |
| 259 | if (d.max_amount and flt(d.amount) > flt(d.max_amount)) else d.amount |
| 260 | |
| 261 | exemptions.get(d.exemption_category).setdefault("total_exemption_amount", 0.0) |
| 262 | exemptions.get(d.exemption_category).total_exemption_amount += flt(sub_category_exemption_amount) |
| 263 | |
| 264 | if category_max_amount and exemptions.get(d.exemption_category).total_exemption_amount > category_max_amount: |
| 265 | exemptions.get(d.exemption_category).total_exemption_amount = category_max_amount |
| 266 | |
| 267 | total_exemption_amount = sum([flt(d.total_exemption_amount) for d in exemptions.values()]) |
| 268 | return total_exemption_amount |
rohitwaghchaure | 3f0c735 | 2018-05-14 20:47:35 +0530 | [diff] [blame] | 269 | |
Jamsheer | 0e2cc55 | 2018-05-08 11:48:25 +0530 | [diff] [blame] | 270 | def get_leave_period(from_date, to_date, company): |
| 271 | leave_period = frappe.db.sql(""" |
| 272 | select name, from_date, to_date |
| 273 | from `tabLeave Period` |
| 274 | where company=%(company)s and is_active=1 |
| 275 | and (from_date between %(from_date)s and %(to_date)s |
| 276 | or to_date between %(from_date)s and %(to_date)s |
| 277 | or (from_date < %(from_date)s and to_date > %(to_date)s)) |
| 278 | """, { |
| 279 | "from_date": from_date, |
| 280 | "to_date": to_date, |
| 281 | "company": company |
| 282 | }, as_dict=1) |
| 283 | |
| 284 | if leave_period: |
| 285 | return leave_period |
Ranjith | b485b1e | 2018-05-16 23:01:40 +0530 | [diff] [blame] | 286 | |
Mangesh-Khairnar | f281f00 | 2019-08-05 14:47:02 +0530 | [diff] [blame] | 287 | def generate_leave_encashment(): |
| 288 | ''' Generates a draft leave encashment on allocation expiry ''' |
| 289 | from erpnext.hr.doctype.leave_encashment.leave_encashment import create_leave_encashment |
Mangesh-Khairnar | 3662ed5 | 2019-08-08 19:47:17 +0530 | [diff] [blame] | 290 | |
Mangesh-Khairnar | f281f00 | 2019-08-05 14:47:02 +0530 | [diff] [blame] | 291 | if frappe.db.get_single_value('HR Settings', 'auto_leave_encashment'): |
Mangesh-Khairnar | 3662ed5 | 2019-08-08 19:47:17 +0530 | [diff] [blame] | 292 | leave_type = frappe.get_all('Leave Type', filters={'allow_encashment': 1}, fields=['name']) |
| 293 | leave_type=[l['name'] for l in leave_type] |
Mangesh-Khairnar | f281f00 | 2019-08-05 14:47:02 +0530 | [diff] [blame] | 294 | |
| 295 | leave_allocation = frappe.get_all("Leave Allocation", filters={ |
| 296 | 'to_date': add_days(today(), -1), |
| 297 | 'leave_type': ('in', leave_type) |
| 298 | }, fields=['employee', 'leave_period', 'leave_type', 'to_date', 'total_leaves_allocated', 'new_leaves_allocated']) |
| 299 | |
| 300 | create_leave_encashment(leave_allocation=leave_allocation) |
| 301 | |
Ranjith Kurungadam | 375db61 | 2018-06-01 16:09:28 +0530 | [diff] [blame] | 302 | def allocate_earned_leaves(): |
| 303 | '''Allocate earned leaves to Employees''' |
Anurag Mishra | 755b773 | 2020-11-25 16:05:17 +0530 | [diff] [blame] | 304 | e_leave_types = get_earned_leaves() |
Ranjith Kurungadam | 375db61 | 2018-06-01 16:09:28 +0530 | [diff] [blame] | 305 | today = getdate() |
Ranjith Kurungadam | 375db61 | 2018-06-01 16:09:28 +0530 | [diff] [blame] | 306 | |
Mangesh-Khairnar | 3863fc5 | 2019-06-06 20:34:10 +0530 | [diff] [blame] | 307 | for e_leave_type in e_leave_types: |
Anurag Mishra | 755b773 | 2020-11-25 16:05:17 +0530 | [diff] [blame] | 308 | |
| 309 | leave_allocations = get_leave_allocations(today, e_leave_type.name) |
| 310 | |
Mangesh-Khairnar | 3863fc5 | 2019-06-06 20:34:10 +0530 | [diff] [blame] | 311 | for allocation in leave_allocations: |
Anurag Mishra | 755b773 | 2020-11-25 16:05:17 +0530 | [diff] [blame] | 312 | |
| 313 | if not allocation.leave_policy_assignment and not allocation.leave_policy: |
Mangesh-Khairnar | 3863fc5 | 2019-06-06 20:34:10 +0530 | [diff] [blame] | 314 | continue |
Anurag Mishra | 755b773 | 2020-11-25 16:05:17 +0530 | [diff] [blame] | 315 | |
| 316 | leave_policy = allocation.leave_policy if allocation.leave_policy else frappe.db.get_value( |
| 317 | "Leave Policy Assignment", allocation.leave_policy_assignment, ["leave_policy"]) |
| 318 | |
Mangesh-Khairnar | 261d132 | 2019-08-09 13:18:52 +0530 | [diff] [blame] | 319 | annual_allocation = frappe.db.get_value("Leave Policy Detail", filters={ |
Anurag Mishra | 755b773 | 2020-11-25 16:05:17 +0530 | [diff] [blame] | 320 | 'parent': leave_policy, |
Mangesh-Khairnar | 3662ed5 | 2019-08-08 19:47:17 +0530 | [diff] [blame] | 321 | 'leave_type': e_leave_type.name |
Mangesh-Khairnar | 261d132 | 2019-08-09 13:18:52 +0530 | [diff] [blame] | 322 | }, fieldname=['annual_allocation']) |
Mangesh-Khairnar | 3863fc5 | 2019-06-06 20:34:10 +0530 | [diff] [blame] | 323 | |
Anurag Mishra | 755b773 | 2020-11-25 16:05:17 +0530 | [diff] [blame] | 324 | from_date=allocation.from_date |
Mangesh-Khairnar | 5d5f5b4 | 2020-02-20 13:25:55 +0530 | [diff] [blame] | 325 | |
Anurag Mishra | 755b773 | 2020-11-25 16:05:17 +0530 | [diff] [blame] | 326 | if e_leave_type.based_on_date_of_joining_date: |
| 327 | from_date = frappe.db.get_value("Employee", allocation.employee, "date_of_joining") |
Mangesh-Khairnar | 3863fc5 | 2019-06-06 20:34:10 +0530 | [diff] [blame] | 328 | |
Anurag Mishra | 755b773 | 2020-11-25 16:05:17 +0530 | [diff] [blame] | 329 | if check_effective_date(from_date, today, e_leave_type.earned_leave_frequency, e_leave_type.based_on_date_of_joining_date): |
| 330 | update_previous_leave_allocation(allocation, annual_allocation, e_leave_type) |
| 331 | |
| 332 | def update_previous_leave_allocation(allocation, annual_allocation, e_leave_type): |
Nabin Hait | 190106a | 2021-03-02 13:38:14 +0530 | [diff] [blame] | 333 | earned_leaves = get_monthly_earned_leave(annual_allocation, e_leave_type.earned_leave_frequency, e_leave_type.rounding) |
Anurag Mishra | 755b773 | 2020-11-25 16:05:17 +0530 | [diff] [blame] | 334 | |
| 335 | allocation = frappe.get_doc('Leave Allocation', allocation.name) |
| 336 | new_allocation = flt(allocation.total_leaves_allocated) + flt(earned_leaves) |
| 337 | |
| 338 | if new_allocation > e_leave_type.max_leaves_allowed and e_leave_type.max_leaves_allowed > 0: |
| 339 | new_allocation = e_leave_type.max_leaves_allowed |
| 340 | |
| 341 | if new_allocation != allocation.total_leaves_allocated: |
| 342 | allocation.db_set("total_leaves_allocated", new_allocation, update_modified=False) |
| 343 | today_date = today() |
| 344 | create_additional_leave_ledger_entry(allocation, earned_leaves, today_date) |
| 345 | |
Nabin Hait | 190106a | 2021-03-02 13:38:14 +0530 | [diff] [blame] | 346 | def get_monthly_earned_leave(annual_leaves, frequency, rounding): |
| 347 | earned_leaves = 0.0 |
| 348 | divide_by_frequency = {"Yearly": 1, "Half-Yearly": 6, "Quarterly": 4, "Monthly": 12} |
| 349 | if annual_leaves: |
| 350 | earned_leaves = flt(annual_leaves) / divide_by_frequency[frequency] |
| 351 | if rounding: |
| 352 | if rounding == "0.25": |
| 353 | earned_leaves = round(earned_leaves * 4) / 4 |
| 354 | elif rounding == "0.5": |
| 355 | earned_leaves = round(earned_leaves * 2) / 2 |
| 356 | else: |
| 357 | earned_leaves = round(earned_leaves) |
| 358 | |
| 359 | return earned_leaves |
| 360 | |
Anurag Mishra | 755b773 | 2020-11-25 16:05:17 +0530 | [diff] [blame] | 361 | |
| 362 | def get_leave_allocations(date, leave_type): |
| 363 | return frappe.db.sql("""select name, employee, from_date, to_date, leave_policy_assignment, leave_policy |
| 364 | from `tabLeave Allocation` |
| 365 | where |
| 366 | %s between from_date and to_date and docstatus=1 |
| 367 | and leave_type=%s""", |
| 368 | (date, leave_type), as_dict=1) |
| 369 | |
| 370 | |
| 371 | def get_earned_leaves(): |
| 372 | return frappe.get_all("Leave Type", |
| 373 | fields=["name", "max_leaves_allowed", "earned_leave_frequency", "rounding", "based_on_date_of_joining"], |
| 374 | filters={'is_earned_leave' : 1}) |
Mangesh-Khairnar | 3863fc5 | 2019-06-06 20:34:10 +0530 | [diff] [blame] | 375 | |
Mangesh-Khairnar | 4350846 | 2019-12-09 14:27:38 +0530 | [diff] [blame] | 376 | def create_additional_leave_ledger_entry(allocation, leaves, date): |
| 377 | ''' Create leave ledger entry for leave types ''' |
| 378 | allocation.new_leaves_allocated = leaves |
Mangesh-Khairnar | 3863fc5 | 2019-06-06 20:34:10 +0530 | [diff] [blame] | 379 | allocation.from_date = date |
Mangesh-Khairnar | 5cbe616 | 2019-08-08 17:06:15 +0530 | [diff] [blame] | 380 | allocation.unused_leaves = 0 |
Mangesh-Khairnar | 3863fc5 | 2019-06-06 20:34:10 +0530 | [diff] [blame] | 381 | allocation.create_leave_ledger_entry() |
Ranjith Kurungadam | 375db61 | 2018-06-01 16:09:28 +0530 | [diff] [blame] | 382 | |
Anurag Mishra | 755b773 | 2020-11-25 16:05:17 +0530 | [diff] [blame] | 383 | def check_effective_date(from_date, to_date, frequency, based_on_date_of_joining_date): |
| 384 | import calendar |
Ranjith Kurungadam | 375db61 | 2018-06-01 16:09:28 +0530 | [diff] [blame] | 385 | from dateutil import relativedelta |
Anurag Mishra | 755b773 | 2020-11-25 16:05:17 +0530 | [diff] [blame] | 386 | |
| 387 | from_date = get_datetime(from_date) |
| 388 | to_date = get_datetime(to_date) |
| 389 | rd = relativedelta.relativedelta(to_date, from_date) |
| 390 | #last day of month |
| 391 | last_day = calendar.monthrange(to_date.year, to_date.month)[1] |
| 392 | |
| 393 | if (from_date.day == to_date.day and based_on_date_of_joining_date) or (not based_on_date_of_joining_date and to_date.day == last_day): |
| 394 | if frequency == "Monthly": |
Ranjith Kurungadam | 375db61 | 2018-06-01 16:09:28 +0530 | [diff] [blame] | 395 | return True |
Anurag Mishra | 755b773 | 2020-11-25 16:05:17 +0530 | [diff] [blame] | 396 | elif frequency == "Quarterly" and rd.months % 3: |
Joyce Babu | 3d01213 | 2019-03-06 13:04:45 +0530 | [diff] [blame] | 397 | return True |
Anurag Mishra | 755b773 | 2020-11-25 16:05:17 +0530 | [diff] [blame] | 398 | elif frequency == "Half-Yearly" and rd.months % 6: |
Ranjith Kurungadam | 375db61 | 2018-06-01 16:09:28 +0530 | [diff] [blame] | 399 | return True |
Anurag Mishra | 755b773 | 2020-11-25 16:05:17 +0530 | [diff] [blame] | 400 | elif frequency == "Yearly" and rd.months % 12: |
| 401 | return True |
| 402 | |
| 403 | if frappe.flags.in_test: |
| 404 | return True |
| 405 | |
Ranjith Kurungadam | 375db61 | 2018-06-01 16:09:28 +0530 | [diff] [blame] | 406 | return False |
Nabin Hait | 8c7af49 | 2018-06-04 11:23:36 +0530 | [diff] [blame] | 407 | |
Anurag Mishra | 755b773 | 2020-11-25 16:05:17 +0530 | [diff] [blame] | 408 | |
Ranjith | 155ecc1 | 2018-05-30 13:37:15 +0530 | [diff] [blame] | 409 | def get_salary_assignment(employee, date): |
| 410 | assignment = frappe.db.sql(""" |
| 411 | select * from `tabSalary Structure Assignment` |
| 412 | where employee=%(employee)s |
| 413 | and docstatus = 1 |
Ranjith Kurungadam | b4ad3c3 | 2018-06-25 10:29:54 +0530 | [diff] [blame] | 414 | and %(on_date)s >= from_date order by from_date desc limit 1""", { |
Ranjith | 155ecc1 | 2018-05-30 13:37:15 +0530 | [diff] [blame] | 415 | 'employee': employee, |
| 416 | 'on_date': date, |
| 417 | }, as_dict=1) |
| 418 | return assignment[0] if assignment else None |
Ranjith | 793f8e8 | 2018-05-30 20:50:48 +0530 | [diff] [blame] | 419 | |
Jamsheer | 8d66f1e | 2018-06-12 11:30:59 +0530 | [diff] [blame] | 420 | def get_sal_slip_total_benefit_given(employee, payroll_period, component=False): |
| 421 | total_given_benefit_amount = 0 |
| 422 | query = """ |
| 423 | select sum(sd.amount) as 'total_amount' |
| 424 | from `tabSalary Slip` ss, `tabSalary Detail` sd |
| 425 | where ss.employee=%(employee)s |
| 426 | and ss.docstatus = 1 and ss.name = sd.parent |
| 427 | and sd.is_flexible_benefit = 1 and sd.parentfield = "earnings" |
| 428 | and sd.parenttype = "Salary Slip" |
| 429 | and (ss.start_date between %(start_date)s and %(end_date)s |
| 430 | or ss.end_date between %(start_date)s and %(end_date)s |
| 431 | or (ss.start_date < %(start_date)s and ss.end_date > %(end_date)s)) |
| 432 | """ |
| 433 | |
| 434 | if component: |
| 435 | query += "and sd.salary_component = %(component)s" |
| 436 | |
| 437 | sum_of_given_benefit = frappe.db.sql(query, { |
| 438 | 'employee': employee, |
| 439 | 'start_date': payroll_period.start_date, |
| 440 | 'end_date': payroll_period.end_date, |
| 441 | 'component': component |
| 442 | }, as_dict=True) |
| 443 | |
Rushabh Mehta | df23c7d | 2018-07-05 15:19:28 +0530 | [diff] [blame] | 444 | if sum_of_given_benefit and flt(sum_of_given_benefit[0].total_amount) > 0: |
Jamsheer | 8d66f1e | 2018-06-12 11:30:59 +0530 | [diff] [blame] | 445 | total_given_benefit_amount = sum_of_given_benefit[0].total_amount |
| 446 | return total_given_benefit_amount |
Jamsheer | cc25eb0 | 2018-06-13 15:14:24 +0530 | [diff] [blame] | 447 | |
| 448 | def get_holidays_for_employee(employee, start_date, end_date): |
| 449 | holiday_list = get_holiday_list_for_employee(employee) |
Mangesh-Khairnar | 4350846 | 2019-12-09 14:27:38 +0530 | [diff] [blame] | 450 | |
Jamsheer | cc25eb0 | 2018-06-13 15:14:24 +0530 | [diff] [blame] | 451 | holidays = frappe.db.sql_list('''select holiday_date from `tabHoliday` |
| 452 | where |
| 453 | parent=%(holiday_list)s |
| 454 | and holiday_date >= %(start_date)s |
| 455 | and holiday_date <= %(end_date)s''', { |
| 456 | "holiday_list": holiday_list, |
| 457 | "start_date": start_date, |
| 458 | "end_date": end_date |
| 459 | }) |
| 460 | |
| 461 | holidays = [cstr(i) for i in holidays] |
| 462 | |
| 463 | return holidays |
Ranjith Kurungadam | a8e047a | 2018-06-14 17:56:16 +0530 | [diff] [blame] | 464 | |
| 465 | @erpnext.allow_regional |
| 466 | def calculate_annual_eligible_hra_exemption(doc): |
| 467 | # Don't delete this method, used for localization |
| 468 | # Indian HRA Exemption Calculation |
| 469 | return {} |
| 470 | |
| 471 | @erpnext.allow_regional |
| 472 | def calculate_hra_exemption_for_period(doc): |
| 473 | # Don't delete this method, used for localization |
| 474 | # Indian HRA Exemption Calculation |
| 475 | return {} |
Jamsheer | 55a2f4d | 2018-06-20 11:04:21 +0530 | [diff] [blame] | 476 | |
| 477 | def get_previous_claimed_amount(employee, payroll_period, non_pro_rata=False, component=False): |
| 478 | total_claimed_amount = 0 |
| 479 | query = """ |
| 480 | select sum(claimed_amount) as 'total_amount' |
| 481 | from `tabEmployee Benefit Claim` |
| 482 | where employee=%(employee)s |
| 483 | and docstatus = 1 |
| 484 | and (claim_date between %(start_date)s and %(end_date)s) |
| 485 | """ |
| 486 | if non_pro_rata: |
| 487 | query += "and pay_against_benefit_claim = 1" |
| 488 | if component: |
| 489 | query += "and earning_component = %(component)s" |
| 490 | |
| 491 | sum_of_claimed_amount = frappe.db.sql(query, { |
| 492 | 'employee': employee, |
| 493 | 'start_date': payroll_period.start_date, |
| 494 | 'end_date': payroll_period.end_date, |
| 495 | 'component': component |
| 496 | }, as_dict=True) |
Rushabh Mehta | df23c7d | 2018-07-05 15:19:28 +0530 | [diff] [blame] | 497 | if sum_of_claimed_amount and flt(sum_of_claimed_amount[0].total_amount) > 0: |
Jamsheer | 55a2f4d | 2018-06-20 11:04:21 +0530 | [diff] [blame] | 498 | total_claimed_amount = sum_of_claimed_amount[0].total_amount |
| 499 | return total_claimed_amount |
Anurag Mishra | 755b773 | 2020-11-25 16:05:17 +0530 | [diff] [blame] | 500 | |
| 501 | def grant_leaves_automatically(): |
| 502 | automatically_allocate_leaves_based_on_leave_policy = frappe.db.get_singles_value("HR Settings", "automatically_allocate_leaves_based_on_leave_policy") |
| 503 | if automatically_allocate_leaves_based_on_leave_policy: |
| 504 | lpa = frappe.db.get_all("Leave Policy Assignment", filters={"effective_from": getdate(), "docstatus": 1, "leaves_allocated":0}) |
| 505 | for assignment in lpa: |
| 506 | frappe.get_doc("Leave Policy Assignment", assignment.name).grant_leave_alloc_for_employee() |
Rucha Mahabal | ba10ef4 | 2021-04-04 17:16:48 +0530 | [diff] [blame] | 507 | |
| 508 | def share_doc_with_approver(doc, user): |
| 509 | # if approver does not have permissions, share |
| 510 | if not frappe.has_permission(doc=doc, ptype="submit", user=user): |
Rucha Mahabal | 8c055b5 | 2021-04-04 18:45:06 +0530 | [diff] [blame] | 511 | frappe.share.add(doc.doctype, doc.name, user, submit=1, |
| 512 | flags={"ignore_share_permission": True}) |
| 513 | |
Rucha Mahabal | ba10ef4 | 2021-04-04 17:16:48 +0530 | [diff] [blame] | 514 | frappe.msgprint(_("Shared with the user {0} with {1} access").format( |
| 515 | user, frappe.bold("submit"), alert=True)) |
| 516 | |
| 517 | # remove shared doc if approver changes |
| 518 | doc_before_save = doc.get_doc_before_save() |
| 519 | if doc_before_save: |
| 520 | approvers = { |
| 521 | "Leave Application": "leave_approver", |
| 522 | "Expense Claim": "expense_approver", |
| 523 | "Shift Request": "approver" |
| 524 | } |
| 525 | |
| 526 | approver = approvers.get(doc.doctype) |
| 527 | if doc_before_save.get(approver) != doc.get(approver): |
| 528 | frappe.share.remove(doc.doctype, doc.name, doc_before_save.get(approver)) |