DELIMITER $$
CREATE OR REPLACE DEFINER=`vn-admin`@`localhost` PROCEDURE `vn`.`setParking`(
	vParam VARCHAR(8),
	vParkingCode VARCHAR(8)
)
BEGIN
/**
 * Aparca una colección, un ticket, un saleGroup o un shelving en un parking
 *
 * @param vParam id del ticket, colección, saleGroup o shelving
 * @param vParkingCode código del parking
 */
	DECLARE vParkingFk INT;
	DECLARE vLastWeek DATE;

	SET vParkingCode = REPLACE(vParkingCode, ' ', '');

	SELECT id INTO vParkingFk
		FROM parking
		WHERE code = vParkingCode COLLATE utf8_unicode_ci;

	IF vParkingFk IS NULL THEN
		CALL util.throw('parkingNotExist');
	END IF;

	SET vLastWeek = util.VN_CURDATE() - INTERVAL 1 WEEK;

	-- Comprobamos si es una prep. previa, ticket, colección o shelving
	IF (SELECT TRUE FROM saleGroup WHERE id = vParam) THEN
		CALL saleGroup_setParking(vParam, vParkingFk);
	ELSEIF (SELECT TRUE FROM ticket	WHERE id = vParam AND shipped >= vLastWeek) THEN
		CALL ticket_setParking(vParam, vParkingFk);
	ELSEIF (SELECT TRUE	FROM `collection` WHERE id = vParam AND created >= vLastWeek) THEN
		CALL collection_setParking(vParam, vParkingFk);
	ELSEIF (SELECT TRUE	FROM shelving WHERE code = vParam COLLATE utf8_unicode_ci) THEN
		CALL shelving_setParking(vParam, vParkingFk);
	ELSE
		CALL util.throw('paramNotExist');
	END IF;
END$$
DELIMITER ;
