def apply_for_overtime_page(request): applicant = request.user.solitonuser.employee if request.POST: start_time = request.POST.get('start_time') end_time = request.POST.get('end_time') description = request.POST.get('description') if not is_duration_valid(start_time, end_time): messages.error(request, "Duration for the overtime application is not valid") return HttpResponseRedirect(reverse("apply_for_overtime_page")) overtime_application = OvertimeApplication.objects.create( start_time=start_time, end_time=end_time, description=description, applicant=applicant ) approver = get_supervisor_user(applicant) message = "You need to approve/reject overtime application from {}".format(applicant) create_notification("Overtime", message, [approver]) approver = get_supervisor_user(applicant) send_overtime_application_mail([approver], overtime_application) messages.success(request, "You have successfully submitted your overtime application") return HttpResponseRedirect(reverse('apply_for_overtime_page')) recent_applications = get_recent_overtime_applications(limit=5, applicant=applicant) context = { "overtime_page": "active", "recent_applications": recent_applications } return render(request, 'overtime/apply_for_overtime.html', context)
def ceo_reject_plan(overtime_plan): overtime_plan.cfo_approval = 'Rejected' overtime_plan.status = "Rejected" overtime_plan.save() receivers = [overtime_plan.applicant] create_notification("Overtime Plan Rejected", "Your overtime plan has been rejected", receivers) return overtime_plan
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 create_overtime_plan(request): hod = request.user.solitonuser.employee overtime_plan = OvertimePlan.objects.create(applicant=hod) receivers = get_ceo_users() create_notification("Overtime Plan", f"{hod}'s overtime plan pending approval", receivers) return HttpResponseRedirect(reverse(create_overtime_plan_page))
def reject_leave_plan(request, id): leave_plan = get_leave_plan(id=id) leave_plan.approval_status = "Rejected" leave_plan.save() create_notification("Leave Plan Rejected", f"Your leave plan has been rejected", [leave_plan.employee]) messages.warning(request, f'Leave plan rejected') return HttpResponseRedirect(reverse(approve_leave_plan_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")