/src/plugins/orangehrmRESTPlugin/lib/Api/User/Attendance/EmployeePunchInAPI.php

https://github.com/orangehrm/OrangeHRM · PHP · 136 lines · 94 code · 16 blank · 26 comment · 8 complexity · d5a3972d399b4ab424601f7aac53bf8d 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\Rest\Api\User\Attendance;
  20. use Orangehrm\Rest\Api\Exception\InvalidParamException;
  21. use Orangehrm\Rest\Api\Exception\RecordNotFoundException;
  22. use Orangehrm\Rest\Api\Exception\BadRequestException;
  23. use Orangehrm\Rest\Http\Response;
  24. use Orangehrm\Rest\Api\Attendance\PunchInAPI;
  25. use \PluginAttendanceRecord;
  26. use \AttendanceRecord;
  27. class EmployeePunchInAPI extends PunchInAPI
  28. {
  29. /**
  30. * @return Response
  31. * @throws InvalidParamException
  32. * @throws RecordNotFoundException
  33. */
  34. public function savePunchIn()
  35. {
  36. $params = $this->getParameters();
  37. $timeZoneOffset = $params[parent::PARAMETER_TIME_ZONE];
  38. $punchInNote = $params[parent::PARAMETER_NOTE];
  39. $dateTime = $params[parent::PARAMETER_DATE_TIME];
  40. if (empty($dateTime)) {
  41. throw new InvalidParamException('Datetime Cannot Be Empty');
  42. }
  43. if (empty($timeZoneOffset)) {
  44. throw new InvalidParamException('TimeZone Offset Cannot Be Empty');
  45. }
  46. if (!in_array($timeZoneOffset, $this->getValidateTimezoneOffsetList())) {
  47. throw new InvalidParamException('Invalid Time Zone Offset');
  48. }
  49. $empNumber = $this->getLoggedInEmployeeNumber();
  50. if (!$this->checkValidEmployee($empNumber)) {
  51. throw new BadRequestException('Employee Id ' . $empNumber . ' Not Found');
  52. }
  53. $actionableStatesList = array(PluginAttendanceRecord::STATE_PUNCHED_IN);
  54. $attendanceRecord = $this->getAttendanceService()->getLastPunchRecord(
  55. $empNumber,
  56. $actionableStatesList
  57. );
  58. if ($attendanceRecord) {
  59. throw new InvalidParamException('Cannot Proceed Punch In Employee Already Punched In');
  60. }
  61. $attendanceRecord = new AttendanceRecord();
  62. $attendanceRecord->setEmployeeId($empNumber);
  63. $nextState = PluginAttendanceRecord::STATE_PUNCHED_IN;
  64. //check overlapping
  65. $punchInUtcTime = date('Y-m-d H:i', strtotime($dateTime) - $timeZoneOffset * 3600);
  66. $editable = $this->getAttendanceService()->getPunchTimeUserConfiguration();
  67. if (!$editable) {
  68. $utcNowTimeValue = strtotime($this->getCurrentUTCTime());
  69. $userEnterTimeUTCValue = strtotime($punchInUtcTime);
  70. $diff = abs($utcNowTimeValue - $userEnterTimeUTCValue);
  71. if ($diff > 180) {
  72. throw new InvalidParamException('You Are Not Allowed To Change Current Date & Time');
  73. }
  74. }
  75. $isValid = $this->getAttendanceService()->checkForPunchInOverLappingRecords(
  76. $punchInUtcTime,
  77. $empNumber
  78. );
  79. if (!$isValid) {
  80. throw new InvalidParamException('Overlapping Records Found');
  81. }
  82. $attendanceRecord = $this->setPunchInRecord(
  83. $attendanceRecord,
  84. $nextState,
  85. $punchInUtcTime,
  86. $dateTime,
  87. $timeZoneOffset,
  88. $punchInNote
  89. );
  90. return new Response(
  91. array(
  92. 'id' => $attendanceRecord->getId(),
  93. 'datetime' => $attendanceRecord->getPunchInUserTime(),
  94. 'timezoneOffset' => $attendanceRecord->getPunchInTimeOffset(),
  95. 'note' => $attendanceRecord->getPunchInNote()
  96. )
  97. );
  98. }
  99. public function getParameters()
  100. {
  101. $params = array();
  102. $params[parent::PARAMETER_TIME_ZONE] = $this->getRequestParams()->getPostParam(
  103. parent::PARAMETER_TIME_ZONE_OFFSET
  104. );
  105. $params[parent::PARAMETER_NOTE] = $this->getRequestParams()->getPostParam(parent::PARAMETER_NOTE);
  106. $params[parent::PARAMETER_DATE_TIME] = $this->getRequestParams()->getPostParam(parent::PARAMETER_DATE_TIME);
  107. return $params;
  108. }
  109. /**
  110. * @return array
  111. */
  112. public function getValidationRules(): array
  113. {
  114. return [
  115. parent::PARAMETER_NOTE => ['StringType' => true, 'Length' => [1, 250]],
  116. parent::PARAMETER_DATE_TIME => ['Date' => ['Y-m-d H:i']],
  117. parent::PARAMETER_TIME_ZONE => ['Numeric' => true, 'NotEmpty' => true]
  118. ];
  119. }
  120. }