public function addLeave($req)
 {
     $employee = $this->baseService->getElement('Employee', $this->getCurrentEmployeeId());
     $rule = $this->getLeaveRule($employee, $req->leave_type);
     if ($rule->employee_can_apply == "No") {
         return new IceResponse(IceResponse::ERROR, "You are not allowed to apply for this type of leaves");
     }
     //Find Current leave period
     $leaveCounts = array();
     $currentLeavePeriod = $this->getCurrentLeavePeriod();
     if (empty($currentLeavePeriod)) {
         return new IceResponse(IceResponse::ERROR, "Leave period is not defined");
     }
     //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 el.leave_type = ? and ld.leave_date = ? and ld.leave_type = ? and el.status <> 'Rejected'";
         $rs = $this->baseService->getDB()->Execute($sql, array($employee->id, $req->leave_type, 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;
     $ok = $employeeLeave->Save();
     if (!$ok) {
         error_log($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);
     }
     return new IceResponse(IceResponse::SUCCESS, $employeeLeave);
 }
 public function cancelLeave($req)
 {
     $employee = $this->baseService->getElement('Employee', $this->getCurrentProfileId(), null, true);
     $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' && $this->getCurrentProfileId() != $employeeLeave->employee) {
         return new IceResponse(IceResponse::ERROR, "Only an admin or owner of the leave can do this");
     }
     if ($employeeLeave->status != 'Approved') {
         return new IceResponse(IceResponse::ERROR, "Only an approved leave can be cancelled");
     }
     $employeeLeave->status = 'Cancellation Requested';
     $ok = $employeeLeave->Save();
     if (!$ok) {
         LogManager::getInstance()->error("Error occured while cancelling the leave:" . $employeeLeave->ErrorMsg());
         return new IceResponse(IceResponse::ERROR, "Error occured while cancelling the leave. Please contact admin.");
     }
     $employeeLeaveLog = new EmployeeLeaveLog();
     $employeeLeaveLog->employee_leave = $employeeLeave->id;
     $employeeLeaveLog->user_id = $this->baseService->getCurrentUser()->id;
     $employeeLeaveLog->status_from = 'Approved';
     $employeeLeaveLog->status_to = $employeeLeave->status;
     $employeeLeaveLog->created = date("Y-m-d H:i:s");
     $employeeLeaveLog->data = "Leave cancellation request sent";
     $ok = $employeeLeaveLog->Save();
     if (!$ok) {
         LogManager::getInstance()->info($employeeLeaveLog->ErrorMsg());
     }
     if (!empty($this->emailSender)) {
         $leavesEmailSender = new LeavesEmailSender($this->emailSender, $this);
         $leavesEmailSender->sendLeaveApplicationEmail($employee, true);
     }
     $this->baseService->audit(IceConstants::AUDIT_ACTION, "Leave cancellation \\ start:" . $employeeLeave->date_start . "\\ end:" . $employeeLeave->date_end);
     $notificationMsg = $employee->first_name . " " . $employee->last_name . " cancelled a leave. Visit leave module to approve";
     $this->baseService->notificationManager->addNotification($employee->supervisor, $notificationMsg, '{"type":"url","url":"g=modules&n=leaves&m=module_Leaves#tabSubEmployeeLeaveCancel"}', IceConstants::NOTIFICATION_LEAVE);
     return new IceResponse(IceResponse::SUCCESS, $employeeLeave);
 }
Esempio n. 3
0
	public function addLeave($req){
		$employee = $this->baseService->getElement('Employee',$this->getCurrentProfileId(),null,true);
		$rule = $this->getLeaveRule($employee, $req->leave_type);
		
		
		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");
			}
		}


		//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();
		}
		
		//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);
	}