SET TERM ^ ;

CREATE trigger ttbgnre_nf_b01d for tbgnre_nf
active before delete position 1
AS
DECLARE VARIABLE DOCUMENTO_FISCAL INTEGER;
BEGIN
    IF(COALESCE(OLD.GNRE_ID, 0) > 0) THEN
    BEGIN
        IF(CHAR_LENGTH(OLD.HTML) > 0) THEN
        BEGIN
            IF(OLD.PDV_CFE_ID > 0) THEN
            BEGIN
                :DOCUMENTO_FISCAL = OLD.PDV_CFE_ID;
            END ELSE
            BEGIN
                :DOCUMENTO_FISCAL = OLD.NF_ID;
            END

            EXCEPTION "..."'DOCUMENTO FISCAL '||:DOCUMENTO_FISCAL||' JA CONSTA PROTOCOLO DE AUTORIZACAO. EXCLUSAO CANCELADA.';
        END

    END
END
^

SET TERM ; ^

SET TERM ^ ;

CREATE trigger ttbgnre_nf_b09d for tbgnre_nf
active before delete position 9
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
DECLARE VARIABLE DOCUMENTO_FISCAL INTEGER;
BEGIN
    HISTORICO = '';

    IF(COALESCE(OLD.GNRE_ID, 0) > 0) THEN
    BEGIN
        IF(OLD.PDV_CFE_ID > 0) THEN
        BEGIN
            :DOCUMENTO_FISCAL = OLD.PDV_CFE_ID;
        END ELSE
        BEGIN
            :DOCUMENTO_FISCAL = OLD.NF_ID;
        END

        IF (OLD.ID > 0) THEN
        BEGIN
            HISTORICO = HISTORICO||'DOCUMENTO FISCAL '||:DOCUMENTO_FISCAL||' EXCLUIDO';
        END
    
        IF (HISTORICO <> '') THEN
        BEGIN
            EXECUTE PROCEDURE SPI_HISTORICO('TBGNRE', OLD.GNRE_ID, HISTORICO);
        END
    END
    
END
^

SET TERM ; ^


SET TERM ^ ;

CREATE trigger ttbgnre_b09i for tbgnre
active before insert position 9
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN
    HISTORICO = '';

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

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

SET TERM ; ^

ALTER TABLE TBGNRE
    ADD STATUS INTEGER DEFAULT 0 NOT NULL;

COMMENT ON COLUMN TBGNRE.STATUS IS
'0 - LOTE NAO TRANSMITIDO;
1 - LOTE REJEITADO;
2 - LOTE TRANSMITIDO;';

ALTER TABLE TBGNRE
    ADD REJEICAO VARCHAR(500);

SET TERM ^ ;

CREATE trigger ttbgnre_b09u for tbgnre
active before update position 9
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN
    HISTORICO = '';

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

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

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

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

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

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

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

SET TERM ; ^

