DELIMITER $$
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`ticket_setVolumeItemCost`(
	vItemFk INT
)
BEGIN
/**
 * Update the volume tickets of item
 *
 * @param vSelf Ticket id
 */
	CREATE OR REPLACE TEMPORARY TABLE tTicket
		(PRIMARY KEY (id))
		ENGINE = MEMORY
		SELECT t.id, SUM(s.quantity * ic.cm3delivery / 1000000) volume
			FROM sale s
				JOIN ticket t ON t.id = s.ticketFk
				JOIN itemCost ic ON ic.itemFk = s.itemFk
					AND ic.warehouseFk = t.warehouseFk
			WHERE s.itemFk = vItemFk
				AND t.shipped >= util.VN_CURDATE()
			GROUP BY t.id;

	UPDATE ticket t
			JOIN tTicket tt ON tt.id = t.id
		SET t.volume = tt.volume;

	DROP TEMPORARY TABLE tTicket;
END$$
DELIMITER ;
