DELIMITER $$
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`reportLabelCollection_get`(
	vParam INT,
	vLabelCount INT
)
BEGIN
/**
 * Obtains tag data from a ticket or collection.
 *
 * @param vParam Ticket/Collection from which data is to be taken.
 * @param vLabelCount number of times the collection has been printed.
 */
	DECLARE vIsCollection BOOL;

	SELECT IF(id, TRUE, FALSE) INTO vIsCollection
		FROM vn.collection c
		WHERE id = vParam;

	SELECT c.itemPackingTypeFk,
			CONCAT(tc.collectionFk, ' ', LEFT(cc.code, 4)) color,
			CONCAT(tc.collectionFk, ' ', SUBSTRING('ABCDEFGH',tc.wagon, 1), '-', tc.level) levelV,
			tc.ticketFk,
			LEFT(COALESCE(rs.description, zo.name, am.name),12) agencyDescription,
			am.name,
			t.clientFk,
			CONCAT(CAST(SUM(sv.volume) AS DECIMAL(5, 2)), 'm³') m3 ,
			CAST(IF(ic.code = 'plant', CONCAT(MAX(i.size), ' cm'), COUNT(*)) AS CHAR) size,
			w.code workerCode,
			IF(vLabelCount IS NULL, tt.labelCount, vLabelCount) labelCount,
			IF(HOUR(t.shipped), TIME(t.shipped), TIME(zo.hour)) agencyHour,
			t.shipped,
			COUNT(*) lineCount,
			t.nickName
		FROM ticket t
			JOIN ticketCollection tc ON tc.ticketFk = t.id
			JOIN collection c ON c.id = tc.collectionFk
			LEFT JOIN collectionColors cc ON cc.shelve = tc.level
				AND cc.wagon = tc.wagon
				AND cc.trainFk = c.trainFk
			JOIN sale s ON s.ticketFk = t.id
			LEFT JOIN saleVolume sv ON sv.saleFk = s.id
			JOIN item i ON i.id = s.itemFk
			JOIN itemType it ON it.id = i.typeFk
			JOIN itemCategory ic ON ic.id = it.categoryFk
			JOIN worker w ON w.id = c.workerFk
			JOIN agencyMode am ON am.id = t.agencyModeFk
			LEFT JOIN ticketTrolley tt ON tt.ticket = t.id
			LEFT JOIN zone zo ON t.zoneFk = zo.id
			LEFT JOIN routesMonitor rm ON rm.routeFk = t.routeFk
			LEFT JOIN roadmapStop rs ON rs.id = rm.expeditionTruckFk
		WHERE IF(vIsCollection, tc.collectionFk = vParam, tc.ticketFk = vParam)
		GROUP BY t.id
		ORDER BY cc.code;
END$$
DELIMITER ;
