SET TERM ^ ;

CREATE OR ALTER TRIGGER TTBROMANEIO_TINTURARIA_B01D FOR TBROMANEIO_TINTURARIA
ACTIVE BEFORE DELETE POSITION 1
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
DECLARE VARIABLE DESCRICAOOLD VARCHAR(500);
DECLARE VARIABLE TEM_ITENS INTEGER;
BEGIN

    IF(OLD.STATUS > 0)THEN
    BEGIN
        --IF(OLD.STATUS = 0)THEN BEGIN DESCRICAOOLD = 'EM ABERTO';        END
        IF(OLD.STATUS = 1)THEN BEGIN DESCRICAOOLD = 'GERANDO LOTES';    END
        IF(OLD.STATUS = 2)THEN BEGIN DESCRICAOOLD = 'GERANDO OC';       END
        IF(OLD.STATUS = 3)THEN BEGIN DESCRICAOOLD = 'GERANDO NOTA';     END
        IF(OLD.STATUS = 4)THEN BEGIN DESCRICAOOLD = 'FINALIZADO';       END
    
        TEM_ITENS = COALESCE((SELECT FIRST 1 ITENS.ID FROM TBROMANEIO_TINTURARIA_ITENS ITENS WHERE ITENS.ROMANEIO_ID = OLD.ID),0);

        IF((OLD.STATUS = 1 AND TEM_ITENS > 0) OR (OLD.STATUS <> 1)) THEN
        BEGIN
            EXCEPTION "..." 'ROMANEIO "'||DESCRICAOOLD||'", OPERACAO CANCELADA';
        END

    END

    HISTORICO = '';

    IF ( OLD.ID > 0) THEN
    BEGIN
        HISTORICO = HISTORICO||'EXCLUIDA: '||OLD.ID;
    END

    IF (HISTORICO <> '') THEN
    BEGIN
        EXECUTE PROCEDURE SPI_HISTORICO('TBROMANEIO_TINTURARIA',OLD.ID, HISTORICO);
    END
END^

SET TERM ; ^

