SET TERM ^ ;

CREATE trigger ttbregistro_orcamento_b02i for tbregistro_orcamento
active before insert position 0
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN
    HISTORICO = '';

    IF(NEW.id > 0) THEN
    BEGIN
        HISTORICO = 'REGISTRO INCLUÍDO';
    END

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

SET TERM ; ^

SET TERM ^ ;

CREATE trigger ttbregistro_orcamento_b09u for tbregistro_orcamento
active before update position 9
AS
declare variable HISTORICO VARCHAR(500);
begin
    HISTORICO = '';

    IF(NEW.CLIENTE_ID IS DISTINCT FROM OLD.CLIENTE_ID) THEN
    BEGIN
        HISTORICO = HISTORICO||'CLIENTE: '||OLD.CLIENTE_ID||' -> '||NEW.CLIENTE_ID;
    END

    IF(NEW.ESTABELECIMENTO_ID IS DISTINCT FROM OLD.ESTABELECIMENTO_ID) THEN
    BEGIN
        HISTORICO = HISTORICO||'ESTAB: '||OLD.ESTABELECIMENTO_ID||' -> '||NEW.ESTABELECIMENTO_ID;
    END

    IF(NEW.TABELA_PRECO_ID IS DISTINCT FROM OLD.TABELA_PRECO_ID) THEN
    BEGIN
        HISTORICO = HISTORICO||'TAB.PRECO: '||OLD.TABELA_PRECO_ID||' -> '||NEW.TABELA_PRECO_ID;
    END

    IF(NEW.REPRESENTANTE_ID IS DISTINCT FROM OLD.REPRESENTANTE_ID) THEN
    BEGIN
        HISTORICO = HISTORICO||'REP: '||OLD.REPRESENTANTE_ID||' -> '||NEW.REPRESENTANTE_ID;
    END

    IF(HISTORICO <> '') THEN
    BEGIN
        EXECUTE PROCEDURE SPI_HISTORICO('TBREGISTRO_ORCAMENTO', NEW.ID, HISTORICO);
    END
end
^

SET TERM ; ^

SET TERM ^ ;

CREATE OR ALTER trigger tbregistro_orcamento_item_bi0 for tbregistro_orcamento_item
active before insert position 0
AS
declare variable HISTORICO VARCHAR(500);
declare variable MODELO_DESCRICAO VARCHAR(200);
declare variable COR_DESCRICAO VARCHAR (200);
begin
    HISTORICO = '';

    MODELO_DESCRICAO = (SELECT FIRST 1 T.DESCRICAO FROM TBMODELO T WHERE T.CODIGO = NEW.MODELO_ID);
    COR_DESCRICAO = (SELECT FIRST 1 C.DESCRICAO FROM TBCOR C WHERE C.CODIGO = NEW.COR_ID);

    IF(NEW.ID > 0) THEN
    BEGIN
        HISTORICO = 'MODELO '||MODELO_DESCRICAO||' '||COR_DESCRICAO||' '||NEW.TAM_DESCRICAO||' INCLUÍDO';
    END

    IF(HISTORICO <> '') THEN
    BEGIN
        EXECUTE PROCEDURE SPI_HISTORICO('TBREGISTRO_ORCAMENTO', NEW.ORCAMENTO_ID, HISTORICO);
    END
end
^

SET TERM ; ^

SET TERM ^ ;

CREATE OR ALTER trigger ttbregistro_orcamento_item_b09d for tbregistro_orcamento_item
active before delete position 9
AS
declare variable HISTORICO VARCHAR(500);
declare variable MODELO_DESCRICAO VARCHAR(200);
declare variable COR_DESCRICAO VARCHAR (200);
begin
    HISTORICO = '';

    MODELO_DESCRICAO = (SELECT FIRST 1 T.DESCRICAO FROM TBMODELO T WHERE T.CODIGO = OLD.MODELO_ID);
    COR_DESCRICAO = (SELECT FIRST 1 C.DESCRICAO FROM TBCOR C WHERE C.CODIGO = OLD.COR_ID);

    IF(OLD.ID > 0) THEN
    BEGIN
        HISTORICO = 'MODELO '||MODELO_DESCRICAO||' '||COR_DESCRICAO||' '||OLD.TAM_DESCRICAO||' EXCLUÍDO';
    END

    IF(HISTORICO <> '') THEN
    BEGIN
        EXECUTE PROCEDURE SPI_HISTORICO('TBREGISTRO_ORCAMENTO', OLD.ORCAMENTO_ID, HISTORICO);
    END
end
^

SET TERM ; ^




