def create_leave_plan_page(request): user = request.user employee = user.solitonuser.employee if request.POST: start_date = request.POST.get("start_date") end_date = request.POST.get("end_date") description = request.POST.get("description") try: new_leave_plan = LeavePlan.objects.create(employee=employee, start_date=start_date, end_date=end_date, description=description) receivers = get_hod_users(employee) create_notification( "New Leave Plan", f"A leave plan from {employee} pending approval", receivers) send_leave_plan_email(receivers, new_leave_plan, domain=None) except IntegrityError: return HttpResponseRedirect(reverse(create_leave_plan_page)) context = { "leave_plans": "", "leave_page": "active", "recent_leave_plans": get_recent_leave_plans(5, employee), } return render(request, "leave/create_leave_plan.html", context)
def approve_leave(request): if request.method == "POST": user = request.user application_id = request.POST.get("application_id") comment = request.POST.get("comment") leave_application = LeaveApplication.objects.get(pk=application_id) employee = leave_application.employee l_type = leave_application.leave_type n_days = leave_application.no_of_days leave_record = Leave_Records.objects. \ get(employee=employee, leave_year=datetime.date.today().year) if user.is_supervisor: leave_application.supervisor = user.solitonuser.employee leave_application.supervisor_status = "Approved" leave_application.supervisor_comment = request.POST.get("comment") leave_application.save() hods = get_hod_users(employee) send_leave_application_email(hods, leave_application) elif user.is_hod: leave_application.hod = user.solitonuser.employee leave_application.hod_status = "Approved" leave_application.hod_comment = request.POST.get("comment") leave_application.save() hrs = get_hr_users(employee) send_leave_application_email(hrs, leave_application) elif user.is_hr: curr_balance = int(leave_record.balance) total_applied = int(leave_record.leave_applied) total_taken = int(leave_record.total_taken) if l_type.leave_type == "Annual": new_balance = int(curr_balance) - int(n_days) total_applied += 1 total_taken += int(n_days) else: new_balance = curr_balance leave_application.hr = user.solitonuser.employee leave_application.hr_status = "Approved" leave_application.hr_comment = request.POST.get("comment") leave_application.balance = new_balance leave_application.overall_status = "Approved" leave_application.save() Leave_Records.objects.filter( employee=employee, leave_year=datetime.date.today().year).update( leave_applied=total_applied, total_taken=total_taken, balance=new_balance ) send_leave_response_email(leave_application) else: messages.warning(request, 'Leave Approval Failed') return JsonResponse({'success': True, 'redirect': "leave_dashboard_page"}) messages.success(request, 'Leave Approved Successfully') return JsonResponse({'success': True, 'redirect': "leave_dashboard_page"})
def apply_leave(request): if request.method == "POST": user = request.user employee = user.solitonuser.employee organisationdetail = get_organisationdetail(user) department = organisationdetail.department team = organisationdetail.team leave_record = get_leave_record(employee) leave_type = get_leave_type(request.POST["ltype"]) start_date = request.POST["s_date"] end_date = request.POST["e_date"] days_applied = int(request.POST["no_days"]) leave_type_days = leave_type.leave_days curr_balance = 0 if days_applied <= leave_type_days: new_balance = 0 if leave_type.leave_type == "Annual": curr_balance = leave_record.balance new_balance = curr_balance - days_applied if new_balance >= 0: if user.is_supervisor: leave_application = LeaveApplication( employee=employee, leave_type=leave_type, start_date=start_date, end_date=end_date, no_of_days=days_applied, balance=curr_balance, department=department, team=team, supervisor=employee, supervisor_status="Approved", ) leave_application.save() approvers = get_hod_users(employee) send_leave_application_email(approvers, leave_application) create_notification("Leave", f"New Leave Request from {employee.first_name}", approvers) elif user.is_hod: leave_application = LeaveApplication( employee=employee, leave_type=leave_type, start_date=start_date, end_date=end_date, no_of_days=days_applied, balance=curr_balance, department=department, team=team, supervisor_status="Approved", hod=employee, hod_status="Approved" ) leave_application.save() approvers = get_hr_users() send_leave_application_email(approvers, leave_application) create_notification("Leave", f"New Leave Request from {employee.first_name}", approvers) else: leave_application = LeaveApplication( employee=employee, leave_type=leave_type, start_date=start_date, end_date=end_date, no_of_days=days_applied, balance=curr_balance, department=department, team=team ) leave_application.save() approvers = get_supervisor_users(employee) send_leave_application_email(approvers, leave_application) create_notification("Leave", f"New Leave Request from {employee.first_name}", approvers) messages.success(request, 'Leave Request Sent Successfully') return redirect('apply_leave_page') else: messages.warning(request, f'You have insufficient {leave_type} leave Balance {curr_balance}') return redirect('apply_leave_page') else: messages.warning(request, f'You cannot Request({days_applied}) for more than the\ {leave_type.leave_type} leave days ({leave_type.leave_days})') return render(request, "leave/leave.html")