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