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