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); } }
protected function getLeaveEntitlement($values) { if (isset($values['filters']['bulk_assign'])) { $leaveEntitlement = new LeaveEntitlement(); $leaveEntitlement->setNoOfDays($values['entitlement']); } else { if (isset($values['id'])) { $id = $values['id']; $leaveEntitlement = $this->getLeaveEntitlementService()->getLeaveEntitlement($id); $leaveEntitlement->setNoOfDays($values['entitlement']); } else { if (LeavePeriodService::getLeavePeriodStatus() == LeavePeriodService::LEAVE_PERIOD_STATUS_FORCED) { $empNumber = $values['employee']['empId']; $fromDate = $values['date']['from']; $toDate = $values['date']['to']; $leaveTypeId = $values['leave_type']; $entitlementList = $this->getLeaveEntitlementService()->getMatchingEntitlements($empNumber, $leaveTypeId, $fromDate, $toDate); if (count($entitlementList) > 0) { $leaveEntitlement = $entitlementList->getFirst(); $newValue = $leaveEntitlement->getNoOfDays() + $values['entitlement']; $leaveEntitlement->setNoOfDays($newValue); } else { $leaveEntitlement = new LeaveEntitlement(); $leaveEntitlement->setNoOfDays($values['entitlement']); } } else { $leaveEntitlement = new LeaveEntitlement(); $leaveEntitlement->setNoOfDays($values['entitlement']); } } } if (isset($values['employee']['empId'])) { $leaveEntitlement->setEmpNumber($values['employee']['empId']); } if (isset($values['leave_type'])) { $leaveEntitlement->setLeaveTypeId($values['leave_type']); } $user = $this->getUser(); $userId = $user->getAttribute('auth.userId'); $createdBy = $this->getUser()->getAttribute('auth.firstName'); $leaveEntitlement->setCreditedDate(date('Y-m-d')); $leaveEntitlement->setCreatedById($userId); $leaveEntitlement->setCreatedByName($createdBy); $leaveEntitlement->setEntitlementType(LeaveEntitlement::ENTITLEMENT_TYPE_ADD); $leaveEntitlement->setDeleted(0); $leaveEntitlement->setNoOfDays(round($leaveEntitlement->getNoOfDays(), 2)); $leaveEntitlement->setFromDate($values['date']['from']); $leaveEntitlement->setToDate($values['date']['to']); return $leaveEntitlement; }