blob: d700e7fccf2da99321f34033d791e94586a89483 [file] [log] [blame]
Anand Doshi885e0742015-03-03 14:55:30 +05301# Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
Rushabh Mehtae67d1fb2013-08-05 14:59:54 +05302# License: GNU General Public License v3. See license.txt
Anand Doshi60666a22013-04-12 20:19:53 +05303
4from __future__ import unicode_literals
Ranjith Kurungadama8e047a2018-06-14 17:56:16 +05305import frappe, erpnext
Rushabh Mehta793ba6b2014-02-14 15:47:51 +05306from frappe import _
Mangesh-Khairnarf281f002019-08-05 14:47:02 +05307from frappe.utils import formatdate, format_datetime, getdate, get_datetime, nowdate, flt, cstr, add_days, today
Manas Solankib6988462018-05-10 18:07:20 +05308from frappe.model.document import Document
9from frappe.desk.form import assign_to
Jamsheercc25eb02018-06-13 15:14:24 +053010from erpnext.hr.doctype.employee.employee import get_holiday_list_for_employee
Manas Solankib6988462018-05-10 18:07:20 +053011
Nabin Hait58ee6c12020-04-26 17:45:57 +053012class DuplicateDeclarationError(frappe.ValidationError): pass
13
Manas Solankib6988462018-05-10 18:07:20 +053014class EmployeeBoardingController(Document):
15 '''
16 Create the project and the task for the boarding process
17 Assign to the concerned person and roles as per the onboarding/separation template
18 '''
19 def validate(self):
20 # remove the task if linked before submitting the form
21 if self.amended_from:
22 for activity in self.activities:
23 activity.task = ''
24
25 def on_submit(self):
26 # create the project for the given employee onboarding
Manas Solanki70899f52018-05-15 18:52:14 +053027 project_name = _(self.doctype) + " : "
Manas Solankib6988462018-05-10 18:07:20 +053028 if self.doctype == "Employee Onboarding":
Manas Solanki70899f52018-05-15 18:52:14 +053029 project_name += self.job_applicant
Manas Solankib6988462018-05-10 18:07:20 +053030 else:
Manas Solanki70899f52018-05-15 18:52:14 +053031 project_name += self.employee
Manas Solankib6988462018-05-10 18:07:20 +053032 project = frappe.get_doc({
33 "doctype": "Project",
34 "project_name": project_name,
35 "expected_start_date": self.date_of_joining if self.doctype == "Employee Onboarding" else self.resignation_letter_date,
36 "department": self.department,
37 "company": self.company
38 }).insert(ignore_permissions=True)
39 self.db_set("project", project.name)
Shreya5c6ade42018-06-20 15:48:27 +053040 self.db_set("boarding_status", "Pending")
Mangesh-Khairnar06a0afa2019-08-05 10:07:05 +053041 self.reload()
42 self.create_task_and_notify_user()
Manas Solankib6988462018-05-10 18:07:20 +053043
Mangesh-Khairnar06a0afa2019-08-05 10:07:05 +053044 def create_task_and_notify_user(self):
Manas Solankib6988462018-05-10 18:07:20 +053045 # create the task for the given project and assign to the concerned person
46 for activity in self.activities:
Mangesh-Khairnar06a0afa2019-08-05 10:07:05 +053047 if activity.task:
48 continue
49
Manas Solankib6988462018-05-10 18:07:20 +053050 task = frappe.get_doc({
51 "doctype": "Task",
Mangesh-Khairnar06a0afa2019-08-05 10:07:05 +053052 "project": self.project,
Manas Solanki094e1842018-05-14 20:33:28 +053053 "subject": activity.activity_name + " : " + self.employee_name,
Manas Solankib6988462018-05-10 18:07:20 +053054 "description": activity.description,
55 "department": self.department,
Suraj Shettyc90364f2019-04-12 14:12:03 +053056 "company": self.company,
57 "task_weight": activity.task_weight
Manas Solankib6988462018-05-10 18:07:20 +053058 }).insert(ignore_permissions=True)
59 activity.db_set("task", task.name)
60 users = [activity.user] if activity.user else []
61 if activity.role:
62 user_list = frappe.db.sql_list('''select distinct(parent) from `tabHas Role`
63 where parenttype='User' and role=%s''', activity.role)
64 users = users + user_list
65
Anurag Mishraadd6bf32019-01-04 11:36:30 +053066 if "Administrator" in users:
67 users.remove("Administrator")
68
Manas Solankib6988462018-05-10 18:07:20 +053069 # assign the task the users
70 if users:
71 self.assign_task_to_users(task, set(users))
72
73 def assign_task_to_users(self, task, users):
74 for user in users:
75 args = {
Anurag Mishra225802e2020-06-04 14:11:18 +053076 'assign_to': [user],
77 'doctype': task.doctype,
78 'name': task.name,
79 'description': task.description or task.subject,
80 'notify': self.notify_users_by_email
Manas Solankib6988462018-05-10 18:07:20 +053081 }
82 assign_to.add(args)
83
84 def on_cancel(self):
85 # delete task project
86 for task in frappe.get_all("Task", filters={"project": self.project}):
Zarrar9a3b7852018-07-11 14:34:55 +053087 frappe.delete_doc("Task", task.name, force=1)
88 frappe.delete_doc("Project", self.project, force=1)
Manas Solankib6988462018-05-10 18:07:20 +053089 self.db_set('project', '')
90 for activity in self.activities:
91 activity.db_set("task", "")
92
93
94@frappe.whitelist()
95def get_onboarding_details(parent, parenttype):
Nabin Hait01b2a652018-08-28 14:09:27 +053096 return frappe.get_all("Employee Boarding Activity",
Himanshu4cb1a1e2019-06-05 10:26:01 +053097 fields=["activity_name", "role", "user", "required_for_employee_creation", "description", "task_weight"],
Manas Solankib6988462018-05-10 18:07:20 +053098 filters={"parent": parent, "parenttype": parenttype},
99 order_by= "idx")
Anand Doshi60666a22013-04-12 20:19:53 +0530100
Shreya5c6ade42018-06-20 15:48:27 +0530101@frappe.whitelist()
102def get_boarding_status(project):
103 status = 'Pending'
104 if project:
105 doc = frappe.get_doc('Project', project)
106 if flt(doc.percent_complete) > 0.0 and flt(doc.percent_complete) < 100.0:
107 status = 'In Process'
108 elif flt(doc.percent_complete) == 100.0:
109 status = 'Completed'
110 return status
111
Anand Doshic280d062014-05-30 14:43:36 +0530112def set_employee_name(doc):
113 if doc.employee and not doc.employee_name:
114 doc.employee_name = frappe.db.get_value("Employee", doc.employee, "employee_name")
Ranjithfddfffd2018-05-05 13:27:26 +0530115
Ranjith Kurungadame46639f2018-06-11 11:24:44 +0530116def update_employee(employee, details, date=None, cancel=False):
117 internal_work_history = {}
Manas Solankib6988462018-05-10 18:07:20 +0530118 for item in details:
119 fieldtype = frappe.get_meta("Employee").get_field(item.fieldname).fieldtype
120 new_data = item.new if not cancel else item.current
121 if fieldtype == "Date" and new_data:
122 new_data = getdate(new_data)
123 elif fieldtype =="Datetime" and new_data:
124 new_data = get_datetime(new_data)
125 setattr(employee, item.fieldname, new_data)
Ranjith Kurungadame46639f2018-06-11 11:24:44 +0530126 if item.fieldname in ["department", "designation", "branch"]:
127 internal_work_history[item.fieldname] = item.new
128 if internal_work_history and not cancel:
129 internal_work_history["from_date"] = date
130 employee.append("internal_work_history", internal_work_history)
Manas Solankib6988462018-05-10 18:07:20 +0530131 return employee
132
Ranjithfddfffd2018-05-05 13:27:26 +0530133@frappe.whitelist()
134def get_employee_fields_label():
135 fields = []
136 for df in frappe.get_meta("Employee").get("fields"):
Ranjith Kurungadamc1030a32018-06-20 12:42:58 +0530137 if df.fieldname in ["salutation", "user_id", "employee_number", "employment_type",
Nabin Hait6b9d64c2019-05-16 11:23:04 +0530138 "holiday_list", "branch", "department", "designation", "grade",
139 "notice_number_of_days", "reports_to", "leave_policy", "company_email"]:
140 fields.append({"value": df.fieldname, "label": df.label})
Ranjithfddfffd2018-05-05 13:27:26 +0530141 return fields
142
143@frappe.whitelist()
144def get_employee_field_property(employee, fieldname):
145 if employee and fieldname:
146 field = frappe.get_meta("Employee").get_field(fieldname)
147 value = frappe.db.get_value("Employee", employee, fieldname)
148 options = field.options
149 if field.fieldtype == "Date":
150 value = formatdate(value)
151 elif field.fieldtype == "Datetime":
152 value = format_datetime(value)
153 return {
154 "value" : value,
155 "datatype" : field.fieldtype,
156 "label" : field.label,
157 "options" : options
158 }
159 else:
160 return False
161
Jamsheer0e2cc552018-05-08 11:48:25 +0530162def validate_dates(doc, from_date, to_date):
163 date_of_joining, relieving_date = frappe.db.get_value("Employee", doc.employee, ["date_of_joining", "relieving_date"])
164 if getdate(from_date) > getdate(to_date):
165 frappe.throw(_("To date can not be less than from date"))
166 elif getdate(from_date) > getdate(nowdate()):
167 frappe.throw(_("Future dates not allowed"))
168 elif date_of_joining and getdate(from_date) < getdate(date_of_joining):
169 frappe.throw(_("From date can not be less than employee's joining date"))
170 elif relieving_date and getdate(to_date) > getdate(relieving_date):
171 frappe.throw(_("To date can not greater than employee's relieving date"))
172
173def validate_overlap(doc, from_date, to_date, company = None):
174 query = """
175 select name
176 from `tab{0}`
177 where name != %(name)s
178 """
179 query += get_doc_condition(doc.doctype)
180
181 if not doc.name:
182 # hack! if name is null, it could cause problems with !=
183 doc.name = "New "+doc.doctype
184
185 overlap_doc = frappe.db.sql(query.format(doc.doctype),{
Nabin Haitd53c2c02018-07-30 20:16:48 +0530186 "employee": doc.get("employee"),
Jamsheer0e2cc552018-05-08 11:48:25 +0530187 "from_date": from_date,
188 "to_date": to_date,
189 "name": doc.name,
190 "company": company
191 }, as_dict = 1)
192
193 if overlap_doc:
deepeshgarg00778b273a2018-10-31 18:12:03 +0530194 if doc.get("employee"):
195 exists_for = doc.employee
Jamsheer0e2cc552018-05-08 11:48:25 +0530196 if company:
197 exists_for = company
198 throw_overlap_error(doc, exists_for, overlap_doc[0].name, from_date, to_date)
199
200def get_doc_condition(doctype):
201 if doctype == "Compensatory Leave Request":
202 return "and employee = %(employee)s and docstatus < 2 \
203 and (work_from_date between %(from_date)s and %(to_date)s \
204 or work_end_date between %(from_date)s and %(to_date)s \
205 or (work_from_date < %(from_date)s and work_end_date > %(to_date)s))"
206 elif doctype == "Leave Period":
207 return "and company = %(company)s and (from_date between %(from_date)s and %(to_date)s \
208 or to_date between %(from_date)s and %(to_date)s \
209 or (from_date < %(from_date)s and to_date > %(to_date)s))"
210
211def throw_overlap_error(doc, exists_for, overlap_doc, from_date, to_date):
212 msg = _("A {0} exists between {1} and {2} (").format(doc.doctype,
213 formatdate(from_date), formatdate(to_date)) \
214 + """ <b><a href="#Form/{0}/{1}">{1}</a></b>""".format(doc.doctype, overlap_doc) \
215 + _(") for {0}").format(exists_for)
216 frappe.throw(msg)
217
Nabin Hait58ee6c12020-04-26 17:45:57 +0530218def validate_duplicate_exemption_for_payroll_period(doctype, docname, payroll_period, employee):
219 existing_record = frappe.db.exists(doctype, {
220 "payroll_period": payroll_period,
221 "employee": employee,
222 'docstatus': ['<', 2],
223 'name': ['!=', docname]
224 })
225 if existing_record:
226 frappe.throw(_("{0} already exists for employee {1} and period {2}")
227 .format(doctype, employee, payroll_period), DuplicateDeclarationError)
228
Ranjith5a8e6422018-05-10 15:06:49 +0530229def validate_tax_declaration(declarations):
230 subcategories = []
Nabin Hait04e7bf42019-04-25 18:44:10 +0530231 for d in declarations:
232 if d.exemption_sub_category in subcategories:
233 frappe.throw(_("More than one selection for {0} not allowed").format(d.exemption_sub_category))
234 subcategories.append(d.exemption_sub_category)
235
236def get_total_exemption_amount(declarations):
Nabin Hait04e7bf42019-04-25 18:44:10 +0530237 exemptions = frappe._dict()
238 for d in declarations:
239 exemptions.setdefault(d.exemption_category, frappe._dict())
240 category_max_amount = exemptions.get(d.exemption_category).max_amount
241 if not category_max_amount:
242 category_max_amount = frappe.db.get_value("Employee Tax Exemption Category", d.exemption_category, "max_amount")
243 exemptions.get(d.exemption_category).max_amount = category_max_amount
244 sub_category_exemption_amount = d.max_amount \
245 if (d.max_amount and flt(d.amount) > flt(d.max_amount)) else d.amount
246
247 exemptions.get(d.exemption_category).setdefault("total_exemption_amount", 0.0)
248 exemptions.get(d.exemption_category).total_exemption_amount += flt(sub_category_exemption_amount)
249
250 if category_max_amount and exemptions.get(d.exemption_category).total_exemption_amount > category_max_amount:
251 exemptions.get(d.exemption_category).total_exemption_amount = category_max_amount
252
253 total_exemption_amount = sum([flt(d.total_exemption_amount) for d in exemptions.values()])
254 return total_exemption_amount
rohitwaghchaure3f0c7352018-05-14 20:47:35 +0530255
Jamsheer0e2cc552018-05-08 11:48:25 +0530256def get_leave_period(from_date, to_date, company):
257 leave_period = frappe.db.sql("""
258 select name, from_date, to_date
259 from `tabLeave Period`
260 where company=%(company)s and is_active=1
261 and (from_date between %(from_date)s and %(to_date)s
262 or to_date between %(from_date)s and %(to_date)s
263 or (from_date < %(from_date)s and to_date > %(to_date)s))
264 """, {
265 "from_date": from_date,
266 "to_date": to_date,
267 "company": company
268 }, as_dict=1)
269
270 if leave_period:
271 return leave_period
Ranjithb485b1e2018-05-16 23:01:40 +0530272
Mangesh-Khairnarf281f002019-08-05 14:47:02 +0530273def generate_leave_encashment():
274 ''' Generates a draft leave encashment on allocation expiry '''
275 from erpnext.hr.doctype.leave_encashment.leave_encashment import create_leave_encashment
Mangesh-Khairnar3662ed52019-08-08 19:47:17 +0530276
Mangesh-Khairnarf281f002019-08-05 14:47:02 +0530277 if frappe.db.get_single_value('HR Settings', 'auto_leave_encashment'):
Mangesh-Khairnar3662ed52019-08-08 19:47:17 +0530278 leave_type = frappe.get_all('Leave Type', filters={'allow_encashment': 1}, fields=['name'])
279 leave_type=[l['name'] for l in leave_type]
Mangesh-Khairnarf281f002019-08-05 14:47:02 +0530280
281 leave_allocation = frappe.get_all("Leave Allocation", filters={
282 'to_date': add_days(today(), -1),
283 'leave_type': ('in', leave_type)
284 }, fields=['employee', 'leave_period', 'leave_type', 'to_date', 'total_leaves_allocated', 'new_leaves_allocated'])
285
286 create_leave_encashment(leave_allocation=leave_allocation)
287
Ranjith Kurungadam375db612018-06-01 16:09:28 +0530288def allocate_earned_leaves():
289 '''Allocate earned leaves to Employees'''
Anurag Mishra755b7732020-11-25 16:05:17 +0530290 e_leave_types = get_earned_leaves()
Ranjith Kurungadam375db612018-06-01 16:09:28 +0530291 today = getdate()
Ranjith Kurungadam375db612018-06-01 16:09:28 +0530292
Mangesh-Khairnar3863fc52019-06-06 20:34:10 +0530293 for e_leave_type in e_leave_types:
Anurag Mishra755b7732020-11-25 16:05:17 +0530294
295 leave_allocations = get_leave_allocations(today, e_leave_type.name)
296
Mangesh-Khairnar3863fc52019-06-06 20:34:10 +0530297 for allocation in leave_allocations:
Anurag Mishra755b7732020-11-25 16:05:17 +0530298
299 if not allocation.leave_policy_assignment and not allocation.leave_policy:
Mangesh-Khairnar3863fc52019-06-06 20:34:10 +0530300 continue
Anurag Mishra755b7732020-11-25 16:05:17 +0530301
302 leave_policy = allocation.leave_policy if allocation.leave_policy else frappe.db.get_value(
303 "Leave Policy Assignment", allocation.leave_policy_assignment, ["leave_policy"])
304
Mangesh-Khairnar261d1322019-08-09 13:18:52 +0530305 annual_allocation = frappe.db.get_value("Leave Policy Detail", filters={
Anurag Mishra755b7732020-11-25 16:05:17 +0530306 'parent': leave_policy,
Mangesh-Khairnar3662ed52019-08-08 19:47:17 +0530307 'leave_type': e_leave_type.name
Mangesh-Khairnar261d1322019-08-09 13:18:52 +0530308 }, fieldname=['annual_allocation'])
Mangesh-Khairnar3863fc52019-06-06 20:34:10 +0530309
Anurag Mishra755b7732020-11-25 16:05:17 +0530310 from_date=allocation.from_date
Mangesh-Khairnar5d5f5b42020-02-20 13:25:55 +0530311
Anurag Mishra755b7732020-11-25 16:05:17 +0530312 if e_leave_type.based_on_date_of_joining_date:
313 from_date = frappe.db.get_value("Employee", allocation.employee, "date_of_joining")
Mangesh-Khairnar3863fc52019-06-06 20:34:10 +0530314
Anurag Mishra755b7732020-11-25 16:05:17 +0530315 if check_effective_date(from_date, today, e_leave_type.earned_leave_frequency, e_leave_type.based_on_date_of_joining_date):
316 update_previous_leave_allocation(allocation, annual_allocation, e_leave_type)
317
318def update_previous_leave_allocation(allocation, annual_allocation, e_leave_type):
319 divide_by_frequency = {"Yearly": 1, "Half-Yearly": 6, "Quarterly": 4, "Monthly": 12}
320 if annual_allocation:
321 earned_leaves = flt(annual_allocation) / divide_by_frequency[e_leave_type.earned_leave_frequency]
322 if e_leave_type.rounding == "0.5":
323 earned_leaves = round(earned_leaves * 2) / 2
324 else:
325 earned_leaves = round(earned_leaves)
326
327 allocation = frappe.get_doc('Leave Allocation', allocation.name)
328 new_allocation = flt(allocation.total_leaves_allocated) + flt(earned_leaves)
329
330 if new_allocation > e_leave_type.max_leaves_allowed and e_leave_type.max_leaves_allowed > 0:
331 new_allocation = e_leave_type.max_leaves_allowed
332
333 if new_allocation != allocation.total_leaves_allocated:
334 allocation.db_set("total_leaves_allocated", new_allocation, update_modified=False)
335 today_date = today()
336 create_additional_leave_ledger_entry(allocation, earned_leaves, today_date)
337
338
339def get_leave_allocations(date, leave_type):
340 return frappe.db.sql("""select name, employee, from_date, to_date, leave_policy_assignment, leave_policy
341 from `tabLeave Allocation`
342 where
343 %s between from_date and to_date and docstatus=1
344 and leave_type=%s""",
345 (date, leave_type), as_dict=1)
346
347
348def get_earned_leaves():
349 return frappe.get_all("Leave Type",
350 fields=["name", "max_leaves_allowed", "earned_leave_frequency", "rounding", "based_on_date_of_joining"],
351 filters={'is_earned_leave' : 1})
Mangesh-Khairnar3863fc52019-06-06 20:34:10 +0530352
Mangesh-Khairnar43508462019-12-09 14:27:38 +0530353def create_additional_leave_ledger_entry(allocation, leaves, date):
354 ''' Create leave ledger entry for leave types '''
355 allocation.new_leaves_allocated = leaves
Mangesh-Khairnar3863fc52019-06-06 20:34:10 +0530356 allocation.from_date = date
Mangesh-Khairnar5cbe6162019-08-08 17:06:15 +0530357 allocation.unused_leaves = 0
Mangesh-Khairnar3863fc52019-06-06 20:34:10 +0530358 allocation.create_leave_ledger_entry()
Ranjith Kurungadam375db612018-06-01 16:09:28 +0530359
Anurag Mishra755b7732020-11-25 16:05:17 +0530360def check_effective_date(from_date, to_date, frequency, based_on_date_of_joining_date):
361 import calendar
Ranjith Kurungadam375db612018-06-01 16:09:28 +0530362 from dateutil import relativedelta
Anurag Mishra755b7732020-11-25 16:05:17 +0530363
364 from_date = get_datetime(from_date)
365 to_date = get_datetime(to_date)
366 rd = relativedelta.relativedelta(to_date, from_date)
367 #last day of month
368 last_day = calendar.monthrange(to_date.year, to_date.month)[1]
369
370 if (from_date.day == to_date.day and based_on_date_of_joining_date) or (not based_on_date_of_joining_date and to_date.day == last_day):
371 if frequency == "Monthly":
Ranjith Kurungadam375db612018-06-01 16:09:28 +0530372 return True
Anurag Mishra755b7732020-11-25 16:05:17 +0530373 elif frequency == "Quarterly" and rd.months % 3:
Joyce Babu3d012132019-03-06 13:04:45 +0530374 return True
Anurag Mishra755b7732020-11-25 16:05:17 +0530375 elif frequency == "Half-Yearly" and rd.months % 6:
Ranjith Kurungadam375db612018-06-01 16:09:28 +0530376 return True
Anurag Mishra755b7732020-11-25 16:05:17 +0530377 elif frequency == "Yearly" and rd.months % 12:
378 return True
379
380 if frappe.flags.in_test:
381 return True
382
Ranjith Kurungadam375db612018-06-01 16:09:28 +0530383 return False
Nabin Hait8c7af492018-06-04 11:23:36 +0530384
Anurag Mishra755b7732020-11-25 16:05:17 +0530385
Ranjith155ecc12018-05-30 13:37:15 +0530386def get_salary_assignment(employee, date):
387 assignment = frappe.db.sql("""
388 select * from `tabSalary Structure Assignment`
389 where employee=%(employee)s
390 and docstatus = 1
Ranjith Kurungadamb4ad3c32018-06-25 10:29:54 +0530391 and %(on_date)s >= from_date order by from_date desc limit 1""", {
Ranjith155ecc12018-05-30 13:37:15 +0530392 'employee': employee,
393 'on_date': date,
394 }, as_dict=1)
395 return assignment[0] if assignment else None
Ranjith793f8e82018-05-30 20:50:48 +0530396
Jamsheer8d66f1e2018-06-12 11:30:59 +0530397def get_sal_slip_total_benefit_given(employee, payroll_period, component=False):
398 total_given_benefit_amount = 0
399 query = """
400 select sum(sd.amount) as 'total_amount'
401 from `tabSalary Slip` ss, `tabSalary Detail` sd
402 where ss.employee=%(employee)s
403 and ss.docstatus = 1 and ss.name = sd.parent
404 and sd.is_flexible_benefit = 1 and sd.parentfield = "earnings"
405 and sd.parenttype = "Salary Slip"
406 and (ss.start_date between %(start_date)s and %(end_date)s
407 or ss.end_date between %(start_date)s and %(end_date)s
408 or (ss.start_date < %(start_date)s and ss.end_date > %(end_date)s))
409 """
410
411 if component:
412 query += "and sd.salary_component = %(component)s"
413
414 sum_of_given_benefit = frappe.db.sql(query, {
415 'employee': employee,
416 'start_date': payroll_period.start_date,
417 'end_date': payroll_period.end_date,
418 'component': component
419 }, as_dict=True)
420
Rushabh Mehtadf23c7d2018-07-05 15:19:28 +0530421 if sum_of_given_benefit and flt(sum_of_given_benefit[0].total_amount) > 0:
Jamsheer8d66f1e2018-06-12 11:30:59 +0530422 total_given_benefit_amount = sum_of_given_benefit[0].total_amount
423 return total_given_benefit_amount
Jamsheercc25eb02018-06-13 15:14:24 +0530424
425def get_holidays_for_employee(employee, start_date, end_date):
426 holiday_list = get_holiday_list_for_employee(employee)
Mangesh-Khairnar43508462019-12-09 14:27:38 +0530427
Jamsheercc25eb02018-06-13 15:14:24 +0530428 holidays = frappe.db.sql_list('''select holiday_date from `tabHoliday`
429 where
430 parent=%(holiday_list)s
431 and holiday_date >= %(start_date)s
432 and holiday_date <= %(end_date)s''', {
433 "holiday_list": holiday_list,
434 "start_date": start_date,
435 "end_date": end_date
436 })
437
438 holidays = [cstr(i) for i in holidays]
439
440 return holidays
Ranjith Kurungadama8e047a2018-06-14 17:56:16 +0530441
442@erpnext.allow_regional
443def calculate_annual_eligible_hra_exemption(doc):
444 # Don't delete this method, used for localization
445 # Indian HRA Exemption Calculation
446 return {}
447
448@erpnext.allow_regional
449def calculate_hra_exemption_for_period(doc):
450 # Don't delete this method, used for localization
451 # Indian HRA Exemption Calculation
452 return {}
Jamsheer55a2f4d2018-06-20 11:04:21 +0530453
454def get_previous_claimed_amount(employee, payroll_period, non_pro_rata=False, component=False):
455 total_claimed_amount = 0
456 query = """
457 select sum(claimed_amount) as 'total_amount'
458 from `tabEmployee Benefit Claim`
459 where employee=%(employee)s
460 and docstatus = 1
461 and (claim_date between %(start_date)s and %(end_date)s)
462 """
463 if non_pro_rata:
464 query += "and pay_against_benefit_claim = 1"
465 if component:
466 query += "and earning_component = %(component)s"
467
468 sum_of_claimed_amount = frappe.db.sql(query, {
469 'employee': employee,
470 'start_date': payroll_period.start_date,
471 'end_date': payroll_period.end_date,
472 'component': component
473 }, as_dict=True)
Rushabh Mehtadf23c7d2018-07-05 15:19:28 +0530474 if sum_of_claimed_amount and flt(sum_of_claimed_amount[0].total_amount) > 0:
Jamsheer55a2f4d2018-06-20 11:04:21 +0530475 total_claimed_amount = sum_of_claimed_amount[0].total_amount
476 return total_claimed_amount
Anurag Mishra755b7732020-11-25 16:05:17 +0530477
478def grant_leaves_automatically():
479 automatically_allocate_leaves_based_on_leave_policy = frappe.db.get_singles_value("HR Settings", "automatically_allocate_leaves_based_on_leave_policy")
480 if automatically_allocate_leaves_based_on_leave_policy:
481 lpa = frappe.db.get_all("Leave Policy Assignment", filters={"effective_from": getdate(), "docstatus": 1, "leaves_allocated":0})
482 for assignment in lpa:
483 frappe.get_doc("Leave Policy Assignment", assignment.name).grant_leave_alloc_for_employee()