|
|
|
@ -3,61 +3,69 @@ CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `srt`.`moving_clean`()
|
|
|
|
|
BEGIN
|
|
|
|
|
/**
|
|
|
|
|
* Elimina movimientos por inactividad
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
DECLARE vExpeditionFk INT;
|
|
|
|
|
DECLARE vBufferToFk INT;
|
|
|
|
|
DECLARE vBufferFromFk INT;
|
|
|
|
|
DECLARE done BOOL DEFAULT FALSE;
|
|
|
|
|
|
|
|
|
|
DECLARE cur CURSOR FOR
|
|
|
|
|
DECLARE vDone BOOL DEFAULT FALSE;
|
|
|
|
|
DECLARE vSorter CURSOR FOR
|
|
|
|
|
SELECT m.expeditionFk, m.bufferToFk, m.bufferFromFk
|
|
|
|
|
FROM srt.moving m
|
|
|
|
|
JOIN srt.config c
|
|
|
|
|
JOIN (SELECT bufferFk, SUM(isActive) hasBox
|
|
|
|
|
FROM srt.photocell
|
|
|
|
|
GROUP BY bufferFk) sub ON sub.bufferFk = m.bufferFromFk
|
|
|
|
|
WHERE m.created < TIMESTAMPADD(MINUTE, - c.movingMaxLife , util.VN_NOW())
|
|
|
|
|
FROM moving m
|
|
|
|
|
JOIN (
|
|
|
|
|
SELECT bufferFk, SUM(isActive) hasBox
|
|
|
|
|
FROM photocell
|
|
|
|
|
GROUP BY bufferFk
|
|
|
|
|
) sub ON sub.bufferFk = m.bufferFromFk
|
|
|
|
|
WHERE m.created < (util.VN_NOW() - INTERVAL (SELECT movingMaxLife FROM config) MINUTE)
|
|
|
|
|
AND NOT sub.hasBox;
|
|
|
|
|
|
|
|
|
|
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
|
|
|
|
|
DECLARE CONTINUE HANDLER FOR NOT FOUND SET vDone = TRUE;
|
|
|
|
|
|
|
|
|
|
OPEN cur;
|
|
|
|
|
DECLARE EXIT HANDLER FOR SQLEXCEPTION
|
|
|
|
|
BEGIN
|
|
|
|
|
ROLLBACK;
|
|
|
|
|
RESIGNAL;
|
|
|
|
|
END;
|
|
|
|
|
|
|
|
|
|
bucle: LOOP
|
|
|
|
|
OPEN vSorter;
|
|
|
|
|
l: LOOP
|
|
|
|
|
SET vDone = FALSE;
|
|
|
|
|
FETCH vSorter INTO vExpeditionFk, vBufferToFk, vBufferFromFk;
|
|
|
|
|
|
|
|
|
|
FETCH cur INTO vExpeditionFk, vBufferToFk, vBufferFromFk;
|
|
|
|
|
|
|
|
|
|
IF done THEN
|
|
|
|
|
LEAVE bucle;
|
|
|
|
|
IF vDone THEN
|
|
|
|
|
LEAVE l;
|
|
|
|
|
END IF;
|
|
|
|
|
|
|
|
|
|
DELETE FROM srt.moving
|
|
|
|
|
START TRANSACTION;
|
|
|
|
|
|
|
|
|
|
SELECT id
|
|
|
|
|
FROM moving
|
|
|
|
|
WHERE expeditionFk = vExpeditionFk
|
|
|
|
|
FOR UPDATE;
|
|
|
|
|
|
|
|
|
|
DELETE FROM moving
|
|
|
|
|
WHERE expeditionFk = vExpeditionFk;
|
|
|
|
|
|
|
|
|
|
UPDATE srt.expedition e
|
|
|
|
|
JOIN srt.expeditionState es ON es.description = 'OUT'
|
|
|
|
|
SET
|
|
|
|
|
bufferFk = NULL,
|
|
|
|
|
SELECT id
|
|
|
|
|
FROM expedition
|
|
|
|
|
WHERE id = vExpeditionFk
|
|
|
|
|
OR (bufferFk = vBufferFromFk AND `position` > 0)
|
|
|
|
|
FOR UPDATE;
|
|
|
|
|
|
|
|
|
|
UPDATE expedition
|
|
|
|
|
SET bufferFk = NULL,
|
|
|
|
|
`position` = NULL,
|
|
|
|
|
stateFk = es.id
|
|
|
|
|
WHERE e.id = vExpeditionFk;
|
|
|
|
|
stateFk = (SELECT id FROM expeditionState WHERE `description` = 'OUT')
|
|
|
|
|
WHERE id = vExpeditionFk;
|
|
|
|
|
|
|
|
|
|
UPDATE srt.expedition e
|
|
|
|
|
SET e.`position` = e.`position` - 1
|
|
|
|
|
WHERE e.bufferFk = vBufferFromFk
|
|
|
|
|
AND e.`position` > 0;
|
|
|
|
|
UPDATE expedition
|
|
|
|
|
SET `position` = `position` - 1
|
|
|
|
|
WHERE bufferFk = vBufferFromFk
|
|
|
|
|
AND `position` > 0;
|
|
|
|
|
|
|
|
|
|
CALL vn.mail_insert(
|
|
|
|
|
'pako@verdnatura.es, carles@verdnatura.es',
|
|
|
|
|
NULL,
|
|
|
|
|
CONCAT('Moving_clean. Expedition: ', vExpeditionFk, ' estaba parada'),
|
|
|
|
|
CONCAT('Expedition: ', vExpeditionFk,' vBufferToFk: ', vBufferToFk)
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
END LOOP bucle;
|
|
|
|
|
|
|
|
|
|
CLOSE cur;
|
|
|
|
|
COMMIT;
|
|
|
|
|
|
|
|
|
|
END LOOP l;
|
|
|
|
|
CLOSE vSorter;
|
|
|
|
|
END$$
|
|
|
|
|
DELIMITER ;
|
|
|
|
|