CREATE DEFINER=`admin`@`localhost` PROCEDURE `get_trips_drivers_repse2`(IN bucode VARCHAR (45), IN sdate DATETIME, IN edate DATETIME)
BEGIN

	SET @query_gcode = "";
	-- IF gcode != '' THEN
	--	SET @query_gcode = CONCAT(" AND g.code = '", gcode, "' ");
	-- END IF;
    
    IF bucode = 'all' THEN
		SET @query_bucode = "";
	ELSE 
		SET @query_bucode = CONCAT(" AND bu.code = '", bucode, "'");
	END IF;

					
	SET @query = CONCAT("SELECT 
							t.`id`,
							bu.`description` AS `Unidad de negocio`,
                            CASE 
                            WHEN  bu.`administrative_description` = 'LIPU LAZARO CARDENAS' THEN 'LIPU LAZARO'
                            WHEN  bu.`administrative_description` = 'LIPU VALLE DE MEXICO' THEN 'LIPU VALLE MEXICO'
                            WHEN  bu.`administrative_description` = 'SETTEPI CIUDAD JUAREZ' THEN 'GRUPOSETTEPI CD JUAREZ'
							WHEN  bu.`administrative_description` = 'UTEP CELAYA' THEN 'LIPU SALVATIERRA'
                            WHEN  bu.`administrative_description` = 'SETTEPI SILAO' THEN 'ETEP SILAO'
							WHEN  bu.`administrative_description` = 'LIPU GUADALAJARA' THEN 'ETEP GUADALAJARA'
                            ELSE  bu.`administrative_description`
                            END AS `UN (nombre administrativo)`,
                            g.`description` AS `Cliente`,
							r.`description` AS `Ruta`,
                            r.`shift` AS `Turno`,
                            r.`route_type` AS `Tipo de Viaje`,
							t.`car` AS `Unidad`,
							CONCAT (t.`start_date`, ' ',t.`start_time`) AS 'Fecha Inicio',
							CONCAT (t.`end_date`, ' ',t.`end_time`) AS 'Fecha Fin',
                            IFNULL(r.km_set, '-') AS `km cotizado`,
							IFNULL(r.km_average, '-') AS `km promedio`,
                            di.`name` AS `Operador`,
                            
							SUBSTRING_INDEX(di.`name`,' ',1) AS n1,
                            SUBSTRING_INDEX(SUBSTRING_INDEX(di.`name`, ' ', 2), ' ', -1) AS n2,
                             SUBSTRING_INDEX(di.`name`,' ',-1) AS n3,
                            
                            tb.`driver_worker_id` AS `Nomina`,
                            di.`RFC`,
                            di.`CURP`,
                            di.`NSS`,
                            di.`zone`
						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`.`business_units` AS bu ON bu.`id` = g.`business_unit`
						INNER JOIN `tracker`.`trips_billing` AS tb ON tb.trip = t.id AND tb.end_driver IS NOT NULL AND tb.end_driver != ''
                        INNER JOIN `tracker`.`drivers_info` AS di ON di.worker_id = tb.driver_worker_id
						WHERE t.`status` != 9 ", @query_bucode, @query_gcode, "
						AND t.`start_date` >= '", sdate, "'
						AND t.`start_date` <= '", edate, "'
						ORDER BY t.`start_date`, t.`start_time` ASC;");
		
	PREPARE stmt FROM @query;
	EXECUTE stmt;
	DEALLOCATE PREPARE stmt;
END