DELIMITER $$
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`item_getInfo`(IN `vBarcode` VARCHAR(22), IN `vWarehouseFk` INT)
BEGIN
/**
 * Devuelve información relativa al item correspondiente del vBarcode pasado
 *
 * @param vBarcode código relativo al ítem
 * @param vWarehouseFk código del almacén
 */
	DECLARE vCacheVisibleFk INT;
	DECLARE vCacheAvailableFk INT;
	DECLARE vVisibleItemShelving INT;
	DECLARE vItemFk INT;

	CALL cache.visible_refresh(vCacheVisibleFk, FALSE, vWarehouseFk);
	CALL cache.available_refresh(vCacheAvailableFk, FALSE, vWarehouseFk, util.VN_CURDATE());
	CALL buyUltimate(vWarehouseFk, util.VN_CURDATE());

	SELECT barcodeToItem(vBarcode) INTO vItemFk;
	SELECT SUM(visible) INTO vVisibleItemShelving
		FROM itemShelvingStock
		WHERE itemFk = vItemFk
			AND warehouseFk = vWarehouseFk;

	SELECT i.id,
			i.longName,
			i.tag5,
			i.value5,
			i.tag6,
			i.value6,
			i.tag7,
			i.value7,
			i.image,
			i.`size`,
			i.stems,
			i.category,
			i.minimum `min`,
			p.name producer,
			o.code origin,
			v.visible - IFNULL(vVisibleItemShelving, 0) unlocated,
			a.available,
			vVisibleItemShelving,
			v.visible,
			b.`grouping`,
			b.packing,
			CONCAT('http:', ic.url, '/catalog/200x200/', i.image) urlImage200,
			CONCAT('http:', ic.url, '/catalog/1600x900/', i.image) urlImage,
			i.itemPackingTypeFk,
			i.comment reference,
			u.name buyer,
			IFNULL(b.buyingValue, 0) +
				IFNULL(b.freightValue, 0) +
				IFNULL(b.comissionValue, 0) +
				IFNULL(b.packageValue, 0) itemCost
		FROM item i
			LEFT JOIN producer p ON p.id = i.producerFk
			LEFT JOIN origin o ON o.id = i.originFk
			LEFT JOIN cache.visible v ON v.calc_id = vCacheVisibleFk
				AND v.item_id = i.id
			LEFT JOIN cache.available a ON a.calc_id = vCacheAvailableFk
				AND a.item_id = i.id
			LEFT JOIN tmp.buyUltimate bu ON bu.itemFk = i.id
			LEFT JOIN buy b ON b.id = bu.buyFk
				AND bu.warehouseFk = vWarehouseFk
			JOIN itemType it ON it.id = i.typeFk
			JOIN account.user u ON u.id = it.workerFk
            JOIN hedera.imageConfig ic
		WHERE i.id = vItemFk;

	DROP TEMPORARY TABLE tmp.buyUltimate;
END$$
DELIMITER ;
