protected function getMatchingEmployees($parameters)
 {
     $parameterHolder = new EmployeeSearchParameterHolder();
     $filters = array('location' => $parameters['location'], 'sub_unit' => $parameters['subunit']);
     $fromDate = isset($parameters['fd']) ? $parameters['fd'] : null;
     $toDate = isset($parameters['td']) ? $parameters['td'] : null;
     $leaveType = isset($parameters['lt']) ? $parameters['lt'] : null;
     $newValue = isset($parameters['ent']) ? $parameters['ent'] : null;
     $offset = isset($parameters['offset']) ? $parameters['offset'] : 0;
     $pageSize = sfConfig::get('app_items_per_page');
     $parameterHolder->setFilters($filters);
     $parameterHolder->setOffset($offset);
     $parameterHolder->setLimit($pageSize);
     $parameterHolder->setReturnType(EmployeeSearchParameterHolder::RETURN_TYPE_ARRAY);
     $employees = $this->getEmployeeService()->searchEmployees($parameterHolder);
     $names = array();
     foreach ($employees as $employee) {
         $leaveEntitlementSearchParameterHolder = new LeaveEntitlementSearchParameterHolder();
         $leaveEntitlementSearchParameterHolder->setEmpNumber($employee['empNumber']);
         $leaveEntitlementSearchParameterHolder->setFromDate($fromDate);
         $leaveEntitlementSearchParameterHolder->setLeaveTypeId($leaveType);
         $leaveEntitlementSearchParameterHolder->setToDate($toDate);
         $entitlementList = $this->getEntitlementService()->searchLeaveEntitlements($leaveEntitlementSearchParameterHolder);
         $oldValue = 0;
         if (count($entitlementList) > 0) {
             $existingLeaveEntitlement = $entitlementList->getFirst();
             $oldValue = $existingLeaveEntitlement->getNoOfDays();
         }
         $names[] = array($employee['firstName'] . ' ' . $employee['middleName'] . ' ' . $employee['lastName'], $oldValue, $newValue + $oldValue);
     }
     $data = array('offset' => $offset, 'pageSize' => $pageSize, 'data' => $names);
     return $data;
 }
 protected function getEmployeeEntitlement($parameters)
 {
     $leaveEntitlementSearchParameterHolder = new LeaveEntitlementSearchParameterHolder();
     $leaveEntitlementSearchParameterHolder->setEmpNumber($parameters['empId']);
     $leaveEntitlementSearchParameterHolder->setFromDate($parameters['fd']);
     $leaveEntitlementSearchParameterHolder->setLeaveTypeId($parameters['lt']);
     $leaveEntitlementSearchParameterHolder->setToDate($parameters['td']);
     $entitlementList = $this->getEntitlementService()->searchLeaveEntitlements($leaveEntitlementSearchParameterHolder);
     $oldValue = 0;
     $newValue = $parameters['ent'];
     if (count($entitlementList) > 0) {
         $existingLeaveEntitlement = $entitlementList->getFirst();
         $oldValue = $existingLeaveEntitlement->getNoOfDays();
     }
     return array($oldValue, $newValue + $oldValue);
 }
 protected function getEmployeeEntitlement($parameters)
 {
     $localizationService = new LocalizationService();
     $inputDatePattern = sfContext::getInstance()->getUser()->getDateFormat();
     $fromDate = $localizationService->convertPHPFormatDateToISOFormatDate($inputDatePattern, $parameters['fd']);
     $toDate = $localizationService->convertPHPFormatDateToISOFormatDate($inputDatePattern, $parameters['td']);
     $leaveEntitlementSearchParameterHolder = new LeaveEntitlementSearchParameterHolder();
     $leaveEntitlementSearchParameterHolder->setEmpNumber($parameters['empId']);
     $leaveEntitlementSearchParameterHolder->setFromDate($fromDate);
     $leaveEntitlementSearchParameterHolder->setLeaveTypeId($parameters['lt']);
     $leaveEntitlementSearchParameterHolder->setToDate($toDate);
     $entitlementList = $this->getEntitlementService()->searchLeaveEntitlements($leaveEntitlementSearchParameterHolder);
     $oldValue = 0;
     $newValue = $parameters['ent'];
     foreach ($entitlementList as $existingEntitlement) {
         $oldValue += $existingEntitlement->getNoOfDays();
     }
     return array($oldValue, $newValue + $oldValue);
 }
 public function bulkAssignLeaveEntitlements($employeeNumbers, LeaveEntitlement $leaveEntitlement)
 {
     $conn = Doctrine_Manager::connection();
     $conn->beginTransaction();
     $pdo = Doctrine_Manager::getInstance()->getCurrentConnection()->getDbh();
     try {
         $allEntitlements = array();
         $updateEmpList = array();
         $updateEntitlementIdList = array();
         $savedCount = 0;
         $leaveTypeId = $leaveEntitlement->getLeaveTypeId();
         $fromDate = $leaveEntitlement->getFromDate();
         $toDate = $leaveEntitlement->getToDate();
         $leaveEntitlementSearchParameterHolder = new LeaveEntitlementSearchParameterHolder();
         $leaveEntitlementSearchParameterHolder->setFromDate($fromDate);
         $leaveEntitlementSearchParameterHolder->setLeaveTypeId($leaveTypeId);
         $leaveEntitlementSearchParameterHolder->setToDate($toDate);
         $leaveEntitlementSearchParameterHolder->setEmpIdList($employeeNumbers);
         $leaveEntitlementSearchParameterHolder->setHydrationMode(Doctrine::HYDRATE_ARRAY);
         $entitlementList = $this->searchLeaveEntitlements($leaveEntitlementSearchParameterHolder);
         if (count($entitlementList) > 0) {
             foreach ($entitlementList as $updateEntitlement) {
                 $empNumber = $updateEntitlement['emp_number'];
                 if (!isset($allEntitlements[$empNumber])) {
                     $entitlement = new LeaveEntitlement();
                     $noOfDays = $leaveEntitlement->getNoOfDays();
                     $entitlement->setEmpNumber($empNumber);
                     $entitlement->setLeaveTypeId($leaveTypeId);
                     $entitlement->setCreditedDate($leaveEntitlement->getCreditedDate());
                     $entitlement->setCreatedById($leaveEntitlement->getCreatedById());
                     $entitlement->setCreatedByName($leaveEntitlement->getCreatedByName());
                     $entitlement->setEntitlementType($leaveEntitlement->getEntitlementType());
                     $entitlement->setDeleted(0);
                     $entitlement->setNoOfDays($leaveEntitlement->getNoOfDays());
                     $entitlement->setFromDate($fromDate);
                     $entitlement->setToDate($toDate);
                     $entitlement->setId($updateEntitlement['id']);
                     $allEntitlements[$empNumber] = $entitlement;
                     $updateEmpList[] = $updateEntitlement['emp_number'];
                     $updateEntitlementIdList[] = $updateEntitlement['id'];
                     $savedCount++;
                 }
             }
             $updateQuery = sprintf(" UPDATE ohrm_leave_entitlement SET no_of_days=no_of_days+ %d WHERE id IN (%s)", $leaveEntitlement->getNoOfDays(), implode(',', $updateEntitlementIdList));
             $updateStmt = $pdo->prepare($updateQuery);
             $updateStmt->execute();
         }
         $newEmployeeList = array_diff($employeeNumbers, $updateEmpList);
         if (count($newEmployeeList) > 0) {
             $query = " INSERT INTO ohrm_leave_entitlement(`emp_number`,`leave_type_id`,`from_date`,`to_date`,`no_of_days`,`entitlement_type`) VALUES " . "(?, ?, ?, ?, ?, ?)";
             $stmt = $pdo->prepare($query);
             foreach ($newEmployeeList as $empNumber) {
                 if (!isset($allEntitlements[$empNumber])) {
                     $entitlement = new LeaveEntitlement();
                     $noOfDays = $leaveEntitlement->getNoOfDays();
                     $entitlement->setEmpNumber($empNumber);
                     $entitlement->setLeaveTypeId($leaveEntitlement->getLeaveTypeId());
                     $entitlement->setCreditedDate($leaveEntitlement->getCreditedDate());
                     $entitlement->setCreatedById($leaveEntitlement->getCreatedById());
                     $entitlement->setCreatedByName($leaveEntitlement->getCreatedByName());
                     $entitlement->setEntitlementType($leaveEntitlement->getEntitlementType());
                     $entitlement->setDeleted(0);
                     $entitlement->setNoOfDays($noOfDays);
                     $entitlement->setFromDate($fromDate);
                     $entitlement->setToDate($toDate);
                     $params = array($empNumber, $leaveEntitlement->getLeaveTypeId(), $fromDate, $toDate, $noOfDays, LeaveEntitlement::ENTITLEMENT_TYPE_ADD);
                     $stmt->execute($params);
                     $entitlement->setId($pdo->lastInsertId());
                     $allEntitlements[$empNumber] = $entitlement;
                     $savedCount++;
                 }
             }
         }
         // If leave period is forced, we can bulk assign at once, because from and to date of
         // all leave entitlements will be the same
         $leavePeriodStatus = LeavePeriodService::getLeavePeriodStatus();
         if ($leavePeriodStatus == LeavePeriodService::LEAVE_PERIOD_STATUS_FORCED) {
             $this->bulkLinkLeaveToUnusedLeaveEntitlements($allEntitlements, $leaveTypeId, $fromDate, $toDate);
         } else {
             foreach ($allEntitlements as $leaveEntitlement) {
                 $this->linkLeaveToUnusedLeaveEntitlement($leaveEntitlement);
             }
         }
         $conn->commit();
         return $savedCount;
     } catch (Exception $e) {
         $conn->rollback();
         throw new DaoException($e->getMessage(), 0, $e);
     }
 }
 public function testSearchLeaveEntitlementsSamePeriod()
 {
     Doctrine_Query::create()->delete()->from('LeaveEntitlement')->execute();
     $leaveEntitlements = array(array('id' => 1, 'leave_type_id' => 1, 'emp_number' => 1, 'no_of_days' => 3, 'from_date' => '2013-01-01 00:00:00', 'to_date' => '2013-12-31 00:00:00', 'entitlement_type' => 1, 'deleted' => 0), array('id' => 2, 'leave_type_id' => 1, 'emp_number' => 2, 'no_of_days' => 3, 'from_date' => '2013-01-01 00:00:00', 'to_date' => '2013-12-31 00:00:00', 'entitlement_type' => 1, 'deleted' => 0));
     foreach ($leaveEntitlements as $entitlement) {
         $leaveEntitlement = new LeaveEntitlement();
         $leaveEntitlement->fromArray($entitlement);
         $leaveEntitlement->save();
     }
     $fromDate = '2013-01-01';
     $toDate = '2013-12-31';
     $employeeNumbers = array(1, 2);
     $leaveTypeId = 1;
     $leaveEntitlementSearchParameterHolder = new LeaveEntitlementSearchParameterHolder();
     $leaveEntitlementSearchParameterHolder->setFromDate($fromDate);
     $leaveEntitlementSearchParameterHolder->setLeaveTypeId($leaveTypeId);
     $leaveEntitlementSearchParameterHolder->setToDate($toDate);
     $leaveEntitlementSearchParameterHolder->setEmpIdList($employeeNumbers);
     $leaveEntitlementSearchParameterHolder->setHydrationMode(Doctrine::HYDRATE_ARRAY);
     $entitlementList = $this->dao->searchLeaveEntitlements($leaveEntitlementSearchParameterHolder);
     $this->assertEquals(2, count($entitlementList));
 }
 public function deleteLeaveEntitlements($ids)
 {
     $deleted = 0;
     $allDeleted = true;
     $avaliableToDeleteIds = array();
     $leaveEntitlementSearchParameterHolder = new LeaveEntitlementSearchParameterHolder();
     $leaveEntitlementSearchParameterHolder->setIdList($ids);
     $entitlementList = $this->searchLeaveEntitlements($leaveEntitlementSearchParameterHolder);
     foreach ($entitlementList as $entitlement) {
         if ($entitlement->getDaysUsed() > 0) {
             $allDeleted = false;
         } else {
             $avaliableToDeleteIds[] = $entitlement->getId();
         }
     }
     if (count($avaliableToDeleteIds) > 0) {
         $deleted = $this->getLeaveEntitlementDao()->deleteLeaveEntitlements($avaliableToDeleteIds);
     }
     if (!$allDeleted) {
         throw new Exception("Entitlement/s will not be deleted since it's already in use");
     }
     return $deleted;
 }
 protected function getSearchParameterObject($filters)
 {
     $searchParameters = new LeaveEntitlementSearchParameterHolder();
     $employeeName = $filters['employee'];
     $id = $employeeName['empId'];
     $userRoleManager = $this->getContext()->getUserRoleManager();
     $isAccessible = $userRoleManager->isEntityAccessible('Employee', $id);
     if (!empty($id)) {
         if ($isAccessible || $this->getUser()->getAttribute('auth.empNumber') == $id) {
             $searchParameters->setEmpNumber($id);
         } else {
             $this->getUser()->setFlash('warning', 'Access Denied to Selected Employee');
             $this->redirect('leave/viewLeaveEntitlements');
         }
     }
     $searchParameters->setLeaveTypeId($filters['leave_type']);
     $searchParameters->setFromDate($filters['date']['from']);
     $searchParameters->setToDate($filters['date']['to']);
     return $searchParameters;
 }