drop table xx;
create table xx as select * from inv_articulo
where des_articulo='Libre';

delete inv_artic_bodega
where cod_articulo in (select cod_articulo from xx);

delete inv_lote_artic_bodega
where cod_articulo in (select cod_articulo from xx);

delete inv_artic_prov
where cod_articulo in (select cod_articulo from xx);

delete inv_lote
where cod_articulo in (select cod_articulo from xx);

delete inv_detmovimiento
where cod_articulo in (select cod_articulo from xx);

commit;

update vendetfactura
set articulo='9999'
where articulo in (select cod_articulo from xx);

update vendetdevolucion
set articulo='9999'
where articulo in (select cod_articulo from xx);

update estadis
set articulo='9999'
where articulo in (select cod_articulo from xx);

commit;

drop table estadis;
create table estadis as
select * from estdet
where articulo in (select cod_articulo from xx);

insert into estadis
select * from estdet
where articulo='9999';

delete estdet
where articulo='9999';

update estadis
set articulo='9999';

commit;

Insert InTo EstDet
      (cia, sucursal, bodega ,cliente, agente, articulo, periodo, moneda,
     montoven, cantidadven, costoven, montodev, cantidaddev, costodev,imptoven,imptodev)
     select cia, sucursal, bodega, cliente, agente, articulo, to_number(periodo), moneda,
     sum(montoven), sum(cantidadven), sum(costoven), sum(montodev), sum(cantidaddev), 
     sum(costodev),sum(imptoven),sum(imptodev)
     from estadis
    group by cia, sucursal, bodega, cliente, agente, articulo, periodo, moneda;

commit;

drop table invacum;

create table invacum
 select * from inv_acum_periodo
 where cod_articulo in (select cod_articulo from xx);

insert into invacum
  select * from inv_acum_periodo
  where cod_articulo='9999';

commit;

delete inv_acum_periodo
where cod_articulo='9999';

delete inv_acum_periodo
where cod_articulo in (select cod_articulo from xx);

update invacum
set cod_articulo='9999';

commit;

insert into inv_acum_periodo
 select cod_cia,sucursal,cod_bodega,cod_articulo,ano_fiscal,per_proceso,clase_movto,
  sum(cantidad),sum(monto)
  from invacum
  group by cod_cia,sucursal,cod_bodega,cod_articulo,ano_fiscal,per_proceso,clase_movto;

delete inv_articulo
where cod_articulo in (select cod_articulo from xx);


commit;

