CREATE TRIGGER TTBRELATORIO_HTML_B01I FOR TBRELATORIO_HTML
ACTIVE BEFORE INSERT POSITION 0
AS
BEGIN
  IF (NEW.ID IS NULL) THEN
    NEW.ID = GEN_ID(GTBRELATORIO_HTML,1);
END;

CREATE TRIGGER TTBRELATORIO_HTML_B02I FOR TBRELATORIO_HTML
ACTIVE BEFORE INSERT POSITION 0
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

    HISTORICO = '';

    IF ( NEW.ID > 0) THEN
    BEGIN
        HISTORICO = HISTORICO||'Relatorio incluido';
    END

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

CREATE OR ALTER TRIGGER TTBRELATORIO_HTML_B01U FOR TBRELATORIO_HTML
ACTIVE BEFORE UPDATE POSITION 0
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

    HISTORICO = '';

    IF ( OLD.DESCRICAO <> NEW.DESCRICAO) THEN
    BEGIN
        HISTORICO = HISTORICO||'Descricao:'|| OLD.DESCRICAO||'->'||NEW.DESCRICAO;
    END

    IF ( OLD.FILTRO <> NEW.FILTRO) THEN
    BEGIN
        HISTORICO = HISTORICO||', Campo Filtro Alterado.'|| OLD.FILTRO||'->'||NEW.FILTRO;
    END

    IF ( OLD.TIPO <> NEW.TIPO) THEN
    BEGIN
        HISTORICO = HISTORICO||', Tipo:'|| OLD.TIPO||'->'||NEW.TIPO;
    END

    IF ( OLD.ZEBRADO <> NEW.ZEBRADO) THEN
    BEGIN
        HISTORICO = HISTORICO||', Zebrado:'|| OLD.ZEBRADO||'->'||NEW.ZEBRADO;
    END

    IF ( OLD.AGRUPAMENTO <> NEW.AGRUPAMENTO) THEN
    BEGIN
        HISTORICO = HISTORICO||', Agrupamento:'|| OLD.AGRUPAMENTO||'->'||NEW.AGRUPAMENTO;
    END

    IF ( OLD.AGRUPAR <> NEW.AGRUPAR) THEN
    BEGIN
        HISTORICO = HISTORICO||', Agrupar:'|| OLD.AGRUPAR||'->'||NEW.AGRUPAR;
    END

    IF ( OLD.VERCAO <> NEW.VERCAO) THEN
    BEGIN
        HISTORICO = HISTORICO||', Versao:'|| OLD.VERCAO||'->'||NEW.VERCAO;
    END

    IF ( OLD.USUARIO <> NEW.USUARIO) THEN
    BEGIN
        HISTORICO = HISTORICO||', Usuario:'|| OLD.USUARIO||'->'||NEW.USUARIO;
    END

    IF ( OLD.CABECARIO_ID <> NEW.CABECARIO_ID) THEN
    BEGIN
        HISTORICO = HISTORICO||', ID do Cabecario:'|| OLD.CABECARIO_ID||'->'||NEW.CABECARIO_ID;
    END

    IF ( OLD.RODAPE_ID <> NEW.RODAPE_ID) THEN
    BEGIN
        HISTORICO = HISTORICO||', ID do Rodape:'|| OLD.RODAPE_ID||'->'||NEW.RODAPE_ID;
    END

    IF ( OLD.FILE <> NEW.FILE) THEN
    BEGIN
        HISTORICO = HISTORICO||', Campo File:'|| OLD.FILE||'->'||NEW.FILE;
    END

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

CREATE TRIGGER TTBRELATORIO_HTML_B01D FOR TBRELATORIO_HTML
ACTIVE BEFORE DELETE POSITION 0
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

    HISTORICO = '';

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

    IF (HISTORICO <> '') THEN
    BEGIN
        EXECUTE PROCEDURE SPI_HISTORICO('TBRELATORIO_HTML',old.ID, HISTORICO);
    END
end;

CREATE TRIGGER TTBRELATORIO_HTML_DETALHE_B01I FOR TBRELATORIO_HTML_DETALHE
ACTIVE BEFORE INSERT POSITION 0
AS
BEGIN
  IF (NEW.ID IS NULL) THEN
    NEW.ID = GEN_ID(GTBRELATORIO_HTML_DETALHE,1);
END;

CREATE OR ALTER TRIGGER TTBRELATORIO_HTML_DETALHE_B02I FOR TBRELATORIO_HTML_DETALHE
ACTIVE BEFORE INSERT POSITION 0
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

    HISTORICO = '';

    IF ( NEW.ID > 0) THEN
    BEGIN
        HISTORICO = HISTORICO||'Coluna incluida: '||NEW.DESCRICAO;
    END

    IF (HISTORICO <> '') THEN
    BEGIN
        EXECUTE PROCEDURE SPI_HISTORICO('TBRELATORIO_HTML',NEW.RELATORIO_ID, HISTORICO);
    END
end;

CREATE OR ALTER TRIGGER TTBRELATORIO_HTML_DETALHE_B01U FOR TBRELATORIO_HTML_DETALHE
ACTIVE BEFORE UPDATE POSITION 0
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

    HISTORICO = '';

    IF ( OLD.PERCENTUAL <> NEW.PERCENTUAL) THEN
    BEGIN
        HISTORICO = HISTORICO||'Campo Percentual:'|| OLD.PERCENTUAL||'->'||NEW.PERCENTUAL;
    END

    IF ( OLD.DESCRICAO <> NEW.DESCRICAO) THEN
    BEGIN
        HISTORICO = HISTORICO||', Descricao Coluna:'|| OLD.DESCRICAO||'->'||NEW.DESCRICAO;
    END

    IF ( OLD.CLASS <> NEW.CLASS) THEN
    BEGIN
        HISTORICO = HISTORICO||', Campo CLASS:'|| OLD.CLASS||'->'||NEW.CLASS;
    END

    IF ( OLD.CAMPO <> NEW.CAMPO) THEN
    BEGIN
        HISTORICO = HISTORICO||', CAMPO:'|| OLD.CAMPO||'->'||NEW.CAMPO;
    END

    IF ( OLD.COLSPANT <> NEW.COLSPANT) THEN
    BEGIN
        HISTORICO = HISTORICO||', Campo COLSPANT:'|| OLD.COLSPANT||'->'||NEW.COLSPANT;
    END

    IF ( OLD.COLSPANL <> NEW.COLSPANL) THEN
    BEGIN
        HISTORICO = HISTORICO||', Campo COLSPANL:'|| OLD.COLSPANL||'->'||NEW.COLSPANL;
    END

    IF ( OLD.ORDEM <> NEW.ORDEM) THEN
    BEGIN
        HISTORICO = HISTORICO||', Campo ORDEM:'|| OLD.ORDEM||'->'||NEW.ORDEM;
    END

    IF ( OLD.MASCARA <> NEW.MASCARA) THEN
    BEGIN
        HISTORICO = HISTORICO||', Coluna Campo MASCARA:'|| OLD.MASCARA||'->'||NEW.MASCARA;
    END

    IF (HISTORICO <> '') THEN
    BEGIN
        EXECUTE PROCEDURE SPI_HISTORICO('TBRELATORIO_HTML',NEW.RELATORIO_ID, HISTORICO);
    END
end;

CREATE OR ALTER TRIGGER TTBRELATORIO_HTML_DETALHE_B01D FOR TBRELATORIO_HTML_DETALHE
ACTIVE BEFORE DELETE POSITION 0
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

    HISTORICO = '';

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

    IF (HISTORICO <> '') THEN
    BEGIN
        EXECUTE PROCEDURE SPI_HISTORICO('TBRELATORIO_HTML',old.RELATORIO_ID, HISTORICO);
    END
end;

ALTER SEQUENCE GTBRELATORIO_HTML RESTART WITH 12 INCREMENT BY 1;
ALTER SEQUENCE GTBRELATORIO_HTML_DETALHE RESTART WITH 120 INCREMENT BY 1;
ALTER SEQUENCE GTBHTML_TOTALIZADOR_GERAL RESTART WITH 5 INCREMENT BY 1;

CREATE TRIGGER TTBHTML_TOTALIZADOR_GERAL_B01I FOR TBHTML_TOTALIZADOR_GERAL
ACTIVE BEFORE INSERT POSITION 0
AS
BEGIN
  IF (NEW.ID IS NULL) THEN
    NEW.ID = GEN_ID(GTBHTML_TOTALIZADOR_GERAL,1);
END;

CREATE OR ALTER TRIGGER TTBHTML_TOTALIZADOR_GERAL_B02I FOR TBHTML_TOTALIZADOR_GERAL
ACTIVE BEFORE INSERT POSITION 0
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

    HISTORICO = '';

    IF ( NEW.ID > 0) THEN
    BEGIN
        HISTORICO = HISTORICO||'Totalizador incluido: '||NEW.DESCRICAO;
    END

    IF (HISTORICO <> '') THEN
    BEGIN
        EXECUTE PROCEDURE SPI_HISTORICO('TBRELATORIO_HTML',NEW.RELATORIO_ID, HISTORICO);
    END
end;

CREATE OR ALTER TRIGGER TTBHTML_TOTALIZADOR_GERAL_B01U FOR TBHTML_TOTALIZADOR_GERAL
ACTIVE BEFORE UPDATE POSITION 0
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

    HISTORICO = '';

    IF ( OLD.DETALHE_ID <> NEW.DETALHE_ID) THEN
    BEGIN
        HISTORICO = HISTORICO||'Campo DETALHE ID:'|| OLD.DETALHE_ID||'->'||NEW.DETALHE_ID;
    END

    IF ( OLD.TIPO <> NEW.TIPO) THEN
    BEGIN
        HISTORICO = HISTORICO||', Campo TIPO:'|| OLD.TIPO||'->'||NEW.TIPO;
    END

    IF ( OLD.CLASS <> NEW.CLASS) THEN
    BEGIN
        HISTORICO = HISTORICO||', Campo CLASS:'|| OLD.CLASS||'->'||NEW.CLASS;
    END

    IF ( OLD.DESCRICAO <> NEW.DESCRICAO) THEN
    BEGIN
        HISTORICO = HISTORICO||', Descricao totalizador:'|| OLD.DESCRICAO||'->'||NEW.DESCRICAO;
    END

    IF ( OLD.MASCARA <> NEW.MASCARA) THEN
    BEGIN
        HISTORICO = HISTORICO||', Totalizador Campo MASCARA:'|| OLD.MASCARA||'->'||NEW.MASCARA;
    END

    IF (HISTORICO <> '') THEN
    BEGIN
        EXECUTE PROCEDURE SPI_HISTORICO('TBRELATORIO_HTML',NEW.RELATORIO_ID, HISTORICO);
    END
end;

CREATE OR ALTER TRIGGER TTBHTML_TOTALIZADOR_GERAL_B01D FOR TBHTML_TOTALIZADOR_GERAL
ACTIVE BEFORE DELETE POSITION 0
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

    HISTORICO = '';

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

    IF (HISTORICO <> '') THEN
    BEGIN
        EXECUTE PROCEDURE SPI_HISTORICO('TBRELATORIO_HTML',old.RELATORIO_ID, HISTORICO);
    END
end;












