USE `vn`;
DROP procedure IF EXISTS `itemLastEntries`;

DELIMITER $$
USE `vn`$$
CREATE DEFINER=`root`@`%` PROCEDURE `itemLastEntries`(vItem INT, vDays DATE)
BEGIN
	SELECT
		w.id AS warehouseFk,
		tr.landed,
		b.entryFk,
		b.isIgnored,
		b.price2, 
		b.price3,
		b.stickers,
		b.packing,
		b.grouping,
		i.stems,
		b.quantity,
		b.buyingValue,
		b.packageFk ,
		s.id AS supplierFk
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.itemFk = vItem And tr.shipped > vDays
		ORDER BY tr.landed DESC , b.id DESC;
END$$

DELIMITER ;

