DELIMITER $$
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `stock`.`log_refreshAll`()
BEGIN
/**
 * Recalculates the entire cache. It takes a considerable time,
 * please avoid calls to this procedure from commonly used operations.
 */
	DECLARE EXIT HANDLER FOR SQLEXCEPTION
	BEGIN
		DO RELEASE_LOCK('stock.log_sync');
		RESIGNAL;
	END;
    
	IF !GET_LOCK('stock.log_sync', 30) THEN
		CALL util.throw('Lock timeout exceeded');
	END IF;

	TRUNCATE TABLE stock.`log`;
	TRUNCATE TABLE stock.`inbound`;
	TRUNCATE TABLE stock.`inboundPick`;
	TRUNCATE TABLE stock.`outbound`;
	TRUNCATE TABLE stock.`visible`;

	CALL log_refreshSale(NULL, NULL);
	CALL log_refreshBuy(NULL, NULL);
	CALL log_refreshOrder(NULL, NULL);

	UPDATE outbound SET isSync = TRUE;
	CALL log_sync(TRUE);

	DO RELEASE_LOCK('stock.log_sync');
END$$
DELIMITER ;
