public function addLeave($req) { if ($this->hasAllRequiredDoucments() == false) { return new IceResponse(IceResponse::ERROR, "You are missing some required documents you can't apply for leaves"); } $leaveTypeTemp = new LeaveType(); $allowedLeaveTypes = $leaveTypeTemp->getUserLeaveTypes(); $allowed = false; foreach ($allowedLeaveTypes as $leaveTypeTemp) { if ($leaveTypeTemp->id == $req->leave_type) { $allowed = true; } } $allowed = true; if (!$allowed) { return new IceResponse(IceResponse::ERROR, "You are not allowed to apply for this type of leaves"); } //Find Current leave period $leaveCounts = array(); $currentLeavePeriodResp = $this->getCurrentLeavePeriod($req->date_start, $req->date_end); if ($currentLeavePeriodResp->getStatus() != IceResponse::SUCCESS) { return new IceResponse(IceResponse::ERROR, $currentLeavePeriodResp->getData()); } else { $currentLeavePeriod = $currentLeavePeriodResp->getData(); } $employee = $this->baseService->getElement('Employee', $this->getCurrentProfileId(), null, true); $rule = $this->getLeaveRule($employee, $req->leave_type, $currentLeavePeriod); if ($this->user->user_level == 'Admin' && $this->getCurrentProfileId() != $this->user->employee) { //Admin is updating information for an employee if ($rule->supervisor_leave_assign == "No") { return new IceResponse(IceResponse::ERROR, "You are not allowed to assign this type of leaves as admin"); } } else { if ($rule->employee_can_apply == "No") { return new IceResponse(IceResponse::ERROR, "You are not allowed to apply for this type of leaves"); } } //Validate leave days $days = json_decode($req->days); foreach ($days as $day => $type) { $sql = "select ld.id as leaveId from EmployeeLeaveDays ld join EmployeeLeaves el on ld.employee_leave = el.id where el.employee = ? and ld.leave_date = ? and ld.leave_type = ? and el.status <> 'Rejected'"; $rs = $this->baseService->getDB()->Execute($sql, array($employee->id, date("Y-m-d", strtotime($day)), $type)); $counter = 0; foreach ($rs as $k => $v) { $counter++; } if ($counter > 0) { return new IceResponse(IceResponse::ERROR, "This leave is overlapping with another leave you have already applied"); } } //Adding Employee Leave $employeeLeave = new EmployeeLeave(); $employeeLeave->employee = $employee->id; $employeeLeave->leave_type = $req->leave_type; $employeeLeave->leave_period = $currentLeavePeriod->id; $employeeLeave->date_start = $req->date_start; $employeeLeave->date_end = $req->date_end; $employeeLeave->details = $req->details; $employeeLeave->status = "Pending"; $employeeLeave->details = $req->details; $employeeLeave->attachment = isset($req->attachment) ? $req->attachment : ""; $ok = $employeeLeave->Save(); if (!$ok) { LogManager::getInstance()->info($employeeLeave->ErrorMsg()); return new IceResponse(IceResponse::ERROR, "Error occured while applying leave."); } $days = json_decode($req->days); foreach ($days as $day => $type) { $employeeLeaveDay = new EmployeeLeaveDay(); $employeeLeaveDay->employee_leave = $employeeLeave->id; $employeeLeaveDay->leave_date = date("Y-m-d", strtotime($day)); $employeeLeaveDay->leave_type = $type; $employeeLeaveDay->Save(); } if (!empty($this->emailSender)) { $leavesEmailSender = new LeavesEmailSender($this->emailSender, $this); $leavesEmailSender->sendLeaveApplicationEmail($employee); $leavesEmailSender->sendLeaveApplicationSubmittedEmail($employee); } //$this->baseService->audit(IceConstants::AUDIT_ACTION, "Leave applied \ start:".$employeeLeave->date_start."\ end:".$employeeLeave->date_end); $notificationMsg = $employee->first_name . " " . $employee->last_name . " applied for a leave. Visit leave module to approve or reject"; $this->baseService->notificationManager->addNotification($employee->supervisor, $notificationMsg, '{"type":"url","url":"g=modules&n=leaves&m=module_Leaves#tabSubEmployeeLeaveAll"}', IceConstants::NOTIFICATION_LEAVE); return new IceResponse(IceResponse::SUCCESS, $employeeLeave); }