SELECT lul.id_user, u.manager_user_id, u.name AS user_name,
       lul.latitude AS current_latitude, lul.longitude AS current_longitude, lul.updated_at AS last_seen_at,
       a.co_oficina, o.nombre AS oficina_name,
       l.created_at AS checkin_at, lo.created_at AS checkout_at,
       round(st_distance_sphere(POINT(l.longitude, l.latitude), POINT(o.longitud, o.latitud)), 0) AS dist_checkin_oficina,
       round(st_distance_sphere(POINT(lo.longitude, lo.latitude), POINT(o.longitud, o.latitud)), 0) AS dist_checkout_oficina,
       round(st_distance_sphere(POINT(l.longitude, l.latitude), POINT(lul.longitude, lul.latitude)), 0) AS dist_checkin_current,
       round(st_distance_sphere(POINT(lo.longitude, lo.latitude), POINT(lul.longitude, lul.latitude)), 0) AS dist_checkout_current,
       (a.id_location_out IS NOT NULL) AS actividad_finalizada,
       sec_to_time(TIMESTAMPDIFF(SECOND, l.created_at, CURRENT_TIMESTAMP)) AS time_since_checkin,
       sec_to_time(TIMESTAMPDIFF(SECOND, lo.created_at, CURRENT_TIMESTAMP)) AS time_since_checkout,
       sec_to_time(TIMESTAMPDIFF(SECOND, lul.updated_at, CURRENT_TIMESTAMP)) AS time_since_last_seen,
       TIMESTAMPDIFF(SECOND, lul.updated_at, CURRENT_TIMESTAMP) as secs_since_last_seen
FROM last_user_locations lul LEFT JOIN vw_last_actividad a
       ON lul.id_user = a.id_user
LEFT JOIN locations l
       ON a.id_location_in = l.id
LEFT JOIN locations lo
       ON a.id_location_out = lo.id
JOIN users u
       ON u.id = lul.id_user
left JOIN oficinas o
       ON a.co_oficina = o.co_oficina
