Costear todas las ventas y devoluciones como primer paso

declare cursor xx is select distinct bodega,articulo from vendetfactura
where costo=0;
 vcosto vendetfactura.costo%type;
begin
  for a in xx loop
    begin
      select cos_total/can_existe into vcosto
      from inv_lote_artic_bodega
      where cod_cia='001'
      and   cod_bodega=a.bodega
      and   cod_articulo=a.articulo
      and   can_existe<>0;
    exception
      when no_data_found then
         vcosto:=0;
    end;
    if vcosto=0 then
      begin
        select costo into vcosto
        from predetlista
        where cod_cia='001'
        and   cod_lista_precio='001'
        and   cod_articulo=a.articulo;
       exception
         when no_data_found then
            vcosto:=0;
       end;
    end if;
    update vendetfactura
    set costo=vcosto
    where cia='001'
    and articulo=a.articulo
    and costo=0;
    commit;
  end loop;
end;   


declare cursor xx is select distinct articulo from vendetdevolucion
where costo=0;
 vcosto vendetfactura.costo%type;
begin
  for a in xx loop
    begin
      select cos_total/can_existe into vcosto
      from inv_lote_artic_bodega
      where cod_cia='001'
      and   cod_bodega=a.bodega
      and   cod_articulo=a.articulo
      and   can_existe<>0;
    exception
      when no_data_found then
         vcosto:=0;
    end;
    if vcosto=0 then
      begin
        select costo into vcosto
        from predetlista
        where cod_cia='001'
        and   cod_lista_precio='001'
        and   cod_articulo=a.articulo;
       exception
         when no_data_found then
            vcosto:=0;
       end;
    end if;
    update vendetdevolucion
    set costo=vcosto
    where cia='001'
    and articulo=a.articulo
    and costo=0;
  end loop;
end;   

-----------------------
--- desde aqui --------
-----------------------

create global temporary table esttmp
(
 CIA                 CHAR(3)           NOT NULL,
 SUCURSAL            CHAR(3)           NOT NULL,
 BODEGA              CHAR(3)           NOT NULL,
 MONEDA              VARCHAR2(3)       NOT NULL,
 PERIODO             NUMBER(8)         NOT NULL,
 CLIENTE             VARCHAR2(25)      NOT NULL,
 AGENTE              VARCHAR2(3)       NOT NULL,
 ARTICULO            VARCHAR2(25)      NOT NULL,
 CANTIDADVEN         NUMBER(12,4)      NOT NULL,
 COSTOVEN            NUMBER(14,2)      NOT NULL,
 IMPTOVEN            NUMBER(14,2)      NOT NULL,
 MONTOVEN            NUMBER(14,2)      NOT NULL,
 CANTIDADDEV         NUMBER(12,4)      NOT NULL,
 COSTODEV            NUMBER(14,2)      NOT NULL,
 IMPTODEV            NUMBER(14,2)      NOT NULL,
 MONTODEV            NUMBER(14,2)      NOT NULL
)
on commit preserve rows;


-- Veriest.Recalcula(pcia in varchar2, pper in number);

Insert InTo EstDet
    (cia, sucursal, cliente, agente, articulo, periodo, moneda,
 		montoven, cantidadven, costoven, montodev, cantidaddev, costodev)
select '001','001',cliente,agente,articulo,to_number(periodo),'CL',
     sum(montoven),sum(cantidadven),sum(costoven),sum(montodev),sum(cantidaddev),sum(costodev)
from xx
group by '001','001',cliente,agente,articulo,to_number(periodo),'CL'
             

declare 
cursor xx is select cia, sucursal, cliente, agenteventa, articulo, to_number(periodo) periodo, moneda,
	sum(montoven) montoven, sum(cantidadven) cantidadven, sum(costoven) costoven,
        sum(montodev) montodev, sum(cantidaddev) cantidaddev, sum(costodev) costodev
        from xx
        group by cia, sucursal, cliente, agenteventa, articulo, periodo, moneda;

 n number(10):=0;
begin
  for a in xx loop
    n:=n+1;
    if mod(n,1500)=0 then 
       commit;
    end if;
    insert into estdet values(a.CIA,a.SUCURSAL,a.MONEDA,a.PERIODO,a.CLIENTE,a.AGENTEVENTA,
            a.ARTICULO,a.CANTIDADVEN,a.COSTOVEN,a.MONTOVEN,a.CANTIDADDEV,a.COSTODEV,a.MONTODEV);
   end loop;
   commit;
end;
              
