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