/src/plugins/orangehrmPimPlugin/Service/EmployeeService.php

https://github.com/orangehrm/OrangeHRM · PHP · 326 lines · 163 code · 31 blank · 132 comment · 8 complexity · 77fbf0d25555ba620b459af0165ecf71 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\Service;
  20. use DateTime;
  21. use OrangeHRM\Core\Exception\CoreServiceException;
  22. use OrangeHRM\Core\Registration\Event\RegistrationEvent;
  23. use OrangeHRM\Core\Service\IDGeneratorService;
  24. use OrangeHRM\Core\Traits\EventDispatcherTrait;
  25. use OrangeHRM\Core\Traits\Service\ConfigServiceTrait;
  26. use OrangeHRM\Core\Traits\Service\NormalizerServiceTrait;
  27. use OrangeHRM\Entity\Employee;
  28. use OrangeHRM\Pim\Dao\EmployeeDao;
  29. use OrangeHRM\Pim\Dto\EmployeeSearchFilterParams;
  30. use OrangeHRM\Pim\Event\EmployeeAddedEvent;
  31. use OrangeHRM\Pim\Event\EmployeeEvents;
  32. use OrangeHRM\Pim\Event\EmployeeJoinedDateChangedEvent;
  33. use OrangeHRM\Pim\Service\Model\EmployeeModel;
  34. class EmployeeService
  35. {
  36. use EventDispatcherTrait;
  37. use ConfigServiceTrait;
  38. use NormalizerServiceTrait;
  39. /**
  40. * @var EmployeeDao|null
  41. */
  42. protected ?EmployeeDao $employeeDao = null;
  43. /**
  44. * @var EmployeeEventService|null
  45. */
  46. protected ?EmployeeEventService $employeeEventService = null;
  47. /**
  48. * @var EmployeeTerminationService|null
  49. */
  50. protected ?EmployeeTerminationService $employeeTerminationService = null;
  51. /**
  52. * @var IDGeneratorService|null
  53. */
  54. protected ?IDGeneratorService $iDGeneratorService = null;
  55. /**
  56. * @return EmployeeDao
  57. */
  58. public function getEmployeeDao(): EmployeeDao
  59. {
  60. if (is_null($this->employeeDao)) {
  61. $this->employeeDao = new EmployeeDao();
  62. }
  63. return $this->employeeDao;
  64. }
  65. /**
  66. * @param EmployeeDao $employeeDao
  67. */
  68. public function setEmployeeDao(EmployeeDao $employeeDao): void
  69. {
  70. $this->employeeDao = $employeeDao;
  71. }
  72. /**
  73. * @return EmployeeEventService
  74. */
  75. public function getEmployeeEventService(): EmployeeEventService
  76. {
  77. if (!$this->employeeEventService instanceof EmployeeEventService) {
  78. $this->employeeEventService = new EmployeeEventService();
  79. }
  80. return $this->employeeEventService;
  81. }
  82. /**
  83. * @param EmployeeEventService $employeeEventService
  84. */
  85. public function setEmployeeEventService(EmployeeEventService $employeeEventService): void
  86. {
  87. $this->employeeEventService = $employeeEventService;
  88. }
  89. /**
  90. * @return EmployeeTerminationService
  91. */
  92. public function getEmployeeTerminationService(): EmployeeTerminationService
  93. {
  94. if (!$this->employeeTerminationService instanceof EmployeeTerminationService) {
  95. $this->employeeTerminationService = new EmployeeTerminationService();
  96. }
  97. return $this->employeeTerminationService;
  98. }
  99. /**
  100. * @return IDGeneratorService
  101. */
  102. public function getIDGeneratorService(): IDGeneratorService
  103. {
  104. if (!$this->iDGeneratorService instanceof IDGeneratorService) {
  105. $this->iDGeneratorService = new IDGeneratorService();
  106. }
  107. return $this->iDGeneratorService;
  108. }
  109. /**
  110. * @param EmployeeSearchFilterParams $employeeSearchParamHolder
  111. * @return Employee[]
  112. */
  113. public function getEmployeeList(EmployeeSearchFilterParams $employeeSearchParamHolder): array
  114. {
  115. return $this->getEmployeeDao()->getEmployeeList($employeeSearchParamHolder);
  116. }
  117. /**
  118. * @param EmployeeSearchFilterParams $employeeSearchParamHolder
  119. * @return int
  120. */
  121. public function getEmployeeCount(EmployeeSearchFilterParams $employeeSearchParamHolder): int
  122. {
  123. return $this->getEmployeeDao()->getEmployeeCount($employeeSearchParamHolder);
  124. }
  125. /**
  126. * @param Employee $employee
  127. * @return Employee
  128. */
  129. public function saveEmployee(Employee $employee): Employee
  130. {
  131. $savedEmployee = $this->getEmployeeDao()->saveEmployee($employee);
  132. // TODO:: Improve
  133. $eventName = $employee->getEmployeeTerminationRecord() == null ? RegistrationEvent::EMPLOYEE_ADD_EVENT_NAME : RegistrationEvent::EMPLOYEE_TERMINATE_EVENT_NAME;
  134. $this->getEventDispatcher()->dispatch(new RegistrationEvent(), $eventName);
  135. return $savedEmployee;
  136. }
  137. /**
  138. * @param Employee $employee
  139. * @return Employee
  140. */
  141. public function saveNewEmployee(Employee $employee): Employee
  142. {
  143. $this->getIDGeneratorService()->incrementId(Employee::class);
  144. return $this->saveEmployee($employee);
  145. }
  146. /**
  147. * @param Employee $employee
  148. */
  149. public function dispatchAddEmployeeEvent(Employee $employee): void
  150. {
  151. $this->getEmployeeEventService()->saveAddEmployeeEvent($employee->getEmpNumber());
  152. $this->getEventDispatcher()->dispatch(new EmployeeAddedEvent($employee), EmployeeEvents::EMPLOYEE_ADDED);
  153. }
  154. /**
  155. * @param Employee $employee
  156. * @return Employee
  157. */
  158. public function updateEmployeePersonalDetails(Employee $employee): Employee
  159. {
  160. $employee = $this->saveEmployee($employee);
  161. $this->getEmployeeEventService()->saveUpdateEmployeePersonalDetailsEvent($employee->getEmpNumber());
  162. return $employee;
  163. }
  164. /**
  165. * @param Employee $employee
  166. */
  167. public function updateEmployeeJobDetails(Employee $employee): void
  168. {
  169. $employee = $this->saveEmployee($employee);
  170. $this->getEmployeeEventService()->saveUpdateJobDetailsEvent($employee->getEmpNumber());
  171. }
  172. /**
  173. * @param Employee $employee
  174. * @param DateTime|null $previousJoinedDate
  175. */
  176. public function dispatchJoinedDateChangedEvent(Employee $employee, ?DateTime $previousJoinedDate): void
  177. {
  178. $this->getEventDispatcher()->dispatch(
  179. new EmployeeJoinedDateChangedEvent($employee, $previousJoinedDate),
  180. EmployeeEvents::JOINED_DATE_CHANGED
  181. );
  182. }
  183. /**
  184. * @param int $empNumber
  185. * @return Employee|null
  186. */
  187. public function getEmployeeByEmpNumber(int $empNumber): ?Employee
  188. {
  189. return $this->getEmployeeDao()->getEmployeeByEmpNumber($empNumber);
  190. }
  191. /**
  192. * @param bool $includeTerminated
  193. * @return int
  194. */
  195. public function getNumberOfEmployees(bool $includeTerminated = false): int
  196. {
  197. return $this->getEmployeeDao()->getNumberOfEmployees($includeTerminated);
  198. }
  199. /**
  200. * Returns an array of empNumbers of subordinates for given supervisor ID
  201. *
  202. * empNumbers of whole chain under given supervisor are returned.
  203. *
  204. * @param int $supervisorId Supervisor's ID
  205. * @param bool|null $includeChain Include Supervisor chain or not
  206. * @param int|null $maxDepth
  207. * @return int[] An array of empNumbers
  208. * @throws CoreServiceException
  209. */
  210. public function getSubordinateIdListBySupervisorId(
  211. int $supervisorId,
  212. ?bool $includeChain = null,
  213. int $maxDepth = null
  214. ): array {
  215. if (is_null($includeChain)) {
  216. $includeChain = $this->getConfigService()->isSupervisorChainSupported();
  217. }
  218. return $this->getEmployeeDao()->getSubordinateIdListBySupervisorId($supervisorId, $includeChain, [], $maxDepth);
  219. }
  220. /**
  221. * Return List of Subordinates for given Supervisor
  222. *
  223. * @param int $supervisorId Supervisor Id
  224. * @param bool $includeTerminated Terminated status
  225. * @return Employee[] of Subordinates
  226. */
  227. public function getSubordinateList(int $supervisorId, bool $includeTerminated = false): array
  228. {
  229. $includeChain = $this->getConfigService()->isSupervisorChainSupported();
  230. return $this->getEmployeeDao()->getSubordinateList($supervisorId, $includeTerminated, $includeChain);
  231. }
  232. /**
  233. * Check if employee with given employee number is a supervisor
  234. *
  235. * @param int $empNumber Employee Number
  236. * @return bool True if given employee is a supervisor, false if not
  237. */
  238. public function isSupervisor(int $empNumber): bool
  239. {
  240. return $this->getEmployeeDao()->isSupervisor($empNumber);
  241. }
  242. /**
  243. * @param int[] $empNumbers
  244. * @return int
  245. */
  246. public function deleteEmployees(array $empNumbers): int
  247. {
  248. return $this->getEmployeeDao()->deleteEmployees($empNumbers);
  249. }
  250. /**
  251. * Returns an array of empNumbers of supervisors for given subordinate ID
  252. *
  253. * empNumbers of whole chain under given subordinate are returned.
  254. *
  255. * @param int $subordinateId
  256. * @param bool|null $includeChain Include Supervisor chain or not
  257. * @param int|null $maxDepth
  258. * @return int[] An array of empNumbers
  259. * @throws CoreServiceException
  260. */
  261. public function getSupervisorIdListBySubordinateId(
  262. int $subordinateId,
  263. ?bool $includeChain = null,
  264. int $maxDepth = null
  265. ): array {
  266. if (is_null($includeChain)) {
  267. $includeChain = $this->getConfigService()->isSupervisorChainSupported();
  268. }
  269. return $this->getEmployeeDao()->getSupervisorIdListBySubordinateId(
  270. $subordinateId,
  271. $includeChain,
  272. [],
  273. $maxDepth
  274. );
  275. }
  276. /**
  277. * @param int $empNumber
  278. * @return array|null
  279. */
  280. public function getEmployeeAsArray(int $empNumber): ?array
  281. {
  282. $employee = $this->getEmployeeByEmpNumber($empNumber);
  283. if (!$employee instanceof Employee) {
  284. return null;
  285. }
  286. return $this->getNormalizerService()->normalize(EmployeeModel::class, $employee);
  287. }
  288. /**
  289. * @return Employee[]
  290. */
  291. public function getAvailableEmployeesForWorkShift(EmployeeSearchFilterParams $employeeSearchParamHolder): array
  292. {
  293. return $this->getEmployeeDao()->getAvailableEmployeeListForWorkShift($employeeSearchParamHolder);
  294. }
  295. }