salix/db/routines/srt/procedures/expedition_moving.sql

48 lines
1.1 KiB
SQL

DELIMITER $$
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `srt`.`expedition_moving`(vBufferFk INT)
BEGIN
/**
* Una expedición ha entrado en el celuveyor desde un buffer
*
* @param vBufferFk Identificador de srt.buffer
*
*/
DECLARE vExpeditionFk INT;
DECLARE vPosition INT;
SELECT min(`position`) INTO vPosition
FROM srt.expedition e
WHERE e.bufferFk = vBufferFk;
SELECT e.id INTO vExpeditionFk
FROM srt.expedition e
WHERE e.`position` = vPosition
AND e.bufferFk = vBufferFk
LIMIT 1;
INSERT INTO srt.expeditionLog (expeditionFk, bufferFk, `action`)
VALUES(vExpeditionFk, vBufferFk, 'MOV');
UPDATE srt.expedition e
JOIN srt.expeditionState es on es.description = 'MOVING'
JOIN srt.moving m ON m.expeditionFk = e.id
SET e.stateFk = es.id,
e.bufferFk = m.bufferToFk,
e.`position` = 0
WHERE e.id = vExpeditionFk;
SET @posicion := 0;
UPDATE srt.expedition e
SET e.`position` = @posicion := @posicion + 1
WHERE e.bufferFk = vBufferFk
AND e.id != vExpeditionFk
ORDER BY e.`position`;
CALL srt.buffer_setStateType(vBufferFk, 'FREE', NULL);
END$$
DELIMITER ;