Skip to content

Commit bdf8038

Browse files
authored
Merge pull request #1265 from MydsiIversen/Time-API
feat: Add API endpoint to retrieve time worked by technicians
2 parents d1eeba6 + 62fb2c9 commit bdf8038

File tree

1 file changed

+73
-0
lines changed

1 file changed

+73
-0
lines changed

api/v1/technicians/time.php

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
3+
/*
4+
* API - technicians/time.php
5+
* Returns time worked by technicians on tickets
6+
*
7+
* GET Parameters:
8+
* api_key (required) - API key for authentication
9+
* year (optional) - Filter by year (default: current year)
10+
* month (optional) - Filter by month 1-12 (default: current month)
11+
* technician_id (optional) - Filter by specific technician user ID
12+
* limit (optional) - Number of results to return (default: 50)
13+
* offset (optional) - Offset for pagination (default: 0)
14+
*/
15+
16+
require_once '../validate_api_key.php';
17+
require_once '../require_get_method.php';
18+
19+
// Get filter parameters
20+
$year = isset($_GET['year']) ? intval($_GET['year']) : intval(date('Y'));
21+
$month = isset($_GET['month']) ? intval($_GET['month']) : null;
22+
23+
// Validate month if provided
24+
if ($month !== null && ($month < 1 || $month > 12)) {
25+
$return_arr['success'] = "False";
26+
$return_arr['message'] = "Invalid month parameter. Must be between 1 and 12.";
27+
echo json_encode($return_arr);
28+
exit();
29+
}
30+
31+
// Optional technician filter
32+
$technician_id = isset($_GET['technician_id']) ? intval($_GET['technician_id']) : null;
33+
34+
// Build WHERE conditions for date filtering
35+
$date_conditions = "YEAR(tr.ticket_reply_created_at) = $year";
36+
if ($month !== null) {
37+
$date_conditions .= " AND MONTH(tr.ticket_reply_created_at) = $month";
38+
}
39+
40+
// Build technician filter
41+
$technician_condition = "";
42+
if ($technician_id !== null) {
43+
$technician_condition = "AND tr.ticket_reply_by = $technician_id";
44+
}
45+
46+
// Query to get time worked per ticket reply, grouped by technician
47+
$sql = mysqli_query(
48+
$mysqli,
49+
"SELECT
50+
t.ticket_id,
51+
CONCAT(t.ticket_prefix, t.ticket_number) AS ticket_number,
52+
t.ticket_subject,
53+
c.client_id,
54+
c.client_name AS company,
55+
u.user_id AS technician_id,
56+
u.user_name AS technician,
57+
SEC_TO_TIME(SUM(TIME_TO_SEC(tr.ticket_reply_time_worked))) AS time_worked
58+
FROM ticket_replies tr
59+
INNER JOIN tickets t ON t.ticket_id = tr.ticket_reply_ticket_id
60+
INNER JOIN clients c ON c.client_id = t.ticket_client_id
61+
INNER JOIN users u ON u.user_id = tr.ticket_reply_by
62+
WHERE tr.ticket_reply_time_worked IS NOT NULL
63+
AND tr.ticket_reply_time_worked != '00:00:00'
64+
AND $date_conditions
65+
AND t.ticket_client_id LIKE '$client_id'
66+
$technician_condition
67+
GROUP BY t.ticket_id, u.user_id
68+
ORDER BY c.client_name ASC, t.ticket_number ASC, u.user_name ASC
69+
LIMIT $limit OFFSET $offset"
70+
);
71+
72+
// Output
73+
require_once "../read_output.php";

0 commit comments

Comments
 (0)