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