@query_bucode es el codigo de unidad de negocio, puede contener el valor de "all" o 

LIPU MONTERREY		lip_mty
GRUPOSETTEPI MONTERREY		set_mty
LIPU GUADALAJARA		lip_gdl
SETTEPI ORIENTE HERMOSILLO		set_hmo
SETTEPI ORIENTE SILAO		set_slo
GRUPOSETTEPI TIJUANA		set_tij
LIPU VALLE DE MEXICO		lip_vdm
SETTEPI ORIENTE PUEBLA		set_pue
LIPU CANCUN		lip_can
SETTEPI ORIENTE SALTILLO		set_sal
ETEP SAN LUIS POTOSI		utp_slp
ETEP CDMX		utp_mex
LIPU SALVATIERRA		lip_cel
ETEP QUERETARO		utp_qro
MEZA CDMX		mza_mex
LIPU SALTILLO		lip_sal
LIPU AGUASCALIENTES		set_ags
LIPU LAZARO CARDENAS		lip_mch
LIPU		set_slp
LIPU MERIDA		lip_mid
ETEP CELAYA		utp_cel
LIPU VILLAHERMOSA		lip_vil
SETTEPI CIUDAD JUAREZ		set_jua
LIPU COLIMA		lip_col
LIPU VALLARTA		lip_vll
SETTEPI LOS CABOS		set_cab
SETTEPI GARCIA		set_gar
SETTEPI SAN QUINTIN		set_qui	
LIPU QUERETARO		lip_qro




start_date = es la fecha de inicio en formato 2025-05-01
end_date = es la fecha de inicio en formato 2025-05-01

CONCAT("WITH tsums AS (
		SELECT g.id AS group_id,COUNT(t.id) AS trip_count FROM tracker.trips AS t
		INNER JOIN tracker.routes AS r ON r.id = t.route
		INNER JOIN tracker.groups AS g ON g.id = r.group
		INNER JOIN tracker.groups_billing_vars AS gbv ON gbv.group_id = g.id
        INNER JOIN business_units AS bu ON bu.id = g.business_unit ",@query_bucode,"
		WHERE t.start_date BETWEEN '",start_date,"' AND '",end_date,"' AND gbv.billing_type IS NOT NULL AND gbv.billing_type LIKE 'fixed%' AND t.status IN (5,6,7,8)
		GROUP BY g.id
	),
	gbvt AS (
		SELECT g.id AS group_id,
			CALCULATE_INTERVAL_FRACTIONS_VALUE(gbv.fixed_billing_interval,'",start_date,"','",end_date,"',SUM(gbfp.car_amount * gbfp.car_price),true) AS fixed_price
			FROM `tracker`.`groups` AS g
		LEFT JOIN tracker.groups_billing_fixed_prices AS gbfp ON gbfp.group_id = g.id AND gbfp.status = 1
        INNER JOIN tracker.groups_billing_vars AS gbv ON gbv.group_id = g.id -- AND gbv.billing_type LIKE 'fixed%'
        INNER JOIN business_units AS bu ON bu.id = g.business_unit ",@query_bucode,"
		WHERE gbv.billing_type IS NOT NULL
		GROUP BY g.id
	)
	SELECT 
			t.`id`,
			GENERATE_SERVICE_ID(g.plant_key, r.sequential_id, r.shift, r.daytimes, t.start_time, t.options) AS `id_servicio`,
            bu.`description` AS business_unit,
			r.`description` AS des,
            r.`route_type` AS `Tipo de Viaje`,
            LPAD(r.sequential_id, 4, '0') AS `ID Ruta`,
            JSON_UNQUOTE(t.options->'$.comment') as desc_adicional,
			t.`car`,
			t.`status`,
			r.`shift`,
			g.`description` AS `group`,
			t.`start_date`,
			t.`start_time`,
			te.`start_eta`,
			te.`start_status`,
			t.`end_date`,
			t.`end_time`,
			te.`end_eta`,
			te.`end_status`,
            te.`status` AS `record_quality`,
			IFNULL(r.km_set, '-') AS `km cotizado`,
            IFNULL(r.km_average, '-') AS `km promedio`,
			IFNULL(tb.end_km - tb.start_km, '-') AS km_calc,
			IFNULL(tb.end_km_vac - tb.start_km_vac, '-') AS `km_calc + vac`,
            IFNULL((tb.start_km - tb.start_km_vac), '-') AS km_vac_inicial,
			IFNULL((tb.end_km_vac - tb.end_km), '-') AS km_vac_final,
			d.`name` AS driver,
			d.`worker_id`,
            tb.`driver_name` AS `operador`,
			tb.`driver_worker_id` AS `no. de nomina`,
			IFNULL(ual.user,'SISTEMA') AS 'usuario',
            IFNULL(ual.action_timestamp,'AUTOMÁTICO') AS 'Fecha de creación (Tiempo de Servidor)',
            IF(ual.action_timestamp IS NOT NULL,
				IF((ual.action_timestamp + INTERVAL bu.tsft HOUR) BETWEEN TIMESTAMP(start_date,start_time) AND TIMESTAMP(end_date,end_time),
				'DURANTE VIAJE', 
				IF(TIMESTAMP(end_date, end_time) < (ual.action_timestamp + INTERVAL bu.tsft HOUR), 
					'DESPUÉS DE VIAJE' , 
                    'ANTES DE VIAJE') ), 
            'AUTOMÁTICO' ) AS 'momento',
            WEEK(t.start_date) + 1 AS `Sem Via`,
            IF( t.car IS NULL,
				'Sin Veh',
				IF(
					te.start_status = te.end_status OR te.start_status IS NULL,
					GENERATE_TRIP_CALIBRATION_LABEL(te.start_status),
					CONCAT(GENERATE_TRIP_CALIBRATION_LABEL(te.start_status),',',GENERATE_TRIP_CALIBRATION_LABEL(te.end_status))
				) 
			) AS `Sol Viaje`,
			GENERATE_TRIP_DIAGNOSIS(TIMESTAMP(t.start_date, te.start_eta), TIMESTAMP(t.start_date, te.start_eta), TIMESTAMP(t.end_date, t.end_time), TIMESTAMP(DATE(te.eta_time), te.end_eta)) AS `Diagnostico Viaje`,
            IF( gbv.billing_type = 'fixed','N/A', IFNULL(tb.price, '-') ) AS `$ Viaje`,
			IF( gbv.billing_type LIKE 'fixed%', IFNULL( ROUND(gbvt.fixed_price / tsums.trip_count, 2),'-' ), 'N/A' ) AS `$ Viaje Fijo`,
			CASE gbv.billing_type 
				WHEN 'fixed' THEN 'Fijo'
                WHEN 'km_var' THEN 'Variable por KM'
                WHEN 'route_var' THEN 'Variable por Ruta'
                WHEN 'fixed_km_var' THEN 'Fijo más Variable por KM'
                WHEN 'fixed_route_var' THEN 'Fijo más Variable por Ruta'
                WHEN 'stagged' THEN 'Escalonado'
                ELSE 'Sin asignar'
			END
            AS `Tipo de Facturación`,
            CASE gbv.fixed_billing_interval 
				WHEN 'weekly' THEN 'Semanal'
                WHEN 'biweekly' THEN 'Catorcena'
                WHEN 'monthly' THEN 'Mensual'
				ELSE 'Sin asignar'
			END
            AS `Periodo de Facturación`,
            IFNULL(ual.action_timestamp + INTERVAL bu.tsft HOUR, 'AUTOMÁTICO') AS 'Fecha de creación (Tiempo de Unidad de Negocios)',
            IFNULL(trop.passenger_count, '-') AS `Aforo`
		FROM `tracker`.`trips` AS t USE INDEX(`start_datetime`)
		INNER JOIN `tracker`.`routes` AS r ON r.`id` = t.`route`
		INNER JOIN `tracker`.`groups` AS g ON g.`id` = r.`group`
		INNER JOIN `tracker`.`business_units` AS bu ON bu.`id` = g.`business_unit`",@query_bucode,"
		INNER JOIN `tracker`.`trips_billing` AS tb ON tb.trip = t.id
        LEFT JOIN `tracker`.`trips_operation` AS trop ON trop.trip_id = t.id
        LEFT JOIN `tracker`.`trips_eta` AS te ON te.`trip` = t.`id`
        LEFT JOIN `tracker`.`groups_billing_vars` AS gbv ON gbv.group_id = g.id
		LEFT JOIN gbvt ON gbvt.group_id = g.id
		LEFT JOIN tsums ON tsums.group_id = g.id
		LEFT JOIN `tracker`.`drivers` AS d ON d.driver_id = tb.end_driver
		LEFT JOIN `tracker`.`user_actions_log` AS ual ON ual.table = 'trips' AND ual.action = 'insert' AND t.id = ual.record_id
		WHERE t.status IN (5,6,7,8) AND t.`start_date` BETWEEN '",start_date,"' AND '",end_date,"'
		ORDER BY t.`start_date`, t.`start_time` ASC;