SELECT v.id as id_actividad,u.manager_user_id, v.id_user, v.co_oficina, ta.tipo, sta.subtipo, ta.id as id_tipo, sta.id as id_subtipo,
       v.id_location_in, v.id_location_out, v.nombre_contacto,
       v.email_contacto, v.telefono_contacto, v.comentario, v.id_actividad_asignada,
       u.`name` AS user_name, o.nombre AS oficina_name, lin.created_at AS 'checkin_ts', lout.created_at AS 'checkout_ts',
       timestampdiff(SECOND, lin.created_at, lout.created_at) AS dur_segundos, SEC_TO_TIME(timestampdiff(SECOND, lin.created_at, lout.created_at)) AS duracion,
       o.store, (v.id_encuesta is not null and v.encuesta = 0) as reporte_pendiente, v.id_encuesta, v.reporte_edit_at, v.in_cancelacion, ct.tipo_cancelacion, aa.cliente,
       (aa.deleted_at is not null) as deleted, CASE WHEN l.id IS NULL THEN 0 ELSE 1 END AS has_lead
FROM actividades v JOIN subtipo_actividades sta
    ON v.id_subtipo_actividad = sta.id
JOIN tipo_actividades ta
    ON sta.id_tipo_actividad = ta.id
JOIN vw_oficina o
    ON v.co_oficina=o.co_oficina
JOIN users u
    ON u.id = v.id_user
JOIN locations lin
    ON lin.id = v.id_location_in
left JOIN locations lout
    ON lout.id = v.id_location_out
left join cancelacion_tipos ct
    on v.id_tipo_cancelacion = ct.id
JOIN actividad_asignadas aa
    ON v.id_actividad_asignada = aa.id
LEFT JOIN leads l ON l.id_actividad = v.id
WHERE 1
