Example #1
0
	public function changeLeaveStatus($req){
		
		//$employee = $this->baseService->getElement('Employee',$this->getCurrentEmployeeId());
		
		
		$employeeLeave = new EmployeeLeave();
		$employeeLeave->Load("id = ?",array($req->id));
		if($employeeLeave->id != $req->id){
			return new IceResponse(IceResponse::ERROR,"Leave not found");
		}
		
		if($this->user->user_level != 'Admin'){
			return new IceResponse(IceResponse::ERROR,"Only an admin can do this");	
		}
		
		$oldLeaveStatus = $employeeLeave->status;
		$employeeLeave->status = $req->status;
		$ok = $employeeLeave->Save();
		if(!$ok){
			LogManager::getInstance()->info($employeeLeave->ErrorMsg());
			return new IceResponse(IceResponse::ERROR,"Error occured while saving leave infomation. Please contact admin");
		}
		
		$employeeLeaveLog = new EmployeeLeaveLog();
		$employeeLeaveLog->employee_leave = $employeeLeave->id;
		$employeeLeaveLog->user_id = $this->baseService->getCurrentUser()->id;
		$employeeLeaveLog->status_from = $oldLeaveStatus;
		$employeeLeaveLog->status_to = $employeeLeave->status;
		$employeeLeaveLog->created = date("Y-m-d H:i:s");
		$employeeLeaveLog->data = isset($req->reason)?$req->reason:"";
		$ok = $employeeLeaveLog->Save();
		if(!$ok){
			LogManager::getInstance()->info($employeeLeaveLog->ErrorMsg());
		}
		
		$employee = $this->getEmployeeById($employeeLeave->employee);
		
		if($oldLeaveStatus != $employeeLeave->status){
			$this->sendLeaveStatusChangedEmail($employee, $employeeLeave);
		}
		
		$this->baseService->audit(IceConstants::AUDIT_ACTION, "Leave status changed \ from:".$oldLeaveStatus."\ to:".$employeeLeave->status." \ id:".$employeeLeave->id);
		
		$currentEmpId = $this->getCurrentProfileId();
		
		if(!empty($currentEmpId)){
			$employee = $this->baseService->getElement('Employee',$currentEmpId);
			
			if($employeeLeave->status != "Pending"){
				$notificationMsg = "Your leave has been $employeeLeave->status by ".$employee->first_name." ".$employee->last_name;
				if(!empty($req->reason)){
					$notificationMsg.=" (Note:".$req->reason.")";
				}
			}
			
			$this->baseService->notificationManager->addNotification($employeeLeave->employee,$notificationMsg,'{"type":"url","url":"g=modules&n=leaves&m=module_Leaves#tabEmployeeLeaveApproved"}',IceConstants::NOTIFICATION_LEAVE);
			
		}
		
		
		return new IceResponse(IceResponse::SUCCESS,"");
	}
 public function changeLeaveStatus($req)
 {
     $employee = $this->baseService->getElement('Employee', $this->getCurrentProfileId(), null, true);
     $subordinate = new Employee();
     $subordinates = $subordinate->Find("supervisor = ?", array($employee->id));
     $subordinatesIds = array();
     foreach ($subordinates as $sub) {
         $subordinatesIds[] = $sub->id;
     }
     $employeeLeave = new EmployeeLeave();
     $employeeLeave->Load("id = ?", array($req->id));
     if ($employeeLeave->id != $req->id) {
         return new IceResponse(IceResponse::ERROR, "Leave not found");
     }
     if (!in_array($employeeLeave->employee, $subordinatesIds) && $this->user->user_level != 'Admin') {
         return new IceResponse(IceResponse::ERROR, "This leave does not belong to any of your subordinates");
     }
     $oldLeaveStatus = $employeeLeave->status;
     $employeeLeave->status = $req->status;
     if ($oldLeaveStatus == $req->status) {
         return new IceResponse(IceResponse::SUCCESS, "");
     }
     $ok = $employeeLeave->Save();
     if (!$ok) {
         LogManager::getInstance()->info($employeeLeave->ErrorMsg());
         return new IceResponse(IceResponse::ERROR, "Error occured while saving leave infomation. Please contact admin");
     }
     $employeeLeaveLog = new EmployeeLeaveLog();
     $employeeLeaveLog->employee_leave = $employeeLeave->id;
     $employeeLeaveLog->user_id = $this->baseService->getCurrentUser()->id;
     $employeeLeaveLog->status_from = $oldLeaveStatus;
     $employeeLeaveLog->status_to = $employeeLeave->status;
     $employeeLeaveLog->created = date("Y-m-d H:i:s");
     $employeeLeaveLog->data = isset($req->reason) ? $req->reason : "";
     $ok = $employeeLeaveLog->Save();
     if (!$ok) {
         LogManager::getInstance()->info($employeeLeaveLog->ErrorMsg());
     }
     if (!empty($this->emailSender) && $oldLeaveStatus != $employeeLeave->status) {
         $leavesEmailSender = new LeavesEmailSender($this->emailSender, $this);
         $leavesEmailSender->sendLeaveStatusChangedEmail($employee, $employeeLeave);
     }
     $this->baseService->audit(IceConstants::AUDIT_ACTION, "Leave status changed \\ from:" . $oldLeaveStatus . "\\ to:" . $employeeLeave->status . " \\ id:" . $employeeLeave->id);
     if ($employeeLeave->status != "Pending") {
         $notificationMsg = "Your leave has been {$employeeLeave->status} by " . $employee->first_name . " " . $employee->last_name;
         if (!empty($req->reason)) {
             $notificationMsg .= " (Note:" . $req->reason . ")";
         }
     }
     $this->baseService->notificationManager->addNotification($employeeLeave->employee, $notificationMsg, '{"type":"url","url":"g=modules&n=leaves&m=module_Leaves#tabEmployeeLeaveApproved"}', IceConstants::NOTIFICATION_LEAVE);
     return new IceResponse(IceResponse::SUCCESS, "");
 }
 public function getLeaveDaysReadonly($req)
 {
     $leaveId = $req->leave_id;
     $leaveLogs = array();
     $employeeLeave = new EmployeeLeave();
     $employeeLeave->Load("id = ?", array($leaveId));
     $currentLeavePeriodResp = $this->getCurrentLeavePeriod($employeeLeave->date_start, $employeeLeave->date_end);
     if ($currentLeavePeriodResp->getStatus() != IceResponse::SUCCESS) {
         return new IceResponse(IceResponse::ERROR, $currentLeavePeriodResp->getData());
     } else {
         $currentLeavePeriod = $currentLeavePeriodResp->getData();
     }
     $employee = $this->baseService->getElement('Employee', $employeeLeave->employee, null, true);
     $rule = $this->getLeaveRule($employee, $employeeLeave->leave_type, $currentLeavePeriod);
     $currentLeavePeriodResp = $this->getCurrentLeavePeriod($employeeLeave->date_start, $employeeLeave->date_end);
     if ($currentLeavePeriodResp->getStatus() != IceResponse::SUCCESS) {
         return new IceResponse(IceResponse::ERROR, $currentLeavePeriodResp->getData());
     } else {
         $currentLeavePeriod = $currentLeavePeriodResp->getData();
     }
     $leaveMatrix = $this->getAvailableLeaveMatrixForEmployeeLeaveType($employee, $currentLeavePeriod, $employeeLeave->leave_type);
     $leaves = array();
     $leaves['totalLeaves'] = floatval($leaveMatrix[0]);
     $leaves['pendingLeaves'] = floatval($leaveMatrix[1]);
     $leaves['approvedLeaves'] = floatval($leaveMatrix[2]);
     $leaves['rejectedLeaves'] = floatval($leaveMatrix[3]);
     $leaves['cancelRequestedLeaves'] = floatval($leaveMatrix[5]);
     $leaves['availableLeaves'] = $leaves['totalLeaves'] - $leaves['pendingLeaves'] - $leaves['approvedLeaves'] - $leaves['cancelRequestedLeaves'];
     $leaves['attachment'] = $employeeLeave->attachment;
     $employeeLeaveDay = new EmployeeLeaveDay();
     $days = $employeeLeaveDay->Find("employee_leave = ?", array($leaveId));
     $employeeLeaveLog = new EmployeeLeaveLog();
     $logsTemp = $employeeLeaveLog->Find("employee_leave = ? order by created", array($leaveId));
     foreach ($logsTemp as $empLeaveLog) {
         $t = array();
         $t['time'] = $empLeaveLog->created;
         $t['status_from'] = $empLeaveLog->status_from;
         $t['status_to'] = $empLeaveLog->status_to;
         $t['time'] = $empLeaveLog->created;
         $userName = null;
         if (!empty($empLeaveLog->user_id)) {
             $lgUser = new User();
             $lgUser->Load("id = ?", array($empLeaveLog->user_id));
             if ($lgUser->id == $empLeaveLog->user_id) {
                 if (!empty($lgUser->employee)) {
                     $lgEmployee = new Employee();
                     $lgEmployee->Load("id = ?", array($lgUser->employee));
                     $userName = $lgEmployee->first_name . " " . $lgEmployee->last_name;
                 } else {
                     $userName = $lgUser->userName;
                 }
             }
         }
         if (!empty($userName)) {
             $t['note'] = $empLeaveLog->data . " (by: " . $userName . ")";
         } else {
             $t['note'] = $empLeaveLog->data;
         }
         $leaveLogs[] = $t;
     }
     return new IceResponse(IceResponse::SUCCESS, array($days, $leaves, $leaveId, $employeeLeave, $leaveLogs));
 }