def on_submit(self): company = frappe.db.get_value("Employee", self.employee, "company") date_difference = date_diff(self.work_end_date, self.work_from_date) + 1 if self.half_day: date_difference -= 0.5 leave_period = get_leave_period(self.work_from_date, self.work_end_date, company) if leave_period: leave_allocation = self.get_existing_allocation_for_period( leave_period) if leave_allocation: leave_allocation.new_leaves_allocated += date_difference leave_allocation.validate() leave_allocation.db_set( "new_leaves_allocated", leave_allocation.total_leaves_allocated) leave_allocation.db_set( "total_leaves_allocated", leave_allocation.total_leaves_allocated) # generate additional ledger entry for the new compensatory leaves off create_additional_leave_ledger_entry( leave_allocation, date_difference, add_days(self.work_end_date, 1)) else: leave_allocation = self.create_leave_allocation( leave_period, date_difference) self.leave_allocation = leave_allocation.name else: frappe.throw( _("There is no leave period in between {0} and {1}").format( format_date(self.work_from_date), format_date(self.work_end_date)))
def validate_optional_leave(self): leave_period = get_leave_period(self.from_date, self.to_date, self.company) if not leave_period: frappe.throw(_("Cannot find active Leave Period")) optional_holiday_list = frappe.db.get_value("Leave Period", leave_period[0]["name"], "optional_holiday_list") if not optional_holiday_list: frappe.throw(_("Optional Holiday List not set for leave period {0}").format(leave_period[0]["name"])) day = getdate(self.from_date) while day <= getdate(self.to_date): if not frappe.db.exists({"doctype": "Holiday", "parent": optional_holiday_list, "holiday_date": day}): frappe.throw(_("{0} is not in Optional Holiday List").format(formatdate(day)), NotAnOptionalHoliday) day = add_days(day, 1)
def validate_leave_allocation_days(self): company = frappe.db.get_value("Employee", self.employee, "company") leave_period = get_leave_period(self.from_date, self.to_date, company) max_leaves_allowed = frappe.db.get_value("Leave Type", self.leave_type, "max_leaves_allowed") if max_leaves_allowed > 0: leave_allocated = 0 if leave_period: leave_allocated = get_leave_allocation_for_period(self.employee, self.leave_type, leave_period[0].from_date, leave_period[0].to_date) leave_allocated += self.new_leaves_allocated if leave_allocated > max_leaves_allowed: frappe.throw(_("Total allocated leaves are more days than maximum allocation of {0} leave type for employee {1} in the period")\ .format(self.leave_type, self.employee))
def validate_leave_allocation_days(self): company = frappe.db.get_value("Employee", self.employee, "company") leave_period = get_leave_period(self.from_date, self.to_date, company) max_leaves_allowed = flt(frappe.db.get_value("Leave Type", self.leave_type, "max_leaves_allowed")) if max_leaves_allowed > 0: leave_allocated = 0 if leave_period: leave_allocated = get_leave_allocation_for_period(self.employee, self.leave_type, leave_period[0].from_date, leave_period[0].to_date) leave_allocated += flt(self.new_leaves_allocated) if leave_allocated > max_leaves_allowed: frappe.throw(_("Total allocated leaves are more days than maximum allocation of {0} leave type for employee {1} in the period") .format(self.leave_type, self.employee))
def mark_cl(): date_time = date.today() date1 = frappe.defaults.get_defaults().fiscal_year year_start_date = frappe.db.get_value("Fiscal Year", {'name': date1}, ['year_start_date']) year_end_date = frappe.db.get_value("Fiscal Year", {'name': date1}, ['year_end_date']) leave_type = 'Casual Leave' leave_period = get_leave_period(date_time, date_time, frappe.db.get_default("Company")) new_leaves_allocated = 1.5 d = date_time.strftime("%B %Y") employees = frappe.db.sql("""select name from `tabEmployee` where status = "Active" and employment_type != "Contract" and company = "Voltech HR Services Private Limited" and datediff(curdate(),date_of_joining) >= 365 """, as_dict=True) for employee in employees: emp = frappe.get_doc("Employee", employee) cl_ids = get_cl(emp.employee, year_start_date, year_end_date) if cl_ids: for cl_id in cl_ids: cl = frappe.get_doc("Leave Allocation", cl_id['name']) cl.new_leaves_allocated += new_leaves_allocated cl.total_leaves_allocated += new_leaves_allocated if cl.description: cl.description += '<br>' + \ 'casual Leave for {0} is {1}'.format( date_time, cl.new_leaves_allocated) else: cl.description = '<br>' + \ 'casual Leave for {0} is {1}'.format( date_time, cl.new_leaves_allocated) cl.db_update() frappe.db.commit else: cl = frappe.new_doc("Leave Allocation") cl.employee = emp.employee cl.leave_type = 'casual Leave' cl.from_date = year_start_date cl.to_date = year_end_date cl.new_leaves_allocated = new_leaves_allocated cl.description = 'casual Leave for {0} is {1}'.format( date_time, cl.new_leaves_allocated) cl.save(ignore_permissions=True) cl.submit() frappe.db.commit() leave_ledger_entry = create_leave_ledger(cl.name, emp.employee, year_start_date, year_end_date, leave_type, new_leaves_allocated, leave_period)
def encash_unused_leave(self): leave_period = get_leave_period(self.from_date, self.to_date, self.company) if leave_period: leave_period = leave_period[0]["name"] else: frappe.throw("Please Check Leave Period") leave_encashment = frappe.new_doc("Leave Encashment") leave_encashment.leave_period = leave_period leave_encashment.employee = self.employee leave_encashment.leave_type = self.leave_type leave_encashment.leave_allocation = get_leave_allocation( self.posting_date, self.leave_type, self.employee) leave_encashment.leave_balance = self.total_leave_days leave_encashment.encashable_days_from_out = self.total_leaves leave_encashment.encashable_days = self.total_leaves leave_encashment.encashment_date = self.posting_date leave_encashment.save(ignore_permissions=True) leave_encashment.submit()
def on_submit(self): company = frappe.db.get_value("Employee", self.employee, "company") date_difference = date_diff(self.work_end_date, self.work_from_date) + 1 if self.half_day: date_difference -= 0.5 leave_period = get_leave_period(self.work_from_date, self.work_end_date, company) if leave_period: leave_allocation = self.exists_allocation_for_period(leave_period) if leave_allocation: leave_allocation.new_leaves_allocated += date_difference leave_allocation.submit() else: leave_allocation = self.create_leave_allocation( leave_period, date_difference) self.db_set("leave_allocation", leave_allocation.name) else: frappe.throw( _("There is no leave period in between {0} and {1}").format( self.work_from_date, self.work_end_date))
def get_carry_forwarded_leaves(employee, leave_type, date, carry_forward=None, till_now=False): carry_forwarded_leaves = 0 if carry_forward: validate_carry_forward(leave_type) previous_allocation = frappe.db.sql(""" select name, from_date, to_date, total_leaves_allocated from `tabLeave Allocation` where employee=%s and leave_type=%s and docstatus=1 and to_date < %s order by to_date desc limit 1 """, (employee, leave_type, date), as_dict=1) if previous_allocation: leaves_taken = get_approved_leaves_for_period( employee, leave_type, previous_allocation[0].from_date, previous_allocation[0].to_date) carry_forwarded_leaves = flt( previous_allocation[0].total_leaves_allocated) - flt( leaves_taken) if till_now: days_in_current_allocated = 0.0 current_allocation = frappe.db.sql(""" select from_date, to_date, total_leaves_allocated, new_leaves_allocated ,(select sum(encashable_days) from `tabLeave Encashment` where employee= %s and encashment_date BETWEEN L.from_date and L.to_date) last_encashed from `tabLeave Allocation` L where employee= %s and leave_type= %s and %s BETWEEN from_date and to_date and docstatus=1 """, (employee, employee, 'Casual Leave', date), as_dict=1) # frappe.msgprint(str(current_allocation)) if current_allocation: current_allocation = current_allocation[0] leave_period = get_leave_period( current_allocation["from_date"], current_allocation["to_date"], get_default_company(frappe.session.user)) if leave_period: leave_period = leave_period[0] total_lwp_days = get_leaves_without_pay( leave_period["from_date"], leave_period["to_date"], employee) else: frappe.throw("Please set Leave Period") days_diff = date_diff(date, current_allocation["from_date"]) + 1 month_diff = \ get_dates_diff(add_days(current_allocation["from_date"], (total_lwp_days if total_lwp_days else 0.0)), date)["months"] Calculation_option = 360 #frappe.db.get_single_value("HR Settings", "day_calculation") old_leaves_allocated = flt( current_allocation["total_leaves_allocated"]) - flt( current_allocation["new_leaves_allocated"]) balance_encashment_options = frappe.db.get_single_value( "HR Settings", "balance_encashment_options") if Calculation_option in ("360", "365"): days_diff -= (total_lwp_days if total_lwp_days else 0.0) days_in_current_allocated = round_to_nearest_half( (flt(current_allocation["new_leaves_allocated"]) / flt(Calculation_option)) * flt(days_diff)) else: days_in_current_allocated = round_to_nearest_half( (flt(current_allocation["new_leaves_allocated"]) / 12) * (month_diff if month_diff > 0 else 0)) if balance_encashment_options == "Forwarded Balance From last Year": carry_forwarded_leaves = old_leaves_allocated days_in_current_allocated = 0 # elif balance_encashment_options=="Balance Till Submit Day": elif balance_encashment_options == "Annual Balance": days_in_current_allocated = days_in_current_allocated + old_leaves_allocated carry_forwarded_leaves += ((days_in_current_allocated) if days_in_current_allocated > 0 else 0) if current_allocation[ "last_encashed"] != None and carry_forwarded_leaves > flt( current_allocation["last_encashed"]): carry_forwarded_leaves = carry_forwarded_leaves - flt( current_allocation["last_encashed"]) # frappe.msgprint(str(carry_forwarded_leaves)) return carry_forwarded_leaves
def mark_comp_off(): # day = datetime.strptime(add_days(today(),-1), "%Y-%m-%d").date() # company = frappe.db.get_value("Employee", self.employee, "company") # days = ['2019-10-02','2019-10-06','2019-10-07','2019-10-13','2019-10-26','2019-10-27'] # for day in days: day = '2020-01-05' leave_period = get_leave_period(day, day, frappe.db.get_default("Company")) query = """select employee,time,name from `tabEmployee Checkin` where date(time) = '%s' and comp_off_allocated = 0 order by employee,time,name """ % day checkins = frappe.db.sql(query, as_dict=1) if checkins: for key, group in itertools.groupby(checkins, key=lambda x: (x['employee'])): logs = list(group) total_hours = 0 in_time = out_time = None in_time = logs[0].time employee = logs[0].employee from_date = day to_date = leave_period[0].to_date leave_type = 'Compensatory Off' if len(logs) >= 2: out_time = logs[-1].time new_leaves_allocated = 0 if in_time and out_time: total_hours = time_diff_in_hours(in_time, logs[-1].time) if total_hours > 5: new_leaves_allocated = 0.5 if total_hours >= 9.5: new_leaves_allocated = 1 holiday_list = frappe.db.get_value("Employee", {'employee': employee}, ['holiday_list']) holiday_date = frappe.db.get_value("Holiday", { 'holiday_date': day, 'parent': holiday_list }, ["holiday_date"]) lal_id = frappe.db.get_value( "Leave Allocation", { 'employee': employee, "docstatus": 1, "leave_type": "Compensatory Off" }) if lal_id: lal = frappe.get_doc("Leave Allocation", lal_id) lal.new_leaves_allocated += new_leaves_allocated lal.total_leaves_allocated += new_leaves_allocated # lal.description += '<br>' + \ # 'Comp-off for {0} for {1} day'.format( # day, new_leaves_allocated) lal.db_update() frappe.db.commit else: lal = frappe.new_doc("Leave Allocation") lal.employee = employee lal.leave_type = 'Compensatory Off' lal.from_date = day lal.to_date = leave_period[0].to_date lal.new_leaves_allocated = new_leaves_allocated lal.description = 'Comp-off for {0} for {1} day'.format( day, new_leaves_allocated) lal.save(ignore_permissions=True) lal.submit() frappe.db.commit() leave_ledger_entry = create_leave_ledger( lal.name, employee, from_date, to_date, leave_type, new_leaves_allocated, leave_period) frappe.db.set_value("Employee Checkin", logs[0].name, "comp_off_allocated", 1) frappe.db.set_value("Employee Checkin", logs[-1].name, "comp_off_allocated", 1)