CREATE OR ALTER trigger ttbobrigacao_icms_b01i for tbobrigacao_icms
active before insert position 0
AS
BEGIN
  IF (NEW.ID IS NULL) THEN
    NEW.ID = GEN_ID(GTBOBRIGACAO_ICMS,1);
END



CREATE trigger ttbobrigacao_icms_b01u for tbobrigacao_icms
active before update position 0
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

    HISTORICO = '';

    IF ( OLD.CODIGO <> NEW.CODIGO) THEN
    BEGIN
        HISTORICO = HISTORICO||' COD.'|| OLD.CODIGO||'->'||NEW.CODIGO;
    END

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

    IF ( OLD.DATA_INICIO <> NEW.DATA_INICIO) THEN
    BEGIN
        HISTORICO = HISTORICO||' DAT.INI:'|| OLD.DATA_INICIO||'->'||NEW.DATA_INICIO;
    END

    IF ( OLD.DATA_FIM <> NEW.DATA_FIM) THEN
    BEGIN
        HISTORICO = HISTORICO||' DAT.FIM:'|| OLD.DATA_FIM||'->'||NEW.DATA_FIM;
    END

    IF ( OLD.ICMS_ST <> NEW.ICMS_ST) THEN
    BEGIN
        HISTORICO = HISTORICO||' S.T:'|| OLD.ICMS_ST||'->'||NEW.ICMS_ST;
    END

    IF (HISTORICO <> '') THEN
    BEGIN
        HISTORICO = 'REGISTRO ALTERADO:'||HISTORICO;
        EXECUTE PROCEDURE SPI_HISTORICO('TBOBRIGACAO_ICMS', NEW.ID, FN_ELIPSE(:HISTORICO, 500));
    END
end



CREATE trigger ttbobrigacao_icms_b01d for tbobrigacao_icms
active before delete position 0
AS
declare variable HISTORICO VARCHAR(500);
begin
   HISTORICO = '';

   IF(OLD.ID > 0) THEN
   BEGIN
       HISTORICO = HISTORICO||'ITEM EXCLUIDO: '||OLD.ID||' - '||OLD.DESCRICAO;
   END

   IF(HISTORICO <> '') THEN
   BEGIN
      EXECUTE PROCEDURE SPI_HISTORICO('TBOBRIGACAO_ICMS', OLD.ID, FN_ELIPSE(:HISTORICO, 500));
   END
end



