DELIMITER $$
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`itemShelvingSale_reserveByCollection`(
	vCollectionFk INT(11)
)
BEGIN
/**
 * Reserva cantidades con ubicaciones para el contenido de una colección
 *
 * @param vCollectionFk Identificador de collection
 */
	CREATE OR REPLACE TEMPORARY TABLE tmp.sale
		(INDEX(saleFk))
		ENGINE = MEMORY
		SELECT s.id saleFk, NULL userFk
			FROM ticketCollection tc
				JOIN sale s ON s.ticketFk = tc.ticketFk
				LEFT JOIN (
					SELECT DISTINCT saleFk
						FROM saleTracking st
							JOIN state s ON s.id = st.stateFk
						WHERE st.isChecked
							AND s.semaphore = 1)st ON st.saleFk = s.id
			WHERE tc.collectionFk = vCollectionFk
				AND st.saleFk IS NULL
				AND NOT s.isPicked;

	CALL itemShelvingSale_reserve();
END$$
DELIMITER ;