salix/db/routines/srt/functions/expedition_check.sql

61 lines
1.5 KiB
MySQL
Raw Permalink Normal View History

DELIMITER $$
CREATE OR REPLACE DEFINER=`root`@`localhost` FUNCTION `srt`.`expedition_check`(vExpeditionFk INT)
RETURNS int(11)
NOT DETERMINISTIC
READS SQL DATA
BEGIN
/**
* Comprueba si existe una expedición, generando una aleatoria en caso contrario.
*
* @param vExpeditionFk Identificador de srt.expedition
*
*/
DECLARE vExpeditionOutFk INT;
DECLARE vIsOnTheCelluveyor BOOL;
SELECT id INTO vExpeditionOutFk
FROM srt.expedition
WHERE id = vExpeditionFk;
SELECT COUNT(*) INTO vIsOnTheCelluveyor
FROM srt.moving m
JOIN srt.movingState ms ON ms.id = m.stateFk
WHERE m.expeditionFk = vExpeditionFk
AND ms.description != 'NEW';
IF ISNULL(vExpeditionOutFk) OR vIsOnTheCelluveyor THEN
SELECT LEAST(-1,MIN(id)- 1) INTO vExpeditionOutFk
FROM srt.expedition;
CASE vExpeditionFk
WHEN 0 THEN -- Genera una EXP que termine en 0
SET vExpeditionOutFk = CEIL((vExpeditionOutFk - 10) / 10) * 10;
WHEN 2 THEN -- Genera una EXP que termine en 2
SET vExpeditionOutFk = -2 + CEIL((vExpeditionOutFk - 10) / 10) * 10;
ELSE -- Genera una EXP que termine en 9
SET vExpeditionOutFk = -9 + CEIL((vExpeditionOutFk - 10) / 10) * 10;
END CASE;
INSERT INTO srt.expedition(id,stateFk,width,`length`, height)
SELECT vExpeditionOutFk, es.id , p.width * 10, p.`depth` *10, p.height * 10
FROM srt.expeditionState es
JOIN vn.packaging p ON p.id = '94'
WHERE es.description = 'ENTERING';
END IF;
RETURN vExpeditionOutFk;
END$$
DELIMITER ;