From 62fb2c91a1a257cd66f6c9546f69595b4ee60641 Mon Sep 17 00:00:00 2001 From: Mads Iversen Date: Mon, 9 Feb 2026 12:09:56 +0000 Subject: [PATCH] feat: Add API endpoint to retrieve time worked by technicians on tickets with filtering by date and technician. --- api/v1/technicians/time.php | 73 +++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 api/v1/technicians/time.php diff --git a/api/v1/technicians/time.php b/api/v1/technicians/time.php new file mode 100644 index 00000000..5d17be98 --- /dev/null +++ b/api/v1/technicians/time.php @@ -0,0 +1,73 @@ + 12)) { + $return_arr['success'] = "False"; + $return_arr['message'] = "Invalid month parameter. Must be between 1 and 12."; + echo json_encode($return_arr); + exit(); +} + +// Optional technician filter +$technician_id = isset($_GET['technician_id']) ? intval($_GET['technician_id']) : null; + +// Build WHERE conditions for date filtering +$date_conditions = "YEAR(tr.ticket_reply_created_at) = $year"; +if ($month !== null) { + $date_conditions .= " AND MONTH(tr.ticket_reply_created_at) = $month"; +} + +// Build technician filter +$technician_condition = ""; +if ($technician_id !== null) { + $technician_condition = "AND tr.ticket_reply_by = $technician_id"; +} + +// Query to get time worked per ticket reply, grouped by technician +$sql = mysqli_query( + $mysqli, + "SELECT + t.ticket_id, + CONCAT(t.ticket_prefix, t.ticket_number) AS ticket_number, + t.ticket_subject, + c.client_id, + c.client_name AS company, + u.user_id AS technician_id, + u.user_name AS technician, + SEC_TO_TIME(SUM(TIME_TO_SEC(tr.ticket_reply_time_worked))) AS time_worked + FROM ticket_replies tr + INNER JOIN tickets t ON t.ticket_id = tr.ticket_reply_ticket_id + INNER JOIN clients c ON c.client_id = t.ticket_client_id + INNER JOIN users u ON u.user_id = tr.ticket_reply_by + WHERE tr.ticket_reply_time_worked IS NOT NULL + AND tr.ticket_reply_time_worked != '00:00:00' + AND $date_conditions + AND t.ticket_client_id LIKE '$client_id' + $technician_condition + GROUP BY t.ticket_id, u.user_id + ORDER BY c.client_name ASC, t.ticket_number ASC, u.user_name ASC + LIMIT $limit OFFSET $offset" +); + +// Output +require_once "../read_output.php";