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