public function getLeavesForMeAndSubordinates($req)
 {
     $map = json_decode('{"employee":["Employee","id","first_name+last_name"],"leave_type":["LeaveType","id","name"]}');
     $employee = $this->baseService->getElement('Employee', $this->getCurrentProfileId(), null, true);
     $employeeLeave = new EmployeeLeave();
     $startDate = date("Y-m-d H:i:s", $req->start);
     $endDate = date("Y-m-d H:i:s", $req->end);
     $list = $employeeLeave->Find("status in ('Approved','Pending') and ((date_start >= ? and date_start <= ? ) or (date_end >= ? and date_end <= ?))", array($startDate, $endDate, $startDate, $endDate));
     if (!$list) {
         LogManager::getInstance()->info($employeeLeave->ErrorMsg());
     }
     if (!empty($map)) {
         $list = $this->baseService->populateMapping($list, $map);
     }
     $data = array();
     $leaveType = new LeaveType();
     $leaveTypesTemp = $leaveType->Find("1=1");
     $leaveTypes = array();
     foreach ($leaveTypesTemp as $leaveType) {
         $leaveTypes[$leaveType->name] = $leaveType;
     }
     foreach ($list as $leave) {
         $data[] = $this->leaveToEvent($leave, $leaveTypes);
     }
     $holiday = new HoliDay();
     $holidays = $holiday->Find("1=1", array());
     foreach ($holidays as $holiday) {
         $data[] = $this->holidayToEvent($holiday);
     }
     echo json_encode($data);
     exit;
 }
 public function getUserLeaveTypes()
 {
     $ele = new LeaveType();
     $empLeaveGroupId = NULL;
     $employeeId = BaseService::getInstance()->getCurrentProfileId();
     $empLeaveGroup = new LeaveGroupEmployee();
     $empLeaveGroup->Load("employee = ?", array($employeeId));
     if ($empLeaveGroup->employee == $employeeId && !empty($empLeaveGroup->id)) {
         $empLeaveGroupId = $empLeaveGroup->leave_group;
     }
     if (empty($empLeaveGroupId)) {
         $list = $ele->Find('leave_group IS NULL', array());
     } else {
         $list = $ele->Find('leave_group IS NULL or leave_group = ?', array($empLeaveGroupId));
     }
     return $list;
 }
 private function getLeaveRuleOnly($employee, $leaveType)
 {
     $rule = null;
     $leaveRule = new LeaveRule();
     $leaveTypeObj = new LeaveType();
     $rules = $leaveRule->Find("employee = ? and leave_type = ?", array($employee->id, $leaveType));
     if (count($rules) > 0) {
         return $rules[0];
     }
     //Check whether this employee has a leave group
     $leaveGroupId = $this->getEmployeeLeaveGroup($employee->id);
     if (!empty($leaveGroupId)) {
         $rules = $leaveRule->Find("leave_group = ? and leave_type = ?", array($leaveGroupId, $leaveType));
         if (count($rules) > 0) {
             return $rules[0];
         }
         $rules = $leaveRule->Find("leave_group = ? and job_title = ? and employment_status = ? and leave_type = ? and employee is null", array($leaveGroupId, $employee->job_title, $employee->employment_status, $leaveType));
         if (count($rules) > 0) {
             return $rules[0];
         }
         $rules = $leaveRule->Find("leave_group = ? and job_title = ? and employment_status is null and leave_type = ? and employee is null", array($leaveGroupId, $employee->job_title, $leaveType));
         if (count($rules) > 0) {
             return $rules[0];
         }
         $rules = $leaveRule->Find("leave_group = ? and job_title is null and employment_status = ? and leave_type = ? and employee is null", array($leaveGroupId, $employee->employment_status, $leaveType));
         if (count($rules) > 0) {
             return $rules[0];
         }
         $rules = $leaveTypeObj->Find("leave_group = ? and id = ?", array($leaveGroupId, $leaveType));
         if (count($rules) > 0) {
             return $rules[0];
         }
     }
     $rules = $leaveRule->Find("job_title = ? and employment_status = ? and leave_type = ? and employee is null", array($employee->job_title, $employee->employment_status, $leaveType));
     if (count($rules) > 0) {
         return $rules[0];
     }
     $rules = $leaveRule->Find("job_title = ? and employment_status is null and leave_type = ? and employee is null", array($employee->job_title, $leaveType));
     if (count($rules) > 0) {
         return $rules[0];
     }
     $rules = $leaveRule->Find("job_title is null and employment_status = ? and leave_type = ? and employee is null", array($employee->employment_status, $leaveType));
     if (count($rules) > 0) {
         return $rules[0];
     }
     $rules = $leaveTypeObj->Find("id = ?", array($leaveType));
     if (count($rules) > 0) {
         return $rules[0];
     }
 }
	private function getLeaveRule($employee,$leaveType){
		$rule = null;
		$leaveRule = new LeaveRule();
		$leaveTypeObj = new LeaveType();
		$rules = $leaveRule->Find("employee = ? and leave_type = ?",array($employee->id,$leaveType));
		if(count($rules)>0){
			return $rules[0];
		}

		$rules = $leaveRule->Find("job_title = ? and employment_status = ? and leave_type = ? and employee is null",array($employee->job_title,$employee->employment_status,$leaveType));
		if(count($rules)>0){
			return $rules[0];
		}

		$rules = $leaveRule->Find("job_title = ? and employment_status is null and leave_type = ? and employee is null",array($employee->job_title,$leaveType));
		if(count($rules)>0){
			return $rules[0];
		}

		$rules = $leaveRule->Find("job_title is null and employment_status = ? and leave_type = ? and employee is null",array($employee->employment_status,$leaveType));
		if(count($rules)>0){
			return $rules[0];
		}

		$rules = $leaveTypeObj->Find("id = ?",array($leaveType));
		if(count($rules)>0){
			return $rules[0];
		}

	}
 public function getLeavesForMeAndSubordinates($req)
 {
     $req->start = gmdate("Y-m-d", $req->start);
     $req->end = gmdate("Y-m-d", $req->end);
     $shareCalendar = $this->baseService->settingsManager->getSetting("Leave: Share Calendar to Whole Company");
     $map = json_decode('{"employee":["Employee","id","first_name+last_name"],"leave_type":["LeaveType","id","name"]}');
     $employee = $this->baseService->getElement('Employee', $this->getCurrentProfileId(), null, true);
     if ($shareCalendar != "1") {
         $subordinate = new Employee();
         $subordinates = $subordinate->Find("supervisor = ?", array($employee->id));
         $subordinatesIds = $employee->id;
         foreach ($subordinates as $sub) {
             if ($subordinatesIds != "") {
                 $subordinatesIds .= ",";
             }
             $subordinatesIds .= $sub->id;
         }
         $employeeLeave = new EmployeeLeave();
         $startDate = date("Y-m-d H:i:s", $req->start);
         $endDate = date("Y-m-d H:i:s", $req->end);
         $list = $employeeLeave->Find("employee in (" . $subordinatesIds . ") and status in ('Approved','Pending') and ((date_start >= ? and date_start <= ? ) or (date_end >= ? and date_end <= ?))", array($startDate, $endDate, $startDate, $endDate));
     } else {
         $employeeLeave = new EmployeeLeave();
         $startDate = $req->start;
         $endDate = $req->end;
         $list = $employeeLeave->Find("status in ('Approved','Pending') and ((date_start >= ? and date_start <= ? ) or (date_end >= ? and date_end <= ?))", array($startDate, $endDate, $startDate, $endDate));
     }
     if (!$list) {
         LogManager::getInstance()->info($employeeLeave->ErrorMsg());
     }
     if (!empty($map)) {
         $list = $this->baseService->populateMapping($list, $map);
     }
     $leaveType = new LeaveType();
     $leaveTypesTemp = $leaveType->Find("1=1");
     $leaveTypes = array();
     foreach ($leaveTypesTemp as $leaveType) {
         $leaveTypes[$leaveType->name] = $leaveType;
     }
     $data = array();
     $mode = CalendarTools::getCalendarMode($req->start, $req->end);
     foreach ($list as $leave) {
         $tmpEvents = $this->leaveToEvents($leave, $leaveTypes);
         foreach ($tmpEvents as $event) {
             $data[] = $event;
         }
     }
     /*
             if($mode == CalendarTools::MODE_MONTH){
                 foreach($list as $leave){
                     $data[] = $this->leaveToEvent($leave, $leaveTypes);
                 }
             }else{
                 foreach($list as $leave){
                     $tmpEvents = $this->leaveToEvents($leave, $leaveTypes);
                     foreach($tmpEvents as $event){
                         $data[] =  $event;
                     }
                 }
             }
     */
     $holiday = new HoliDay();
     $holidays = $holiday->Find("1=1", array());
     foreach ($holidays as $holiday) {
         $data[] = $this->holidayToEvent($holiday);
     }
     echo json_encode($data);
     exit;
 }