DELIMITER $$
CREATE OR REPLACE DEFINER=`vn`@`localhost` PROCEDURE `edi`.`item_getNewByEkt`(vEktFk INT, OUT vItemFk INT)
BEGIN

/**
 *  Devuelve un número nuevo de item a partir de un registro de la tabla edi.ekt
 * 
 *  @param vEktFk Identificador de la tabla edi.ekt
 */

	START TRANSACTION;

		SELECT MIN(id) id INTO vItemFk
			FROM edi.item_free;

		DELETE FROM edi.item_free 
			WHERE id = vItemFk;
	
	COMMIT;
	
	IF ISNULL(vItemFk) THEN
		SELECT MAX(i.id) + 1
			INTO vItemFk
			FROM vn.item i;
	END IF;
	
	INSERT INTO vn.item(id,
			`name`,
			longName,
			subName,
			expenseFk,
			typeFk,
			intrastatFk,
			originFk,
			numberOfItemsPerCask,
			embalageCode,
			quality,
			isFloramondo
		)
		SELECT vItemFk,
			e.item,
			e.item,
			e.item,
			igto.expenseFk,
			igto.itemTypeFk,
			igto.intrastatFk,
			igto.originFk,
			e.pac,
			least(IF((e.package = 800),((e.package * 10) + e.pac), e.package), IFNULL(idt.bucket_id, '999')) packageFk,
			e.cat,
			TRUE
		FROM edi.ekt e 
			JOIN edi.item i ON i.id = e.`ref` 
			JOIN edi.item_groupToOffer igto ON igto.group_code = i.group_id 
			LEFT JOIN edi.item_defaultType idt ON idt.item_id = e.`ref` 
		WHERE e.id = vEktFk;

	SET @isTriggerDisabled = TRUE;
	
	INSERT INTO vn.itemTag(itemFk, tagFk, value, priority)
		SELECT vItemFk, t.id , e.item , 1	
			FROM edi.ekt e 
				JOIN vn.tag t ON t.`name` = 'Producto'
			WHERE e.id = vEktFk
				AND NOT ISNULL(e.item);

	INSERT INTO vn.itemTag(itemFk, tagFk, value, priority)
		SELECT vItemFk, t.id , e.cat , 3	
			FROM edi.ekt e 
				JOIN vn.tag t ON t.`name` = 'Calidad'
			WHERE e.id = vEktFk
				AND NOT ISNULL(e.cat);
	
	INSERT INTO vn.itemTag(itemFk, tagFk, value, priority)
		SELECT vItemFk, t.id , s.company_name , 4	
			FROM edi.ekt e 
				JOIN edi.supplier s ON s.supplier_id = e.pro
				JOIN vn.tag t ON t.`name` = 'Productor'
			WHERE e.id = vEktFk
				AND NOT ISNULL(s.company_name);

	INSERT INTO vn.itemTag(itemFk, tagFk, value, priority)
		SELECT vItemFk, t.id , e.s1, 5	
			FROM edi.ekt e
				LEFT JOIN edi.item_feature eif ON eif.item_id = e.`ref` 
					AND eif.presentation_order = 1 
					AND eif.expiry_date IS NULL
				LEFT JOIN edi.`value` ev ON ev.type_id = eif.feature 
					AND e.s1 = ev.type_value
				JOIN vn.tag t ON t.ediTypeFk = eif.feature
			WHERE e.id = vEktFk
				AND NOT ISNULL(e.s1);
	
	INSERT INTO vn.itemTag(itemFk, tagFk, value, priority)
		SELECT vItemFk, t.id , e.s2, 6	
			FROM edi.ekt e
				LEFT JOIN edi.item_feature eif ON eif.item_id = e.`ref` 
					AND eif.presentation_order = 2 
					AND eif.expiry_date IS NULL
				LEFT JOIN edi.`value` ev ON ev.type_id = eif.feature 
					AND e.s2 = ev.type_value
				JOIN vn.tag t ON t.ediTypeFk = eif.feature
			WHERE e.id = vEktFk
				AND NOT ISNULL(e.s2);
		
	INSERT INTO vn.itemTag(itemFk, tagFk, value, priority)
		SELECT vItemFk, t.id , e.s3, 7	
			FROM edi.ekt e
				LEFT JOIN edi.item_feature eif ON eif.item_id = e.`ref` 
					AND eif.presentation_order = 3 
					AND eif.expiry_date IS NULL
				LEFT JOIN edi.`value` ev ON ev.type_id = eif.feature 
					AND e.s3 = ev.type_value
				JOIN vn.tag t ON t.ediTypeFk = eif.feature
			WHERE e.id = vEktFk
				AND NOT ISNULL(e.s3);
	
	INSERT INTO vn.itemTag(itemFk, tagFk, value, priority)
		SELECT vItemFk, t.id , e.s4, 8	
			FROM edi.ekt e
				LEFT JOIN edi.item_feature eif ON eif.item_id = e.`ref` 
					AND eif.presentation_order = 4
					AND eif.expiry_date IS NULL
				LEFT JOIN edi.`value` ev ON ev.type_id = eif.feature 
					AND e.s4 = ev.type_value
				JOIN vn.tag t ON t.ediTypeFk = eif.feature
			WHERE e.id = vEktFk
				AND NOT ISNULL(e.s4);
				
	INSERT INTO vn.itemTag(itemFk, tagFk, value, priority)
		SELECT vItemFk, t.id , e.s5, 9	
			FROM edi.ekt e
				LEFT JOIN edi.item_feature eif ON eif.item_id = e.`ref` 
					AND eif.presentation_order = 5
					AND eif.expiry_date IS NULL
				LEFT JOIN edi.`value` ev ON ev.type_id = eif.feature 
					AND e.s5 = ev.type_value
				JOIN vn.tag t ON t.ediTypeFk = eif.feature
			WHERE e.id = vEktFk
				AND NOT ISNULL(e.s5);

	INSERT INTO vn.itemTag(itemFk, tagFk, value, priority)
		SELECT vItemFk, t.id , e.s6, 10	
			FROM edi.ekt e
				LEFT JOIN edi.item_feature eif ON eif.item_id = e.`ref` 
					AND eif.presentation_order = 6
					AND eif.expiry_date IS NULL
				LEFT JOIN edi.`value` ev ON ev.type_id = eif.feature 
					AND e.s6 = ev.type_value
				JOIN vn.tag t ON t.ediTypeFk = eif.feature
			WHERE e.id = vEktFk
				AND NOT ISNULL(e.s6);

	INSERT INTO vn.itemTag(itemFk, tagFk, value, priority)
		SELECT  vItemFk, t.id, IFNULL(ink.name, ik.color), 11	
			FROM edi.ekt e				
				JOIN vn.tag t ON t.`name` = 'Color'
				LEFT JOIN edi.feature f ON f.item_id = e.`ref` 
				LEFT JOIN edi.`type` tp ON tp.type_id = f.feature_type_id AND tp.`description` = 'Hoofdkleur 1'
				LEFT JOIN vn.ink ON ink.dutchCode = f.feature_value
				LEFT JOIN vn.itemInk ik ON ik.longName = e.item
			WHERE e.id = vEktFk
				AND ( ink.name IS NOT NULL 
				OR ik.color IS NOT NULL)
			LIMIT 1;

	DROP TABLE IF EXISTS tmp.item;

	CREATE TABLE tmp.item
		(PRIMARY KEY (id))
		SELECT vItemFk id ;

	CALL vn.item_refreshTags();

	SET @isTriggerDisabled = FALSE;

	INSERT IGNORE INTO vn.itemImageQueue(itemFk, url)
        SELECT vItemFk, sr.PictureReference
            FROM edi.ekt e
                JOIN edi.supplyResponse sr ON e.ref = sr.Item_ArticleCode
            WHERE e.id = vEktFk
				AND NOT sr.PictureReference IS NULL
            ORDER BY sr.created DESC
            LIMIT 1;
END$$
DELIMITER ;
