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