def grant_leave_alloc(self, employee): self.validate_allocation_exists(employee) leave_policy = get_employee_leave_policy(employee) if leave_policy: for leave_policy_detail in leave_policy.leave_policy_details: if not frappe.db.get_value("Leave Type", leave_policy_detail.leave_type, "is_lwp"): self.create_leave_allocation(employee, leave_policy_detail.leave_type, leave_policy_detail.annual_allocation)
def grant_leave_alloc(self, employee): self.validate_allocation_exists(employee) leave_policy = get_employee_leave_policy(employee) if leave_policy: for leave_policy_detail in leave_policy.leave_policy_details: if not frappe.db.get_value("Leave Type", leave_policy_detail.leave_type, "is_lwp"): self.create_leave_allocation( employee, leave_policy_detail.leave_type, leave_policy_detail.annual_allocation)
def get_context(context): if frappe.session.user == 'Guest': frappe.local.flags.redirect_location = '/' raise frappe.Redirect context.user = frappe.session.user context.csrf_token = frappe.sessions.get_csrf_token() context.employee = frappe.db.get_value( "Employee", {"user_id": context.user}, "name") or "" valid_roles = ['HR Manager'] if not frappe.utils.is_subset(valid_roles, frappe.get_roles()): frappe.throw( _('Only users with {0} role can access').format( ', '.join(valid_roles)), frappe.PermissionError) company = get_default_company() leave_period = get_leave_period(nowdate(), nowdate(), company) if leave_period: context.leave_period = leave_period[0] else: context.leave_period = frappe.get_doc("Leave Period") context.list_company = frappe.db.get_all("Company", fields=["company_name"]) context.list_holiday = frappe.db.get_all("Holiday List", fields=["holiday_list_name"]) holiday_list_name = frappe.get_cached_value('Company', company, "default_holiday_list") holiday_list_name_param = frappe.form_dict.holiday_list_name if holiday_list_name_param: holiday_list_name = holiday_list_name_param if holiday_list_name: context.holiday_list_name = holiday_list_name context.holiday = frappe.get_doc("Holiday List", holiday_list_name) else: context.holiday = frappe.new_doc("Holiday List") context.holiday_list_name = "" context.list_leave_type = frappe.db.get_all("Leave Type", fields=["name"]) if context.employee: leave_policy = get_employee_leave_policy(context.employee) if leave_policy: context.leave_policy_name = leave_policy.name else: context.leave_policy_name = "" return context
def get_project_earned_leave_balance(employee, leave_type, start_date=getdate(nowdate())): leave_type_doc = frappe.get_doc("Leave Type", leave_type) if leave_type_doc.is_earned_leave == 0: return None leave_policy = get_employee_leave_policy(employee) if not leave_policy: return None #current balance current_balance = get_leave_balance_on( employee, leave_type, start_date, consider_all_leaves_in_the_allocation_period=True) #end_balance = get_leave_balance_on(employee, leave_type, # start_date, consider_all_leaves_in_the_allocation_period=True) divide_by_frequency = {"Yearly": 1, "Quarterly": 4, "Monthly": 12} days_to_project = date_diff(start_date, get_first_day(getdate(nowdate()))) number_of_months = days_to_project / 30 annual_allocation = frappe.db.sql( """select annual_allocation from `tabLeave Policy Detail` where parent=%s and leave_type=%s""", (leave_policy.name, leave_type_doc.name)) if annual_allocation and annual_allocation[0]: leaves_per_frequency = flt( annual_allocation[0][0]) / divide_by_frequency[ leave_type_doc.earned_leave_frequency] divide_by_frequency = {"Yearly": 12, "Quarterly": 4, "Monthly": 1} future_leaves = (number_of_months / divide_by_frequency[leave_type_doc.earned_leave_frequency] ) * leaves_per_frequency if leave_type_doc.rounding == "0.5": future_leaves = round(future_leaves * 2) / 2 else: future_leaves = round(future_leaves) projected_earned_leave_balance = current_balance + future_leaves return projected_earned_leave_balance
def grant_leave_alloc_for_employees(employee_records, leave_period, carry_forward=0): leave_allocations = [] existing_allocations_for = get_existing_allocations(list(employee_records.keys()), leave_period.name) leave_type_details = get_leave_type_details() count = 0 for employee in employee_records.keys(): if employee in existing_allocations_for: continue count +=1 leave_policy = get_employee_leave_policy(employee) if leave_policy: for leave_policy_detail in leave_policy.leave_policy_details: if not leave_type_details.get(leave_policy_detail.leave_type).is_lwp: leave_allocation = create_leave_allocation(employee, leave_policy_detail.leave_type, leave_policy_detail.annual_allocation, leave_type_details, leave_period, carry_forward, employee_records.get(employee)) leave_allocations.append(leave_allocation) frappe.db.commit() frappe.publish_progress(count*100/len(set(employee_records.keys()) - set(existing_allocations_for)), title = _("Allocating leaves...")) if leave_allocations: frappe.msgprint(_("Leaves has been granted sucessfully"))
def grant_leave_alloc_for_employees(employees, leave_period, carry_forward_leaves=0): leave_allocations = [] existing_allocations_for = get_existing_allocations(employees, leave_period.name) leave_type_details = get_leave_type_details() count=0 for employee in employees: if employee in existing_allocations_for: continue count +=1 leave_policy = get_employee_leave_policy(employee) if leave_policy: for leave_policy_detail in leave_policy.leave_policy_details: if not leave_type_details.get(leave_policy_detail.leave_type).is_lwp: leave_allocation = create_leave_allocation(employee, leave_policy_detail.leave_type, leave_policy_detail.annual_allocation, leave_type_details, leave_period, carry_forward_leaves) leave_allocations.append(leave_allocation) frappe.db.commit() frappe.publish_progress(count*100/len(set(employees) - set(existing_allocations_for)), title = _("Allocating leaves...")) if leave_allocations: frappe.msgprint(_("Leaves has been granted sucessfully"))
def allocate_earned_leaves_to_employees_who_joined_last_month(self, method): '''Allocate earned leaves to Employees who joined last month''' print("allocate_earned_leaves_to_employees_who_joined_last_month") e_leave_types = frappe.get_all("Leave Type", fields=[ "name", "max_leaves_allowed", "earned_leave_frequency", "rounding" ], filters={'is_earned_leave': 1}) today = getdate() divide_by_frequency = { "Yearly": 1, "Half-Yearly": 6, "Quarterly": 4, "Monthly": 12 } divide_by_frequency_per_day = { "Yearly": 365, "Half-Yearly": 183, "Quarterly": 90, "Monthly": 30 } for e_leave_type in e_leave_types: leave_allocations = frappe.db.sql( """select name, employee, from_date, to_date from `tabLeave Allocation` where %s between from_date and to_date and docstatus=1 and leave_type=%s""", (today, e_leave_type.name), as_dict=1) for allocation in leave_allocations: employee_date_of_joining = frappe.db.get_value( "Employee", allocation.employee, "date_of_joining") first_date_of_last_month = get_first_day(add_months(today, -1)) if employee_date_of_joining < today and employee_date_of_joining >= first_date_of_last_month: print("true") working_days = date_diff(add_days(today, -1), employee_date_of_joining) print(working_days) leave_policy = get_employee_leave_policy(allocation.employee) if not leave_policy: continue if not e_leave_type.earned_leave_frequency == "Monthly": if not check_frequency_hit( allocation.from_date, today, e_leave_type.earned_leave_frequency): continue annual_allocation = frappe.db.get_value( "Leave Policy Detail", filters={ 'parent': leave_policy.name, 'leave_type': e_leave_type.name }, fieldname=['annual_allocation']) if annual_allocation: earned_leaves = flt( annual_allocation) / divide_by_frequency[ e_leave_type.earned_leave_frequency] custom_earned_leaves = flt( annual_allocation * working_days) / divide_by_frequency_per_day[ e_leave_type.earned_leave_frequency] if e_leave_type.rounding == "0.5": earned_leaves = round(earned_leaves * 2) / 2 else: earned_leaves = round(earned_leaves) if e_leave_type.rounding == "0.5": custom_earned_leaves = round( custom_earned_leaves * 2) / 2 else: custom_earned_leaves = round(custom_earned_leaves) allocation = frappe.get_doc('Leave Allocation', allocation.name) new_allocation = flt( allocation.total_leaves_allocated) - flt( earned_leaves) + flt(custom_earned_leaves) if new_allocation > e_leave_type.max_leaves_allowed and e_leave_type.max_leaves_allowed > 0: new_allocation = e_leave_type.max_leaves_allowed if new_allocation == allocation.total_leaves_allocated: continue allocation.db_set("total_leaves_allocated", new_allocation, update_modified=False) frappe.db.sql("""DELETE FROM `tabLeave Ledger Entry` WHERE transaction_type = 'Leave Allocation' AND leave_type = %s""")
def allocate_earned_leaves_cron(*args, **kwargs): '''Allocate earned leaves to Employees''' e_leave_types = frappe.get_all("Leave Type", fields=[ "name", "max_leaves_allowed", "earned_leave_frequency", "rounding" ], filters={'is_earned_leave': 1}) today = getdate() divide_by_frequency = { "Yearly": 1, "Half-Yearly": 6, "Quarterly": 4, "Monthly": 12 } for e_leave_type in e_leave_types: print("lt") # import pdb;pdb.set_trace() leave_allocations = frappe.db.sql( """select name, employee, from_date, to_date from `tabLeave Allocation` where %s between from_date and to_date and docstatus=1 and leave_type=%s""", (today, e_leave_type.name), as_dict=1) for allocation in leave_allocations: print("la") # import pdb;pdb.set_trace() leave_policy = get_employee_leave_policy(allocation.employee) if not leave_policy: continue if not e_leave_type.earned_leave_frequency == "Monthly": if not check_frequency_hit( allocation.from_date, today, e_leave_type.earned_leave_frequency): continue annual_allocation = frappe.db.get_value( "Leave Policy Detail", filters={ 'parent': leave_policy.name, 'leave_type': e_leave_type.name }, fieldname=['annual_allocation']) if annual_allocation: print("aa") # import pdb;pdb.set_trace() earned_leaves = flt(annual_allocation) / divide_by_frequency[ e_leave_type.earned_leave_frequency] if e_leave_type.rounding == "0.5": earned_leaves = round(earned_leaves * 2) / 2 elif e_leave_type.rounding == "1": earned_leaves = round(earned_leaves) else: earned_leaves = earned_leaves l_allocation = frappe.get_doc('Leave Allocation', allocation.name) new_allocation = flt( l_allocation.total_leaves_allocated) + flt(earned_leaves) if new_allocation > e_leave_type.max_leaves_allowed and e_leave_type.max_leaves_allowed > 0: new_allocation = e_leave_type.max_leaves_allowed if new_allocation == l_allocation.total_leaves_allocated: continue l_allocation.db_set("total_leaves_allocated", new_allocation, update_modified=False) create_additional_leave_ledger_entry(l_allocation, earned_leaves, today)