work_hours.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <?php
  2. header('Content-Type: application/json');
  3. header('Access-Control-Allow-Origin: *');
  4. header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE');
  5. header('Access-Control-Allow-Headers: Content-Type, Authorization');
  6. require_once __DIR__ . '/../config/database.php';
  7. require_once __DIR__ . '/../models/WorkHour.php';
  8. class WorkHourAPI {
  9. private $conn;
  10. private $workHour;
  11. public function __construct() {
  12. $database = new Database();
  13. $this->conn = $database->getConnection();
  14. $this->workHour = new WorkHour($this->conn);
  15. }
  16. public function create($data) {
  17. $this->workHour->task_id = $data['task_id'];
  18. $this->workHour->user_id = $data['user_id'];
  19. $this->workHour->date = $data['date'];
  20. $this->workHour->hours = $data['hours'];
  21. $this->workHour->description = $data['description'] ?? '';
  22. $this->workHour->rate = $data['rate'] ?? null;
  23. // Calculate total amount if rate is provided
  24. if ($this->workHour->rate) {
  25. $this->workHour->total_amount = $this->workHour->hours * $this->workHour->rate;
  26. }
  27. if ($this->workHour->create()) {
  28. return [
  29. 'success' => true,
  30. 'message' => 'Work hour entry created successfully',
  31. 'id' => $this->conn->lastInsertId()
  32. ];
  33. }
  34. return [
  35. 'success' => false,
  36. 'message' => 'Failed to create work hour entry'
  37. ];
  38. }
  39. public function getAll($task_id = null, $user_id = null) {
  40. if ($task_id) {
  41. $stmt = $this->workHour->readByTask($task_id);
  42. } elseif ($user_id) {
  43. // For user-specific queries, we'd need to add a readByUser method
  44. $stmt = $this->workHour->read();
  45. } else {
  46. $stmt = $this->workHour->read();
  47. }
  48. $workHours = [];
  49. while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
  50. $workHours[] = $row;
  51. }
  52. return [
  53. 'success' => true,
  54. 'data' => $workHours
  55. ];
  56. }
  57. public function getById($id) {
  58. $this->workHour->id = $id;
  59. $this->workHour->readOne();
  60. if ($this->workHour->id) {
  61. return [
  62. 'success' => true,
  63. 'data' => [
  64. 'id' => $this->workHour->id,
  65. 'task_id' => $this->workHour->task_id,
  66. 'user_id' => $this->workHour->user_id,
  67. 'date' => $this->workHour->date,
  68. 'hours' => $this->workHour->hours,
  69. 'description' => $this->workHour->description,
  70. 'rate' => $this->workHour->rate,
  71. 'total_amount' => $this->workHour->total_amount,
  72. 'created_at' => $this->workHour->created_at,
  73. 'updated_at' => $this->workHour->updated_at
  74. ]
  75. ];
  76. }
  77. return [
  78. 'success' => false,
  79. 'message' => 'Work hour entry not found'
  80. ];
  81. }
  82. public function update($id, $data) {
  83. $this->workHour->id = $id;
  84. $this->workHour->task_id = $data['task_id'];
  85. $this->workHour->user_id = $data['user_id'];
  86. $this->workHour->date = $data['date'];
  87. $this->workHour->hours = $data['hours'];
  88. $this->workHour->description = $data['description'] ?? '';
  89. $this->workHour->rate = $data['rate'] ?? null;
  90. // Recalculate total amount if rate is provided
  91. if ($this->workHour->rate) {
  92. $this->workHour->total_amount = $this->workHour->hours * $this->workHour->rate;
  93. }
  94. if ($this->workHour->update()) {
  95. return [
  96. 'success' => true,
  97. 'message' => 'Work hour entry updated successfully'
  98. ];
  99. }
  100. return [
  101. 'success' => false,
  102. 'message' => 'Failed to update work hour entry'
  103. ];
  104. }
  105. public function delete($id) {
  106. $this->workHour->id = $id;
  107. if ($this->workHour->delete()) {
  108. return [
  109. 'success' => true,
  110. 'message' => 'Work hour entry deleted successfully'
  111. ];
  112. }
  113. return [
  114. 'success' => false,
  115. 'message' => 'Failed to delete work hour entry'
  116. ];
  117. }
  118. public function getTaskSummary($task_id) {
  119. $summary = $this->workHour->getTotalHoursByTask($task_id);
  120. return [
  121. 'success' => true,
  122. 'data' => $summary
  123. ];
  124. }
  125. }
  126. // Handle API requests
  127. $method = $_SERVER['REQUEST_METHOD'];
  128. try {
  129. $api = new WorkHourAPI();
  130. switch ($method) {
  131. case 'GET':
  132. $task_id = $_GET['task_id'] ?? null;
  133. $user_id = $_GET['user_id'] ?? null;
  134. $result = $api->getAll($task_id, $user_id);
  135. echo json_encode($result);
  136. break;
  137. case 'POST':
  138. $data = json_decode(file_get_contents('php://input'), true);
  139. $result = $api->create($data);
  140. echo json_encode($result);
  141. break;
  142. case 'PUT':
  143. $data = json_decode(file_get_contents('php://input'), true);
  144. $id = $data['id'];
  145. $result = $api->update($id, $data);
  146. echo json_encode($result);
  147. break;
  148. case 'DELETE':
  149. $id = $_GET['id'];
  150. $result = $api->delete($id);
  151. echo json_encode($result);
  152. break;
  153. default:
  154. echo json_encode([
  155. 'success' => false,
  156. 'message' => 'Invalid request method'
  157. ]);
  158. break;
  159. }
  160. } catch (Exception $e) {
  161. echo json_encode([
  162. 'success' => false,
  163. 'message' => 'Database error: ' . $e->getMessage()
  164. ]);
  165. }
  166. ?>