salix/db/routines/cache/procedures/visible_refresh.sql

38 lines
984 B
SQL

DELIMITER $$
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `cache`.`visible_refresh`(OUT v_calc INT, v_refresh BOOL, v_warehouse INT)
proc: BEGIN
DECLARE EXIT HANDLER FOR SQLEXCEPTION
BEGIN
CALL cache_calc_unlock (v_calc);
RESIGNAL;
END;
CALL cache_calc_start (v_calc, v_refresh, 'visible', v_warehouse);
IF !v_refresh THEN
LEAVE proc;
END IF;
-- Calculamos el stock hasta ayer
CALL `cache`.stock_refresh(false);
CREATE OR REPLACE TEMPORARY TABLE tmp.itemVisible
(PRIMARY KEY (item_id))
ENGINE = MEMORY
SELECT item_id, amount stock, amount visible FROM `cache`.stock
WHERE warehouse_id = v_warehouse;
-- Calculamos los movimientos confirmados de hoy
CALL vn.item_GetVisible(v_warehouse, NULL);
DELETE FROM visible WHERE calc_id = v_calc;
INSERT INTO visible (calc_id, item_id,visible)
SELECT v_calc, item_id, visible FROM tmp.itemVisible;
CALL cache_calc_end (v_calc);
DROP TEMPORARY TABLE tmp.itemVisible;
END$$
DELIMITER ;