do $$
declare
    _id_invt integer;
    _id_invt_item integer;
    _id_dep integer;
    _id_emp integer;
    _id_grd_estq varchar;
    _dt date;
    
    
    _list refcursor;
    _item record;
begin
    _id_emp := 1;
    _id_dep := 1;
    _id_grd_estq := '1';
    _dt := '2016-03-15';
    
    _id_invt := nextval('sq_invt');

    insert into tb_invt 
        (id_emp, id_invt, dt_invt, id_dep, sit)
    values
        (_id_emp, _id_invt, current_date, _id_dep, 'A');
        
        
    
    open _list for
        select 
            id_item,
            id_und_med,
            id_emp,
            id_dep,
            id_grd_estq,
            vl_estq,
            vl_estq
                - coalesce(vl_vnd, 0)
                + coalesce(vl_nf_ent, 0)
                - coalesce(vl_nf_sai, 0)
                - coalesce(vl_mnt_estq_sai, 0)
                + coalesce(vl_mnt_estq_ent, 0)
                - coalesce(vl_mnt_estq_trans_sai, 0)
                + coalesce(vl_mnt_estq_trans_ent, 0) 
                - coalesce(vl_carreg_sai, 0) as vl_estq_atu
        from
        (
            select
                tb_item.id_und_med,
                tb_item_estq.id_item,
                tb_item_estq.id_emp,
                tb_item_estq.id_dep,
                tb_item_estq.id_grd_estq,
                tb_item_estq.vl_estq,
                --
                -- Venda
                --
                (select sum(tb_vnd_item.vl_qtd_mov)
                 from tb_vnd
                 join tb_vnd_item on
                    (tb_vnd.id_emp = tb_vnd_item.id_emp and
                     tb_vnd.id_vnd = tb_vnd_item.id_vnd)
                 where
                    tb_vnd.id_emp = tb_item_estq.id_emp and
                    tb_vnd_item.id_item = tb_item_estq.id_item and
                    tb_vnd_item.id_dep = tb_item_estq.id_dep and
                    tb_vnd_item.id_grd_estq = tb_item_estq.id_grd_estq and
                    tb_vnd.dt_vnd >= _dt) as vl_vnd,
                --
                -- Nota fiscal entrada
                --
                (select sum(tb_nf_item.vl_qtd_mov)
                 from tb_nf
                 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.id_emp = tb_item_estq.id_emp and
                    tb_nf_item.id_item = tb_item_estq.id_item and
                    tb_nf_item.id_dep = tb_item_estq.id_dep and
                    tb_nf_item.id_grd_estq = tb_item_estq.id_grd_estq and
                    tb_nf_item.mov_estq and
                    tb_nf.tp_op = '[E]' and
                    tb_nf.dt_op >= _dt) as vl_nf_ent,
                --
                -- Nota fiscal saida
                --
                (select sum(tb_nf_item.vl_qtd_mov)
                 from tb_nf
                 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.id_emp = tb_item_estq.id_emp and
                    tb_nf_item.id_item = tb_item_estq.id_item and
                    tb_nf_item.id_dep = tb_item_estq.id_dep and
                    tb_nf_item.id_grd_estq = tb_item_estq.id_grd_estq and
                    tb_nf_item.mov_estq and
                    tb_nf.tp_op = '[S]' and
                    tb_nf.dt_op >= _dt) as vl_nf_sai,
                --
                -- Manutencao de estoque - saida
                --
                (select sum(tb_mnt_estq_item.vl_qtd_mov)
                 from tb_mnt_estq
                 join tb_mnt_estq_item on
                    (tb_mnt_estq.id_emp = tb_mnt_estq_item.id_emp and
                     tb_mnt_estq.id_mnt_estq = tb_mnt_estq_item.id_mnt_estq)
                 where
                    tb_mnt_estq.id_emp = tb_item_estq.id_emp and
                    tb_mnt_estq_item.id_item = tb_item_estq.id_item and
                    tb_mnt_estq_item.id_dep_dst = tb_item_estq.id_dep and
                    tb_mnt_estq_item.id_grd_estq = tb_item_estq.id_grd_estq and
                    tb_mnt_estq.op = 'S' and
                    tb_mnt_estq.dt_mnt_estq >= _dt) as vl_mnt_estq_sai,
                --
                -- Manutencao de estoque - entrada
                --
                (select sum(tb_mnt_estq_item.vl_qtd_mov)
                 from tb_mnt_estq
                 join tb_mnt_estq_item on
                    (tb_mnt_estq.id_emp = tb_mnt_estq_item.id_emp and
                     tb_mnt_estq.id_mnt_estq = tb_mnt_estq_item.id_mnt_estq)
                 where
                    tb_mnt_estq.id_emp = tb_item_estq.id_emp and
                    tb_mnt_estq_item.id_item = tb_item_estq.id_item and
                    tb_mnt_estq_item.id_dep_dst = tb_item_estq.id_dep and
                    tb_mnt_estq_item.id_grd_estq = tb_item_estq.id_grd_estq and
                    tb_mnt_estq.op = 'E' and
                    tb_mnt_estq.dt_mnt_estq >= _dt) as vl_mnt_estq_ent,
                --
                -- Manutencao de estoque - Transferencia saida
                --
                (select sum(tb_mnt_estq_item.vl_qtd_mov)
                 from tb_mnt_estq
                 join tb_mnt_estq_item on
                    (tb_mnt_estq.id_emp = tb_mnt_estq_item.id_emp and
                     tb_mnt_estq.id_mnt_estq = tb_mnt_estq_item.id_mnt_estq)
                 where
                    tb_mnt_estq.id_emp = tb_item_estq.id_emp and
                    tb_mnt_estq_item.id_item = tb_item_estq.id_item and
                    tb_mnt_estq_item.id_dep_dst = tb_item_estq.id_dep and
                    tb_mnt_estq_item.id_grd_estq = tb_item_estq.id_grd_estq and
                    tb_mnt_estq.op = 'T' and
                    tb_mnt_estq.dt_mnt_estq >= _dt) as vl_mnt_estq_trans_sai,
                --
                -- Manutencao de estoque - Transferencia entrada
                --
                (select sum(tb_mnt_estq_item.vl_qtd_mov)
                 from tb_mnt_estq
                 join tb_mnt_estq_item on
                    (tb_mnt_estq.id_emp = tb_mnt_estq_item.id_emp and
                     tb_mnt_estq.id_mnt_estq = tb_mnt_estq_item.id_mnt_estq)
                 where
                    tb_mnt_estq.id_emp = tb_item_estq.id_emp and
                    tb_mnt_estq_item.id_item = tb_item_estq.id_item and
                    tb_mnt_estq_item.id_dep_org = tb_item_estq.id_dep and
                    tb_mnt_estq_item.id_grd_estq = tb_item_estq.id_grd_estq and
                    tb_mnt_estq.op = 'T' and
                    tb_mnt_estq.dt_mnt_estq >= _dt) as vl_mnt_estq_trans_ent,
                    
                    
               (select sum(tb_ped_item.vl_qtd_mov) 
                from tb_carreg
                join tb_carreg_ped on
                    (tb_carreg.id_emp = tb_carreg_ped.id_emp and
                     tb_carreg.id_carreg = tb_carreg_ped.id_carreg)
                join tb_ped on
                    (tb_carreg_ped.id_emp = tb_ped.id_emp and
                     tb_carreg_ped.id_ped = tb_ped.id_ped)
                join tb_ped_item on
                    (tb_ped.id_emp = tb_ped_item.id_emp and
                     tb_ped.id_ped = tb_ped_item.id_ped)
                where
                    tb_ped.id_emp = tb_item_estq.id_emp and
                    tb_ped_item.id_item = tb_item_estq.id_item and
                    tb_ped.tp_ped in ('CAR') and
                    tb_ped.dt_ped >= _dt and
                    tb_carreg.id_dep_org = tb_item_estq.id_dep and
                    tb_ped.sit = 'F') as vl_carreg_sai
                    
            from tb_item_estq
            join tb_item on
                (tb_item_estq.id_item = tb_item.id_item)
            where
                tb_item_estq.id_emp = _id_emp and
                tb_item_estq.id_dep = _id_dep and
                tb_item_estq.id_grd_estq = _id_grd_estq
                
                
        ) as tb;
        
    fetch _list into _item;
    
    _id_invt_item := 1;
    
    while found loop
        insert into tb_invt_item
            (id_emp, id_invt, id_invt_item, id_item, id_grd_estq, 
             id_und_med,  vl_qtd, id_und_med_mov, vl_qtd_mov)
        values
            (_id_emp, _id_invt, _id_invt_item, _item.id_item, _id_grd_estq,
             _item.id_und_med, _item.vl_estq_atu, _item.id_und_med, _item.vl_estq_atu);
    
        _id_invt_item := _id_invt_item + 1;
        
        fetch _list into _item;
        
    end loop;
    
    
end $$;