DELIMITER $$
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `cache`.`available_updateItem`(
	`vItem` INT,
	`vWarehouse` INT,
	`vDated` DATE,
	`vQuantity` INT
)
BEGIN
	DECLARE vCalc INT;

	SELECT id INTO vCalc FROM cache_calc
		WHERE cacheName = 'available'
			AND params = CONCAT_WS('/', vWarehouse, vDated)
			AND last_refresh <= NOW();

	IF vCalc IS NOT NULL THEN
		UPDATE available
			SET available = available - vQuantity
			WHERE calc_id = vCalc
				AND item_id = vItem;
	END IF;
END$$
DELIMITER ;
