DELIMITER $$
CREATE OR REPLACE DEFINER=`vn`@`localhost` PROCEDURE `stock`.`stock_clean`()
BEGIN
/**
 * Cleans current time dependent cache records.
 */
	DECLARE vExpired DATETIME;
	DECLARE vAliveDate DATE;

	-- Expired order reserves

	SELECT SUBTIME(util.VN_NOW(), reserveTime)
		INTO vExpired
		FROM hedera.orderConfig LIMIT 1;

	UPDATE hedera.order
		SET isReserved = FALSE
		WHERE created < vExpired
			AND isReserved;

	-- Frozen old sales

	SELECT util.VN_CURDATE() - INTERVAL saleLife DAY
		INTO vAliveDate
		FROM config LIMIT 1;

	UPDATE vn.ticket
		SET isAlive = FALSE
		WHERE shipped < vAliveDate
			AND isAlive;
END$$
DELIMITER ;
