DELIMITER $$
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`buy_getUltimateFromInterval`(
	vItemFk INT,
	vWarehouseFk SMALLINT, 
	vStarted DATE, 
	vEnded DATE
)
BEGIN
/**
 * Calcula las últimas compras realizadas
 * desde un rango de fechas.
 *
 * @param vItemFk Id del artículo
 * @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 = vEnded - INTERVAL 1 MONTH;
	END IF;

	-- Item

	CREATE OR REPLACE TEMPORARY TABLE tmp.buyUltimateFromInterval
		(PRIMARY KEY (itemFk, warehouseFk), 
		INDEX(buyFk), INDEX(landed), INDEX(warehouseFk), INDEX(itemFk))
		ENGINE = MEMORY
		SELECT itemFk,
				warehouseFk,
				buyFk,
				landed,
				isIgnored
			FROM (SELECT b.itemFk,
						t.warehouseInFk warehouseFk,
						b.id buyFk,
						t.landed,
						b.isIgnored
					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.itemFk = vItemFk OR vItemFk IS NULL) 
							AND b.price2 > 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, isIgnored)
		SELECT itemFk,
				warehouseFk,
				buyFk,
				landed,
				isIgnored
			FROM (SELECT b.itemFk,
						t.warehouseInFk warehouseFk,
						b.id buyFk,
						t.landed,
						b.isIgnored
					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.itemFk = vItemFk OR vItemFk IS NULL) 
						AND b.price2 > 0
						AND NOT b.isIgnored
					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, isIgnored)
		SELECT itemFk,
				warehouseFk,
				buyFk,
				landed,
				isIgnored
			FROM (SELECT b.itemFk,
						t.warehouseInFk warehouseFk,
						b.id buyFk,
						t.landed,
						b.isIgnored
					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.itemFk = vItemFk OR vItemFk IS NULL) 
						AND b.quantity = 0
					ORDER BY NOT b.isIgnored DESC, t.landed DESC, b.id DESC
					LIMIT 10000000000000000000) sub
			GROUP BY itemFk, warehouseFk;

	-- ItemOriginal

	INSERT IGNORE INTO tmp.buyUltimateFromInterval(itemFk, warehouseFk, buyFk, landed, isIgnored)
		SELECT itemFk,
				warehouseFk,
				buyFk,
				landed,
				isIgnored
			FROM (SELECT b.itemFk,
						t.warehouseInFk warehouseFk,
						b.id buyFk,
						t.landed,
						itemOriginalFk,
						b.isIgnored
					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.itemFk = vItemFk OR vItemFk IS NULL) 
						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 itemFk, warehouseFk;
	INSERT IGNORE INTO tmp.buyUltimateFromInterval(itemFk, warehouseFk, buyFk, landed, isIgnored)
			SELECT itemFk,
					warehouseFk,
					buyFk,
					landed,
					isIgnored
				FROM (SELECT b.itemFk,
							t.warehouseInFk warehouseFk,
							b.id buyFk,
							t.landed,
							b.isIgnored
						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.itemFk = vItemFk OR vItemFk IS NULL) 
							AND b.price2 > 0
							AND NOT b.isIgnored
							AND itemOriginalFk
						ORDER BY t.landed DESC, b.id DESC
						LIMIT 10000000000000000000) sub
				GROUP BY itemFk, warehouseFk;

	INSERT IGNORE INTO tmp.buyUltimateFromInterval(itemFk, warehouseFk, buyFk, landed, isIgnored)
			SELECT itemFk,
					warehouseFk,
					buyFk,
					landed,
					isIgnored
				FROM 
					(SELECT b.itemFk,
							t.warehouseInFk warehouseFk,
							b.id buyFk,
							t.landed,
							b.isIgnored
						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.itemFk = vItemFk OR vItemFk IS NULL) 
							AND b.quantity = 0
							AND itemOriginalFk
						ORDER BY t.landed DESC, b.id DESC
						LIMIT 10000000000000000000) sub
			GROUP BY itemFk, warehouseFk;
END$$
DELIMITER ;
