DELIMITER $$
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`recipe_Plaster`(vItemFk INT, vTicketFk INT, vQuantity INT)
BEGIN

	DECLARE vLastCost DECIMAL(10,2);
	DECLARE vWarehouseFk INT;
	DECLARE vShipped DATE;
	DECLARE vEntryFk INT;
	DECLARE vTravelFk INT;

	CALL cache.last_buy_refresh(FALSE);
	
	SELECT warehouseFk, shipped 
		INTO vWarehouseFk, vShipped
		FROM vn.ticket 
		WHERE id = vTicketFk;

	UPDATE vn.ticket t
		JOIN vn.item i ON i.id = vItemFk
		SET t.nickname = CONCAT(vQuantity,' ',i.longName)
		WHERE t.id = vTicketFk;

	SELECT b.buyingValue INTO vLastCost
		FROM vn.buy b
			JOIN cache.last_buy lb ON lb.buy_id = b.id
		WHERE lb.item_id = vItemFk
		ORDER BY (lb.warehouse_id = vWarehouseFk) DESC
		LIMIT 1;

	INSERT INTO vn.sale(ticketFk, itemFk, quantity, concept, price)
		SELECT vTicketFk, 98, - vQuantity, i.longName, vLastCost
			FROM vn.item i
			WHERE i.id = vItemFk;
END$$
DELIMITER ;
