DELIMITER $$
CREATE OR REPLACE DEFINER=`vn-admin`@`localhost` PROCEDURE `vn`.`sale_getFromTicketOrCollection`(vParam INT)
BEGIN
/**
 * Visualizar lineas de la tabla sale a través del parámetro vParam que puede
 * ser ticket o collection.
 *
 * @param vParam Identificador de ticket o collection
 */
DECLARE vIsCollection BOOL;

	SELECT COUNT(*) INTO vIsCollection
		FROM collection c
		WHERE c.id = vParam;

	DROP TEMPORARY TABLE IF EXISTS tmp.ticket;
	DROP TEMPORARY TABLE IF EXISTS tmp.ticket2;

	IF vIsCollection THEN
		CREATE TEMPORARY TABLE tmp.ticket
			(INDEX (id))
			ENGINE = MEMORY
			SELECT tc.ticketFk id
				FROM ticketCollection tc
					JOIN ticket t ON t.id = tc.ticketFk
				WHERE tc.collectionFk = vParam;

		CREATE TEMPORARY TABLE tmp.ticket2
			(INDEX (id))
			ENGINE = MEMORY
			SELECT tc.ticketFk id
				FROM ticketCollection tc
					JOIN ticket t ON t.id = tc.ticketFk
				WHERE tc.collectionFk = vParam;
	ELSE
		CREATE TEMPORARY TABLE tmp.ticket
			(INDEX (id))
			ENGINE = MEMORY
			SELECT vParam id;

		CREATE TEMPORARY TABLE tmp.ticket2
			(INDEX (id))
			ENGINE = MEMORY
			SELECT vParam id;
	END IF;

	SELECT s.ticketFk,
			sgd.saleGroupFk,
			s.id saleFk,
			s.itemFk,
			s.quantity,
			i.longName,
			i.size,
			s.reserved,
			MAX(IF(st.semaphore <=> 1, TRUE, FALSE)) isPreviousPrepared,
			MAX(IF(st.semaphore <=> 2, TRUE, FALSE)) isPrepared,
			MAX(IF(st.semaphore <=> 3, TRUE, FALSE)) isControlled,
			MAX(IF(st.semaphore <=> 4, TRUE, FALSE)) isPreControlled,
			ic.color,
			ip.productor,
			s.discount,
			s.price,
			i.stems,
			i.category,
			o.code origin,
			tt.clientFk,
			s.originalQuantity,
			TRIM(CONCAT(LPAD(s.concept,30,' '), ' ',
						RPAD(IFNULL(i.size,''),5,' '))) line1,
			'' cel1,
			TRIM(CONCAT(LPAD(IFNULL(ip.productor,''),30,' '), ' ',LPAD(IFNULL(o.code,''),4,' '))) line2, 
			IF(s.quantity MOD IFNULL(b.packing,s.quantity + 1),
				CONCAT(CAST(s.quantity/IFNULL(b.`grouping`,1) AS DECIMAL(10,0)),' x ',IFNULL(b.`grouping`,1)),
				CONCAT(CAST(s.quantity / IFNULL(b.packing,1) AS DECIMAL(10,0)),' pack de ',IFNULL(b.packing,1))) cel2,
			TRIM(CONCAT(ic.color)) line3,
			p.code cel3,
			s.isAdded,
			IF(c.workerFk IS NULL, getUser(), c.workerFk) workerFk,
			IF(SUM(iss.quantity) IS NULL, 0, SUM(iss.quantity)) pickedQuantity,
			MIN(iss.created) picked,
			IF(sm.id, TRUE, FALSE) hasMistake,
			sg.sectorFk,
			b.packing,
			b.grouping,
			o.code
		FROM tmp.ticket t
			JOIN sale s ON s.ticketFk = t.id
			JOIN ticket tt ON tt.id = t.id
			LEFT JOIN cache.last_buy lb ON lb.item_id = s.itemFk AND lb.warehouse_id = tt.warehouseFk
			LEFT JOIN buy b ON b.id = lb.buy_id 
			JOIN item i ON i.id = s.itemFk
			LEFT JOIN ticketCollection tc ON tc.ticketFk = t.id
			LEFT JOIN collection c ON c.id = tc.collectionFk
			LEFT JOIN (SELECT sub.saleFk, sub.isChecked, sub.stateFk, sub.originalQuantity 
							FROM (SELECT DISTINCT st.id, st.saleFk, st.isChecked, st.stateFk, st.originalQuantity 
										FROM tmp.ticket2 t
											JOIN sale s ON s.ticketFk = t.id
											JOIN saleTracking st ON st.saleFk = s.id
										ORDER BY st.id DESC
										LIMIT 10000000000000000000) sub
							GROUP BY sub.saleFk, sub.stateFK) sub2 ON sub2.saleFk = s.id AND sub2.isChecked = 1
			LEFT JOIN itemShelvingSale iss ON iss.saleFk = s.id
			LEFT JOIN state st ON st.id = sub2.stateFk
			LEFT JOIN itemColor ic ON ic.itemFk = s.itemFk
			LEFT JOIN itemProductor ip ON ip.itemFk = s.itemFk
			LEFT JOIN origin o ON o.id = i.originFk
			LEFT JOIN saleGroupDetail sgd ON sgd.saleFk = s.id 
			LEFT JOIN saleGroup sg ON sg.id = sgd.saleGroupFk
			LEFT JOIN parking p ON p.id = sg.parkingFk
			LEFT JOIN saleMistake sm ON sm.saleFk = s.id
		GROUP BY s.id;

	DROP TEMPORARY TABLE
		tmp.ticket,
		tmp.ticket2;
END$$
DELIMITER ;
