DELIMITER $$
CREATE OR REPLACE DEFINER=`vn`@`localhost` PROCEDURE `vn`.`stockTraslation`(
	vDated DATE
)
BEGIN
/**
 * Calcula el stock del almacén de subasta desde FechaInventario hasta vDated
 * sin tener en cuenta las salidas del mismo dia vDated
 * para ver el transporte a reservar
 *
 * @param vDated Fecha hasta la cual calcula el stock
 * @return tmp.item
 */
	DECLARE vAuctionWarehouseFk INT;

	SELECT warehouseFk INTO vAuctionWarehouseFk
		FROM auctionConfig;

	CREATE OR REPLACE TEMPORARY TABLE tmp.item
		(UNIQUE INDEX i USING HASH (itemFk))
		ENGINE = MEMORY
			SELECT itemFk, SUM(quantity) quantity
				FROM itemEntryIn
				WHERE landed = vDated
					AND vDated >= util.VN_CURDATE()
					AND warehouseInFk = vAuctionWarehouseFk
					AND NOT isVirtualStock
				GROUP BY itemFk
				HAVING quantity;

	CALL `cache`.stock_refresh(FALSE);

	INSERT INTO tmp.item (itemFk, quantity)
		SELECT item_id, amount
			FROM `cache`.stock
			WHERE warehouse_id = vAuctionWarehouseFk
			ON DUPLICATE KEY UPDATE quantity = tmp.item.quantity + VALUES(quantity);

	CALL buy_getUltimate(NULL, vAuctionWarehouseFk, vDated);
END$$
DELIMITER ;
