/src/plugins/orangehrmPimPlugin/Dao/TerminationReasonConfigurationDao.php

https://github.com/orangehrm/OrangeHRM · PHP · 184 lines · 113 code · 11 blank · 60 comment · 2 complexity · 56c4d2112813b19fd1be8d7f4fb28d49 MD5 · raw file

  1. <?php
  2. /**
  3. * OrangeHRM is a comprehensive Human Resource Management (HRM) System that captures
  4. * all the essential functionalities required for any enterprise.
  5. * Copyright (C) 2006 OrangeHRM Inc., http://www.orangehrm.com
  6. *
  7. * OrangeHRM is free software; you can redistribute it and/or modify it under the terms of
  8. * the GNU General Public License as published by the Free Software Foundation; either
  9. * version 2 of the License, or (at your option) any later version.
  10. *
  11. * OrangeHRM is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
  12. * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. * See the GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along with this program;
  16. * if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  17. * Boston, MA 02110-1301, USA
  18. */
  19. namespace OrangeHRM\Pim\Dao;
  20. use Exception;
  21. use OrangeHRM\Core\Dao\BaseDao;
  22. use OrangeHRM\Core\Exception\DaoException;
  23. use OrangeHRM\Entity\Employee;
  24. use OrangeHRM\Entity\TerminationReason;
  25. use OrangeHRM\ORM\Paginator;
  26. use OrangeHRM\Pim\Dto\TerminationReasonConfigurationSearchFilterParams;
  27. class TerminationReasonConfigurationDao extends BaseDao
  28. {
  29. /**
  30. * @param TerminationReason $terminationReason
  31. * @return TerminationReason
  32. * @throws DaoException
  33. */
  34. public function saveTerminationReason(TerminationReason $terminationReason): TerminationReason
  35. {
  36. try {
  37. $this->persist($terminationReason);
  38. return $terminationReason;
  39. } catch (Exception $e) {
  40. throw new DaoException($e->getMessage(), $e->getCode(), $e);
  41. }
  42. }
  43. /**
  44. * @param int $id
  45. * @return TerminationReason|null
  46. * @throws DaoException
  47. */
  48. public function getTerminationReasonById(int $id): ?TerminationReason
  49. {
  50. try {
  51. $terminationReason = $this->getRepository(TerminationReason::class)->find($id);
  52. if ($terminationReason instanceof TerminationReason) {
  53. return $terminationReason;
  54. }
  55. return null;
  56. } catch (Exception $e) {
  57. throw new DaoException($e->getMessage(), $e->getCode(), $e);
  58. }
  59. }
  60. /**
  61. * @param string $name
  62. * @return TerminationReason|null
  63. * @throws DaoException
  64. */
  65. public function getTerminationReasonByName(string $name): ?TerminationReason
  66. {
  67. try {
  68. $query = $this->createQueryBuilder(TerminationReason::class, 'tr');
  69. $trimmed = trim($name, ' ');
  70. $query->andWhere('tr.name = :name');
  71. $query->setParameter('name', $trimmed);
  72. return $query->getQuery()->getOneOrNullResult();
  73. } catch (Exception $e) {
  74. throw new DaoException($e->getMessage(), $e->getCode(), $e);
  75. }
  76. }
  77. /**
  78. * @param TerminationReasonConfigurationSearchFilterParams $terminationReasonConfigurationSearchFilterParams
  79. * @return array
  80. * @throws DaoException
  81. */
  82. public function getTerminationReasonList(
  83. TerminationReasonConfigurationSearchFilterParams $terminationReasonConfigurationSearchFilterParams
  84. ): array {
  85. try {
  86. $paginator = $this->getTerminationReasonListPaginator($terminationReasonConfigurationSearchFilterParams);
  87. return $paginator->getQuery()->execute();
  88. } catch (Exception $e) {
  89. throw new DaoException($e->getMessage(), $e->getCode(), $e);
  90. }
  91. }
  92. /**
  93. * @param TerminationReasonConfigurationSearchFilterParams $terminationReasonConfigurationSearchFilterParams
  94. * @return Paginator
  95. */
  96. public function getTerminationReasonListPaginator(
  97. TerminationReasonConfigurationSearchFilterParams $terminationReasonConfigurationSearchFilterParams
  98. ): Paginator {
  99. $q = $this->createQueryBuilder(TerminationReason::class, 'tr');
  100. $this->setSortingAndPaginationParams($q, $terminationReasonConfigurationSearchFilterParams);
  101. return new Paginator($q);
  102. }
  103. /**
  104. * @param TerminationReasonConfigurationSearchFilterParams $terminationReasonConfigurationSearchFilterParams
  105. * @return int
  106. * @throws DaoException
  107. */
  108. public function getTerminationReasonCount(
  109. TerminationReasonConfigurationSearchFilterParams $terminationReasonConfigurationSearchFilterParams
  110. ): int {
  111. try {
  112. $paginator = $this->getTerminationReasonListPaginator($terminationReasonConfigurationSearchFilterParams);
  113. return $paginator->count();
  114. } catch (Exception $e) {
  115. throw new DaoException($e->getMessage(), $e->getCode(), $e);
  116. }
  117. }
  118. /**
  119. * @param array $toDeleteIds
  120. * @return int
  121. * @throws DaoException
  122. */
  123. public function deleteTerminationReasons(array $toDeleteIds): int
  124. {
  125. try {
  126. $q = $this->createQueryBuilder(TerminationReason::class, 'tr');
  127. $q->delete()
  128. ->where($q->expr()->in('tr.id', ':ids'))
  129. ->setParameter('ids', $toDeleteIds);
  130. return $q->getQuery()->execute();
  131. } catch (Exception $e) {
  132. throw new DaoException($e->getMessage(), $e->getCode(), $e);
  133. }
  134. }
  135. /**
  136. * @param string $terminationReasonName
  137. * @return bool
  138. * @throws DaoException
  139. */
  140. public function isExistingTerminationReasonName(string $terminationReasonName): bool
  141. {
  142. try {
  143. $q = $this->createQueryBuilder(TerminationReason::class, 'tr');
  144. $trimmed = trim($terminationReasonName, ' ');
  145. $q->Where('tr.name = :name');
  146. $q->setParameter('name', $trimmed);
  147. $count = $this->count($q);
  148. if ($count > 0) {
  149. return true;
  150. }
  151. return false;
  152. } catch (Exception $e) {
  153. throw new DaoException($e->getMessage(), $e->getCode(), $e);
  154. }
  155. }
  156. /**
  157. * @return array
  158. * @throws DaoException
  159. */
  160. public function getReasonIdsInUse(): array
  161. {
  162. try {
  163. $query = $this->createQueryBuilder(Employee::class, 'e');
  164. $query->leftJoin('e.employeeTerminationRecord', 'et');
  165. $query->leftJoin('et.terminationReason', 'tr');
  166. $query->select('tr.id');
  167. $result = $query->getQuery()->getScalarResult();
  168. return array_column($result, 'id');
  169. } catch (Exception $e) {
  170. throw new DaoException($e->getMessage(), $e->getCode(), $e);
  171. }
  172. }
  173. }