--show statement_timeout
set statement_timeout to 0;
delete from prov_comp_oc_det;
delete from ordenes_compra_det;
delete from remitos_trazas;
delete from comp_trazas;

--delete from pedidos_cons;
delete from prov_comp_rem_det;
delete from service.serv_comp;
delete from repartos_detalles;

delete from wms.tareas_preparados;
delete from pedidos_cons;
delete from wms.pallets_remitos;
--delete from remitos;--hacerlo por partes 

--select count (*) from remitos;
--select * from remitos where id_remito=1;
--delete from remitos where id_remito in (
--select id_remito from remitos limit 25000);

--delete from remitos;

--delete from remitos_detalles;
--delete from remitos_recepciones;

delete from comisiones_liq;
delete from comisiones_liq_det;
delete from service.serv_comp;

delete from cd_rd;

delete from repartos;

delete from ofertas_precios;
--delete from articulos_combos;

--delete from ensamble.op_det_componentes;
--delete from ensamble.op_det;
delete from comp_det_nc;
delete from comp_det;
delete from cn.subdiarios_cabeceras_comprobantes;
delete from cupones;
delete from cajas_comp_pagos;
delete from cajas_comp_cuotas;
delete from comp;
select count(id_comp) from comp_det;

--truncate table cd_rd;
--truncate table comp_det cascade;
--truncate table comp cascade;

--vacuum full verbose;

--truncate tabe articulos_imagenes;

--delete from lotes;

delete from comisiones_reglas;
--update departamentos set id_articulo=null;
delete from ventas;
--delete from articulos_lotes_unitarios;
--delete from articulos_libres;
--delete from enlaces_detalles;
--delete from enlaces;
--delete from articulos_services;

--delete from remitos_detalles;
--delete from existencias;

--delete from articulos_garantias_ext;
delete from conteos_detalles;
--delete from articulos;--hacerlo por partes;

--select * from articulos;

--select count (*) from articulos;

--ALTER TABLE articulos DROP CONSTRAINT fk_id_envase;

--delete from promos_identificaciones;
--delete from promos_regalos;
--delete from articulos;
delete from wms.pallets_articulos;
delete from wms.articulos;
--delete from sujetos_descuentos;
delete from comisiones_objetivos_venta_det;
delete from fidelizacion.valores_puntos;
delete from ofertas_reglas;
delete from ofertas;
--delete from articulos_sustitutos;
--delete from articulos_complementarios;
--delete from articulos where id_articulo in (
--select id_articulo from articulos limit 4206)

--ALTER TABLE articulos
--  ADD CONSTRAINT fk_id_envase FOREIGN KEY (id_envase)
--      REFERENCES articulos (id_articulo) MATCH SIMPLE
--      ON UPDATE NO ACTION ON DELETE NO ACTION;

--delete from marcas;

--delete from articulos_activos;--hacerlo por partes;
--delete from departamentos;
delete from cn.subdiarios_cabeceras_valores;
delete from cn.subdiarios_cabeceras_orden_pago;
delete from cn.subdiarios_cabeceras_prov_comp;
delete from cn.subdiarios_cabeceras_comprobantes;

-----------------------------------------------------------
delete from cn.subdiarios_cabeceras_orden_pago_anul;
delete from cn.subdiarios_cabeceras_recibos;
delete from cn.subdiarios_cabeceras;
--delete from cn.subdiarios_abiertos;
--------------------------------------------------------------
delete from orden_pago_det_anuladas;
delete from cupones_acreditaciones;
delete from valores;
delete from comp_ret_det;


delete from orden_pago_det;
--------------------------------------------------------------

ALTER TABLE prov_comp DROP CONSTRAINT chk_pago_menor_total;
delete from prov_comp_antic;

  
delete from cajas_otros_comp;
delete from cajas_otros;
--delete from cd_rd;
--delete from marcas;

--delete from planesf_familias;
--delete from familias;
delete from cn.subdiarios_cabeceras_orden_pago;

delete from orden_pago_sobrante;
delete from orden_pago;
delete from ordenes_compra;
--delete from precios_activos;

delete from cn.subdiarios_cabeceras_prov_comp;
delete from comisiones_reglas;
----------------------------------------------------------------------*

delete from prov_comp;

delete from recibos_transferencias;

delete from recibos_anulados;
delete from recibos;
--delete from planesf_rubros;
--delete from rubros;
----------

update comp set id_sujeto=null;
--delete from sujetos_relaciones;
delete from service.serv;
------------

-- Check: chk_pago_menor_total

--ALTER TABLE comp_pagos DROP CONSTRAINT chk_pago_menor_total;
delete from cajas_comp_pagos;
--ALTER TABLE comp_pagos ADD CONSTRAINT chk_pago_menor_total CHECK (pago_menor_total(pago, monto * comp_mon_cotiz_base(id_comp)));

delete from comp_pagos;
---------------------------------------------------------------------------------------------------
---delete from pedidos_hr_fact;
delete from liq_det;
--delete from pedidos_hr_cobranza;

delete from comp_pend_compensados;
delete from cajas where id_caja>1;
--delete from areas_exist_usu_aut;
--update areas set id_cajero_predet  = null;
--update areas set id_sujeto = null;
--update usuarios set id_area  = null;
delete from listados;

delete from sujetos_clientes_carpetas_creditos;

delete from cn.subdiarios_cabeceras;

-----*******************************************------------------
--delete from tecnet.mo_parte;
delete from bancos_extractos;

--delete from sujetos_clientes_obse;
--delete from sujetos_clientes_estado_cuenta_gestion;
--delete from sujetos_situaciones_laborales;
delete from cn.planes_cuentas_codigos_tipos_pago;

--delete from sujetos_clientes_condventa;
delete from comp_ret;

--update administradoras set id_sujeto = null;
--update centros_productivos set id_cliente = null;

delete from pedidos_hr;

--update sujetos set id_lug_tra = null;
--update sujetos set id_conyuge = null;

delete from liq;
delete from fidelizacion.fidelizacion_cta_cte;

--delete from cn.planes_cuentas_codigos_asientos_aut;
--delete from sujetos; Hacerlo por partes


--select * from sujetos

--select count (*) from sujetos;

--delete from valores_cambio_estado;

--delete from sujetos where id_sujeto in (
--select id_sujeto from sujetos limit 10000)

delete from conteos;
delete from comisiones_objetivos_venta;
delete from comisiones_objetivos_cobranza;

delete from wms.incidencias;
delete from wms.tareas;

delete from wms.usuarios_sectores_permitidos;
delete from cajas_supervisores;
--delete from usuarios where id_usuario NOT IN ('sistemas','system ','cajero','migracion'); --?sistem y cajero dejar y ver si hay algun otro.

--delete from sujetos_clientes_obse;
--delete from sujetos_clientes_estado_cuenta_gestion;
--delete from sujetos_situaciones_laborales;
--------------------------------------------------------------------
delete from liq;
--delete from ensamble.op;

delete from fidelizacion.fidelizacion_cta_cte;
--delete from pedidos_hr;
--delete from centros_productivos;
--update administradoras set id_sujeto=null
--delete from planesf_areas;
--delete from remitos_recepciones;
--delete from areas_dep;
delete from cupones_presentaciones;
delete from cupones_terminales;

delete from existencias_audit;

--truncate table comp cascade;
--update areas set id_padre=null; 
--delete from areas where id_area>1;
delete from comp_ret;
delete from liq;
--delete from sujetos_tarjetas;
--delete from sujetos where id_sujeto > 1;
delete from bancos_extractos;
--delete from transporte;

--delete from cn.planes_cuentas_codigos_bancos_cuentas;
--delete from bancos_cuentas;
delete from prov_comp_cierre_mes;
--------
--eliminar usuarios que no se van a usar
--select count(id_sujeto) from sujetos;
--insertar consumidor final
--insert into sujetos (id_sujeto,sujeto,id_tipo_doc,id_condicion,doc,direccion) values (1,'CONSUMIDOR FINAL',2,5,'00000000','d');
--

ALTER TABLE prov_comp
  ADD CONSTRAINT chk_pago_menor_total CHECK (pago_menor_total(pago, imp_total));

delete from wms.pallets_remitos;
select * from wms.pallets_remitos;

--truncate table replicacion.rep_instrucciones cascade;

vacuum analyze;
vacuum full verbose;

ALTER DATABASE fvgbelgrano SET statement_timeout=480000; 
ALTER DATABASE fvgbelgrano SET enable_seqscan='off'; 


--select max(idu_rep_instruccion) from replicacion.rep_instrucciones where idu_db = 1;
--delete from replicacion.rep_instrucciones where idu_rep_instruccion <> (select max(idu_rep_instruccion) from replicacion.rep_instrucciones where idu_db = 1);
--vacuum full verbose replicacion.rep_instrucciones;

--select * from replicacion.rep_instrucciones;
