DELIMITER $$
CREATE OR REPLACE DEFINER=`vn`@`localhost` PROCEDURE `vn`.`inventory_repair`()
BEGIN

	DROP TEMPORARY TABLE IF EXISTS tmp.lastEntry;
	CREATE TEMPORARY TABLE tmp.lastEntry
	(PRIMARY KEY (buyFk))
	SELECT
			i.id AS itemFk,
			w.id AS warehouseFk,
			w.name AS warehouse,
			tr.landed,
			b.id AS buyFk,
			b.entryFk,
			b.isIgnored,
			b.price2, 
			b.price3,
			b.stickers,
			b.packing,
			b.grouping,
			b.groupingMode,
			b.weight,
			i.stems,
			b.quantity,
			b.buyingValue,
			b.packagingFk ,
			s.id AS supplierFk,
			s.name AS supplier
		FROM itemType it
		RIGHT JOIN (entry e 
				LEFT JOIN supplier s ON s.id = e.supplierFk
				RIGHT JOIN buy b ON b.entryFk = e.id
				LEFT JOIN item i ON i.id = b.itemFk
				LEFT JOIN ink ON ink.id = i.inkFk 
				LEFT JOIN travel tr ON tr.id = e.travelFk
				LEFT JOIN warehouse w ON w.id = tr.warehouseInFk
				LEFT JOIN origin o ON o.id = i.originFk
				) ON it.id = i.typeFk
		LEFT JOIN edi.ekt ek ON b.ektFk = ek.id
		WHERE (b.packagingFk = "--" OR b.price2 = 0 OR b.buyingValue = 0) AND tr.landed > util.firstDayOfMonth(TIMESTAMPADD(MONTH,-1,util.VN_CURDATE())) AND s.name = 'INVENTARIO';
	  
	DROP TEMPORARY TABLE IF EXISTS tmp.lastEntryOk;
	CREATE TEMPORARY TABLE tmp.lastEntryOk  
	(PRIMARY KEY (buyFk))
	  SELECT
			i.id AS itemFk,
			w.id AS warehouseFk,
			w.name AS warehouse,
			tr.landed,
			b.id AS buyFk,
			b.entryFk,
			b.isIgnored,
			b.price2, 
			b.price3,
			b.stickers,
			b.packing,
			b.grouping,
			b.groupingMode,
			b.weight,
			i.stems,
			b.quantity,
			b.buyingValue,
			b.packagingFk,
			s.id AS supplierFk,
			s.name AS supplier
		FROM itemType it
		RIGHT JOIN (entry e 
				LEFT JOIN supplier s ON s.id = e.supplierFk
				RIGHT JOIN buy b ON b.entryFk = e.id
				LEFT JOIN item i ON i.id = b.itemFk
				LEFT JOIN ink ON ink.id = i.inkFk 
				LEFT JOIN travel tr ON tr.id = e.travelFk
				LEFT JOIN warehouse w ON w.id = tr.warehouseInFk
				LEFT JOIN origin o ON o.id = i.originFk
				) ON it.id = i.typeFk
		LEFT JOIN edi.ekt ek ON b.ektFk = ek.id
		WHERE b.packagingFk != "--" AND b.price2 != 0 AND b.packing != 0 AND b.buyingValue > 0 AND tr.landed > util.firstDayOfMonth(TIMESTAMPADD(MONTH,-2,util.VN_CURDATE()))
		ORDER BY tr.landed DESC;
		
	DROP TEMPORARY TABLE IF EXISTS tmp.lastEntryOkGroup;
	CREATE TEMPORARY TABLE tmp.lastEntryOkGroup  
	(INDEX (warehouseFk,itemFk))
	  SELECT *
		FROM tmp.lastEntryOk tmp
		GROUP BY tmp.itemFk,tmp.warehouseFk;
		
		UPDATE buy b
		JOIN tmp.lastEntry lt ON lt.buyFk = b.id
		JOIN tmp.lastEntryOkGroup eo ON eo.itemFk = lt.itemFk AND eo.warehouseFk = lt.warehouseFk
		SET b.packagingFk = eo.packagingFk WHERE b.packagingFk = "--";
		
		UPDATE buy b
		JOIN tmp.lastEntry lt ON lt.buyFk = b.id
		JOIN tmp.lastEntryOkGroup eo ON eo.itemFk = lt.itemFk AND eo.warehouseFk = lt.warehouseFk
		 SET b.price2 = eo.price2 WHERE  b.price2 = 0 ;
		
		UPDATE buy b
		JOIN tmp.lastEntry lt ON lt.buyFk = b.id
		JOIN tmp.lastEntryOkGroup eo ON eo.itemFk = lt.itemFk AND eo.warehouseFk = lt.warehouseFk
		SET b.buyingValue = eo.buyingValue WHERE  b.buyingValue = 0;
    
		DROP TEMPORARY TABLE tmp.lastEntry;
        DROP TEMPORARY TABLE tmp.lastEntryOk;
    	DROP TEMPORARY TABLE tmp.lastEntryOkGroup;
END$$
DELIMITER ;
