do $$
declare
    _count integer;
    _i integer;
    _nf record;

    _nf_item_list refcursor;
    _nf_item record;
    
    _vl_out_rest numeric;
    _vl_desc_rest numeric;
    
    _vl_out numeric;
    _vl_desc numeric;
begin

    for _nf in 
        select
            tb_nf.id_emp,
            tb_nf.id_nf,
            tb_nf.sit,
            tb_nf.vl_out,
            tb_nf.vl_item,
            sum(tb_nf_item.vl_out) as vl_out_item,
            tb_nf.vl_desc,
            sum(tb_nf_item.vl_desc) as vl_desc_item
        from tb_nf
        left outer join tb_nf_item on
            (tb_nf.id_emp = tb_nf_item.id_emp and
             tb_nf.id_nf = tb_nf_item.id_nf)
        where
            tb_nf.dt_emis >= '2018-01-01' and
            tb_nf.sit not in ('8', '7')
        group by
            tb_nf.id_emp,
            tb_nf.id_nf,
            tb_nf.sit,
            tb_nf.vl_out,
            tb_nf.vl_desc,
            tb_nf.vl_item
        having
            tb_nf.vl_out <> sum(tb_nf_item.vl_out) or
            tb_nf.vl_desc <>  sum(tb_nf_item.vl_desc)
    loop
        
        
        
        _vl_out_rest := _nf.vl_out;
        _vl_desc_rest := _nf.vl_desc;
        
        select count(*) into _count
        from tb_nf_item
        where
            id_emp = _nf.id_emp and
            id_nf= _nf.id_nf;
                
        raise notice 'NF: %   Items: %', _nf.id_nf, _count;
        
        open _nf_item_list for 
            select *
            from tb_nf_item
            where
                id_emp = _nf.id_emp and
                id_nf= _nf.id_nf;
        
        _i := 0;
        
        loop
            fetch _nf_item_list into _nf_item;
            
            _i := _i + 1;
            
            exit when not found;
            
            _vl_out := 0;
            _vl_desc := 0;
            
            if (_vl_out_rest > 0) then
            
                _vl_out := cast(_nf.vl_out * _nf_item.vl_tot / _nf.vl_item as numeric(12,2));
                
                _vl_out_rest := _vl_out_rest - _vl_out;
                
            end if;
            
            if (_vl_desc_rest > 0) then
            
                _vl_desc := cast(_nf.vl_desc * _nf_item.vl_tot / _nf.vl_item as numeric(12,2));
                
                _vl_desc_rest := _vl_desc_rest - _vl_desc;
            end if;
            
            raise notice 'Out: % %  Desc: % % ', _nf_item.vl_out, _vl_out, _nf_item.vl_desc, _vl_desc;

            if (_i = _count) then
                if (_vl_out_rest <> 0) then
                    _vl_out := _vl_out + _vl_out_rest;
                end if;
                
                if (_vl_desc_rest <> 0) then
                    _vl_desc := _vl_desc + _vl_desc_rest;
                end if;
            
                raise notice '%', 'fim';
            end if;
            
        
            update tb_nf_item set
                vl_desc = _vl_desc,
                vl_out = _vl_out
            where
                id_emp = _nf_item.id_emp and
                id_nf = _nf_item.id_nf and
                id_nf_item = _nf_item.id_nf_item;
                
        end loop;
        
        raise notice '%', _vl_out_rest;
        
        close _nf_item_list;

    end loop;
    
end $$;