DELIMITER $$
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`buyUltimateFromInterval`(vWarehouseFk SMALLINT, vStarted DATE, vEnded DATE)
BEGIN
/**
 * Calcula las últimas compras realizadas
 * desde un rango de fechas.
 *
 * @param vWarehouseFk Id del almacén si es NULL se actualizan todos
 * @param vStarted Fecha inicial
 * @param vEnded Fecha fin
 * @return tmp.buyUltimateFromInterval
 */
	IF vEnded IS NULL THEN
		SET vEnded = vStarted;
	END IF;

	IF vEnded < vStarted THEN
		SET vStarted = TIMESTAMPADD(MONTH, -1, vEnded);
	END IF;

	-- Item
	DROP TEMPORARY TABLE  IF EXISTS tmp.buyUltimateFromInterval;
	CREATE TEMPORARY TABLE tmp.buyUltimateFromInterval
		(PRIMARY KEY (itemFk, warehouseFk), INDEX(buyFk), INDEX(landed), INDEX(warehouseFk), INDEX(itemFk))
		ENGINE = MEMORY
		SELECT itemFk,
				warehouseFk,
				buyFk,
				MAX(landed) landed
			FROM (SELECT b.itemFk,
						t.warehouseInFk warehouseFk,
						b.id buyFk,
						t.landed
					FROM buy b
						JOIN entry e ON e.id = b.entryFk
						JOIN travel t ON t.id = e.travelFk
					WHERE t.landed BETWEEN vStarted AND vEnded
							AND (vWarehouseFk IS NULL OR t.warehouseInFk = vWarehouseFk)
							AND b.price2 > 0
							AND b.quantity > 0
					ORDER BY NOT b.isIgnored DESC, t.landed DESC, b.id DESC
					LIMIT 10000000000000000000) sub
			GROUP BY itemFk, warehouseFk;

	INSERT IGNORE INTO tmp.buyUltimateFromInterval(itemFk, warehouseFk, buyFk, landed)
		SELECT
				b.itemFk,
				t.warehouseInFk warehouseFk,
				b.id buyFk,
				t.landed
			FROM buy b
				JOIN entry e ON e.id = b.entryFk
				JOIN travel t ON t.id = e.travelFk
			WHERE t.landed > vEnded
				AND (vWarehouseFk IS NULL OR t.warehouseInFk = vWarehouseFk)
				AND b.price2 > 0
				AND NOT b.isIgnored
			GROUP BY itemFk, warehouseInFk;

	INSERT IGNORE INTO tmp.buyUltimateFromInterval(itemFk, warehouseFk, buyFk, landed)
		SELECT
				b.itemFk,
				t.warehouseInFk warehouseFk,
				b.id buyFk,
				t.landed
			FROM buy b
				JOIN entry e ON e.id = b.entryFk
				JOIN travel t ON t.id = e.travelFk
			WHERE t.landed BETWEEN vStarted AND vEnded
				AND (vWarehouseFk IS NULL OR t.warehouseInFk = vWarehouseFk)
				AND b.quantity = 0
			GROUP BY itemFk, warehouseInFk;

	-- ItemOriginal
	INSERT IGNORE INTO tmp.buyUltimateFromInterval(itemFk, warehouseFk, buyFk, landed)
		SELECT itemFk,
			warehouseFk,
			buyFk,
			MAX(landed) landed
		FROM (SELECT b.itemFk,
					t.warehouseInFk warehouseFk,
					b.id buyFk,
					t.landed,
					itemOriginalFk
				FROM buy b
					JOIN entry e ON e.id = b.entryFk
					JOIN travel t ON t.id = e.travelFk
				WHERE t.landed BETWEEN vStarted AND vEnded
						AND (vWarehouseFk IS NULL OR t.warehouseInFk = vWarehouseFk)
						AND b.price2 > 0
						AND NOT b.isIgnored
						AND b.quantity > 0
						AND itemOriginalFk
				ORDER BY t.landed DESC, b.id DESC
				LIMIT 10000000000000000000) sub
		GROUP BY itemOriginalFk, warehouseFk;

	INSERT IGNORE INTO tmp.buyUltimateFromInterval(itemFk, warehouseFk, buyFk, landed)
	SELECT
			b.itemFk,
			t.warehouseInFk warehouseFk,
			b.id buyFk,
			t.landed
		FROM buy b
			JOIN entry e ON e.id = b.entryFk
			JOIN travel t ON t.id = e.travelFk
		WHERE t.landed > vEnded
			AND (vWarehouseFk IS NULL OR t.warehouseInFk = vWarehouseFk)
			AND b.price2 > 0
			AND NOT b.isIgnored
			AND itemOriginalFk
		GROUP BY itemOriginalFk, warehouseInFk;

	INSERT IGNORE INTO tmp.buyUltimateFromInterval(itemFk, warehouseFk, buyFk, landed)
		SELECT
				b.itemFk,
				t.warehouseInFk warehouseFk,
				b.id buyFk,
				t.landed
			FROM buy b
				JOIN entry e ON e.id = b.entryFk
				JOIN travel t ON t.id = e.travelFk
			WHERE t.landed BETWEEN vStarted AND vEnded
				AND (vWarehouseFk IS NULL OR t.warehouseInFk = vWarehouseFk)
				AND b.quantity = 0
				AND itemOriginalFk
			GROUP BY itemOriginalFk, warehouseInFk;
END$$
DELIMITER ;
