DELIMITER $$
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn2008`.`createBouquet`(
	vName VARCHAR(50), 
	vType int, 
	vSize int, 
	vColour VARCHAR(5),
	OUT vItem INT(11)
)
BEGIN
 
	DECLARE vTaxClass VARCHAR(1);
	DECLARE vIntrastatFk INT(11);
    
	-- COMPROBAMOS SI EXISTE UN ARTÍCULO IGUAL.
	SELECT Id_Article INTO vItem 
		FROM Articles
	WHERE (Id_Article BETWEEN 161000 AND 169000)
			AND Article LIKE CONCAT('%', vName)
			AND tipo_id = vType
			AND Medida = vSize
			AND Color = vColour
	LIMIT 1;
		
	 -- SI NO EXISTE CREAMOS UNO NUEVO
	 IF vItem IS NULL THEN
		 
		-- OBTENER EL ID ENTRE 161000 Y 169000 DEL SIGUIENTE ARTÍCULO
		SELECT (IF(MAX(Id_Article) IS NULL, 161000, (MAX(Id_Article) + 1))) INTO vItem FROM Articles WHERE Id_Article BETWEEN 161000 AND 169000;
	 
		-- INSERTAR EL ARTÍCULO
		INSERT INTO vn.item (id, `name`, typeFk, size, inkFk, originFk, intrastatFk) 
			VALUES ( 
				vItem,
				vName, 
				vType, 
				vSize, 
				vColour, 
				1,
                06031970
			);
            
		SELECT id INTO vTaxClass FROM vn.taxClass WHERE `code` = 'G';
        SELECT id INTO vIntrastatFk FROM vn.intrastat 
			WHERE description LIKE 'Flores,follaje y frutos artificiales: otras%';
            
		UPDATE vn.itemTaxCountry SET taxClassFk = vTaxClass WHERE itemFk = LAST_INSERT_ID();
	END IF;
 
END$$
DELIMITER ;
