ALTER TABLE TBNFS
    ADD SITUACAO_NF INTEGER DEFAULT 1 NOT NULL;

COMMENT ON COLUMN TBNFS.SITUACAO_NF IS
'SITUACAO SEMELHANTE A TBPDV_CFE PARA GERACAO DE SPED FISCAL E RELATORIOS DE APURACAO
1 - ATIVO;
2 - AUTORIZADO;
3 - CANCELADO/DENEGADO;';



ALTER TABLE TBNFE
    ADD SITUACAO_NF INTEGER DEFAULT 1 NOT NULL;

COMMENT ON COLUMN TBNFE.SITUACAO_NF IS
'SITUACAO SEMELHANTE A TBPDV_CFE PARA GERACAO DE SPED FISCAL E RELATORIOS DE APURACAO
1 - ATIVO;
2 - AUTORIZADO;
3 - CANCELADO/DENEGADO;';

SET TERM ^ ;

CREATE trigger ttbnfs_b03i for tbnfs
active before insert position 3
AS
BEGIN
    IF(NEW.NATUREZA = 2) THEN
    BEGIN
        NEW.SITUACAO_NF = 2;
    END
END
^

SET TERM ; ^

SET TERM ^ ;

CREATE trigger ttbnfs_b11u for tbnfs
active before update position 0
AS
BEGIN

    /* MUDANCA DE SITUACAO AUTORIZADA PARA ATIVO */
    IF(OLD.SITUACAO = 2 AND NEW.SITUACAO = 1) THEN
    BEGIN
        NEW.SITUACAO_NF = 1;
    END

    /* MUDANCA DE NATUREZA LANCAMENTO PARA EMISSAO */
    IF(OLD.NATUREZA = 2 AND NEW.NATUREZA = 1) THEN
    BEGIN
        NEW.SITUACAO_NF = 1;
    END
END
^

SET TERM ; ^


SET TERM ^ ;

CREATE trigger ttbnf_lote_b01iu for tbnf_lote
active before insert or update position 1
AS
BEGIN   
    IF(INSERTING) THEN
    BEGIN
        IF(NEW.XML = '07') THEN
        BEGIN
            IF(NEW.NFS_CONTROLE > 0) THEN
            BEGIN
                UPDATE TBNFS T SET T.SITUACAO_NF = 2 WHERE T.CONTROLE = NEW.NFS_CONTROLE;
            END

            IF(NEW.NFE_CONTROLE > 0) THEN
            BEGIN
                UPDATE TBNFE T SET T.SITUACAO_NF = 2 WHERE T.CONTROLE = NEW.NFE_CONTROLE;
            END

        END
    END

    IF(UPDATING) THEN
    BEGIN
        IF(OLD.XML <> NEW.XML) THEN
        BEGIN
            
            IF(NEW.XML = '07') THEN
            BEGIN
                IF(NEW.NFS_CONTROLE > 0) THEN
                BEGIN
                    UPDATE TBNFS T SET T.SITUACAO_NF = 2 WHERE T.CONTROLE = NEW.NFS_CONTROLE;
                END
    
                IF(NEW.NFE_CONTROLE > 0) THEN
                BEGIN
                    UPDATE TBNFE T SET T.SITUACAO_NF = 2 WHERE T.CONTROLE = NEW.NFE_CONTROLE;
                END
            END

            IF(NEW.XML = '08' OR NEW.XML = '10') THEN
            BEGIN
                IF(NEW.NFS_CONTROLE > 0) THEN
                BEGIN
                    UPDATE TBNFS T SET T.SITUACAO_NF = 3 WHERE T.CONTROLE = NEW.NFS_CONTROLE;
                END
    
                IF(NEW.NFE_CONTROLE > 0) THEN
                BEGIN
                    UPDATE TBNFE T SET T.SITUACAO_NF = 3 WHERE T.CONTROLE = NEW.NFE_CONTROLE;
                END
            END

        END
    
    END

END
^

SET TERM ; ^


SET TERM ^ ;

CREATE trigger ttbnfe_b05i for tbnfe
active before insert position 3
AS
BEGIN
    IF(NEW.NATUREZA = 2) THEN
    BEGIN
        NEW.SITUACAO_NF = 2;
    END
END
^

SET TERM ; ^

SET TERM ^ ;

CREATE trigger ttbnfe_b11u for tbnfe
active before update position 0
AS
BEGIN

    /* MUDANCA DE SITUACAO AUTORIZADA PARA ATIVO */
    IF(OLD.SITUACAO = 2 AND NEW.SITUACAO = 1) THEN
    BEGIN
        NEW.SITUACAO_NF = 1;
    END

    /* MUDANCA DE NATUREZA LANCAMENTO PARA EMISSAO */
    IF(OLD.NATUREZA = 2 AND NEW.NATUREZA = 1) THEN
    BEGIN
        NEW.SITUACAO_NF = 1;
    END
END
^

SET TERM ; ^
