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