/src/plugins/orangehrmAdminPlugin/Service/LocationService.php

https://github.com/orangehrm/OrangeHRM · PHP · 176 lines · 76 code · 15 blank · 85 comment · 2 complexity · 214aa684bbe555ebda8fbe634a6b2e94 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\Admin\Service;
  20. use OrangeHRM\Admin\Dao\LocationDao;
  21. use OrangeHRM\Admin\Dto\LocationSearchFilterParams;
  22. use OrangeHRM\Admin\Service\Model\LocationModel;
  23. use OrangeHRM\Core\Traits\Service\NormalizerServiceTrait;
  24. use OrangeHRM\Core\Traits\UserRoleManagerTrait;
  25. use OrangeHRM\Entity\Location;
  26. use OrangeHRM\Pim\Traits\Service\EmployeeServiceTrait;
  27. class LocationService
  28. {
  29. use UserRoleManagerTrait;
  30. use NormalizerServiceTrait;
  31. use EmployeeServiceTrait;
  32. /**
  33. * @var LocationDao|null
  34. */
  35. private ?LocationDao $locationDao = null;
  36. /**
  37. * @return LocationDao
  38. */
  39. public function getLocationDao(): LocationDao
  40. {
  41. if (!($this->locationDao instanceof LocationDao)) {
  42. $this->locationDao = new LocationDao();
  43. }
  44. return $this->locationDao;
  45. }
  46. /**
  47. * @param LocationDao $locationDao
  48. */
  49. public function setLocationDao(LocationDao $locationDao): void
  50. {
  51. $this->locationDao = $locationDao;
  52. }
  53. /**
  54. * Get Location by id
  55. *
  56. * @param int $locationId
  57. *
  58. * @return Location|null
  59. */
  60. public function getLocationById(int $locationId): ?Location
  61. {
  62. return $this->getLocationDao()->getLocationById($locationId);
  63. }
  64. /**
  65. * Search location by location name, city and country.
  66. *
  67. * @param LocationSearchFilterParams $locationSearchFilterParams
  68. *
  69. * @return Location[]
  70. */
  71. public function searchLocations(LocationSearchFilterParams $locationSearchFilterParams): array
  72. {
  73. return $this->getLocationDao()->searchLocations($locationSearchFilterParams);
  74. }
  75. /**
  76. * Get location count of the search results.
  77. *
  78. * @param LocationSearchFilterParams $locationSearchFilterParams
  79. *
  80. * @return int
  81. */
  82. public function getSearchLocationListCount(LocationSearchFilterParams $locationSearchFilterParams): int
  83. {
  84. return $this->getLocationDao()->getSearchLocationListCount($locationSearchFilterParams);
  85. }
  86. /**
  87. * Get total number of employees in a location.
  88. *
  89. * @param int $locationId
  90. *
  91. * @return int
  92. */
  93. public function getNumberOfEmployeesForLocation(int $locationId): int
  94. {
  95. return $this->getLocationDao()->getNumberOfEmployeesForLocation($locationId);
  96. }
  97. /**
  98. * Get LocationIds for Employees with the given employee numbers
  99. *
  100. * @param int[] $empNumbers Array of employee numbers
  101. *
  102. * @return int[] of locationIds of the given employees
  103. */
  104. public function getLocationIdsForEmployees(array $empNumbers): array
  105. {
  106. return $this->getLocationDao()->getLocationIdsForEmployees($empNumbers);
  107. }
  108. /**
  109. * Returns the accessible location list
  110. *
  111. * @param int|null $empNumber
  112. *
  113. * @return array
  114. */
  115. public function getAccessibleLocationsArray(?int $empNumber = null): array
  116. {
  117. $employeeLocationsIds = [];
  118. if (!is_null($empNumber)) {
  119. $employee = $this->getEmployeeService()->getEmployeeByEmpNumber($empNumber);
  120. foreach ($employee->getLocations() as $location) {
  121. $employeeLocationsIds[] = $location->getId();
  122. }
  123. }
  124. $accessibleLocationIds = $this->getUserRoleManager()->getAccessibleEntityIds(Location::class);
  125. $accessibleLocationIds = array_unique(array_merge($accessibleLocationIds, $employeeLocationsIds));
  126. $accessibleLocations = $this->getLocationDao()->getLocationsByIds($accessibleLocationIds);
  127. return $this->getNormalizerService()->normalizeArray(LocationModel::class, $accessibleLocations);
  128. }
  129. /**
  130. * Save Location in the database
  131. *
  132. * @param Location $location
  133. *
  134. * @return Location
  135. */
  136. public function saveLocation(Location $location): Location
  137. {
  138. return $this->getLocationDao()->saveLocation($location);
  139. }
  140. /**
  141. * This will flag the Locations as deleted
  142. *
  143. * @param array $ids
  144. *
  145. * @return int number of affected rows
  146. */
  147. public function deleteLocations(array $ids): int
  148. {
  149. return $this->getLocationDao()->deleteLocations($ids);
  150. }
  151. /**
  152. * @return array
  153. */
  154. public function getLocationsArray(): array
  155. {
  156. $locationSearchFilterParams = new LocationSearchFilterParams();
  157. $locationSearchFilterParams->setLimit(0);
  158. $locations = $this->getLocationDao()->searchLocations($locationSearchFilterParams);
  159. return $this->getNormalizerService()->normalizeArray(LocationModel::class, $locations);
  160. }
  161. }