timer_update.php 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php
  2. header('Content-Type: application/json');
  3. header('Access-Control-Allow-Origin: *');
  4. header('Access-Control-Allow-Methods: POST');
  5. header('Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With');
  6. require_once '../config/database.php';
  7. $database = new Database();
  8. $conn = $database->getConnection();
  9. $data = json_decode(file_get_contents('php://input'), true);
  10. $id = $data['id'] ?? null;
  11. $task_id = $data['task_id'] ?? null;
  12. $description = $data['description'] ?? '';
  13. $hours = $data['hours'] ?? 0;
  14. if ($id && is_numeric($id)) {
  15. try {
  16. // Get timer details first
  17. $query = "SELECT * FROM timers WHERE id = ?";
  18. $stmt = $conn->prepare($query);
  19. $stmt->execute([$id]);
  20. $timer = $stmt->fetch(PDO::FETCH_ASSOC);
  21. if (!$timer) {
  22. echo json_encode(['success' => false, 'message' => 'Timer not found']);
  23. exit;
  24. }
  25. // If task is provided, move to work_hours table
  26. if ($task_id) {
  27. // Calculate end_time if not set (for active timers)
  28. $end_time = $timer['end_time'] ?: gmdate('Y-m-d H:i:s');
  29. // Use existing WorkHour model for work_hours insertion
  30. require_once '../models/WorkHour.php';
  31. $workHour = new WorkHour($conn);
  32. // Set properties on the WorkHour object
  33. $workHour->task_id = $task_id;
  34. $workHour->user_id = $timer['user_id'];
  35. $workHour->date = date('Y-m-d', strtotime($timer['start_time']));
  36. $workHour->hours = $hours;
  37. $workHour->description = $description;
  38. $workHour->rate = null; // Let WorkHour model fetch rate from task/project/customer
  39. $workHourResult = $workHour->create();
  40. if ($workHourResult) {
  41. // Remove timer from timers table after successful move
  42. $deleteQuery = "DELETE FROM timers WHERE id = ?";
  43. $deleteStmt = $conn->prepare($deleteQuery);
  44. $deleteStmt->execute([$id]);
  45. echo json_encode(['success' => true, 'message' => 'Timer moved to work hours successfully']);
  46. } else {
  47. echo json_encode(['success' => false, 'message' => 'Failed to create work hour entry']);
  48. }
  49. } else {
  50. // No task provided, just update timer description
  51. $updateQuery = "UPDATE timers SET description = ?, updated_at = ? WHERE id = ?";
  52. $updateStmt = $conn->prepare($updateQuery);
  53. $updateResult = $updateStmt->execute([$description, gmdate('Y-m-d H:i:s'), $id]);
  54. if ($updateResult) {
  55. echo json_encode(['success' => true, 'message' => 'Timer updated successfully']);
  56. } else {
  57. echo json_encode(['success' => false, 'message' => 'Failed to update timer']);
  58. }
  59. }
  60. } catch (Exception $e) {
  61. echo json_encode(['success' => false, 'error' => 'Error: ' . $e->getMessage()]);
  62. }
  63. } else {
  64. echo json_encode(['success' => false, 'message' => 'Invalid timer ID']);
  65. }
  66. ?>