DELIMITER $$
CREATE OR REPLACE DEFINER=`vn`@`localhost` PROCEDURE `vn`.`itemShelving_getItemDetails`(
	vBarcodeItem INT, 
	vShelvingCode VARCHAR(10) 
)
BEGIN
/**
 * Obtiene el precio y visible de un item
 *
 * @param vBarcodeItem barcode de artículo
 * @param vShelvingCode Ubicación actual del artículo
 */
	DECLARE vIsItem BOOL;
	DECLARE vBuyFk INT;
	DECLARE vWarehouseFk INT;

	SELECT COUNT(*) > 0 INTO vIsItem 
		FROM item 
		WHERE id = vBarcodeItem;

	IF vIsItem THEN 
		SELECT warehouseFk INTO vWarehouseFk
			FROM operator
			WHERE workerFk = account.myUser_getId();

		CALL buy_getUltimate(vBarcodeItem, vWarehouseFk, util.VN_CURDATE());

		SELECT buyFk INTO vBuyFk
			FROM  tmp.buyUltimate
			WHERE itemFk = vBarcodeItem
				AND warehouseFk = vWarehouseFk;

		DELETE FROM tmp.buyUltimate;		
	ELSE
		SELECT vBarcodeItem INTO vBuyFk;
	END IF;
		
	WITH visible AS(
		SELECT itemFk,
				IFNULL(buyingValue, 0) +
					IFNULL(freightValue, 0) +
					IFNULL(comissionValue, 0) +
					IFNULL(packageValue, 0) itemCost
			FROM vn.buy b
			WHERE b.id = vBuyFk
	) SELECT v.itemFk,
			vShelvingCode,
			v.itemCost,
			SUM(ish.visible) visible
		FROM vn.itemShelving ish
			JOIN vn.shelving sh ON sh.id = ish.shelvingFK
			JOIN visible v
		WHERE sh.code = vShelvingCode COLLATE utf8mb3_general_ci
			AND ish.itemFk = v.itemFk;
END$$
DELIMITER ;