SET TERM ^ ;

create or alter procedure SPU_SITUACAO_NF
as
declare variable NFS_ID integer;
declare variable NFE_ID integer;
declare variable XML char(10);
declare variable SITUACAO_NF integer;
BEGIN

    FOR SELECT DISTINCT
        T.NFS_CONTROLE,
        T.NFE_CONTROLE,
        MAX(T.XML) AS XML
    FROM TBNF_LOTE T
    GROUP BY 1, 2
    INTO :NFS_ID, :NFE_ID, :XML
    DO BEGIN

        :SITUACAO_NF = 1;

        IF(:NFS_ID > 0) THEN
        BEGIN

            IF(:XML = '07') THEN
            BEGIN
                :SITUACAO_NF = 2;
            END

            IF(:XML = '08' OR :XML = '10') THEN
            BEGIN
                :SITUACAO_NF = 3;

            END

            UPDATE TBNFS N SET N.SITUACAO_NF = :SITUACAO_NF WHERE N.CONTROLE = :NFS_ID;

        END

        IF(:NFE_ID > 0) THEN
        BEGIN
            IF(:XML = '07') THEN
            BEGIN
                :SITUACAO_NF = 2;
            END

            IF(:XML = '08' OR :XML = '10') THEN
            BEGIN
                :SITUACAO_NF = 3;

            END

            UPDATE TBNFE N SET N.SITUACAO_NF = :SITUACAO_NF WHERE N.CONTROLE = :NFE_ID;

        END



    END
    
END
^

SET TERM ; ^

