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