--
--


SELECT pg_catalog.set_config('search_path', '', false);

--
-- Name: plpgsql; Type: EXTENSION; Schema: -; Owner: -
--

CREATE EXTENSION IF NOT EXISTS plpgsql WITH SCHEMA pg_catalog;


--
-- Name: EXTENSION plpgsql; Type: COMMENT; Schema: -; Owner: 
--

COMMENT ON EXTENSION plpgsql IS 'PL/pgSQL procedural language';


--
-- Name: unaccent; Type: EXTENSION; Schema: -; Owner: -
--

CREATE EXTENSION IF NOT EXISTS unaccent WITH SCHEMA public;


--
-- Name: EXTENSION unaccent; Type: COMMENT; Schema: -; Owner: 
--

COMMENT ON EXTENSION unaccent IS 'text search dictionary that removes accents';


--
-- Name: act_talon(integer); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.act_talon(integer) RETURNS integer
    LANGUAGE sql
    AS $_$select tal_valor from talon where tal_id = $1;$_$;


ALTER FUNCTION public.act_talon(integer) OWNER TO rulemanes;

--
-- Name: actualizar_fecha_modificacion(); Type: FUNCTION; Schema: public; Owner: zona
--

CREATE FUNCTION public.actualizar_fecha_modificacion() RETURNS trigger
    LANGUAGE plpgsql
    AS $$
BEGIN
    NEW.rec_fecha_actualizacion = CURRENT_TIMESTAMP;
    RETURN NEW;
END;
$$;


ALTER FUNCTION public.actualizar_fecha_modificacion() OWNER TO zona;

--
-- Name: anioastoh(integer); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.anioastoh(integer) RETURNS smallint
    LANGUAGE sql
    AS $_$select ash_anio from astoh where ash_id= $1;$_$;


ALTER FUNCTION public.anioastoh(integer) OWNER TO rulemanes;

--
-- Name: ashdeastc(integer); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.ashdeastc(integer) RETURNS integer
    LANGUAGE sql
    AS $_$select astc_idastoh from astoctas where astc_id= $1 ;$_$;


ALTER FUNCTION public.ashdeastc(integer) OWNER TO rulemanes;

--
-- Name: astcdecomp(integer, integer); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.astcdecomp(integer, integer) RETURNS integer
    LANGUAGE sql
    AS $_$select astc_id from astoctas where astc_idastoh= $1 and astc_ctacble= $2 limit 1 ;$_$;


ALTER FUNCTION public.astcdecomp(integer, integer) OWNER TO rulemanes;

--
-- Name: astcdecompximp(integer, integer, numeric); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.astcdecompximp(integer, integer, numeric) RETURNS integer
    LANGUAGE sql
    AS $_$select astc_id from astoctas where astc_idastoh= $1 and astc_ctacble= $2 and astc_importe= $3 limit 1 ;$_$;


ALTER FUNCTION public.astcdecompximp(integer, integer, numeric) OWNER TO rulemanes;

--
-- Name: buscar_sin_acentos(text); Type: FUNCTION; Schema: public; Owner: zona
--

CREATE FUNCTION public.buscar_sin_acentos(texto text) RETURNS text
    LANGUAGE plpgsql
    AS $$
BEGIN
    RETURN unaccent(lower(texto));
END;
$$;


ALTER FUNCTION public.buscar_sin_acentos(texto text) OWNER TO zona;

--
-- Name: cantchq(integer); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.cantchq(integer) RETURNS bigint
    LANGUAGE sql
    AS $_$select count(*) from cheques where chq_idastoc= $1;$_$;


ALTER FUNCTION public.cantchq(integer) OWNER TO rulemanes;

--
-- Name: cantchq(integer, integer); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.cantchq(integer, integer) RETURNS bigint
    LANGUAGE plpgsql
    AS $_$
declare
astc	 alias for $1;
modo	 alias for $2;
ret	 integer;
begin

ret:=0;

if(int4eq(modo,1)) then
    select count(*) into ret from cheques where chq_idastoc= astc;
else
    select count(*) into ret from cheques where chq_idastoce= astc;
end if;

return ret;
end;$_$;


ALTER FUNCTION public.cantchq(integer, integer) OWNER TO rulemanes;

--
-- Name: cargo_articu_receta(integer); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.cargo_articu_receta(integer) RETURNS boolean
    LANGUAGE plpgsql
    AS $_$
declare
elcodemp	 alias for $1;
ret		 boolean;
registro	 record;
codigoart	 character varying;
elidarticu	 integer;
begin
ret:='f';

	for registro in select * from articvta where art_codemp=elcodemp order by 1
	    loop
		insert into articu (artc_codemp,artc_idptov,artc_codigo,artc_tasa,artc_stock,artc_nivrep,artc_unimed,artc_present,artc_peso,artc_estado,artc_codrub,artc_codsrb,artc_automatoper,artc_nombre,artc_leyen,artc_tipo,artc_codcbl,artc_funcion,artc_precio,artc_idcbl)
		     values (registro.art_codemp,registro.art_idptov,registro.art_codigo,registro.art_tasa,registro.art_stock,registro.art_nivrep,registro.art_unimed,registro.art_present,registro.art_peso,registro.art_estado,registro.art_codrub,registro.art_codsrb,registro.art_automatoper,registro.art_nombre,registro.art_leyen,registro.art_tipo,registro.art_codcbl,registro.art_funcion,registro.art_precio,registro.art_idcbl);

		select artc_id into elidarticu from articu where artc_codigo=registro.art_codigo;

		insert into receta (re_codemp,re_idptov,re_articv,re_articc,re_canti,re_estado) values (elcodemp,registro.art_idptov,registro.art_id,elidarticu,1,1);

		ret:='t';
	    end loop;
    return ret;
end;$_$;


ALTER FUNCTION public.cargo_articu_receta(integer) OWNER TO rulemanes;

--
-- Name: cblarticu(integer); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.cblarticu(integer) RETURNS integer
    LANGUAGE sql
    AS $_$select artc_idcbl from articu where artc_id=$1;$_$;


ALTER FUNCTION public.cblarticu(integer) OWNER TO rulemanes;

--
-- Name: cierre_ejercicio(character varying, date, integer, integer, integer); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.cierre_ejercicio(character varying, date, integer, integer, integer) RETURNS boolean
    LANGUAGE plpgsql
    AS $_$
declare
comprobante	 alias for $1;
fecha		 alias for $2;
tipo		 alias for $3; -- 1 Resultado, 2 Patrimoniales
codemp		 alias for $4;
idptov		 alias for $5;
ret 		 boolean;
registro	 record;
elcomprob 	 integer;
cuentardo 	 integer;
nrocomp 	 integer;
cuenta 	 	 integer;
subcuenta 	 integer;
resultad 	 numeric(15,2);
tituloa	 	 character varying;
idastoh 	 integer;
begin
ret:='f';
    select idcomp(comprobante,idptov) into elcomprob;
    select numtalon(idptov,elcomprob,1) into nrocomp;
    select comp_nombre || ' EJERCICIO CERRADO AL ' || fecha into tituloa from comprob where comp_id=elcomprob;

    insert into astoh (ash_codemp,ash_idptov,ash_anio,ash_mes,ash_comprob,ash_numero,ash_titulo,ash_fecomp,ash_fecval,ash_leyen,ash_nro2,ash_estado) 
	values (codemp,idptov,date_part('year',fecha)::int2,date_part('month',fecha)::int2,elcomprob,nrocomp,tituloa,fecha,fecha,'Cierre de Cuentas del Ejercicio ',0,-1);

    select ash_id into idastoh from astoh where ash_codemp=codemp and ash_idptov=idptov and ash_comprob=elcomprob and ash_numero=nrocomp;

    select idcbl(codemp,valvaramb(codemp,0,'CTARDOE')::integer)  into cuentardo;   

    if(int4eq(tipo,1::int4)) then
	for registro in select 
	    astc_ctacble, sum(astc_importe * -1) as cuanto from astoctas 
	      where astc_estado=2 and astc_codemp=codemp and date_le(astc_fecval,fecha) and codcble(astc_ctacble)>=3000 and codcomp(astc_comprob) not in ('AR','Ar') and astc_fecval>'30-06-2008' group by 1  
	    loop
		if(registro.cuanto != 0) then
		    insert into astoctas (astc_idastoh,astc_ctacble,astc_importe,astc_codemp,astc_idptov,astc_comprob,astc_numero,astc_fecval,astc_estado) 
    	    	     values (idastoh,registro.astc_ctacble,registro.cuanto,codemp,idptov,elcomprob,nrocomp,fecha,-1);	
		end if;
	    end loop; 

	select sum(astc_importe * -1) into resultad from astoctas where astc_idastoh=idastoh;

	
	if(resultad != 0) then
	    insert into astoctas (astc_idastoh,astc_ctacble,astc_importe,astc_codemp,astc_idptov,astc_comprob,astc_numero,astc_fecval,astc_estado) 
    	     values (idastoh,cuentardo,resultad,codemp,idptov,elcomprob,nrocomp,fecha,-1);	
	end if;
	ret:='t';


      else

	for registro in select 
	    astc_ctacble, sum(astc_importe * -1) as cuanto from astoctas 
	      where astc_estado=2 and astc_codemp=codemp and astc_idptov=idptov and date_le(astc_fecval,fecha) and codcomp(astc_comprob) not in ('AR','Ar') and astc_fecval>'31-12-2011'  group by 1
	    loop
		if(registro.cuanto != 0) then
		    insert into astoctas (astc_idastoh,astc_ctacble,astc_importe,astc_codemp,astc_idptov,astc_comprob,astc_numero,astc_fecval,astc_estado) 
		     values (idastoh,registro.astc_ctacble,registro.cuanto,codemp,idptov,elcomprob,nrocomp,fecha,-1);	
		end if;
	    end loop; 

	ret:='t';

    end if;
    return ret;
end;$_$;


ALTER FUNCTION public.cierre_ejercicio(character varying, date, integer, integer, integer) OWNER TO rulemanes;

--
-- Name: cliedeastc(integer); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.cliedeastc(integer) RETURNS integer
    LANGUAGE sql
    AS $_$select astc_ctacte from astoctas where astc_idastoh= $1 and astc_ctacble=idcbl(astc_codemp,valvaramb(astc_codemp,astc_idptov,'DEUDXV')::integer) limit 1;$_$;


ALTER FUNCTION public.cliedeastc(integer) OWNER TO rulemanes;

--
-- Name: clienteemi(integer); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.clienteemi(integer) RETURNS integer
    LANGUAGE sql
    AS $_$select emi_codcli from emiti where emi_id = $1 ;$_$;


ALTER FUNCTION public.clienteemi(integer) OWNER TO rulemanes;

--
-- Name: climodfac(integer); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.climodfac(integer) RETURNS smallint
    LANGUAGE sql
    AS $_$select cli_modfac from clientes where cli_id= $1 ;$_$;


ALTER FUNCTION public.climodfac(integer) OWNER TO rulemanes;

--
-- Name: codartic(integer); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.codartic(integer) RETURNS character
    LANGUAGE sql
    AS $_$select art_codigo from articvta where art_id= $1 ;$_$;


ALTER FUNCTION public.codartic(integer) OWNER TO rulemanes;

--
-- Name: codcble(integer); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.codcble(integer) RETURNS smallint
    LANGUAGE sql
    AS $_$select cbl_codigo::int2 from ctacble where cbl_id= $1 ;$_$;


ALTER FUNCTION public.codcble(integer) OWNER TO rulemanes;

--
-- Name: codcli(integer); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.codcli(integer) RETURNS character varying
    LANGUAGE sql
    AS $_$select cli_codigo from clientes where cli_id= $1 ;$_$;


ALTER FUNCTION public.codcli(integer) OWNER TO rulemanes;

--
-- Name: codclideastoh(integer); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.codclideastoh(integer) RETURNS integer
    LANGUAGE sql
    AS $_$select emi_codcli from emiti where emi_idastoh= $1 ;$_$;


ALTER FUNCTION public.codclideastoh(integer) OWNER TO rulemanes;

--
-- Name: codcomp(integer); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.codcomp(integer) RETURNS character
    LANGUAGE sql
    AS $_$select comp_codigo from comprob where comp_id= $1 ;$_$;


ALTER FUNCTION public.codcomp(integer) OWNER TO rulemanes;

--
-- Name: codcompash(integer); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.codcompash(integer) RETURNS character
    LANGUAGE sql
    AS $_$select comp_codigo from comprob,astoh where comp_id=ash_comprob and ash_id= $1 ;$_$;


ALTER FUNCTION public.codcompash(integer) OWNER TO rulemanes;

--
-- Name: codcompastc(integer); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.codcompastc(integer) RETURNS character
    LANGUAGE sql
    AS $_$select comp_codigo from comprob,astoctas where comp_id=astc_comprob and astc_id= $1 ;$_$;


ALTER FUNCTION public.codcompastc(integer) OWNER TO rulemanes;

--
-- Name: codctes(integer); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.codctes(integer) RETURNS integer
    LANGUAGE sql
    AS $_$select cte_codigo from ctes where cte_id= $1 ;$_$;


ALTER FUNCTION public.codctes(integer) OWNER TO rulemanes;

--
-- Name: codempcli(integer); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.codempcli(integer) RETURNS integer
    LANGUAGE sql
    AS $_$select cli_codemp from clientes where cli_id= $1;$_$;


ALTER FUNCTION public.codempcli(integer) OWNER TO rulemanes;

--
-- Name: codprov(integer); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.codprov(integer) RETURNS character varying
    LANGUAGE sql
    AS $_$select prov_codigo from prove where prov_id= $1 ;$_$;


ALTER FUNCTION public.codprov(integer) OWNER TO rulemanes;

--
-- Name: codprovdeastoh(integer); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.codprovdeastoh(integer) RETURNS integer
    LANGUAGE sql
    AS $_$select cpra_prove from compra where cpra_idastoh= $1 ;$_$;


ALTER FUNCTION public.codprovdeastoh(integer) OWNER TO rulemanes;

--
-- Name: codptv(integer); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.codptv(integer) RETURNS integer
    LANGUAGE sql
    AS $_$select ptv_codigo from ptovta where ptv_id= $1 ;$_$;


ALTER FUNCTION public.codptv(integer) OWNER TO rulemanes;

--
-- Name: compbte_fe(character varying); Type: FUNCTION; Schema: public; Owner: zona
--

CREATE FUNCTION public.compbte_fe(character varying) RETURNS integer
    LANGUAGE plpgsql
    AS $_$
    DECLARE
	comp		ALIAS FOR $1; -- Comprobante 
    BEGIN
	if trim(comp)='FA' then
	    RETURN 1;
	  else
	    if trim(comp)='DA' then
		RETURN 2;
	      else
		if trim(comp)='CA' then
		    RETURN 3;
		  else
		    if trim(comp)='FB' then
			RETURN 6;
		      else
			if trim(comp)='DB' then
			    RETURN 7;
			  else
			    if trim(comp)='CB' then
				RETURN 8;
			    else
				if trim(comp)='FM' then
				    RETURN 51;
				else
				    if trim(comp)='FCE' then
					RETURN 201;
				    else
					if trim(comp)='CCE' then
					    RETURN 203;
					end if;
				    end if;
				end if;
			    end if;
			end if;
		    end if;
		end if;
	    end if;
	end if;
    END;$_$;


ALTER FUNCTION public.compbte_fe(character varying) OWNER TO zona;

--
-- Name: compbte_fec(character varying, character varying, numeric); Type: FUNCTION; Schema: public; Owner: zona
--

CREATE FUNCTION public.compbte_fec(character varying, character varying, numeric) RETURNS integer
    LANGUAGE plpgsql
    AS $_$
    DECLARE
	comp		ALIAS FOR $1; -- Comprobante 
	letra		ALIAS FOR $2; -- letra 
	iva		ALIAS FOR $3; -- importe iva 
    BEGIN
	if trim(comp)!='NK' and trim(comp)!='DK' then
	    if trim(letra)='A' then
		RETURN 1;
	    else
		if trim(letra)='B' then
		    RETURN 6;
		else
		    if trim(letra)='C' then
			RETURN 11;
		    else
			if trim(letra)='E' then
			    RETURN 19;
			else
			    if trim(letra)='G' then
				RETURN 39;
			    else
				if trim(letra)='M' then
				    RETURN 51;
				else
				    if trim(letra)='T' then
					if iva!=0 then
					    RETURN 81;
					else
					    RETURN 83;
					end if;
				    end if;
				end if;
			    end if;
			end if;
		    end if;
		end if;
	    end if;
	else
	    if trim(comp)='NK' then
		if trim(letra)='A' then
		    RETURN 3;
		else
		    if trim(letra)='B' then
			RETURN 8;
		    else
			if trim(letra)='C' then
			    RETURN 13;
			else
			    if trim(letra)='E' then
				RETURN 21;
			    else
				if trim(letra)='M' then
				    RETURN 53;
				else
				    if trim(letra)='T' then
					if iva!=0 then
					    RETURN 112;
					else
					    RETURN 111;
					end if;
				    end if;
				end if;
			    end if;
			end if;
		    end if;
		end if;
	    else
		if trim(letra)='A' then
		    RETURN 2;
		else
		    if trim(letra)='B' then
			RETURN 7;
		    else
			if trim(letra)='C' then
			    RETURN 12;
			else
			    if trim(letra)='E' then
				RETURN 20;
			    else
				if trim(letra)='M' then
				    RETURN 52;
				else
				    if trim(letra)='T' then
					if iva!=0 then
					    RETURN 115;
					else
					    RETURN 116;
					end if;
				    end if;
				end if;
			    end if;
			end if;
		    end if;
		end if;
	    end if;
	end if;
    END;$_$;


ALTER FUNCTION public.compbte_fec(character varying, character varying, numeric) OWNER TO zona;

--
-- Name: compdeastc(integer); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.compdeastc(integer) RETURNS integer
    LANGUAGE sql
    AS $_$select astc_comprob from astoctas where astc_id= $1 ;$_$;


ALTER FUNCTION public.compdeastc(integer) OWNER TO rulemanes;

--
-- Name: compnro(integer); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.compnro(integer) RETURNS character
    LANGUAGE sql
    AS $_$select a.comp_codigo::character varying||'  -  '||b.ash_numero from comprob a, astoh  b where b.ash_id= $1 and b.ash_comprob=a.comp_id;$_$;


ALTER FUNCTION public.compnro(integer) OWNER TO rulemanes;

--
-- Name: compnrodeastoctas(integer); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.compnrodeastoctas(integer) RETURNS character varying
    LANGUAGE sql
    AS $_$select comp_codigo::character varying||' - '||astc_numero 
    from comprob,astoctas where comp_id=astc_comprob and astc_id= $1 ;$_$;


ALTER FUNCTION public.compnrodeastoctas(integer) OWNER TO rulemanes;

--
-- Name: comprobemi(integer); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.comprobemi(integer) RETURNS integer
    LANGUAGE sql
    AS $_$select emi_comprob from emiti where emi_id = $1 ;$_$;


ALTER FUNCTION public.comprobemi(integer) OWNER TO rulemanes;

--
-- Name: conchequsado(integer); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.conchequsado(integer) RETURNS bigint
    LANGUAGE sql
    AS $_$select count(*) from cheques,astoctas where chq_idastoc=astc_id and astc_idastoh= $1 and chq_idastoce is not null ;$_$;


ALTER FUNCTION public.conchequsado(integer) OWNER TO rulemanes;

--
-- Name: condocusado(integer); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.condocusado(integer) RETURNS bigint
    LANGUAGE sql
    AS $_$select count(*) from docum,astoctas where doc_idastoc=astc_id and astc_idastoh= $1 and doc_idastoce is not null ;$_$;


ALTER FUNCTION public.condocusado(integer) OWNER TO rulemanes;

--
-- Name: condolusado(integer); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.condolusado(integer) RETURNS bigint
    LANGUAGE sql
    AS $_$select count(*) from monex,astoctas where mne_idastoc=astc_id and astc_idastoh= $1 and mne_idastoce is not null ;$_$;


ALTER FUNCTION public.condolusado(integer) OWNER TO rulemanes;

--
-- Name: conivacli(integer); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.conivacli(integer) RETURNS smallint
    LANGUAGE sql
    AS $_$select cli_coniva from clientes where cli_id= $1 ;$_$;


ALTER FUNCTION public.conivacli(integer) OWNER TO rulemanes;

--
-- Name: conivaprov(integer); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.conivaprov(integer) RETURNS integer
    LANGUAGE sql
    AS $_$select prov_coniva from prove where prov_id= $1 ;$_$;


ALTER FUNCTION public.conivaprov(integer) OWNER TO rulemanes;

--
-- Name: conivash(integer); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.conivash(integer) RETURNS smallint
    LANGUAGE sql
    AS $_$select conivacli(astc_ctacte) from astoctas where astc_idastoh= $1 and astc_tipcta=99 and astc_ctacble=idcbl(astc_codemp,valvaramb(astc_codemp,1,'DEUDXV')::integer);$_$;


ALTER FUNCTION public.conivash(integer) OWNER TO rulemanes;

--
-- Name: conpagcli(integer); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.conpagcli(integer) RETURNS integer
    LANGUAGE sql
    AS $_$select cli_conpag from clientes where cli_id= $1 ;$_$;


ALTER FUNCTION public.conpagcli(integer) OWNER TO rulemanes;

--
-- Name: ctonetoib(integer, numeric); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.ctonetoib(integer, numeric) RETURNS numeric
    LANGUAGE plpgsql
    AS $_$
declare
idpago		 alias for $1;
alicuota	 alias for $2;
neto		 numeric(15,2);
totaux		 numeric(15,2);
codemp		 integer;
idptov		 integer;
cta_retib	 integer;
cta_retibc	 integer;
cta_retiva	 integer;
cta_retiva1	 integer;
cta_iva		 integer;
codcomp		 character varying;
r		 record;
begin
    neto:=0;
    select ash_codemp,ash_idptov into codemp,idptov from astoh where ash_id=idpago;
    select idcbl(codemp,valvaramb(codemp,idptov,'CTAPERIB')::integer) into cta_retib;
    select idcbl(codemp,valvaramb(codemp,idptov,'CTAPERIBC')::integer) into cta_retibc;
    select idcbl(codemp,valvaramb(codemp,idptov,'CTAPERIVA')::integer) into cta_retiva;
    select idcbl(codemp,valvaramb(codemp,idptov,'CTAPERIVA1')::integer) into cta_retiva1;
    select idcbl(codemp,valvaramb(codemp,idptov,'CTAIVAC')::integer) into cta_iva;
    for r in select * from agrupa where ag_idpago=idpago order by 1
	loop
	    if r.ag_idastoh>0 then
		select trim(codcompash(r.ag_idastoh)) into codcomp;
		if codcomp is distinct from 'PP' and codcomp is distinct from 'OP' then
		    select sum(astc_importe) into totaux from astoctas where astc_idastoh=r.ag_idastoh and astc_ctacble not in (cta_iva,cta_retib,cta_retibc,cta_retiva,cta_retiva1) and astc_tasa is not null;
		    neto:=neto + totaux;
		end if;
	    end if;
	end loop;
    return neto;
end;$_$;


ALTER FUNCTION public.ctonetoib(integer, numeric) OWNER TO rulemanes;

--
-- Name: ctoretib(integer, numeric); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.ctoretib(integer, numeric) RETURNS numeric
    LANGUAGE plpgsql
    AS $_$
declare
idpago		 alias for $1;
alicuota	 alias for $2;
neto		 numeric(15,2);
reten		 numeric(15,2);
deboreten	 numeric(15,2);
totpag		 numeric(15,2);
totfac		 numeric(15,2);
totaux		 numeric(15,2);
imppag		 numeric(15,2);
tasa		 numeric(15,2);
codemp		 integer;
idptov		 integer;
cta_retib	 integer;
cta_retibc	 integer;
cta_retiva	 integer;
cta_retiva1	 integer;
cta_iva		 integer;
codcomp		 character varying;
r		 record;
begin
    neto:=0;
    select sum(ag_importe) into totpag from agrupa where ag_idpago=idpago;
    select ash_codemp,ash_idptov into codemp,idptov from astoh where ash_id=idpago;
    select idcbl(codemp,valvaramb(codemp,idptov,'CTAPERIB')::integer) into cta_retib;
    select idcbl(codemp,valvaramb(codemp,idptov,'CTAPERIBC')::integer) into cta_retibc;
    select idcbl(codemp,valvaramb(codemp,idptov,'CTAPERIVA')::integer) into cta_retiva;
    select idcbl(codemp,valvaramb(codemp,idptov,'CTAPERIVA1')::integer) into cta_retiva1;
    select idcbl(codemp,valvaramb(codemp,idptov,'CTAIVAC')::integer) into cta_iva;
    for r in select * from agrupa where ag_idpago=idpago order by 1
	loop
	    deboreten:=0;
	    select trim(codcompash(r.ag_idastoh)) into codcomp;
	    if codcomp is distinct from 'PP' and codcomp is distinct from 'OP' then
		select sum(astc_importe) into totaux from astoctas where astc_idastoh=r.ag_idastoh and astc_ctacble not in (cta_iva,cta_retib,cta_retibc,cta_retiva,cta_retiva1) and astc_tasa is not null;
		deboreten:=totaux * alicuota / 100;
		if(numeric_ne(r.ag_retenido,0)) then
		    deboreten:=deboreten - r.ag_retenido;
		end if;
		neto:=neto + deboreten;
	    end if;
	end loop;
    return neto;
end;$_$;


ALTER FUNCTION public.ctoretib(integer, numeric) OWNER TO rulemanes;

--
-- Name: cual_comprob(integer, integer, integer); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.cual_comprob(integer, integer, integer) RETURNS integer
    LANGUAGE plpgsql
    AS $_$
    DECLARE
	empori		ALIAS FOR $1; 
	empdest		ALIAS FOR $2;
	dato		ALIAS FOR $3;
	cual		character varying;
	retorna		integer;
    BEGIN
	select comp_codigo into cual from comprob where comp_id=dato;
	select comp_id into retorna from comprob where comp_codemp=empdest and bpchareq(comp_codigo,cual);
	RETURN retorna;
    END;$_$;


ALTER FUNCTION public.cual_comprob(integer, integer, integer) OWNER TO rulemanes;

--
-- Name: cual_cuenta(integer, integer, character varying); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.cual_cuenta(integer, integer, character varying) RETURNS integer
    LANGUAGE plpgsql
    AS $_$
    DECLARE
	empori		ALIAS FOR $1; 
	empdest		ALIAS FOR $2;
	dato		ALIAS FOR $3;
	cual		integer;
	retorna		integer;
    BEGIN
	select codcble(comp_cuenta) into cual from comprob where comp_codemp=empori and bpchareq(comp_codigo,dato);
	select cbl_id into retorna from ctacble where cbl_codemp=empdest and int4eq(cbl_codigo::integer,cual);
	RETURN retorna;
    END;$_$;


ALTER FUNCTION public.cual_cuenta(integer, integer, character varying) OWNER TO rulemanes;

--
-- Name: cual_idsub(integer, integer, integer); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.cual_idsub(integer, integer, integer) RETURNS integer
    LANGUAGE plpgsql
    AS $_$
    DECLARE
	empori		ALIAS FOR $1; 
	empdest		ALIAS FOR $2;
	dato		ALIAS FOR $3;
	cual		character varying;
	retorna		integer;
    BEGIN
	select subd_nombre into cual from subd where subd_id=dato;
	select subd_id into retorna from subd where subd_codemp=empdest and bpchareq(subd_nombre,cual);
	RETURN retorna;
    END;$_$;


ALTER FUNCTION public.cual_idsub(integer, integer, integer) OWNER TO rulemanes;

--
-- Name: cual_moneda(character varying); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.cual_moneda(character varying) RETURNS smallint
    LANGUAGE sql
    AS $_$select mon_codigo from moneda where bpchareq(trim(mon_simbol),trim($1));$_$;


ALTER FUNCTION public.cual_moneda(character varying) OWNER TO rulemanes;

--
-- Name: cuanto_facturep(integer, integer, date, date); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.cuanto_facturep(integer, integer, date, date) RETURNS numeric
    LANGUAGE sql
    AS $_$select sum(astc_importe) from astoctas where astc_idptov= $1 and astc_estado=2 and astc_tipcta=99 and astc_ctacte= $2 and date_ge(astc_fecval,$3) and date_lt(astc_fecval,$4) and codcomp(astc_comprob) in ('FA','FB','CA','RE');$_$;


ALTER FUNCTION public.cuanto_facturep(integer, integer, date, date) OWNER TO rulemanes;

--
-- Name: cuit_fe(character varying); Type: FUNCTION; Schema: public; Owner: zona
--

CREATE FUNCTION public.cuit_fe(character varying) RETURNS character
    LANGUAGE sql
    AS $_$select regexp_replace(regexp_replace(trim($1), '-', ''),'-','');$_$;


ALTER FUNCTION public.cuit_fe(character varying) OWNER TO zona;

--
-- Name: cuitcli(integer); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.cuitcli(integer) RETURNS character
    LANGUAGE sql
    AS $_$select cli_nrcuit from clientes where cli_id= $1 ;$_$;


ALTER FUNCTION public.cuitcli(integer) OWNER TO rulemanes;

--
-- Name: cuitprov(integer); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.cuitprov(integer) RETURNS character
    LANGUAGE sql
    AS $_$select prov_nrcuit from prove where prov_id= $1 ;$_$;


ALTER FUNCTION public.cuitprov(integer) OWNER TO rulemanes;

--
-- Name: datos_emp_nueva(integer, integer, integer, integer); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.datos_emp_nueva(integer, integer, integer, integer) RETURNS boolean
    LANGUAGE plpgsql
    AS $_$
declare
codempo		 alias for $1;
codempd		 alias for $2;
ptovtao		 alias for $3;
ptovtad		 alias for $4;
ret 		 boolean;
registro	 record;
regi		 record;
elcomprob 	 integer;
eltalon 	 integer;
existe	 	 integer;
subdid	 	 integer;
compid	 	 integer;
compcodigo	character varying;
subdnombre	character varying;
begin
ret:='f';

	for registro in select * from comprob where comp_codemp=codempo and comp_idptov=ptovtao
	    loop
		insert into comprob (comp_codemp,comp_idptov,comp_codigo,comp_nombre,comp_copias) 
		    values (codempd,ptovtad,registro.comp_codigo,registro.comp_nombre,1);

		select comp_id into elcomprob from comprob where comp_codemp=codempd and comp_idptov=ptovtad and comp_codigo=registro.comp_codigo;

		insert into talon (tal_codemp,tal_idptov,tal_comprob,tal_nombre,tal_valor,tal_fecha,tal_vald,tal_valh) values (codempd,ptovtad,elcomprob,registro.comp_nombre,1,'01-01-2015',1,999999);

		select tal_id into eltalon from talon where tal_codemp=codempd and tal_idptov=ptovtad and tal_comprob=elcomprob;

		update comprob set comp_talon=eltalon where comp_id=elcomprob;
		ret:='t';
	    end loop;

	insert into varambi (vari_codemp,vari_idptov,vari_codigo,vari_nombre,vari_valor,vari_tipo) 
	    select codempd,ptovtad,vari_codigo,vari_nombre,vari_valor,vari_tipo from varambi where vari_codemp=codempo and vari_idptov=ptovtao;

	select vari_codemp into existe from varambi where vari_codemp=codempd and vari_idptov=0 limit 1;
	if not found then
	    insert into varambi (vari_codemp,vari_idptov,vari_codigo,vari_nombre,vari_valor,vari_tipo) select codempd,ptovtad,vari_codigo,vari_nombre,vari_valor,vari_tipo from varambi where vari_codemp=codempo and vari_idptov=0;
	end if;

	insert into ctacble (cbl_codemp,cbl_codigo,cbl_nombre,cbl_identi,cbl_nivel,cbl_posic,cbl_asigna,cbl_moneda,cbl_tipcta,cbl_monetaria,cbl_ajuste,cbl_indice,cbl_ctarei,cbl_ctaaju,cbl_leyed,cbl_leyeh,cbl_present) 
	    select codempd,cbl_codigo,cbl_nombre,cbl_identi,cbl_nivel,cbl_posic,cbl_asigna,cbl_moneda,cbl_tipcta,cbl_monetaria,cbl_ajuste,cbl_indice,cbl_ctarei,cbl_ctaaju,cbl_leyed,cbl_leyeh,cbl_present from ctacble where cbl_codemp=codempo;

	insert into conpag (cdp_codemp,cdp_nombre,cdp_dias) 
	    select codempd,cdp_nombre,cdp_dias from conpag where cdp_codemp=codempo;

	insert into convmult (cmt_codemp,cmt_provin,cmt_tasa,cmt_coef) 
	    select codempd,cmt_provin,cmt_tasa,cmt_coef from convmult where cmt_codemp=codempo;

	insert into cotiza (cot_codemp,cot_moneda,cot_fecha,cot_cotiz) 
	    select codempd,cot_moneda,cot_fecha,cot_cotiz from cotiza where cot_codemp=codempo;

	insert into cblexasi (cxa_codemp,cxa_tipasi,cxa_cble) 
	    select codempd,cxa_tipasi,cxa_cble from cblexasi where cxa_codemp=codempo;

	insert into interes (int_codemp,int_fecha,int_valor) 
	    select codempd,int_fecha,int_valor from interes where int_codemp=codempo;

	insert into subd (subd_codemp,subd_idptov,subd_nombre,subd_titulo,subd_fecd,subd_fech,subd_estado,subd_proxnum,subd_orden,subd_dia) 
	    select codempd,ptovtad,subd_nombre,subd_titulo,subd_fecd,subd_fech,subd_estado,subd_proxnum,subd_orden,subd_dia from subd where subd_codemp=codempo;

	insert into tipasi (tas_codemp,tas_codigo,tas_nombre) 
	    select codempd,tas_codigo,tas_nombre from tipasi where tas_codemp=codempo;

	for regi in select * from subxcomp where subxc_codemp=codempo
	    loop

	    select subd_nombre into subdnombre from subd where subd_id=regi.subxc_idsub;
	    select subd_id into subdid from subd where subd_codemp=codempd and bpchareq(subd_nombre,subdnombre);

	    select comp_codigo into compcodigo from comprob where comp_id=regi.subxc_comprob;
	    select comp_id into compid from comprob where comp_codemp=codempd and bpchareq(comp_codigo,compcodigo);

	    insert into subxcomp (subxc_idsub,subxc_comprob,subxc_codemp) values (subdid,compid,codempd);
	    end loop;
    return ret;
end;$_$;


ALTER FUNCTION public.datos_emp_nueva(integer, integer, integer, integer) OWNER TO rulemanes;

--
-- Name: descartic(integer); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.descartic(integer) RETURNS character
    LANGUAGE sql
    AS $_$select art_codigo||'  -  '||art_nombre from articvta where art_id= $1 ;$_$;


ALTER FUNCTION public.descartic(integer) OWNER TO rulemanes;

--
-- Name: desccble(integer); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.desccble(integer) RETURNS character
    LANGUAGE sql
    AS $_$select cbl_codigo||'  -  '||cbl_nombre from ctacble where cbl_id= $1 ;$_$;


ALTER FUNCTION public.desccble(integer) OWNER TO rulemanes;

--
-- Name: desccblep(integer); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.desccblep(integer) RETURNS character
    LANGUAGE sql
    AS $_$select cbl_present||'  -  '||cbl_nombre from ctacble where cbl_id= $1 ;$_$;


ALTER FUNCTION public.desccblep(integer) OWNER TO rulemanes;

--
-- Name: descchq(smallint); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.descchq(smallint) RETURNS character
    LANGUAGE sql
    AS $_$select bco_codigo||'  -  '||bco_nombre from bancos where bco_codigo= $1 ;$_$;


ALTER FUNCTION public.descchq(smallint) OWNER TO rulemanes;

--
-- Name: descchqf(integer); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.descchqf(integer) RETURNS character varying
    LANGUAGE sql
    AS $_$select bco_nombre||'  - Nro: '||chq_nucheq||'  - Fecha: '||chq_fecheq||'  - Importe: $ '||chq_importe from cheques,bancos where chq_codbco=bco_codigo and chq_id= $1 ;$_$;


ALTER FUNCTION public.descchqf(integer) OWNER TO rulemanes;

--
-- Name: descchqt(integer); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.descchqt(integer) RETURNS character varying
    LANGUAGE sql
    AS $_$select bco_nombre||'  - Nro: '||chq_nucheq||'  - Fecha: '||chq_fecheq||'  - Importe: $ '||chq_importe from cheques,bancos where chq_codbco=bco_codigo and chq_id= $1 and chq_idastoce is not null ;$_$;


ALTER FUNCTION public.descchqt(integer) OWNER TO rulemanes;

--
-- Name: desccli(integer); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.desccli(integer) RETURNS character
    LANGUAGE sql
    AS $_$select cli_codigo||'  -  '||cli_nombre from clientes where cli_id= $1 ;$_$;


ALTER FUNCTION public.desccli(integer) OWNER TO rulemanes;

--
-- Name: desccli(integer, integer); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.desccli(integer, integer) RETURNS character
    LANGUAGE plpgsql
    AS $_$
    DECLARE
	cliente		ALIAS FOR $1; 
	sucursal	ALIAS FOR $2; 
	cuantos 	integer;
	nomcli 		varchar;
	nomsuc 		varchar;
	codcli		varchar;
	retval 		varchar;
    BEGIN

    select count(*) into cuantos from sucursalc where int4eq(cliente,sucu_cliente);
     if int4eq(cuantos,1) then
	select cli_codigo,cli_nombre into codcli,nomcli from clientes where cli_id=cliente;
	retval:= codcli ||'  -  '|| nomcli;
    else
	select cli_codigo,cli_nombre into codcli,nomcli from clientes where int4eq(cli_id,cliente);
	select sucu_nombre into nomsuc from sucursalc where int4eq(sucu_cliente,cliente::int4) and int2eq(sucu_codigo,sucursal::int2);
	retval:= codcli ||'  -  '|| nomcli ||' - '|| nomsuc;
    end if;

    RETURN retval;
    END;$_$;


ALTER FUNCTION public.desccli(integer, integer) OWNER TO rulemanes;

--
-- Name: desccli_emiti(integer); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.desccli_emiti(integer) RETURNS character
    LANGUAGE sql
    AS $_$select a.cli_codigo||'  -  '||a.cli_nombre from clientes a, emiti b where a.cli_id=b.emi_codcli and b.emi_id= $1 ;$_$;


ALTER FUNCTION public.desccli_emiti(integer) OWNER TO rulemanes;

--
-- Name: desccomp(integer); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.desccomp(integer) RETURNS character
    LANGUAGE sql
    AS $_$select trim(comp_codigo)::character varying||'  -  '||rpad(comp_nombre,40,' ') from comprob where comp_id= $1 ;$_$;


ALTER FUNCTION public.desccomp(integer) OWNER TO rulemanes;

--
-- Name: desccompdeastoctas(integer); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.desccompdeastoctas(integer) RETURNS character varying
    LANGUAGE sql
    AS $_$select comp_codigo::character varying||'  -  '||comp_nombre||'  Nro:  '||astc_numero 
    from comprob,astoctas where comp_id=astc_comprob and astc_id= $1 ;$_$;


ALTER FUNCTION public.desccompdeastoctas(integer) OWNER TO rulemanes;

--
-- Name: desccompdeastoh(integer); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.desccompdeastoh(integer) RETURNS character varying
    LANGUAGE sql
    AS $_$select comp_codigo::character varying||'  -  '||comp_nombre||'  Nro:  '||ash_numero 
    from comprob,astoh where comp_id=ash_comprob and ash_id= $1 ;$_$;


ALTER FUNCTION public.desccompdeastoh(integer) OWNER TO rulemanes;

--
-- Name: descctes(integer); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.descctes(integer) RETURNS character
    LANGUAGE sql
    AS $_$select cte_codigo||'  -  '||cte_nombre from ctes where cte_id= $1 ;$_$;


ALTER FUNCTION public.descctes(integer) OWNER TO rulemanes;

--
-- Name: descemp(integer, integer); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.descemp(integer, integer) RETURNS character
    LANGUAGE plpgsql
    AS $_$
    DECLARE
	empres		ALIAS FOR $1; 
	ptovt		ALIAS FOR $2; 
	cuantos		integer;
	nomemp 		varchar;
	nomptov		varchar;
	retval 		varchar;
    BEGIN
    
    select count(*) into cuantos from
    	 ptovta where int4eq(empres,ptv_codemp);
    
	 if int4eq(cuantos,1) then
		select emp_nombre into retval from empresa where int4eq(empres,emp_codigo);
	    else
		select emp_nombre into nomemp from empresa where int4eq(empres,emp_codigo);
		select ptv_nombre into nomptov from ptovta where int4eq(ptovt,ptv_id) and int4eq(empres,ptv_codemp);
		retval:= nomemp ||'  -  '|| nomptov;
	 end if;		
    RETURN retval;
    END;$_$;


ALTER FUNCTION public.descemp(integer, integer) OWNER TO rulemanes;

--
-- Name: descempptv(integer); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.descempptv(integer) RETURNS character
    LANGUAGE sql
    AS $_$select a.emp_nombre||'  -  '||b.ptv_nombre from ptovta b, empresa a where a.emp_codigo=b.ptv_codemp and ptv_id= $1 ;$_$;


ALTER FUNCTION public.descempptv(integer) OWNER TO rulemanes;

--
-- Name: descfacg(integer, integer, integer, integer); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.descfacg(integer, integer, integer, integer) RETURNS character varying
    LANGUAGE sql
    AS $_$select desccomp(astc_comprob)||'  Nro  '||astc_numero from astoctas where astc_codemp= $1 and astc_tipcta= $2 and astc_ctacte= $3 and astc_grupo= $4 order by astc_fecval asc,astc_importe desc limit 1;$_$;


ALTER FUNCTION public.descfacg(integer, integer, integer, integer) OWNER TO rulemanes;

--
-- Name: descfactcpra(integer); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.descfactcpra(integer) RETURNS character varying
    LANGUAGE sql
    AS $_$select 'Prov: '|| cpra_nomprov || '  Factura:  '|| cpra_lecomp ||' - '|| lpad(cpra_sucomp::text,4,'0') ||' - '|| lpad(cpra_nucomp::text,8,'0') from compra where cpra_id= $1 ;$_$;


ALTER FUNCTION public.descfactcpra(integer) OWNER TO rulemanes;

--
-- Name: descfc(integer); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.descfc(integer) RETURNS character varying
    LANGUAGE sql
    AS $_$select cpra_lecomp ||' - '|| lpad(cpra_sucomp::text,4,'0') ||' - '|| lpad(cpra_nucomp::text,8,'0') from compra where cpra_idastoh= $1 ;$_$;


ALTER FUNCTION public.descfc(integer) OWNER TO rulemanes;

--
-- Name: descmon(smallint); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.descmon(smallint) RETURNS character
    LANGUAGE sql
    AS $_$select mon_codigo||'  -  '||mon_nombre from moneda where mon_codigo= $1 ;$_$;


ALTER FUNCTION public.descmon(smallint) OWNER TO rulemanes;

--
-- Name: descprov(integer); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.descprov(integer) RETURNS character
    LANGUAGE sql
    AS $_$select prov_codigo||'  -  '||prov_nombre from prove where prov_id= $1 ;$_$;


ALTER FUNCTION public.descprov(integer) OWNER TO rulemanes;

--
-- Name: descptv(integer); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.descptv(integer) RETURNS character
    LANGUAGE sql
    AS $_$select ptv_codigo||'  -  '||ptv_nombre from ptovta where ptv_id= $1 ;$_$;


ALTER FUNCTION public.descptv(integer) OWNER TO rulemanes;

--
-- Name: deudacli(integer, integer); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.deudacli(integer, integer) RETURNS numeric
    LANGUAGE plpgsql
    AS $_$
    DECLARE
	codemp		ALIAS FOR $1; 
	ctacte		ALIAS FOR $2;
	saldo 		numeric;
	auxsaldo	numeric;
	auximp		numeric;
	mes		integer;
	anio		integer;
	tipcta		integer;
	registro	record;
    BEGIN

    select valvaramb(codemp,0,'TIPCTADXV') into tipcta;
    select saldocli(codemp,tipcta,ctacte) into saldo;

    auxsaldo:=0;
    select date_part('month',astc_fecval)::integer,date_part('year',astc_fecval)::integer into mes,anio from astoctas where astc_tipcta=tipcta and astc_ctacte=ctacte and astc_estado=2 and codcomp(astc_comprob) not in ('RC') and astc_codemp=codemp order by astc_fecval desc limit 1;

    if(numeric_gt(saldo,0)) then
	for registro in select *,date_part('month',astc_fecval)::integer as mes1,date_part('year',astc_fecval)::integer as anio1 from astoctas where astc_tipcta=tipcta and astc_ctacte=ctacte and astc_estado=2 and codcomp(astc_comprob) not in ('RC') and astc_codemp=codemp order by astc_fecval desc loop 
	    if saldo>0 then RAISE NOTICE 'comprob %,numero %, importe %, auxsaldo %, saldo %',codcomp(registro.astc_comprob),registro.astc_numero,registro.astc_importe,auxsaldo,saldo; end if;

	    if(int4ne(registro.mes1,mes) or int4ne(registro.anio1,anio)) then 
		if(numeric_ne(auxsaldo,0)) then
		    if(numeric_ge(auxsaldo,saldo)) then
			insert into deudas (dd_mes,dd_anio,dd_cliente,dd_importe,dd_codemp) values (mes,anio,ctacte,saldo,codemp);
			saldo:=0;
		    else
			insert into deudas (dd_mes,dd_anio,dd_cliente,dd_importe,dd_codemp) values (mes,anio,ctacte,auxsaldo,codemp);
			saldo:=saldo-auxsaldo;
		    end if;
		    auxsaldo:=0;
		end if;

		if(numeric_ne(saldo,0)) then
		    auximp:=auxsaldo+registro.astc_importe;
		    if(numeric_ge(auximp,saldo)) then
			insert into deudas (dd_mes,dd_anio,dd_cliente,dd_importe,dd_codemp) values (registro.mes1,registro.anio1,ctacte,saldo,codemp);
			saldo:=0;
			auxsaldo:=0;
		    else
			auxsaldo:=auxsaldo+registro.astc_importe;
		    end if;
		end if;
		mes:=registro.mes1;
		anio:=registro.anio1;
	    else
		if(numeric_ne(saldo,0)) then
		    auximp:=auxsaldo+registro.astc_importe;
		    if(numeric_ge(auximp,saldo)) then
			insert into deudas (dd_mes,dd_anio,dd_cliente,dd_importe,dd_codemp) values (mes,anio,ctacte,saldo,codemp);
			saldo:=0;
			auxsaldo:=0;
		    else
			auxsaldo:=auxsaldo+registro.astc_importe;
		    end if;
		end if;
		mes:=registro.mes1;
		anio:=registro.anio1;
	    end if;

	    if saldo>0 then RAISE NOTICE 'FIN comprob %,numero %, importe %, auxsaldo %, saldo %, periodo %-%',codcomp(registro.astc_comprob),registro.astc_numero,registro.astc_importe,auxsaldo,saldo,mes,anio; end if;

	end loop;
    else
	if numeric_ne(saldo,0) then
	    select date_part('month',astc_fecval)::integer,date_part('year',astc_fecval)::integer into mes,anio from astoctas where astc_tipcta=tipcta and astc_ctacte=ctacte and astc_estado=2 and codcomp(astc_comprob) not in ('RC') and astc_codemp=codemp order by astc_fecval desc limit 1;
	    insert into deudas (dd_mes,dd_anio,dd_cliente,dd_importe,dd_codemp) values (mes,anio,ctacte,saldo,codemp);
	end if;
    end if;

    RETURN 1;
    END;$_$;


ALTER FUNCTION public.deudacli(integer, integer) OWNER TO rulemanes;

--
-- Name: deudsinvencer(integer, integer, date); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.deudsinvencer(integer, integer, date) RETURNS numeric
    LANGUAGE plpgsql
    AS $_$
declare
codemp		 alias for $1;
cliente		 alias for $2;
fechah		 alias for $3;
ret 		 numeric;
registro	 record;
cuanto		 numeric;
ctadxv		 integer;
begin

ret:=0;

select idcbl(codemp, valvaramb(codemp,0,'DEUDXV')::smallint) into ctadxv;

cuanto:=0;

	for registro in select astc_importe from astoctas,emiti,conpag 
	    where astc_idastoh=emi_idastoh 
		and emi_conpag=cdp_codigo 
		and astc_estado=2 
		and astc_codemp=codemp 
		and astc_ctacble=ctadxv 
		and astc_tipcta=99 
		and astc_ctacte=cliente
		and astc_cierra=1 
		and date_gt((astc_fecval + cdp_dias),fechah)
		order by 1 desc
	    loop
	    cuanto:=registro.astc_importe + cuanto;
	    end loop;

	for registro in select astc_importe from astoctas
	    where astc_idastoh not in (select emi_idastoh from emiti where emi_codcli=cliente and emi_estado=2) 
		and astc_estado=2 
		and astc_codemp=codemp 
		and astc_ctacble=ctadxv 
		and astc_tipcta=99 
		and astc_ctacte=cliente
		and astc_cierra=1 
		and astc_importe<0
		and date_gt(astc_fecval,fechah)
		order by 1 desc
	    loop
	    cuanto:=registro.astc_importe + cuanto;
	    end loop;



    ret:=cuanto;
    return ret;
end;$_$;


ALTER FUNCTION public.deudsinvencer(integer, integer, date) OWNER TO rulemanes;

--
-- Name: diasconpag(integer); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.diasconpag(integer) RETURNS smallint
    LANGUAGE sql
    AS $_$select cdp_dias from conpag where cdp_codigo= $1 ;$_$;


ALTER FUNCTION public.diasconpag(integer) OWNER TO rulemanes;

--
-- Name: dsctocli(integer); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.dsctocli(integer) RETURNS numeric
    LANGUAGE sql
    AS $_$select cli_descto from clientes where cli_id= $1;$_$;


ALTER FUNCTION public.dsctocli(integer) OWNER TO rulemanes;

--
-- Name: elclinew(integer, character varying); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.elclinew(integer, character varying) RETURNS integer
    LANGUAGE sql
    AS $_$select cli_id from clientes where cli_codemp= $1 and bpchareq(trim(cli_nombre),trim($2));$_$;


ALTER FUNCTION public.elclinew(integer, character varying) OWNER TO rulemanes;

--
-- Name: elprecio(integer, integer, integer, character varying); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.elprecio(integer, integer, integer, character varying) RETURNS numeric
    LANGUAGE plpgsql
    AS $_$
    DECLARE
	codemp		ALIAS FOR $1;
	ptovta		ALIAS FOR $2;
	cli		ALIAS FOR $3;
	articulo	ALIAS FOR $4;
	idart 		integer;
	retval 		numeric(15,2);
    BEGIN

	select idarticvta(codemp, ptovta, articulo) into idart;
	if not found then
	    select idarticvta(codemp, -1, articulo) into idart;
	end if;

	select ltp_precio into retval from listpre where int4eq(ltp_codemp,codemp) and int4eq(ltp_idptov,ptovta) and ltp_estado=1 and int4eq(cli,ltp_idcli) and int4eq(idart,ltp_idart);
	if not found then
	    select ltp_precio into retval from listpre where int4eq(ltp_codemp,codemp) and int4eq(ltp_idptov,-1) and ltp_estado=1 and int4eq(cli,ltp_idcli) and int4eq(idart,ltp_idart);
	    if not found then
		select art_precio into retval from articvta where int4eq(idart,art_id);
	    end if;
	end if;

	if retval is null then
	    retval:=0;
	end if;
    RETURN retval;
    END;$_$;


ALTER FUNCTION public.elprecio(integer, integer, integer, character varying) OWNER TO rulemanes;

--
-- Name: elprecioid(integer, integer, integer, integer); Type: FUNCTION; Schema: public; Owner: elm
--

CREATE FUNCTION public.elprecioid(integer, integer, integer, integer) RETURNS numeric
    LANGUAGE plpgsql
    AS $_$
    DECLARE
	codemp		ALIAS FOR $1;
	ptovta		ALIAS FOR $2;
	cli		ALIAS FOR $3;
	idart		ALIAS FOR $4;
	nrolista	integer;
	retval 		numeric;
    BEGIN
	select cli_listpre into nrolista from clientes where cli_id=cli;
	select ltp_precio into retval from listpre where ltp_estado=1 and int4eq(cli,ltp_idcli) and int4eq(idart,ltp_idart) and ltp_nrolista=nrolista and ltp_codemp=codemp and ltp_idptov in (-1,ptovta);
	if not found then
	    select ltp_precio into retval from listpre where ltp_estado=1 and int4eq(cli,ltp_idcli) and int4eq(idart,ltp_idart) and ltp_codemp=codemp and ltp_idptov in (-1,ptovta);
	    if not found then
		select ltp_precio into retval from listpre where ltp_estado=1 and int4eq(idart,ltp_idart) and ltp_codemp=codemp and ltp_idptov in (-1,ptovta) and ltp_nrolista=nrolista and ltp_idcli is null;
	    end if;
	end if;

	if retval is null then
	    retval:=0;
	end if;
    RETURN retval;
    END;$_$;


ALTER FUNCTION public.elprecioid(integer, integer, integer, integer) OWNER TO elm;

--
-- Name: elptovnew(integer, integer); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.elptovnew(integer, integer) RETURNS integer
    LANGUAGE plpgsql
    AS $_$
declare
codemp 		 alias for $1;
oldptv 		 alias for $2;
ptov1		 integer;
ptov2		 integer;
ret		 integer;
begin

select ptv_id into ptov2 from ptovta where ptv_codemp=codemp and bpchareq(trim(ptv_nombre),'PUNTO DE VENTA 2');
select ptv_id into ptov1 from ptovta where ptv_codemp=codemp and bpcharne(trim(ptv_nombre),'PUNTO DE VENTA 2');

if int4eq(oldptv,4) then
    ret:=ptov1;
  else
    ret:=ptov2;
end if;
return ret;

end;$_$;


ALTER FUNCTION public.elptovnew(integer, integer) OWNER TO rulemanes;

--
-- Name: eltalonnew(integer, character varying); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.eltalonnew(integer, character varying) RETURNS integer
    LANGUAGE sql
    AS $_$select tal_id from talon where tal_codemp= $1 and bpchareq(trim(tal_nombre),trim($2));$_$;


ALTER FUNCTION public.eltalonnew(integer, character varying) OWNER TO rulemanes;

--
-- Name: estadoart(integer); Type: FUNCTION; Schema: public; Owner: zona
--

CREATE FUNCTION public.estadoart(integer) RETURNS integer
    LANGUAGE sql
    AS $_$select art_estado from articvta where art_id= $1;$_$;


ALTER FUNCTION public.estadoart(integer) OWNER TO zona;

--
-- Name: esunico(integer, integer, integer, integer); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.esunico(integer, integer, integer, integer) RETURNS integer
    LANGUAGE plpgsql
    AS $_$
declare
    codemp	 alias for $1;
    ptovta	 alias for $2;
    articv	 alias for $3;
    articc	 alias for $4;
    cuantos	 integer;
    registro	 record;
    ret		 integer;
    begin
    ret:=2;

	for registro in select 
	    * from receta where re_codemp=codemp and re_idptov=ptovta and re_articc=articc
	loop
	    if int4eq(registro.re_articv,articv) then
		select count(re_articc) into cuantos from receta where re_codemp=codemp and re_idptov=ptovta and re_articv=registro.re_articv;
		if int4eq(cuantos,1) then
		    ret:=1; 
		end if;
	    end if;
	end loop;
    return ret;
end;$_$;


ALTER FUNCTION public.esunico(integer, integer, integer, integer) OWNER TO rulemanes;

--
-- Name: eval_funcion(text); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.eval_funcion(text) RETURNS numeric
    LANGUAGE plpgsql
    AS $_$
    DECLARE
	res		record;
    BEGIN
	for res in execute 'select '||$1||' as result' loop
	return res.result;
	end loop;
END;$_$;


ALTER FUNCTION public.eval_funcion(text) OWNER TO rulemanes;

--
-- Name: f_ai_articvta(); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.f_ai_articvta() RETURNS trigger
    LANGUAGE plpgsql
    AS $$
DECLARE
regi		record;
BEGIN
	for regi in select * from listas where li_codemp=NEW.art_codemp 
	    loop
		insert into listpre (ltp_codemp,ltp_idptov,ltp_nrolista,ltp_idart,ltp_orden,ltp_precio) 
			values (NEW.art_codemp,NEW.art_idptov,regi.li_codigo,NEW.art_id,NEW.art_codrub,0);
	    end loop;
    RETURN NEW;
END;$$;


ALTER FUNCTION public.f_ai_articvta() OWNER TO rulemanes;

--
-- Name: f_ai_astoctas(); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.f_ai_astoctas() RETURNS trigger
    LANGUAGE plpgsql
    AS $$
    DECLARE
	ctabco		integer;
	tctabco		integer;
    BEGIN
    select idcbl(NEW.astc_codemp, valvaramb(NEW.astc_codemp,NEW.astc_idptov,'BANCO')::smallint) into ctabco;
    select valvaramb(NEW.astc_codemp,NEW.astc_idptov,'TCTBANCO') into tctabco;

    if(int4eq(NEW.astc_ctacble,ctabco))  THEN 

	if(NEW.astc_fecvto is null)  THEN 
	    update astoctas set astc_fecvto=NEW.astc_fecval where astc_id=NEW.astc_id;
	END IF;

	if (NEW.astc_tipcta is null) then 
	    update astoctas set astc_tipcta=tctabco where astc_id=NEW.astc_id;
	END IF;

    END IF;
    RETURN NEW;
    END;$$;


ALTER FUNCTION public.f_ai_astoctas() OWNER TO rulemanes;

--
-- Name: f_ai_clientes(); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.f_ai_clientes() RETURNS trigger
    LANGUAGE plpgsql
    AS $$
    DECLARE
    BEGIN
	insert into sucursalc (sucu_cliente,sucu_nombre) values (NEW.cli_id,NEW.cli_nombre);
    RETURN NEW;
    END;$$;


ALTER FUNCTION public.f_ai_clientes() OWNER TO rulemanes;

--
-- Name: f_ai_empresa(); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.f_ai_empresa() RETURNS trigger
    LANGUAGE plpgsql
    AS $$
    DECLARE
    BEGIN
    insert into ptovta (ptv_codemp,ptv_codigo,ptv_nombre,ptv_direcc,ptv_locali,ptv_codpos,ptv_provin,ptv_pais,ptv_codact,ptv_descact)
	 values (NEW.emp_codigo,1,NEW.emp_nombre,NEW.emp_direcc,NEW.emp_locali,NEW.emp_codpos,NEW.emp_provin,NEW.emp_pais,NEW.emp_codact,NEW.emp_descact);
    RETURN NEW;
    END;$$;


ALTER FUNCTION public.f_ai_empresa() OWNER TO rulemanes;

--
-- Name: f_ai_movsto(); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.f_ai_movsto() RETURNS trigger
    LANGUAGE plpgsql
    AS $$
    DECLARE
	llevastock	integer;
	registro	record;
    BEGIN

    select valvaramb(NEW.movs_codemp,0,'STOCK') into llevastock;
    if int4ne(llevastock,1) THEN 

	for registro in select * from receta 
	      where re_estado=1 and re_codemp=NEW.novs_codemp and re_articv=NEW.movs_artic
	    loop
		if found then
		    insert into stock (stc_codemp,stc_idptov,stc_idart,stc_idemiti,stc_idastoh,stc_idmovsto,stc_tipo,stc_fecval,stc_cantegr,stc_estado)
			values (NEW.movs_codemp,NEW.movs_idptov,registro.re_articc,NEW.movs_idemiti,NEW.movs_idastoh,NEW.movs_id,1,NEW.movs_fecval,(registro.re_canti * NEW.movs_canti),NEW.movs_estado);
		end if;
	    end loop; 

	END IF;
    RETURN NEW;
    END;$$;


ALTER FUNCTION public.f_ai_movsto() OWNER TO rulemanes;

--
-- Name: f_ai_movstoc(); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.f_ai_movstoc() RETURNS trigger
    LANGUAGE plpgsql
    AS $$
    DECLARE
    BEGIN
	insert into stock (stc_codemp,stc_idptov,stc_idartc,stc_idcompra,stc_idastoh,stc_idmovstoc,stc_tipo,stc_fecval,stc_canting,stc_precio,stc_estado)
	    values (NEW.movc_codemp,NEW.movc_idptov,NEW.movc_artic,NEW.movc_idcompra,NEW.movc_idastoh,NEW.movc_id,2,NEW.movc_fecval,NEW.movc_canti,NEW.movc_precio,NEW.movc_estado);
    RETURN NEW;
    END;$$;


ALTER FUNCTION public.f_ai_movstoc() OWNER TO rulemanes;

--
-- Name: f_au_articvta(); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.f_au_articvta() RETURNS trigger
    LANGUAGE plpgsql
    AS $$
DECLARE
    articuid		integer;
BEGIN

    select artc_id into articuid from articu where artc_codigo=OLD.art_codigo;
    if found then
	    update articu set
		artc_codemp=NEW.art_codemp,
		artc_idptov=NEW.art_idptov,
		artc_nivrep=NEW.art_nivrep,
		artc_codigo=NEW.art_codigo,
		artc_marca=NEW.art_marca,
		artc_tasa=NEW.art_tasa,
		artc_codrub=NEW.art_codrub,
		artc_nombre=NEW.art_nombre,
		artc_present=NEW.art_present,
		artc_unimed=NEW.art_unimed,
		artc_estado=NEW.art_estado,
		artc_leyen=NEW.art_leyen where artc_id=articuid;
    end if;

    RETURN NEW;
END;$$;


ALTER FUNCTION public.f_au_articvta() OWNER TO rulemanes;

--
-- Name: f_au_astoctas(); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.f_au_astoctas() RETURNS trigger
    LANGUAGE plpgsql
    AS $$
DECLARE
	total	numeric;
	dias	numeric;
	cont	integer;
BEGIN

    if int4ne(NEW.astc_estado,OLD.astc_estado) THEN 
	update cheques set chq_estado=NEW.astc_estado where chq_idastoc=OLD.astc_id and chq_idastoce is null;
	update monex set mne_estado=NEW.astc_estado where mne_idastoc=OLD.astc_id;
	update docum set doc_estado=NEW.astc_estado where doc_idastoc=OLD.astc_id;
	END IF;

    if int4ne(NEW.astc_ctacte,OLD.astc_ctacte) THEN 
	if int4eq(OLD.astc_ctacble,idcbl(OLD.astc_codemp,valvaramb(OLD.astc_codemp,OLD.astc_idptov,'DEUDXV')::integer)) THEN 
	    update emiti set emi_codcli=NEW.astc_ctacte where emi_idastoh=OLD.astc_idastoh;
	END IF;
	
	if int4eq(OLD.astc_ctacble,idcbl(OLD.astc_codemp,valvaramb(OLD.astc_codemp,OLD.astc_idptov,'DEUDXC')::integer)) THEN 
		update compra set cpra_prove=NEW.astc_ctacte where cpra_idastoh=OLD.astc_idastoh;
	END IF;
    END IF;

    if (int4ne(NEW.astc_estado,OLD.astc_estado) and int4eq(NEW.astc_estado,2)) THEN 
	select sum(astc_importe) into total from astoctas where astc_idastoh=OLD.astc_idastoh and astc_importe>0;
	update compra set cpra_total=total where cpra_idastoh=OLD.astc_idastoh;
    END IF;


    if (int4eq(NEW.astc_estado,999) and int2eq(NEW.astc_cierra,2::int2) and (int4eq(NEW.astc_ctacble,idcbl(NEW.astc_codemp,valvaramb(NEW.astc_codemp,NEW.astc_idptov,'DEUDXV')::integer)) or int4eq(NEW.astc_ctacble,idcbl(NEW.astc_codemp,valvaramb(NEW.astc_codemp,NEW.astc_idptov,'DEUDXC')::integer))))  THEN 
	update astoctas set astc_cierra=1 where astc_ctacble=NEW.astc_ctacble and astc_tipcta=NEW.astc_tipcta and astc_ctacte=NEW.astc_ctacte and astc_grupo=NEW.astc_grupo and astc_idastoh!=OLD.astc_idastoh;
    END IF;

    if (int4eq(NEW.astc_estado,999))  THEN 
	update cheques set chq_estado=2,chq_idastoce=NULL where int4eq(chq_idastoce,OLD.astc_id);
	update cheques set chq_estado=999,chq_idastoce=NULL where int4eq(chq_idastoc,OLD.astc_id);
    END IF;


    if (int4ne(NEW.astc_estado,OLD.astc_estado)  and int2eq(NEW.astc_cierra,2::int2) and int4eq(NEW.astc_ctacble,idcbl(NEW.astc_codemp,valvaramb(NEW.astc_codemp,NEW.astc_idptov,'DEUDXV')::integer))) THEN 
	select diasconpag(emi_conpag) into dias from emiti where emi_idastoh=NEW.astc_idastoh;
	    if found then
		update astoctas set astc_fecvto=astc_fecval+dias::integer where astc_id=NEW.astc_id;
	    end if;
    END IF;

	select max(log_cont)+1 into cont from modlogs where log_tabla='astoctas' and log_idtabla=NEW.astc_id;
	if cont is null then
	    cont:=1;
	end if;
	if (numeric_ne(NEW.astc_importe,OLD.astc_importe)) then
	    insert into modlogs (log_idptov,log_comprob,log_numero,log_tabla,log_idtabla,log_campo,log_valant,log_valpos,log_cont) values (NEW.astc_idptov,NEW.astc_comprob,NEW.astc_numero,'astoctas',NEW.astc_id,'astc_importe',OLD.astc_importe,NEW.astc_importe,cont);
	end if;
	if (int4ne(NEW.astc_ctacte,OLD.astc_ctacte)) then
	    insert into modlogs (log_idptov,log_comprob,log_numero,log_tabla,log_idtabla,log_campo,log_valant,log_valpos,log_cont) values (NEW.astc_idptov,NEW.astc_comprob,NEW.astc_numero,'astoctas',NEW.astc_id,'astc_ctacte',OLD.astc_ctacte,NEW.astc_ctacte,cont);
	end if;
	if (int4ne(NEW.astc_ctacble,OLD.astc_ctacble)) then
	    insert into modlogs (log_idptov,log_comprob,log_numero,log_tabla,log_idtabla,log_campo,log_valant,log_valpos,log_cont) values (NEW.astc_idptov,NEW.astc_comprob,NEW.astc_numero,'astoctas',NEW.astc_id,'astc_ctacble',OLD.astc_ctacble,NEW.astc_ctacble,cont);
	end if;
	if (int4ne(NEW.astc_cierra,OLD.astc_cierra)) then
	    insert into modlogs (log_idptov,log_comprob,log_numero,log_tabla,log_idtabla,log_campo,log_valant,log_valpos,log_cont) values (NEW.astc_idptov,NEW.astc_comprob,NEW.astc_numero,'astoctas',NEW.astc_id,'astc_cierra',OLD.astc_cierra,NEW.astc_cierra,cont);
	end if;
	if (int4ne(NEW.astc_estado,OLD.astc_estado)) then
	    insert into modlogs (log_idptov,log_comprob,log_numero,log_tabla,log_idtabla,log_campo,log_valant,log_valpos,log_cont) values (NEW.astc_idptov,NEW.astc_comprob,NEW.astc_numero,'astoctas',NEW.astc_id,'astc_estado',OLD.astc_estado,NEW.astc_estado,cont);
	end if;

    RETURN NEW;
    END;$$;


ALTER FUNCTION public.f_au_astoctas() OWNER TO rulemanes;

--
-- Name: f_au_astoh(); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.f_au_astoh() RETURNS trigger
    LANGUAGE plpgsql
    AS $$
DECLARE
	cont	integer;
BEGIN

    if int4ne(NEW.ash_estado,OLD.ash_estado) THEN 
	update astoctas set astc_estado=NEW.ash_estado where astc_idastoh=OLD.ash_id;
	update compra set cpra_estado=NEW.ash_estado where cpra_idastoh=OLD.ash_id;
	update emiti set emi_estado=NEW.ash_estado where emi_idastoh=OLD.ash_id;
	delete from astoctas where astc_importe=0 and astc_idastoh=OLD.ash_id;
	END IF;

    if (int4ne(NEW.ash_estado,OLD.ash_estado) and int4eq(NEW.ash_estado,999))  THEN 
	update astoctas set astc_cierra=1,astc_idashce=NULL where astc_idastoh=OLD.ash_id;
	delete from agrupa where ag_idpago=OLD.ash_id;
	delete from debitos where db_idpago=OLD.ash_id;
    END IF;

    if date_ne(NEW.ash_fecval,OLD.ash_fecval) THEN 
	update astoctas set astc_fecval=NEW.ash_fecval where astc_idastoh=OLD.ash_id;
	update compra set cpra_fecval=NEW.ash_fecval where cpra_idastoh=OLD.ash_id;
	update emiti set emi_fecval=NEW.ash_fecval where emi_idastoh=OLD.ash_id;
	update astoh set ash_anio=date_part('year',NEW.ash_fecval),ash_mes=date_part('month',NEW.ash_fecval) where ash_id=OLD.ash_id;
	END IF;

    if date_ne(NEW.ash_fecomp,OLD.ash_fecomp) THEN 
	update compra set cpra_fecori=NEW.ash_fecomp where cpra_idastoh=OLD.ash_id;
	END IF;

    if int4ne(NEW.ash_comprob,OLD.ash_comprob) THEN 
	update astoctas set astc_comprob=NEW.ash_comprob where astc_idastoh=OLD.ash_id;
	update compra set cpra_comprob=NEW.ash_comprob where cpra_idastoh=OLD.ash_id;
	update emiti set emi_comprob=NEW.ash_comprob where emi_idastoh=OLD.ash_id;
	END IF;

    if int4ne(NEW.ash_numero,OLD.ash_numero) THEN 
	update astoctas set astc_numero=NEW.ash_numero where astc_idastoh=OLD.ash_id;
	update compra set cpra_numero=NEW.ash_numero where cpra_idastoh=OLD.ash_id;
	update emiti set emi_numero=NEW.ash_numero where emi_idastoh=OLD.ash_id;
	END IF;

    if int4ne(NEW.ash_codemp,OLD.ash_codemp) THEN 
	update astoctas set astc_codemp=NEW.ash_codemp where astc_idastoh=OLD.ash_id;
	update compra set cpra_codemp=NEW.ash_codemp where cpra_idastoh=OLD.ash_id;
	update emiti set emi_codemp=NEW.ash_codemp where emi_idastoh=OLD.ash_id;
	END IF;

    if int4ne(NEW.ash_idptov,OLD.ash_idptov) THEN 
	update astoctas set astc_idptov=NEW.ash_idptov where astc_idastoh=OLD.ash_id;
	update compra set cpra_idptov=NEW.ash_idptov where cpra_idastoh=OLD.ash_id;
	update emiti set emi_idptov=NEW.ash_idptov where emi_idastoh=OLD.ash_id;
	END IF;


	select max(log_cont)+1 into cont from modlogs where log_tabla='astoh' and log_idtabla=NEW.ash_id;
	if cont is null then
	    cont:=1;
	end if;
	if (int4ne(NEW.ash_anio,OLD.ash_anio)) then
	    insert into modlogs (log_idptov,log_comprob,log_numero,log_tabla,log_idtabla,log_campo,log_valant,log_valpos,log_cont) values (NEW.ash_idptov,NEW.ash_comprob,NEW.ash_numero,'astoh',NEW.ash_id,'ash_anio',OLD.ash_anio,NEW.ash_anio,cont);
	end if;
	if (int4ne(NEW.ash_mes,OLD.ash_mes)) then
	    insert into modlogs (log_idptov,log_comprob,log_numero,log_tabla,log_idtabla,log_campo,log_valant,log_valpos,log_cont) values (NEW.ash_idptov,NEW.ash_comprob,NEW.ash_numero,'astoh',NEW.ash_id,'ash_mes',OLD.ash_mes,NEW.ash_mes,cont);
	end if;
	if (int4ne(NEW.ash_estado,OLD.ash_estado)) then
	    insert into modlogs (log_idptov,log_comprob,log_numero,log_tabla,log_idtabla,log_campo,log_valant,log_valpos,log_cont) values (NEW.ash_idptov,NEW.ash_comprob,NEW.ash_numero,'astoh',NEW.ash_id,'ash_estado',OLD.ash_estado,NEW.ash_estado,cont);
	end if;
	if (date_ne(NEW.ash_fecomp,OLD.ash_fecomp)) then
	    insert into modlogs (log_idptov,log_comprob,log_numero,log_tabla,log_idtabla,log_campo,log_valant,log_valpos,log_cont) values (NEW.ash_idptov,NEW.ash_comprob,NEW.ash_numero,'astoh',NEW.ash_id,'ash_fecomp',OLD.ash_fecomp,NEW.ash_fecomp,cont);
	end if;
	if (date_ne(NEW.ash_fecval,OLD.ash_fecval)) then
	    insert into modlogs (log_idptov,log_comprob,log_numero,log_tabla,log_idtabla,log_campo,log_valant,log_valpos,log_cont) values (NEW.ash_idptov,NEW.ash_comprob,NEW.ash_numero,'astoh',NEW.ash_id,'ash_fecval',OLD.ash_fecval,NEW.ash_fecval,cont);
	end if;


    RETURN NEW;
    END;$$;


ALTER FUNCTION public.f_au_astoh() OWNER TO rulemanes;

--
-- Name: f_au_cheques(); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.f_au_cheques() RETURNS trigger
    LANGUAGE plpgsql
    AS $$
DECLARE
	cont	integer;
BEGIN

	select max(log_cont)+1 into cont from modlogs where log_tabla='cheques' and log_idtabla=NEW.chq_id;
	if cont is null then
	    cont:=1;
	end if;
	if (int4ne(NEW.chq_nucheq,OLD.chq_nucheq)) then
	    insert into modlogs (log_idptov,log_comprob,log_numero,log_tabla,log_idtabla,log_campo,log_valant,log_valpos,log_cont) values (NEW.chq_idptov,1,1,'cheques',NEW.chq_id,'chq_nucheq',OLD.chq_nucheq,NEW.chq_nucheq,cont);
	end if;
	if (int4ne(NEW.chq_codbco,OLD.chq_codbco)) then
	    insert into modlogs (log_idptov,log_comprob,log_numero,log_tabla,log_idtabla,log_campo,log_valant,log_valpos,log_cont) values (NEW.chq_idptov,1,1,'cheques',NEW.chq_id,'chq_codbco',OLD.chq_codbco,NEW.chq_codbco,cont);
	end if;
	if (int4ne(NEW.chq_idastoc,OLD.chq_idastoc)) then
	    insert into modlogs (log_idptov,log_comprob,log_numero,log_tabla,log_idtabla,log_campo,log_valant,log_valpos,log_cont) values (NEW.chq_idptov,1,1,'cheques',NEW.chq_id,'chq_idastoc',OLD.chq_idastoc,NEW.chq_idastoc,cont);
	end if;
	if (int4ne(NEW.chq_idastoce,OLD.chq_idastoce)) then
	    insert into modlogs (log_idptov,log_comprob,log_numero,log_tabla,log_idtabla,log_campo,log_valant,log_valpos,log_cont) values (NEW.chq_idptov,1,1,'cheques',NEW.chq_id,'chq_idastoce',OLD.chq_idastoce,NEW.chq_idastoce,cont);
	end if;
	if (date_ne(NEW.chq_fecheq,OLD.chq_fecheq)) then
	    insert into modlogs (log_idptov,log_comprob,log_numero,log_tabla,log_idtabla,log_campo,log_valant,log_valpos,log_cont) values (NEW.chq_idptov,1,1,'cheques',NEW.chq_id,'chq_fecheq',OLD.chq_fecheq,NEW.chq_fecheq,cont);
	end if;
	if (int4ne(NEW.chq_estado,OLD.chq_estado)) then
	    insert into modlogs (log_idptov,log_comprob,log_numero,log_tabla,log_idtabla,log_campo,log_valant,log_valpos,log_cont) values (NEW.chq_idptov,1,1,'cheques',NEW.chq_id,'chq_estado',OLD.chq_estado,NEW.chq_estado,cont);
	end if;
	if (numeric_ne(NEW.chq_importe,OLD.chq_importe)) then
	    insert into modlogs (log_idptov,log_comprob,log_numero,log_tabla,log_idtabla,log_campo,log_valant,log_valpos,log_cont) values (NEW.chq_idptov,1,1,'cheques',NEW.chq_id,'chq_importe',OLD.chq_importe,NEW.chq_importe,cont);
	end if;
    RETURN NEW;
    END;$$;


ALTER FUNCTION public.f_au_cheques() OWNER TO rulemanes;

--
-- Name: f_au_clientes(); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.f_au_clientes() RETURNS trigger
    LANGUAGE plpgsql
    AS $$
DECLARE
BEGIN

    if bpcharne(NEW.cli_nombre,OLD.cli_nombre) THEN 
	update sucursalc set sucu_nombre=NEW.cli_nombre where sucu_cliente=OLD.cli_id and sucu_nombre=OLD.cli_nombre;
	END IF;
    RETURN NEW;
    END;$$;


ALTER FUNCTION public.f_au_clientes() OWNER TO rulemanes;

--
-- Name: f_au_compra(); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.f_au_compra() RETURNS trigger
    LANGUAGE plpgsql
    AS $$
DECLARE
BEGIN
    if(int4ne(NEW.cpra_prove,OLD.cpra_prove)) THEN 
	update compra set cpra_nomprov=nomprove(NEW.cpra_prove), cpra_nrcuit=cuitprov(NEW.cpra_prove),cpra_coniva=conivaprov(NEW.cpra_prove) where cpra_id=OLD.cpra_id;
    END IF;

    if int4ne(NEW.cpra_estado,OLD.cpra_estado) THEN 
	update movstoc set movc_estado=NEW.cpra_estado where movc_idcompra=OLD.cpra_id;
    END IF;

RETURN NEW;
END;$$;


ALTER FUNCTION public.f_au_compra() OWNER TO rulemanes;

--
-- Name: f_au_concilia(); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.f_au_concilia() RETURNS trigger
    LANGUAGE plpgsql
    AS $$
DECLARE
BEGIN

    if int4ne(NEW.clia_estado,OLD.clia_estado) THEN 
	update conxclia set cxlia_estado=NEW.clia_estado where cxlia_idclia=OLD.cxlia_id;
	END IF;

    RETURN NEW;
    END;$$;


ALTER FUNCTION public.f_au_concilia() OWNER TO rulemanes;

--
-- Name: f_au_contratos(); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.f_au_contratos() RETURNS trigger
    LANGUAGE plpgsql
    AS $$
DECLARE
registro	 record;
BEGIN

-- Estado 999 ANUALADO CON DEVOLUCION DE DISPENSER
-- Estado 9 ANUALADO SIN DEVOLUCION DE DISPENSER

    if (int4ne(NEW.ctr_idcli,OLD.ctr_idcli))  THEN 
	    update contrb set ctrb_estado=NEW.ctr_idcli where ctrb_idcontr=OLD.ctr_id;
    END IF;

    if (int4ne(NEW.ctr_numero,OLD.ctr_numero))  THEN 
	    update contrb set ctrb_numero=NEW.ctr_numero where ctrb_idcontr=OLD.ctr_id;
	    update dispenser set dis_numero=NEW.ctr_numero where dis_idcontr=OLD.ctr_id;
    END IF;

    if (date_ne(NEW.ctr_fecvto,OLD.ctr_fecvto))  THEN 
	    update contrb set ctrb_fecvto=NEW.ctr_fecvto where ctrb_idcontr=OLD.ctr_id;
	    update dispenser set dis_fecvto=NEW.ctr_fecvto where dis_idcontr=OLD.ctr_id;
    END IF;

    if (date_ne(NEW.ctr_fecha,OLD.ctr_fecha))  THEN 
	    update contrb set ctrb_fecha=NEW.ctr_fecha where ctrb_idcontr=OLD.ctr_id;
	    update dispenser set dis_fecha=NEW.ctr_fecha where dis_idcontr=OLD.ctr_id;
    END IF;

    if int4ne(NEW.ctr_estado,OLD.ctr_estado) THEN 
	if (int4eq(NEW.ctr_estado,999))  THEN 
	    update dispenser set dis_idcontr=NULL, dis_idcontrb=-1, dis_estado=1 where dis_idcontr=OLD.ctr_id;
	    update contrb set ctrb_estado=NEW.ctr_estado where ctrb_idcontr=OLD.ctr_id;
	END IF;

	if (int4eq(NEW.ctr_estado,9))  THEN 
	    update dispenser set dis_estado=9 where dis_idcontr=OLD.ctr_id;
	    update contrb set ctrb_estado=NEW.ctr_estado where ctrb_idcontr=OLD.ctr_id;
	END IF;

	if (int4eq(NEW.ctr_estado,9) and int4eq(OLD.ctr_estado,999))  THEN 
	    update contrb set ctrb_estado=NEW.ctr_estado where ctrb_idcontr=OLD.ctr_id;
	    for registro in select * from contrb where ctrb_idcontr=OLD.ctr_id
		loop
		update dispenser set dis_idcontr=OLD.ctr_id, dis_idcontrb=registro.ctrb_id, dis_estado=NEW.ctr_estado where dis_id=registro.ctrb_iddispen;
	    end loop;
	END IF;

	if (int4eq(NEW.ctr_estado,1))  THEN 
	    update contrb set ctrb_estado=NEW.ctr_estado where ctrb_idcontr=OLD.ctr_id;
	    for registro in select * from contrb where ctrb_idcontr=OLD.ctr_id
		loop
		update dispenser set dis_idcontr=OLD.ctr_id, dis_idcontrb=registro.ctrb_id, dis_estado=NEW.ctr_estado where dis_id=registro.ctrb_iddispen;
	    end loop;
	END IF;



    END IF;

RETURN NEW;
END;$$;


ALTER FUNCTION public.f_au_contratos() OWNER TO rulemanes;

--
-- Name: f_au_emiti(); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.f_au_emiti() RETURNS trigger
    LANGUAGE plpgsql
    AS $$
DECLARE
BEGIN

    if int4ne(NEW.emi_estado,OLD.emi_estado) THEN 
	update movsto set movs_estado=NEW.emi_estado where movs_idemiti=OLD.emi_id;

	if int4eq(NEW.emi_estado,2) THEN 
	    update astoctas set astc_fecvto=astc_fecval+diasconpag(NEW.emi_conpag) where astc_idastoh=OLD.emi_idastoh and astc_ctacble=idcbl(astc_codemp,valvaramb(astc_codemp,0,'DEUDXV')::integer);
	    END IF;

	END IF;

    if int4ne(NEW.emi_codcli,OLD.emi_codcli) THEN 
	update emiti set emi_nomcli=nomcli(NEW.emi_codcli),emi_nrcuit=cuitcli(NEW.emi_codcli),emi_conivac=conivacli(NEW.emi_codcli) where emi_id=OLD.emi_id;
	END IF;



    RETURN NEW;
    END;$$;


ALTER FUNCTION public.f_au_emiti() OWNER TO rulemanes;

--
-- Name: f_au_listpre(); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.f_au_listpre() RETURNS trigger
    LANGUAGE plpgsql
    AS $$
DECLARE
	cont	integer;
BEGIN

	select max(log_cont)+1 into cont from modlogs where log_tabla='listpre' and log_idtabla=NEW.ltp_id;
	if cont is null then
	    cont:=1;
	end if;
	if (numeric_ne(NEW.ltp_precio,OLD.ltp_precio)) then
	    insert into modlogs (log_idptov,log_comprob,log_numero,log_tabla,log_idtabla,log_campo,log_valant,log_valpos,log_cont) values (NEW.ltp_idptov,1,1,'listpre',NEW.ltp_id,'ltp_precio',OLD.ltp_precio,NEW.ltp_precio,cont);
	end if;
    RETURN NEW;
    END;$$;


ALTER FUNCTION public.f_au_listpre() OWNER TO rulemanes;

--
-- Name: f_au_movsto(); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.f_au_movsto() RETURNS trigger
    LANGUAGE plpgsql
    AS $$
    DECLARE
	llevastock	integer;
	cont		integer;
	registro	record;
    BEGIN

    if int4ne(NEW.movs_estado,OLD.movs_estado) THEN 
	update stock set stc_estado=NEW.movs_estado where stc_idmovsto=OLD.movs_id;
	END IF;

    if int4ne(NEW.movs_idastoh,OLD.movs_idastoh) THEN 
	update stock set stc_idastoh=NEW.movs_idastoh where stc_idmovsto=OLD.movs_id;
	END IF;

    if int4ne(NEW.movs_idemiti,OLD.movs_idemiti) THEN 
	update stock set stc_idemiti=NEW.movs_idemiti where stc_idmovsto=OLD.movs_id;
	END IF;

    if int4ne(NEW.movs_idptov,OLD.movs_idptov) THEN 
	update stock set stc_idptov=NEW.movs_idptov where stc_idmovsto=OLD.movs_id;
	END IF;

    if date_ne(NEW.movs_fecval,OLD.movs_fecval) THEN 
	update stock set stc_fecval=NEW.movs_fecval where stc_idmovsto=OLD.movs_id;
	END IF;


    if (numeric_ne(NEW.movs_canti,OLD.movs_canti)) then
	select valvaramb(NEW.movs_codemp,0,'STOCK') into llevastock;
	if int4ne(llevastock,1) THEN 
	    delete from stock where int4eq(stc_idmovsto,NEW.movs_id);
	    for registro in select * from receta 
		where re_estado=1 and re_codemp=NEW.novs_codemp and re_articv=NEW.movs_artic
		loop
		if found then
		    insert into stock (stc_codemp,stc_idptov,stc_idart,stc_idemiti,stc_idastoh,stc_idmovsto,stc_tipo,stc_fecval,stc_cantegr,stc_estado)
			values (NEW.movs_codemp,NEW.movs_idptov,registro.re_articc,NEW.movs_idemiti,NEW.movs_idastoh,NEW.movs_id,1,NEW.movs_fecval,(registro.re_canti * NEW.movs_canti),NEW.movs_estado);
		    end if;
		end loop; 
	    END IF;
	END IF;

	select max(log_cont)+1 into cont from modlogs where log_tabla='movsto' and log_idtabla=NEW.movs_id;
	if cont is null then
	    cont:=1;
	end if;
	if (int4ne(NEW.movs_estado,OLD.movs_estado)) then
	    insert into modlogs (log_idptov,log_comprob,log_numero,log_tabla,log_idtabla,log_campo,log_valant,log_valpos,log_cont) values (NEW.movs_idptov,1,1,'movsto',NEW.movs_id,'movs_estado',OLD.movs_estado,NEW.movs_estado,cont);
	end if;
	if (numeric_ne(NEW.movs_canti,OLD.movs_canti)) then
	    insert into modlogs (log_idptov,log_comprob,log_numero,log_tabla,log_idtabla,log_campo,log_valant,log_valpos,log_cont) values (NEW.movs_idptov,1,1,'movsto',NEW.movs_id,'movs_canti',OLD.movs_canti,NEW.movs_canti,cont);
	end if;
	if (numeric_ne(NEW.movs_cantret,OLD.movs_cantret)) then
	    insert into modlogs (log_idptov,log_comprob,log_numero,log_tabla,log_idtabla,log_campo,log_valant,log_valpos,log_cont) values (NEW.movs_idptov,1,1,'movsto',NEW.movs_id,'movs_cantret',OLD.movs_cantret,NEW.movs_cantret,cont);
	end if;
	if (numeric_ne(NEW.movs_importe,OLD.movs_importe)) then
	    insert into modlogs (log_idptov,log_comprob,log_numero,log_tabla,log_idtabla,log_campo,log_valant,log_valpos,log_cont) values (NEW.movs_idptov,1,1,'movsto',NEW.movs_id,'movs_importe',OLD.movs_importe,NEW.movs_importe,cont);
	end if;



    RETURN NEW;
    END;$$;


ALTER FUNCTION public.f_au_movsto() OWNER TO rulemanes;

--
-- Name: f_au_movstoc(); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.f_au_movstoc() RETURNS trigger
    LANGUAGE plpgsql
    AS $$
    DECLARE
    BEGIN

    if int4ne(NEW.movc_estado,OLD.movc_estado) THEN 
	update stock set stc_estado=NEW.movc_estado where stc_idmovstoc=OLD.movc_id;
	END IF;

    if int4ne(NEW.movc_idastoh,OLD.movc_idastoh) THEN 
	update stock set stc_idastoh=NEW.movc_idastoh where stc_idmovstoc=OLD.movc_id;
	END IF;

    if int4ne(NEW.movc_idcompra,OLD.movc_idcompra) THEN 
	update stock set stc_idcompra=NEW.movc_idcompra where stc_idmovstoc=OLD.movc_id;
	END IF;

    if int4ne(NEW.movc_idptov,OLD.movc_idptov) THEN 
	update stock set stc_idptov=NEW.movc_idptov where stc_idmovstoc=OLD.movc_id;
	END IF;

    if date_ne(NEW.movc_fecval,OLD.movc_fecval) THEN 
	update stock set stc_fecval=NEW.movc_fecval where stc_idmovstoc=OLD.movc_id;
	END IF;

    if (numeric_ne(NEW.movc_canti,OLD.movc_canti)) then
	update stock set stc_canting=NEW.movc_canti where stc_idmovstoc=OLD.movc_id;
	END IF;

    if (numeric_ne(NEW.movc_precio,OLD.movc_precio)) then
	update stock set stc_precio=NEW.movc_precio where stc_idmovstoc=OLD.movc_id;
	END IF;

    RETURN NEW;
    END;$$;


ALTER FUNCTION public.f_au_movstoc() OWNER TO rulemanes;

--
-- Name: f_au_prove(); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.f_au_prove() RETURNS trigger
    LANGUAGE plpgsql
    AS $$
DECLARE
BEGIN

    if bpcharne(NEW.prov_nombre,OLD.prov_nombre) THEN 
	update ctes set cte_nombre=NEW.prov_nombre where cte_codigo=OLD.prov_id and cte_codemp=OLD.prov_codemp and cte_tipcta=eval_funcion(valvaramb(NEW.prov_codemp,ptovdefault(NEW.prov_codemp),'TIPCTADXC'));
	END IF;
    RETURN NEW;
    END;$$;


ALTER FUNCTION public.f_au_prove() OWNER TO rulemanes;

--
-- Name: f_bd_astoctas(); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.f_bd_astoctas() RETURNS trigger
    LANGUAGE plpgsql
    AS $$
    DECLARE
    cuantos		int4;
    cont		integer;
    BEGIN

----------------------------  CHEQUES ---------------------------------------    
	
    select count(*) into cuantos from cheques where int4eq(chq_idastoc,OLD.astc_id);
    if int4ne(cuantos,0) then
	select count(*) into cuantos from cheques where int4eq(chq_idastoc,OLD.astc_id) and chq_idastoce is not null;
	if int4ne(cuantos,0) then
		RAISE EXCEPTION  'NO SE PUEDE BORRAR UN CHEQUE ASIGNADO ANTERIORMENTE';
	    else
		delete from cheques where int4eq(chq_idastoc,OLD.astc_id);
	END IF;
    END IF;


    select count(*) into cuantos from cheques where int4eq(chq_idastoce,OLD.astc_id);
    if found then
	update cheques set chq_estado=2,chq_idastoce=NULL where int4eq(chq_idastoce,OLD.astc_id);
    END IF;

----------------------------  MONEX ---------------------------------------    

    select count(*) into cuantos from monex where int4eq(mne_idastoc,OLD.astc_id);
    if int4ne(cuantos,0) then
	select count(*) into cuantos from monex where int4eq(mne_idastoc,OLD.astc_id) and mne_idastoce is not null;
	if int4ne(cuantos,0) then
		RAISE EXCEPTION  'NO SE PUEDE BORRAR UN BILLETE ASIGNADO ANTERIORMENTE';
	    else
		delete from monex where int4eq(mne_idastoc,OLD.astc_id);
	END IF;
    END IF;

    select count(*) into cuantos from monex where int4eq(mne_idastoce,OLD.astc_id);
    if found then
	update monex set mne_estado=2,mne_idastoce=NULL where int4eq(mne_idastoce,OLD.astc_id);
    END IF;


----------------------------  DOCUM ---------------------------------------    

    select count(*) into cuantos from docum where int4eq(doc_idastoc,OLD.astc_id);
    if int4ne(cuantos,0) then
	select count(*) into cuantos from docum where int4eq(doc_idastoc,OLD.astc_id) and doc_idastoce is not null;
	if int4ne(cuantos,0) then
		RAISE EXCEPTION  'NO SE PUEDE BORRAR UN BILLETE ASIGNADO ANTERIORMENTE';
	    else
		delete from docum where int4eq(doc_idastoc,OLD.astc_id);
	END IF;
    END IF;

    select count(*) into cuantos from docum where int4eq(doc_idastoce,OLD.astc_id);
    if found then
	update docum set doc_estado=2,doc_idastoce=NULL where int4eq(doc_idastoce,OLD.astc_id);
    END IF;

---------------------------- CIERRA Y GRUPO -----------------------------

    if (int2eq(OLD.astc_cierra,2::int2) and (int4eq(OLD.astc_ctacble,idcbl(OLD.astc_codemp,valvaramb(OLD.astc_codemp,OLD.astc_idptov,'DEUDXV')::integer)) or int4eq(OLD.astc_ctacble,idcbl(OLD.astc_codemp,valvaramb(OLD.astc_codemp,OLD.astc_idptov,'DEUDXC')::integer))))  THEN 
	update astoctas set astc_cierra=1 where astc_ctacble=OLD.astc_ctacble and astc_tipcta=OLD.astc_tipcta and astc_ctacte=OLD.astc_ctacte and astc_grupo=OLD.astc_grupo and astc_idastoh!=OLD.astc_idastoh;
    END IF;


	select max(log_cont)+1 into cont from modlogs where log_tabla='astoctas' and log_idtabla=OLD.astc_id;
	if cont is null then
	    cont:=1;
	end if;
	insert into modlogs (log_idptov,log_comprob,log_numero,log_tabla,log_idtabla,log_campo,log_valant,log_valpos,log_cont) values (OLD.astc_idptov,OLD.astc_comprob,OLD.astc_numero,'astoctas',OLD.astc_id,'astc_idastoh',OLD.astc_idastoh,'delete',cont);
	insert into modlogs (log_idptov,log_comprob,log_numero,log_tabla,log_idtabla,log_campo,log_valant,log_valpos,log_cont) values (OLD.astc_idptov,OLD.astc_comprob,OLD.astc_numero,'astoctas',OLD.astc_id,'astc_importe',OLD.astc_importe,'delete',cont);
	insert into modlogs (log_idptov,log_comprob,log_numero,log_tabla,log_idtabla,log_campo,log_valant,log_valpos,log_cont) values (OLD.astc_idptov,OLD.astc_comprob,OLD.astc_numero,'astoctas',OLD.astc_id,'astc_ctacble',OLD.astc_ctacble,'delete',cont);


    RETURN OLD;
    END;$$;


ALTER FUNCTION public.f_bd_astoctas() OWNER TO rulemanes;

--
-- Name: f_bd_astoh(); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.f_bd_astoh() RETURNS trigger
    LANGUAGE plpgsql
    AS $$
    DECLARE
    cuantos		int4;
    cont		integer;
    BEGIN

    delete from emiti where int4eq(emi_idastoh,OLD.ash_id);
    delete from compra where int4eq(cpra_idastoh,OLD.ash_id);
    delete from astoctas where int4eq(astc_idastoh,OLD.ash_id);
    update astoctas set astc_cierra=1,astc_idashce=NULL where astc_idastoh=OLD.ash_id;
    delete from relrem where int4eq(rr_idastohp,OLD.ash_id);
    delete from relrem where int4eq(rr_idastohh,OLD.ash_id);
    delete from agrupa where ag_idpago=OLD.ash_id;

	select max(log_cont)+1 into cont from modlogs where log_tabla='astoh' and log_idtabla=OLD.ash_id;
	if cont is null then
	    cont:=1;
	end if;
	insert into modlogs (log_idptov,log_comprob,log_numero,log_tabla,log_idtabla,log_campo,log_valant,log_valpos,log_cont) values (OLD.ash_idptov,OLD.ash_comprob,OLD.ash_numero,'astoh',OLD.ash_id,'ash_titulo',OLD.ash_titulo,'delete',cont);
	insert into modlogs (log_idptov,log_comprob,log_numero,log_tabla,log_idtabla,log_campo,log_valant,log_valpos,log_cont) values (OLD.ash_idptov,OLD.ash_comprob,OLD.ash_numero,'astoh',OLD.ash_id,'ash_fecval',OLD.ash_fecval,'delete',cont);

    RETURN OLD;
    END;$$;


ALTER FUNCTION public.f_bd_astoh() OWNER TO rulemanes;

--
-- Name: f_bd_clientes(); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.f_bd_clientes() RETURNS trigger
    LANGUAGE plpgsql
    AS $$
    DECLARE
    cuantos		int4;
    BEGIN

    delete from sucursalc where int4eq(sucu_cliente,OLD.cli_id);

    RETURN OLD;
    END;$$;


ALTER FUNCTION public.f_bd_clientes() OWNER TO rulemanes;

--
-- Name: f_bd_compra(); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.f_bd_compra() RETURNS trigger
    LANGUAGE plpgsql
    AS $$
    DECLARE
    BEGIN
    delete from movstoc where int4eq(movc_idcompra,OLD.cpra_id);
RETURN OLD;
END;$$;


ALTER FUNCTION public.f_bd_compra() OWNER TO rulemanes;

--
-- Name: f_bd_contratos(); Type: FUNCTION; Schema: public; Owner: estlm
--

CREATE FUNCTION public.f_bd_contratos() RETURNS trigger
    LANGUAGE plpgsql
    AS $$
    DECLARE
    registro	 record;
    BEGIN

	delete from contrb where int4eq(ctrb_idcontr,OLD.ctr_id);

    RETURN OLD;
    END;$$;


ALTER FUNCTION public.f_bd_contratos() OWNER TO estlm;

--
-- Name: f_bd_contrb(); Type: FUNCTION; Schema: public; Owner: estlm
--

CREATE FUNCTION public.f_bd_contrb() RETURNS trigger
    LANGUAGE plpgsql
    AS $$
    DECLARE
    registro	 record;
    BEGIN

	update dispenser set dis_idcontr=NULL, dis_idcontrb=-1, dis_estado=1 where dis_id=OLD.ctrb_iddispen;

    RETURN OLD;
    END;$$;


ALTER FUNCTION public.f_bd_contrb() OWNER TO estlm;

--
-- Name: f_bd_emiti(); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.f_bd_emiti() RETURNS trigger
    LANGUAGE plpgsql
    AS $$
    DECLARE
    cuantos	int4;
    BEGIN

    delete from movsto where int4eq(movs_idemiti,OLD.emi_id);
    delete from relnotp where int4eq(rn_idemitih,OLD.emi_id);
    delete from relnotp where int4eq(rn_idemitip,OLD.emi_id);
    delete from relcotnot where int4eq(rn_idemitih,OLD.emi_id);
    delete from relcotnot where int4eq(rn_idemitip,OLD.emi_id);

    RETURN OLD;
    END;$$;


ALTER FUNCTION public.f_bd_emiti() OWNER TO rulemanes;

--
-- Name: f_bd_empresa(); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.f_bd_empresa() RETURNS trigger
    LANGUAGE plpgsql
    AS $$
    DECLARE
    cuantos		int4;
    BEGIN

    delete from zona where int4eq(zon_codemp,OLD.emp_codigo);
    delete from vendedor where int4eq(ven_codemp,OLD.emp_codigo);
    delete from varambi where int4eq(vari_codemp,OLD.emp_codigo);
    delete from telefon where int4eq(tel_codemp,OLD.emp_codigo);
    delete from talon where int4eq(tal_codemp,OLD.emp_codigo);
    delete from subd where int4eq(subd_codemp,OLD.emp_codigo);
    delete from prove where int4eq(prov_codemp,OLD.emp_codigo);
    delete from clientes where int4eq(cli_codemp,OLD.emp_codigo);
    delete from ctes where int4eq(cte_codemp,OLD.emp_codigo);
    delete from ctacble where int4eq(cbl_codemp,OLD.emp_codigo);
    delete from cotiza where int4eq(cot_codemp,OLD.emp_codigo);
    delete from conpag where int4eq(cdp_codemp,OLD.emp_codigo);
    delete from interes where int4eq(int_codemp,OLD.emp_codigo);
    delete from listpre where int4eq(ltp_codemp,OLD.emp_codigo);
    delete from articvta where int4eq(art_codemp,OLD.emp_codigo);
    delete from comprob where int4eq(comp_codemp,OLD.emp_codigo);
    delete from centroc where int4eq(cco_codemp,OLD.emp_codigo);
    delete from division where int4eq(div_codemp,OLD.emp_codigo);
    delete from ptovta where int4eq(ptv_codemp,OLD.emp_codigo);

    RETURN OLD;
    END;$$;


ALTER FUNCTION public.f_bd_empresa() OWNER TO rulemanes;

--
-- Name: f_bd_movsto(); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.f_bd_movsto() RETURNS trigger
    LANGUAGE plpgsql
    AS $$
    DECLARE
    cont		integer;
    BEGIN

    delete from stock where int4eq(stc_idmovsto,OLD.movs_id);

	select max(log_cont)+1 into cont from modlogs where log_tabla='movsto' and log_idtabla=OLD.movs_id;
	if cont is null then
	    cont:=1;
	end if;
	insert into modlogs (log_idptov,log_comprob,log_numero,log_tabla,log_idtabla,log_campo,log_valant,log_valpos,log_cont) values (OLD.movs_idptov,1,1,'movsto',OLD.movs_id,'movs_idemiti',OLD.movs_idemiti,'delete',cont);
	insert into modlogs (log_idptov,log_comprob,log_numero,log_tabla,log_idtabla,log_campo,log_valant,log_valpos,log_cont) values (OLD.movs_idptov,1,1,'movsto',OLD.movs_id,'movs_fecval',OLD.movs_fecval,'delete',cont);
	insert into modlogs (log_idptov,log_comprob,log_numero,log_tabla,log_idtabla,log_campo,log_valant,log_valpos,log_cont) values (OLD.movs_idptov,1,1,'movsto',OLD.movs_id,'movs_artic',OLD.movs_artic,'delete',cont);
	insert into modlogs (log_idptov,log_comprob,log_numero,log_tabla,log_idtabla,log_campo,log_valant,log_valpos,log_cont) values (OLD.movs_idptov,1,1,'movsto',OLD.movs_id,'movs_precio',OLD.movs_precio,'delete',cont);
	insert into modlogs (log_idptov,log_comprob,log_numero,log_tabla,log_idtabla,log_campo,log_valant,log_valpos,log_cont) values (OLD.movs_idptov,1,1,'movsto',OLD.movs_id,'movs_canti',OLD.movs_canti,'delete',cont);
	insert into modlogs (log_idptov,log_comprob,log_numero,log_tabla,log_idtabla,log_campo,log_valant,log_valpos,log_cont) values (OLD.movs_idptov,1,1,'movsto',OLD.movs_id,'movs_importe',OLD.movs_importe,'delete',cont);
	insert into modlogs (log_idptov,log_comprob,log_numero,log_tabla,log_idtabla,log_campo,log_valant,log_valpos,log_cont) values (OLD.movs_idptov,1,1,'movsto',OLD.movs_id,'movs_codcli',OLD.movs_codcli,'delete',cont);


    RETURN OLD;
    END;$$;


ALTER FUNCTION public.f_bd_movsto() OWNER TO rulemanes;

--
-- Name: f_bd_movstoc(); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.f_bd_movstoc() RETURNS trigger
    LANGUAGE plpgsql
    AS $$
    DECLARE
    cont		integer;
    BEGIN

    delete from stock where int4eq(stc_idmovstoc,OLD.movc_id);

	select max(log_cont)+1 into cont from modlogs where log_tabla='movstoc' and log_idtabla=OLD.movc_id;
	if cont is null then
	    cont:=1;
	end if;
	insert into modlogs (log_idptov,log_comprob,log_numero,log_tabla,log_idtabla,log_campo,log_valant,log_valpos,log_cont) values (OLD.movc_idptov,1,1,'movstoc',OLD.movc_id,'movc_idcompra',OLD.movc_idcompra,'delete',cont);
	insert into modlogs (log_idptov,log_comprob,log_numero,log_tabla,log_idtabla,log_campo,log_valant,log_valpos,log_cont) values (OLD.movc_idptov,1,1,'movstoc',OLD.movc_id,'movc_fecval',OLD.movc_fecval,'delete',cont);
	insert into modlogs (log_idptov,log_comprob,log_numero,log_tabla,log_idtabla,log_campo,log_valant,log_valpos,log_cont) values (OLD.movc_idptov,1,1,'movstoc',OLD.movc_id,'movc_artic',OLD.movc_artic,'delete',cont);
	insert into modlogs (log_idptov,log_comprob,log_numero,log_tabla,log_idtabla,log_campo,log_valant,log_valpos,log_cont) values (OLD.movc_idptov,1,1,'movstoc',OLD.movc_id,'movc_precio',OLD.movc_precio,'delete',cont);
	insert into modlogs (log_idptov,log_comprob,log_numero,log_tabla,log_idtabla,log_campo,log_valant,log_valpos,log_cont) values (OLD.movc_idptov,1,1,'movstoc',OLD.movc_id,'movc_canti',OLD.movc_canti,'delete',cont);
	insert into modlogs (log_idptov,log_comprob,log_numero,log_tabla,log_idtabla,log_campo,log_valant,log_valpos,log_cont) values (OLD.movc_idptov,1,1,'movstoc',OLD.movc_id,'movc_importe',OLD.movc_importe,'delete',cont);

    RETURN OLD;
    END;$$;


ALTER FUNCTION public.f_bd_movstoc() OWNER TO rulemanes;

--
-- Name: f_bu_astoh(); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.f_bu_astoh() RETURNS trigger
    LANGUAGE plpgsql
    AS $$
DECLARE
saldo	numeric;
BEGIN


if(int4eq(NEW.ash_estado,2) and int4eq(OLD.ash_estado,-1)) THEN 


------------------ PARTIDA DOBLE -----------------------------------

    select sum(astc_importe) into saldo from astoctas where astc_idastoh=NEW.ash_id;
    if numeric_ne(saldo,0) then
	RAISE EXCEPTION  'NO SE PUEDE FINALIZAR EL ASIENTO YA QUE EL SALDO ES DISTINTO DE CERO';
    end if;

------------------ CASO CHEQUES -----------------------------------



END IF;
    RETURN NEW;
    END;$$;


ALTER FUNCTION public.f_bu_astoh() OWNER TO rulemanes;

--
-- Name: f_bu_cheques(); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.f_bu_cheques() RETURNS trigger
    LANGUAGE plpgsql
    AS $$
DECLARE
saldo	numeric;
BEGIN


if(int4eq(NEW.chq_estado,2) and int4ne(OLD.chq_estado,2)) THEN 


    select astc_importe into saldo from astoctas where astc_id=NEW.chq_idastoce;
    if found then
	RAISE EXCEPTION  'NO SE PUEDE CAMBIAR DE ESTADO UN CHEQUE UTILIZADO';
    end if;

END IF;
    RETURN NEW;
    END;$$;


ALTER FUNCTION public.f_bu_cheques() OWNER TO rulemanes;

--
-- Name: facturadoemi(integer); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.facturadoemi(integer) RETURNS integer
    LANGUAGE sql
    AS $_$select emi_facturado from emiti where emi_id = $1 ;$_$;


ALTER FUNCTION public.facturadoemi(integer) OWNER TO rulemanes;

--
-- Name: factvenc(integer, date); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.factvenc(integer, date) RETURNS void
    LANGUAGE plpgsql
    AS $_$
declare
codemp		 alias for $1;
fechah		 alias for $2;
ret 		 numeric;
registro	 record;
regcli		 record;
cuanto		 numeric;
ctadxv		 integer;
begin

ret:=0;

select idcbl(codemp, valvaramb(codemp,0,'DEUDXV')::smallint) into ctadxv;

update fact_vencidas set fv_estado=9;
for regcli in select * from clientes where cli_codemp=codemp and saldocli(codemp,99,cli_id)>0 order by 1
    loop
	for registro in select astc_idastoh from astoctas,emiti,conpag 
	    where astc_idastoh=emi_idastoh  and emi_conpag=cdp_codigo and astc_estado=2 and astc_codemp=codemp and astc_ctacble=ctadxv 
		and astc_tipcta=99 and astc_ctacte=regcli.cli_id and astc_cierra=1 and trim(codcomp(astc_comprob)) in ('FA','FB','DA','DB','FCE','DCE') 
		and date_le((astc_fecval + cdp_dias),fechah) order by 1 desc
	    loop
			insert into fact_vencidas (fv_idastoh,fv_cliente) values (registro.astc_idastoh,regcli.cli_id);
	    end loop;
    end loop;
end;$_$;


ALTER FUNCTION public.factvenc(integer, date) OWNER TO rulemanes;

--
-- Name: factxclief(integer, integer, date, date, integer); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.factxclief(integer, integer, date, date, integer) RETURNS numeric
    LANGUAGE plpgsql
    AS $_$
    DECLARE
	cli		ALIAS FOR $1;
	idptov		ALIAS FOR $2;
	fechad		ALIAS FOR $3;
	fechah		ALIAS FOR $4;
	modo		ALIAS FOR $5;
	retval 		numeric;
	ptovta 		integer;

	registro	record;
    BEGIN

    if(int4gt(idptov,0)) then
		if int4eq(modo,1::int4) then
		    select sum(case when trim(codcomp(emi_comprob)) in ('FB','CB','DB') then movs_importe/(1+(movs_tasa/100)) else movs_importe end)::numeric(15,2) into retval from movsto,emiti where movs_idemiti=emi_id and movs_idptov=idptov and movs_codcli=cli and emi_fecval between fechad and fechah and movs_estado=2 and movs_idastoh is not null and trim(codcomp(emi_comprob)) not in ('RE','DV','NP','COT');
		else
		    select sum(movs_canti) into retval from movsto,emiti where emi_id=movs_idemiti and movs_idptov=idptov and movs_codcli=cli and emi_fecval between fechad and fechah and movs_estado=2 and movs_idastoh is not null and trim(codcomp(emi_comprob)) not in ('RE','DV','NP','COT');
		end if;
    else
		if int4eq(modo,1::int4) then
		    select sum(case when trim(codcomp(emi_comprob)) in ('FB','CB','DB') then movs_importe/(1+(movs_tasa/100)) else movs_importe end)::numeric(15,2) into retval from movsto,emiti where movs_idemiti=emi_id and movs_codcli=cli and emi_fecval between fechad and fechah and movs_estado=2 and movs_idastoh is not null and trim(codcomp(emi_comprob)) not in ('RE','DV','NP','COT');
		else
		    select sum(movs_canti) into retval from movsto,emiti where emi_id=movs_idemiti and movs_codcli=cli and emi_fecval between fechad and fechah and movs_estado=2 and movs_idastoh is not null and trim(codcomp(emi_comprob)) not in ('RE','DV','NP','COT');
		end if;
    end if;

    if retval is null then
		retval:=0;
    end if;

    RETURN retval;
    END;$_$;


ALTER FUNCTION public.factxclief(integer, integer, date, date, integer) OWNER TO rulemanes;

--
-- Name: fec_compbte(integer, integer); Type: FUNCTION; Schema: public; Owner: zona
--

CREATE FUNCTION public.fec_compbte(integer, integer) RETURNS character varying
    LANGUAGE plpgsql
    AS $_$
    DECLARE
	comp		ALIAS FOR $1; -- Comprobante 
	modo		ALIAS FOR $2; -- 1 = comprobante , 2 = letra 
	
    BEGIN
	    if int4eq(modo,1) THEN
	    	case
	    		when comp=1 or comp=6 or comp=11 or comp=19 or comp=39 or comp=51 or comp=81 or comp=82 or comp=83 or comp=111 or comp=82 or comp=201 or comp=206 or comp=211 then
	    			return 'FK';
	    		when comp=2 or comp=7 or comp=12 or comp=20 or comp=52 or comp=115 or comp=116 or comp=117 or comp=202 or comp=207 or comp=212 THEN
	    			return 'DK';
	    		when comp=3 or comp=8 or comp=13 or comp=21 or comp=53 or comp=112 or comp=113 or comp=114 or comp=203 or comp=208 or comp=213 THEN
	    			return 'NK';
	    		else
	    			return 'FK';
	    	end case;
	    else
		    case  
		    	when comp>=1 and comp<=3 then
		    		return 'A';
		    	when comp>=6 and comp<=8 then
		    		return 'B';
				when comp>=11 and comp<=13 then
		    		return 'C';
				when comp>=19 and comp<=21 then
		    		return 'E';
				when comp=39 then
		    		return 'G';
				when comp>=51 and comp<=53 then
		    		return 'M';
				when (comp>=81 and comp<=83) or (comp>=110 and comp<=120) then
		    		return 'T';
				when comp>=201 and comp<=203 then
		    		return 'A';
				when comp>=206 and comp<=208 then
		    		return 'B';
				when comp>=211 and comp<=213 then
		    		return 'C';
		    	ELSE
		    		return 'A';
		    end case;
		end if;
	END;
$_$;


ALTER FUNCTION public.fec_compbte(integer, integer) OWNER TO zona;

--
-- Name: fecdechq(integer); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.fecdechq(integer) RETURNS date
    LANGUAGE sql
    AS $_$select chq_fecheq from cheques where chq_id= $1 ;$_$;


ALTER FUNCTION public.fecdechq(integer) OWNER TO rulemanes;

--
-- Name: fecha_fe(date); Type: FUNCTION; Schema: public; Owner: zona
--

CREATE FUNCTION public.fecha_fe(date) RETURNS character
    LANGUAGE sql
    AS $_$select date_part('Y',$1)::character varying||lpad(date_part('Month',$1)::text,2,'0'::text)::character varying||lpad(date_part('D',$1)::text,2,'0'::text)::character varying;$_$;


ALTER FUNCTION public.fecha_fe(date) OWNER TO zona;

--
-- Name: fecvalash(integer); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.fecvalash(integer) RETURNS date
    LANGUAGE sql
    AS $_$select ash_fecval from astoh where ash_id= $1;$_$;


ALTER FUNCTION public.fecvalash(integer) OWNER TO rulemanes;

--
-- Name: fecvalastc(integer); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.fecvalastc(integer) RETURNS date
    LANGUAGE sql
    AS $_$select astc_fecval from astoctas where astc_id= $1 ;$_$;


ALTER FUNCTION public.fecvalastc(integer) OWNER TO rulemanes;

--
-- Name: gastoct(integer, integer, integer, numeric); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.gastoct(integer, integer, integer, numeric) RETURNS integer
    LANGUAGE plpgsql
    AS $_$
    DECLARE
	idastoh		ALIAS FOR $1; 
	cuenta		ALIAS FOR $2; 
	grupo		ALIAS FOR $3; 
	importe		ALIAS FOR $4; 
	retval 		integer;
    BEGIN
    retval:=0;
    if numeric_ne(importe,0::numeric) then

	insert into astoctas (astc_idastoh,astc_ctacble,astc_importe,astc_grupo,astc_codemp,astc_idptov,astc_comprob,astc_numero,astc_fecval,astc_estado) 
	     select idastoh,cuenta,importe,grupo,ash_codemp,ash_idptov,ash_comprob,ash_numero,ash_fecval,-1 
	         from astoh where ash_id=idastoh;

	select astc_id into retval from astoctas order by astc_id desc limit 1;
    end if;
    RETURN retval;
    END;$_$;


ALTER FUNCTION public.gastoct(integer, integer, integer, numeric) OWNER TO rulemanes;

--
-- Name: gastoct_cta(integer, integer, integer, numeric, integer, integer, integer, date); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.gastoct_cta(integer, integer, integer, numeric, integer, integer, integer, date) RETURNS integer
    LANGUAGE plpgsql
    AS $_$
    DECLARE
	idastoh		ALIAS FOR $1; 
	cuenta		ALIAS FOR $2; 
	grupo		ALIAS FOR $3; 
	importe		ALIAS FOR $4; 
	tipcta		ALIAS FOR $5; 
	ctacte		ALIAS FOR $6; 
	nucheq		ALIAS FOR $7; 
	fecvto		ALIAS FOR $8; 
	retval 		integer;
    BEGIN
    retval:=0;
    if numeric_ne(importe,0::numeric) then
	insert into astoctas (astc_idastoh,astc_ctacble,astc_importe,astc_grupo,astc_codemp,astc_idptov,astc_comprob,astc_numero,astc_fecval,astc_estado,astc_tipcta,astc_ctacte,astc_nucheq,astc_fecvto) 
	     select idastoh,cuenta,importe,grupo,ash_codemp,ash_idptov,ash_comprob,ash_numero,ash_fecval,-1,tipcta,ctacte,nucheq,fecvto
	         from astoh where ash_id=idastoh;

	select astc_id into retval from astoctas order by astc_id desc limit 1;
    end if;
    RETURN retval;
    END;$_$;


ALTER FUNCTION public.gastoct_cta(integer, integer, integer, numeric, integer, integer, integer, date) OWNER TO rulemanes;

--
-- Name: generar_numero_reclamo(); Type: FUNCTION; Schema: public; Owner: zona
--

CREATE FUNCTION public.generar_numero_reclamo() RETURNS trigger
    LANGUAGE plpgsql
    AS $_$
DECLARE
    prefijo VARCHAR(10) := 'REC';
    anio VARCHAR(4);
    numero_seq INTEGER;
    nuevo_numero VARCHAR(20);
BEGIN
    -- Obtener el año actual
    anio := EXTRACT(YEAR FROM CURRENT_DATE)::VARCHAR;
    
    -- Obtener el próximo número de secuencia para este año
    SELECT COALESCE(MAX(SUBSTRING(rec_numero FROM '[0-9]+$')::INTEGER), 0) + 1
    INTO numero_seq
    FROM reclamos
    WHERE rec_numero LIKE prefijo || anio || '%';
    
    -- Formatear el número (REC2024001, REC2024002, etc.)
    nuevo_numero := prefijo || anio || LPAD(numero_seq::VARCHAR, 4, '0');
    
    -- Asignar el nuevo número al reclamo
    NEW.rec_numero := nuevo_numero;
    
    RETURN NEW;
END;
$_$;


ALTER FUNCTION public.generar_numero_reclamo() OWNER TO zona;

--
-- Name: getmonedaventa(integer); Type: FUNCTION; Schema: public; Owner: zona
--

CREATE FUNCTION public.getmonedaventa(p_art_id integer) RETURNS smallint
    LANGUAGE plpgsql STABLE
    AS $$
DECLARE
    v_moneda int2;
    v_codemp int4;
    v_codrub int4;
BEGIN
    SELECT art_codemp, art_codrub INTO v_codemp, v_codrub
    FROM articvta WHERE art_id = p_art_id;

    -- Artículo
    SELECT pm_moneda INTO v_moneda
    FROM politmoneda
    WHERE pm_codemp = v_codemp
      AND pm_tiponivel = 3
      AND pm_idnivel = p_art_id
      AND pm_tipo = 2
      AND pm_estado = 1
    LIMIT 1;

    IF v_moneda IS NOT NULL THEN
        RETURN v_moneda;
    END IF;

    -- Rubro
    SELECT pm_moneda INTO v_moneda
    FROM politmoneda
    WHERE pm_codemp = v_codemp
      AND pm_tiponivel = 2
      AND pm_idnivel = v_codrub
      AND pm_tipo = 2
      AND pm_estado = 1
    LIMIT 1;

    IF v_moneda IS NOT NULL THEN
        RETURN v_moneda;
    END IF;

    -- Empresa
    SELECT pm_moneda INTO v_moneda
    FROM politmoneda
    WHERE pm_codemp = v_codemp
      AND pm_tiponivel = 1
      AND pm_idnivel = v_codemp
      AND pm_tipo = 2
      AND pm_estado = 1
    LIMIT 1;

    RETURN COALESCE(v_moneda, 1); -- Default ARS
END;
$$;


ALTER FUNCTION public.getmonedaventa(p_art_id integer) OWNER TO zona;

--
-- Name: graboastocta(integer, numeric, numeric, numeric); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.graboastocta(integer, numeric, numeric, numeric) RETURNS integer
    LANGUAGE plpgsql
    AS $_$
    DECLARE
	idastoh		ALIAS FOR $1; 
	iva		ALIAS FOR $2; 
	ivast		ALIAS FOR $3; 
	descto		ALIAS FOR $4; 
	registro	record;
	regis		record;
	codemp		integer;
	idptov		integer;
	comprob 	integer;
	numero		integer;
	succli		integer;
	fecval		date;
	total		numeric;
	otros		numeric;
	ctadeud		integer;
	tipct		integer;
	tctdepg		integer;
	elcli		integer;
	signo		integer;
	grupo		integer;
	ctaiva		integer;
	ctaivast	integer;
	ctadescto	integer;
	codcompr	character varying;
	retval 		integer;
    BEGIN
select ash_codemp,ash_idptov,ash_comprob,ash_numero,ash_fecval into codemp,idptov,comprob,numero,fecval from astoh where ash_id=idastoh;
select idcbl(codemp, valvaramb(codemp,1,'DEUDXV')::smallint) into ctadeud;
if ctadeud is null then
    select idcbl(codemp, valvaramb(codemp,idptov,'DEUDXV')::smallint) into ctadeud;
end if;
select valvaramb(codemp,1,'TIPCTADEPG')::integer into tctdepg;
if tctdepg is null then
    select valvaramb(codemp,idptov,'TIPCTADEPG')::integer into tctdepg;
end if;
select tipctacbl(ctadeud) into tipct;
select emi_codcli,emi_grupo,emi_succli into elcli,grupo,succli from emiti where emi_idastoh=idastoh;
select codcompash(idastoh) into codcompr;

if (bpchareq(codcompr,'CA') or bpchareq(codcompr,'CB') or bpchareq(codcompr,'CC')) then
	signo= -1; 
    else
	signo= 1; 
end if;


total:=0;
for registro in select a.art_idcbl as cuenta, b.movs_tasa as latasa, tipctacbl(a.art_idcbl) as cualtipcta, sum(b.movs_importe) as cuanto 
     from movsto b, articvta a where b.movs_idastoh=idastoh 
        and b.movs_artic=a.art_id group by 1,2,3
	loop

	if found then 
	    if(int4eq(registro.cualtipcta, tctdepg)) then
		insert into astoctas (astc_ctacte,astc_tipcta,astc_tasa,astc_ctacble,astc_importe,astc_idastoh,astc_estado,astc_codemp,astc_idptov,astc_comprob,astc_numero,astc_fecval) 
		    values (idctes(tctdepg,codcli(elcli)),tctdepg,registro.latasa,registro.cuenta,(registro.cuanto * -1)::numeric,idastoh,-1::int2,codemp,idptov,comprob,numero,fecval::date);
	      else
		insert into astoctas (astc_tasa,astc_ctacble,astc_importe,astc_idastoh,astc_estado,astc_codemp,astc_idptov,astc_comprob,astc_numero,astc_fecval) 
		    values (registro.latasa,registro.cuenta,(registro.cuanto * -1)::numeric,idastoh,-1::int2,codemp,idptov,comprob,numero,fecval::date);
	    end if;
	    total:=total + registro.cuanto;
	end if;
end loop; 


for regis in select movs_tasa as vlatasa, sum(movs_importe) as vcuanto
     from movsto where movs_idastoh=idastoh group by 1
	loop
	if found then 
	    if numeric_ne(regis.vlatasa,0::numeric) then 
		select idcbl(codemp, valvaramb(codemp,1,'CTAIVA')::smallint) into ctaiva;
		if ctaiva is null then
		    select idcbl(codemp, valvaramb(codemp,idptov,'CTAIVA')::smallint) into ctaiva;
		end if;
		insert into astoctas (astc_tasa,astc_ctacble,astc_importe,astc_idastoh,astc_estado,astc_codemp,astc_idptov,astc_comprob,astc_numero,astc_fecval) 
		    values (regis.vlatasa,ctaiva,(regis.vcuanto * -1 * regis.vlatasa / 100)::numeric(15,2),idastoh,-1::int2,codemp,idptov,comprob,numero,fecval::date);
	    end if;
	end if;
end loop; 


if numeric_ne(iva,0::numeric) then
    total:=total + iva;
end if;

if numeric_ne(ivast,0::numeric) then
select idcbl(codemp, valvaramb(codemp,1,'CTAIVAST')::smallint) into ctaivast;
if ctaivast is null then
    select idcbl(codemp, valvaramb(codemp,idptov,'CTAIVAST')::smallint) into ctaivast;
end if;
    insert into astoctas (astc_ctacble,astc_importe,astc_idastoh,astc_estado,astc_codemp,astc_idptov,astc_comprob,astc_numero,astc_fecval) 
	values (ctaivast,(ivast * -1 * signo)::numeric,idastoh,-1::int2,codemp,idptov,comprob,numero,fecval::date);
total:=total + (ivast * signo);
end if;

if numeric_ne(descto,0::numeric) then
select idcbl(codemp, valvaramb(codemp,1,'CTADESCTO')::smallint) into ctadescto;
if ctadescto is null then
    select idcbl(codemp, valvaramb(codemp,idptov,'CTADESCTO')::smallint) into ctadescto;
end if;
    insert into astoctas (astc_ctacble,astc_importe,astc_idastoh,astc_estado,astc_codemp,astc_idptov,astc_comprob,astc_numero,astc_fecval) 
	values (ctadescto,(descto * -1 * signo)::numeric,idastoh,-1::int2,codemp,idptov,comprob,numero,fecval::date);
total:=total + (descto * signo);
end if;

insert into astoctas (astc_succli,astc_grupo,astc_ctacte,astc_tipcta,astc_ctacble,astc_importe,astc_idastoh,astc_estado,astc_codemp,astc_idptov,astc_comprob,astc_numero,astc_fecval) 
	values (succli,grupo,elcli,tipct,ctadeud,total::numeric,idastoh,-1::int2,codemp,idptov,comprob,numero,fecval::date);

update emiti set emi_total=total where emi_idastoh=idastoh;

retval:=signo;

RETURN retval;
END;$_$;


ALTER FUNCTION public.graboastocta(integer, numeric, numeric, numeric) OWNER TO rulemanes;

--
-- Name: graboastocta(integer, numeric, numeric, numeric, numeric, numeric); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.graboastocta(integer, numeric, numeric, numeric, numeric, numeric) RETURNS integer
    LANGUAGE plpgsql
    AS $_$
    DECLARE
	idastoh		ALIAS FOR $1; 
	iva		ALIAS FOR $2; 
	ivast		ALIAS FOR $3; 
	descto		ALIAS FOR $4; 
	iibb		ALIAS FOR $5; 
	alic		ALIAS FOR $6; 
	registro	record;
	regis		record;
	codemp		integer;
	idptov		integer;
	comprob 	integer;
	numero		integer;
	succli		integer;
	fecval		date;
	total		numeric;
	otros		numeric;
	ctadeud		integer;
	tipct		integer;
	tctdepg		integer;
	elcli		integer;
	signo		integer;
	grupo		integer;
	ctaiva		integer;
	ctaivast	integer;
	ctadescto	integer;
	ctaiibb		integer;
	codcompr	character varying;
	retval 		integer;
    BEGIN
select ash_codemp,ash_idptov,ash_comprob,ash_numero,ash_fecval into codemp,idptov,comprob,numero,fecval from astoh where ash_id=idastoh;
select idcbl(codemp, valvaramb(codemp,1,'DEUDXV')::smallint) into ctadeud;
if ctadeud is null then
    select idcbl(codemp, valvaramb(codemp,idptov,'DEUDXV')::smallint) into ctadeud;
end if;
select valvaramb(codemp,1,'TIPCTADEPG')::integer into tctdepg;
if tctdepg is null then
    select valvaramb(codemp,idptov,'TIPCTADEPG')::integer into tctdepg;
end if;
select tipctacbl(ctadeud) into tipct;
select emi_codcli,emi_grupo,emi_succli into elcli,grupo,succli from emiti where emi_idastoh=idastoh;
select codcompash(idastoh) into codcompr;

if (bpchareq(codcompr,'CA') or bpchareq(codcompr,'CB') or bpchareq(codcompr,'CC')) then
	signo= -1; 
    else
	signo= 1; 
end if;


total:=0;
for registro in select a.art_idcbl as cuenta, b.movs_tasa as latasa, tipctacbl(a.art_idcbl) as cualtipcta, sum(b.movs_importe) as cuanto 
     from movsto b, articvta a where b.movs_idastoh=idastoh 
        and b.movs_artic=a.art_id group by 1,2,3
	loop

	if found then 
	    if(int4eq(registro.cualtipcta, tctdepg)) then
		insert into astoctas (astc_ctacte,astc_tipcta,astc_tasa,astc_ctacble,astc_importe,astc_idastoh,astc_estado,astc_codemp,astc_idptov,astc_comprob,astc_numero,astc_fecval) 
		    values (idctes(tctdepg,codcli(elcli)),tctdepg,registro.latasa,registro.cuenta,(registro.cuanto * -1)::numeric,idastoh,-1::int2,codemp,idptov,comprob,numero,fecval::date);
	      else
		insert into astoctas (astc_tasa,astc_ctacble,astc_importe,astc_idastoh,astc_estado,astc_codemp,astc_idptov,astc_comprob,astc_numero,astc_fecval) 
		    values (registro.latasa,registro.cuenta,(registro.cuanto * -1)::numeric,idastoh,-1::int2,codemp,idptov,comprob,numero,fecval::date);
	    end if;
	    total:=total + registro.cuanto;
	end if;
end loop; 


for regis in select movs_tasa as vlatasa, sum(movs_importe) as vcuanto
     from movsto where movs_idastoh=idastoh group by 1
	loop
	if found then 
	    if numeric_ne(regis.vlatasa,0::numeric) then 
		select idcbl(codemp, valvaramb(codemp,1,'CTAIVA')::smallint) into ctaiva;
		if ctaiva is null then
		    select idcbl(codemp, valvaramb(codemp,idptov,'CTAIVA')::smallint) into ctaiva;
		end if;
		insert into astoctas (astc_tasa,astc_ctacble,astc_importe,astc_idastoh,astc_estado,astc_codemp,astc_idptov,astc_comprob,astc_numero,astc_fecval) 
		    values (regis.vlatasa,ctaiva,(regis.vcuanto * -1 * regis.vlatasa / 100)::numeric(15,2),idastoh,-1::int2,codemp,idptov,comprob,numero,fecval::date);
	    end if;
	end if;
end loop; 


if numeric_ne(iva,0::numeric) then
    total:=total + iva;
end if;

if numeric_ne(ivast,0::numeric) then
select idcbl(codemp, valvaramb(codemp,1,'CTAIVAST')::smallint) into ctaivast;
if ctaivast is null then
    select idcbl(codemp, valvaramb(codemp,idptov,'CTAIVAST')::smallint) into ctaivast;
end if;
    insert into astoctas (astc_ctacble,astc_importe,astc_idastoh,astc_estado,astc_codemp,astc_idptov,astc_comprob,astc_numero,astc_fecval) 
	values (ctaivast,(ivast * -1 * signo)::numeric,idastoh,-1::int2,codemp,idptov,comprob,numero,fecval::date);
total:=total + (ivast * signo);
end if;

if numeric_ne(descto,0::numeric) then
select idcbl(codemp, valvaramb(codemp,1,'CTADESCTO')::smallint) into ctadescto;
if ctadescto is null then
    select idcbl(codemp, valvaramb(codemp,idptov,'CTADESCTO')::smallint) into ctadescto;
end if;
    insert into astoctas (astc_ctacble,astc_importe,astc_idastoh,astc_estado,astc_codemp,astc_idptov,astc_comprob,astc_numero,astc_fecval) 
	values (ctadescto,(descto * -1 * signo)::numeric,idastoh,-1::int2,codemp,idptov,comprob,numero,fecval::date);
total:=total + (descto * signo);
end if;

if numeric_ne(iibb,0::numeric) then
    select idcbl(codemp,valvaramb(codemp,1,'CTAPERIBD')::smallint) into ctaiibb;
    if ctaiibb is null then
	select idcbl(codemp, valvaramb(codemp,idptov,'CTAPERIBD')::smallint) into ctaiibb;
    end if;
    insert into astoctas (astc_tasa,astc_ctacble,astc_importe,astc_idastoh,astc_estado,astc_codemp,astc_idptov,astc_comprob,astc_numero,astc_fecval) 
	values (alic,ctaiibb,(iibb * -1)::numeric,idastoh,-1::int2,codemp,idptov,comprob,numero,fecval::date);
    total:=total + iibb;
end if;


insert into astoctas (astc_succli,astc_grupo,astc_ctacte,astc_tipcta,astc_ctacble,astc_importe,astc_idastoh,astc_estado,astc_codemp,astc_idptov,astc_comprob,astc_numero,astc_fecval) 
	values (succli,grupo,elcli,tipct,ctadeud,total::numeric,idastoh,-1::int2,codemp,idptov,comprob,numero,fecval::date);

update emiti set emi_total=total where emi_idastoh=idastoh;

retval:=signo;

RETURN retval;
END;$_$;


ALTER FUNCTION public.graboastocta(integer, numeric, numeric, numeric, numeric, numeric) OWNER TO rulemanes;

--
-- Name: graboastoctac(integer, numeric, numeric, numeric); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.graboastoctac(integer, numeric, numeric, numeric) RETURNS integer
    LANGUAGE plpgsql
    AS $_$
    DECLARE
	idastoh		ALIAS FOR $1; 
	iva21		ALIAS FOR $2; 
	iva27		ALIAS FOR $3; 
	iva105		ALIAS FOR $4; 
	registro	record;
	regis		record;
	codemp		integer;
	idptov		integer;
	comprob 	integer;
	numero		integer;
	fecval		date;
	total		numeric;
	totalcpra	numeric;
	otros		numeric;
	impiva		numeric;
	ctadeud		integer;
	tipct		integer;
	tctdepg		integer;
	elcli		integer;
	signo		integer;
	grupo		integer;
	ctaiva		integer;
	ctaivast	integer;
	ctadescto	integer;
	ctaiibb		integer;
	codcompr	character varying;
	retval 		integer;
    BEGIN
select ash_codemp,ash_idptov,ash_comprob,ash_numero,ash_fecval into codemp,idptov,comprob,numero,fecval from astoh where ash_id=idastoh;
select idcbl(codemp, valvaramb(codemp,1,'DEUDXC')::smallint) into ctadeud;
if ctadeud is null then
    select idcbl(codemp, valvaramb(codemp,idptov,'DEUDXC')::smallint) into ctadeud;
end if;
select tipctacbl(ctadeud) into tipct;
select cpra_prove into elcli from compra where cpra_idastoh=idastoh;
select grupoprov(elcli,1) into grupo;
select codcompash(idastoh) into codcompr;

if (bpchareq(codcompr,'NK')) then
	signo= -1; 
    else
	signo= 1; 
end if;


total:=0;

select sum(astc_importe)::numeric(15,2) into otros from astoctas where astc_idastoh=idastoh;
if numeric_ne(otros,0::numeric) then
    if (bpchareq(codcompr,'NK')) then
	otros:=otros*-1;
    end if;
    total:= total - otros;
end if;


for registro in select a.artc_idcbl as cuenta, b.movc_tasa as latasa, tipctacbl(a.artc_idcbl) as cualtipcta, sum(b.movc_importe) as cuanto 
     from movstoc b, articu a where b.movc_idastoh=idastoh 
        and b.movc_artic=a.artc_id group by 1,2,3
	loop
	if found then 
	    insert into astoctas (astc_tasa,astc_ctacble,astc_importe,astc_idastoh,astc_estado,astc_codemp,astc_idptov,astc_comprob,astc_numero,astc_fecval) 
		    values (registro.latasa,registro.cuenta,(registro.cuanto * signo)::numeric,idastoh,-1::int2,codemp,idptov,comprob,numero,fecval::date);
	    total:=total - registro.cuanto;
	end if;
end loop; 


for regis in select movc_tasa as vlatasa, sum(movc_importe) as vcuanto
     from movstoc where movc_idastoh=idastoh group by 1
	loop
	if found then 
	    if numeric_ne(regis.vlatasa,0::numeric) then 
		select idcbl(codemp, valvaramb(codemp,1,'CTAIVAC')::smallint) into ctaiva;
		if ctaiva is null then
		    select idcbl(codemp, valvaramb(codemp,idptov,'CTAIVAC')::smallint) into ctaiva;
		end if;

		if(numeric_eq(regis.vlatasa,21::numeric)) then
		    impiva:=iva21;
		else
		    if(numeric_eq(regis.vlatasa,27::numeric)) then
			impiva:=iva27;
		    else
			if(numeric_eq(regis.vlatasa,10.5::numeric)) then
			    impiva:=iva105;
			end if;
		    end if;
		end if;
		insert into astoctas (astc_tasa,astc_ctacble,astc_importe,astc_idastoh,astc_estado,astc_codemp,astc_idptov,astc_comprob,astc_numero,astc_fecval) 
		    values (regis.vlatasa,ctaiva,(signo * impiva)::numeric(15,2),idastoh,-1::int2,codemp,idptov,comprob,numero,fecval::date);
	    end if;
	end if;
end loop; 

if numeric_ne(iva21,0::numeric) then
    total:=total - iva21;
end if;
if numeric_ne(iva27,0::numeric) then
    total:=total - iva27;
end if;
if numeric_ne(iva105,0::numeric) then
    total:=total - iva105;
end if;


insert into astoctas (astc_grupo,astc_ctacte,astc_tipcta,astc_ctacble,astc_importe,astc_idastoh,astc_estado,astc_codemp,astc_idptov,astc_comprob,astc_numero,astc_fecval) 
	values (grupo,elcli,tipct,ctadeud,(total*signo)::numeric(15,2),idastoh,-1::int2,codemp,idptov,comprob,numero,fecval::date);

select cpra_total into totalcpra from compra where cpra_idastoh=idastoh;
if(numeric_ne(totalcpra,total))then
    if (bpchareq(codcompr,'NK')) then
	if(numeric_ge(total,0::numeric)) then
	    total:=total*-1;
	    update compra set cpra_total=total where cpra_idastoh=idastoh;
	end if;
    else
	if(numeric_le(total,0::numeric)) then
	    total:=total*-1;
	    update compra set cpra_total=total where cpra_idastoh=idastoh;
	end if;
    end if;
end if;

retval:=signo;

RETURN retval;
END;$_$;


ALTER FUNCTION public.graboastoctac(integer, numeric, numeric, numeric) OWNER TO rulemanes;

--
-- Name: graboastoctac(integer, numeric, numeric, numeric, integer, integer); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.graboastoctac(integer, numeric, numeric, numeric, integer, integer) RETURNS integer
    LANGUAGE plpgsql
    AS $_$
    DECLARE
	idastoh		ALIAS FOR $1; 
	iva		ALIAS FOR $2; 
	ivast		ALIAS FOR $3; 
	descto		ALIAS FOR $4; 
	tipcta		ALIAS FOR $5; 
	ctact		ALIAS FOR $6; 
	registro	record;
	regis		record;
	codemp		integer;
	idptov		integer;
	comprob 	integer;
	numero		integer;
	fecval		date;
	total		numeric;
	otros		numeric;
	ctadeud		integer;
	tipct		integer;
	tctdepg		integer;
	elcli		integer;
	signo		integer;
	grupo		integer;
	ctaiva		integer;
	ctaivast	integer;
	ctadescto	integer;
	codcompr	character varying;
	retval 		integer;
    BEGIN
select ash_codemp,ash_idptov,ash_comprob,ash_numero,ash_fecval into codemp,idptov,comprob,numero,fecval from astoh where ash_id=idastoh;
select idcbl(codemp, valvaramb(codemp,idptov,'DEUDXV')::smallint) into ctadeud;
select valvaramb(codemp,1,'TIPCTADEPG')::integer into tctdepg;
select tipctacbl(ctadeud) into tipct;
select emi_codcli,emi_grupo into elcli,grupo from emiti where emi_idastoh=idastoh;
select codcompash(idastoh) into codcompr;

if (bpchareq(codcompr,'CA') or bpchareq(codcompr,'CB') or bpchareq(codcompr,'CC')) then
	signo= -1; 
    else
	signo= 1; 
end if;


total:=0;
for registro in select a.art_idcbl as cuenta, b.movs_tasa as latasa, tipctacbl(a.art_idcbl) as cualtipcta, sum(b.movs_importe) as cuanto 
     from movsto b, articvta a where b.movs_idastoh=idastoh 
        and b.movs_artic=a.art_id group by 1,2,3
	loop

	if found then 
	    if(int4eq(registro.cualtipcta, tctdepg)) then
		insert into astoctas (astc_ctacte,astc_tipcta,astc_tasa,astc_ctacble,astc_importe,astc_idastoh,astc_estado,astc_codemp,astc_idptov,astc_comprob,astc_numero,astc_fecval) 
		    values (idctes(tctdepg,codcli(elcli)),tctdepg,registro.latasa,registro.cuenta,(registro.cuanto * -1)::numeric,idastoh,-1::int2,codemp,idptov,comprob,numero,fecval::date);
	      else
	    if(int4eq(registro.cualtipcta, tipcta)) then
		insert into astoctas (astc_ctacte,astc_tipcta,astc_tasa,astc_ctacble,astc_importe,astc_idastoh,astc_estado,astc_codemp,astc_idptov,astc_comprob,astc_numero,astc_fecval) 
		    values (ctact,tipcta,registro.latasa,registro.cuenta,(registro.cuanto * -1)::numeric,idastoh,-1::int2,codemp,idptov,comprob,numero,fecval::date);
	      else
		insert into astoctas (astc_tasa,astc_ctacble,astc_importe,astc_idastoh,astc_estado,astc_codemp,astc_idptov,astc_comprob,astc_numero,astc_fecval) 
		    values (registro.latasa,registro.cuenta,(registro.cuanto * -1)::numeric,idastoh,-1::int2,codemp,idptov,comprob,numero,fecval::date);
	    end if;
	    end if;
	    total:=total + registro.cuanto;
	end if;
end loop; 


for regis in select movs_tasa as vlatasa, sum(movs_importe) as vcuanto
     from movsto where movs_idastoh=idastoh group by 1
	loop
	if found then 
	    if numeric_ne(regis.vlatasa,0::numeric) then 
		select idcbl(codemp, valvaramb(codemp,idptov,'CTAIVA')::smallint) into ctaiva;
		insert into astoctas (astc_tasa,astc_ctacble,astc_importe,astc_idastoh,astc_estado,astc_codemp,astc_idptov,astc_comprob,astc_numero,astc_fecval) 
		    values (regis.vlatasa,ctaiva,(regis.vcuanto * -1 * regis.vlatasa / 100)::numeric(15,2),idastoh,-1::int2,codemp,idptov,comprob,numero,fecval::date);
	    end if;
	end if;
end loop; 


if numeric_ne(iva,0::numeric) then
    total:=total + iva;
end if;

if numeric_ne(ivast,0::numeric) then
select idcbl(codemp, valvaramb(codemp,idptov,'CTAIVAST')::smallint) into ctaivast;
    insert into astoctas (astc_ctacble,astc_importe,astc_idastoh,astc_estado,astc_codemp,astc_idptov,astc_comprob,astc_numero,astc_fecval) 
	values (ctaivast,(ivast * -1 * signo)::numeric,idastoh,-1::int2,codemp,idptov,comprob,numero,fecval::date);
total:=total + (ivast * signo);
end if;

if numeric_ne(descto,0::numeric) then
select idcbl(codemp, valvaramb(codemp,idptov,'CTADESCTO')::smallint) into ctadescto;
    insert into astoctas (astc_ctacble,astc_importe,astc_idastoh,astc_estado,astc_codemp,astc_idptov,astc_comprob,astc_numero,astc_fecval) 
	values (ctadescto,(descto * -1 * signo)::numeric,idastoh,-1::int2,codemp,idptov,comprob,numero,fecval::date);
total:=total + (descto * signo);
end if;

insert into astoctas (astc_grupo,astc_ctacte,astc_tipcta,astc_ctacble,astc_importe,astc_idastoh,astc_estado,astc_codemp,astc_idptov,astc_comprob,astc_numero,astc_fecval) 
	values (grupo,elcli,tipct,ctadeud,total::numeric,idastoh,-1::int2,codemp,idptov,comprob,numero,fecval::date);

update emiti set emi_total=total where emi_idastoh=idastoh;

retval:=signo;

RETURN retval;
END;$_$;


ALTER FUNCTION public.graboastoctac(integer, numeric, numeric, numeric, integer, integer) OWNER TO rulemanes;

--
-- Name: graboastoctaj(integer, numeric, numeric, numeric, numeric, numeric); Type: FUNCTION; Schema: public; Owner: zona
--

CREATE FUNCTION public.graboastoctaj(integer, numeric, numeric, numeric, numeric, numeric) RETURNS integer
    LANGUAGE plpgsql
    AS $_$
    DECLARE
	idastoh		ALIAS FOR $1; 
	iva		ALIAS FOR $2; 
	ivast		ALIAS FOR $3; 
	descto		ALIAS FOR $4; 
	iibb		ALIAS FOR $5; 
	alic		ALIAS FOR $6; 
	registro	record;
	regis		record;
	codemp		integer;
	idptov		integer;
	comprob 	integer;
	numero		integer;
	fecval		date;
	total		numeric;
	otros		numeric;
	ctadeud		integer;
	tipct		integer;
	auximp		numeric(15,2);
	auxiva		numeric(15,2);
	difiva		numeric(15,2);
	iibbp		numeric(15,2);
	ivap		numeric(15,2);
	elcli		integer;
	signo		integer;
	grupo		integer;
	ctaiva		integer;
	ctaivast	integer;
	ctadescto	integer;
	ctaiibb		integer;
	codcompr	character varying;
    BEGIN

delete from astoctas where astc_idastoh=idastoh;
select ash_codemp,ash_idptov,ash_comprob,ash_numero,ash_fecval into codemp,idptov,comprob,numero,fecval from astoh where ash_id=idastoh;

select idcbl(codemp, valvaramb(codemp,1,'DEUDXV')::smallint) into ctadeud;
if ctadeud is null then
    select idcbl(codemp, valvaramb(codemp,idptov,'DEUDXV')::smallint) into ctadeud;
end if;
select tipctacbl(ctadeud) into tipct;

select idcbl(codemp, valvaramb(codemp,1,'CTAIVA')::smallint) into ctaiva;
if ctaiva is null then
    select idcbl(codemp, valvaramb(codemp,idptov,'CTAIVA')::smallint) into ctaiva;
end if;

select emi_codcli,emi_grupo into elcli,grupo from emiti where emi_idastoh=idastoh;
select trim(codcomp(ash_comprob)) into codcompr from astoh where ash_id=idastoh;

if ((codcompr is not distinct from 'CA') or (codcompr is not distinct from 'CB') or (codcompr is not distinct from 'CC')) then
	signo= -1; 
    else
	signo= 1; 
end if;



total:=0;
for registro in select a.art_idcbl as cuenta, b.movs_tasa as latasa, tipctacbl(a.art_idcbl) as cualtipcta, sum(b.movs_importe) as cuanto 
     from movsto b, articvta a where b.movs_idastoh=idastoh 
        and b.movs_artic=a.art_id group by 1,2,3
	loop
	    if (bpchareq(codcompr,'FB') or bpchareq(codcompr,'CB') or bpchareq(codcompr,'DB')) then
		auximp:=registro.cuanto / (1 + registro.latasa/100);
	    else
		auximp:=registro.cuanto;
	    end if;
	    if (auximp<0) then auximp:=auximp*-1; end if;

	    insert into astoctas (astc_tasa,astc_ctacble,astc_importe,astc_idastoh,astc_estado,astc_codemp,astc_idptov,astc_comprob,astc_numero,astc_fecval) 
		    values (registro.latasa,registro.cuenta,(auximp * -1 * signo)::numeric,idastoh,-1::int2,codemp,idptov,comprob,numero,fecval::date);
	    total:=total + (auximp * signo);
	end loop; 


for regis in select movs_tasa as vlatasa, sum(movs_importe) as vcuanto
     from movsto where movs_idastoh=idastoh group by 1
	loop
	    if (bpchareq(codcompr,'FB') or bpchareq(codcompr,'CB') or bpchareq(codcompr,'DB')) then
		auximp:=regis.vcuanto / (1 + regis.vlatasa/100);
	    else
		auximp:=regis.vcuanto;
	    end if;

	    if (auximp<0) then auximp:=auximp*-1; end if;

	    if numeric_ne(regis.vlatasa,0::numeric) then 
		insert into astoctas (astc_tasa,astc_ctacble,astc_importe,astc_idastoh,astc_estado,astc_codemp,astc_idptov,astc_comprob,astc_numero,astc_fecval) 
		    values (regis.vlatasa,ctaiva,(auximp * -1 * signo * regis.vlatasa / 100)::numeric(15,2),idastoh,-1::int2,codemp,idptov,comprob,numero,fecval::date);
	    end if;
	end loop; 

select sum(astc_importe) into auxiva from astoctas where astc_idastoh=idastoh and astc_ctacble=ctaiva;

if (iva<0) then ivap:=iva*-1; else ivap:=iva; end if;

if (numeric_ne(ivap,auxiva)) then
    difiva:=ivap-auxiva;
    if numeric_le(difiva,0) then
	difiva:=difiva*-1;
    end if;
    update astoctas set astc_importe=(ivap*-1*signo) where astc_idastoh=idastoh and astc_ctacble=ctaiva;
end if;

if numeric_ne(ivap,0::numeric) then
    total:=total + (ivap * signo);
end if;

if numeric_ne(ivast,0::numeric) then
    select idcbl(codemp, valvaramb(codemp,1,'CTAIVAST')::smallint) into ctaivast;
    if ctaivast is null then
	select idcbl(codemp, valvaramb(codemp,idptov,'CTAIVAST')::smallint) into ctaivast;
    end if;

    insert into astoctas (astc_ctacble,astc_importe,astc_idastoh,astc_estado,astc_codemp,astc_idptov,astc_comprob,astc_numero,astc_fecval) 
	values (ctaivast,(ivast * -1 * signo)::numeric,idastoh,-1::int2,codemp,idptov,comprob,numero,fecval::date);

    total:=total + (ivast * signo);
end if;

if numeric_ne(descto,0::numeric) then
    select idcbl(codemp, valvaramb(codemp,1,'CTADESCTO')::smallint) into ctadescto;
    if ctadescto is null then
	select idcbl(codemp, valvaramb(codemp,idptov,'CTADESCTO')::smallint) into ctadescto;
    end if;
    insert into astoctas (astc_ctacble,astc_importe,astc_idastoh,astc_estado,astc_codemp,astc_idptov,astc_comprob,astc_numero,astc_fecval) 
	values (ctadescto,(descto * -1 * signo)::numeric,idastoh,-1::int2,codemp,idptov,comprob,numero,fecval::date);
    total:=total + (descto * signo);
end if;

if numeric_ne(iibb,0::numeric) then
    if(iibb<0) then iibbp:=iibb*-1; else iibbp:=iibb; end if;
    select idcbl(codemp,valvaramb(codemp,1,'CTAPERIBD')::smallint) into ctaiibb;
    if ctaiibb is null then
	select idcbl(codemp, valvaramb(codemp,idptov,'CTAPERIBD')::smallint) into ctaiibb;
    end if;
    insert into astoctas (astc_tasa,astc_ctacble,astc_importe,astc_idastoh,astc_estado,astc_codemp,astc_idptov,astc_comprob,astc_numero,astc_fecval) 
	values (alic,ctaiibb,(iibbp * -1 * signo)::numeric,idastoh,-1::int2,codemp,idptov,comprob,numero,fecval::date);
    total:=total + (iibbp * signo);
end if;


insert into astoctas (astc_grupo,astc_ctacte,astc_tipcta,astc_ctacble,astc_importe,astc_idastoh,astc_estado,astc_codemp,astc_idptov,astc_comprob,astc_numero,astc_fecval) 
	values (grupo,elcli,tipct,ctadeud,total::numeric,idastoh,-1::int2,codemp,idptov,comprob,numero,fecval::date);

update emiti set emi_total=total where emi_idastoh=idastoh;

RETURN signo;
END;$_$;


ALTER FUNCTION public.graboastoctaj(integer, numeric, numeric, numeric, numeric, numeric) OWNER TO zona;

--
-- Name: graboastoctan(integer, numeric, numeric, numeric, numeric, numeric); Type: FUNCTION; Schema: public; Owner: estlm
--

CREATE FUNCTION public.graboastoctan(integer, numeric, numeric, numeric, numeric, numeric) RETURNS integer
    LANGUAGE plpgsql
    AS $_$
    DECLARE
	idastoh		ALIAS FOR $1; 
	iva		ALIAS FOR $2; 
	ivast		ALIAS FOR $3; 
	descto		ALIAS FOR $4; 
	iibb		ALIAS FOR $5; 
	alic		ALIAS FOR $6; 
	registro	record;
	regis		record;
	codemp		integer;
	idptov		integer;
	comprob 	integer;
	numero		integer;
	fecval		date;
	total		numeric;
	otros		numeric;
	ctadeud		integer;
	tipct		integer;
	auximp		numeric(15,2);
	auxiva		numeric(15,2);
	difiva		numeric(15,2);
	iibbp		numeric(15,2);
	ivap		numeric(15,2);
	elcli		integer;
	signo		integer;
	grupo		integer;
	ctaiva		integer;
	ctaivast	integer;
	ctadescto	integer;
	ctaiibb		integer;
	codcompr	character varying;
    BEGIN

delete from astoctas where astc_idastoh=idastoh;
select ash_codemp,ash_idptov,ash_comprob,ash_numero,ash_fecval into codemp,idptov,comprob,numero,fecval from astoh where ash_id=idastoh;

select idcbl(codemp, valvaramb(codemp,1,'DEUDXV')::smallint) into ctadeud;
if ctadeud is null then
    select idcbl(codemp, valvaramb(codemp,idptov,'DEUDXV')::smallint) into ctadeud;
end if;
select tipctacbl(ctadeud) into tipct;

select idcbl(codemp, valvaramb(codemp,1,'CTAIVA')::smallint) into ctaiva;
if ctaiva is null then
    select idcbl(codemp, valvaramb(codemp,idptov,'CTAIVA')::smallint) into ctaiva;
end if;

select emi_codcli,emi_grupo into elcli,grupo from emiti where emi_idastoh=idastoh;
select trim(codcomp(ash_comprob)) into codcompr from astoh where ash_id=idastoh;

if ((codcompr is not distinct from 'CA') or (codcompr is not distinct from 'CB') or (codcompr is not distinct from 'CC')) then
	signo= -1; 
    else
	signo= 1; 
end if;



total:=0;
for registro in select a.art_idcbl as cuenta, b.movs_tasa as latasa, tipctacbl(a.art_idcbl) as cualtipcta, sum(b.movs_importe) as cuanto 
     from movsto b, articvta a where b.movs_idastoh=idastoh 
        and b.movs_artic=a.art_id group by 1,2,3
	loop
	    if (bpchareq(codcompr,'FB') or bpchareq(codcompr,'CB') or bpchareq(codcompr,'DB')) then
		auximp:=registro.cuanto / (1 + registro.latasa/100);
	    else
		auximp:=registro.cuanto;
	    end if;
	    if (auximp<0) then auximp:=auximp*-1; end if;

	    insert into astoctas (astc_tasa,astc_ctacble,astc_importe,astc_idastoh,astc_estado,astc_codemp,astc_idptov,astc_comprob,astc_numero,astc_fecval) 
		    values (registro.latasa,registro.cuenta,(auximp * -1 * signo)::numeric,idastoh,-1::int2,codemp,idptov,comprob,numero,fecval::date);
	    total:=total + (auximp * signo);
	end loop; 


for regis in select movs_tasa as vlatasa, sum(movs_importe) as vcuanto
     from movsto where movs_idastoh=idastoh group by 1
	loop
	    if (bpchareq(codcompr,'FB') or bpchareq(codcompr,'CB') or bpchareq(codcompr,'DB')) then
		auximp:=regis.vcuanto / (1 + regis.vlatasa/100);
	    else
		auximp:=regis.vcuanto;
	    end if;

	    if (auximp<0) then auximp:=auximp*-1; end if;

	    if numeric_ne(regis.vlatasa,0::numeric) then 
		insert into astoctas (astc_tasa,astc_ctacble,astc_importe,astc_idastoh,astc_estado,astc_codemp,astc_idptov,astc_comprob,astc_numero,astc_fecval) 
		    values (regis.vlatasa,ctaiva,(auximp * -1 * signo * regis.vlatasa / 100)::numeric(15,2),idastoh,-1::int2,codemp,idptov,comprob,numero,fecval::date);
	    end if;
	end loop; 

select sum(astc_importe) into auxiva from astoctas where astc_idastoh=idastoh and astc_ctacble=ctaiva;

if (iva<0) then ivap:=iva*-1; else ivap:=iva; end if;

if (numeric_ne(ivap,auxiva)) then
    difiva:=ivap-auxiva;
    if numeric_le(difiva,0) then
	difiva:=difiva*-1;
    end if;
    update astoctas set astc_importe=(ivap*-1*signo) where astc_idastoh=idastoh and astc_ctacble=ctaiva;
end if;

if numeric_ne(ivap,0::numeric) then
    total:=total + (ivap * signo);
end if;

if numeric_ne(ivast,0::numeric) then
    select idcbl(codemp, valvaramb(codemp,1,'CTAIVAST')::smallint) into ctaivast;
    if ctaivast is null then
	select idcbl(codemp, valvaramb(codemp,idptov,'CTAIVAST')::smallint) into ctaivast;
    end if;

    insert into astoctas (astc_ctacble,astc_importe,astc_idastoh,astc_estado,astc_codemp,astc_idptov,astc_comprob,astc_numero,astc_fecval) 
	values (ctaivast,(ivast * -1 * signo)::numeric,idastoh,-1::int2,codemp,idptov,comprob,numero,fecval::date);

    total:=total + (ivast * signo);
end if;

if numeric_ne(descto,0::numeric) then
    select idcbl(codemp, valvaramb(codemp,1,'CTADESCTO')::smallint) into ctadescto;
    if ctadescto is null then
	select idcbl(codemp, valvaramb(codemp,idptov,'CTADESCTO')::smallint) into ctadescto;
    end if;
    insert into astoctas (astc_ctacble,astc_importe,astc_idastoh,astc_estado,astc_codemp,astc_idptov,astc_comprob,astc_numero,astc_fecval) 
	values (ctadescto,(descto * -1 * signo)::numeric,idastoh,-1::int2,codemp,idptov,comprob,numero,fecval::date);
    total:=total + (descto * signo);
end if;

if numeric_ne(iibb,0::numeric) then
    if(iibb<0) then iibbp:=iibb*-1; else iibbp:=iibb; end if;
    select idcbl(codemp,valvaramb(codemp,1,'CTAPERIBD')::smallint) into ctaiibb;
    if ctaiibb is null then
	select idcbl(codemp, valvaramb(codemp,idptov,'CTAPERIBD')::smallint) into ctaiibb;
    end if;
    insert into astoctas (astc_tasa,astc_ctacble,astc_importe,astc_idastoh,astc_estado,astc_codemp,astc_idptov,astc_comprob,astc_numero,astc_fecval) 
	values (alic,ctaiibb,(iibbp * -1 * signo)::numeric,idastoh,-1::int2,codemp,idptov,comprob,numero,fecval::date);
    total:=total + (iibbp * signo);
end if;


insert into astoctas (astc_grupo,astc_ctacte,astc_tipcta,astc_ctacble,astc_importe,astc_idastoh,astc_estado,astc_codemp,astc_idptov,astc_comprob,astc_numero,astc_fecval) 
	values (grupo,elcli,tipct,ctadeud,total::numeric,idastoh,-1::int2,codemp,idptov,comprob,numero,fecval::date);

update emiti set emi_total=total where emi_idastoh=idastoh;

RETURN signo;
END;$_$;


ALTER FUNCTION public.graboastoctan(integer, numeric, numeric, numeric, numeric, numeric) OWNER TO estlm;

--
-- Name: graboastoctan(integer, numeric, numeric, numeric, numeric, numeric, numeric); Type: FUNCTION; Schema: public; Owner: zona
--

CREATE FUNCTION public.graboastoctan(integer, numeric, numeric, numeric, numeric, numeric, numeric) RETURNS integer
    LANGUAGE plpgsql
    AS $_$
    DECLARE
	idastoh		ALIAS FOR $1; 
	iva		ALIAS FOR $2; 
	ivast		ALIAS FOR $3; 
	descto		ALIAS FOR $4; 
	iibb		ALIAS FOR $5; 
	alic		ALIAS FOR $6; 
	periva		ALIAS FOR $7;
	registro	record;
	regis		record;
	codemp		integer;
	idptov		integer;
	comprob 	integer;
	numero		integer;
	fecval		date;
	total		numeric;
	otros		numeric;
	ctadeud		integer;
	tipct		integer;
	auximp		numeric(15,2);
	auxiva		numeric(15,2);
	difiva		numeric(15,2);
	iibbp		numeric(15,2);
	perivap		numeric(15,2);
	ivap		numeric(15,2);
	elcli		integer;
	signo		integer;
	grupo		integer;
	ctaiva		integer;
	ctapiva		integer;
	ctaivast	integer;
	ctadescto	integer;
	ctaiibb		integer;
	codcompr	character varying;
    BEGIN

delete from astoctas where astc_idastoh=idastoh;
select ash_codemp,ash_idptov,ash_comprob,ash_numero,ash_fecval into codemp,idptov,comprob,numero,fecval from astoh where ash_id=idastoh;

select idcbl(codemp, valvaramb(codemp,1,'DEUDXV')::smallint) into ctadeud;
if ctadeud is null then
    select idcbl(codemp, valvaramb(codemp,idptov,'DEUDXV')::smallint) into ctadeud;
end if;
select tipctacbl(ctadeud) into tipct;

select idcbl(codemp, valvaramb(codemp,1,'CTAIVA')::smallint) into ctaiva;
if ctaiva is null then
    select idcbl(codemp, valvaramb(codemp,idptov,'CTAIVA')::smallint) into ctaiva;
end if;

select emi_codcli,emi_grupo into elcli,grupo from emiti where emi_idastoh=idastoh;
select trim(codcomp(ash_comprob)) into codcompr from astoh where ash_id=idastoh;

if ((codcompr is not distinct from 'CA') or (codcompr is not distinct from 'CB') or (codcompr is not distinct from 'CC')) then
	signo= -1; 
    else
	signo= 1; 
end if;



total:=0;
for registro in select a.art_idcbl as cuenta, b.movs_tasa as latasa, tipctacbl(a.art_idcbl) as cualtipcta, sum(b.movs_importe) as cuanto 
     from movsto b, articvta a where b.movs_idastoh=idastoh 
        and b.movs_artic=a.art_id group by 1,2,3
	loop
	    if (bpchareq(codcompr,'FB') or bpchareq(codcompr,'CB') or bpchareq(codcompr,'DB')) then
		auximp:=registro.cuanto / (1 + registro.latasa/100);
	    else
		auximp:=registro.cuanto;
	    end if;
	    if (auximp<0) then auximp:=auximp*-1; end if;

	    insert into astoctas (astc_tasa,astc_ctacble,astc_importe,astc_idastoh,astc_estado,astc_codemp,astc_idptov,astc_comprob,astc_numero,astc_fecval) 
		    values (registro.latasa,registro.cuenta,(auximp * -1 * signo)::numeric,idastoh,-1::int2,codemp,idptov,comprob,numero,fecval::date);
	    total:=total + (auximp * signo);
	end loop; 


for regis in select movs_tasa as vlatasa, sum(movs_importe) as vcuanto
     from movsto where movs_idastoh=idastoh group by 1
	loop
	    if (bpchareq(codcompr,'FB') or bpchareq(codcompr,'CB') or bpchareq(codcompr,'DB')) then
		auximp:=regis.vcuanto / (1 + regis.vlatasa/100);
	    else
		auximp:=regis.vcuanto;
	    end if;

	    if (auximp<0) then auximp:=auximp*-1; end if;

	    if numeric_ne(regis.vlatasa,0::numeric) then 
		insert into astoctas (astc_tasa,astc_ctacble,astc_importe,astc_idastoh,astc_estado,astc_codemp,astc_idptov,astc_comprob,astc_numero,astc_fecval) 
		    values (regis.vlatasa,ctaiva,(auximp * -1 * signo * regis.vlatasa / 100)::numeric(15,2),idastoh,-1::int2,codemp,idptov,comprob,numero,fecval::date);
	    end if;
	end loop; 

select sum(astc_importe) into auxiva from astoctas where astc_idastoh=idastoh and astc_ctacble=ctaiva;

if (iva<0) then ivap:=iva*-1; else ivap:=iva; end if;

if (numeric_ne(ivap,auxiva)) then
    difiva:=ivap-auxiva;
    if numeric_le(difiva,0) then
	difiva:=difiva*-1;
    end if;
    update astoctas set astc_importe=(ivap*-1*signo) where astc_idastoh=idastoh and astc_ctacble=ctaiva;
end if;

if numeric_ne(ivap,0::numeric) then
    total:=total + (ivap * signo);
end if;

if numeric_ne(ivast,0::numeric) then
    select idcbl(codemp, valvaramb(codemp,1,'CTAIVAST')::smallint) into ctaivast;
    if ctaivast is null then
	select idcbl(codemp, valvaramb(codemp,idptov,'CTAIVAST')::smallint) into ctaivast;
    end if;

    insert into astoctas (astc_ctacble,astc_importe,astc_idastoh,astc_estado,astc_codemp,astc_idptov,astc_comprob,astc_numero,astc_fecval) 
	values (ctaivast,(ivast * -1 * signo)::numeric,idastoh,-1::int2,codemp,idptov,comprob,numero,fecval::date);

    total:=total + (ivast * signo);
end if;

if numeric_ne(descto,0::numeric) then
    select idcbl(codemp, valvaramb(codemp,1,'CTADESCTO')::smallint) into ctadescto;
    if ctadescto is null then
	select idcbl(codemp, valvaramb(codemp,idptov,'CTADESCTO')::smallint) into ctadescto;
    end if;
    insert into astoctas (astc_ctacble,astc_importe,astc_idastoh,astc_estado,astc_codemp,astc_idptov,astc_comprob,astc_numero,astc_fecval) 
	values (ctadescto,(descto * -1 * signo)::numeric,idastoh,-1::int2,codemp,idptov,comprob,numero,fecval::date);
    total:=total + (descto * signo);
end if;

if numeric_ne(iibb,0::numeric) then
    if(iibb<0) then iibbp:=iibb*-1; else iibbp:=iibb; end if;
    select idcbl(codemp,valvaramb(codemp,1,'CTAPERIBD')::smallint) into ctaiibb;
    if ctaiibb is null then
	select idcbl(codemp, valvaramb(codemp,idptov,'CTAPERIBD')::smallint) into ctaiibb;
    end if;
    insert into astoctas (astc_tasa,astc_ctacble,astc_importe,astc_idastoh,astc_estado,astc_codemp,astc_idptov,astc_comprob,astc_numero,astc_fecval) 
	values (alic,ctaiibb,(iibbp * -1 * signo)::numeric,idastoh,-1::int2,codemp,idptov,comprob,numero,fecval::date);
    total:=total + (iibbp * signo);
end if;


if numeric_ne(periva,0::numeric) then
    if(periva<0) then perivap:=periva*-1; else perivap:=periva; end if;
    select idcbl(codemp,valvaramb(codemp,1,'PERIVADEP')::smallint) into ctapiva;
    if ctapiva is null then
	select idcbl(codemp, valvaramb(codemp,idptov,'PERIVADEP')::smallint) into ctapiva;
    end if;
    insert into astoctas (astc_tasa,astc_ctacble,astc_importe,astc_idastoh,astc_estado,astc_codemp,astc_idptov,astc_comprob,astc_numero,astc_fecval) 
	values (3,ctapiva,(perivap * -1 * signo)::numeric,idastoh,-1::int2,codemp,idptov,comprob,numero,fecval::date);
    total:=total + (perivap * signo);
end if;


insert into astoctas (astc_grupo,astc_ctacte,astc_tipcta,astc_ctacble,astc_importe,astc_idastoh,astc_estado,astc_codemp,astc_idptov,astc_comprob,astc_numero,astc_fecval) 
	values (grupo,elcli,tipct,ctadeud,total::numeric,idastoh,-1::int2,codemp,idptov,comprob,numero,fecval::date);

update emiti set emi_total=total where emi_idastoh=idastoh;

RETURN signo;
END;$_$;


ALTER FUNCTION public.graboastoctan(integer, numeric, numeric, numeric, numeric, numeric, numeric) OWNER TO zona;

--
-- Name: graboastoctasi(integer); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.graboastoctasi(integer) RETURNS integer
    LANGUAGE plpgsql
    AS $_$
    DECLARE
	idastoh		ALIAS FOR $1;
	registro	record;
	regis		record;
	codemp		integer;
	idptov		integer;
	comprob 	integer;
	succli		integer;
	numero		integer;
	fecval		date;
	total		numeric;
	iva		numeric;
	ivap		numeric;
	otros		numeric;
	ctadeud		integer;
	ctaiva		integer;
	tipct		integer;
	tctdepg		integer;
	elcli		integer;
	signo		integer;
	grupo		integer;
	codcompr	character varying;
	retval 		integer;
    BEGIN

select ash_codemp,ash_idptov,ash_comprob,ash_numero,ash_fecval into codemp,idptov,comprob,numero,fecval from astoh where ash_id=idastoh;
select idcble(codemp, valvaramb(codemp,idptov,'DEUDXV')) into ctadeud;
select idcble(codemp, valvaramb(codemp,idptov,'CTAIVA')) into ctaiva;
-- select valvaramb(codemp,1,'TIPCTADEPG')::integer into tctdepg;
select tipctacbl(ctadeud) into tipct;
select emi_codcli,emi_grupo,emi_succli into elcli,grupo,succli from emiti where emi_idastoh=idastoh;
select codcompash(idastoh) into codcompr;

if (bpchareq(codcompr,'CA') or bpchareq(codcompr,'CB') or bpchareq(codcompr,'CC')) then
	signo= 1;
    else
	signo= 1;
end if;

total:=0;
iva:=0;
for registro in select a.art_codcbl as cuenta, b.movs_tasa as latasa, sum(b.movs_importe) as cuanto 
     from movsto b, articvta a where b.movs_idastoh=idastoh
        and b.movs_artic=a.art_id group by 1,2
	loop
	    insert into astoctas (astc_tasa,astc_ctacble,astc_importe,astc_idastoh,astc_estado,astc_codemp,astc_idptov,astc_comprob,astc_numero,astc_fecval) 
		    values (registro.latasa,registro.cuenta,(registro.cuanto * -1 * signo)::numeric,idastoh,-1::int2,codemp,idptov,comprob,numero,fecval);
	    total:=total + (registro.cuanto * signo);
	end loop; 


for regis in select b.movs_tasa as latasa, sum(b.movs_importe) as cuanto 
     from movsto b, articvta a where b.movs_idastoh=idastoh and bpcharne(codcompash(movs_idastoh),'RE') and b.movs_artic=a.art_id group by 1
	loop
	    select (regis.cuanto * regis.latasa / 100 * signo)::numeric(15,2) into ivap;
	    insert into astoctas (astc_tasa,astc_ctacble,astc_importe,astc_idastoh,astc_estado,astc_codemp,astc_idptov,astc_comprob,astc_numero,astc_fecval) 
		    values (regis.latasa,ctaiva,(ivap * -1),idastoh,-1::int2,codemp,idptov,comprob,numero,fecval);

	    iva:=iva + ivap ;
	end loop; 


if numeric_ne(iva,0) then
    total:=total + iva;
end if;

insert into astoctas (astc_succli,astc_grupo,astc_ctacte,astc_tipcta,astc_ctacble,astc_importe,astc_idastoh,astc_estado,astc_codemp,astc_idptov,astc_comprob,astc_numero,astc_fecval) 
	values (succli,grupo,elcli,tipct,ctadeud,total,idastoh,-1::int2,codemp,idptov,comprob,numero,fecval);

update emiti set emi_total=total where emi_idastoh=idastoh;

retval:=signo;

RETURN retval;
END;$_$;


ALTER FUNCTION public.graboastoctasi(integer) OWNER TO rulemanes;

--
-- Name: graboastoctasi(integer, numeric); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.graboastoctasi(integer, numeric) RETURNS integer
    LANGUAGE plpgsql
    AS $_$
    DECLARE
	idastoh		ALIAS FOR $1;
	alicuota	ALIAS FOR $2;
	registro	record;
	regis		record;
	codemp		integer;
	idptov		integer;
	comprob 	integer;
	succli		integer;
	coniva		integer;
	numero		integer;
	fecval		date;
	total		numeric;
	iva			numeric;
	ivap		numeric;
	otros		numeric;
	impaux		numeric;
	bimp	 	numeric;
	miniibb	 	numeric;
	minpiva	 	numeric;
	baux	 	numeric;
	alic	 	numeric;
	totalic	 	numeric;
	totpiva	 	numeric;
	ctadeud		integer;
	ctaiva		integer;
	cliperiva	integer;
	tipct		integer;
	ctaiibb		integer;
	ctaivap		integer;
	tctdepg		integer;
	elcli		integer;
	signo		integer;
	grupo		integer;
	codcompr	character varying;
	retval 		integer;
    BEGIN

select ash_codemp,ash_idptov,ash_comprob,ash_numero,ash_fecval into codemp,idptov,comprob,numero,fecval from astoh where ash_id=idastoh;
select idcble(codemp, valvaramb(codemp,idptov,'DEUDXV')) into ctadeud;
select idcble(codemp, valvaramb(codemp,idptov,'CTAIVA')) into ctaiva;
select valvaramb(codemp,idptov,'MINIIBB') into miniibb;
select valvaramb(codemp,idptov,'MINIVA') into minpiva;
select tipctacbl(ctadeud) into tipct;
select emi_codcli,emi_grupo,emi_succli,emi_conivac into elcli,grupo,succli,coniva from emiti where emi_idastoh=idastoh;
select cli_perciva into cliperiva from clientes where cli_id=elcli;
select codcompash(idastoh) into codcompr;

total:=0;
iva:=0;
for registro in select a.art_codcbl as cuenta, b.movs_tasa as latasa, sum(b.movs_importe) as cuanto 
     from movsto b, articvta a where b.movs_idastoh=idastoh
        and b.movs_artic=a.art_id group by 1,2
	loop
	    if (bpchareq(codcompr,'FB') or bpchareq(codcompr,'CB') or bpchareq(codcompr,'DB')) then
		impaux:=(registro.cuanto / (1 + (registro.latasa / 100)))::numeric(15,2);
	    else
		impaux:=registro.cuanto::numeric(15,2);
	    end if;
	    insert into astoctas (astc_tasa,astc_ctacble,astc_importe,astc_idastoh,astc_estado,astc_codemp,astc_idptov,astc_comprob,astc_numero,astc_fecval) 
		    values (registro.latasa,registro.cuenta,(impaux * -1)::numeric,idastoh,-1::int2,codemp,idptov,comprob,numero,fecval);
	    total:=total + (impaux);
	end loop; 


for regis in select b.movs_tasa as latasa, sum(b.movs_importe) as cuanto 
     from movsto b, articvta a where b.movs_idastoh=idastoh and bpcharne(codcompash(movs_idastoh),'RE') and b.movs_artic=a.art_id group by 1
	loop
	    if (bpchareq(codcompr,'FB') or bpchareq(codcompr,'CB') or bpchareq(codcompr,'DB')) then
		select (regis.cuanto - (regis.cuanto / (1 + (regis.latasa / 100))))::numeric(15,2) into ivap;
	    else
		select (regis.cuanto * regis.latasa / 100)::numeric(15,2) into ivap;
	    end if;
	    insert into astoctas (astc_tasa,astc_ctacble,astc_importe,astc_idastoh,astc_estado,astc_codemp,astc_idptov,astc_comprob,astc_numero,astc_fecval) 
		    values (regis.latasa,ctaiva,(ivap * -1),idastoh,-1::int2,codemp,idptov,comprob,numero,fecval);

	    iva:=iva + ivap ;
	end loop; 

bimp:=total;

if numeric_ne(iva,0) then
    total:=total + iva;
end if;

if (bpchareq(codcompr,'FB') or bpchareq(codcompr,'CB') or bpchareq(codcompr,'DB')) then
    bimp:=total;
end if;

baux:=bimp;
if (numeric_le(baux,0)) then
    baux:=baux*-1;
end if;

if (numeric_gt(alicuota,0) and numeric_gt(baux,miniibb)) then
    alic:= alicuota / 100;
    totalic:= (bimp * alic)::numeric(15,2);
    if (numeric_ne(totalic,0)) then
	select idcbl(codemp,valvaramb(codemp,1,'CTAPERIBD')::smallint) into ctaiibb;
	if ctaiibb is null then
	    select idcbl(codemp, valvaramb(codemp,idptov,'CTAPERIBD')::smallint) into ctaiibb;
	end if;
	insert into astoctas (astc_tasa,astc_ctacble,astc_importe,astc_idastoh,astc_estado,astc_codemp,astc_idptov,astc_comprob,astc_numero,astc_fecval) 
	    values (alicuota,ctaiibb,(totalic * -1)::numeric,idastoh,-1::int2,codemp,idptov,comprob,numero,fecval::date);
	total:=total + totalic;
    end if;
end if;

if (int4eq(coniva,1::int4) and numeric_ge(baux,minpiva) and int4eq(cliperiva,1)) THEN
	if (bpchareq(codcompr,'FA') or bpchareq(codcompr,'CA')) then
		totpiva:= (bimp * 0.03)::numeric(15,2);

		select idcbl(codemp,valvaramb(codemp,1,'PERIVADEP')::smallint) into ctaivap;
		if ctaivap is null then
	    	select idcbl(codemp, valvaramb(codemp,idptov,'PERIVADEP')::smallint) into ctaivap;
		end if;

		insert into astoctas (astc_tasa,astc_ctacble,astc_importe,astc_idastoh,astc_estado,astc_codemp,astc_idptov,astc_comprob,astc_numero,astc_fecval) 
	    	values (3,ctaivap,(totpiva * -1)::numeric,idastoh,-1::int2,codemp,idptov,comprob,numero,fecval::date);
		total:=total + totpiva;
    end if;

end if;

insert into astoctas (astc_succli,astc_grupo,astc_ctacte,astc_tipcta,astc_ctacble,astc_importe,astc_idastoh,astc_estado,astc_codemp,astc_idptov,astc_comprob,astc_numero,astc_fecval) 
	values (succli,grupo,elcli,tipct,ctadeud,total,idastoh,-1::int2,codemp,idptov,comprob,numero,fecval);

update emiti set emi_total=total where emi_idastoh=idastoh;

retval:=signo;

RETURN retval;
END;$_$;


ALTER FUNCTION public.graboastoctasi(integer, numeric) OWNER TO rulemanes;

--
-- Name: graboauxfe(integer); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.graboauxfe(integer) RETURNS void
    LANGUAGE plpgsql
    AS $_$
    DECLARE
	idastoh		ALIAS FOR $1;
	idemiti		integer;
	idfe		integer;
	codemp 		integer;
	ptovta 		integer;
	elcompbte	integer;
	idcli 		integer;
	nrcuit 		character varying;
	cdpag 		integer;
	diasconpag	integer;
	compbte		integer;
	numero 		integer;
	mes	 	integer;
	anio	 	integer;
	ctaiibb	 	integer;
	fecval	 	date;
	ngrav	 	numeric(15,2);
	grav	 	numeric(15,2);
	ivatot	 	numeric(15,2);
	total	 	numeric(15,2);
	ivaex	 	numeric(15,2);
	iva21	 	numeric(15,2);
	auxiva	 	numeric(15,2);
	iva105	 	numeric(15,2);
	tributo	 	numeric(15,2);
	fechaf	 	character varying;
	inserv	 	character varying;
	finserv	 	character varying;
	fecvto	 	character varying;
	regis		record;
	registro	record;
	comporig	character varying;
    BEGIN

	select ash_codemp,ash_idptov,emi_codcli,trim(codcomp(ash_comprob)),ash_fecval,emi_id,emi_conpag,emi_total into codemp,ptovta,idcli,comporig,fecval,idemiti,cdpag,total from astoh,emiti where ash_id=idastoh and emi_idastoh=ash_id;

	select idcbl(codemp,valvaramb(codemp,1,'CTAPERIBD')::smallint) into ctaiibb;
	if ctaiibb is null then
	    select idcbl(codemp, valvaramb(codemp,ptovta,'CTAPERIBD')::smallint) into ctaiibb;
	end if;

	select (astc_importe*-1) into tributo from astoctas where astc_idastoh=idastoh and astc_ctacble=ctaiibb;
	if not found then
	    tributo:=0;
	end if;

	select fecha_fe(fecval) into fechaf;
	select date_part('month',fecval) into mes;
	select date_part('year',fecval) into anio;
	inserv:='01'||'-'||mes::text||'-'||anio::text;
	select fecha_fe((date(inserv::date + '1 month'::interval)-1)::date) into finserv;
	select fecha_fe(inserv::date) into inserv;
	select cuit_fe(cuitcli(idcli)) into nrcuit;
	select cdp_dias into diasconpag from conpag where cdp_codigo=cdpag;
	select fecha_fe(date(fecval::date + diasconpag)) into fecvto;
	select compbte_fe(comporig) into compbte;

	grav:=0;
	ivatot:=0;
	ngrav:=0;
	ivaex:=0;
	iva21:=0;
	iva105:=0;
	for regis in select movs_importe,movs_tasa from movsto where movs_idastoh=idastoh 
	    loop
		if (numeric_eq(regis.movs_tasa,(21)::numeric)) then
		    grav:=grav+regis.movs_importe;
		    auxiva:=regis.movs_importe*0.21;
		    iva21:=iva21+auxiva;
		    ivatot:=ivatot+auxiva;
		else
		    if (numeric_eq(regis.movs_tasa,(10.5)::numeric)) then
			grav:=grav+regis.movs_importe;
			auxiva:=regis.movs_importe*0.105;
			iva105:=iva105+auxiva;
			ivatot:=ivatot+auxiva;
		    else
			ngrav:=ngrav+regis.movs_importe;
		    end if;
		end if;
	    end loop;

	insert into auxfe (fe_idcli,fe_cuit,fe_cbte,fe_codemp,fe_ptovta,fe_fechaf,fe_iniserv,fe_finserv,fe_fechavp,fe_nograv,fe_grav,fe_ivatot,fe_total,fe_tributo,fe_ivaex,fe_iva21,fe_iva105) values 
			    (idcli,nrcuit,compbte,codemp,ptovta,fechaf,inserv,finserv,fecvto,ngrav,grav,ivatot,total,tributo,ivaex,iva21,iva105);
	select fe_id into idfe from auxfe where fe_idcli=idcli and fe_fechaf=fechaf and fe_grav=grav and fe_total=total and fe_iva21=iva21 order by 1 desc limit 1;

	update emiti set emi_idfe=idfe where emi_id=idemiti;
    RETURN;
    END;$_$;


ALTER FUNCTION public.graboauxfe(integer) OWNER TO rulemanes;

--
-- Name: graboauxfe(integer, numeric); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.graboauxfe(integer, numeric) RETURNS void
    LANGUAGE plpgsql
    AS $_$
    DECLARE
	idastoh		ALIAS FOR $1;
	alic		ALIAS FOR $2;
	idemiti		integer;
	idfe		integer;
	codemp 		integer;
	ptovta 		integer;
	elcompbte	integer;
	idcli 		integer;
	nrcuit 		character varying;
	cdpag 		integer;
	diasconpag	integer;
	compbte		integer;
	numero 		integer;
	mes	 		integer;
	anio	 	integer;
	ctaiibb	 	integer;
	ctaivap		integer;
	fecval	 	date;
	ngrav	 	numeric(15,2);
	grav	 	numeric(15,2);
	ivatot	 	numeric(15,5);
	total	 	numeric(15,2);
	ivaex	 	numeric(15,2);
	iva21	 	numeric(15,5);
	auxiva	 	numeric(15,2);
	iva105	 	numeric(15,2);
	bimp	 	numeric(15,2);
	tributo	 	numeric(15,2);
	tributo2 	numeric(15,2);
	auximp	 	numeric(15,2);
	auxtot	 	numeric(15,2);
	dif	 		numeric(15,2);
	fechaf	 	character varying;
	inserv	 	character varying;
	finserv	 	character varying;
	fecvto	 	character varying;
	regis		record;
	registro	record;
	comporig	character varying;
    BEGIN

	select ash_codemp,ash_idptov,emi_codcli,trim(codcomp(ash_comprob)),ash_fecval,emi_id,emi_conpag,emi_total into codemp,ptovta,idcli,comporig,fecval,idemiti,cdpag,total from astoh,emiti where ash_id=idastoh and emi_idastoh=ash_id;

	select idcbl(codemp,valvaramb(codemp,1,'CTAPERIBD')::smallint) into ctaiibb;
	if ctaiibb is null then
	    select idcbl(codemp, valvaramb(codemp,ptovta,'CTAPERIBD')::smallint) into ctaiibb;
	end if;

	select (astc_importe*-1) into tributo from astoctas where astc_idastoh=idastoh and astc_ctacble=ctaiibb;
	if not found then
	    tributo:=0;
	end if;
	
	select idcbl(codemp,valvaramb(codemp,1,'PERIVADEP')::smallint) into ctaivap;
	if ctaiibb is null then
	    select idcbl(codemp, valvaramb(codemp,ptovta,'PERIVADEP')::smallint) into ctaivap;
	end if;
	
	select (astc_importe*-1) into tributo2 from astoctas where astc_idastoh=idastoh and astc_ctacble=ctaivap;
	if not found then
	    tributo2:=0;
	end if;

	select fecha_fe(fecval) into fechaf;
	select date_part('month',fecval) into mes;
	select date_part('year',fecval) into anio;
	inserv:='01'||'-'||mes::text||'-'||anio::text;
	select fecha_fe((date(inserv::date + '1 month'::interval)-1)::date) into finserv;
	select fecha_fe(inserv::date) into inserv;
	select cuit_fe(cuitcli(idcli)) into nrcuit;
	select cdp_dias into diasconpag from conpag where cdp_codigo=cdpag;
	select fecha_fe(date(fecval::date + diasconpag)) into fecvto;
	select compbte_fe(comporig) into compbte;

	grav:=0;
	ivatot:=0;
	ngrav:=0;
	ivaex:=0;
	iva21:=0;
	iva105:=0;
	for regis in select movs_tasa,sum(movs_importe) as importe from movsto where movs_idastoh=idastoh group by 1 
	    loop
		if (numeric_eq(regis.movs_tasa,(21)::numeric)) then
		    if (bpchareq(comporig,'FB') or bpchareq(comporig,'CB') or bpchareq(comporig,'DB')) then
				auximp:=regis.importe / 1.21;
		    else
				auximp:=regis.importe;
		    end if;
		    grav:=grav+auximp;
		    auxiva:=auximp*0.21;
		    iva21:=iva21+auxiva;
		    ivatot:=ivatot+auxiva;
		else
		    if (numeric_eq(regis.movs_tasa,(10.5)::numeric)) then
				if (bpchareq(comporig,'FB') or bpchareq(comporig,'CB') or bpchareq(comporig,'DB')) then
			   		auximp:=regis.importe / 1.105;
				else
			    	auximp:=regis.importe;
				end if;
				grav:=grav+auximp;
				auxiva:=auximp*0.105;
				iva105:=iva105+auxiva;
				ivatot:=ivatot+auxiva;
		    else
				ngrav:=ngrav+regis.importe;
		    end if;
		end if;
	    end loop;

	auxtot:=grav+iva21+iva105+tributo+tributo2;
	if (numeric_ne(auxtot,total)) then
	    dif:=auxtot-total;
	    if(numeric_le(dif,0))then
			dif:=dif*-1;
			iva21:=iva21+dif;
			ivatot:=ivatot+dif;
	    else
			iva21:=iva21-dif;
			ivatot:=ivatot-dif;
	    end if;
	end if;

	insert into auxfe (fe_idcli,fe_cuit,fe_cbte,fe_codemp,fe_ptovta,fe_fechaf,fe_iniserv,fe_finserv,fe_fechavp,fe_nograv,fe_grav,fe_ivatot,fe_total,fe_tributo,fe_ivaex,fe_iva21,fe_iva105) 	values 
			    (idcli,nrcuit,compbte,codemp,ptovta,fechaf,inserv,finserv,fecvto,ngrav,grav,ivatot::numeric(15,2),total,tributo+tributo2,ivaex,iva21::numeric(15,2),iva105);
	select fe_id into idfe from auxfe where fe_idcli=idcli and fe_fechaf=fechaf and fe_grav=grav and fe_total=total and fe_iva21=iva21 order by 1 desc limit 1;

    if (bpchareq(comporig,'FB') or bpchareq(comporig,'CB') or bpchareq(comporig,'DB')) then
		bimp:=total;
    else
		bimp:=grav;
    end if;

    if(numeric_ne(tributo,0)) then
		insert into tributosfe (t_idfe,t_cod,t_desc,t_baseimp,t_alic,t_imp) values (idfe, 2, 'Percep IIBB Pcia de Bs As', bimp, alic, tributo);
    end if;
    
    if(numeric_ne(tributo2,0)) then
		insert into tributosfe (t_idfe,t_cod,t_desc,t_baseimp,t_alic,t_imp) values (idfe, 1, 'Percepion IVA', bimp, 3, tributo2);
    end if;

    update emiti set emi_idfe=idfe where emi_id=idemiti;

    RETURN;
    END;$_$;


ALTER FUNCTION public.graboauxfe(integer, numeric) OWNER TO rulemanes;

--
-- Name: grbretib(integer, numeric, numeric); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.grbretib(integer, numeric, numeric) RETURNS numeric
    LANGUAGE plpgsql
    AS $_$
declare
idpago		 alias for $1;
alicuota	 alias for $2;
retfinal	 alias for $3;
neto		 numeric(15,2);
reten		 numeric(15,2);
deboreten	 numeric(15,2);
totpag		 numeric(15,2);
totfac		 numeric(15,2);
totaux		 numeric(15,2);
imppag		 numeric(15,2);
tasa		 numeric(15,2);
codemp		 integer;
idptov		 integer;
cta_retib	 integer;
cta_retibc	 integer;
cta_retiva	 integer;
cta_retiva1	 integer;
cta_iva		 integer;
codcomp		 character varying;
r		 record;
begin
    neto:=0;
    select sum(ag_importe) into totpag from agrupa where ag_idpago=idpago;

    select ash_codemp,ash_idptov into codemp,idptov from astoh where ash_id=idpago;
    select idcbl(codemp,valvaramb(codemp,idptov,'CTAPERIB')::integer) into cta_retib;
    select idcbl(codemp,valvaramb(codemp,idptov,'CTAPERIBC')::integer) into cta_retibc;
    select idcbl(codemp,valvaramb(codemp,idptov,'CTAPERIVA')::integer) into cta_retiva;
    select idcbl(codemp,valvaramb(codemp,idptov,'CTAPERIVA1')::integer) into cta_retiva1;
    select idcbl(codemp,valvaramb(codemp,idptov,'CTAIVAC')::integer) into cta_iva;
    for r in select * from agrupa where ag_idpago=idpago order by 1
	loop
	    select trim(codcompash(r.ag_idastoh)) into codcomp;
	    if codcomp is distinct from 'PP' and codcomp is distinct from 'OP' then
		select sum(astc_importe) into totaux from astoctas where astc_idastoh=r.ag_idastoh and astc_ctacble not in (cta_iva,cta_retib,cta_retibc,cta_retiva,cta_retiva1) and astc_tasa is not null;
		deboreten:=totaux * alicuota / 100;
		if(numeric_ne(r.ag_retenido,0)) then
		    deboreten:=deboreten - r.ag_retenido;
		end if;
		neto:=neto + deboreten;
	    end if;
	end loop;

    if(numeric_eq(retfinal,neto)) then
	for r in select * from agrupa where ag_idpago=idpago order by 1
	    loop
		select trim(codcompash(r.ag_idastoh)) into codcomp;
		if codcomp is distinct from 'PP' and codcomp is distinct from 'OP' then
		    select sum(astc_importe) into totaux from astoctas where astc_idastoh=r.ag_idastoh and astc_ctacble not in (cta_iva,cta_retib,cta_retibc,cta_retiva,cta_retiva1) and astc_tasa is not null;
		    deboreten:=totaux * alicuota / 100;
		    update agrupa set ag_retenido=deboreten where ag_id=r.ag_id;
		end if;
	    end loop;
    else
	imppag:=retfinal;
	for r in select * from agrupa where ag_idpago=idpago order by 1
	    loop
		select trim(codcompash(r.ag_idastoh)) into codcomp;
		if codcomp is distinct from 'PP' and codcomp is distinct from 'OP' then
		    select sum(astc_importe) into totaux from astoctas where astc_idastoh=r.ag_idastoh and astc_ctacble not in (cta_iva,cta_retib,cta_retibc,cta_retiva,cta_retiva1) and astc_tasa is not null;
		    deboreten:=totaux * alicuota / 100;
		    if(numeric_ne(r.ag_retenido,0)) then
			deboreten:=deboreten - r.ag_retenido;
		    end if;
		    if(numeric_gt(imppag,deboreten)) then
			if(numeric_ne(r.ag_retenido,0)) then
			    deboreten:=deboreten + r.ag_retenido;
			end if;
			update agrupa set ag_retenido=deboreten where ag_id=r.ag_id;
			imppag:=imppag - deboreten;
		    else
			if(numeric_gt(imppag,0)) then
			    if(numeric_ne(r.ag_retenido,0)) then
				imppag:=imppag + r.ag_retenido;
			    end if;
			    update agrupa set ag_retenido=imppag where ag_id=r.ag_id;
			    imppag:=0;
			end if;
		    end if;
		end if;
	    end loop;
    end if;

    return neto;
end;$_$;


ALTER FUNCTION public.grbretib(integer, numeric, numeric) OWNER TO rulemanes;

--
-- Name: grupocli(integer, integer); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.grupocli(integer, integer) RETURNS integer
    LANGUAGE plpgsql
    AS $_$
    DECLARE
	cliente		ALIAS FOR $1; 
	modo		ALIAS FOR $2; -- 1:graba  2:consulta
	otro 		integer;
	retval 		integer;
    BEGIN
    select cli_grupo into retval from
    	 clientes where int4eq(cliente,cli_id);

    if found then
	if int4eq(modo,1) then
	    update clientes set cli_grupo=cli_grupo+1 where int4eq(cliente,cli_id);
	end if;		
    end if;
    RETURN retval;
    END;$_$;


ALTER FUNCTION public.grupocli(integer, integer) OWNER TO rulemanes;

--
-- Name: grupoprov(integer, integer); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.grupoprov(integer, integer) RETURNS integer
    LANGUAGE plpgsql
    AS $_$
    DECLARE
	provd		ALIAS FOR $1; 
	modo		ALIAS FOR $2; -- 1:graba  2:consulta
	otro 		integer;
	retval 		integer;
    BEGIN
    select prov_grupo into retval from
    	 prove where int4eq(provd,prov_id);

    if found then
	if int4eq(modo,1) then
	    update prove set prov_grupo=prov_grupo+1 where int4eq(provd,prov_id);
	end if;		
    end if;
    RETURN retval;
    END;$_$;


ALTER FUNCTION public.grupoprov(integer, integer) OWNER TO rulemanes;

--
-- Name: habro_ejercicio(character varying, date, integer, integer); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.habro_ejercicio(character varying, date, integer, integer) RETURNS boolean
    LANGUAGE plpgsql
    AS $_$
declare
comprobante	 alias for $1;
fecha		 alias for $2;
codemp		 alias for $3;
idptov		 alias for $4;
ret 		 boolean;
registro	 record;
elcomprob 	 integer;
compcierre 	 integer;
cuentardo 	 integer;
nrocomp 	 integer;
cuenta 	 	 integer;
subcuenta 	 integer;
resultad 	 numeric(15,2);
tituloa	 	 character varying;
idastoh 	 integer;
idashcierre 	 integer;
begin
ret:='f';
    select idcomp(comprobante,idptov) into elcomprob;
    select idcomp('ZZ',idptov) into compcierre;
    select numtalon(idptov,elcomprob,1) into nrocomp;
    select comp_nombre || ' INICIADO EL ' || fecha into tituloa from comprob where comp_id=elcomprob;

    insert into astoh (ash_codemp,ash_idptov,ash_anio,ash_mes,ash_comprob,ash_numero,ash_titulo,ash_fecomp,ash_fecval,ash_leyen,ash_nro2,ash_estado) 
	values (codemp,idptov,date_part('year',fecha)::int2,date_part('month',fecha)::int2,elcomprob,nrocomp,tituloa,fecha,fecha,'Apertura de Cuentas del Ejercicio ',0,-1);

    select ash_id into idastoh from astoh where ash_codemp=codemp and ash_idptov=idptov and ash_comprob=elcomprob and ash_numero=nrocomp;

    select ash_id into idashcierre from astoh where ash_codemp=codemp and ash_idptov=idptov and ash_comprob=compcierre and date_le(ash_fecval,fecha) order by ash_id desc limit 1;

    for registro in select 
	    astc_ctacble, astc_tipcta, astc_ctacte, (astc_importe * -1) as cuanto from astoctas 
	      where astc_idastoh=idashcierre  
	    loop
		if(registro.cuanto != 0) then
		    insert into astoctas (astc_idastoh,astc_ctacble,astc_importe,astc_codemp,astc_idptov,astc_comprob,astc_numero,astc_fecval,astc_estado,astc_tipcta,astc_ctacte) 
    	    	     values (idastoh,registro.astc_ctacble,registro.cuanto,codemp,idptov,elcomprob,nrocomp,fecha,-1,registro.astc_tipcta,registro.astc_ctacte);	
		end if;
	ret:='t';
    end loop; 
    return ret;
end;$_$;


ALTER FUNCTION public.habro_ejercicio(character varying, date, integer, integer) OWNER TO rulemanes;

--
-- Name: idarticvta(integer, integer, character varying); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.idarticvta(integer, integer, character varying) RETURNS integer
    LANGUAGE sql
    AS $_$select art_id from articvta where art_codemp= $1 and art_idptov= $2 and bpchareq(trim( $3 ),art_codigo);$_$;


ALTER FUNCTION public.idarticvta(integer, integer, character varying) OWNER TO rulemanes;

--
-- Name: idashdecomp(integer, integer, integer, integer); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.idashdecomp(integer, integer, integer, integer) RETURNS integer
    LANGUAGE sql
    AS $_$select ash_id from astoh where ash_codemp= $1 and ash_idptov= $2 and ash_comprob= $3 and ash_numero= $4;$_$;


ALTER FUNCTION public.idashdecomp(integer, integer, integer, integer) OWNER TO rulemanes;

--
-- Name: idcbl(integer, integer); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.idcbl(integer, integer) RETURNS integer
    LANGUAGE sql
    AS $_$select cbl_id from ctacble where cbl_codemp in ( $1,0) and int4eq(cbl_codigo::integer, $2) order by cbl_codemp desc limit 1;$_$;


ALTER FUNCTION public.idcbl(integer, integer) OWNER TO rulemanes;

--
-- Name: idcble(integer, character varying); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.idcble(integer, character varying) RETURNS integer
    LANGUAGE sql
    AS $_$select cbl_id from ctacble where cbl_codemp= $1 and bpchareq(cbl_codigo, $2);$_$;


ALTER FUNCTION public.idcble(integer, character varying) OWNER TO rulemanes;

--
-- Name: idcli(integer, character varying); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.idcli(integer, character varying) RETURNS integer
    LANGUAGE sql
    AS $_$select cli_id from clientes where cli_codemp= $1 and bpchareq(cli_codigo, $2);$_$;


ALTER FUNCTION public.idcli(integer, character varying) OWNER TO rulemanes;

--
-- Name: idcli(integer, integer, character varying); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.idcli(integer, integer, character varying) RETURNS integer
    LANGUAGE sql
    AS $_$select cli_id from clientes where cli_codemp= $1 and cli_idptov= $2 and bpchareq(cli_codigo, $3);$_$;


ALTER FUNCTION public.idcli(integer, integer, character varying) OWNER TO rulemanes;

--
-- Name: idcomp(character varying, integer); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.idcomp(character varying, integer) RETURNS integer
    LANGUAGE sql
    AS $_$select comp_id from comprob where bpchareq(trim(comp_codigo),trim( $1 )) and comp_idptov in ( $2 ,0, -1) order by comp_idptov desc limit 1;$_$;


ALTER FUNCTION public.idcomp(character varying, integer) OWNER TO rulemanes;

--
-- Name: idcomp(character varying, integer, integer); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.idcomp(character varying, integer, integer) RETURNS integer
    LANGUAGE sql
    AS $_$select comp_id from comprob where bpchareq(trim(comp_codigo),trim( $1 )) and comp_idptov in ( $2 , -1) and comp_codemp= $3 order by comp_idptov desc limit 1;$_$;


ALTER FUNCTION public.idcomp(character varying, integer, integer) OWNER TO rulemanes;

--
-- Name: idctes(integer, integer); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.idctes(integer, integer) RETURNS integer
    LANGUAGE sql
    AS $_$select cte_id from ctes where cte_tipcta= $1 and cte_codigo= $2;$_$;


ALTER FUNCTION public.idctes(integer, integer) OWNER TO rulemanes;

--
-- Name: idemitiash(integer); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.idemitiash(integer) RETURNS integer
    LANGUAGE sql
    AS $_$select emi_id from emiti where emi_idastoh = $1 ;$_$;


ALTER FUNCTION public.idemitiash(integer) OWNER TO rulemanes;

--
-- Name: idprov(integer, character varying); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.idprov(integer, character varying) RETURNS integer
    LANGUAGE sql
    AS $_$select prov_id from prove where prov_codemp= $1 and bpchareq(prov_codigo, $2);$_$;


ALTER FUNCTION public.idprov(integer, character varying) OWNER TO rulemanes;

--
-- Name: idptov(integer, integer); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.idptov(integer, integer) RETURNS integer
    LANGUAGE sql
    AS $_$select ptv_id from ptovta where ptv_codemp= $1 and ptv_codigo= $2;$_$;


ALTER FUNCTION public.idptov(integer, integer) OWNER TO rulemanes;

--
-- Name: importe_fe(numeric); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.importe_fe(numeric) RETURNS text
    LANGUAGE plpgsql
    AS $_$
    DECLARE
	imp		ALIAS FOR $1;
	retval		text;
    BEGIN
	select lpad((imp::numeric(15,2)*100)::numeric(15,0)::text,15,'0') into retval;
	RETURN retval;
    END;$_$;


ALTER FUNCTION public.importe_fe(numeric) OWNER TO rulemanes;

--
-- Name: importedeleyen(integer, integer, integer, integer, character varying); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.importedeleyen(integer, integer, integer, integer, character varying) RETURNS numeric
    LANGUAGE sql
    AS $_$select sum(astc_importe) from astoctas where astc_estado=2 and astc_idptov=$1 and astc_ctacble=$2 and date_part('month',astc_fecval)=$3 and date_part('year',astc_fecval)=$4 and trim(astc_leyen) like trim($5);$_$;


ALTER FUNCTION public.importedeleyen(integer, integer, integer, integer, character varying) OWNER TO rulemanes;

--
-- Name: leyartic(integer); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.leyartic(integer) RETURNS character varying
    LANGUAGE sql
    AS $_$select art_leyen from articvta where art_id= $1 ;$_$;


ALTER FUNCTION public.leyartic(integer) OWNER TO rulemanes;

--
-- Name: llevastock(integer); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.llevastock(integer) RETURNS boolean
    LANGUAGE sql
    AS $_$select true from varambi where vari_codemp= $1 and vari_codigo='STOCK' and vari_valor='1' limit 1;$_$;


ALTER FUNCTION public.llevastock(integer) OWNER TO rulemanes;

--
-- Name: mesastoh(integer); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.mesastoh(integer) RETURNS smallint
    LANGUAGE sql
    AS $_$select ash_mes from astoh where ash_id= $1;$_$;


ALTER FUNCTION public.mesastoh(integer) OWNER TO rulemanes;

--
-- Name: monedacbl(integer); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.monedacbl(integer) RETURNS smallint
    LANGUAGE sql
    AS $_$select cbl_moneda from ctacble where cbl_id= $1 ;$_$;


ALTER FUNCTION public.monedacbl(integer) OWNER TO rulemanes;

--
-- Name: monedeempre(integer, integer); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.monedeempre(integer, integer) RETURNS boolean
    LANGUAGE sql
    AS $_$select true from monex a, astoctas b where a.mne_id= $2 and a.mne_idastoc=b.astc_id and b.astc_codemp= $1 ;$_$;


ALTER FUNCTION public.monedeempre(integer, integer) OWNER TO rulemanes;

--
-- Name: nomartic(integer); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.nomartic(integer) RETURNS character
    LANGUAGE sql
    AS $_$select art_nombre from articvta where art_id= $1 ;$_$;


ALTER FUNCTION public.nomartic(integer) OWNER TO rulemanes;

--
-- Name: nomarticc(integer); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.nomarticc(integer) RETURNS character
    LANGUAGE sql
    AS $_$select artc_nombre from articu where artc_id= $1 ;$_$;


ALTER FUNCTION public.nomarticc(integer) OWNER TO rulemanes;

--
-- Name: nomcble(integer); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.nomcble(integer) RETURNS character
    LANGUAGE sql
    AS $_$select cbl_nombre from ctacble where cbl_id= $1 ;$_$;


ALTER FUNCTION public.nomcble(integer) OWNER TO rulemanes;

--
-- Name: nomcli(integer); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.nomcli(integer) RETURNS character
    LANGUAGE sql
    AS $_$select cli_nombre from clientes where cli_id= $1 ;$_$;


ALTER FUNCTION public.nomcli(integer) OWNER TO rulemanes;

--
-- Name: nomcomp(integer); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.nomcomp(integer) RETURNS character
    LANGUAGE sql
    AS $_$select comp_nombre from comprob where comp_id= $1 ;$_$;


ALTER FUNCTION public.nomcomp(integer) OWNER TO rulemanes;

--
-- Name: nomconpag(integer); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.nomconpag(integer) RETURNS character
    LANGUAGE sql
    AS $_$select cdp_nombre from conpag where cdp_codigo= $1 ;$_$;


ALTER FUNCTION public.nomconpag(integer) OWNER TO rulemanes;

--
-- Name: nomctes(integer); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.nomctes(integer) RETURNS character
    LANGUAGE sql
    AS $_$select cte_nombre from ctes where cte_id= $1 ;$_$;


ALTER FUNCTION public.nomctes(integer) OWNER TO rulemanes;

--
-- Name: nomelm(integer); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.nomelm(integer) RETURNS character
    LANGUAGE sql
    AS $_$select zon_nombre from zona where zon_codigo= $1 ;$_$;


ALTER FUNCTION public.nomelm(integer) OWNER TO rulemanes;

--
-- Name: nomemp(integer); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.nomemp(integer) RETURNS character
    LANGUAGE sql
    AS $_$select emp_nombre from empresa where emp_codigo= $1 ;$_$;


ALTER FUNCTION public.nomemp(integer) OWNER TO rulemanes;

--
-- Name: nomiva(integer); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.nomiva(integer) RETURNS character
    LANGUAGE sql
    AS $_$select iva_nombre from coniva where iva_codigo= $1 ;$_$;


ALTER FUNCTION public.nomiva(integer) OWNER TO rulemanes;

--
-- Name: nomivacli(integer); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.nomivacli(integer) RETURNS character
    LANGUAGE sql
    AS $_$select iva_nombre from coniva,clientes where iva_codigo=cli_coniva and cli_id= $1 ;$_$;


ALTER FUNCTION public.nomivacli(integer) OWNER TO rulemanes;

--
-- Name: nommoneda(smallint); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.nommoneda(smallint) RETURNS character
    LANGUAGE sql
    AS $_$select mon_nombre from moneda where mon_codigo= $1 ;$_$;


ALTER FUNCTION public.nommoneda(smallint) OWNER TO rulemanes;

--
-- Name: nomopera(integer); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.nomopera(integer) RETURNS character
    LANGUAGE sql
    AS $_$select operad_nombre from operadores where operad_id= $1 ;$_$;


ALTER FUNCTION public.nomopera(integer) OWNER TO rulemanes;

--
-- Name: nompais(smallint); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.nompais(smallint) RETURNS character
    LANGUAGE sql
    AS $_$select pai_nombre from pais where pai_codigo= $1 ;$_$;


ALTER FUNCTION public.nompais(smallint) OWNER TO rulemanes;

--
-- Name: nomprov(integer); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.nomprov(integer) RETURNS character
    LANGUAGE sql
    AS $_$select pro_nombre from provin where pro_codigo= $1 ;$_$;


ALTER FUNCTION public.nomprov(integer) OWNER TO rulemanes;

--
-- Name: nomprove(integer); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.nomprove(integer) RETURNS character
    LANGUAGE sql
    AS $_$select prov_nombre from prove where prov_id= $1 ;$_$;


ALTER FUNCTION public.nomprove(integer) OWNER TO rulemanes;

--
-- Name: nomptv(integer); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.nomptv(integer) RETURNS character
    LANGUAGE sql
    AS $_$select ptv_nombre from ptovta where ptv_id= $1 ;$_$;


ALTER FUNCTION public.nomptv(integer) OWNER TO rulemanes;

--
-- Name: nomtipcta(integer); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.nomtipcta(integer) RETURNS character
    LANGUAGE sql
    AS $_$select tct_nombre from tipcta where tct_codigo = $1;$_$;


ALTER FUNCTION public.nomtipcta(integer) OWNER TO rulemanes;

--
-- Name: nomvend(integer); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.nomvend(integer) RETURNS character
    LANGUAGE sql
    AS $_$select ven_nombre from vendedor where ven_id= $1 ;$_$;


ALTER FUNCTION public.nomvend(integer) OWNER TO rulemanes;

--
-- Name: nroastc(integer); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.nroastc(integer) RETURNS integer
    LANGUAGE sql
    AS $_$select astc_numero from astoctas where astc_id= $1 ;$_$;


ALTER FUNCTION public.nroastc(integer) OWNER TO rulemanes;

--
-- Name: numash(integer); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.numash(integer) RETURNS integer
    LANGUAGE sql
    AS $_$select ash_numero from astoh where ash_id= $1;$_$;


ALTER FUNCTION public.numash(integer) OWNER TO rulemanes;

--
-- Name: numemiti(integer); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.numemiti(integer) RETURNS integer
    LANGUAGE sql
    AS $_$select emi_numero from emiti where emi_id = $1 ;$_$;


ALTER FUNCTION public.numemiti(integer) OWNER TO rulemanes;

--
-- Name: numero_letras(numeric); Type: FUNCTION; Schema: public; Owner: zona
--

CREATE FUNCTION public.numero_letras(numeric) RETURNS text
    LANGUAGE plpgsql
    AS $_$
DECLARE
     numero	ALIAS FOR $1;
     lnEntero INTEGER;
     lcRetorno TEXT = '';
     lnTerna INTEGER = 1;
     lcMiles TEXT;
     lcCadena TEXT;
     maxi TEXT;
     lnUnidades INTEGER;
     lnDecenas INTEGER;
     lnCentenas INTEGER;
     lnFraccion INTEGER;
     lnSw INTEGER;
BEGIN
     lnEntero := FLOOR(numero)::INTEGER;--Obtenemos la parte Entera
     lnFraccion := FLOOR(((numero - lnEntero) * 100))::INTEGER;--Obtenemos la Fraccion del Monto
    -- lcRetorno := '';
    -- lnTerna := 1;
     IF lnEntero > 0 THEN
     lnSw := LENGTH(lnEntero::varchar);
     WHILE lnTerna <= lnSw LOOP
        -- Recorro terna por terna
        lcCadena = '';
        lnUnidades = lnEntero % 10;
        lnEntero = CAST(lnEntero/10 AS INTEGER);
        lnDecenas = lnEntero % 10;
        lnEntero = CAST(lnEntero/10 AS INTEGER);
        lnCentenas = lnEntero % 10;
        lnEntero = CAST(lnEntero/10 AS INTEGER);
       SELECT
         CASE /* UNIDADES */
           WHEN lnUnidades = 1 AND lnTerna = 1 THEN 'UNO ' || lcCadena
           WHEN lnUnidades = 1 AND lnTerna <> 1 THEN 'UN ' || lcCadena
           WHEN lnUnidades = 2 THEN 'DOS ' || lcCadena
           WHEN lnUnidades = 3 THEN 'TRES ' || lcCadena
           WHEN lnUnidades = 4 THEN 'CUATRO ' || lcCadena
           WHEN lnUnidades = 5 THEN 'CINCO ' || lcCadena
           WHEN lnUnidades = 6 THEN 'SEIS ' || lcCadena
           WHEN lnUnidades = 7 THEN 'SIETE ' || lcCadena
           WHEN lnUnidades = 8 THEN 'OCHO ' || lcCadena
           WHEN lnUnidades = 9 THEN 'NUEVE ' || lcCadena
           ELSE lcCadena
          END INTO lcCadena;
          /* UNIDADES */
    -- Analizo las decenas
    SELECT
    CASE /* DECENAS */
      WHEN lnDecenas = 1 THEN
        CASE lnUnidades
          WHEN 0 THEN 'DIEZ '
          WHEN 1 THEN 'ONCE '
          WHEN 2 THEN 'DOCE '
          WHEN 3 THEN 'TRECE '
          WHEN 4 THEN 'CATORCE '
          WHEN 5 THEN 'QUINCE '
          ELSE 'DIECI' || lcCadena
        END
      WHEN lnDecenas = 2 AND lnUnidades = 0 THEN 'VEINTE ' || lcCadena
      WHEN lnDecenas = 2 AND lnUnidades <> 0 THEN 'VEINTI' || lcCadena
      WHEN lnDecenas = 3 AND lnUnidades = 0 THEN 'TREINTA ' || lcCadena
      WHEN lnDecenas = 3 AND lnUnidades <> 0 THEN 'TREINTA Y ' || lcCadena
      WHEN lnDecenas = 4 AND lnUnidades = 0 THEN 'CUARENTA ' || lcCadena
      WHEN lnDecenas = 4 AND lnUnidades <> 0 THEN 'CUARENTA Y ' || lcCadena
      WHEN lnDecenas = 5 AND lnUnidades = 0 THEN 'CINCUENTA ' || lcCadena
      WHEN lnDecenas = 5 AND lnUnidades <> 0 THEN 'CINCUENTA Y ' || lcCadena
      WHEN lnDecenas = 6 AND lnUnidades = 0 THEN 'SESENTA ' || lcCadena
      WHEN lnDecenas = 6 AND lnUnidades <> 0 THEN 'SESENTA Y ' || lcCadena
      WHEN lnDecenas = 7 AND lnUnidades = 0 THEN 'SETENTA ' || lcCadena
      WHEN lnDecenas = 7 AND lnUnidades <> 0 THEN 'SETENTA Y ' || lcCadena
      WHEN lnDecenas = 8 AND lnUnidades = 0 THEN 'OCHENTA ' || lcCadena
      WHEN lnDecenas = 8 AND lnUnidades <> 0 THEN 'OCHENTA Y ' || lcCadena
      WHEN lnDecenas = 9 AND lnUnidades = 0 THEN 'NOVENTA ' || lcCadena
      WHEN lnDecenas = 9 AND lnUnidades <> 0 THEN 'NOVENTA Y ' || lcCadena
      ELSE lcCadena
    END INTO lcCadena; /* DECENAS */
    -- Analizo las centenas
    SELECT
    CASE /* CENTENAS */
      WHEN lnCentenas = 1 AND lnUnidades = 0 AND lnDecenas = 0 THEN 'CIEN ' || lcCadena
      WHEN lnCentenas = 1 AND NOT(lnUnidades = 0 AND lnDecenas = 0) THEN 'CIENTO ' || lcCadena
      WHEN lnCentenas = 2 THEN 'DOSCIENTOS ' || lcCadena
      WHEN lnCentenas = 3 THEN 'TRESCIENTOS ' || lcCadena
      WHEN lnCentenas = 4 THEN 'CUATROCIENTOS ' || lcCadena
      WHEN lnCentenas = 5 THEN 'QUINIENTOS ' || lcCadena
      WHEN lnCentenas = 6 THEN 'SEISCIENTOS ' || lcCadena
      WHEN lnCentenas = 7 THEN 'SETECIENTOS ' || lcCadena
      WHEN lnCentenas = 8 THEN 'OCHOCIENTOS ' || lcCadena
      WHEN lnCentenas = 9 THEN 'NOVECIENTOS ' || lcCadena
      ELSE lcCadena
    END INTO lcCadena;/* CENTENAS */
    -- Analizo la terna
    SELECT
    CASE /* TERNA */
      WHEN lnTerna = 1 THEN lcCadena
      WHEN lnTerna = 2 AND (lnUnidades + lnDecenas + lnCentenas <> 0) THEN lcCadena || ' MIL '
      WHEN lnTerna = 3 AND (lnUnidades + lnDecenas + lnCentenas <> 0) AND
        lnUnidades = 1 AND lnDecenas = 0 AND lnCentenas = 0 THEN lcCadena || ' MILLON '
      WHEN lnTerna = 3 AND (lnUnidades + lnDecenas + lnCentenas <> 0) AND
        NOT (lnUnidades = 1 AND lnDecenas = 0 AND lnCentenas = 0) THEN lcCadena || ' MILLONES '
      WHEN lnTerna = 4 AND (lnUnidades + lnDecenas + lnCentenas <> 0) THEN lcCadena || ' MIL MILLONES '
      ELSE ''
    END INTO lcCadena;/* TERNA */
 
    --Retornamos los Valores Obtenidos
    lcRetorno = lcCadena  || lcRetorno;
    lnTerna = lnTerna + 1;
    END LOOP;
  END IF;
  IF lnTerna = 1 THEN
    lcRetorno := 'CERO';
  END IF;

  
  IF lnFraccion!= 0  THEN
	lcRetorno :=' PESOS ' || RTRIM(lcRetorno) || ' CON ' || LTRIM(lnFraccion::varchar) || '/100 CENTAVOS ------';
    else
	lcRetorno :=' PESOS ' || RTRIM(lcRetorno) || ' ------';
  END IF;

RETURN lcRetorno;
end;$_$;


ALTER FUNCTION public.numero_letras(numeric) OWNER TO zona;

--
-- Name: numtalon(integer, integer, integer); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.numtalon(integer, integer, integer) RETURNS integer
    LANGUAGE plpgsql
    AS $_$
    DECLARE
	ptovta		ALIAS FOR $1; 
	comproba	ALIAS FOR $2; 
	modo		ALIAS FOR $3; -- 1:graba  2:consulta
	otro 		integer;
	talonario	integer;
	retval 		integer;
    BEGIN

    select comp_talon into talonario from
    	 comprob where int4eq(comproba,comp_id);

    select tal_valor into retval from
    	 talon where int4eq(talonario,tal_id);

    if found then
	if int4eq(modo,1) then
	    update talon set tal_valor=tal_valor+1 where int4eq(talonario,tal_id);
	end if;		
    end if;
    RETURN retval;
    END;$_$;


ALTER FUNCTION public.numtalon(integer, integer, integer) OWNER TO rulemanes;

--
-- Name: numtalonf(integer, integer, date); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.numtalonf(integer, integer, date) RETURNS integer
    LANGUAGE plpgsql
    AS $_$
    DECLARE
	ptovta		ALIAS FOR $1; 
	comprob		ALIAS FOR $2; 
	fecha		ALIAS FOR $3;
	otro 		integer;
	anter		date;
	retval 		integer;
    BEGIN

    select tal_fecha into anter from
    	 talon where int4eq(ptovta,tal_idptov) and int4eq(comprob,tal_comprob);

    if(date_le(anter,fecha)) then
	select tal_valor into retval from
    	    talon where int4eq(ptovta,tal_idptov) and int4eq(comprob,tal_comprob);
	if not found then
		otro:=0;
		select tal_valor into retval from talon where int4eq(otro,tal_idptov) and int4eq(comprob,tal_comprob);
		update talon set tal_valor=tal_valor+1,tal_fecha=fecha where int4eq(otro,tal_idptov) and int4eq(comprob,tal_comprob);
	     else
		update talon set tal_valor=tal_valor+1,tal_fecha=fecha where int4eq(ptovta,tal_idptov) and int4eq(comprob,tal_comprob);
	end if;
      else
        retval:=-1;
    end if;

    RETURN retval;
    END;$_$;


ALTER FUNCTION public.numtalonf(integer, integer, date) OWNER TO rulemanes;

--
-- Name: obtener_estadisticas_reclamos_cliente(integer, integer); Type: FUNCTION; Schema: public; Owner: zona
--

CREATE FUNCTION public.obtener_estadisticas_reclamos_cliente(p_cliente_id integer, p_codemp integer) RETURNS TABLE(total_reclamos bigint, reclamos_abiertos bigint, reclamos_en_proceso bigint, reclamos_resueltos bigint, reclamos_cerrados bigint, tiempo_promedio_resolucion numeric)
    LANGUAGE plpgsql
    AS $$
BEGIN
    RETURN QUERY
    SELECT 
        COUNT(*) as total_reclamos,
        COUNT(CASE WHEN rec_estado = 1 THEN 1 END) as reclamos_abiertos,
        COUNT(CASE WHEN rec_estado = 2 THEN 1 END) as reclamos_en_proceso,
        COUNT(CASE WHEN rec_estado = 3 THEN 1 END) as reclamos_resueltos,
        COUNT(CASE WHEN rec_estado = 4 THEN 1 END) as reclamos_cerrados,
        COALESCE(AVG(EXTRACT(EPOCH FROM (rec_fecha_cierre - rec_fecha_creacion))/86400), 0) as tiempo_promedio_resolucion
    FROM reclamos
    WHERE rec_cliente_id = p_cliente_id 
    AND rec_codemp = p_codemp;
END;
$$;


ALTER FUNCTION public.obtener_estadisticas_reclamos_cliente(p_cliente_id integer, p_codemp integer) OWNER TO zona;

--
-- Name: obtener_orden_popularidad_cliente(integer, integer); Type: FUNCTION; Schema: public; Owner: zona
--

CREATE FUNCTION public.obtener_orden_popularidad_cliente(p_cli_id integer, p_codemp integer) RETURNS TABLE(art_id integer, popularidad numeric)
    LANGUAGE plpgsql
    AS $$
BEGIN
    RETURN QUERY
    SELECT 
        m.movs_artic as art_id,
        SUM(m.movs_canti) as popularidad
    FROM movsto m
    WHERE m.movs_codcli = p_cli_id 
      AND m.movs_codemp = p_codemp
      AND m.movs_canti > 0
    GROUP BY m.movs_artic
    ORDER BY popularidad DESC;
END;
$$;


ALTER FUNCTION public.obtener_orden_popularidad_cliente(p_cli_id integer, p_codemp integer) OWNER TO zona;

--
-- Name: pagfac1(integer); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.pagfac1(integer) RETURNS character
    LANGUAGE plpgsql
    AS $_$
declare
idastoh		 alias for $1;
ret 		 varchar;
aux1 		 varchar;
aux2 		 varchar;
codemp	 	 integer;
cuantchq 	 integer;
pago	 	 numeric;
eneft	 	 numeric;
enchq	 	 numeric;
enotr	 	 numeric;
begin
codemp:=1;

    select sum(astc_importe) into pago from astoctas where astc_idastoh=idastoh and astc_ctacble=idcbl(codemp,valvaramb(codemp,0,'DEUDXV')::integer) and astc_importe<0 and codcomp(astc_comprob) not in ('CA','CB');

    if pago is not null then
	ret:='Forma de Pago:';
	select sum(astc_importe) into eneft from astoctas where astc_idastoh=idastoh and astc_ctacble=idcbl(codemp,valvaramb(codemp,0,'CAJA')::integer) and astc_importe>0;
	select sum(astc_importe) into enchq from astoctas where astc_idastoh=idastoh and astc_ctacble=idcbl(codemp,valvaramb(codemp,0,'CHEQUE')::integer) and astc_importe>0;
	select sum(astc_importe) into enotr from astoctas where astc_idastoh=idastoh and astc_ctacble not in (idcbl(codemp,valvaramb(codemp,0,'CHEQUE')::integer),idcbl(codemp,valvaramb(codemp,0,'CAJA')::integer),idcbl(codemp,valvaramb(codemp,0,'DEUDXV')::integer)) and astc_importe>0;
	if eneft is not null then
	    ret:= ret || '  Efectivo                                                      $'|| eneft ;
	  else	    
	    if enchq is not null then
		select count(*) into cuantchq from cheques, astoctas where chq_idastoc=astc_id and astc_idastoh=idastoh and astc_ctacble=idcbl(codemp,valvaramb(codemp,0,'CHEQUE')::integer) and astc_importe>0;
		if (cuantchq>2) then
		    ret:= ret || '  Cheques segun detalle al dorso                                 $'|| enchq ;
		    end if;
		if (cuantchq<3) then
		    select descchqf(chq_id) into aux1 from cheques, astoctas where chq_idastoc=astc_id and astc_idastoh=idastoh and astc_ctacble=idcbl(codemp,valvaramb(codemp,0,'CHEQUE')::integer) and astc_importe>0 order by chq_id asc limit 1;
		    ret:= ret ||' Cheque '|| aux1;
		    end if;
	      else	    
		if enotr is not null then
		    select descble(astc_ctacble) into aux2 from astoctas where astc_idastoh=idastoh and astc_ctacble not in (idcbl(codemp,valvaramb(codemp,0,'CHEQUE')::integer),idcbl(codemp,valvaramb(codemp,0,'CAJA')::integer),idcbl(codemp,valvaramb(codemp,0,'DEUDXV')::integer)) and astc_importe>0;
		    ret:= ret ||'  '|| aux2 ||'                 $'||  enotr;
		    end if;
		end if;
	    end if;
	end if;

if ret is null then 
    ret:='   ';
end if;

return ret;
end;$_$;


ALTER FUNCTION public.pagfac1(integer) OWNER TO rulemanes;

--
-- Name: pagfac2(integer); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.pagfac2(integer) RETURNS character
    LANGUAGE plpgsql
    AS $_$
declare
idastoh		 alias for $1;
ret 		 varchar;
aux1 		 varchar;
aux2 		 varchar;
codemp	 	 integer;
cuantchq 	 integer;
pago	 	 numeric;
eneft	 	 numeric;
enchq	 	 numeric;
enotr	 	 numeric;
begin
codemp:=1;

    select sum(astc_importe) into pago from astoctas where astc_idastoh=idastoh and astc_ctacble=idcbl(codemp,valvaramb(codemp,0,'DEUDXV')::integer) and astc_importe<0 and codcomp(astc_comprob) not in ('CA','CB');

    if pago is not null then
	select sum(astc_importe) into eneft from astoctas where astc_idastoh=idastoh and astc_ctacble=idcbl(codemp,valvaramb(codemp,0,'CAJA')::integer) and astc_importe>0;
	select sum(astc_importe) into enchq from astoctas where astc_idastoh=idastoh and astc_ctacble=idcbl(codemp,valvaramb(codemp,0,'CHEQUE')::integer) and astc_importe>0;
	select sum(astc_importe) into enotr from astoctas where astc_idastoh=idastoh and astc_ctacble not in (idcbl(codemp,valvaramb(codemp,0,'CHEQUE')::integer),idcbl(codemp,valvaramb(codemp,0,'CAJA')::integer),idcbl(codemp,valvaramb(codemp,0,'DEUDXV')::integer)) and astc_importe>0;
	if eneft is not null then
	    if enchq is not null then
		select descchqf(chq_id) into aux1 from cheques, astoctas where chq_idastoc=astc_id and astc_idastoh=idastoh and astc_ctacble=idcbl(codemp,valvaramb(codemp,0,'CHEQUE')::integer) and astc_importe>0 limit 1;
		ret:= ret ||' Cheque '|| aux1;
	      else	    
		if enotr is not null then
		    select descble(astc_ctacble) into aux2 from astoctas where astc_idastoh=idastoh and astc_ctacble not in (idcbl(codemp,valvaramb(codemp,0,'CHEQUE')::integer),idcbl(codemp,valvaramb(codemp,0,'CAJA')::integer),idcbl(codemp,valvaramb(codemp,0,'DEUDXV')::integer)) and astc_importe>0;
		    ret:= ret ||'  '|| aux2 ||'                 $'||  enotr;
		    end if;
		end if;

	  else    

	    if enchq is not null then
		select count(*) into cuantchq from cheques, astoctas where chq_idastoc=astc_id and astc_idastoh=idastoh and astc_ctacble=idcbl(codemp,valvaramb(codemp,0,'CHEQUE')::integer) and astc_importe>0;
		if int4eq(cuantchq,2) then
		    select descchqf(chq_id) into aux1 from cheques, astoctas where chq_idastoc=astc_id and astc_idastoh=idastoh and astc_ctacble=idcbl(codemp,valvaramb(codemp,0,'CHEQUE')::integer) and astc_importe>0 order by chq_id desc limit 1;
		    ret:= ret ||' Cheque '|| aux1;
	    	  else	    
		    if enotr is not null then
			select descble(astc_ctacble) into aux2 from astoctas where astc_idastoh=idastoh and astc_ctacble not in (idcbl(codemp,valvaramb(codemp,0,'CHEQUE')::integer),idcbl(codemp,valvaramb(codemp,0,'CAJA')::integer),idcbl(codemp,valvaramb(codemp,0,'DEUDXV')::integer)) and astc_importe>0;
			ret:= ret ||'  '|| aux2 ||'                 $'||  enotr;
			end if;
		    end if;

	      else	    

		if enotr is not null then
		    select descble(astc_ctacble) into aux2 from astoctas where astc_idastoh=idastoh and astc_ctacble not in (idcbl(codemp,valvaramb(codemp,0,'CHEQUE')::integer),idcbl(codemp,valvaramb(codemp,0,'CAJA')::integer),idcbl(codemp,valvaramb(codemp,0,'DEUDXV')::integer)) and astc_importe>0;
		    ret:= ret ||'  '|| aux2 ||'                 $'||  enotr;
		    end if;
		end if;
	    end if;
	end if;

if ret is null then 
    ret:='   ';
end if;

return ret;
end;$_$;


ALTER FUNCTION public.pagfac2(integer) OWNER TO rulemanes;

--
-- Name: pedirem(integer); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.pedirem(integer) RETURNS boolean
    LANGUAGE plpgsql
    AS $_$
    DECLARE
	idemiti		ALIAS FOR $1;
	compara 	integer;
	retval 		boolean;
    BEGIN
    retval:='f';

    select rn_idemitip into compara from relnotp where rn_idemitih=idemiti limit 1;
    if not found then 
	retval:='t';
    end if;

    RETURN retval;
    END;$_$;


ALTER FUNCTION public.pedirem(integer) OWNER TO rulemanes;

--
-- Name: prescble(integer, integer); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.prescble(integer, integer) RETURNS boolean
    LANGUAGE plpgsql
    AS $_$
declare
origen 		 alias for $1;
destino		 alias for $2;
ret 		 boolean;
registro	 record;
auxo		 varchar;
begin
ret:='t';
	for registro in select * from ctacble 
	      where cbl_codemp=destino and cbl_present is null 
	    loop
		select cbl_present into auxo from ctacble where cbl_codemp=origen and bpchareq(cbl_codigo,registro.cbl_codigo);
		if auxo is not null then
		    update ctacble set cbl_present=auxo where cbl_id=registro.cbl_id;
		end if;
	    end loop; 
return ret;
end;$_$;


ALTER FUNCTION public.prescble(integer, integer) OWNER TO rulemanes;

--
-- Name: provastoh(integer); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.provastoh(integer) RETURNS character varying
    LANGUAGE plpgsql
    AS $_$
declare
astc	 alias for $1;
ret	 varchar;
begin

ret:='';
select descprov(astc_ctacte) into ret from astoctas where astc_idastoh= astc and astc_tipcta=98 and astc_ctacble=1766;
return ret;
end;$_$;


ALTER FUNCTION public.provastoh(integer) OWNER TO rulemanes;

--
-- Name: provedeart(integer); Type: FUNCTION; Schema: public; Owner: zona
--

CREATE FUNCTION public.provedeart(art_id_venta integer) RETURNS integer
    LANGUAGE plpgsql
    AS $$
DECLARE
    proveedor_id integer;
    art_id_compra integer;
BEGIN
    -- 1. Obtener el ID del artículo de compra relacionado
    SELECT re_articc INTO art_id_compra
    FROM receta
    WHERE re_articv = art_id_venta
    LIMIT 1;
    
    IF art_id_compra IS NULL THEN
        RAISE NOTICE 'No se encontró artículo de compra para el artículo de venta %', art_id_venta;
        RETURN NULL;
    END IF;
    
    -- 2. Buscar el proveedor activo más reciente para este artículo de compra
    SELECT pxa_idprov INTO proveedor_id
    FROM pxarticu
    WHERE pxa_idart = art_id_compra
      AND pxa_estado = 1  -- Solo precios activos
    ORDER BY pxa_fecha DESC  -- Asumiendo que hay un campo de fecha
    LIMIT 1;
    
    IF proveedor_id IS NULL THEN
        RAISE NOTICE 'No se encontró proveedor activo para el artículo de compra %', art_id_compra;
    END IF;
    
    RETURN proveedor_id;
END;
$$;


ALTER FUNCTION public.provedeart(art_id_venta integer) OWNER TO zona;

--
-- Name: provedemovc(integer); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.provedemovc(integer) RETURNS integer
    LANGUAGE sql
    AS $_$select cpra_prove from compra where cpra_id = (select movc_idcompra from movstoc where movc_id = $1) ;$_$;


ALTER FUNCTION public.provedemovc(integer) OWNER TO rulemanes;

--
-- Name: provinclie(integer, integer); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.provinclie(integer, integer) RETURNS integer
    LANGUAGE sql
    AS $_$select sucu_provin from sucursalc where sucu_cliente= $1 and sucu_codigo= $2 ;$_$;


ALTER FUNCTION public.provinclie(integer, integer) OWNER TO rulemanes;

--
-- Name: proxgpocli(integer); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.proxgpocli(integer) RETURNS integer
    LANGUAGE sql
    AS $_$select max(astc_grupo)+1 from astoctas where astc_codemp=1 and astc_ctacble=18 and astc_ctacte= $1 ;$_$;


ALTER FUNCTION public.proxgpocli(integer) OWNER TO rulemanes;

--
-- Name: proxgpocte(integer); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.proxgpocte(integer) RETURNS integer
    LANGUAGE sql
    AS $_$select max(astc_grupo)+1 from astoctas where astc_ctacte= $1 ;$_$;


ALTER FUNCTION public.proxgpocte(integer) OWNER TO rulemanes;

--
-- Name: ptovdeastc(integer); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.ptovdeastc(integer) RETURNS integer
    LANGUAGE sql
    AS $_$select astc_idptov from astoctas where astc_id= $1 ;$_$;


ALTER FUNCTION public.ptovdeastc(integer) OWNER TO rulemanes;

--
-- Name: ptovdeastoc(integer); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.ptovdeastoc(integer) RETURNS integer
    LANGUAGE sql
    AS $_$select astc_idptov from astoctas where astc_id= $1 ;$_$;


ALTER FUNCTION public.ptovdeastoc(integer) OWNER TO rulemanes;

--
-- Name: ptovdefault(integer); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.ptovdefault(integer) RETURNS integer
    LANGUAGE sql
    AS $_$select ptv_id from ptovta where ptv_codemp= $1 order by ptv_id limit 1;$_$;


ALTER FUNCTION public.ptovdefault(integer) OWNER TO rulemanes;

--
-- Name: ptovemp(integer); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.ptovemp(integer) RETURNS integer
    LANGUAGE plpgsql
    AS $_$
    DECLARE
	empresa		ALIAS FOR $1; 
	retval 		integer;
	cuantos		integer;
    BEGIN
    select count(*) into cuantos from
    	 ptovta where int4eq(empresa,ptv_codemp);

	 if int4eq(cuantos,1) then
		select ptv_id into retval from
    		ptovta where int4eq(empresa,ptv_codemp);
	    else
		retval:=-1;
	 end if;		
    RETURN retval;
    END;$_$;


ALTER FUNCTION public.ptovemp(integer) OWNER TO rulemanes;

--
-- Name: remitofactur(integer, date, integer); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.remitofactur(integer, date, integer) RETURNS integer
    LANGUAGE plpgsql
    AS $_$
    DECLARE
	idastoh		ALIAS FOR $1;
	fecfactu	ALIAS FOR $2;
	modo		ALIAS FOR $3;
	retval 		integer;
	retvalc		character;
	civaemp		integer;
	civacli		integer;
	codemp 		integer;
	idptov 		integer;
	elcompbte	integer;
	compremito	integer;
	idcli 		integer;
	tipiva 		integer;
	succli 		integer;
	numero 		integer;
	newidastoh 	integer;
	newidemiti 	integer;
	fecfact 	date;
	ultfecfact 	date;
	anio		smallint;
	mes		smallint;
	regis		record;
	registro	record;
	titulo 		character varying;
	nomclie		character varying;
	comporig	character varying;
    BEGIN

	retval:=0;
	fecfact:=fecfactu;

	select ash_codemp,ash_idptov,emi_codcli,emi_succli,trim(codcomp(ash_comprob)) into codemp,idptov,idcli,succli,comporig from astoh,emiti where ash_id=idastoh and emi_idastoh=ash_id;
	select ptv_tipiva::int4 into tipiva from ptovta where ptv_id=idptov;
	select emp_coniva::int4 into civaemp from empresa where emp_codigo=codemp;
	select cli_coniva::int4 into civacli from clientes where cli_id=idcli;

	select date_part('Y',fecfact) into anio;
	select date_part('month',fecfact) into mes;

	if int4eq(civaemp,1) then
	    if (int4eq(civacli,1) or int4eq(civacli,8))then
		if bpchareq(comporig,'DV') then
		    select idcomp('CA',idptov,codemp) into elcompbte;
		  else
		    if (int4eq(codemp,2)) then
			select idcomp('FM',idptov,codemp) into elcompbte;
		    else
			select idcomp('FA',idptov,codemp) into elcompbte;
		    end if;
		end if;

	      else

		if bpchareq(comporig,'DV') then
		    select idcomp('CB',idptov,codemp) into elcompbte;
		  else
		    select idcomp('FB',idptov,codemp) into elcompbte;
		end if;
	    end if;
	end if;

	if bpchareq(comporig,'DV') then
	    select idcomp('DV',idptov,codemp) into compremito;
	  else
	    select idcomp('RE',idptov,codemp) into compremito;
	end if;

	select ash_fecval into ultfecfact from astoh where ash_codemp=codemp and ash_idptov=idptov and ash_estado=2 and ash_comprob=elcompbte order by 1 desc limit 1;
	if(date_lt(fecfact,ultfecfact)) then
	    fecfact:=ultfecfact;
	end if;

	select desccli(idcli,succli) into nomclie;

	select nomcomp(elcompbte) ||' DE '|| nomclie into titulo;

	select numtalon(idptov,elcompbte,1) into numero;

	insert into astoh (ash_codemp,ash_idptov,ash_anio,ash_mes,ash_comprob,ash_numero,ash_titulo,ash_fecomp,ash_fecval,ash_nro2,ash_estado) 
	    values (codemp,idptov,anio,mes,elcompbte,numero,titulo,fecfact,fecfact,0,-1);

	select ash_id into newidastoh from astoh where ash_codemp=codemp and ash_idptov=idptov and ash_comprob=elcompbte and ash_numero=numero;

	insert into emiti (emi_idastoh,emi_codemp,emi_idptov,emi_comprob,emi_numero,emi_fecval,emi_codcli,emi_nomcli,emi_nrcuit,emi_provin,emi_grupo,emi_succli,emi_conivac,emi_conpag,emi_estado) 
	    select newidastoh,emi_codemp,emi_idptov,elcompbte,numero,fecfact,emi_codcli,emi_nomcli,emi_nrcuit,emi_provin,emi_grupo,emi_succli,emi_conivac,emi_conpag,-1 from emiti where emi_idastoh=idastoh;

	select emi_id into newidemiti from emiti where emi_idastoh=newidastoh;

	if int4eq(modo,1) then
	    insert into relrem (rr_idastohp,rr_idastohh) values (newidastoh,idastoh);

	    insert into movsto (movs_idastoh,movs_idemiti,movs_codemp,movs_idptov,movs_fecval,movs_artic,movs_canti,movs_dias,movs_precio,movs_importe,movs_estado,movs_codcli,movs_tasa) 
		select newidastoh,newidemiti,movs_codemp,movs_idptov,fecfact,movs_artic,movs_canti,movs_dias,movs_precio,movs_importe,-1,movs_codcli,movs_tasa from movsto where movs_idastoh=idastoh;

	    update astoh set ash_idemiti=newidemiti where ash_id=newidastoh;

	    update emiti set emi_facturado=2 where emi_idastoh=idastoh;

	    update astoh set ash_estado=100 where ash_id=idastoh;
	end if;



	if int4eq(modo,2) then
	    for regis in select emi_idastoh from emiti where emi_codemp=codemp and emi_idptov=idptov and emi_comprob=compremito and emi_facturado=1 and date_le(emi_fecval,fecfact) and emi_codcli=idcli and emi_succli=succli and emi_estado=2 order by emi_numero
		loop
		insert into relrem (rr_idastohp,rr_idastohh) values (newidastoh,regis.emi_idastoh);

		insert into movsto (movs_idastoh,movs_idemiti,movs_codemp,movs_idptov,movs_fecval,movs_artic,movs_canti,movs_dias,movs_precio,movs_importe,movs_estado,movs_codcli,movs_tasa) 
		    select newidastoh,newidemiti,movs_codemp,movs_idptov,fecfact,movs_artic,movs_canti,movs_dias,movs_precio,movs_importe,-2,movs_codcli,movs_tasa from movsto where movs_idastoh=regis.emi_idastoh;

		update emiti set emi_facturado=2 where emi_idastoh=regis.emi_idastoh;
		update astoh set ash_estado=100 where ash_id=regis.emi_idastoh;
		end loop;

	    for registro in select movs_artic,movs_precio,movs_tasa,sum(movs_canti) as lacanti,sum(movs_importe) as elimporte from movsto where movs_idastoh=newidastoh group by 1,2,3
		loop
		insert into movsto (movs_idastoh,movs_idemiti,movs_codemp,movs_idptov,movs_fecval,movs_artic,movs_canti,movs_dias,movs_precio,movs_importe,movs_estado,movs_codcli,movs_tasa) 
		    values (newidastoh,newidemiti,codemp,idptov,fecfact,registro.movs_artic,registro.lacanti,1,registro.movs_precio,registro.elimporte,-1,idcli,registro.movs_tasa);
		end loop;

	    delete from movsto where movs_idastoh=newidastoh and movs_estado=-2;

	    update astoh set ash_idemiti=newidemiti where ash_id=newidastoh;
	end if;



	select graboastoctasi(newidastoh) into retval;

	update astoh set ash_estado=2 where ash_id=newidastoh;

	update astoh set ash_estado=2 where ash_estado=35 and int4eq(codclideastoh(ash_id),idcli);

	if(date_ge(fecfactu,'01-07-2015'::date) and int4eq(tipiva,1)) then
	    select graboauxfe(newidastoh) into retvalc;
	end if;

    RETURN retval;
    END;$_$;


ALTER FUNCTION public.remitofactur(integer, date, integer) OWNER TO rulemanes;

--
-- Name: remitofactur(integer, date, integer, numeric); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.remitofactur(integer, date, integer, numeric) RETURNS integer
    LANGUAGE plpgsql
    AS $_$
    DECLARE
	idastoh		ALIAS FOR $1;
	fecfactu	ALIAS FOR $2;
	modo		ALIAS FOR $3;
	alicuota	ALIAS FOR $4;
	retval 		integer;
	retvalc		character;
	civaemp		integer;
	civacli		integer;
	codemp 		integer;
	idptov 		integer;
	elcompbte	integer;
	compremito	integer;
	idcli 		integer;
	tipiva 		integer;
	disciva		integer;
	succli 		integer;
	numero 		integer;
	maxnum 		integer;
	minnum 		integer;
	newidastoh 	integer;
	newidemiti 	integer;
	fecfact 	date;
	ultfecfact 	date;
	anio		smallint;
	mes		smallint;
	regis		record;
	registro	record;
	titulo 		character varying;
	nomclie		character varying;
	comporig	character varying;
    BEGIN

	retval:=0;
	fecfact:=fecfactu;

	select ash_codemp,ash_idptov,emi_codcli,emi_succli,trim(codcomp(ash_comprob)) into codemp,idptov,idcli,succli,comporig from astoh,emiti where ash_id=idastoh and emi_idastoh=ash_id;
	select ptv_tipiva::int4 into tipiva from ptovta where ptv_id=idptov;
	select emp_coniva::int4 into civaemp from empresa where emp_codigo=codemp;
	select cli_coniva::int4 into civacli from clientes where cli_id=idcli;
	select iva_discrim::int4 into disciva from coniva where iva_codigo=civacli;

	select date_part('Y',fecfact) into anio;
	select date_part('month',fecfact) into mes;

	if int4eq(civaemp,1) then
	    if (int4eq(disciva,1))then
		if bpchareq(comporig,'DV') then
		    select idcomp('CA',idptov,codemp) into elcompbte;
		  else
		    select idcomp('FA',idptov,codemp) into elcompbte;
		end if;

	      else

		if bpchareq(comporig,'DV') then
		    select idcomp('CB',idptov,codemp) into elcompbte;
		  else
		    select idcomp('FB',idptov,codemp) into elcompbte;
		end if;
	    end if;
	end if;

	if bpchareq(comporig,'DV') then
	    select idcomp('DV',idptov,codemp) into compremito;
	  else
	    select idcomp('RE',idptov,codemp) into compremito;
	end if;

	select ash_fecval into ultfecfact from astoh where ash_codemp=codemp and ash_idptov=idptov and ash_estado=2 and ash_comprob=elcompbte order by 1 desc limit 1;
	if(date_lt(fecfact,ultfecfact)) then
	    fecfact:=ultfecfact;
	end if;

	select desccli(idcli,succli) into nomclie;

	select nomcomp(elcompbte) ||' DE '|| nomclie into titulo;

	select ash_numero into maxnum from astoh where ash_comprob=elcompbte and ash_idptov=idptov order by ash_numero desc limit 1;
	select ash_numero into minnum from astoh where ash_comprob=elcompbte and ash_idptov=idptov order by ash_numero asc limit 1;
	if (numeric_le(minnum,0)) then
	    minnum:= minnum*-1;
	end if;

	if maxnum is null then
	    numero=-1;
	else
	    if(numeric_ge(maxnum,minnum)) then
		numero:=(maxnum+1)*-1;
	    else
		numero:=(minnum*-1)-1;
	    end if;
	end if;

	insert into astoh (ash_codemp,ash_idptov,ash_anio,ash_mes,ash_comprob,ash_numero,ash_titulo,ash_fecomp,ash_fecval,ash_nro2,ash_estado) 
	    values (codemp,idptov,anio,mes,elcompbte,numero,titulo,fecfact,fecfact,0,-1);

	select ash_id into newidastoh from astoh where ash_codemp=codemp and ash_idptov=idptov and ash_comprob=elcompbte and ash_numero=numero;

	insert into emiti (emi_idastoh,emi_codemp,emi_idptov,emi_comprob,emi_numero,emi_fecval,emi_codcli,emi_nomcli,emi_nrcuit,emi_provin,emi_grupo,emi_succli,emi_conivac,emi_conpag,emi_estado,emi_leyen,emi_ordencompra,emi_vended) 
	    select newidastoh,emi_codemp,emi_idptov,elcompbte,numero,fecfact,emi_codcli,emi_nomcli,emi_nrcuit,emi_provin,emi_grupo,emi_succli,emi_conivac,emi_conpag,-1,emi_leyen,emi_ordencompra,emi_vended from emiti where emi_idastoh=idastoh;
	select emi_id into newidemiti from emiti where emi_idastoh=newidastoh;

	update emiti set emi_grupo=grupocli(idcli,1) where emi_id=newidastoh;


	if int4eq(modo,1) then
	    insert into relrem (rr_idastohp,rr_idastohh) values (newidastoh,idastoh);

	    insert into movsto (movs_idastoh,movs_idemiti,movs_codemp,movs_idptov,movs_fecval,movs_artic,movs_canti,movs_dias,movs_precio,movs_importe,movs_estado,movs_codcli,movs_tasa,movs_bonif,movs_preciosb,movs_leybonif) 
		select newidastoh,newidemiti,movs_codemp,movs_idptov,fecfact,movs_artic,movs_canti,movs_dias,movs_precio,movs_importe,-1,movs_codcli,movs_tasa,movs_bonif,movs_preciosb,movs_leybonif from movsto where movs_idastoh=idastoh;

	    update astoh set ash_idemiti=newidemiti where ash_id=newidastoh;

	    update emiti set emi_facturado=2 where emi_idastoh=idastoh;

	    update astoh set ash_estado=100 where ash_id=idastoh;
	end if;



	if int4eq(modo,2) then
	    for regis in select emi_idastoh from emiti where emi_codemp=codemp and emi_idptov=idptov and emi_comprob=compremito and emi_facturado=1 and date_le(emi_fecval,fecfact) and emi_codcli=idcli and emi_succli=succli and emi_estado=2 order by emi_numero
		loop
		insert into relrem (rr_idastohp,rr_idastohh) values (newidastoh,regis.emi_idastoh);

		insert into movsto (movs_idastoh,movs_idemiti,movs_codemp,movs_idptov,movs_fecval,movs_artic,movs_canti,movs_dias,movs_precio,movs_importe,movs_estado,movs_codcli,movs_tasa,movs_bonif,movs_preciosb,movs_leybonif) 
		    select newidastoh,newidemiti,movs_codemp,movs_idptov,fecfact,movs_artic,movs_canti,movs_dias,movs_precio,movs_importe,-2,movs_codcli,movs_tasa,movs_bonif,movs_preciosb,movs_leybonif  from movsto where movs_idastoh=regis.emi_idastoh;

		update emiti set emi_facturado=2 where emi_idastoh=regis.emi_idastoh;
		update astoh set ash_estado=100 where ash_id=regis.emi_idastoh;
		end loop;

	    for registro in select movs_artic,movs_precio,movs_tasa,movs_bonif,movs_leybonif,movs_preciosb,sum(movs_canti) as lacanti,sum(movs_importe) as elimporte from movsto where movs_idastoh=newidastoh group by 1,2,3,4,5,6
		loop
		insert into movsto (movs_idastoh,movs_idemiti,movs_codemp,movs_idptov,movs_fecval,movs_artic,movs_canti,movs_dias,movs_precio,movs_importe,movs_estado,movs_codcli,movs_tasa,movs_bonif,movs_preciosb,movs_leybonif) 
		    values (newidastoh,newidemiti,codemp,idptov,fecfact,registro.movs_artic,registro.lacanti,1,registro.movs_precio,registro.elimporte,-1,idcli,registro.movs_tasa,registro.movs_bonif,registro.movs_preciosb,registro.movs_leybonif);
		end loop;

	    delete from movsto where movs_idastoh=newidastoh and movs_estado=-2;

	    update astoh set ash_idemiti=newidemiti where ash_id=newidastoh;
	end if;



	select graboastoctasi(newidastoh,alicuota) into retval;

	retval:=newidastoh;

	update astoh set ash_estado=2 where ash_id=newidastoh;

	update astoh set ash_estado=2 where ash_estado=35 and int4eq(codclideastoh(ash_id),idcli);

	if(date_ge(fecfactu,'01-07-2015'::date) and int4eq(tipiva,1)) then
	    select graboauxfe(newidastoh,alicuota) into retvalc;
	end if;

    RETURN retval;
    END;$_$;


ALTER FUNCTION public.remitofactur(integer, date, integer, numeric) OWNER TO rulemanes;

--
-- Name: retenig(integer, numeric, date); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.retenig(integer, numeric, date) RETURNS numeric
    LANGUAGE plpgsql
    AS $_$
    DECLARE
	provd		ALIAS FOR $1; 
	imppago		ALIAS FOR $2;
	fecha		ALIAS FOR $3;
	codemp		integer;
	ptov		integer;
	tipct		integer;
	retsn		integer;
	idastoh		integer;
	deudxc		integer;
	ctaretig	integer;
	compnk		integer;
	registro	record;
	totpagt		numeric;
	totrett		numeric;
	totpag 		numeric;
	totret 		numeric;
	mnoi 		numeric;
	tasa 		numeric;
	retval 		numeric;
    BEGIN
    select prov_codemp into codemp from prove where prov_id=provd;
    select prov_idptov into ptov from prove where prov_id=provd;
    select valvaramb(codemp,ptov,'TIPCTADXC') into tipct;	
    select idcbl(codemp,valvaramb(codemp,ptov,'DEUDXC')::int4) into deudxc;
    select idcbl(codemp,valvaramb(codemp,ptov,'CTARETIG')::int4) into ctaretig;
    select valvaramb(codemp,ptov,'MNIRETIG') into mnoi;
    select valvaramb(codemp,ptov,'TASARIG') into tasa;
    select prov_retig into retsn from prove where prov_id=provd;
    select idcomp('NK',1) into compnk;

    totrett:=0;
    totpagt:=0;
    totret:=0;
    totpag:=0;
    retval:=0;


    if int4eq(retsn,1) then

	for registro in select 
         * from astoctas where astc_estado=2 and astc_ctacble=deudxc and astc_tipcta=tipct and astc_ctacte=provd and date_part('year',astc_fecval)=date_part('year',fecha::date) and date_part('month',astc_fecval)=date_part('month',fecha::date) and astc_importe>0 and astc_comprob!=compnk  
	    loop 
	    select sum(astc_importe*-1) into totrett from astoctas where astc_idastoh=registro.astc_idastoh and astc_ctacble=ctaretig and astc_importe<0; 
	    select sum(astc_importe*-1) into totpagt from astoctas where astc_idastoh=registro.astc_idastoh and astc_importe<0; 
	
	    if totpagt is null then
		totpagt:=0;
	    end if;

	    if totrett is null then
		totrett:=0;
	    end if;

	    totpag:= totpag + totpagt;
	    totret:= totret + totrett;
	    
	    end loop;

	    
	if totpag is null then
	    totpag:=0;
	end if;

	if totret is null then
	    totret:=0;
	end if;

	totpag:= ((totpag + imppago)/1.21) - mnoi;


	if(totpag > 0 ) then
	    retval:= ( totpag * tasa / 100) - totret;
	end if;

	if(retval < 0 ) then
	end if;

    end if;

    RETURN retval;
    END;$_$;


ALTER FUNCTION public.retenig(integer, numeric, date) OWNER TO rulemanes;

--
-- Name: rubdeart(integer); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.rubdeart(integer) RETURNS smallint
    LANGUAGE sql
    AS $_$select art_codrub from articvta where art_id = $1;$_$;


ALTER FUNCTION public.rubdeart(integer) OWNER TO rulemanes;

--
-- Name: saldoal(integer, integer, integer, integer, date); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.saldoal(integer, integer, integer, integer, date) RETURNS numeric
    LANGUAGE plpgsql
    AS $_$
    DECLARE
	codemp		ALIAS FOR $1; 
	ptovta		ALIAS FOR $2; 
	tipcta		ALIAS FOR $3; 
	ctacte		ALIAS FOR $4;
	fechad		ALIAS FOR $5;
	salant 		numeric;
	retval 		numeric;
    BEGIN
    select sum(astc_importe) into retval from astoctas 
	where astc_codemp=codemp and astc_idptov=ptovta and astc_tipcta=tipcta 
	and astc_ctacte=ctacte and astc_fecval< fechad and astc_cierra= 1
	and astc_estado=2;

    if retval is NULL then
	    retval:=0;
    end if;
    RETURN retval;
    END;$_$;


ALTER FUNCTION public.saldoal(integer, integer, integer, integer, date) OWNER TO rulemanes;

--
-- Name: saldocbl(integer, integer, integer, date); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.saldocbl(integer, integer, integer, date) RETURNS numeric
    LANGUAGE plpgsql
    AS $_$
    DECLARE
	codemp		ALIAS FOR $1; 
	ptovta		ALIAS FOR $2; 
	cuenta		ALIAS FOR $3; 
	fechad		ALIAS FOR $4;
	retval 		numeric;
    BEGIN
    if int4eq(codemp,-1) then
	    select sum(astc_importe) into retval from astoctas 
		where astc_ctacble=cuenta and astc_fecval< fechad and astc_estado=2 and codcomp(astc_comprob)!='AR' ;
        else
	    if int4eq(ptovta,0) then
		    select sum(astc_importe) into retval from astoctas 
			where astc_codemp=codemp and astc_ctacble=cuenta 
			    and astc_estado=2 and astc_comprob not in (6,7,147,157) and astc_fecval< fechad;
    		else
		    select sum(astc_importe) into retval from astoctas 
			where astc_codemp=codemp and astc_idptov=ptovta  
			    and astc_ctacble=cuenta and astc_estado=2 and astc_comprob not in (6,7,147,157) 
			    and astc_fecval< fechad ;
	    end if;
    end if;

    if retval is NULL then
	    retval:=0;
    end if;
    RETURN retval;
    END;$_$;


ALTER FUNCTION public.saldocbl(integer, integer, integer, date) OWNER TO rulemanes;

--
-- Name: saldocli(integer, integer, integer); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.saldocli(integer, integer, integer) RETURNS numeric
    LANGUAGE plpgsql
    AS $_$
    DECLARE
	codemp		ALIAS FOR $1; 
	tipcta		ALIAS FOR $2; 
	ctacte		ALIAS FOR $3;
	salant 		numeric;
	retval 		numeric;
    BEGIN
    select sum(astc_importe) into retval from astoctas 
	where astc_codemp=codemp and astc_tipcta=tipcta 
	and astc_ctacte=ctacte and astc_cierra= 1
	and astc_estado=2;

    if retval is NULL then
	    retval:=0;
    end if;
    RETURN retval;
    END;$_$;


ALTER FUNCTION public.saldocli(integer, integer, integer) OWNER TO rulemanes;

--
-- Name: salgrup(integer, integer, integer, integer); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.salgrup(integer, integer, integer, integer) RETURNS numeric
    LANGUAGE plpgsql
    AS $_$
    DECLARE
	codemp		ALIAS FOR $1; 
	tipcta		ALIAS FOR $2;
	ctacte		ALIAS FOR $3;
	grupo		ALIAS FOR $4;
	esisti		integer;
	retval 		numeric;
    BEGIN
    if int4eq(grupo,0) then
	    select sum(astc_importe) into retval from astoctas
		where astc_codemp=codemp and astc_estado=2 and astc_tipcta=tipcta
		    and astc_ctacte=ctacte and astc_grupo is null;
	else
	    select sum(astc_importe) into retval from astoctas
		where astc_codemp=codemp and astc_estado=2 and astc_tipcta=tipcta
		    and astc_ctacte=ctacte and astc_grupo=grupo;
    end if;

    if not found then
	retval:=0;
    end if;
    RETURN retval;
    END;$_$;


ALTER FUNCTION public.salgrup(integer, integer, integer, integer) OWNER TO rulemanes;

--
-- Name: simbmoneda(smallint); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.simbmoneda(smallint) RETURNS character
    LANGUAGE sql
    AS $_$select mon_simbol from moneda where mon_codigo= $1 ;$_$;


ALTER FUNCTION public.simbmoneda(smallint) OWNER TO rulemanes;

--
-- Name: stasaconiva(smallint); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.stasaconiva(smallint) RETURNS numeric
    LANGUAGE sql
    AS $_$select iva_stasa from coniva where iva_codigo= $1;$_$;


ALTER FUNCTION public.stasaconiva(smallint) OWNER TO rulemanes;

--
-- Name: stivacli(integer); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.stivacli(integer) RETURNS numeric
    LANGUAGE sql
    AS $_$select stasaconiva(cli_coniva) from clientes where cli_id= $1;$_$;


ALTER FUNCTION public.stivacli(integer) OWNER TO rulemanes;

--
-- Name: stockafecha(integer, date); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.stockafecha(integer, date) RETURNS numeric
    LANGUAGE plpgsql
    AS $_$
    DECLARE
	artVenta	ALIAS FOR $1;
	fechah		ALIAS FOR $2;
	retval 		numeric;
	artCompra	integer;
	cantReceta	integer;
	cantIni		numeric;
	cantRem		numeric;
	fecIni 		date;
	fecRem		date;
	ok		integer;
	cantComp	numeric;
	cantVend	numeric;
	reg		record;
	regis		record;
    BEGIN

	retval:=0;
	cantIni:=0;
	cantComp:=0;
	cantVend:=0;

	select count(*) into cantReceta from receta where re_articv=artVenta;
	if (int4gt(cantReceta,1)) then
	    RAISE EXCEPTION 'Esta funcion es para relaciones 1 a 1';
	end if;

	select re_articc into artCompra from receta where re_articv=artVenta limit 1;

	select stk_cantidad,stk_fecval into cantIni,fecIni from stockinicial where stk_artic=artCompra and stk_estado=1 order by stk_fecval desc limit 1;

	if (cantIni is not null) then
	    RAISE NOTICE 'STOCK INICIAL %, FECHA %',cantIni,fecIni;
	    select sum(movc_canti) into cantComp from movstoc where movc_idastoh is not null and date_gt(movc_fecval,fecIni) and date_le(movc_fecval,fechah) and movc_estado=2 and movc_artic=artCompra;
	    if cantComp is null then
		cantComp:=0;
	    end if;
	    RAISE NOTICE 'COMPRADOS %',cantComp;

	    for reg in select * from movsto where date_gt(movs_fecval,fecini) and date_le(movs_fecval,fechah) and movs_estado=2 and movs_artic=artVenta and codcomp(comprobemi(movs_idemiti)) not in ('COT','NP') order by movs_id loop
		ok:=1;
		for regis in select * from relrem where rr_idastohp=reg.movs_idastoh loop
		    select ash_fecval into fecRem from astoh where ash_id=regis.rr_idastohh;
		    select movs_canti into cantRem from movsto where movs_idastoh=regis.rr_idastohh and movs_artic=artVenta;
		    if(date_le(fecIni,fecRem))then
			ok:=2;
			if(cantRem is not null) then
			    cantVend:=cantVend+cantRem;
			    RAISE NOTICE 'Vendidos A %, Idastoh F/R: %/%',cantRem,reg.movs_idastoh,regis.rr_idastohh;
			end if;
		    else
			ok:=2;
		    end if;
		end loop;
		if(int4eq(ok,1)) then
		    cantVend:=cantVend+reg.movs_canti;
		    RAISE NOTICE 'Vendidos B %, Idastoh: %',reg.movs_canti,reg.movs_idastoh;
		end if;
	    end loop;
	    retval:=cantIni+cantComp-cantVend;
	else
	    select sum(movc_canti) into cantComp from movstoc where movc_idastoh is not null and movc_estado=2 and movc_artic=artCompra and date_le(movc_fecval,fechah);
	    if cantComp is null then
		cantComp:=0;
	    end if;
	    select sum(movs_canti) into cantVend from movsto where movs_estado=2 and movs_artic=artVenta and codcomp(comprobemi(movs_idemiti)) not in ('COT','NP') and date_le(movs_fecval,fechah);
	    if cantVend is null then
		cantVend:=0;
	    end if;
	    RAISE NOTICE '// COMPRADOS %, VENDIDOS %',cantComp,cantVend;
	    retval:=cantComp-cantVend;
	end if;

    RETURN retval;
    END;$_$;


ALTER FUNCTION public.stockafecha(integer, date) OWNER TO rulemanes;

--
-- Name: stockhistorico(integer); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.stockhistorico(integer) RETURNS numeric
    LANGUAGE plpgsql
    AS $_$
    DECLARE
	artVenta	ALIAS FOR $1;
	retval 		numeric;
	artCompra	integer;
	cantReceta	integer;
	cantIni		numeric;
	cantRem		numeric;
	fecIni 		date;
	fecRem		date;
	ok		integer;
	cantComp	numeric;
	cantVend	numeric;
	reg		record;
	regis		record;
    BEGIN

	retval:=0;
	cantIni:=0;
	cantComp:=0;
	cantVend:=0;

	select count(*) into cantReceta from receta where re_articv=artVenta;
	if (int4gt(cantReceta,1)) then
	    RAISE EXCEPTION 'Esta funcion es para relaciones 1 a 1';
	end if;

	select re_articc into artCompra from receta where re_articv=artVenta limit 1;

	select stk_cantidad,stk_fecval into cantIni,fecIni from stockinicial where stk_artic=artCompra and stk_estado=1 order by stk_fecval desc limit 1;

	if (cantIni is not null) then
	    --RAISE NOTICE 'STOCK INICIAL %, FECHA %',cantIni,fecIni;
	    select sum(movc_canti) into cantComp from movstoc where movc_idastoh is not null and date_gt(movc_fecval,fecIni) and movc_estado=2 and movc_artic=artCompra;
	    if cantComp is null then
		cantComp:=0;
	    end if;
	    --RAISE NOTICE 'COMPRADOS %',cantComp;

	    for reg in select * from movsto where date_gt(movs_fecval,fecini) and movs_estado=2 and movs_artic=artVenta and codcomp(comprobemi(movs_idemiti)) not in ('COT','NP') order by movs_id loop
		ok:=1;
		for regis in select * from relrem where rr_idastohp=reg.movs_idastoh loop
		    select ash_fecval into fecRem from astoh where ash_id=regis.rr_idastohh;
		    select movs_canti into cantRem from movsto where movs_idastoh=regis.rr_idastohh and movs_artic=artVenta;
		    if(date_le(fecIni,fecRem))then
			ok:=2;
			if(cantRem is not null) then
			    cantVend:=cantVend+cantRem;
			    --RAISE NOTICE 'Vendidos A %, Idastoh F/R: %/%',cantRem,reg.movs_idastoh,regis.rr_idastohh;
			end if;
		    else
			ok:=2;
		    end if;
		end loop;
		if(int4eq(ok,1)) then
		    cantVend:=cantVend+reg.movs_canti;
		    --RAISE NOTICE 'Vendidos B %, Idastoh: %',reg.movs_canti,reg.movs_idastoh;
		end if;
	    end loop;
	    retval:=cantIni+cantComp-cantVend;
	else
	    select sum(movc_canti) into cantComp from movstoc where movc_idastoh is not null and movc_estado=2 and movc_artic=artCompra;
	    if cantComp is null then
		cantComp:=0;
	    end if;
	    select sum(movs_canti) into cantVend from movsto where movs_estado=2 and movs_artic=artVenta and codcomp(comprobemi(movs_idemiti)) not in ('COT','NP');
	    if cantVend is null then
		cantVend:=0;
	    end if;
	    --RAISE NOTICE '// COMPRADOS %, VENDIDOS %',cantComp,cantVend;
	    retval:=cantComp-cantVend;
	end if;

    RETURN retval;
    END;$_$;


ALTER FUNCTION public.stockhistorico(integer) OWNER TO rulemanes;

--
-- Name: subtotf(integer); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.subtotf(integer) RETURNS numeric
    LANGUAGE sql
    AS $_$select sum(movs_importe) from movsto where movs_idastoh= $1 ;$_$;


ALTER FUNCTION public.subtotf(integer) OWNER TO rulemanes;

--
-- Name: sucuemi(integer); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.sucuemi(integer) RETURNS integer
    LANGUAGE sql
    AS $_$select emi_succli from emiti where emi_id = $1 ;$_$;


ALTER FUNCTION public.sucuemi(integer) OWNER TO rulemanes;

--
-- Name: sumchq(integer, integer); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.sumchq(integer, integer) RETURNS numeric
    LANGUAGE plpgsql
    AS $_$
declare
astc	 alias for $1;
modo	 alias for $2;
ret	 numeric;
begin

ret:=0;

if(int4eq(modo,1)) then
    select sum(chq_importe) into ret from cheques where chq_idastoc= astc;
else
    select sum(chq_importe) into ret from cheques where chq_idastoce= astc;
end if;

return ret;
end;$_$;


ALTER FUNCTION public.sumchq(integer, integer) OWNER TO rulemanes;

--
-- Name: taldecomp(integer, integer); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.taldecomp(integer, integer) RETURNS integer
    LANGUAGE sql
    AS $_$select tal_id from talon where tal_codemp = $1 and tal_comprob= $2;$_$;


ALTER FUNCTION public.taldecomp(integer, integer) OWNER TO rulemanes;

--
-- Name: tasaartic(integer); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.tasaartic(integer) RETURNS numeric
    LANGUAGE sql
    AS $_$select art_tasa from articvta where art_id= $1 ;$_$;


ALTER FUNCTION public.tasaartic(integer) OWNER TO rulemanes;

--
-- Name: tasaconiva(smallint); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.tasaconiva(smallint) RETURNS numeric
    LANGUAGE sql
    AS $_$select iva_tasa from coniva where iva_codigo= $1;$_$;


ALTER FUNCTION public.tasaconiva(smallint) OWNER TO rulemanes;

--
-- Name: tasaiva_fe(numeric); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.tasaiva_fe(numeric) RETURNS text
    LANGUAGE plpgsql
    AS $_$
    DECLARE
	imp		ALIAS FOR $1;
	inte		integer;
	retval		text;
    BEGIN
	if (numeric_eq(imp,0)) then
	    inte:=3;
	else
	    if (numeric_eq(imp,10.5)) then
		inte:=4;
	    else
		if (numeric_eq(imp,21)) then
		    inte:=5;
		else
		    if (numeric_eq(imp,27)) then
			inte:=6;
		    else
			if (numeric_eq(imp,5)) then
			    inte:=8;
			else
			    if (numeric_eq(imp,2.5)) then
				inte:=9;
			    end if;
			end if;
		    end if;
		end if;
	    end if;
	end if;
	select lpad(inte::text,4,'0') into retval;
	RETURN retval;
    END;$_$;


ALTER FUNCTION public.tasaiva_fe(numeric) OWNER TO rulemanes;

--
-- Name: tipctacbl(integer); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.tipctacbl(integer) RETURNS integer
    LANGUAGE sql
    AS $_$select cbl_tipcta from ctacble where cbl_id= $1 ;$_$;


ALTER FUNCTION public.tipctacbl(integer) OWNER TO rulemanes;

--
-- Name: tipprove(integer); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.tipprove(integer) RETURNS integer
    LANGUAGE sql
    AS $_$select prov_tipo from prove where prov_id= $1 ;$_$;


ALTER FUNCTION public.tipprove(integer) OWNER TO rulemanes;

--
-- Name: titastoh(integer); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.titastoh(integer) RETURNS character
    LANGUAGE sql
    AS $_$select ash_titulo from astoh where ash_id= $1 ;$_$;


ALTER FUNCTION public.titastoh(integer) OWNER TO rulemanes;

--
-- Name: tivacli(integer); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.tivacli(integer) RETURNS numeric
    LANGUAGE sql
    AS $_$select tasaconiva(cli_coniva) from clientes where cli_id= $1;$_$;


ALTER FUNCTION public.tivacli(integer) OWNER TO rulemanes;

--
-- Name: totdescto(integer); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.totdescto(integer) RETURNS numeric
    LANGUAGE sql
    AS $_$select sum(astc_importe) from astoctas where astc_idastoh= $1 and astc_ctacble=idcbl(astc_codemp,valvaramb(astc_codemp,astc_idptov,'CTADESCTO')::integer);$_$;


ALTER FUNCTION public.totdescto(integer) OWNER TO rulemanes;

--
-- Name: totdesctoa(integer); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.totdesctoa(integer) RETURNS numeric
    LANGUAGE sql
    AS $_$select sum(astc_importe) from astoctas where astc_idastoh= $1 and astc_ctacble=idcbl(astc_codemp,valvaramb(astc_codemp,1,'CTADESCTO')::integer);$_$;


ALTER FUNCTION public.totdesctoa(integer) OWNER TO rulemanes;

--
-- Name: totimpastc(integer, integer); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.totimpastc(integer, integer) RETURNS numeric
    LANGUAGE sql
    AS $_$select astc_importe from astoctas where astc_ctacble= $1 and astc_idastoh= $2 ;$_$;


ALTER FUNCTION public.totimpastc(integer, integer) OWNER TO rulemanes;

--
-- Name: totiva21(integer); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.totiva21(integer) RETURNS numeric
    LANGUAGE sql
    AS $_$select sum(astc_importe) from astoctas where astc_idastoh= $1 and astc_ctacble=idcbl(astc_codemp,valvaramb(astc_codemp,astc_idptov,'CTAIVA')::integer);$_$;


ALTER FUNCTION public.totiva21(integer) OWNER TO rulemanes;

--
-- Name: totiva21a(integer); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.totiva21a(integer) RETURNS numeric
    LANGUAGE sql
    AS $_$select sum(astc_importe) from astoctas where astc_idastoh= $1 and astc_ctacble=idcbl(astc_codemp,valvaramb(astc_codemp,1,'CTAIVA')::integer);$_$;


ALTER FUNCTION public.totiva21a(integer) OWNER TO rulemanes;

--
-- Name: totivast(integer); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.totivast(integer) RETURNS numeric
    LANGUAGE sql
    AS $_$select sum(astc_importe) from astoctas where astc_idastoh= $1 and astc_ctacble=idcbl(astc_codemp,valvaramb(astc_codemp,astc_idptov,'CTAIVAST')::integer);$_$;


ALTER FUNCTION public.totivast(integer) OWNER TO rulemanes;

--
-- Name: totivasta(integer); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.totivasta(integer) RETURNS numeric
    LANGUAGE sql
    AS $_$select sum(astc_importe) from astoctas where astc_idastoh= $1 and astc_ctacble=idcbl(astc_codemp,valvaramb(astc_codemp,1,'CTAIVAST')::integer);$_$;


ALTER FUNCTION public.totivasta(integer) OWNER TO rulemanes;

--
-- Name: tposastc(integer); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.tposastc(integer) RETURNS numeric
    LANGUAGE sql
    AS $_$select sum(astc_importe) from astoctas where astc_idastoh= $1 and astc_importe>0 ;$_$;


ALTER FUNCTION public.tposastc(integer) OWNER TO rulemanes;

--
-- Name: ult_cotiz(integer); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.ult_cotiz(integer) RETURNS numeric
    LANGUAGE sql
    AS $_$select cot_cotiz  from cotiza where cot_moneda = $1  order by cot_fecha desc limit 1;$_$;


ALTER FUNCTION public.ult_cotiz(integer) OWNER TO rulemanes;

--
-- Name: valvaramb(integer, integer, character varying); Type: FUNCTION; Schema: public; Owner: rulemanes
--

CREATE FUNCTION public.valvaramb(integer, integer, character varying) RETURNS character
    LANGUAGE plpgsql
    AS $_$
    DECLARE
	codemp		ALIAS FOR $1; 
	ptovta		ALIAS FOR $2; 
	codigo		ALIAS FOR $3; 
	otro 		varchar;
	retval 		varchar;
    BEGIN

    select vari_valor into otro from
	 varambi where int4eq(codemp,vari_codemp) and bpchareq('PTOVEMP',vari_codigo);


    select vari_valor into retval from
	 varambi where int4eq(ptovta,vari_idptov) and int4eq(codemp,vari_codemp) and bpchareq(codigo,vari_codigo);

    if not found then
	select vari_valor into retval from
	    varambi where int4eq(0,vari_idptov) and int4eq(codemp,vari_codemp) and bpchareq(codigo,vari_codigo);
	if not found then
	    select vari_valor into retval from
		varambi where int4eq(0,vari_idptov) and int4eq(0,vari_codemp) and bpchareq(codigo,vari_codigo);
	    if not found then
		select vari_valor into retval from
		    varambi where int4eq(otro::integer,vari_idptov) and int4eq(codemp,vari_codemp) and bpchareq(codigo,vari_codigo);
	    end if;
	end if;
    end if;
    RETURN retval;
    END;$_$;


ALTER FUNCTION public.valvaramb(integer, integer, character varying) OWNER TO rulemanes;



--
-- Name: agrupa; Type: TABLE; Schema: public; Owner: rulemanes
--

CREATE TABLE public.agrupa (
    ag_id integer NOT NULL,
    ag_idpago integer NOT NULL,
    ag_idastoh integer,
    ag_importe numeric(15,2),
    ag_imppago numeric(15,2) DEFAULT 0,
    ag_estado integer DEFAULT 1 NOT NULL,
    ag_leyenda text,
    ag_grupo integer,
    ag_retenido numeric(15,2) DEFAULT 0
);


ALTER TABLE public.agrupa OWNER TO rulemanes;

--
-- Name: agrupa_ag_id_seq; Type: SEQUENCE; Schema: public; Owner: rulemanes
--

CREATE SEQUENCE public.agrupa_ag_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


ALTER TABLE public.agrupa_ag_id_seq OWNER TO rulemanes;

--
-- Name: agrupa_ag_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: rulemanes
--

ALTER SEQUENCE public.agrupa_ag_id_seq OWNED BY public.agrupa.ag_id;


--
-- Name: art_aplicaciones; Type: TABLE; Schema: public; Owner: zona
--

CREATE TABLE public.art_aplicaciones (
    app_id integer NOT NULL,
    app_codemp integer NOT NULL,
    app_artid integer NOT NULL,
    app_marca_id integer,
    app_modelo_id integer,
    app_anio_desde integer,
    app_anio_hasta integer,
    app_posicion_id integer,
    app_notas text
);


ALTER TABLE public.art_aplicaciones OWNER TO zona;

--
-- Name: art_aplicaciones_app_id_seq; Type: SEQUENCE; Schema: public; Owner: zona
--

CREATE SEQUENCE public.art_aplicaciones_app_id_seq
    AS integer
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


ALTER TABLE public.art_aplicaciones_app_id_seq OWNER TO zona;

--
-- Name: art_aplicaciones_app_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: zona
--

ALTER SEQUENCE public.art_aplicaciones_app_id_seq OWNED BY public.art_aplicaciones.app_id;


--
-- Name: art_atrib_def_aad_id_seq; Type: SEQUENCE; Schema: public; Owner: zona
--

CREATE SEQUENCE public.art_atrib_def_aad_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


ALTER TABLE public.art_atrib_def_aad_id_seq OWNER TO zona;

--
-- Name: art_atrib_def; Type: TABLE; Schema: public; Owner: zona
--

CREATE TABLE public.art_atrib_def (
    aad_id integer DEFAULT nextval('public.art_atrib_def_aad_id_seq'::regclass) NOT NULL,
    aad_codemp integer NOT NULL,
    aad_rubro smallint,
    aad_nombre character varying(80) NOT NULL,
    aad_datatype smallint NOT NULL,
    aad_multival boolean DEFAULT false,
    aad_unidad character varying(20),
    aad_opciones jsonb,
    aad_activo smallint DEFAULT 1
);


ALTER TABLE public.art_atrib_def OWNER TO zona;

--
-- Name: art_atrib_val_aav_id_seq; Type: SEQUENCE; Schema: public; Owner: zona
--

CREATE SEQUENCE public.art_atrib_val_aav_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


ALTER TABLE public.art_atrib_val_aav_id_seq OWNER TO zona;

--
-- Name: art_atrib_val; Type: TABLE; Schema: public; Owner: zona
--

CREATE TABLE public.art_atrib_val (
    aav_id integer DEFAULT nextval('public.art_atrib_val_aav_id_seq'::regclass) NOT NULL,
    aav_artid integer NOT NULL,
    aav_defid integer NOT NULL,
    aav_val_text text,
    aav_val_num numeric(20,5),
    aav_val_bool smallint
);


ALTER TABLE public.art_atrib_val OWNER TO zona;

--
-- Name: art_atrib_valm_aavm_id_seq; Type: SEQUENCE; Schema: public; Owner: zona
--

CREATE SEQUENCE public.art_atrib_valm_aavm_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


ALTER TABLE public.art_atrib_valm_aavm_id_seq OWNER TO zona;

--
-- Name: art_atrib_valm; Type: TABLE; Schema: public; Owner: zona
--

CREATE TABLE public.art_atrib_valm (
    aavm_id integer DEFAULT nextval('public.art_atrib_valm_aavm_id_seq'::regclass) NOT NULL,
    aavm_artid integer NOT NULL,
    aavm_defid integer NOT NULL,
    aavm_value text NOT NULL
);


ALTER TABLE public.art_atrib_valm OWNER TO zona;

--
-- Name: art_tipo; Type: TABLE; Schema: public; Owner: zona
--

CREATE TABLE public.art_tipo (
    at_id smallint NOT NULL,
    at_nombre character varying(40) NOT NULL
);


ALTER TABLE public.art_tipo OWNER TO zona;

--
-- Name: articu; Type: TABLE; Schema: public; Owner: rulemanes
--

CREATE TABLE public.articu (
    artc_id integer NOT NULL,
    artc_codemp integer NOT NULL,
    artc_idptov integer,
    artc_codigo character varying(160),
    artc_tasa numeric(20,5) DEFAULT 21,
    artc_stock numeric(20,5) DEFAULT 0,
    artc_nivrep numeric(20,5) DEFAULT 0,
    artc_unimed smallint,
    artc_present character varying(50) DEFAULT 0,
    artc_peso numeric(20,5) DEFAULT 0,
    artc_estado smallint DEFAULT 1,
    artc_codrub smallint,
    artc_codsrb integer,
    artc_automatoper character varying(100),
    artc_nombre character varying(350),
    artc_leyen text,
    artc_tipo smallint DEFAULT 1,
    artc_codcbl integer,
    artc_funcion text,
    artc_precio numeric(20,5) DEFAULT 0,
    artc_idcbl integer,
    artc_barcode text,
    artc_marca integer
);


ALTER TABLE public.articu OWNER TO rulemanes;

--
-- Name: articu_artc_id_seq; Type: SEQUENCE; Schema: public; Owner: rulemanes
--

CREATE SEQUENCE public.articu_artc_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


ALTER TABLE public.articu_artc_id_seq OWNER TO rulemanes;

--
-- Name: articu_artc_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: rulemanes
--

ALTER SEQUENCE public.articu_artc_id_seq OWNED BY public.articu.artc_id;


--
-- Name: articvta; Type: TABLE; Schema: public; Owner: rulemanes
--

CREATE TABLE public.articvta (
    art_id integer NOT NULL,
    art_codemp integer NOT NULL,
    art_idptov integer,
    art_codigo character varying(160),
    art_tasa numeric(20,5) DEFAULT 21,
    art_stock numeric(20,5) DEFAULT 0,
    art_nivrep numeric(20,5) DEFAULT 0,
    art_unimed smallint,
    art_present character varying(50) DEFAULT 0,
    art_peso numeric(20,5) DEFAULT 0,
    art_estado smallint DEFAULT 1,
    art_codrub smallint,
    art_automatoper character varying(100),
    art_tipoper integer,
    art_precio_unique numeric(20,5) DEFAULT 0,
    art_nombre character varying(350),
    art_leyen text,
    art_siste smallint DEFAULT 1,
    art_tipo smallint DEFAULT 1,
    art_codsrb integer,
    art_codcbl integer,
    art_funcion text,
    art_precio numeric(20,5) DEFAULT 0,
    art_idcbl integer,
    art_barcode text,
    art_web smallint DEFAULT 0,
    art_marca integer,
    art_tipo_prod smallint
);


ALTER TABLE public.articvta OWNER TO rulemanes;

--
-- Name: articvta_art_id_seq; Type: SEQUENCE; Schema: public; Owner: rulemanes
--

CREATE SEQUENCE public.articvta_art_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


ALTER TABLE public.articvta_art_id_seq OWNER TO rulemanes;

--
-- Name: articvta_art_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: rulemanes
--

ALTER SEQUENCE public.articvta_art_id_seq OWNED BY public.articvta.art_id;


--
-- Name: astoctas; Type: TABLE; Schema: public; Owner: rulemanes
--

CREATE TABLE public.astoctas (
    astc_id integer NOT NULL,
    astc_idastoh integer NOT NULL,
    astc_codemp integer NOT NULL,
    astc_idptov integer,
    astc_iddiv integer,
    astc_idcco integer,
    astc_comprob integer NOT NULL,
    astc_numero integer NOT NULL,
    astc_fecval date NOT NULL,
    astc_ctacble integer NOT NULL,
    astc_moneda smallint DEFAULT 1 NOT NULL,
    astc_valor numeric(20,2) DEFAULT 0,
    astc_cotiza numeric(20,5) DEFAULT 0,
    astc_importe numeric(20,2) DEFAULT 0 NOT NULL,
    astc_tipcta integer,
    astc_ctacte integer,
    astc_nucheq integer DEFAULT 0,
    astc_fecvto date,
    astc_estado smallint DEFAULT 1 NOT NULL,
    astc_cierra smallint DEFAULT 1 NOT NULL,
    astc_grupo integer DEFAULT 1 NOT NULL,
    astc_tasa numeric(20,5),
    astc_leyen character varying(160),
    astc_idashce integer,
    astc_succli integer DEFAULT 1
);


ALTER TABLE public.astoctas OWNER TO rulemanes;

--
-- Name: astoctas_astc_id_seq; Type: SEQUENCE; Schema: public; Owner: rulemanes
--

CREATE SEQUENCE public.astoctas_astc_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


ALTER TABLE public.astoctas_astc_id_seq OWNER TO rulemanes;

--
-- Name: astoctas_astc_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: rulemanes
--

ALTER SEQUENCE public.astoctas_astc_id_seq OWNED BY public.astoctas.astc_id;


--
-- Name: astoh; Type: TABLE; Schema: public; Owner: rulemanes
--

CREATE TABLE public.astoh (
    ash_id integer NOT NULL,
    ash_codemp integer NOT NULL,
    ash_idptov integer,
    ash_anio smallint NOT NULL,
    ash_mes smallint NOT NULL,
    ash_comprob integer NOT NULL,
    ash_numero integer NOT NULL,
    ash_fecomp date NOT NULL,
    ash_fecval date,
    ash_fecreg timestamp without time zone DEFAULT now() NOT NULL,
    ash_estado smallint DEFAULT 1,
    ash_idcpra integer,
    ash_idemiti integer,
    ash_nro2 integer,
    ash_leyen text,
    ash_operad integer,
    ash_date date,
    ash_titulo character varying(200)
);


ALTER TABLE public.astoh OWNER TO rulemanes;

--
-- Name: astoh_ash_id_seq; Type: SEQUENCE; Schema: public; Owner: rulemanes
--

CREATE SEQUENCE public.astoh_ash_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


ALTER TABLE public.astoh_ash_id_seq OWNER TO rulemanes;

--
-- Name: astoh_ash_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: rulemanes
--

ALTER SEQUENCE public.astoh_ash_id_seq OWNED BY public.astoh.ash_id;


--
-- Name: auditoria_pedidos_audi_id_seq; Type: SEQUENCE; Schema: public; Owner: zona
--

CREATE SEQUENCE public.auditoria_pedidos_audi_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


ALTER TABLE public.auditoria_pedidos_audi_id_seq OWNER TO zona;

--
-- Name: auditoria_pedidos; Type: TABLE; Schema: public; Owner: zona
--

CREATE TABLE public.auditoria_pedidos (
    audi_id integer DEFAULT nextval('public.auditoria_pedidos_audi_id_seq'::regclass) NOT NULL,
    audi_pedido integer NOT NULL,
    audi_accion character varying(50) NOT NULL,
    audi_usuario integer NOT NULL,
    audi_fecha timestamp without time zone DEFAULT now(),
    audi_observaciones text
);


ALTER TABLE public.auditoria_pedidos OWNER TO zona;

--
-- Name: auxfe; Type: TABLE; Schema: public; Owner: zona
--

CREATE TABLE public.auxfe (
    fe_id integer NOT NULL,
    fe_idcli integer NOT NULL,
    fe_cuit character varying(11) NOT NULL,
    fe_cbte smallint DEFAULT 1 NOT NULL,
    fe_ptovta smallint DEFAULT 1 NOT NULL,
    fe_nograv numeric(15,2),
    fe_grav numeric(15,2),
    fe_ivatot numeric(15,2),
    fe_total numeric(15,2),
    fe_tributo numeric(15,2),
    fe_ivaex numeric(15,2),
    fe_iva21 numeric(15,2),
    fe_iva105 numeric(15,2),
    fe_fechaf character varying(8),
    fe_iniserv character varying(8),
    fe_finserv character varying(8),
    fe_fechavp character varying(8),
    fe_cae character varying(14),
    fe_fechavtocae character varying(8),
    fe_numero integer,
    fe_estado integer DEFAULT 1 NOT NULL,
    fe_errores text,
    fe_codemp integer DEFAULT 1 NOT NULL,
    fe_observ text,
    fe_moneda integer DEFAULT 1,
    fe_tc numeric DEFAULT 1
);


ALTER TABLE public.auxfe OWNER TO zona;

--
-- Name: auxfe_fe_id_seq; Type: SEQUENCE; Schema: public; Owner: zona
--

CREATE SEQUENCE public.auxfe_fe_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


ALTER TABLE public.auxfe_fe_id_seq OWNER TO zona;

--
-- Name: auxfe_fe_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: zona
--

ALTER SEQUENCE public.auxfe_fe_id_seq OWNED BY public.auxfe.fe_id;


--
-- Name: bancos; Type: TABLE; Schema: public; Owner: rulemanes
--

CREATE TABLE public.bancos (
    bco_codigo smallint NOT NULL,
    bco_nombre character varying(160) NOT NULL,
    bco_estado smallint DEFAULT 1
);


ALTER TABLE public.bancos OWNER TO rulemanes;

--
-- Name: carritos_cliente_id_seq; Type: SEQUENCE; Schema: public; Owner: zona
--

CREATE SEQUENCE public.carritos_cliente_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


ALTER TABLE public.carritos_cliente_id_seq OWNER TO zona;

--
-- Name: carritos_cliente; Type: TABLE; Schema: public; Owner: zona
--

CREATE TABLE public.carritos_cliente (
    id integer DEFAULT nextval('public.carritos_cliente_id_seq'::regclass) NOT NULL,
    cliente_id integer NOT NULL,
    carrito_data text,
    fecha_creacion timestamp without time zone DEFAULT now(),
    fecha_actualizacion timestamp without time zone DEFAULT now()
);


ALTER TABLE public.carritos_cliente OWNER TO zona;

--
-- Name: cblexasi; Type: TABLE; Schema: public; Owner: rulemanes
--

CREATE TABLE public.cblexasi (
    cxa_codemp integer NOT NULL,
    cxa_tipasi integer NOT NULL,
    cxa_cble integer NOT NULL
);


ALTER TABLE public.cblexasi OWNER TO rulemanes;

--
-- Name: cbteasoc; Type: TABLE; Schema: public; Owner: rulemanes
--

CREATE TABLE public.cbteasoc (
    cb_id integer NOT NULL,
    cb_idfc integer NOT NULL,
    cb_idnc integer NOT NULL,
    cb_fecreg timestamp without time zone DEFAULT now() NOT NULL,
    cb_estado integer DEFAULT 1,
    cb_anul integer DEFAULT 1 NOT NULL
);


ALTER TABLE public.cbteasoc OWNER TO rulemanes;

--
-- Name: cbteasoc_cb_id_seq; Type: SEQUENCE; Schema: public; Owner: rulemanes
--

CREATE SEQUENCE public.cbteasoc_cb_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


ALTER TABLE public.cbteasoc_cb_id_seq OWNER TO rulemanes;

--
-- Name: cbteasoc_cb_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: rulemanes
--

ALTER SEQUENCE public.cbteasoc_cb_id_seq OWNED BY public.cbteasoc.cb_id;


--
-- Name: chat; Type: TABLE; Schema: public; Owner: zona
--

CREATE TABLE public.chat (
    c_id integer NOT NULL,
    c_codemp integer NOT NULL,
    c_message text NOT NULL,
    c_sent timestamp without time zone DEFAULT now() NOT NULL,
    c_recd integer DEFAULT 0 NOT NULL,
    c_from integer,
    c_to integer
);


ALTER TABLE public.chat OWNER TO zona;

--
-- Name: chat_c_id_seq; Type: SEQUENCE; Schema: public; Owner: zona
--

CREATE SEQUENCE public.chat_c_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


ALTER TABLE public.chat_c_id_seq OWNER TO zona;

--
-- Name: chat_c_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: zona
--

ALTER SEQUENCE public.chat_c_id_seq OWNED BY public.chat.c_id;


--
-- Name: cheques; Type: TABLE; Schema: public; Owner: rulemanes
--

CREATE TABLE public.cheques (
    chq_id integer NOT NULL,
    chq_codbco smallint NOT NULL,
    chq_nucheq integer NOT NULL,
    chq_codemp integer NOT NULL,
    chq_idptov integer,
    chq_idastoc integer NOT NULL,
    chq_fechaem date,
    chq_fecheq date NOT NULL,
    chq_moneda smallint DEFAULT 1 NOT NULL,
    chq_importe numeric(20,2) DEFAULT 0 NOT NULL,
    chq_estado smallint DEFAULT 1 NOT NULL,
    chq_nrcuit character varying(20),
    chq_ctacte integer,
    chq_idastoce integer,
    chq_observ text,
    chq_echq integer
);


ALTER TABLE public.cheques OWNER TO rulemanes;

--
-- Name: cheques_chq_id_seq; Type: SEQUENCE; Schema: public; Owner: rulemanes
--

CREATE SEQUENCE public.cheques_chq_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


ALTER TABLE public.cheques_chq_id_seq OWNER TO rulemanes;

--
-- Name: cheques_chq_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: rulemanes
--

ALTER SEQUENCE public.cheques_chq_id_seq OWNED BY public.cheques.chq_id;


--
-- Name: clientes; Type: TABLE; Schema: public; Owner: rulemanes
--

CREATE TABLE public.clientes (
    cli_id integer NOT NULL,
    cli_codemp integer NOT NULL,
    cli_idptov integer,
    cli_codigo character varying(40) NOT NULL,
    cli_nrcuit character varying(20) NOT NULL,
    cli_tipcli smallint,
    cli_nombre character varying(160),
    cli_fantasy character varying(160),
    cli_nomabre character varying(40),
    cli_coniva smallint NOT NULL,
    cli_descto numeric(20,5) DEFAULT 0,
    cli_conpag integer,
    cli_catego integer,
    cli_limcred numeric(20,2) DEFAULT 0,
    cli_interes numeric(20,5) DEFAULT 0,
    cli_grupo integer,
    cli_saldo numeric(20,2),
    cli_estado smallint DEFAULT 1 NOT NULL,
    cli_modfac smallint DEFAULT 1,
    cli_listpre integer DEFAULT 1,
    cli_comentarios text,
    cli_art integer DEFAULT 1,
    cli_perciva integer DEFAULT 1 NOT NULL,
    cli_rubro integer DEFAULT 7,
    cli_vendedor integer
);


ALTER TABLE public.clientes OWNER TO rulemanes;

--
-- Name: clientes_cli_id_seq; Type: SEQUENCE; Schema: public; Owner: rulemanes
--

CREATE SEQUENCE public.clientes_cli_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


ALTER TABLE public.clientes_cli_id_seq OWNER TO rulemanes;

--
-- Name: clientes_cli_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: rulemanes
--

ALTER SEQUENCE public.clientes_cli_id_seq OWNED BY public.clientes.cli_id;


--
-- Name: compra; Type: TABLE; Schema: public; Owner: rulemanes
--

CREATE TABLE public.compra (
    cpra_id integer NOT NULL,
    cpra_idastoh integer,
    cpra_codemp integer NOT NULL,
    cpra_idptov integer,
    cpra_comprob integer NOT NULL,
    cpra_numero integer NOT NULL,
    cpra_prove integer,
    cpra_lecomp character varying(2),
    cpra_sucomp smallint,
    cpra_nucomp integer,
    cpra_fecori date,
    cpra_fecval date,
    cpra_fecvto date,
    cpra_estado smallint DEFAULT 1 NOT NULL,
    cpra_nomprov character varying(160),
    cpra_nrcuit character varying(20),
    cpra_coniva smallint,
    cpra_total numeric(20,2) DEFAULT 0,
    cpra_stock integer DEFAULT 0,
    cpra_obs text
);


ALTER TABLE public.compra OWNER TO rulemanes;

--
-- Name: compra_cpra_id_seq; Type: SEQUENCE; Schema: public; Owner: rulemanes
--

CREATE SEQUENCE public.compra_cpra_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


ALTER TABLE public.compra_cpra_id_seq OWNER TO rulemanes;

--
-- Name: compra_cpra_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: rulemanes
--

ALTER SEQUENCE public.compra_cpra_id_seq OWNED BY public.compra.cpra_id;


--
-- Name: comprob; Type: TABLE; Schema: public; Owner: rulemanes
--

CREATE TABLE public.comprob (
    comp_id integer NOT NULL,
    comp_codemp integer NOT NULL,
    comp_idptov integer,
    comp_codigo character(10) NOT NULL,
    comp_nombre character varying(160) NOT NULL,
    comp_sistem smallint DEFAULT 1,
    comp_siste smallint DEFAULT 4,
    comp_credeb smallint DEFAULT 1,
    comp_stcksn smallint DEFAULT 1,
    comp_tipo smallint DEFAULT 1,
    comp_intext smallint DEFAULT 1,
    comp_cuenta integer,
    comp_talon integer,
    comp_copias integer,
    comp_letra character varying(3),
    comp_leyen text,
    comp_orden integer
);


ALTER TABLE public.comprob OWNER TO rulemanes;

--
-- Name: comprob_comp_id_seq; Type: SEQUENCE; Schema: public; Owner: rulemanes
--

CREATE SEQUENCE public.comprob_comp_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


ALTER TABLE public.comprob_comp_id_seq OWNER TO rulemanes;

--
-- Name: comprob_comp_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: rulemanes
--

ALTER SEQUENCE public.comprob_comp_id_seq OWNED BY public.comprob.comp_id;


--
-- Name: concilia; Type: TABLE; Schema: public; Owner: rulemanes
--

CREATE TABLE public.concilia (
    clia_id integer NOT NULL,
    clia_cte integer NOT NULL,
    clia_fecha timestamp without time zone DEFAULT now() NOT NULL,
    clia_idastcg integer,
    clia_salant numeric(20,5) DEFAULT 0,
    clia_salcon numeric(20,5) DEFAULT 0,
    clia_grav numeric(20,5) DEFAULT 0,
    clia_iva numeric(20,5) DEFAULT 0,
    clia_ngrab numeric(20,5) DEFAULT 0,
    clia_piibb numeric(20,5) DEFAULT 0,
    clia_sircreb numeric(20,5) DEFAULT 0,
    clia_leyd numeric(20,5) DEFAULT 0,
    clia_leyc numeric(20,5) DEFAULT 0,
    clia_operad integer NOT NULL,
    clia_estado integer DEFAULT 1
);


ALTER TABLE public.concilia OWNER TO rulemanes;

--
-- Name: concilia_clia_id_seq; Type: SEQUENCE; Schema: public; Owner: rulemanes
--

CREATE SEQUENCE public.concilia_clia_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


ALTER TABLE public.concilia_clia_id_seq OWNER TO rulemanes;

--
-- Name: concilia_clia_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: rulemanes
--

ALTER SEQUENCE public.concilia_clia_id_seq OWNED BY public.concilia.clia_id;


--
-- Name: coniva; Type: TABLE; Schema: public; Owner: rulemanes
--

CREATE TABLE public.coniva (
    iva_codigo smallint NOT NULL,
    iva_nombre character varying(160) NOT NULL,
    iva_tasa numeric(20,5) DEFAULT 0,
    iva_stasa numeric(20,5) DEFAULT 0,
    iva_discrim smallint DEFAULT 1,
    iva_estado integer DEFAULT 1,
    iva_afip integer
);


ALTER TABLE public.coniva OWNER TO rulemanes;

--
-- Name: conpag; Type: TABLE; Schema: public; Owner: rulemanes
--

CREATE TABLE public.conpag (
    cdp_codigo integer NOT NULL,
    cdp_codemp integer NOT NULL,
    cdp_nombre character varying(160) NOT NULL,
    cdp_dias smallint
);


ALTER TABLE public.conpag OWNER TO rulemanes;

--
-- Name: conpag_cdp_codigo_seq; Type: SEQUENCE; Schema: public; Owner: rulemanes
--

CREATE SEQUENCE public.conpag_cdp_codigo_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


ALTER TABLE public.conpag_cdp_codigo_seq OWNER TO rulemanes;

--
-- Name: conpag_cdp_codigo_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: rulemanes
--

ALTER SEQUENCE public.conpag_cdp_codigo_seq OWNED BY public.conpag.cdp_codigo;


--
-- Name: contratos; Type: TABLE; Schema: public; Owner: rulemanes
--

CREATE TABLE public.contratos (
    ctr_id integer NOT NULL,
    ctr_codemp integer NOT NULL,
    ctr_idptov integer NOT NULL,
    ctr_idcli integer NOT NULL,
    ctr_fecha date DEFAULT date(now()) NOT NULL,
    ctr_idastoh integer,
    ctr_comprob integer,
    ctr_numero integer,
    ctr_fecvto date,
    ctr_estado integer DEFAULT 1 NOT NULL,
    ctr_leyenda text
);


ALTER TABLE public.contratos OWNER TO rulemanes;

--
-- Name: contratos_ctr_id_seq; Type: SEQUENCE; Schema: public; Owner: rulemanes
--

CREATE SEQUENCE public.contratos_ctr_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


ALTER TABLE public.contratos_ctr_id_seq OWNER TO rulemanes;

--
-- Name: contratos_ctr_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: rulemanes
--

ALTER SEQUENCE public.contratos_ctr_id_seq OWNED BY public.contratos.ctr_id;


--
-- Name: contrb; Type: TABLE; Schema: public; Owner: rulemanes
--

CREATE TABLE public.contrb (
    ctrb_id integer NOT NULL,
    ctrb_idcontr integer NOT NULL,
    ctrb_idcli integer NOT NULL,
    ctrb_idartic integer,
    ctrb_iddispen integer NOT NULL,
    ctrb_fecha date DEFAULT date(now()) NOT NULL,
    ctrb_idastoh integer,
    ctrb_comprob integer,
    ctrb_numero integer,
    ctrb_fecvto date,
    ctrb_estado integer DEFAULT 1 NOT NULL,
    ctrb_oldcontr integer
);


ALTER TABLE public.contrb OWNER TO rulemanes;

--
-- Name: contrb_ctrb_id_seq; Type: SEQUENCE; Schema: public; Owner: rulemanes
--

CREATE SEQUENCE public.contrb_ctrb_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


ALTER TABLE public.contrb_ctrb_id_seq OWNER TO rulemanes;

--
-- Name: contrb_ctrb_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: rulemanes
--

ALTER SEQUENCE public.contrb_ctrb_id_seq OWNED BY public.contrb.ctrb_id;


--
-- Name: convmult; Type: TABLE; Schema: public; Owner: zona
--

CREATE TABLE public.convmult (
    cmt_codemp integer NOT NULL,
    cmt_provin integer NOT NULL,
    cmt_tasa numeric(15,5) DEFAULT 0 NOT NULL,
    cmt_coef numeric(15,5) DEFAULT 0
);


ALTER TABLE public.convmult OWNER TO zona;

--
-- Name: conxclia; Type: TABLE; Schema: public; Owner: rulemanes
--

CREATE TABLE public.conxclia (
    cxlia_id integer NOT NULL,
    cxlia_idclia integer NOT NULL,
    cxlia_idastc integer NOT NULL,
    cxlia_importe numeric(20,5) DEFAULT 0,
    cxlia_estado integer DEFAULT 1
);


ALTER TABLE public.conxclia OWNER TO rulemanes;

--
-- Name: conxcxlia_cxlia_id_seq; Type: SEQUENCE; Schema: public; Owner: rulemanes
--

CREATE SEQUENCE public.conxcxlia_cxlia_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


ALTER TABLE public.conxcxlia_cxlia_id_seq OWNER TO rulemanes;

--
-- Name: conxcxlia_cxlia_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: rulemanes
--

ALTER SEQUENCE public.conxcxlia_cxlia_id_seq OWNED BY public.conxclia.cxlia_id;


--
-- Name: cotiza; Type: TABLE; Schema: public; Owner: zona
--

CREATE TABLE public.cotiza (
    cot_codemp integer DEFAULT '-1'::integer NOT NULL,
    cot_moneda smallint NOT NULL,
    cot_fecha date NOT NULL,
    cot_cotiz numeric(20,6) DEFAULT 1 NOT NULL
);


ALTER TABLE public.cotiza OWNER TO zona;

--
-- Name: cta_nivel_id_seq; Type: SEQUENCE; Schema: public; Owner: zona
--

CREATE SEQUENCE public.cta_nivel_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


ALTER TABLE public.cta_nivel_id_seq OWNER TO zona;

--
-- Name: cta_nivel; Type: TABLE; Schema: public; Owner: zona
--

CREATE TABLE public.cta_nivel (
    cn_codemp integer NOT NULL,
    cn_id integer DEFAULT nextval('public.cta_nivel_id_seq'::regclass) NOT NULL,
    cn_nombre character varying(160) NOT NULL,
    cn_codfrag character varying(16),
    cn_padre integer,
    cn_nivel smallint DEFAULT 1 NOT NULL
);


ALTER TABLE public.cta_nivel OWNER TO zona;

--
-- Name: ctacble; Type: TABLE; Schema: public; Owner: rulemanes
--

CREATE TABLE public.ctacble (
    cbl_id integer NOT NULL,
    cbl_codemp integer,
    cbl_codigo character varying(60) NOT NULL,
    cbl_nombre character varying(160) NOT NULL,
    cbl_identi integer,
    cbl_nivel integer,
    cbl_posic integer,
    cbl_asigna integer DEFAULT 1,
    cbl_moneda smallint DEFAULT 1 NOT NULL,
    cbl_tipcta integer,
    cbl_monetaria smallint DEFAULT 1,
    cbl_ajuste smallint DEFAULT 1,
    cbl_indice smallint DEFAULT 1,
    cbl_ctarei integer,
    cbl_ctaaju integer,
    cbl_leyed character varying(160),
    cbl_leyeh character varying(160),
    cbl_present character varying(60)
);


ALTER TABLE public.ctacble OWNER TO rulemanes;

--
-- Name: ctacble_cbl_id_seq; Type: SEQUENCE; Schema: public; Owner: rulemanes
--

CREATE SEQUENCE public.ctacble_cbl_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


ALTER TABLE public.ctacble_cbl_id_seq OWNER TO rulemanes;

--
-- Name: ctacble_cbl_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: rulemanes
--

ALTER SEQUENCE public.ctacble_cbl_id_seq OWNED BY public.ctacble.cbl_id;


--
-- Name: ctes; Type: TABLE; Schema: public; Owner: rulemanes
--

CREATE TABLE public.ctes (
    cte_id integer NOT NULL,
    cte_codemp integer NOT NULL,
    cte_tipcta integer NOT NULL,
    cte_tipct smallint,
    cte_codigo integer NOT NULL,
    cte_nombre character varying(160),
    cte_idtal integer,
    cte_estado smallint DEFAULT 1,
    cte_idptov integer DEFAULT '-1'::integer
);


ALTER TABLE public.ctes OWNER TO rulemanes;

--
-- Name: ctes_cte_id_seq; Type: SEQUENCE; Schema: public; Owner: rulemanes
--

CREATE SEQUENCE public.ctes_cte_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


ALTER TABLE public.ctes_cte_id_seq OWNER TO rulemanes;

--
-- Name: ctes_cte_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: rulemanes
--

ALTER SEQUENCE public.ctes_cte_id_seq OWNED BY public.ctes.cte_id;


--
-- Name: debitos; Type: TABLE; Schema: public; Owner: rulemanes
--

CREATE TABLE public.debitos (
    db_id integer NOT NULL,
    db_idastoh integer,
    db_operador integer,
    db_estado integer DEFAULT 1,
    db_idpago integer
);


ALTER TABLE public.debitos OWNER TO rulemanes;

--
-- Name: debitos_db_id_seq; Type: SEQUENCE; Schema: public; Owner: rulemanes
--

CREATE SEQUENCE public.debitos_db_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


ALTER TABLE public.debitos_db_id_seq OWNER TO rulemanes;

--
-- Name: debitos_db_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: rulemanes
--

ALTER SEQUENCE public.debitos_db_id_seq OWNED BY public.debitos.db_id;


--
-- Name: descuentos_rubro; Type: TABLE; Schema: public; Owner: zona
--

CREATE TABLE public.descuentos_rubro (
    dr_id integer NOT NULL,
    dr_cliente integer NOT NULL,
    dr_rubro integer NOT NULL,
    dr_descuento numeric(5,2) NOT NULL,
    dr_codemp integer NOT NULL
);


ALTER TABLE public.descuentos_rubro OWNER TO zona;

--
-- Name: descuentos_rubro_dr_id_seq; Type: SEQUENCE; Schema: public; Owner: zona
--

CREATE SEQUENCE public.descuentos_rubro_dr_id_seq
    AS integer
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


ALTER TABLE public.descuentos_rubro_dr_id_seq OWNER TO zona;

--
-- Name: descuentos_rubro_dr_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: zona
--

ALTER SEQUENCE public.descuentos_rubro_dr_id_seq OWNED BY public.descuentos_rubro.dr_id;


--
-- Name: deudas; Type: TABLE; Schema: public; Owner: rulemanes
--

CREATE TABLE public.deudas (
    dd_id integer NOT NULL,
    dd_mes integer NOT NULL,
    dd_anio integer NOT NULL,
    dd_cliente integer,
    dd_importe numeric(15,2),
    dd_estado integer DEFAULT 1,
    dd_codemp integer NOT NULL
);


ALTER TABLE public.deudas OWNER TO rulemanes;

--
-- Name: deudas_dd_id_seq; Type: SEQUENCE; Schema: public; Owner: rulemanes
--

CREATE SEQUENCE public.deudas_dd_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


ALTER TABLE public.deudas_dd_id_seq OWNER TO rulemanes;

--
-- Name: deudas_dd_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: rulemanes
--

ALTER SEQUENCE public.deudas_dd_id_seq OWNED BY public.deudas.dd_id;


--
-- Name: dispenser; Type: TABLE; Schema: public; Owner: rulemanes
--

CREATE TABLE public.dispenser (
    dis_id integer NOT NULL,
    dis_idprov integer,
    dis_codemp integer NOT NULL,
    dis_idptov integer NOT NULL,
    dis_idartic integer,
    dis_idcontr integer,
    dis_idcontrb integer DEFAULT '-1'::integer,
    dis_fecha date DEFAULT date(now()) NOT NULL,
    dis_idastoh integer,
    dis_comprob integer,
    dis_numero integer,
    dis_fecvto date,
    dis_estado integer DEFAULT 1 NOT NULL,
    dis_leyen text
);


ALTER TABLE public.dispenser OWNER TO rulemanes;

--
-- Name: dispenser_dis_id_seq; Type: SEQUENCE; Schema: public; Owner: rulemanes
--

CREATE SEQUENCE public.dispenser_dis_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


ALTER TABLE public.dispenser_dis_id_seq OWNER TO rulemanes;

--
-- Name: dispenser_dis_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: rulemanes
--

ALTER SEQUENCE public.dispenser_dis_id_seq OWNED BY public.dispenser.dis_id;


--
-- Name: docum; Type: TABLE; Schema: public; Owner: rulemanes
--

CREATE TABLE public.docum (
    doc_id integer NOT NULL,
    doc_codemp integer NOT NULL,
    doc_idptov integer,
    doc_idastoc integer NOT NULL,
    doc_numero integer NOT NULL,
    doc_fechaem date,
    doc_fecdoc date NOT NULL,
    doc_moneda smallint DEFAULT 1 NOT NULL,
    doc_importe numeric(20,2) DEFAULT 0 NOT NULL,
    doc_estado smallint DEFAULT 1 NOT NULL,
    doc_nrcuit character varying(20),
    doc_ctacte integer,
    doc_idastoce integer,
    doc_observ text
);


ALTER TABLE public.docum OWNER TO rulemanes;

--
-- Name: docum_doc_id_seq; Type: SEQUENCE; Schema: public; Owner: rulemanes
--

CREATE SEQUENCE public.docum_doc_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


ALTER TABLE public.docum_doc_id_seq OWNER TO rulemanes;

--
-- Name: docum_doc_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: rulemanes
--

ALTER SEQUENCE public.docum_doc_id_seq OWNED BY public.docum.doc_id;


--
-- Name: emiti; Type: TABLE; Schema: public; Owner: rulemanes
--

CREATE TABLE public.emiti (
    emi_id integer NOT NULL,
    emi_idastoh integer,
    emi_codemp integer NOT NULL,
    emi_idptov integer,
    emi_comprob integer NOT NULL,
    emi_numero integer NOT NULL,
    emi_fecval date,
    emi_grupo integer,
    emi_conpag integer,
    emi_codcli integer,
    emi_succli integer,
    emi_nomcli character varying(160),
    emi_conivac smallint,
    emi_nrcuit character varying(20),
    emi_vended integer,
    emi_provin integer,
    emi_elm integer,
    emi_desre numeric(20,2) DEFAULT 0,
    emi_total numeric(20,2) DEFAULT 0,
    emi_retiro integer DEFAULT 1,
    emi_estado smallint DEFAULT 1,
    emi_leyen text,
    emi_idfe integer,
    emi_cae character varying(14),
    emi_facturado integer DEFAULT 1,
    emi_idoper integer,
    emi_stock integer DEFAULT 1,
    emi_ordencompra character varying(40),
    emi_descrip text,
    emi_carrito text
);


ALTER TABLE public.emiti OWNER TO rulemanes;

--
-- Name: emiti_emi_id_seq; Type: SEQUENCE; Schema: public; Owner: rulemanes
--

CREATE SEQUENCE public.emiti_emi_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


ALTER TABLE public.emiti_emi_id_seq OWNER TO rulemanes;

--
-- Name: emiti_emi_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: rulemanes
--

ALTER SEQUENCE public.emiti_emi_id_seq OWNED BY public.emiti.emi_id;


--
-- Name: empresa; Type: TABLE; Schema: public; Owner: rulemanes
--

CREATE TABLE public.empresa (
    emp_codigo integer NOT NULL,
    emp_nombre character varying(160),
    emp_direcc character varying(160),
    emp_locali character varying(160),
    emp_codpos character varying(10),
    emp_provin integer DEFAULT 1,
    emp_pais smallint DEFAULT 200,
    emp_nrcuit character varying(20) NOT NULL,
    emp_estado smallint DEFAULT 1,
    emp_codact character varying(60),
    emp_descact character varying(160),
    emp_respons character varying(160),
    emp_respcargo character varying(160),
    emp_nroanses character varying(60),
    emp_nroigj character varying(60),
    emp_fecigj date,
    emp_vtocont date DEFAULT '2028-12-31'::date,
    emp_tipo smallint DEFAULT 1,
    emp_coniva smallint DEFAULT 1,
    emp_anti date DEFAULT date(now()),
    emp_fecint date,
    emp_grupo integer DEFAULT 1,
    emp_redond smallint DEFAULT 0,
    emp_nomabr character varying(10),
    emp_codcli integer DEFAULT 1,
    emp_codprov integer DEFAULT 1,
    emp_pwdanul text,
    emp_color_primary character varying(16) DEFAULT '#209C1C'::character varying,
    emp_color_primary_contrast character varying(16) DEFAULT '#ffffff'::character varying,
    emp_color_sidebar_bg character varying(16) DEFAULT '#145214'::character varying,
    emp_color_sidebar_text character varying(16) DEFAULT '#ffffff'::character varying,
    emp_color_link character varying(16) DEFAULT '#0d6efd'::character varying
);


ALTER TABLE public.empresa OWNER TO rulemanes;

--
-- Name: estados_reclamos; Type: TABLE; Schema: public; Owner: zona
--

CREATE TABLE public.estados_reclamos (
    est_id integer NOT NULL,
    est_nombre character varying(50) NOT NULL,
    est_color character varying(20) DEFAULT '#6c757d'::character varying,
    est_orden integer DEFAULT 0,
    est_activo boolean DEFAULT true,
    est_codemp integer NOT NULL,
    created_at timestamp without time zone DEFAULT CURRENT_TIMESTAMP
);


ALTER TABLE public.estados_reclamos OWNER TO zona;

--
-- Name: estados_reclamos_est_id_seq; Type: SEQUENCE; Schema: public; Owner: zona
--

CREATE SEQUENCE public.estados_reclamos_est_id_seq
    AS integer
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


ALTER TABLE public.estados_reclamos_est_id_seq OWNER TO zona;

--
-- Name: estados_reclamos_est_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: zona
--

ALTER SEQUENCE public.estados_reclamos_est_id_seq OWNED BY public.estados_reclamos.est_id;


--
-- Name: estcivil; Type: TABLE; Schema: public; Owner: rulemanes
--

CREATE TABLE public.estcivil (
    ec_codigo smallint NOT NULL,
    ec_nombre character varying(160) NOT NULL
);


ALTER TABLE public.estcivil OWNER TO rulemanes;

--
-- Name: fact_vencidas; Type: TABLE; Schema: public; Owner: rulemanes
--

CREATE TABLE public.fact_vencidas (
    fv_id integer NOT NULL,
    fv_idastoh integer NOT NULL,
    fv_fecreg timestamp without time zone DEFAULT now() NOT NULL,
    fv_estado integer DEFAULT 1,
    fv_cliente integer
);


ALTER TABLE public.fact_vencidas OWNER TO rulemanes;

--
-- Name: fact_vencidas_fv_id_seq; Type: SEQUENCE; Schema: public; Owner: rulemanes
--

CREATE SEQUENCE public.fact_vencidas_fv_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


ALTER TABLE public.fact_vencidas_fv_id_seq OWNER TO rulemanes;

--
-- Name: fact_vencidas_fv_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: rulemanes
--

ALTER SEQUENCE public.fact_vencidas_fv_id_seq OWNED BY public.fact_vencidas.fv_id;


--
-- Name: firman; Type: TABLE; Schema: public; Owner: rulemanes
--

CREATE TABLE public.firman (
    fir_id integer NOT NULL,
    fir_codemp integer NOT NULL,
    fir_nrcuit character varying(20) NOT NULL,
    fir_nombre character varying(160) NOT NULL,
    fir_direcc character varying(160),
    fir_locali character varying(160),
    fir_codpos character varying(10),
    fir_provin integer,
    fir_pais smallint,
    fir_estado smallint DEFAULT 1,
    fir_observ text
);


ALTER TABLE public.firman OWNER TO rulemanes;

--
-- Name: firman_fir_id_seq; Type: SEQUENCE; Schema: public; Owner: rulemanes
--

CREATE SEQUENCE public.firman_fir_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


ALTER TABLE public.firman_fir_id_seq OWNER TO rulemanes;

--
-- Name: firman_fir_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: rulemanes
--

ALTER SEQUENCE public.firman_fir_id_seq OWNED BY public.firman.fir_id;


--
-- Name: grupos; Type: TABLE; Schema: public; Owner: rulemanes
--

CREATE TABLE public.grupos (
    gpo_id integer NOT NULL,
    gpo_nombre character varying(160) NOT NULL
);


ALTER TABLE public.grupos OWNER TO rulemanes;

--
-- Name: grupos_gpo_id_seq; Type: SEQUENCE; Schema: public; Owner: rulemanes
--

CREATE SEQUENCE public.grupos_gpo_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


ALTER TABLE public.grupos_gpo_id_seq OWNER TO rulemanes;

--
-- Name: grupos_gpo_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: rulemanes
--

ALTER SEQUENCE public.grupos_gpo_id_seq OWNED BY public.grupos.gpo_id;


--
-- Name: hojaruta; Type: TABLE; Schema: public; Owner: rulemanes
--

CREATE TABLE public.hojaruta (
    hr_id integer NOT NULL,
    hr_fecha date DEFAULT now(),
    hr_chofer character varying(80),
    hr_camion integer,
    hr_controla character varying(80),
    hr_operador integer,
    hr_estado integer DEFAULT 1,
    hr_fecreg timestamp without time zone DEFAULT now()
);


ALTER TABLE public.hojaruta OWNER TO rulemanes;

--
-- Name: hojaruta_hr_id_seq; Type: SEQUENCE; Schema: public; Owner: rulemanes
--

CREATE SEQUENCE public.hojaruta_hr_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


ALTER TABLE public.hojaruta_hr_id_seq OWNER TO rulemanes;

--
-- Name: hojaruta_hr_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: rulemanes
--

ALTER SEQUENCE public.hojaruta_hr_id_seq OWNED BY public.hojaruta.hr_id;


--
-- Name: iibbfile; Type: TABLE; Schema: public; Owner: rulemanes
--

CREATE TABLE public.iibbfile (
    if_id integer NOT NULL,
    if_mes integer NOT NULL,
    if_anio integer NOT NULL,
    if_url text NOT NULL,
    if_estado integer DEFAULT 1 NOT NULL
);


ALTER TABLE public.iibbfile OWNER TO rulemanes;

--
-- Name: iibbfile_if_id_seq; Type: SEQUENCE; Schema: public; Owner: rulemanes
--

CREATE SEQUENCE public.iibbfile_if_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


ALTER TABLE public.iibbfile_if_id_seq OWNER TO rulemanes;

--
-- Name: iibbfile_if_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: rulemanes
--

ALTER SEQUENCE public.iibbfile_if_id_seq OWNED BY public.iibbfile.if_id;


--
-- Name: indices; Type: TABLE; Schema: public; Owner: rulemanes
--

CREATE TABLE public.indices (
    ind_codigo smallint NOT NULL,
    ind_nombre character varying(160) NOT NULL
);


ALTER TABLE public.indices OWNER TO rulemanes;

--
-- Name: interes; Type: TABLE; Schema: public; Owner: rulemanes
--

CREATE TABLE public.interes (
    int_codemp integer NOT NULL,
    int_fecha date DEFAULT date(now()) NOT NULL,
    int_valor numeric(20,5) DEFAULT 0
);


ALTER TABLE public.interes OWNER TO rulemanes;

--
-- Name: listas; Type: TABLE; Schema: public; Owner: rulemanes
--

CREATE TABLE public.listas (
    li_codigo integer NOT NULL,
    li_codemp integer DEFAULT 0 NOT NULL,
    li_nombre character varying(140) NOT NULL
);


ALTER TABLE public.listas OWNER TO rulemanes;

--
-- Name: listas_li_codigo_seq; Type: SEQUENCE; Schema: public; Owner: rulemanes
--

CREATE SEQUENCE public.listas_li_codigo_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


ALTER TABLE public.listas_li_codigo_seq OWNER TO rulemanes;

--
-- Name: listas_li_codigo_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: rulemanes
--

ALTER SEQUENCE public.listas_li_codigo_seq OWNED BY public.listas.li_codigo;


--
-- Name: listpre; Type: TABLE; Schema: public; Owner: rulemanes
--

CREATE TABLE public.listpre (
    ltp_id integer NOT NULL,
    ltp_codemp integer NOT NULL,
    ltp_idptov integer,
    ltp_idcli integer,
    ltp_nrolista integer,
    ltp_idart integer NOT NULL,
    ltp_orden integer,
    ltp_precio numeric(20,5) DEFAULT 0,
    ltp_fecmod date DEFAULT date(now()),
    ltp_estado integer DEFAULT 1 NOT NULL,
    ltp_vto date,
    ltp_act date,
    ltp_moneda smallint,
    ltp_cotiz numeric(20,6),
    ltp_precio_ars numeric(20,5)
);


ALTER TABLE public.listpre OWNER TO rulemanes;

--
-- Name: listpre_ltp_id_seq; Type: SEQUENCE; Schema: public; Owner: rulemanes
--

CREATE SEQUENCE public.listpre_ltp_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


ALTER TABLE public.listpre_ltp_id_seq OWNER TO rulemanes;

--
-- Name: listpre_ltp_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: rulemanes
--

ALTER SEQUENCE public.listpre_ltp_id_seq OWNED BY public.listpre.ltp_id;


--
-- Name: logs; Type: TABLE; Schema: public; Owner: rulemanes
--

CREATE TABLE public.logs (
    lg_id integer NOT NULL,
    lg_codemp integer NOT NULL,
    lg_idptov integer NOT NULL,
    lg_tabla character varying(200) NOT NULL,
    lg_idregistro integer NOT NULL,
    lg_accion text,
    lg_operador integer,
    lg_motivo text
);


ALTER TABLE public.logs OWNER TO rulemanes;

--
-- Name: logs_carrito_cliente; Type: TABLE; Schema: public; Owner: zona
--

CREATE TABLE public.logs_carrito_cliente (
    log_id integer NOT NULL,
    log_cliente_id integer NOT NULL,
    log_operador_id integer NOT NULL,
    log_codemp integer NOT NULL,
    log_accion character varying(50) NOT NULL,
    log_producto_codigo character varying(50),
    log_producto_nombre text,
    log_cantidad_anterior integer,
    log_cantidad_nueva integer,
    log_precio_unitario numeric(15,2),
    log_descuento_aplicado numeric(5,2),
    log_detalles text,
    log_fecha timestamp without time zone DEFAULT CURRENT_TIMESTAMP
);


ALTER TABLE public.logs_carrito_cliente OWNER TO zona;

--
-- Name: logs_carrito_cliente_log_id_seq; Type: SEQUENCE; Schema: public; Owner: zona
--

CREATE SEQUENCE public.logs_carrito_cliente_log_id_seq
    AS integer
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


ALTER TABLE public.logs_carrito_cliente_log_id_seq OWNER TO zona;

--
-- Name: logs_carrito_cliente_log_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: zona
--

ALTER SEQUENCE public.logs_carrito_cliente_log_id_seq OWNED BY public.logs_carrito_cliente.log_id;


--
-- Name: logs_lg_id_seq; Type: SEQUENCE; Schema: public; Owner: rulemanes
--

CREATE SEQUENCE public.logs_lg_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


ALTER TABLE public.logs_lg_id_seq OWNER TO rulemanes;

--
-- Name: logs_lg_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: rulemanes
--

ALTER SEQUENCE public.logs_lg_id_seq OWNED BY public.logs.lg_id;


--
-- Name: marcas; Type: TABLE; Schema: public; Owner: zona
--

CREATE TABLE public.marcas (
    mar_id integer NOT NULL,
    mar_codemp integer,
    mar_nombre character varying(150)
);


ALTER TABLE public.marcas OWNER TO zona;

--
-- Name: marcas_auto; Type: TABLE; Schema: public; Owner: zona
--

CREATE TABLE public.marcas_auto (
    maraut_id integer NOT NULL,
    maraut_codemp integer NOT NULL,
    maraut_nombre character varying(80) NOT NULL,
    maraut_activo smallint DEFAULT 1
);


ALTER TABLE public.marcas_auto OWNER TO zona;

--
-- Name: marcas_auto_maraut_id_seq; Type: SEQUENCE; Schema: public; Owner: zona
--

CREATE SEQUENCE public.marcas_auto_maraut_id_seq
    AS integer
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


ALTER TABLE public.marcas_auto_maraut_id_seq OWNER TO zona;

--
-- Name: marcas_auto_maraut_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: zona
--

ALTER SEQUENCE public.marcas_auto_maraut_id_seq OWNED BY public.marcas_auto.maraut_id;


--
-- Name: marcas_mar_id_seq; Type: SEQUENCE; Schema: public; Owner: zona
--

CREATE SEQUENCE public.marcas_mar_id_seq
    AS integer
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


ALTER TABLE public.marcas_mar_id_seq OWNER TO zona;

--
-- Name: marcas_mar_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: zona
--

ALTER SEQUENCE public.marcas_mar_id_seq OWNED BY public.marcas.mar_id;


--
-- Name: margenxarticu; Type: TABLE; Schema: public; Owner: estlm
--

CREATE TABLE public.margenxarticu (
    mxa_id integer NOT NULL,
    mxa_codemp integer NOT NULL,
    mxa_idptov integer NOT NULL,
    mxa_idart integer NOT NULL,
    mxa_lista integer,
    mxa_margen numeric(15,5),
    mxa_fecha date DEFAULT date(now()),
    mxa_estado integer DEFAULT 1 NOT NULL
);


ALTER TABLE public.margenxarticu OWNER TO estlm;

--
-- Name: margenxarticu_mxa_id_seq; Type: SEQUENCE; Schema: public; Owner: estlm
--

CREATE SEQUENCE public.margenxarticu_mxa_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


ALTER TABLE public.margenxarticu_mxa_id_seq OWNER TO estlm;

--
-- Name: margenxarticu_mxa_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: estlm
--

ALTER SEQUENCE public.margenxarticu_mxa_id_seq OWNED BY public.margenxarticu.mxa_id;


--
-- Name: menues; Type: TABLE; Schema: public; Owner: rulemanes
--

CREATE TABLE public.menues (
    men_id integer NOT NULL,
    men_titulo text NOT NULL,
    men_tipo smallint DEFAULT 1,
    men_idmenu integer,
    men_estado smallint DEFAULT 1,
    men_alias text,
    men_orden integer DEFAULT 1,
    men_icon character varying(50),
    men_codemp integer
);


ALTER TABLE public.menues OWNER TO rulemanes;

--
-- Name: menues_men_id_seq; Type: SEQUENCE; Schema: public; Owner: rulemanes
--

CREATE SEQUENCE public.menues_men_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


ALTER TABLE public.menues_men_id_seq OWNER TO rulemanes;

--
-- Name: menues_men_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: rulemanes
--

ALTER SEQUENCE public.menues_men_id_seq OWNED BY public.menues.men_id;


--
-- Name: modelos_auto; Type: TABLE; Schema: public; Owner: zona
--

CREATE TABLE public.modelos_auto (
    modaut_id integer NOT NULL,
    modaut_marca integer NOT NULL,
    modaut_nombre character varying(80) NOT NULL,
    modaut_activo smallint DEFAULT 1
);


ALTER TABLE public.modelos_auto OWNER TO zona;

--
-- Name: modelos_auto_modaut_id_seq; Type: SEQUENCE; Schema: public; Owner: zona
--

CREATE SEQUENCE public.modelos_auto_modaut_id_seq
    AS integer
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


ALTER TABLE public.modelos_auto_modaut_id_seq OWNER TO zona;

--
-- Name: modelos_auto_modaut_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: zona
--

ALTER SEQUENCE public.modelos_auto_modaut_id_seq OWNED BY public.modelos_auto.modaut_id;


--
-- Name: modlogs; Type: TABLE; Schema: public; Owner: zona
--

CREATE TABLE public.modlogs (
    log_id integer NOT NULL,
    log_idptov integer NOT NULL,
    log_comprob integer NOT NULL,
    log_numero integer NOT NULL,
    log_tabla character varying(60),
    log_idtabla integer DEFAULT 1 NOT NULL,
    log_campo character varying(60),
    log_valant text,
    log_valpos text,
    log_fecha timestamp without time zone DEFAULT now() NOT NULL,
    log_operador integer,
    log_cont integer DEFAULT 1,
    log_leyen text
);


ALTER TABLE public.modlogs OWNER TO zona;

--
-- Name: modlogs_log_id_seq; Type: SEQUENCE; Schema: public; Owner: zona
--

CREATE SEQUENCE public.modlogs_log_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


ALTER TABLE public.modlogs_log_id_seq OWNER TO zona;

--
-- Name: modlogs_log_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: zona
--

ALTER SEQUENCE public.modlogs_log_id_seq OWNED BY public.modlogs.log_id;


--
-- Name: modulos_sistema; Type: TABLE; Schema: public; Owner: zona
--

CREATE TABLE public.modulos_sistema (
    mod_id integer NOT NULL,
    mod_nombre character varying(100) NOT NULL,
    mod_seccion character varying(50) NOT NULL,
    mod_orden integer NOT NULL,
    mod_codemp integer NOT NULL,
    mod_icon character varying(100)
);


ALTER TABLE public.modulos_sistema OWNER TO zona;

--
-- Name: modulos_sistema_mod_id_seq; Type: SEQUENCE; Schema: public; Owner: zona
--

CREATE SEQUENCE public.modulos_sistema_mod_id_seq
    AS integer
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


ALTER TABLE public.modulos_sistema_mod_id_seq OWNER TO zona;

--
-- Name: modulos_sistema_mod_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: zona
--

ALTER SEQUENCE public.modulos_sistema_mod_id_seq OWNED BY public.modulos_sistema.mod_id;


--
-- Name: moneda; Type: TABLE; Schema: public; Owner: rulemanes
--

CREATE TABLE public.moneda (
    mon_codigo smallint NOT NULL,
    mon_nombre character varying(160) NOT NULL,
    mon_simbol character varying(5)
);


ALTER TABLE public.moneda OWNER TO rulemanes;

--
-- Name: monex; Type: TABLE; Schema: public; Owner: rulemanes
--

CREATE TABLE public.monex (
    mne_id integer NOT NULL,
    mne_codemp integer NOT NULL,
    mne_idastoc integer NOT NULL,
    mne_moneda smallint NOT NULL,
    mne_numero character varying(30),
    mne_valor numeric(20,2),
    mne_estado smallint DEFAULT 1,
    mne_idastoce integer
);


ALTER TABLE public.monex OWNER TO rulemanes;

--
-- Name: monex_mne_id_seq; Type: SEQUENCE; Schema: public; Owner: rulemanes
--

CREATE SEQUENCE public.monex_mne_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


ALTER TABLE public.monex_mne_id_seq OWNER TO rulemanes;

--
-- Name: monex_mne_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: rulemanes
--

ALTER SEQUENCE public.monex_mne_id_seq OWNED BY public.monex.mne_id;


--
-- Name: movsleyen; Type: TABLE; Schema: public; Owner: zona
--

CREATE TABLE public.movsleyen (
    mvl_id integer NOT NULL,
    mvl_idmovs integer NOT NULL,
    mvl_leyen character varying(350) NOT NULL
);


ALTER TABLE public.movsleyen OWNER TO zona;

--
-- Name: movsleyen_mvl_id_seq; Type: SEQUENCE; Schema: public; Owner: zona
--

CREATE SEQUENCE public.movsleyen_mvl_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


ALTER TABLE public.movsleyen_mvl_id_seq OWNER TO zona;

--
-- Name: movsleyen_mvl_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: zona
--

ALTER SEQUENCE public.movsleyen_mvl_id_seq OWNED BY public.movsleyen.mvl_id;


--
-- Name: movsto; Type: TABLE; Schema: public; Owner: rulemanes
--

CREATE TABLE public.movsto (
    movs_id integer NOT NULL,
    movs_codemp integer NOT NULL,
    movs_idptov integer,
    movs_idastoh integer,
    movs_idemiti integer NOT NULL,
    movs_fecval date,
    movs_artic integer NOT NULL,
    movs_canti numeric(20,5) DEFAULT 0,
    movs_dias numeric(20,2) DEFAULT 0,
    movs_precio numeric(15,2) DEFAULT 0,
    movs_importe numeric(20,2) DEFAULT 0,
    movs_tasa numeric(20,5) DEFAULT 0,
    movs_peso numeric(20,2) DEFAULT 0,
    movs_volum numeric(20,2) DEFAULT 0,
    movs_entreg numeric(20,2) DEFAULT 0,
    movs_fecrep date,
    movs_estado integer DEFAULT 1,
    movs_codcli integer,
    movs_indir integer,
    movs_clireal integer,
    movs_idoper integer,
    movs_ticket integer,
    movs_leyen text,
    movs_bonif numeric(15,2) DEFAULT 0 NOT NULL,
    movs_preciosb numeric(15,2) DEFAULT 0 NOT NULL,
    movs_compbte character varying(40),
    movs_cantret numeric(20,5) DEFAULT 0,
    movs_leybonif text
);


ALTER TABLE public.movsto OWNER TO rulemanes;

--
-- Name: movsto_movs_id_seq; Type: SEQUENCE; Schema: public; Owner: rulemanes
--

CREATE SEQUENCE public.movsto_movs_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


ALTER TABLE public.movsto_movs_id_seq OWNER TO rulemanes;

--
-- Name: movsto_movs_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: rulemanes
--

ALTER SEQUENCE public.movsto_movs_id_seq OWNED BY public.movsto.movs_id;


--
-- Name: movstoc; Type: TABLE; Schema: public; Owner: rulemanes
--

CREATE TABLE public.movstoc (
    movc_id integer NOT NULL,
    movc_codemp integer NOT NULL,
    movc_idptov integer,
    movc_idastoh integer,
    movc_idcompra integer NOT NULL,
    movc_fecval date,
    movc_artic integer,
    movc_canti numeric(20,5) DEFAULT 0,
    movc_precio numeric(15,5) DEFAULT 0,
    movc_importe numeric(20,2) DEFAULT 0,
    movc_tasa numeric(20,5) DEFAULT 0,
    movc_peso numeric(20,2) DEFAULT 0,
    movc_volum numeric(20,2) DEFAULT 0,
    movc_entreg numeric(20,2) DEFAULT 0,
    movc_fecrep date,
    movc_estado integer DEFAULT 1,
    movc_codpro integer,
    movc_leyen text,
    movc_bonif numeric,
    movc_preciosb numeric
);


ALTER TABLE public.movstoc OWNER TO rulemanes;

--
-- Name: movstoc_movc_id_seq; Type: SEQUENCE; Schema: public; Owner: rulemanes
--

CREATE SEQUENCE public.movstoc_movc_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


ALTER TABLE public.movstoc_movc_id_seq OWNER TO rulemanes;

--
-- Name: movstoc_movc_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: rulemanes
--

ALTER SEQUENCE public.movstoc_movc_id_seq OWNED BY public.movstoc.movc_id;


--
-- Name: operadores; Type: TABLE; Schema: public; Owner: rulemanes
--

CREATE TABLE public.operadores (
    operad_id integer NOT NULL,
    operad_nombre character varying(160) NOT NULL,
    operad_pass character varying(40) NOT NULL,
    operad_sector character varying(160),
    operad_estado smallint DEFAULT 1 NOT NULL,
    operad_nivaut1 character(2) DEFAULT 'NO'::bpchar NOT NULL,
    operad_nivaut2 character(2) DEFAULT 'NO'::bpchar NOT NULL,
    operad_count integer DEFAULT 1 NOT NULL,
    operad_grupo smallint,
    operad_cliente smallint,
    operad_login character varying(40) NOT NULL,
    operad_last date DEFAULT date(now()),
    operad_contacto character varying(190),
    operad_telefono character varying(190),
    operad_codemp integer,
    operad_ptovta integer,
    operad_vendedor integer,
    operad_notificaciones integer DEFAULT 1
);


ALTER TABLE public.operadores OWNER TO rulemanes;

--
-- Name: operadores_modulos; Type: TABLE; Schema: public; Owner: zona
--

CREATE TABLE public.operadores_modulos (
    om_id integer NOT NULL,
    om_operador integer NOT NULL,
    om_modulo integer NOT NULL,
    om_codemp integer NOT NULL
);


ALTER TABLE public.operadores_modulos OWNER TO zona;

--
-- Name: operadores_modulos_om_id_seq; Type: SEQUENCE; Schema: public; Owner: zona
--

CREATE SEQUENCE public.operadores_modulos_om_id_seq
    AS integer
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


ALTER TABLE public.operadores_modulos_om_id_seq OWNER TO zona;

--
-- Name: operadores_modulos_om_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: zona
--

ALTER SEQUENCE public.operadores_modulos_om_id_seq OWNED BY public.operadores_modulos.om_id;


--
-- Name: operadores_operad_id_seq; Type: SEQUENCE; Schema: public; Owner: rulemanes
--

CREATE SEQUENCE public.operadores_operad_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


ALTER TABLE public.operadores_operad_id_seq OWNER TO rulemanes;

--
-- Name: operadores_operad_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: rulemanes
--

ALTER SEQUENCE public.operadores_operad_id_seq OWNED BY public.operadores.operad_id;


--
-- Name: pais; Type: TABLE; Schema: public; Owner: rulemanes
--

CREATE TABLE public.pais (
    pai_codigo smallint NOT NULL,
    pai_nombre character varying(160) NOT NULL
);


ALTER TABLE public.pais OWNER TO rulemanes;

--
-- Name: politmoneda; Type: TABLE; Schema: public; Owner: zona
--

CREATE TABLE public.politmoneda (
    pm_id integer NOT NULL,
    pm_codemp integer NOT NULL,
    pm_tiponivel smallint NOT NULL,
    pm_idnivel integer NOT NULL,
    pm_moneda smallint NOT NULL,
    pm_tipo smallint DEFAULT 2 NOT NULL,
    pm_estado smallint DEFAULT 1
);


ALTER TABLE public.politmoneda OWNER TO zona;

--
-- Name: politmoneda_pm_id_seq; Type: SEQUENCE; Schema: public; Owner: zona
--

CREATE SEQUENCE public.politmoneda_pm_id_seq
    AS integer
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


ALTER TABLE public.politmoneda_pm_id_seq OWNER TO zona;

--
-- Name: politmoneda_pm_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: zona
--

ALTER SEQUENCE public.politmoneda_pm_id_seq OWNED BY public.politmoneda.pm_id;


--
-- Name: posiciones; Type: TABLE; Schema: public; Owner: zona
--

CREATE TABLE public.posiciones (
    pos_id integer NOT NULL,
    pos_nombre character varying(80) NOT NULL,
    pos_activo smallint DEFAULT 1
);


ALTER TABLE public.posiciones OWNER TO zona;

--
-- Name: posiciones_pos_id_seq; Type: SEQUENCE; Schema: public; Owner: zona
--

CREATE SEQUENCE public.posiciones_pos_id_seq
    AS integer
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


ALTER TABLE public.posiciones_pos_id_seq OWNER TO zona;

--
-- Name: posiciones_pos_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: zona
--

ALTER SEQUENCE public.posiciones_pos_id_seq OWNED BY public.posiciones.pos_id;


--
-- Name: prove; Type: TABLE; Schema: public; Owner: rulemanes
--

CREATE TABLE public.prove (
    prov_id integer NOT NULL,
    prov_codemp integer,
    prov_idptov integer,
    prov_nrcuit character varying(20),
    prov_codigo character varying(40),
    prov_nombre character varying(160),
    prov_direcc character varying(160),
    prov_locali character varying(160),
    prov_provin integer DEFAULT 1 NOT NULL,
    prov_pais smallint DEFAULT 200 NOT NULL,
    prov_estado smallint DEFAULT 1 NOT NULL,
    prov_direce character varying(160),
    prov_observ text,
    prov_grupo integer DEFAULT 1,
    prov_tipo integer DEFAULT 1,
    prov_coniva integer DEFAULT 1,
    prov_retig integer DEFAULT 1,
    prov_codpos character varying
);


ALTER TABLE public.prove OWNER TO rulemanes;

--
-- Name: prove_prov_id_seq; Type: SEQUENCE; Schema: public; Owner: rulemanes
--

CREATE SEQUENCE public.prove_prov_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


ALTER TABLE public.prove_prov_id_seq OWNER TO rulemanes;

--
-- Name: prove_prov_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: rulemanes
--

ALTER SEQUENCE public.prove_prov_id_seq OWNED BY public.prove.prov_id;


--
-- Name: provin; Type: TABLE; Schema: public; Owner: rulemanes
--

CREATE TABLE public.provin (
    pro_codigo integer NOT NULL,
    pro_nombre character varying(160) NOT NULL,
    pro_tasa numeric(20,5) DEFAULT 0,
    pro_coef numeric(20,5) DEFAULT 0,
    pro_valor numeric(20,2) DEFAULT 0
);


ALTER TABLE public.provin OWNER TO rulemanes;

--
-- Name: provin_pro_codigo_seq; Type: SEQUENCE; Schema: public; Owner: rulemanes
--

CREATE SEQUENCE public.provin_pro_codigo_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


ALTER TABLE public.provin_pro_codigo_seq OWNER TO rulemanes;

--
-- Name: provin_pro_codigo_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: rulemanes
--

ALTER SEQUENCE public.provin_pro_codigo_seq OWNED BY public.provin.pro_codigo;


--
-- Name: ptovta; Type: TABLE; Schema: public; Owner: rulemanes
--

CREATE TABLE public.ptovta (
    ptv_id integer NOT NULL,
    ptv_codemp integer,
    ptv_codigo integer NOT NULL,
    ptv_nombre character varying(160),
    ptv_direcc character varying(160),
    ptv_locali character varying(160),
    ptv_codpos character varying(10),
    ptv_provin integer,
    ptv_pais smallint,
    ptv_estado smallint DEFAULT 1,
    ptv_codact character varying(60),
    ptv_descact character varying(160),
    ptv_porcib numeric(15,5),
    ptv_tipiva integer DEFAULT 1,
    ptv_cbu text
);


ALTER TABLE public.ptovta OWNER TO rulemanes;

--
-- Name: ptovta_ptv_id_seq; Type: SEQUENCE; Schema: public; Owner: rulemanes
--

CREATE SEQUENCE public.ptovta_ptv_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


ALTER TABLE public.ptovta_ptv_id_seq OWNER TO rulemanes;

--
-- Name: ptovta_ptv_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: rulemanes
--

ALTER SEQUENCE public.ptovta_ptv_id_seq OWNED BY public.ptovta.ptv_id;


--
-- Name: pxarticu; Type: TABLE; Schema: public; Owner: estlm
--

CREATE TABLE public.pxarticu (
    pxa_id integer NOT NULL,
    pxa_codemp integer NOT NULL,
    pxa_idptov integer DEFAULT '-1'::integer NOT NULL,
    pxa_idart integer NOT NULL,
    pxa_idprov integer,
    pxa_precio numeric(15,5),
    pxa_fecha date DEFAULT date(now()),
    pxa_estado integer DEFAULT 1 NOT NULL,
    pxa_fecanul timestamp without time zone,
    pxa_moneda smallint,
    pxa_cotiz numeric(20,6),
    pxa_precio_ars numeric(20,5)
);


ALTER TABLE public.pxarticu OWNER TO estlm;

--
-- Name: pxarticu_pxa_id_seq; Type: SEQUENCE; Schema: public; Owner: estlm
--

CREATE SEQUENCE public.pxarticu_pxa_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


ALTER TABLE public.pxarticu_pxa_id_seq OWNER TO estlm;

--
-- Name: pxarticu_pxa_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: estlm
--

ALTER SEQUENCE public.pxarticu_pxa_id_seq OWNED BY public.pxarticu.pxa_id;


--
-- Name: receta; Type: TABLE; Schema: public; Owner: rulemanes
--

CREATE TABLE public.receta (
    re_id integer NOT NULL,
    re_codemp integer NOT NULL,
    re_idptov integer DEFAULT 1,
    re_articv integer NOT NULL,
    re_articc integer NOT NULL,
    re_canti numeric(20,5) DEFAULT 1 NOT NULL,
    re_estado integer DEFAULT 1
);


ALTER TABLE public.receta OWNER TO rulemanes;

--
-- Name: receta_re_id_seq; Type: SEQUENCE; Schema: public; Owner: rulemanes
--

CREATE SEQUENCE public.receta_re_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


ALTER TABLE public.receta_re_id_seq OWNER TO rulemanes;

--
-- Name: receta_re_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: rulemanes
--

ALTER SEQUENCE public.receta_re_id_seq OWNED BY public.receta.re_id;


--
-- Name: reclamos; Type: TABLE; Schema: public; Owner: zona
--

CREATE TABLE public.reclamos (
    rec_id integer NOT NULL,
    rec_numero character varying(20) NOT NULL,
    rec_cliente_id integer NOT NULL,
    rec_codemp integer NOT NULL,
    rec_tipo integer,
    rec_estado integer DEFAULT 1,
    rec_pedido_id integer,
    rec_asunto character varying(200) NOT NULL,
    rec_descripcion text NOT NULL,
    rec_urgencia integer DEFAULT 1,
    rec_prioridad integer DEFAULT 1,
    rec_fecha_creacion timestamp without time zone DEFAULT CURRENT_TIMESTAMP,
    rec_fecha_actualizacion timestamp without time zone DEFAULT CURRENT_TIMESTAMP,
    rec_fecha_cierre timestamp without time zone,
    rec_creado_por integer,
    rec_asignado_a integer,
    rec_tiempo_respuesta integer,
    rec_calificacion integer,
    rec_comentario_cierre text
);


ALTER TABLE public.reclamos OWNER TO zona;

--
-- Name: reclamos_adjuntos; Type: TABLE; Schema: public; Owner: zona
--

CREATE TABLE public.reclamos_adjuntos (
    adj_id integer NOT NULL,
    adj_reclamo_id integer,
    adj_nombre_archivo character varying(255) NOT NULL,
    adj_ruta_archivo character varying(500) NOT NULL,
    adj_tipo character varying(100),
    adj_tamanio integer,
    adj_descripcion text,
    adj_creado_por integer,
    adj_fecha_creacion timestamp without time zone DEFAULT CURRENT_TIMESTAMP
);


ALTER TABLE public.reclamos_adjuntos OWNER TO zona;

--
-- Name: reclamos_adjuntos_adj_id_seq; Type: SEQUENCE; Schema: public; Owner: zona
--

CREATE SEQUENCE public.reclamos_adjuntos_adj_id_seq
    AS integer
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


ALTER TABLE public.reclamos_adjuntos_adj_id_seq OWNER TO zona;

--
-- Name: reclamos_adjuntos_adj_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: zona
--

ALTER SEQUENCE public.reclamos_adjuntos_adj_id_seq OWNED BY public.reclamos_adjuntos.adj_id;


--
-- Name: reclamos_rec_id_seq; Type: SEQUENCE; Schema: public; Owner: zona
--

CREATE SEQUENCE public.reclamos_rec_id_seq
    AS integer
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


ALTER TABLE public.reclamos_rec_id_seq OWNER TO zona;

--
-- Name: reclamos_rec_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: zona
--

ALTER SEQUENCE public.reclamos_rec_id_seq OWNED BY public.reclamos.rec_id;


--
-- Name: reclamos_seguimiento; Type: TABLE; Schema: public; Owner: zona
--

CREATE TABLE public.reclamos_seguimiento (
    seg_id integer NOT NULL,
    seg_reclamo_id integer,
    seg_estado_anterior integer,
    seg_estado_nuevo integer,
    seg_comentario text,
    seg_archivo character varying(255),
    seg_creado_por integer,
    seg_fecha_creacion timestamp without time zone DEFAULT CURRENT_TIMESTAMP,
    seg_tipo_accion character varying(50) DEFAULT 'COMENTARIO'::character varying
);


ALTER TABLE public.reclamos_seguimiento OWNER TO zona;

--
-- Name: reclamos_seguimiento_seg_id_seq; Type: SEQUENCE; Schema: public; Owner: zona
--

CREATE SEQUENCE public.reclamos_seguimiento_seg_id_seq
    AS integer
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


ALTER TABLE public.reclamos_seguimiento_seg_id_seq OWNER TO zona;

--
-- Name: reclamos_seguimiento_seg_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: zona
--

ALTER SEQUENCE public.reclamos_seguimiento_seg_id_seq OWNED BY public.reclamos_seguimiento.seg_id;


--
-- Name: relcotnot; Type: TABLE; Schema: public; Owner: rulemanes
--

CREATE TABLE public.relcotnot (
    rn_id integer NOT NULL,
    rn_idemitip integer NOT NULL,
    rn_idemitih integer NOT NULL,
    rn_operador integer,
    rn_estado integer DEFAULT 1
);


ALTER TABLE public.relcotnot OWNER TO rulemanes;

--
-- Name: relcotnot_rn_id_seq; Type: SEQUENCE; Schema: public; Owner: rulemanes
--

CREATE SEQUENCE public.relcotnot_rn_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


ALTER TABLE public.relcotnot_rn_id_seq OWNER TO rulemanes;

--
-- Name: relcotnot_rn_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: rulemanes
--

ALTER SEQUENCE public.relcotnot_rn_id_seq OWNED BY public.relcotnot.rn_id;


--
-- Name: relmenu; Type: TABLE; Schema: public; Owner: rulemanes
--

CREATE TABLE public.relmenu (
    rmen_id integer NOT NULL,
    rmen_titulo text NOT NULL,
    rmen_link text NOT NULL,
    rmen_tipo smallint DEFAULT 1,
    rmen_idmenu integer,
    rmen_nivel smallint DEFAULT 0,
    rmen_estado smallint DEFAULT 1,
    rmen_orden integer DEFAULT 1,
    rmen_icon character varying(50),
    rmen_codemp integer
);


ALTER TABLE public.relmenu OWNER TO rulemanes;

--
-- Name: relmenu_rmen_id_seq; Type: SEQUENCE; Schema: public; Owner: rulemanes
--

CREATE SEQUENCE public.relmenu_rmen_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


ALTER TABLE public.relmenu_rmen_id_seq OWNER TO rulemanes;

--
-- Name: relmenu_rmen_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: rulemanes
--

ALTER SEQUENCE public.relmenu_rmen_id_seq OWNED BY public.relmenu.rmen_id;


--
-- Name: relnotp; Type: TABLE; Schema: public; Owner: rulemanes
--

CREATE TABLE public.relnotp (
    rn_id integer NOT NULL,
    rn_idemitip integer NOT NULL,
    rn_idemitih integer NOT NULL,
    rn_operador integer,
    rn_estado integer DEFAULT 1
);


ALTER TABLE public.relnotp OWNER TO rulemanes;

--
-- Name: relnotp_rn_id_seq; Type: SEQUENCE; Schema: public; Owner: rulemanes
--

CREATE SEQUENCE public.relnotp_rn_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


ALTER TABLE public.relnotp_rn_id_seq OWNER TO rulemanes;

--
-- Name: relnotp_rn_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: rulemanes
--

ALTER SEQUENCE public.relnotp_rn_id_seq OWNED BY public.relnotp.rn_id;


--
-- Name: relrem; Type: TABLE; Schema: public; Owner: rulemanes
--

CREATE TABLE public.relrem (
    rr_id integer NOT NULL,
    rr_idastohp integer NOT NULL,
    rr_idastohh integer NOT NULL,
    rr_operador integer,
    rr_estado integer DEFAULT 1
);


ALTER TABLE public.relrem OWNER TO rulemanes;

--
-- Name: relrem_rr_id_seq; Type: SEQUENCE; Schema: public; Owner: rulemanes
--

CREATE SEQUENCE public.relrem_rr_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


ALTER TABLE public.relrem_rr_id_seq OWNER TO rulemanes;

--
-- Name: relrem_rr_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: rulemanes
--

ALTER SEQUENCE public.relrem_rr_id_seq OWNED BY public.relrem.rr_id;


--
-- Name: relremc; Type: TABLE; Schema: public; Owner: rulemanes
--

CREATE TABLE public.relremc (
    rr_id integer NOT NULL,
    rr_idastohp integer NOT NULL,
    rr_idastohh integer NOT NULL,
    rr_operador integer,
    rr_estado integer DEFAULT 1
);


ALTER TABLE public.relremc OWNER TO rulemanes;

--
-- Name: relremc_rr_id_seq; Type: SEQUENCE; Schema: public; Owner: rulemanes
--

CREATE SEQUENCE public.relremc_rr_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


ALTER TABLE public.relremc_rr_id_seq OWNER TO rulemanes;

--
-- Name: relremc_rr_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: rulemanes
--

ALTER SEQUENCE public.relremc_rr_id_seq OWNED BY public.relremc.rr_id;


--
-- Name: rtoxcontr; Type: TABLE; Schema: public; Owner: rulemanes
--

CREATE TABLE public.rtoxcontr (
    rtc_idcontr integer,
    rtc_idemiti integer,
    rtc_numrem integer,
    rtc_oldctr integer
);


ALTER TABLE public.rtoxcontr OWNER TO rulemanes;

--
-- Name: rubcli; Type: TABLE; Schema: public; Owner: zona
--

CREATE TABLE public.rubcli (
    rb_id integer NOT NULL,
    rb_nombre text,
    rb_estado integer DEFAULT 1
);


ALTER TABLE public.rubcli OWNER TO zona;

--
-- Name: rubcli_rb_id_seq; Type: SEQUENCE; Schema: public; Owner: zona
--

CREATE SEQUENCE public.rubcli_rb_id_seq
    AS integer
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


ALTER TABLE public.rubcli_rb_id_seq OWNER TO zona;

--
-- Name: rubcli_rb_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: zona
--

ALTER SEQUENCE public.rubcli_rb_id_seq OWNED BY public.rubcli.rb_id;


--
-- Name: rubros; Type: TABLE; Schema: public; Owner: rulemanes
--

CREATE TABLE public.rubros (
    rub_id integer NOT NULL,
    rub_codemp integer DEFAULT 0 NOT NULL,
    rub_idptov integer DEFAULT '-1'::integer NOT NULL,
    rub_tipo integer DEFAULT 1,
    rub_codigo integer,
    rub_nombre character varying(140) NOT NULL,
    rub_abrevi character varying(10),
    rub_tipo_app smallint DEFAULT 0
);


ALTER TABLE public.rubros OWNER TO rulemanes;

--
-- Name: rubros_rub_id_seq; Type: SEQUENCE; Schema: public; Owner: rulemanes
--

CREATE SEQUENCE public.rubros_rub_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


ALTER TABLE public.rubros_rub_id_seq OWNER TO rulemanes;

--
-- Name: rubros_rub_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: rulemanes
--

ALTER SEQUENCE public.rubros_rub_id_seq OWNED BY public.rubros.rub_id;


--
-- Name: rutaxastoh; Type: TABLE; Schema: public; Owner: rulemanes
--

CREATE TABLE public.rutaxastoh (
    ra_id integer NOT NULL,
    ra_idruta integer NOT NULL,
    ra_idastoh integer NOT NULL,
    ra_estado integer DEFAULT 1,
    ra_codcli integer,
    ra_mano integer DEFAULT 0,
    ra_entregado date
);


ALTER TABLE public.rutaxastoh OWNER TO rulemanes;

--
-- Name: rutaxastoh_ra_id_seq; Type: SEQUENCE; Schema: public; Owner: rulemanes
--

CREATE SEQUENCE public.rutaxastoh_ra_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


ALTER TABLE public.rutaxastoh_ra_id_seq OWNER TO rulemanes;

--
-- Name: rutaxastoh_ra_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: rulemanes
--

ALTER SEQUENCE public.rutaxastoh_ra_id_seq OWNED BY public.rutaxastoh.ra_id;


--
-- Name: stock; Type: TABLE; Schema: public; Owner: rulemanes
--

CREATE TABLE public.stock (
    stc_id integer NOT NULL,
    stc_codemp integer NOT NULL,
    stc_idptov integer,
    stc_idart integer,
    stc_idartc integer,
    stc_idemiti integer,
    stc_idcompra integer,
    stc_idastoh integer,
    stc_idmovsto integer,
    stc_idmovstoc integer,
    stc_tipo integer DEFAULT 1,
    stc_fecval date,
    stc_canting numeric(20,5),
    stc_cantegr numeric(20,5),
    stc_precio numeric(20,5) DEFAULT 0,
    stc_estado integer DEFAULT 1
);


ALTER TABLE public.stock OWNER TO rulemanes;

--
-- Name: stock_stc_id_seq; Type: SEQUENCE; Schema: public; Owner: rulemanes
--

CREATE SEQUENCE public.stock_stc_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


ALTER TABLE public.stock_stc_id_seq OWNER TO rulemanes;

--
-- Name: stock_stc_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: rulemanes
--

ALTER SEQUENCE public.stock_stc_id_seq OWNED BY public.stock.stc_id;


--
-- Name: stockinicial; Type: TABLE; Schema: public; Owner: zona
--

CREATE TABLE public.stockinicial (
    stk_id integer NOT NULL,
    stk_codemp integer NOT NULL,
    stk_idptov integer NOT NULL,
    stk_artic integer NOT NULL,
    stk_fecval date NOT NULL,
    stk_cantidad numeric(15,2) NOT NULL,
    stk_cantant numeric(15,2),
    stk_diferencia numeric(15,2),
    stk_estado integer DEFAULT 1
);


ALTER TABLE public.stockinicial OWNER TO zona;

--
-- Name: stockinicial_stk_id_seq; Type: SEQUENCE; Schema: public; Owner: zona
--

CREATE SEQUENCE public.stockinicial_stk_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


ALTER TABLE public.stockinicial_stk_id_seq OWNER TO zona;

--
-- Name: stockinicial_stk_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: zona
--

ALTER SEQUENCE public.stockinicial_stk_id_seq OWNED BY public.stockinicial.stk_id;


--
-- Name: subd; Type: TABLE; Schema: public; Owner: rulemanes
--

CREATE TABLE public.subd (
    subd_id integer NOT NULL,
    subd_codemp integer NOT NULL,
    subd_idptov integer,
    subd_nombre character varying(160),
    subd_titulo character varying(160),
    subd_fecd date,
    subd_fech date,
    subd_estado smallint DEFAULT 1 NOT NULL,
    subd_proxnum integer,
    subd_orden integer,
    subd_dia integer
);


ALTER TABLE public.subd OWNER TO rulemanes;

--
-- Name: subd_subd_id_seq; Type: SEQUENCE; Schema: public; Owner: rulemanes
--

CREATE SEQUENCE public.subd_subd_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


ALTER TABLE public.subd_subd_id_seq OWNER TO rulemanes;

--
-- Name: subd_subd_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: rulemanes
--

ALTER SEQUENCE public.subd_subd_id_seq OWNED BY public.subd.subd_id;


--
-- Name: subxcomp; Type: TABLE; Schema: public; Owner: rulemanes
--

CREATE TABLE public.subxcomp (
    subxc_idsub integer NOT NULL,
    subxc_comprob integer NOT NULL,
    subxc_codemp integer
);


ALTER TABLE public.subxcomp OWNER TO rulemanes;

--
-- Name: sucursalc; Type: TABLE; Schema: public; Owner: rulemanes
--

CREATE TABLE public.sucursalc (
    sucu_cliente integer NOT NULL,
    sucu_codigo smallint DEFAULT 1 NOT NULL,
    sucu_nombre character varying(160) NOT NULL,
    sucu_direcc character varying(160),
    sucu_locali character varying(160),
    sucu_codpos character varying(15),
    sucu_provin integer DEFAULT 1,
    sucu_pais smallint,
    sucu_repres character varying(160),
    sucu_contacto character varying(160),
    sucu_observ text,
    sucu_direccl character varying(160),
    sucu_localil character varying(160),
    sucu_codposl character varying(15),
    sucu_codprovl integer,
    sucu_paisl smallint,
    sucu_mail character varying(160)
);


ALTER TABLE public.sucursalc OWNER TO rulemanes;

--
-- Name: sucursalp; Type: TABLE; Schema: public; Owner: rulemanes
--

CREATE TABLE public.sucursalp (
    suc_prove integer NOT NULL,
    suc_codigo smallint DEFAULT 1 NOT NULL,
    suc_nombre character varying(160) NOT NULL,
    suc_direcc character varying(160),
    suc_locali character varying(160),
    suc_codpos character varying(15),
    suc_codprov integer,
    suc_pais smallint,
    suc_repres character varying(160),
    suc_contacto character varying(160),
    suc_observ text,
    suc_direccl character varying(160),
    suc_localil character varying(160),
    suc_codposl character varying(15),
    suc_codprovl integer,
    suc_paisl smallint
);


ALTER TABLE public.sucursalp OWNER TO rulemanes;

--
-- Name: talon; Type: TABLE; Schema: public; Owner: rulemanes
--

CREATE TABLE public.talon (
    tal_id integer NOT NULL,
    tal_codemp integer NOT NULL,
    tal_idptov integer,
    tal_comprob integer,
    tal_nombre character varying(160) NOT NULL,
    tal_valor integer,
    tal_fecha date,
    tal_estado smallint DEFAULT 1,
    tal_vald integer DEFAULT 1,
    tal_valh integer DEFAULT 100000,
    tal_fecvto date DEFAULT '2028-12-31'::date
);


ALTER TABLE public.talon OWNER TO rulemanes;

--
-- Name: talon_tal_id_seq; Type: SEQUENCE; Schema: public; Owner: rulemanes
--

CREATE SEQUENCE public.talon_tal_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


ALTER TABLE public.talon_tal_id_seq OWNER TO rulemanes;

--
-- Name: talon_tal_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: rulemanes
--

ALTER SEQUENCE public.talon_tal_id_seq OWNED BY public.talon.tal_id;


--
-- Name: telefon; Type: TABLE; Schema: public; Owner: rulemanes
--

CREATE TABLE public.telefon (
    tel_id integer NOT NULL,
    tel_codemp integer NOT NULL,
    tel_tipent integer NOT NULL,
    tel_codigo integer NOT NULL,
    tel_tiptel integer NOT NULL,
    tel_contact character varying(160) NOT NULL,
    tel_leyen text,
    tel_dato character varying(300)
);


ALTER TABLE public.telefon OWNER TO rulemanes;

--
-- Name: telefon_tel_id_seq; Type: SEQUENCE; Schema: public; Owner: rulemanes
--

CREATE SEQUENCE public.telefon_tel_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


ALTER TABLE public.telefon_tel_id_seq OWNER TO rulemanes;

--
-- Name: telefon_tel_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: rulemanes
--

ALTER SEQUENCE public.telefon_tel_id_seq OWNED BY public.telefon.tel_id;


--
-- Name: tipasi; Type: TABLE; Schema: public; Owner: rulemanes
--

CREATE TABLE public.tipasi (
    tas_codemp integer NOT NULL,
    tas_codigo integer NOT NULL,
    tas_nombre character varying(160) NOT NULL
);


ALTER TABLE public.tipasi OWNER TO rulemanes;

--
-- Name: tipcta; Type: TABLE; Schema: public; Owner: rulemanes
--

CREATE TABLE public.tipcta (
    tct_codigo integer NOT NULL,
    tct_nombre character varying(160) NOT NULL
);


ALTER TABLE public.tipcta OWNER TO rulemanes;

--
-- Name: tipcta_tct_codigo_seq; Type: SEQUENCE; Schema: public; Owner: rulemanes
--

CREATE SEQUENCE public.tipcta_tct_codigo_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


ALTER TABLE public.tipcta_tct_codigo_seq OWNER TO rulemanes;

--
-- Name: tipcta_tct_codigo_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: rulemanes
--

ALTER SEQUENCE public.tipcta_tct_codigo_seq OWNED BY public.tipcta.tct_codigo;


--
-- Name: tipdocu; Type: TABLE; Schema: public; Owner: rulemanes
--

CREATE TABLE public.tipdocu (
    td_codigo character varying(5) NOT NULL,
    td_nombre character varying(160) NOT NULL
);


ALTER TABLE public.tipdocu OWNER TO rulemanes;

--
-- Name: tipemp; Type: TABLE; Schema: public; Owner: rulemanes
--

CREATE TABLE public.tipemp (
    tem_codigo smallint NOT NULL,
    tem_nombre character varying(160) NOT NULL
);


ALTER TABLE public.tipemp OWNER TO rulemanes;

--
-- Name: tipentid; Type: TABLE; Schema: public; Owner: rulemanes
--

CREATE TABLE public.tipentid (
    tet_codigo integer NOT NULL,
    tet_nombre character varying(30) NOT NULL
);


ALTER TABLE public.tipentid OWNER TO rulemanes;

--
-- Name: tipentid_tet_codigo_seq; Type: SEQUENCE; Schema: public; Owner: rulemanes
--

CREATE SEQUENCE public.tipentid_tet_codigo_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


ALTER TABLE public.tipentid_tet_codigo_seq OWNER TO rulemanes;

--
-- Name: tipentid_tet_codigo_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: rulemanes
--

ALTER SEQUENCE public.tipentid_tet_codigo_seq OWNED BY public.tipentid.tet_codigo;


--
-- Name: tipos_reclamos; Type: TABLE; Schema: public; Owner: zona
--

CREATE TABLE public.tipos_reclamos (
    tpr_id integer NOT NULL,
    tpr_nombre character varying(100) NOT NULL,
    tpr_descripcion text,
    tpr_activo boolean DEFAULT true,
    tpr_codemp integer NOT NULL,
    created_at timestamp without time zone DEFAULT CURRENT_TIMESTAMP
);


ALTER TABLE public.tipos_reclamos OWNER TO zona;

--
-- Name: tipos_reclamos_tpr_id_seq; Type: SEQUENCE; Schema: public; Owner: zona
--

CREATE SEQUENCE public.tipos_reclamos_tpr_id_seq
    AS integer
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


ALTER TABLE public.tipos_reclamos_tpr_id_seq OWNER TO zona;

--
-- Name: tipos_reclamos_tpr_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: zona
--

ALTER SEQUENCE public.tipos_reclamos_tpr_id_seq OWNED BY public.tipos_reclamos.tpr_id;


--
-- Name: tiptel; Type: TABLE; Schema: public; Owner: rulemanes
--

CREATE TABLE public.tiptel (
    ttl_codigo integer NOT NULL,
    ttl_nombre character varying(160) NOT NULL
);


ALTER TABLE public.tiptel OWNER TO rulemanes;

--
-- Name: tiptel_ttl_codigo_seq; Type: SEQUENCE; Schema: public; Owner: rulemanes
--

CREATE SEQUENCE public.tiptel_ttl_codigo_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


ALTER TABLE public.tiptel_ttl_codigo_seq OWNER TO rulemanes;

--
-- Name: tiptel_ttl_codigo_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: rulemanes
--

ALTER SEQUENCE public.tiptel_ttl_codigo_seq OWNED BY public.tiptel.ttl_codigo;


--
-- Name: tributosfe; Type: TABLE; Schema: public; Owner: rulemanes
--

CREATE TABLE public.tributosfe (
    t_id integer NOT NULL,
    t_idfe integer NOT NULL,
    t_cod integer,
    t_desc character varying(200),
    t_alic numeric(15,2),
    t_imp numeric(15,2) DEFAULT 0,
    t_baseimp numeric(15,2)
);


ALTER TABLE public.tributosfe OWNER TO rulemanes;

--
-- Name: tributosfe_t_id_seq; Type: SEQUENCE; Schema: public; Owner: rulemanes
--

CREATE SEQUENCE public.tributosfe_t_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


ALTER TABLE public.tributosfe_t_id_seq OWNER TO rulemanes;

--
-- Name: tributosfe_t_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: rulemanes
--

ALTER SEQUENCE public.tributosfe_t_id_seq OWNED BY public.tributosfe.t_id;


--
-- Name: unimed; Type: TABLE; Schema: public; Owner: rulemanes
--

CREATE TABLE public.unimed (
    uni_codigo smallint NOT NULL,
    uni_coef numeric(20,5) DEFAULT 0,
    uni_nombre character varying(160),
    uni_simbolo character varying(160)
);


ALTER TABLE public.unimed OWNER TO rulemanes;

--
-- Name: user_theme; Type: TABLE; Schema: public; Owner: zona
--

CREATE TABLE public.user_theme (
    ut_operador_id integer NOT NULL,
    ut_json text NOT NULL,
    ut_updated_at timestamp without time zone DEFAULT now() NOT NULL
);


ALTER TABLE public.user_theme OWNER TO zona;

--
-- Name: usermenu; Type: TABLE; Schema: public; Owner: rulemanes
--

CREATE TABLE public.usermenu (
    umen_id integer NOT NULL,
    umen_operador integer NOT NULL,
    umen_idmenu integer,
    umen_estado smallint DEFAULT 1,
    umen_menufinal integer,
    umen_codemp integer
);


ALTER TABLE public.usermenu OWNER TO rulemanes;

--
-- Name: usermenu_umen_id_seq; Type: SEQUENCE; Schema: public; Owner: rulemanes
--

CREATE SEQUENCE public.usermenu_umen_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


ALTER TABLE public.usermenu_umen_id_seq OWNER TO rulemanes;

--
-- Name: usermenu_umen_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: rulemanes
--

ALTER SEQUENCE public.usermenu_umen_id_seq OWNED BY public.usermenu.umen_id;


--
-- Name: varambi; Type: TABLE; Schema: public; Owner: rulemanes
--

CREATE TABLE public.varambi (
    vari_codemp integer DEFAULT 0 NOT NULL,
    vari_idptov integer DEFAULT 0 NOT NULL,
    vari_codigo character varying(160) NOT NULL,
    vari_nombre character varying(160) NOT NULL,
    vari_valor character varying(160),
    vari_tipo integer DEFAULT 1
);


ALTER TABLE public.varambi OWNER TO rulemanes;

--
-- Name: vendedor; Type: TABLE; Schema: public; Owner: rulemanes
--

CREATE TABLE public.vendedor (
    ven_id integer NOT NULL,
    ven_codemp integer NOT NULL,
    ven_nombre character varying(160) NOT NULL,
    ven_direcc character varying(160),
    ven_locali character varying(160),
    ven_codpos character varying(10),
    ven_provin integer,
    ven_pais smallint,
    ven_nrcuit character varying(20),
    ven_estado smallint DEFAULT 1,
    ven_comis numeric(20,5) DEFAULT 0
);


ALTER TABLE public.vendedor OWNER TO rulemanes;

--
-- Name: vendedor_ven_id_seq; Type: SEQUENCE; Schema: public; Owner: rulemanes
--

CREATE SEQUENCE public.vendedor_ven_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


ALTER TABLE public.vendedor_ven_id_seq OWNER TO rulemanes;

--
-- Name: vendedor_ven_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: rulemanes
--

ALTER SEQUENCE public.vendedor_ven_id_seq OWNED BY public.vendedor.ven_id;


--
-- Name: vw_reclamos_detallados; Type: VIEW; Schema: public; Owner: zona
--

CREATE VIEW public.vw_reclamos_detallados AS
 SELECT r.rec_id,
    r.rec_numero,
    r.rec_cliente_id,
    c.cli_nombre AS cliente_nombre,
    r.rec_codemp,
    r.rec_tipo,
    tp.tpr_nombre AS tipo_nombre,
    r.rec_estado,
    es.est_nombre AS estado_nombre,
    es.est_color AS estado_color,
    r.rec_pedido_id,
    e.emi_numero AS pedido_numero,
    e.emi_fecval AS pedido_fecha,
    r.rec_asunto,
    r.rec_descripcion,
    r.rec_urgencia,
        CASE
            WHEN (r.rec_urgencia = 1) THEN 'Baja'::text
            WHEN (r.rec_urgencia = 2) THEN 'Media'::text
            WHEN (r.rec_urgencia = 3) THEN 'Alta'::text
            ELSE 'No definida'::text
        END AS urgencia_texto,
    r.rec_prioridad,
    r.rec_fecha_creacion,
    r.rec_fecha_actualizacion,
    r.rec_fecha_cierre,
    r.rec_creado_por,
    op_creador.operad_nombre AS creador_nombre,
    r.rec_asignado_a,
    op_asignado.operad_nombre AS asignado_nombre,
    r.rec_tiempo_respuesta,
    r.rec_calificacion,
    r.rec_comentario_cierre,
    EXTRACT(days FROM (COALESCE((r.rec_fecha_cierre)::timestamp with time zone, CURRENT_TIMESTAMP) - (r.rec_fecha_creacion)::timestamp with time zone)) AS dias_transcurridos
   FROM ((((((public.reclamos r
     LEFT JOIN public.clientes c ON ((r.rec_cliente_id = c.cli_id)))
     LEFT JOIN public.tipos_reclamos tp ON ((r.rec_tipo = tp.tpr_id)))
     LEFT JOIN public.estados_reclamos es ON ((r.rec_estado = es.est_id)))
     LEFT JOIN public.emiti e ON ((r.rec_pedido_id = e.emi_id)))
     LEFT JOIN public.operadores op_creador ON ((r.rec_creado_por = op_creador.operad_id)))
     LEFT JOIN public.operadores op_asignado ON ((r.rec_asignado_a = op_asignado.operad_id)));


ALTER TABLE public.vw_reclamos_detallados OWNER TO zona;

--
-- Name: zona; Type: TABLE; Schema: public; Owner: rulemanes
--

CREATE TABLE public.zona (
    zon_codigo integer NOT NULL,
    zon_codemp integer NOT NULL,
    zon_pais integer,
    zon_provin smallint,
    zon_ciudad integer,
    zon_barrio integer,
    zon_nombre character varying(160) NOT NULL
);


ALTER TABLE public.zona OWNER TO rulemanes;

--
-- Name: zona_zon_codigo_seq; Type: SEQUENCE; Schema: public; Owner: rulemanes
--

CREATE SEQUENCE public.zona_zon_codigo_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


ALTER TABLE public.zona_zon_codigo_seq OWNER TO rulemanes;

--
-- Name: zona_zon_codigo_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: rulemanes
--

ALTER SEQUENCE public.zona_zon_codigo_seq OWNED BY public.zona.zon_codigo;


--
-- Name: agrupa ag_id; Type: DEFAULT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.agrupa ALTER COLUMN ag_id SET DEFAULT nextval('public.agrupa_ag_id_seq'::regclass);


--
-- Name: art_aplicaciones app_id; Type: DEFAULT; Schema: public; Owner: zona
--

ALTER TABLE ONLY public.art_aplicaciones ALTER COLUMN app_id SET DEFAULT nextval('public.art_aplicaciones_app_id_seq'::regclass);


--
-- Name: articu artc_id; Type: DEFAULT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.articu ALTER COLUMN artc_id SET DEFAULT nextval('public.articu_artc_id_seq'::regclass);


--
-- Name: articvta art_id; Type: DEFAULT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.articvta ALTER COLUMN art_id SET DEFAULT nextval('public.articvta_art_id_seq'::regclass);


--
-- Name: astoctas astc_id; Type: DEFAULT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.astoctas ALTER COLUMN astc_id SET DEFAULT nextval('public.astoctas_astc_id_seq'::regclass);


--
-- Name: astoh ash_id; Type: DEFAULT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.astoh ALTER COLUMN ash_id SET DEFAULT nextval('public.astoh_ash_id_seq'::regclass);


--
-- Name: auxfe fe_id; Type: DEFAULT; Schema: public; Owner: zona
--

ALTER TABLE ONLY public.auxfe ALTER COLUMN fe_id SET DEFAULT nextval('public.auxfe_fe_id_seq'::regclass);


--
-- Name: cbteasoc cb_id; Type: DEFAULT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.cbteasoc ALTER COLUMN cb_id SET DEFAULT nextval('public.cbteasoc_cb_id_seq'::regclass);


--
-- Name: chat c_id; Type: DEFAULT; Schema: public; Owner: zona
--

ALTER TABLE ONLY public.chat ALTER COLUMN c_id SET DEFAULT nextval('public.chat_c_id_seq'::regclass);


--
-- Name: cheques chq_id; Type: DEFAULT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.cheques ALTER COLUMN chq_id SET DEFAULT nextval('public.cheques_chq_id_seq'::regclass);


--
-- Name: clientes cli_id; Type: DEFAULT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.clientes ALTER COLUMN cli_id SET DEFAULT nextval('public.clientes_cli_id_seq'::regclass);


--
-- Name: compra cpra_id; Type: DEFAULT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.compra ALTER COLUMN cpra_id SET DEFAULT nextval('public.compra_cpra_id_seq'::regclass);


--
-- Name: comprob comp_id; Type: DEFAULT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.comprob ALTER COLUMN comp_id SET DEFAULT nextval('public.comprob_comp_id_seq'::regclass);


--
-- Name: concilia clia_id; Type: DEFAULT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.concilia ALTER COLUMN clia_id SET DEFAULT nextval('public.concilia_clia_id_seq'::regclass);


--
-- Name: conpag cdp_codigo; Type: DEFAULT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.conpag ALTER COLUMN cdp_codigo SET DEFAULT nextval('public.conpag_cdp_codigo_seq'::regclass);


--
-- Name: contratos ctr_id; Type: DEFAULT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.contratos ALTER COLUMN ctr_id SET DEFAULT nextval('public.contratos_ctr_id_seq'::regclass);


--
-- Name: contrb ctrb_id; Type: DEFAULT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.contrb ALTER COLUMN ctrb_id SET DEFAULT nextval('public.contrb_ctrb_id_seq'::regclass);


--
-- Name: conxclia cxlia_id; Type: DEFAULT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.conxclia ALTER COLUMN cxlia_id SET DEFAULT nextval('public.conxcxlia_cxlia_id_seq'::regclass);


--
-- Name: ctacble cbl_id; Type: DEFAULT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.ctacble ALTER COLUMN cbl_id SET DEFAULT nextval('public.ctacble_cbl_id_seq'::regclass);


--
-- Name: ctes cte_id; Type: DEFAULT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.ctes ALTER COLUMN cte_id SET DEFAULT nextval('public.ctes_cte_id_seq'::regclass);


--
-- Name: debitos db_id; Type: DEFAULT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.debitos ALTER COLUMN db_id SET DEFAULT nextval('public.debitos_db_id_seq'::regclass);


--
-- Name: descuentos_rubro dr_id; Type: DEFAULT; Schema: public; Owner: zona
--

ALTER TABLE ONLY public.descuentos_rubro ALTER COLUMN dr_id SET DEFAULT nextval('public.descuentos_rubro_dr_id_seq'::regclass);


--
-- Name: deudas dd_id; Type: DEFAULT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.deudas ALTER COLUMN dd_id SET DEFAULT nextval('public.deudas_dd_id_seq'::regclass);


--
-- Name: dispenser dis_id; Type: DEFAULT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.dispenser ALTER COLUMN dis_id SET DEFAULT nextval('public.dispenser_dis_id_seq'::regclass);


--
-- Name: docum doc_id; Type: DEFAULT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.docum ALTER COLUMN doc_id SET DEFAULT nextval('public.docum_doc_id_seq'::regclass);


--
-- Name: emiti emi_id; Type: DEFAULT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.emiti ALTER COLUMN emi_id SET DEFAULT nextval('public.emiti_emi_id_seq'::regclass);


--
-- Name: estados_reclamos est_id; Type: DEFAULT; Schema: public; Owner: zona
--

ALTER TABLE ONLY public.estados_reclamos ALTER COLUMN est_id SET DEFAULT nextval('public.estados_reclamos_est_id_seq'::regclass);


--
-- Name: fact_vencidas fv_id; Type: DEFAULT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.fact_vencidas ALTER COLUMN fv_id SET DEFAULT nextval('public.fact_vencidas_fv_id_seq'::regclass);


--
-- Name: firman fir_id; Type: DEFAULT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.firman ALTER COLUMN fir_id SET DEFAULT nextval('public.firman_fir_id_seq'::regclass);


--
-- Name: grupos gpo_id; Type: DEFAULT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.grupos ALTER COLUMN gpo_id SET DEFAULT nextval('public.grupos_gpo_id_seq'::regclass);


--
-- Name: hojaruta hr_id; Type: DEFAULT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.hojaruta ALTER COLUMN hr_id SET DEFAULT nextval('public.hojaruta_hr_id_seq'::regclass);


--
-- Name: iibbfile if_id; Type: DEFAULT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.iibbfile ALTER COLUMN if_id SET DEFAULT nextval('public.iibbfile_if_id_seq'::regclass);


--
-- Name: listas li_codigo; Type: DEFAULT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.listas ALTER COLUMN li_codigo SET DEFAULT nextval('public.listas_li_codigo_seq'::regclass);


--
-- Name: listpre ltp_id; Type: DEFAULT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.listpre ALTER COLUMN ltp_id SET DEFAULT nextval('public.listpre_ltp_id_seq'::regclass);


--
-- Name: logs lg_id; Type: DEFAULT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.logs ALTER COLUMN lg_id SET DEFAULT nextval('public.logs_lg_id_seq'::regclass);


--
-- Name: logs_carrito_cliente log_id; Type: DEFAULT; Schema: public; Owner: zona
--

ALTER TABLE ONLY public.logs_carrito_cliente ALTER COLUMN log_id SET DEFAULT nextval('public.logs_carrito_cliente_log_id_seq'::regclass);


--
-- Name: marcas mar_id; Type: DEFAULT; Schema: public; Owner: zona
--

ALTER TABLE ONLY public.marcas ALTER COLUMN mar_id SET DEFAULT nextval('public.marcas_mar_id_seq'::regclass);


--
-- Name: marcas_auto maraut_id; Type: DEFAULT; Schema: public; Owner: zona
--

ALTER TABLE ONLY public.marcas_auto ALTER COLUMN maraut_id SET DEFAULT nextval('public.marcas_auto_maraut_id_seq'::regclass);


--
-- Name: margenxarticu mxa_id; Type: DEFAULT; Schema: public; Owner: estlm
--

ALTER TABLE ONLY public.margenxarticu ALTER COLUMN mxa_id SET DEFAULT nextval('public.margenxarticu_mxa_id_seq'::regclass);


--
-- Name: menues men_id; Type: DEFAULT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.menues ALTER COLUMN men_id SET DEFAULT nextval('public.menues_men_id_seq'::regclass);


--
-- Name: modelos_auto modaut_id; Type: DEFAULT; Schema: public; Owner: zona
--

ALTER TABLE ONLY public.modelos_auto ALTER COLUMN modaut_id SET DEFAULT nextval('public.modelos_auto_modaut_id_seq'::regclass);


--
-- Name: modlogs log_id; Type: DEFAULT; Schema: public; Owner: zona
--

ALTER TABLE ONLY public.modlogs ALTER COLUMN log_id SET DEFAULT nextval('public.modlogs_log_id_seq'::regclass);


--
-- Name: modulos_sistema mod_id; Type: DEFAULT; Schema: public; Owner: zona
--

ALTER TABLE ONLY public.modulos_sistema ALTER COLUMN mod_id SET DEFAULT nextval('public.modulos_sistema_mod_id_seq'::regclass);


--
-- Name: monex mne_id; Type: DEFAULT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.monex ALTER COLUMN mne_id SET DEFAULT nextval('public.monex_mne_id_seq'::regclass);


--
-- Name: movsleyen mvl_id; Type: DEFAULT; Schema: public; Owner: zona
--

ALTER TABLE ONLY public.movsleyen ALTER COLUMN mvl_id SET DEFAULT nextval('public.movsleyen_mvl_id_seq'::regclass);


--
-- Name: movsto movs_id; Type: DEFAULT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.movsto ALTER COLUMN movs_id SET DEFAULT nextval('public.movsto_movs_id_seq'::regclass);


--
-- Name: movstoc movc_id; Type: DEFAULT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.movstoc ALTER COLUMN movc_id SET DEFAULT nextval('public.movstoc_movc_id_seq'::regclass);


--
-- Name: operadores operad_id; Type: DEFAULT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.operadores ALTER COLUMN operad_id SET DEFAULT nextval('public.operadores_operad_id_seq'::regclass);


--
-- Name: operadores_modulos om_id; Type: DEFAULT; Schema: public; Owner: zona
--

ALTER TABLE ONLY public.operadores_modulos ALTER COLUMN om_id SET DEFAULT nextval('public.operadores_modulos_om_id_seq'::regclass);


--
-- Name: politmoneda pm_id; Type: DEFAULT; Schema: public; Owner: zona
--

ALTER TABLE ONLY public.politmoneda ALTER COLUMN pm_id SET DEFAULT nextval('public.politmoneda_pm_id_seq'::regclass);


--
-- Name: posiciones pos_id; Type: DEFAULT; Schema: public; Owner: zona
--

ALTER TABLE ONLY public.posiciones ALTER COLUMN pos_id SET DEFAULT nextval('public.posiciones_pos_id_seq'::regclass);


--
-- Name: prove prov_id; Type: DEFAULT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.prove ALTER COLUMN prov_id SET DEFAULT nextval('public.prove_prov_id_seq'::regclass);


--
-- Name: provin pro_codigo; Type: DEFAULT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.provin ALTER COLUMN pro_codigo SET DEFAULT nextval('public.provin_pro_codigo_seq'::regclass);


--
-- Name: ptovta ptv_id; Type: DEFAULT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.ptovta ALTER COLUMN ptv_id SET DEFAULT nextval('public.ptovta_ptv_id_seq'::regclass);


--
-- Name: pxarticu pxa_id; Type: DEFAULT; Schema: public; Owner: estlm
--

ALTER TABLE ONLY public.pxarticu ALTER COLUMN pxa_id SET DEFAULT nextval('public.pxarticu_pxa_id_seq'::regclass);


--
-- Name: receta re_id; Type: DEFAULT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.receta ALTER COLUMN re_id SET DEFAULT nextval('public.receta_re_id_seq'::regclass);


--
-- Name: reclamos rec_id; Type: DEFAULT; Schema: public; Owner: zona
--

ALTER TABLE ONLY public.reclamos ALTER COLUMN rec_id SET DEFAULT nextval('public.reclamos_rec_id_seq'::regclass);


--
-- Name: reclamos_adjuntos adj_id; Type: DEFAULT; Schema: public; Owner: zona
--

ALTER TABLE ONLY public.reclamos_adjuntos ALTER COLUMN adj_id SET DEFAULT nextval('public.reclamos_adjuntos_adj_id_seq'::regclass);


--
-- Name: reclamos_seguimiento seg_id; Type: DEFAULT; Schema: public; Owner: zona
--

ALTER TABLE ONLY public.reclamos_seguimiento ALTER COLUMN seg_id SET DEFAULT nextval('public.reclamos_seguimiento_seg_id_seq'::regclass);


--
-- Name: relcotnot rn_id; Type: DEFAULT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.relcotnot ALTER COLUMN rn_id SET DEFAULT nextval('public.relcotnot_rn_id_seq'::regclass);


--
-- Name: relmenu rmen_id; Type: DEFAULT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.relmenu ALTER COLUMN rmen_id SET DEFAULT nextval('public.relmenu_rmen_id_seq'::regclass);


--
-- Name: relnotp rn_id; Type: DEFAULT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.relnotp ALTER COLUMN rn_id SET DEFAULT nextval('public.relnotp_rn_id_seq'::regclass);


--
-- Name: relrem rr_id; Type: DEFAULT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.relrem ALTER COLUMN rr_id SET DEFAULT nextval('public.relrem_rr_id_seq'::regclass);


--
-- Name: relremc rr_id; Type: DEFAULT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.relremc ALTER COLUMN rr_id SET DEFAULT nextval('public.relremc_rr_id_seq'::regclass);


--
-- Name: rubcli rb_id; Type: DEFAULT; Schema: public; Owner: zona
--

ALTER TABLE ONLY public.rubcli ALTER COLUMN rb_id SET DEFAULT nextval('public.rubcli_rb_id_seq'::regclass);


--
-- Name: rubros rub_id; Type: DEFAULT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.rubros ALTER COLUMN rub_id SET DEFAULT nextval('public.rubros_rub_id_seq'::regclass);


--
-- Name: rutaxastoh ra_id; Type: DEFAULT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.rutaxastoh ALTER COLUMN ra_id SET DEFAULT nextval('public.rutaxastoh_ra_id_seq'::regclass);


--
-- Name: stock stc_id; Type: DEFAULT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.stock ALTER COLUMN stc_id SET DEFAULT nextval('public.stock_stc_id_seq'::regclass);


--
-- Name: stockinicial stk_id; Type: DEFAULT; Schema: public; Owner: zona
--

ALTER TABLE ONLY public.stockinicial ALTER COLUMN stk_id SET DEFAULT nextval('public.stockinicial_stk_id_seq'::regclass);


--
-- Name: subd subd_id; Type: DEFAULT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.subd ALTER COLUMN subd_id SET DEFAULT nextval('public.subd_subd_id_seq'::regclass);


--
-- Name: talon tal_id; Type: DEFAULT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.talon ALTER COLUMN tal_id SET DEFAULT nextval('public.talon_tal_id_seq'::regclass);


--
-- Name: telefon tel_id; Type: DEFAULT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.telefon ALTER COLUMN tel_id SET DEFAULT nextval('public.telefon_tel_id_seq'::regclass);


--
-- Name: tipcta tct_codigo; Type: DEFAULT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.tipcta ALTER COLUMN tct_codigo SET DEFAULT nextval('public.tipcta_tct_codigo_seq'::regclass);


--
-- Name: tipentid tet_codigo; Type: DEFAULT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.tipentid ALTER COLUMN tet_codigo SET DEFAULT nextval('public.tipentid_tet_codigo_seq'::regclass);


--
-- Name: tipos_reclamos tpr_id; Type: DEFAULT; Schema: public; Owner: zona
--

ALTER TABLE ONLY public.tipos_reclamos ALTER COLUMN tpr_id SET DEFAULT nextval('public.tipos_reclamos_tpr_id_seq'::regclass);


--
-- Name: tiptel ttl_codigo; Type: DEFAULT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.tiptel ALTER COLUMN ttl_codigo SET DEFAULT nextval('public.tiptel_ttl_codigo_seq'::regclass);


--
-- Name: tributosfe t_id; Type: DEFAULT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.tributosfe ALTER COLUMN t_id SET DEFAULT nextval('public.tributosfe_t_id_seq'::regclass);


--
-- Name: usermenu umen_id; Type: DEFAULT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.usermenu ALTER COLUMN umen_id SET DEFAULT nextval('public.usermenu_umen_id_seq'::regclass);


--
-- Name: vendedor ven_id; Type: DEFAULT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.vendedor ALTER COLUMN ven_id SET DEFAULT nextval('public.vendedor_ven_id_seq'::regclass);


--
-- Name: zona zon_codigo; Type: DEFAULT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.zona ALTER COLUMN zon_codigo SET DEFAULT nextval('public.zona_zon_codigo_seq'::regclass);


--
-- Name: agrupa agrupa_pkey; Type: CONSTRAINT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.agrupa
    ADD CONSTRAINT agrupa_pkey PRIMARY KEY (ag_id);


--
-- Name: art_aplicaciones art_aplicaciones_pkey; Type: CONSTRAINT; Schema: public; Owner: zona
--

ALTER TABLE ONLY public.art_aplicaciones
    ADD CONSTRAINT art_aplicaciones_pkey PRIMARY KEY (app_id);


--
-- Name: art_atrib_def art_atrib_def_pkey; Type: CONSTRAINT; Schema: public; Owner: zona
--

ALTER TABLE ONLY public.art_atrib_def
    ADD CONSTRAINT art_atrib_def_pkey PRIMARY KEY (aad_id);


--
-- Name: art_atrib_val art_atrib_val_aav_artid_aav_defid_key; Type: CONSTRAINT; Schema: public; Owner: zona
--

ALTER TABLE ONLY public.art_atrib_val
    ADD CONSTRAINT art_atrib_val_aav_artid_aav_defid_key UNIQUE (aav_artid, aav_defid);


--
-- Name: art_atrib_val art_atrib_val_pkey; Type: CONSTRAINT; Schema: public; Owner: zona
--

ALTER TABLE ONLY public.art_atrib_val
    ADD CONSTRAINT art_atrib_val_pkey PRIMARY KEY (aav_id);


--
-- Name: art_atrib_valm art_atrib_valm_pkey; Type: CONSTRAINT; Schema: public; Owner: zona
--

ALTER TABLE ONLY public.art_atrib_valm
    ADD CONSTRAINT art_atrib_valm_pkey PRIMARY KEY (aavm_id);


--
-- Name: art_tipo art_tipo_pkey; Type: CONSTRAINT; Schema: public; Owner: zona
--

ALTER TABLE ONLY public.art_tipo
    ADD CONSTRAINT art_tipo_pkey PRIMARY KEY (at_id);


--
-- Name: articu articu_pkey; Type: CONSTRAINT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.articu
    ADD CONSTRAINT articu_pkey PRIMARY KEY (artc_id);


--
-- Name: articvta articvta_pkey; Type: CONSTRAINT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.articvta
    ADD CONSTRAINT articvta_pkey PRIMARY KEY (art_id);


--
-- Name: zona artl_pkey; Type: CONSTRAINT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.zona
    ADD CONSTRAINT artl_pkey PRIMARY KEY (zon_codigo);


--
-- Name: astoctas astoctas_pkey; Type: CONSTRAINT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.astoctas
    ADD CONSTRAINT astoctas_pkey PRIMARY KEY (astc_id);


--
-- Name: astoh astoh_pkey; Type: CONSTRAINT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.astoh
    ADD CONSTRAINT astoh_pkey PRIMARY KEY (ash_id);


--
-- Name: auditoria_pedidos auditoria_pedidos_pkey; Type: CONSTRAINT; Schema: public; Owner: zona
--

ALTER TABLE ONLY public.auditoria_pedidos
    ADD CONSTRAINT auditoria_pedidos_pkey PRIMARY KEY (audi_id);


--
-- Name: auxfe auxfe_pkey; Type: CONSTRAINT; Schema: public; Owner: zona
--

ALTER TABLE ONLY public.auxfe
    ADD CONSTRAINT auxfe_pkey PRIMARY KEY (fe_id);


--
-- Name: bancos bancos_pkey; Type: CONSTRAINT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.bancos
    ADD CONSTRAINT bancos_pkey PRIMARY KEY (bco_codigo);


--
-- Name: carritos_cliente carritos_cliente_pkey; Type: CONSTRAINT; Schema: public; Owner: zona
--

ALTER TABLE ONLY public.carritos_cliente
    ADD CONSTRAINT carritos_cliente_pkey PRIMARY KEY (id);


--
-- Name: cblexasi cblexasi_pkey; Type: CONSTRAINT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.cblexasi
    ADD CONSTRAINT cblexasi_pkey PRIMARY KEY (cxa_codemp, cxa_tipasi, cxa_cble);


--
-- Name: cbteasoc cbteasoc_pkey; Type: CONSTRAINT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.cbteasoc
    ADD CONSTRAINT cbteasoc_pkey PRIMARY KEY (cb_id);


--
-- Name: chat chat_pkey; Type: CONSTRAINT; Schema: public; Owner: zona
--

ALTER TABLE ONLY public.chat
    ADD CONSTRAINT chat_pkey PRIMARY KEY (c_id);


--
-- Name: cheques cheques_pkey; Type: CONSTRAINT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.cheques
    ADD CONSTRAINT cheques_pkey PRIMARY KEY (chq_id);


--
-- Name: clientes clientes_pkey; Type: CONSTRAINT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.clientes
    ADD CONSTRAINT clientes_pkey PRIMARY KEY (cli_id);


--
-- Name: compra compra_pkey; Type: CONSTRAINT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.compra
    ADD CONSTRAINT compra_pkey PRIMARY KEY (cpra_id);


--
-- Name: comprob comprob_pkey; Type: CONSTRAINT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.comprob
    ADD CONSTRAINT comprob_pkey PRIMARY KEY (comp_id);


--
-- Name: concilia concilia_pkey; Type: CONSTRAINT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.concilia
    ADD CONSTRAINT concilia_pkey PRIMARY KEY (clia_id);


--
-- Name: coniva coniva_pkey; Type: CONSTRAINT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.coniva
    ADD CONSTRAINT coniva_pkey PRIMARY KEY (iva_codigo);


--
-- Name: conpag conpag_pkey; Type: CONSTRAINT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.conpag
    ADD CONSTRAINT conpag_pkey PRIMARY KEY (cdp_codigo);


--
-- Name: contratos contratos_pkey; Type: CONSTRAINT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.contratos
    ADD CONSTRAINT contratos_pkey PRIMARY KEY (ctr_id);


--
-- Name: contrb contrb_pkey; Type: CONSTRAINT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.contrb
    ADD CONSTRAINT contrb_pkey PRIMARY KEY (ctrb_id);


--
-- Name: convmult convmult_pkey; Type: CONSTRAINT; Schema: public; Owner: zona
--

ALTER TABLE ONLY public.convmult
    ADD CONSTRAINT convmult_pkey PRIMARY KEY (cmt_codemp, cmt_provin);


--
-- Name: conxclia conxcxlia_pkey; Type: CONSTRAINT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.conxclia
    ADD CONSTRAINT conxcxlia_pkey PRIMARY KEY (cxlia_id);


--
-- Name: cta_nivel cta_nivel_pkey; Type: CONSTRAINT; Schema: public; Owner: zona
--

ALTER TABLE ONLY public.cta_nivel
    ADD CONSTRAINT cta_nivel_pkey PRIMARY KEY (cn_id);


--
-- Name: ctacble ctacble_pkey; Type: CONSTRAINT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.ctacble
    ADD CONSTRAINT ctacble_pkey PRIMARY KEY (cbl_id);


--
-- Name: ctes ctes_pkey; Type: CONSTRAINT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.ctes
    ADD CONSTRAINT ctes_pkey PRIMARY KEY (cte_id);


--
-- Name: debitos debitos_pkey; Type: CONSTRAINT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.debitos
    ADD CONSTRAINT debitos_pkey PRIMARY KEY (db_id);


--
-- Name: descuentos_rubro descuentos_rubro_dr_cliente_dr_rubro_dr_codemp_key; Type: CONSTRAINT; Schema: public; Owner: zona
--

ALTER TABLE ONLY public.descuentos_rubro
    ADD CONSTRAINT descuentos_rubro_dr_cliente_dr_rubro_dr_codemp_key UNIQUE (dr_cliente, dr_rubro, dr_codemp);


--
-- Name: descuentos_rubro descuentos_rubro_pkey; Type: CONSTRAINT; Schema: public; Owner: zona
--

ALTER TABLE ONLY public.descuentos_rubro
    ADD CONSTRAINT descuentos_rubro_pkey PRIMARY KEY (dr_id);


--
-- Name: dispenser dispenser_pkey; Type: CONSTRAINT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.dispenser
    ADD CONSTRAINT dispenser_pkey PRIMARY KEY (dis_id);


--
-- Name: docum docum_pkey; Type: CONSTRAINT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.docum
    ADD CONSTRAINT docum_pkey PRIMARY KEY (doc_id);


--
-- Name: emiti emiti_pkey; Type: CONSTRAINT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.emiti
    ADD CONSTRAINT emiti_pkey PRIMARY KEY (emi_id);


--
-- Name: empresa empresa_pkey; Type: CONSTRAINT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.empresa
    ADD CONSTRAINT empresa_pkey PRIMARY KEY (emp_codigo);


--
-- Name: estados_reclamos estados_reclamos_pkey; Type: CONSTRAINT; Schema: public; Owner: zona
--

ALTER TABLE ONLY public.estados_reclamos
    ADD CONSTRAINT estados_reclamos_pkey PRIMARY KEY (est_id);


--
-- Name: estcivil estcivil_pkey; Type: CONSTRAINT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.estcivil
    ADD CONSTRAINT estcivil_pkey PRIMARY KEY (ec_codigo);


--
-- Name: fact_vencidas fact_vencidas_pkey; Type: CONSTRAINT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.fact_vencidas
    ADD CONSTRAINT fact_vencidas_pkey PRIMARY KEY (fv_id);


--
-- Name: firman firman_pkey; Type: CONSTRAINT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.firman
    ADD CONSTRAINT firman_pkey PRIMARY KEY (fir_id);


--
-- Name: grupos grupos_pkey; Type: CONSTRAINT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.grupos
    ADD CONSTRAINT grupos_pkey PRIMARY KEY (gpo_id);


--
-- Name: hojaruta hojaruta_pkey; Type: CONSTRAINT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.hojaruta
    ADD CONSTRAINT hojaruta_pkey PRIMARY KEY (hr_id);


--
-- Name: iibbfile iibbfile_pkey; Type: CONSTRAINT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.iibbfile
    ADD CONSTRAINT iibbfile_pkey PRIMARY KEY (if_id);


--
-- Name: indices indices_pkey; Type: CONSTRAINT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.indices
    ADD CONSTRAINT indices_pkey PRIMARY KEY (ind_codigo);


--
-- Name: interes interes_pkey; Type: CONSTRAINT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.interes
    ADD CONSTRAINT interes_pkey PRIMARY KEY (int_codemp, int_fecha);


--
-- Name: listas listas_pkey; Type: CONSTRAINT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.listas
    ADD CONSTRAINT listas_pkey PRIMARY KEY (li_codigo);


--
-- Name: listpre listpre_pkey; Type: CONSTRAINT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.listpre
    ADD CONSTRAINT listpre_pkey PRIMARY KEY (ltp_id);


--
-- Name: logs_carrito_cliente logs_carrito_cliente_pkey; Type: CONSTRAINT; Schema: public; Owner: zona
--

ALTER TABLE ONLY public.logs_carrito_cliente
    ADD CONSTRAINT logs_carrito_cliente_pkey PRIMARY KEY (log_id);


--
-- Name: marcas_auto marcas_auto_pkey; Type: CONSTRAINT; Schema: public; Owner: zona
--

ALTER TABLE ONLY public.marcas_auto
    ADD CONSTRAINT marcas_auto_pkey PRIMARY KEY (maraut_id);


--
-- Name: margenxarticu margenxarticu_pkey; Type: CONSTRAINT; Schema: public; Owner: estlm
--

ALTER TABLE ONLY public.margenxarticu
    ADD CONSTRAINT margenxarticu_pkey PRIMARY KEY (mxa_id);


--
-- Name: menues menues_pkey; Type: CONSTRAINT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.menues
    ADD CONSTRAINT menues_pkey PRIMARY KEY (men_id);


--
-- Name: modelos_auto modelos_auto_pkey; Type: CONSTRAINT; Schema: public; Owner: zona
--

ALTER TABLE ONLY public.modelos_auto
    ADD CONSTRAINT modelos_auto_pkey PRIMARY KEY (modaut_id);


--
-- Name: modlogs modlogs_pkey; Type: CONSTRAINT; Schema: public; Owner: zona
--

ALTER TABLE ONLY public.modlogs
    ADD CONSTRAINT modlogs_pkey PRIMARY KEY (log_id);


--
-- Name: modulos_sistema modulos_sistema_mod_seccion_mod_codemp_key; Type: CONSTRAINT; Schema: public; Owner: zona
--

ALTER TABLE ONLY public.modulos_sistema
    ADD CONSTRAINT modulos_sistema_mod_seccion_mod_codemp_key UNIQUE (mod_seccion, mod_codemp);


--
-- Name: modulos_sistema modulos_sistema_pkey; Type: CONSTRAINT; Schema: public; Owner: zona
--

ALTER TABLE ONLY public.modulos_sistema
    ADD CONSTRAINT modulos_sistema_pkey PRIMARY KEY (mod_id);


--
-- Name: moneda moneda_pkey; Type: CONSTRAINT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.moneda
    ADD CONSTRAINT moneda_pkey PRIMARY KEY (mon_codigo);


--
-- Name: monex monex_pkey; Type: CONSTRAINT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.monex
    ADD CONSTRAINT monex_pkey PRIMARY KEY (mne_id);


--
-- Name: movsleyen movsleyen_pkey; Type: CONSTRAINT; Schema: public; Owner: zona
--

ALTER TABLE ONLY public.movsleyen
    ADD CONSTRAINT movsleyen_pkey PRIMARY KEY (mvl_id);


--
-- Name: movsto movsto_pkey; Type: CONSTRAINT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.movsto
    ADD CONSTRAINT movsto_pkey PRIMARY KEY (movs_id);


--
-- Name: movstoc movstoc_pkey; Type: CONSTRAINT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.movstoc
    ADD CONSTRAINT movstoc_pkey PRIMARY KEY (movc_id);


--
-- Name: operadores_modulos operadores_modulos_om_operador_om_modulo_om_codemp_key; Type: CONSTRAINT; Schema: public; Owner: zona
--

ALTER TABLE ONLY public.operadores_modulos
    ADD CONSTRAINT operadores_modulos_om_operador_om_modulo_om_codemp_key UNIQUE (om_operador, om_modulo, om_codemp);


--
-- Name: operadores_modulos operadores_modulos_pkey; Type: CONSTRAINT; Schema: public; Owner: zona
--

ALTER TABLE ONLY public.operadores_modulos
    ADD CONSTRAINT operadores_modulos_pkey PRIMARY KEY (om_id);


--
-- Name: operadores operadores_pkey; Type: CONSTRAINT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.operadores
    ADD CONSTRAINT operadores_pkey PRIMARY KEY (operad_id);


--
-- Name: pais pais_pkey; Type: CONSTRAINT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.pais
    ADD CONSTRAINT pais_pkey PRIMARY KEY (pai_codigo);


--
-- Name: politmoneda politmoneda_pkey; Type: CONSTRAINT; Schema: public; Owner: zona
--

ALTER TABLE ONLY public.politmoneda
    ADD CONSTRAINT politmoneda_pkey PRIMARY KEY (pm_id);


--
-- Name: posiciones posiciones_pkey; Type: CONSTRAINT; Schema: public; Owner: zona
--

ALTER TABLE ONLY public.posiciones
    ADD CONSTRAINT posiciones_pkey PRIMARY KEY (pos_id);


--
-- Name: prove prove_pkey; Type: CONSTRAINT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.prove
    ADD CONSTRAINT prove_pkey PRIMARY KEY (prov_id);


--
-- Name: provin provin_pkey; Type: CONSTRAINT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.provin
    ADD CONSTRAINT provin_pkey PRIMARY KEY (pro_codigo);


--
-- Name: ptovta ptovta_pkey; Type: CONSTRAINT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.ptovta
    ADD CONSTRAINT ptovta_pkey PRIMARY KEY (ptv_id);


--
-- Name: pxarticu pxarticu_pkey; Type: CONSTRAINT; Schema: public; Owner: estlm
--

ALTER TABLE ONLY public.pxarticu
    ADD CONSTRAINT pxarticu_pkey PRIMARY KEY (pxa_id);


--
-- Name: receta receta_pkey; Type: CONSTRAINT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.receta
    ADD CONSTRAINT receta_pkey PRIMARY KEY (re_id);


--
-- Name: reclamos_adjuntos reclamos_adjuntos_pkey; Type: CONSTRAINT; Schema: public; Owner: zona
--

ALTER TABLE ONLY public.reclamos_adjuntos
    ADD CONSTRAINT reclamos_adjuntos_pkey PRIMARY KEY (adj_id);


--
-- Name: reclamos reclamos_pkey; Type: CONSTRAINT; Schema: public; Owner: zona
--

ALTER TABLE ONLY public.reclamos
    ADD CONSTRAINT reclamos_pkey PRIMARY KEY (rec_id);


--
-- Name: reclamos reclamos_rec_numero_key; Type: CONSTRAINT; Schema: public; Owner: zona
--

ALTER TABLE ONLY public.reclamos
    ADD CONSTRAINT reclamos_rec_numero_key UNIQUE (rec_numero);


--
-- Name: reclamos_seguimiento reclamos_seguimiento_pkey; Type: CONSTRAINT; Schema: public; Owner: zona
--

ALTER TABLE ONLY public.reclamos_seguimiento
    ADD CONSTRAINT reclamos_seguimiento_pkey PRIMARY KEY (seg_id);


--
-- Name: relcotnot relcotnot_pkey; Type: CONSTRAINT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.relcotnot
    ADD CONSTRAINT relcotnot_pkey PRIMARY KEY (rn_id);


--
-- Name: relmenu relmenu_pkey; Type: CONSTRAINT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.relmenu
    ADD CONSTRAINT relmenu_pkey PRIMARY KEY (rmen_id);


--
-- Name: relnotp relnotp_pkey; Type: CONSTRAINT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.relnotp
    ADD CONSTRAINT relnotp_pkey PRIMARY KEY (rn_id);


--
-- Name: relrem relrem_pkey; Type: CONSTRAINT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.relrem
    ADD CONSTRAINT relrem_pkey PRIMARY KEY (rr_id);


--
-- Name: relremc relremc_pkey; Type: CONSTRAINT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.relremc
    ADD CONSTRAINT relremc_pkey PRIMARY KEY (rr_id);


--
-- Name: rubcli rubcli_pkey; Type: CONSTRAINT; Schema: public; Owner: zona
--

ALTER TABLE ONLY public.rubcli
    ADD CONSTRAINT rubcli_pkey PRIMARY KEY (rb_id);


--
-- Name: rubros rubros_pkey; Type: CONSTRAINT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.rubros
    ADD CONSTRAINT rubros_pkey PRIMARY KEY (rub_id);


--
-- Name: rutaxastoh rutaxastoh_pkey; Type: CONSTRAINT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.rutaxastoh
    ADD CONSTRAINT rutaxastoh_pkey PRIMARY KEY (ra_id);


--
-- Name: stock stock_pkey; Type: CONSTRAINT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.stock
    ADD CONSTRAINT stock_pkey PRIMARY KEY (stc_id);


--
-- Name: stockinicial stockinicial_pkey; Type: CONSTRAINT; Schema: public; Owner: zona
--

ALTER TABLE ONLY public.stockinicial
    ADD CONSTRAINT stockinicial_pkey PRIMARY KEY (stk_id);


--
-- Name: subxcomp subxcomp_pkey; Type: CONSTRAINT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.subxcomp
    ADD CONSTRAINT subxcomp_pkey PRIMARY KEY (subxc_idsub, subxc_comprob);


--
-- Name: sucursalc sucursalc_pkey; Type: CONSTRAINT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.sucursalc
    ADD CONSTRAINT sucursalc_pkey PRIMARY KEY (sucu_cliente, sucu_codigo);


--
-- Name: sucursalp sucursalp_pkey; Type: CONSTRAINT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.sucursalp
    ADD CONSTRAINT sucursalp_pkey PRIMARY KEY (suc_prove, suc_codigo);


--
-- Name: talon talon_pkey; Type: CONSTRAINT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.talon
    ADD CONSTRAINT talon_pkey PRIMARY KEY (tal_id);


--
-- Name: telefon telefon_pkey; Type: CONSTRAINT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.telefon
    ADD CONSTRAINT telefon_pkey PRIMARY KEY (tel_id);


--
-- Name: tipasi tipasi_pkey; Type: CONSTRAINT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.tipasi
    ADD CONSTRAINT tipasi_pkey PRIMARY KEY (tas_codemp, tas_codigo);


--
-- Name: tipcta tipcta_pkey; Type: CONSTRAINT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.tipcta
    ADD CONSTRAINT tipcta_pkey PRIMARY KEY (tct_codigo);


--
-- Name: tipdocu tipdocu_pkey; Type: CONSTRAINT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.tipdocu
    ADD CONSTRAINT tipdocu_pkey PRIMARY KEY (td_codigo);


--
-- Name: tipemp tipemp_pkey; Type: CONSTRAINT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.tipemp
    ADD CONSTRAINT tipemp_pkey PRIMARY KEY (tem_codigo);


--
-- Name: tipentid tipentid_pkey; Type: CONSTRAINT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.tipentid
    ADD CONSTRAINT tipentid_pkey PRIMARY KEY (tet_codigo);


--
-- Name: tipos_reclamos tipos_reclamos_pkey; Type: CONSTRAINT; Schema: public; Owner: zona
--

ALTER TABLE ONLY public.tipos_reclamos
    ADD CONSTRAINT tipos_reclamos_pkey PRIMARY KEY (tpr_id);


--
-- Name: tiptel tiptel_pkey; Type: CONSTRAINT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.tiptel
    ADD CONSTRAINT tiptel_pkey PRIMARY KEY (ttl_codigo);


--
-- Name: tributosfe tributosfe_pkey; Type: CONSTRAINT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.tributosfe
    ADD CONSTRAINT tributosfe_pkey PRIMARY KEY (t_id);


--
-- Name: unimed unimed_pkey; Type: CONSTRAINT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.unimed
    ADD CONSTRAINT unimed_pkey PRIMARY KEY (uni_codigo);


--
-- Name: user_theme user_theme_pkey; Type: CONSTRAINT; Schema: public; Owner: zona
--

ALTER TABLE ONLY public.user_theme
    ADD CONSTRAINT user_theme_pkey PRIMARY KEY (ut_operador_id);


--
-- Name: usermenu usermenu_pkey; Type: CONSTRAINT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.usermenu
    ADD CONSTRAINT usermenu_pkey PRIMARY KEY (umen_id);


--
-- Name: varambi varambi_pkey; Type: CONSTRAINT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.varambi
    ADD CONSTRAINT varambi_pkey PRIMARY KEY (vari_codemp, vari_idptov, vari_codigo);


--
-- Name: vendedor vendedor_pkey; Type: CONSTRAINT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.vendedor
    ADD CONSTRAINT vendedor_pkey PRIMARY KEY (ven_id);


--
-- Name: carritos_cliente_cliente_id_key; Type: INDEX; Schema: public; Owner: zona
--

CREATE UNIQUE INDEX carritos_cliente_cliente_id_key ON public.carritos_cliente USING btree (cliente_id);


--
-- Name: idx_aad_emp_rub; Type: INDEX; Schema: public; Owner: zona
--

CREATE INDEX idx_aad_emp_rub ON public.art_atrib_def USING btree (aad_codemp, aad_rubro, aad_activo);


--
-- Name: idx_aav_art; Type: INDEX; Schema: public; Owner: zona
--

CREATE INDEX idx_aav_art ON public.art_atrib_val USING btree (aav_artid);


--
-- Name: idx_aavm_art; Type: INDEX; Schema: public; Owner: zona
--

CREATE INDEX idx_aavm_art ON public.art_atrib_valm USING btree (aavm_artid, aavm_defid);


--
-- Name: idx_adjuntos_reclamo; Type: INDEX; Schema: public; Owner: zona
--

CREATE INDEX idx_adjuntos_reclamo ON public.reclamos_adjuntos USING btree (adj_reclamo_id);


--
-- Name: idx_app_artid; Type: INDEX; Schema: public; Owner: zona
--

CREATE INDEX idx_app_artid ON public.art_aplicaciones USING btree (app_artid);


--
-- Name: idx_app_empresa; Type: INDEX; Schema: public; Owner: zona
--

CREATE INDEX idx_app_empresa ON public.art_aplicaciones USING btree (app_codemp);


--
-- Name: idx_app_marca_modelo; Type: INDEX; Schema: public; Owner: zona
--

CREATE INDEX idx_app_marca_modelo ON public.art_aplicaciones USING btree (app_marca_id, app_modelo_id);


--
-- Name: idx_articu_1; Type: INDEX; Schema: public; Owner: rulemanes
--

CREATE UNIQUE INDEX idx_articu_1 ON public.articu USING btree (artc_codemp, artc_codigo);


--
-- Name: idx_articvta_1; Type: INDEX; Schema: public; Owner: rulemanes
--

CREATE UNIQUE INDEX idx_articvta_1 ON public.articvta USING btree (art_codemp, art_codigo);


--
-- Name: idx_astoctas_1; Type: INDEX; Schema: public; Owner: rulemanes
--

CREATE INDEX idx_astoctas_1 ON public.astoctas USING btree (astc_idastoh);


--
-- Name: idx_astoctas_10; Type: INDEX; Schema: public; Owner: rulemanes
--

CREATE INDEX idx_astoctas_10 ON public.astoctas USING btree (astc_codemp, astc_idptov, astc_tipcta, astc_ctacte);


--
-- Name: idx_astoctas_11; Type: INDEX; Schema: public; Owner: rulemanes
--

CREATE INDEX idx_astoctas_11 ON public.astoctas USING btree (astc_codemp, astc_idptov, astc_tipcta, astc_ctacte, astc_cierra, astc_fecval);


--
-- Name: idx_astoctas_12; Type: INDEX; Schema: public; Owner: rulemanes
--

CREATE INDEX idx_astoctas_12 ON public.astoctas USING btree (astc_codemp, astc_ctacble, astc_moneda);


--
-- Name: idx_astoctas_13; Type: INDEX; Schema: public; Owner: rulemanes
--

CREATE INDEX idx_astoctas_13 ON public.astoctas USING btree (astc_codemp, astc_idptov, astc_ctacble, astc_estado, astc_fecval);


--
-- Name: idx_astoctas_14; Type: INDEX; Schema: public; Owner: rulemanes
--

CREATE INDEX idx_astoctas_14 ON public.astoctas USING btree (astc_codemp, astc_ctacble, astc_estado, astc_fecval);


--
-- Name: idx_astoctas_15; Type: INDEX; Schema: public; Owner: rulemanes
--

CREATE INDEX idx_astoctas_15 ON public.astoctas USING btree (astc_codemp, astc_estado, astc_tipcta, astc_ctacte);


--
-- Name: idx_astoctas_2; Type: INDEX; Schema: public; Owner: rulemanes
--

CREATE INDEX idx_astoctas_2 ON public.astoctas USING btree (astc_codemp, astc_ctacble, astc_fecval);


--
-- Name: idx_astoctas_3; Type: INDEX; Schema: public; Owner: rulemanes
--

CREATE INDEX idx_astoctas_3 ON public.astoctas USING btree (astc_codemp, astc_idptov, astc_ctacble, astc_fecval);


--
-- Name: idx_astoctas_4; Type: INDEX; Schema: public; Owner: rulemanes
--

CREATE INDEX idx_astoctas_4 ON public.astoctas USING btree (astc_codemp, astc_ctacble, astc_ctacte, astc_fecval);


--
-- Name: idx_astoctas_5; Type: INDEX; Schema: public; Owner: rulemanes
--

CREATE INDEX idx_astoctas_5 ON public.astoctas USING btree (astc_codemp, astc_ctacble, astc_ctacte, astc_cierra, astc_fecval);


--
-- Name: idx_astoctas_6; Type: INDEX; Schema: public; Owner: rulemanes
--

CREATE INDEX idx_astoctas_6 ON public.astoctas USING btree (astc_codemp, astc_ctacble, astc_ctacte, astc_cierra, astc_fecvto);


--
-- Name: idx_astoctas_7; Type: INDEX; Schema: public; Owner: rulemanes
--

CREATE INDEX idx_astoctas_7 ON public.astoctas USING btree (astc_ctacble, astc_fecval);


--
-- Name: idx_astoctas_8; Type: INDEX; Schema: public; Owner: rulemanes
--

CREATE INDEX idx_astoctas_8 ON public.astoctas USING btree (astc_codemp, astc_idptov, astc_ctacble, astc_ctacte, astc_grupo);


--
-- Name: idx_astoctas_9; Type: INDEX; Schema: public; Owner: rulemanes
--

CREATE INDEX idx_astoctas_9 ON public.astoctas USING btree (astc_codemp, astc_idptov, astc_comprob, astc_numero);


--
-- Name: idx_astoh_1; Type: INDEX; Schema: public; Owner: rulemanes
--

CREATE UNIQUE INDEX idx_astoh_1 ON public.astoh USING btree (ash_codemp, ash_idptov, ash_comprob, ash_numero);


--
-- Name: idx_astoh_2; Type: INDEX; Schema: public; Owner: rulemanes
--

CREATE INDEX idx_astoh_2 ON public.astoh USING btree (ash_codemp, ash_anio, ash_mes, ash_fecomp);


--
-- Name: idx_astoh_3; Type: INDEX; Schema: public; Owner: rulemanes
--

CREATE INDEX idx_astoh_3 ON public.astoh USING btree (ash_codemp, ash_idptov, ash_anio, ash_mes, ash_fecomp);


--
-- Name: idx_astoh_4; Type: INDEX; Schema: public; Owner: rulemanes
--

CREATE INDEX idx_astoh_4 ON public.astoh USING btree (ash_idemiti);


--
-- Name: idx_astoh_5; Type: INDEX; Schema: public; Owner: rulemanes
--

CREATE INDEX idx_astoh_5 ON public.astoh USING btree (ash_idcpra);


--
-- Name: idx_auxfe_1; Type: INDEX; Schema: public; Owner: zona
--

CREATE INDEX idx_auxfe_1 ON public.auxfe USING btree (fe_estado, fe_fechaf, fe_idcli);


--
-- Name: idx_cheques_1; Type: INDEX; Schema: public; Owner: rulemanes
--

CREATE INDEX idx_cheques_1 ON public.cheques USING btree (chq_codemp, chq_estado);


--
-- Name: idx_cheques_10; Type: INDEX; Schema: public; Owner: rulemanes
--

CREATE INDEX idx_cheques_10 ON public.cheques USING btree (chq_moneda, chq_codemp, chq_estado, chq_fecheq);


--
-- Name: idx_cheques_2; Type: INDEX; Schema: public; Owner: rulemanes
--

CREATE INDEX idx_cheques_2 ON public.cheques USING btree (chq_codemp, chq_idptov, chq_estado);


--
-- Name: idx_cheques_3; Type: INDEX; Schema: public; Owner: rulemanes
--

CREATE INDEX idx_cheques_3 ON public.cheques USING btree (chq_codbco, chq_nucheq);


--
-- Name: idx_cheques_4; Type: INDEX; Schema: public; Owner: rulemanes
--

CREATE INDEX idx_cheques_4 ON public.cheques USING btree (chq_idastoc);


--
-- Name: idx_cheques_5; Type: INDEX; Schema: public; Owner: rulemanes
--

CREATE INDEX idx_cheques_5 ON public.cheques USING btree (chq_idastoce);


--
-- Name: idx_cheques_6; Type: INDEX; Schema: public; Owner: rulemanes
--

CREATE INDEX idx_cheques_6 ON public.cheques USING btree (chq_estado);


--
-- Name: idx_cheques_7; Type: INDEX; Schema: public; Owner: rulemanes
--

CREATE INDEX idx_cheques_7 ON public.cheques USING btree (chq_codemp, chq_estado, chq_fecheq);


--
-- Name: idx_cheques_8; Type: INDEX; Schema: public; Owner: rulemanes
--

CREATE INDEX idx_cheques_8 ON public.cheques USING btree (chq_estado, chq_fecheq);


--
-- Name: idx_cheques_9; Type: INDEX; Schema: public; Owner: rulemanes
--

CREATE INDEX idx_cheques_9 ON public.cheques USING btree (chq_moneda);


--
-- Name: idx_clientes_1; Type: INDEX; Schema: public; Owner: rulemanes
--

CREATE UNIQUE INDEX idx_clientes_1 ON public.clientes USING btree (cli_codemp, cli_idptov, cli_codigo);


--
-- Name: idx_clientes_2; Type: INDEX; Schema: public; Owner: rulemanes
--

CREATE INDEX idx_clientes_2 ON public.clientes USING btree (cli_codemp, cli_codigo);


--
-- Name: idx_clientes_3; Type: INDEX; Schema: public; Owner: rulemanes
--

CREATE INDEX idx_clientes_3 ON public.clientes USING btree (cli_nrcuit);


--
-- Name: idx_clientes_4; Type: INDEX; Schema: public; Owner: rulemanes
--

CREATE INDEX idx_clientes_4 ON public.clientes USING btree (cli_nombre);


--
-- Name: idx_clientes_5; Type: INDEX; Schema: public; Owner: rulemanes
--

CREATE INDEX idx_clientes_5 ON public.clientes USING btree (cli_id, cli_estado);


--
-- Name: idx_compra_1; Type: INDEX; Schema: public; Owner: rulemanes
--

CREATE UNIQUE INDEX idx_compra_1 ON public.compra USING btree (cpra_codemp, cpra_idptov, cpra_comprob, cpra_numero);


--
-- Name: idx_compra_2; Type: INDEX; Schema: public; Owner: rulemanes
--

CREATE INDEX idx_compra_2 ON public.compra USING btree (cpra_codemp, cpra_fecval);


--
-- Name: idx_compra_3; Type: INDEX; Schema: public; Owner: rulemanes
--

CREATE INDEX idx_compra_3 ON public.compra USING btree (cpra_idastoh);


--
-- Name: idx_comprob_1; Type: INDEX; Schema: public; Owner: rulemanes
--

CREATE UNIQUE INDEX idx_comprob_1 ON public.comprob USING btree (comp_codemp, comp_idptov, comp_codigo);


--
-- Name: idx_comprob_2; Type: INDEX; Schema: public; Owner: rulemanes
--

CREATE INDEX idx_comprob_2 ON public.comprob USING btree (comp_codemp, comp_idptov, comp_siste, comp_codigo);


--
-- Name: idx_concilia_1; Type: INDEX; Schema: public; Owner: rulemanes
--

CREATE INDEX idx_concilia_1 ON public.concilia USING btree (clia_cte, clia_fecha);


--
-- Name: idx_conpag_1; Type: INDEX; Schema: public; Owner: rulemanes
--

CREATE INDEX idx_conpag_1 ON public.conpag USING btree (cdp_codemp, cdp_codigo);


--
-- Name: idx_contratos_1; Type: INDEX; Schema: public; Owner: rulemanes
--

CREATE INDEX idx_contratos_1 ON public.contratos USING btree (ctr_idcli);


--
-- Name: idx_contrb_1; Type: INDEX; Schema: public; Owner: rulemanes
--

CREATE INDEX idx_contrb_1 ON public.contrb USING btree (ctrb_idcontr);


--
-- Name: idx_contrb_2; Type: INDEX; Schema: public; Owner: rulemanes
--

CREATE INDEX idx_contrb_2 ON public.contrb USING btree (ctrb_idcli);


--
-- Name: idx_contrb_3; Type: INDEX; Schema: public; Owner: rulemanes
--

CREATE INDEX idx_contrb_3 ON public.contrb USING btree (ctrb_iddispen);


--
-- Name: idx_conxcxlia_1; Type: INDEX; Schema: public; Owner: rulemanes
--

CREATE INDEX idx_conxcxlia_1 ON public.conxclia USING btree (cxlia_idclia);


--
-- Name: idx_conxcxlia_2; Type: INDEX; Schema: public; Owner: rulemanes
--

CREATE INDEX idx_conxcxlia_2 ON public.conxclia USING btree (cxlia_idastc);


--
-- Name: idx_cta_nivel_emp_nivel; Type: INDEX; Schema: public; Owner: zona
--

CREATE INDEX idx_cta_nivel_emp_nivel ON public.cta_nivel USING btree (cn_codemp, cn_nivel);


--
-- Name: idx_cta_nivel_emp_padre; Type: INDEX; Schema: public; Owner: zona
--

CREATE INDEX idx_cta_nivel_emp_padre ON public.cta_nivel USING btree (cn_codemp, cn_padre);


--
-- Name: idx_ctacble_1; Type: INDEX; Schema: public; Owner: rulemanes
--

CREATE UNIQUE INDEX idx_ctacble_1 ON public.ctacble USING btree (cbl_codemp, cbl_codigo);


--
-- Name: idx_ctacble_2; Type: INDEX; Schema: public; Owner: rulemanes
--

CREATE INDEX idx_ctacble_2 ON public.ctacble USING btree (cbl_codemp, cbl_posic);


--
-- Name: idx_ctes_1; Type: INDEX; Schema: public; Owner: rulemanes
--

CREATE UNIQUE INDEX idx_ctes_1 ON public.ctes USING btree (cte_codemp, cte_tipcta, cte_tipct, cte_codigo);


--
-- Name: idx_dispenser_1; Type: INDEX; Schema: public; Owner: rulemanes
--

CREATE INDEX idx_dispenser_1 ON public.dispenser USING btree (dis_idcontr);


--
-- Name: idx_dispenser_2; Type: INDEX; Schema: public; Owner: rulemanes
--

CREATE INDEX idx_dispenser_2 ON public.dispenser USING btree (dis_idprov);


--
-- Name: idx_docum_1; Type: INDEX; Schema: public; Owner: rulemanes
--

CREATE INDEX idx_docum_1 ON public.docum USING btree (doc_codemp, doc_nrcuit);


--
-- Name: idx_docum_2; Type: INDEX; Schema: public; Owner: rulemanes
--

CREATE INDEX idx_docum_2 ON public.docum USING btree (doc_codemp, doc_estado, doc_nrcuit);


--
-- Name: idx_docum_3; Type: INDEX; Schema: public; Owner: rulemanes
--

CREATE INDEX idx_docum_3 ON public.docum USING btree (doc_codemp, doc_idptov, doc_estado);


--
-- Name: idx_docum_4; Type: INDEX; Schema: public; Owner: rulemanes
--

CREATE INDEX idx_docum_4 ON public.docum USING btree (doc_idastoc);


--
-- Name: idx_docum_5; Type: INDEX; Schema: public; Owner: rulemanes
--

CREATE INDEX idx_docum_5 ON public.docum USING btree (doc_idastoce);


--
-- Name: idx_docum_6; Type: INDEX; Schema: public; Owner: rulemanes
--

CREATE INDEX idx_docum_6 ON public.docum USING btree (doc_codemp, doc_estado);


--
-- Name: idx_emiti_1; Type: INDEX; Schema: public; Owner: rulemanes
--

CREATE UNIQUE INDEX idx_emiti_1 ON public.emiti USING btree (emi_codemp, emi_idptov, emi_comprob, emi_numero);


--
-- Name: idx_emiti_2; Type: INDEX; Schema: public; Owner: rulemanes
--

CREATE INDEX idx_emiti_2 ON public.emiti USING btree (emi_codemp, emi_fecval);


--
-- Name: idx_emiti_3; Type: INDEX; Schema: public; Owner: rulemanes
--

CREATE INDEX idx_emiti_3 ON public.emiti USING btree (emi_idastoh);


--
-- Name: idx_empresa_1; Type: INDEX; Schema: public; Owner: rulemanes
--

CREATE UNIQUE INDEX idx_empresa_1 ON public.empresa USING btree (emp_nrcuit);


--
-- Name: idx_firman_1; Type: INDEX; Schema: public; Owner: rulemanes
--

CREATE UNIQUE INDEX idx_firman_1 ON public.firman USING btree (fir_codemp, fir_nrcuit);


--
-- Name: idx_firman_2; Type: INDEX; Schema: public; Owner: rulemanes
--

CREATE INDEX idx_firman_2 ON public.firman USING btree (fir_nrcuit);


--
-- Name: idx_listpre_2; Type: INDEX; Schema: public; Owner: rulemanes
--

CREATE INDEX idx_listpre_2 ON public.listpre USING btree (ltp_codemp, ltp_idptov, ltp_estado, ltp_idcli, ltp_nrolista, ltp_idart, ltp_orden);


--
-- Name: idx_logs_accion_fecha; Type: INDEX; Schema: public; Owner: zona
--

CREATE INDEX idx_logs_accion_fecha ON public.logs_carrito_cliente USING btree (log_accion, log_fecha);


--
-- Name: idx_logs_cliente_fecha; Type: INDEX; Schema: public; Owner: zona
--

CREATE INDEX idx_logs_cliente_fecha ON public.logs_carrito_cliente USING btree (log_cliente_id, log_fecha);


--
-- Name: idx_maraut_emp; Type: INDEX; Schema: public; Owner: zona
--

CREATE INDEX idx_maraut_emp ON public.marcas_auto USING btree (maraut_codemp);


--
-- Name: idx_modaut_marca; Type: INDEX; Schema: public; Owner: zona
--

CREATE INDEX idx_modaut_marca ON public.modelos_auto USING btree (modaut_marca);


--
-- Name: idx_modlogs_1; Type: INDEX; Schema: public; Owner: zona
--

CREATE INDEX idx_modlogs_1 ON public.modlogs USING btree (log_idptov, log_tabla);


--
-- Name: idx_modlogs_2; Type: INDEX; Schema: public; Owner: zona
--

CREATE INDEX idx_modlogs_2 ON public.modlogs USING btree (log_comprob, log_numero);


--
-- Name: idx_modlogs_3; Type: INDEX; Schema: public; Owner: zona
--

CREATE INDEX idx_modlogs_3 ON public.modlogs USING btree (log_tabla, log_idtabla);


--
-- Name: idx_monex_1; Type: INDEX; Schema: public; Owner: rulemanes
--

CREATE INDEX idx_monex_1 ON public.monex USING btree (mne_idastoc);


--
-- Name: idx_monex_2; Type: INDEX; Schema: public; Owner: rulemanes
--

CREATE INDEX idx_monex_2 ON public.monex USING btree (mne_idastoce);


--
-- Name: idx_movsto_1; Type: INDEX; Schema: public; Owner: rulemanes
--

CREATE INDEX idx_movsto_1 ON public.movsto USING btree (movs_idemiti);


--
-- Name: idx_movsto_10; Type: INDEX; Schema: public; Owner: rulemanes
--

CREATE INDEX idx_movsto_10 ON public.movsto USING btree (movs_idptov, movs_codcli, movs_idastoh, movs_estado, movs_canti, movs_fecval);


--
-- Name: idx_movsto_2; Type: INDEX; Schema: public; Owner: rulemanes
--

CREATE INDEX idx_movsto_2 ON public.movsto USING btree (movs_idastoh);


--
-- Name: idx_movsto_3; Type: INDEX; Schema: public; Owner: rulemanes
--

CREATE INDEX idx_movsto_3 ON public.movsto USING btree (movs_artic);


--
-- Name: idx_movsto_4; Type: INDEX; Schema: public; Owner: rulemanes
--

CREATE INDEX idx_movsto_4 ON public.movsto USING btree (movs_codemp, movs_idptov, movs_artic);


--
-- Name: idx_movsto_5; Type: INDEX; Schema: public; Owner: rulemanes
--

CREATE INDEX idx_movsto_5 ON public.movsto USING btree (movs_idastoh, movs_artic);


--
-- Name: idx_movsto_6; Type: INDEX; Schema: public; Owner: rulemanes
--

CREATE INDEX idx_movsto_6 ON public.movsto USING btree (movs_codemp, movs_idptov, movs_artic, movs_fecval);


--
-- Name: idx_movsto_7; Type: INDEX; Schema: public; Owner: rulemanes
--

CREATE INDEX idx_movsto_7 ON public.movsto USING btree (movs_idptov, movs_idastoh, movs_fecval, movs_codcli, movs_canti);


--
-- Name: idx_movsto_8; Type: INDEX; Schema: public; Owner: rulemanes
--

CREATE INDEX idx_movsto_8 ON public.movsto USING btree (movs_idptov, movs_idastoh, movs_fecval, movs_codcli, movs_importe);


--
-- Name: idx_movsto_9; Type: INDEX; Schema: public; Owner: rulemanes
--

CREATE INDEX idx_movsto_9 ON public.movsto USING btree (movs_idptov, movs_codcli, movs_idastoh, movs_estado, movs_importe, movs_fecval);


--
-- Name: idx_movstoc_1; Type: INDEX; Schema: public; Owner: rulemanes
--

CREATE INDEX idx_movstoc_1 ON public.movstoc USING btree (movc_idcompra);


--
-- Name: idx_movstoc_2; Type: INDEX; Schema: public; Owner: rulemanes
--

CREATE INDEX idx_movstoc_2 ON public.movstoc USING btree (movc_idastoh);


--
-- Name: idx_movstoc_3; Type: INDEX; Schema: public; Owner: rulemanes
--

CREATE INDEX idx_movstoc_3 ON public.movstoc USING btree (movc_artic);


--
-- Name: idx_movstoc_4; Type: INDEX; Schema: public; Owner: rulemanes
--

CREATE INDEX idx_movstoc_4 ON public.movstoc USING btree (movc_codemp, movc_idptov, movc_artic);


--
-- Name: idx_movstoc_5; Type: INDEX; Schema: public; Owner: rulemanes
--

CREATE INDEX idx_movstoc_5 ON public.movstoc USING btree (movc_idastoh, movc_artic);


--
-- Name: idx_movstoc_6; Type: INDEX; Schema: public; Owner: rulemanes
--

CREATE INDEX idx_movstoc_6 ON public.movstoc USING btree (movc_codemp, movc_idptov, movc_artic, movc_fecval);


--
-- Name: idx_novemplre_1; Type: INDEX; Schema: public; Owner: rulemanes
--

CREATE INDEX idx_novemplre_1 ON public.receta USING btree (re_codemp, re_idptov, re_articv);


--
-- Name: idx_operadores_2; Type: INDEX; Schema: public; Owner: rulemanes
--

CREATE UNIQUE INDEX idx_operadores_2 ON public.operadores USING btree (operad_login);


--
-- Name: idx_pm_codemp_tiponivel; Type: INDEX; Schema: public; Owner: zona
--

CREATE INDEX idx_pm_codemp_tiponivel ON public.politmoneda USING btree (pm_codemp, pm_tiponivel, pm_idnivel, pm_tipo);


--
-- Name: idx_prove_2; Type: INDEX; Schema: public; Owner: rulemanes
--

CREATE INDEX idx_prove_2 ON public.prove USING btree (prov_nrcuit);


--
-- Name: idx_prove_3; Type: INDEX; Schema: public; Owner: rulemanes
--

CREATE INDEX idx_prove_3 ON public.prove USING btree (prov_codemp, prov_codigo);


--
-- Name: idx_ptovta_1; Type: INDEX; Schema: public; Owner: rulemanes
--

CREATE UNIQUE INDEX idx_ptovta_1 ON public.ptovta USING btree (ptv_codemp, ptv_codigo);


--
-- Name: idx_reclamos_cliente; Type: INDEX; Schema: public; Owner: zona
--

CREATE INDEX idx_reclamos_cliente ON public.reclamos USING btree (rec_cliente_id);


--
-- Name: idx_reclamos_estado; Type: INDEX; Schema: public; Owner: zona
--

CREATE INDEX idx_reclamos_estado ON public.reclamos USING btree (rec_estado);


--
-- Name: idx_reclamos_fecha; Type: INDEX; Schema: public; Owner: zona
--

CREATE INDEX idx_reclamos_fecha ON public.reclamos USING btree (rec_fecha_creacion);


--
-- Name: idx_reclamos_numero; Type: INDEX; Schema: public; Owner: zona
--

CREATE INDEX idx_reclamos_numero ON public.reclamos USING btree (rec_numero);


--
-- Name: idx_reclamos_pedido; Type: INDEX; Schema: public; Owner: zona
--

CREATE INDEX idx_reclamos_pedido ON public.reclamos USING btree (rec_pedido_id);


--
-- Name: idx_reclamos_tipo; Type: INDEX; Schema: public; Owner: zona
--

CREATE INDEX idx_reclamos_tipo ON public.reclamos USING btree (rec_tipo);


--
-- Name: idx_relcotnot_1; Type: INDEX; Schema: public; Owner: rulemanes
--

CREATE INDEX idx_relcotnot_1 ON public.relcotnot USING btree (rn_idemitip);


--
-- Name: idx_relcotnot_2; Type: INDEX; Schema: public; Owner: rulemanes
--

CREATE INDEX idx_relcotnot_2 ON public.relcotnot USING btree (rn_idemitip, rn_idemitih);


--
-- Name: idx_relcotnot_3; Type: INDEX; Schema: public; Owner: rulemanes
--

CREATE INDEX idx_relcotnot_3 ON public.relcotnot USING btree (rn_idemitih);


--
-- Name: idx_relcotnot_4; Type: INDEX; Schema: public; Owner: rulemanes
--

CREATE INDEX idx_relcotnot_4 ON public.relcotnot USING btree (rn_idemitip, rn_estado);


--
-- Name: idx_relnotp_1; Type: INDEX; Schema: public; Owner: rulemanes
--

CREATE INDEX idx_relnotp_1 ON public.relnotp USING btree (rn_idemitip);


--
-- Name: idx_relnotp_2; Type: INDEX; Schema: public; Owner: rulemanes
--

CREATE INDEX idx_relnotp_2 ON public.relnotp USING btree (rn_idemitip, rn_idemitih);


--
-- Name: idx_relnotp_3; Type: INDEX; Schema: public; Owner: rulemanes
--

CREATE INDEX idx_relnotp_3 ON public.relnotp USING btree (rn_idemitih);


--
-- Name: idx_relnotp_4; Type: INDEX; Schema: public; Owner: rulemanes
--

CREATE INDEX idx_relnotp_4 ON public.relnotp USING btree (rn_idemitip, rn_estado);


--
-- Name: idx_relrem_1; Type: INDEX; Schema: public; Owner: rulemanes
--

CREATE INDEX idx_relrem_1 ON public.relrem USING btree (rr_idastohp);


--
-- Name: idx_relrem_2; Type: INDEX; Schema: public; Owner: rulemanes
--

CREATE INDEX idx_relrem_2 ON public.relrem USING btree (rr_idastohp, rr_idastohh);


--
-- Name: idx_relrem_3; Type: INDEX; Schema: public; Owner: rulemanes
--

CREATE INDEX idx_relrem_3 ON public.relrem USING btree (rr_idastohh);


--
-- Name: idx_relrem_4; Type: INDEX; Schema: public; Owner: rulemanes
--

CREATE INDEX idx_relrem_4 ON public.relrem USING btree (rr_idastohp, rr_estado);


--
-- Name: idx_rtoxcontr_1; Type: INDEX; Schema: public; Owner: rulemanes
--

CREATE INDEX idx_rtoxcontr_1 ON public.rtoxcontr USING btree (rtc_idcontr);


--
-- Name: idx_rtoxcontr_2; Type: INDEX; Schema: public; Owner: rulemanes
--

CREATE INDEX idx_rtoxcontr_2 ON public.rtoxcontr USING btree (rtc_idemiti);


--
-- Name: idx_rubros_1; Type: INDEX; Schema: public; Owner: rulemanes
--

CREATE UNIQUE INDEX idx_rubros_1 ON public.rubros USING btree (rub_tipo, rub_codigo);


--
-- Name: idx_seguimiento_reclamo; Type: INDEX; Schema: public; Owner: zona
--

CREATE INDEX idx_seguimiento_reclamo ON public.reclamos_seguimiento USING btree (seg_reclamo_id);


--
-- Name: idx_stock_1; Type: INDEX; Schema: public; Owner: rulemanes
--

CREATE INDEX idx_stock_1 ON public.stock USING btree (stc_codemp, stc_estado, stc_idart);


--
-- Name: idx_stock_2; Type: INDEX; Schema: public; Owner: rulemanes
--

CREATE INDEX idx_stock_2 ON public.stock USING btree (stc_codemp, stc_estado, stc_idartc);


--
-- Name: idx_stock_3; Type: INDEX; Schema: public; Owner: rulemanes
--

CREATE INDEX idx_stock_3 ON public.stock USING btree (stc_idcompra);


--
-- Name: idx_stock_4; Type: INDEX; Schema: public; Owner: rulemanes
--

CREATE INDEX idx_stock_4 ON public.stock USING btree (stc_idemiti);


--
-- Name: idx_stock_5; Type: INDEX; Schema: public; Owner: rulemanes
--

CREATE INDEX idx_stock_5 ON public.stock USING btree (stc_idastoh);


--
-- Name: idx_stock_6; Type: INDEX; Schema: public; Owner: rulemanes
--

CREATE INDEX idx_stock_6 ON public.stock USING btree (stc_idmovsto);


--
-- Name: idx_stock_7; Type: INDEX; Schema: public; Owner: rulemanes
--

CREATE INDEX idx_stock_7 ON public.stock USING btree (stc_idmovstoc);


--
-- Name: idx_stock_8; Type: INDEX; Schema: public; Owner: rulemanes
--

CREATE INDEX idx_stock_8 ON public.stock USING btree (stc_codemp, stc_estado, stc_idart, stc_fecval);


--
-- Name: idx_subd_1; Type: INDEX; Schema: public; Owner: rulemanes
--

CREATE INDEX idx_subd_1 ON public.subd USING btree (subd_codemp, subd_idptov);


--
-- Name: idx_talon_1; Type: INDEX; Schema: public; Owner: rulemanes
--

CREATE UNIQUE INDEX idx_talon_1 ON public.talon USING btree (tal_idptov, tal_comprob);


--
-- Name: idx_telefon_1; Type: INDEX; Schema: public; Owner: rulemanes
--

CREATE INDEX idx_telefon_1 ON public.telefon USING btree (tel_codemp, tel_tipent, tel_codigo, tel_tiptel);


--
-- Name: idx_telefon_2; Type: INDEX; Schema: public; Owner: rulemanes
--

CREATE INDEX idx_telefon_2 ON public.telefon USING btree (tel_dato);


--
-- Name: idx_telefon_3; Type: INDEX; Schema: public; Owner: rulemanes
--

CREATE INDEX idx_telefon_3 ON public.telefon USING btree (tel_contact);


--
-- Name: articvta ai_articvta; Type: TRIGGER; Schema: public; Owner: rulemanes
--

CREATE TRIGGER ai_articvta AFTER INSERT ON public.articvta FOR EACH ROW EXECUTE FUNCTION public.f_ai_articvta();


--
-- Name: astoctas ai_astoctas; Type: TRIGGER; Schema: public; Owner: rulemanes
--

CREATE TRIGGER ai_astoctas AFTER INSERT ON public.astoctas FOR EACH ROW EXECUTE FUNCTION public.f_ai_astoctas();


--
-- Name: clientes ai_clientes; Type: TRIGGER; Schema: public; Owner: rulemanes
--

CREATE TRIGGER ai_clientes AFTER INSERT ON public.clientes FOR EACH ROW EXECUTE FUNCTION public.f_ai_clientes();


--
-- Name: empresa ai_empresa; Type: TRIGGER; Schema: public; Owner: rulemanes
--

CREATE TRIGGER ai_empresa AFTER INSERT ON public.empresa FOR EACH ROW EXECUTE FUNCTION public.f_ai_empresa();


--
-- Name: movsto ai_movsto; Type: TRIGGER; Schema: public; Owner: rulemanes
--

CREATE TRIGGER ai_movsto AFTER INSERT ON public.movsto FOR EACH ROW EXECUTE FUNCTION public.f_ai_movsto();


--
-- Name: movstoc ai_movstoc; Type: TRIGGER; Schema: public; Owner: rulemanes
--

CREATE TRIGGER ai_movstoc AFTER INSERT ON public.movstoc FOR EACH ROW EXECUTE FUNCTION public.f_ai_movstoc();


--
-- Name: articvta au_articvta; Type: TRIGGER; Schema: public; Owner: rulemanes
--

CREATE TRIGGER au_articvta AFTER UPDATE ON public.articvta FOR EACH ROW EXECUTE FUNCTION public.f_au_articvta();


--
-- Name: astoctas au_astoctas; Type: TRIGGER; Schema: public; Owner: rulemanes
--

CREATE TRIGGER au_astoctas AFTER UPDATE ON public.astoctas FOR EACH ROW EXECUTE FUNCTION public.f_au_astoctas();


--
-- Name: astoh au_astoh; Type: TRIGGER; Schema: public; Owner: rulemanes
--

CREATE TRIGGER au_astoh AFTER UPDATE ON public.astoh FOR EACH ROW EXECUTE FUNCTION public.f_au_astoh();


--
-- Name: cheques au_cheques; Type: TRIGGER; Schema: public; Owner: rulemanes
--

CREATE TRIGGER au_cheques AFTER UPDATE ON public.cheques FOR EACH ROW EXECUTE FUNCTION public.f_au_cheques();


--
-- Name: clientes au_clientes; Type: TRIGGER; Schema: public; Owner: rulemanes
--

CREATE TRIGGER au_clientes AFTER UPDATE ON public.clientes FOR EACH ROW EXECUTE FUNCTION public.f_au_clientes();


--
-- Name: compra au_compra; Type: TRIGGER; Schema: public; Owner: rulemanes
--

CREATE TRIGGER au_compra AFTER UPDATE ON public.compra FOR EACH ROW EXECUTE FUNCTION public.f_au_compra();


--
-- Name: concilia au_concilia; Type: TRIGGER; Schema: public; Owner: rulemanes
--

CREATE TRIGGER au_concilia AFTER UPDATE ON public.concilia FOR EACH ROW EXECUTE FUNCTION public.f_au_concilia();


--
-- Name: contratos au_contratos; Type: TRIGGER; Schema: public; Owner: rulemanes
--

CREATE TRIGGER au_contratos AFTER UPDATE ON public.contratos FOR EACH ROW EXECUTE FUNCTION public.f_au_contratos();


--
-- Name: emiti au_emiti; Type: TRIGGER; Schema: public; Owner: rulemanes
--

CREATE TRIGGER au_emiti AFTER UPDATE ON public.emiti FOR EACH ROW EXECUTE FUNCTION public.f_au_emiti();


--
-- Name: listpre au_listpre; Type: TRIGGER; Schema: public; Owner: rulemanes
--

CREATE TRIGGER au_listpre AFTER UPDATE ON public.listpre FOR EACH ROW EXECUTE FUNCTION public.f_au_listpre();


--
-- Name: movsto au_movsto; Type: TRIGGER; Schema: public; Owner: rulemanes
--

CREATE TRIGGER au_movsto AFTER UPDATE ON public.movsto FOR EACH ROW EXECUTE FUNCTION public.f_au_movsto();


--
-- Name: movstoc au_movstoc; Type: TRIGGER; Schema: public; Owner: rulemanes
--

CREATE TRIGGER au_movstoc AFTER UPDATE ON public.movstoc FOR EACH ROW EXECUTE FUNCTION public.f_au_movstoc();


--
-- Name: prove au_prove; Type: TRIGGER; Schema: public; Owner: rulemanes
--

CREATE TRIGGER au_prove AFTER UPDATE ON public.prove FOR EACH ROW EXECUTE FUNCTION public.f_au_prove();


--
-- Name: astoctas bd_astoctas; Type: TRIGGER; Schema: public; Owner: rulemanes
--

CREATE TRIGGER bd_astoctas BEFORE DELETE ON public.astoctas FOR EACH ROW EXECUTE FUNCTION public.f_bd_astoctas();


--
-- Name: astoh bd_astoh; Type: TRIGGER; Schema: public; Owner: rulemanes
--

CREATE TRIGGER bd_astoh BEFORE DELETE ON public.astoh FOR EACH ROW EXECUTE FUNCTION public.f_bd_astoh();


--
-- Name: clientes bd_clientes; Type: TRIGGER; Schema: public; Owner: rulemanes
--

CREATE TRIGGER bd_clientes BEFORE DELETE ON public.clientes FOR EACH ROW EXECUTE FUNCTION public.f_bd_clientes();


--
-- Name: compra bd_compra; Type: TRIGGER; Schema: public; Owner: rulemanes
--

CREATE TRIGGER bd_compra BEFORE DELETE ON public.compra FOR EACH ROW EXECUTE FUNCTION public.f_bd_compra();


--
-- Name: contratos bd_contratos; Type: TRIGGER; Schema: public; Owner: rulemanes
--

CREATE TRIGGER bd_contratos BEFORE DELETE ON public.contratos FOR EACH ROW EXECUTE FUNCTION public.f_bd_contratos();


--
-- Name: contrb bd_contrb; Type: TRIGGER; Schema: public; Owner: rulemanes
--

CREATE TRIGGER bd_contrb BEFORE DELETE ON public.contrb FOR EACH ROW EXECUTE FUNCTION public.f_bd_contrb();


--
-- Name: emiti bd_emiti; Type: TRIGGER; Schema: public; Owner: rulemanes
--

CREATE TRIGGER bd_emiti BEFORE DELETE ON public.emiti FOR EACH ROW EXECUTE FUNCTION public.f_bd_emiti();


--
-- Name: empresa bd_empresa; Type: TRIGGER; Schema: public; Owner: rulemanes
--

CREATE TRIGGER bd_empresa BEFORE DELETE ON public.empresa FOR EACH ROW EXECUTE FUNCTION public.f_bd_empresa();


--
-- Name: movsto bd_movsto; Type: TRIGGER; Schema: public; Owner: rulemanes
--

CREATE TRIGGER bd_movsto BEFORE DELETE ON public.movsto FOR EACH ROW EXECUTE FUNCTION public.f_bd_movsto();


--
-- Name: movstoc bd_movstoc; Type: TRIGGER; Schema: public; Owner: rulemanes
--

CREATE TRIGGER bd_movstoc BEFORE DELETE ON public.movstoc FOR EACH ROW EXECUTE FUNCTION public.f_bd_movstoc();


--
-- Name: astoh bu_astoh; Type: TRIGGER; Schema: public; Owner: rulemanes
--

CREATE TRIGGER bu_astoh BEFORE UPDATE ON public.astoh FOR EACH ROW EXECUTE FUNCTION public.f_bu_astoh();


--
-- Name: cheques bu_cheques; Type: TRIGGER; Schema: public; Owner: rulemanes
--

CREATE TRIGGER bu_cheques BEFORE UPDATE ON public.cheques FOR EACH ROW EXECUTE FUNCTION public.f_bu_cheques();


--
-- Name: reclamos tr_actualizar_fecha_reclamo; Type: TRIGGER; Schema: public; Owner: zona
--

CREATE TRIGGER tr_actualizar_fecha_reclamo BEFORE UPDATE ON public.reclamos FOR EACH ROW EXECUTE FUNCTION public.actualizar_fecha_modificacion();


--
-- Name: reclamos tr_generar_numero_reclamo; Type: TRIGGER; Schema: public; Owner: zona
--

CREATE TRIGGER tr_generar_numero_reclamo BEFORE INSERT ON public.reclamos FOR EACH ROW EXECUTE FUNCTION public.generar_numero_reclamo();


--
-- Name: ptovta $1; Type: FK CONSTRAINT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.ptovta
    ADD CONSTRAINT "$1" FOREIGN KEY (ptv_codemp) REFERENCES public.empresa(emp_codigo);


--
-- Name: comprob $1; Type: FK CONSTRAINT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.comprob
    ADD CONSTRAINT "$1" FOREIGN KEY (comp_codemp) REFERENCES public.empresa(emp_codigo);


--
-- Name: articvta $1; Type: FK CONSTRAINT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.articvta
    ADD CONSTRAINT "$1" FOREIGN KEY (art_codemp) REFERENCES public.empresa(emp_codigo);


--
-- Name: astoh $1; Type: FK CONSTRAINT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.astoh
    ADD CONSTRAINT "$1" FOREIGN KEY (ash_codemp) REFERENCES public.empresa(emp_codigo);


--
-- Name: astoctas $1; Type: FK CONSTRAINT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.astoctas
    ADD CONSTRAINT "$1" FOREIGN KEY (astc_codemp) REFERENCES public.empresa(emp_codigo);


--
-- Name: monex $1; Type: FK CONSTRAINT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.monex
    ADD CONSTRAINT "$1" FOREIGN KEY (mne_codemp) REFERENCES public.empresa(emp_codigo);


--
-- Name: firman $1; Type: FK CONSTRAINT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.firman
    ADD CONSTRAINT "$1" FOREIGN KEY (fir_codemp) REFERENCES public.empresa(emp_codigo);


--
-- Name: interes $1; Type: FK CONSTRAINT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.interes
    ADD CONSTRAINT "$1" FOREIGN KEY (int_codemp) REFERENCES public.empresa(emp_codigo);


--
-- Name: cheques $1; Type: FK CONSTRAINT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.cheques
    ADD CONSTRAINT "$1" FOREIGN KEY (chq_codemp) REFERENCES public.empresa(emp_codigo);


--
-- Name: conpag $1; Type: FK CONSTRAINT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.conpag
    ADD CONSTRAINT "$1" FOREIGN KEY (cdp_codemp) REFERENCES public.empresa(emp_codigo);


--
-- Name: ctacble $1; Type: FK CONSTRAINT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.ctacble
    ADD CONSTRAINT "$1" FOREIGN KEY (cbl_codemp) REFERENCES public.empresa(emp_codigo);


--
-- Name: ctes $1; Type: FK CONSTRAINT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.ctes
    ADD CONSTRAINT "$1" FOREIGN KEY (cte_codemp) REFERENCES public.empresa(emp_codigo);


--
-- Name: docum $1; Type: FK CONSTRAINT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.docum
    ADD CONSTRAINT "$1" FOREIGN KEY (doc_codemp) REFERENCES public.empresa(emp_codigo);


--
-- Name: clientes $1; Type: FK CONSTRAINT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.clientes
    ADD CONSTRAINT "$1" FOREIGN KEY (cli_codemp) REFERENCES public.empresa(emp_codigo);


--
-- Name: compra $1; Type: FK CONSTRAINT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.compra
    ADD CONSTRAINT "$1" FOREIGN KEY (cpra_codemp) REFERENCES public.empresa(emp_codigo);


--
-- Name: emiti $1; Type: FK CONSTRAINT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.emiti
    ADD CONSTRAINT "$1" FOREIGN KEY (emi_codemp) REFERENCES public.empresa(emp_codigo);


--
-- Name: prove $1; Type: FK CONSTRAINT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.prove
    ADD CONSTRAINT "$1" FOREIGN KEY (prov_codemp) REFERENCES public.empresa(emp_codigo);


--
-- Name: sucursalc $1; Type: FK CONSTRAINT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.sucursalc
    ADD CONSTRAINT "$1" FOREIGN KEY (sucu_cliente) REFERENCES public.clientes(cli_id);


--
-- Name: sucursalp $1; Type: FK CONSTRAINT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.sucursalp
    ADD CONSTRAINT "$1" FOREIGN KEY (suc_prove) REFERENCES public.prove(prov_id);


--
-- Name: talon $1; Type: FK CONSTRAINT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.talon
    ADD CONSTRAINT "$1" FOREIGN KEY (tal_codemp) REFERENCES public.empresa(emp_codigo);


--
-- Name: telefon $1; Type: FK CONSTRAINT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.telefon
    ADD CONSTRAINT "$1" FOREIGN KEY (tel_codemp) REFERENCES public.empresa(emp_codigo);


--
-- Name: varambi $1; Type: FK CONSTRAINT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.varambi
    ADD CONSTRAINT "$1" FOREIGN KEY (vari_codemp) REFERENCES public.empresa(emp_codigo);


--
-- Name: vendedor $1; Type: FK CONSTRAINT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.vendedor
    ADD CONSTRAINT "$1" FOREIGN KEY (ven_codemp) REFERENCES public.empresa(emp_codigo);


--
-- Name: zona $1; Type: FK CONSTRAINT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.zona
    ADD CONSTRAINT "$1" FOREIGN KEY (zon_codemp) REFERENCES public.empresa(emp_codigo);


--
-- Name: subd $1; Type: FK CONSTRAINT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.subd
    ADD CONSTRAINT "$1" FOREIGN KEY (subd_codemp) REFERENCES public.empresa(emp_codigo);


--
-- Name: articu $1; Type: FK CONSTRAINT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.articu
    ADD CONSTRAINT "$1" FOREIGN KEY (artc_codemp) REFERENCES public.empresa(emp_codigo);


--
-- Name: receta $1; Type: FK CONSTRAINT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.receta
    ADD CONSTRAINT "$1" FOREIGN KEY (re_codemp) REFERENCES public.empresa(emp_codigo);


--
-- Name: movsto $1; Type: FK CONSTRAINT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.movsto
    ADD CONSTRAINT "$1" FOREIGN KEY (movs_artic) REFERENCES public.articvta(art_id);


--
-- Name: movstoc $1; Type: FK CONSTRAINT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.movstoc
    ADD CONSTRAINT "$1" FOREIGN KEY (movc_artic) REFERENCES public.articu(artc_id);


--
-- Name: stock $1; Type: FK CONSTRAINT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.stock
    ADD CONSTRAINT "$1" FOREIGN KEY (stc_codemp) REFERENCES public.empresa(emp_codigo);


--
-- Name: listpre $1; Type: FK CONSTRAINT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.listpre
    ADD CONSTRAINT "$1" FOREIGN KEY (ltp_codemp) REFERENCES public.empresa(emp_codigo);


--
-- Name: relrem $1; Type: FK CONSTRAINT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.relrem
    ADD CONSTRAINT "$1" FOREIGN KEY (rr_idastohp) REFERENCES public.astoh(ash_id);


--
-- Name: relnotp $1; Type: FK CONSTRAINT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.relnotp
    ADD CONSTRAINT "$1" FOREIGN KEY (rn_idemitip) REFERENCES public.emiti(emi_id);


--
-- Name: concilia $1; Type: FK CONSTRAINT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.concilia
    ADD CONSTRAINT "$1" FOREIGN KEY (clia_cte) REFERENCES public.ctes(cte_id);


--
-- Name: conxclia $1; Type: FK CONSTRAINT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.conxclia
    ADD CONSTRAINT "$1" FOREIGN KEY (cxlia_idclia) REFERENCES public.concilia(clia_id);


--
-- Name: dispenser $1; Type: FK CONSTRAINT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.dispenser
    ADD CONSTRAINT "$1" FOREIGN KEY (dis_codemp) REFERENCES public.empresa(emp_codigo);


--
-- Name: contratos $1; Type: FK CONSTRAINT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.contratos
    ADD CONSTRAINT "$1" FOREIGN KEY (ctr_idcli) REFERENCES public.clientes(cli_id);


--
-- Name: contrb $1; Type: FK CONSTRAINT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.contrb
    ADD CONSTRAINT "$1" FOREIGN KEY (ctrb_idcli) REFERENCES public.clientes(cli_id);


--
-- Name: rtoxcontr $1; Type: FK CONSTRAINT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.rtoxcontr
    ADD CONSTRAINT "$1" FOREIGN KEY (rtc_idcontr) REFERENCES public.contratos(ctr_id);


--
-- Name: relremc $1; Type: FK CONSTRAINT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.relremc
    ADD CONSTRAINT "$1" FOREIGN KEY (rr_idastohh) REFERENCES public.astoh(ash_id);


--
-- Name: comprob $2; Type: FK CONSTRAINT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.comprob
    ADD CONSTRAINT "$2" FOREIGN KEY (comp_idptov) REFERENCES public.ptovta(ptv_id);


--
-- Name: astoh $2; Type: FK CONSTRAINT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.astoh
    ADD CONSTRAINT "$2" FOREIGN KEY (ash_idptov) REFERENCES public.ptovta(ptv_id);


--
-- Name: astoctas $2; Type: FK CONSTRAINT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.astoctas
    ADD CONSTRAINT "$2" FOREIGN KEY (astc_idptov) REFERENCES public.ptovta(ptv_id);


--
-- Name: monex $2; Type: FK CONSTRAINT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.monex
    ADD CONSTRAINT "$2" FOREIGN KEY (mne_idastoc) REFERENCES public.astoctas(astc_id);


--
-- Name: cheques $2; Type: FK CONSTRAINT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.cheques
    ADD CONSTRAINT "$2" FOREIGN KEY (chq_idptov) REFERENCES public.ptovta(ptv_id);


--
-- Name: ctacble $2; Type: FK CONSTRAINT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.ctacble
    ADD CONSTRAINT "$2" FOREIGN KEY (cbl_moneda) REFERENCES public.moneda(mon_codigo);


--
-- Name: ctes $2; Type: FK CONSTRAINT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.ctes
    ADD CONSTRAINT "$2" FOREIGN KEY (cte_tipcta) REFERENCES public.tipcta(tct_codigo);


--
-- Name: docum $2; Type: FK CONSTRAINT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.docum
    ADD CONSTRAINT "$2" FOREIGN KEY (doc_idptov) REFERENCES public.ptovta(ptv_id);


--
-- Name: compra $2; Type: FK CONSTRAINT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.compra
    ADD CONSTRAINT "$2" FOREIGN KEY (cpra_idptov) REFERENCES public.ptovta(ptv_id);


--
-- Name: emiti $2; Type: FK CONSTRAINT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.emiti
    ADD CONSTRAINT "$2" FOREIGN KEY (emi_idptov) REFERENCES public.ptovta(ptv_id);


--
-- Name: talon $2; Type: FK CONSTRAINT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.talon
    ADD CONSTRAINT "$2" FOREIGN KEY (tal_idptov) REFERENCES public.ptovta(ptv_id);


--
-- Name: telefon $2; Type: FK CONSTRAINT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.telefon
    ADD CONSTRAINT "$2" FOREIGN KEY (tel_tipent) REFERENCES public.tipentid(tet_codigo);


--
-- Name: cblexasi $2; Type: FK CONSTRAINT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.cblexasi
    ADD CONSTRAINT "$2" FOREIGN KEY (cxa_cble) REFERENCES public.ctacble(cbl_id);


--
-- Name: subd $2; Type: FK CONSTRAINT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.subd
    ADD CONSTRAINT "$2" FOREIGN KEY (subd_idptov) REFERENCES public.ptovta(ptv_id);


--
-- Name: subxcomp $2; Type: FK CONSTRAINT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.subxcomp
    ADD CONSTRAINT "$2" FOREIGN KEY (subxc_comprob) REFERENCES public.comprob(comp_id);


--
-- Name: receta $2; Type: FK CONSTRAINT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.receta
    ADD CONSTRAINT "$2" FOREIGN KEY (re_articv) REFERENCES public.articvta(art_id);


--
-- Name: movsto $2; Type: FK CONSTRAINT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.movsto
    ADD CONSTRAINT "$2" FOREIGN KEY (movs_idemiti) REFERENCES public.emiti(emi_id);


--
-- Name: movstoc $2; Type: FK CONSTRAINT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.movstoc
    ADD CONSTRAINT "$2" FOREIGN KEY (movc_idcompra) REFERENCES public.compra(cpra_id);


--
-- Name: stock $2; Type: FK CONSTRAINT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.stock
    ADD CONSTRAINT "$2" FOREIGN KEY (stc_idart) REFERENCES public.articvta(art_id);


--
-- Name: listpre $2; Type: FK CONSTRAINT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.listpre
    ADD CONSTRAINT "$2" FOREIGN KEY (ltp_idart) REFERENCES public.articvta(art_id);


--
-- Name: relrem $2; Type: FK CONSTRAINT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.relrem
    ADD CONSTRAINT "$2" FOREIGN KEY (rr_idastohh) REFERENCES public.astoh(ash_id);


--
-- Name: relnotp $2; Type: FK CONSTRAINT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.relnotp
    ADD CONSTRAINT "$2" FOREIGN KEY (rn_idemitih) REFERENCES public.emiti(emi_id);


--
-- Name: conxclia $2; Type: FK CONSTRAINT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.conxclia
    ADD CONSTRAINT "$2" FOREIGN KEY (cxlia_idastc) REFERENCES public.astoctas(astc_id);


--
-- Name: dispenser $2; Type: FK CONSTRAINT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.dispenser
    ADD CONSTRAINT "$2" FOREIGN KEY (dis_idptov) REFERENCES public.ptovta(ptv_id);


--
-- Name: contratos $2; Type: FK CONSTRAINT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.contratos
    ADD CONSTRAINT "$2" FOREIGN KEY (ctr_codemp) REFERENCES public.empresa(emp_codigo);


--
-- Name: contrb $2; Type: FK CONSTRAINT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.contrb
    ADD CONSTRAINT "$2" FOREIGN KEY (ctrb_idcontr) REFERENCES public.contratos(ctr_id);


--
-- Name: rtoxcontr $2; Type: FK CONSTRAINT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.rtoxcontr
    ADD CONSTRAINT "$2" FOREIGN KEY (rtc_idemiti) REFERENCES public.emiti(emi_id);


--
-- Name: relremc $2; Type: FK CONSTRAINT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.relremc
    ADD CONSTRAINT "$2" FOREIGN KEY (rr_idastohp) REFERENCES public.astoh(ash_id);


--
-- Name: astoh $3; Type: FK CONSTRAINT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.astoh
    ADD CONSTRAINT "$3" FOREIGN KEY (ash_comprob) REFERENCES public.comprob(comp_id);


--
-- Name: astoctas $3; Type: FK CONSTRAINT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.astoctas
    ADD CONSTRAINT "$3" FOREIGN KEY (astc_comprob) REFERENCES public.comprob(comp_id);


--
-- Name: monex $3; Type: FK CONSTRAINT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.monex
    ADD CONSTRAINT "$3" FOREIGN KEY (mne_moneda) REFERENCES public.moneda(mon_codigo);


--
-- Name: cheques $3; Type: FK CONSTRAINT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.cheques
    ADD CONSTRAINT "$3" FOREIGN KEY (chq_idastoc) REFERENCES public.astoctas(astc_id);


--
-- Name: docum $3; Type: FK CONSTRAINT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.docum
    ADD CONSTRAINT "$3" FOREIGN KEY (doc_idastoc) REFERENCES public.astoctas(astc_id);


--
-- Name: compra $3; Type: FK CONSTRAINT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.compra
    ADD CONSTRAINT "$3" FOREIGN KEY (cpra_comprob) REFERENCES public.comprob(comp_id);


--
-- Name: emiti $3; Type: FK CONSTRAINT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.emiti
    ADD CONSTRAINT "$3" FOREIGN KEY (emi_comprob) REFERENCES public.comprob(comp_id);


--
-- Name: telefon $3; Type: FK CONSTRAINT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.telefon
    ADD CONSTRAINT "$3" FOREIGN KEY (tel_tiptel) REFERENCES public.tiptel(ttl_codigo);


--
-- Name: receta $3; Type: FK CONSTRAINT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.receta
    ADD CONSTRAINT "$3" FOREIGN KEY (re_articc) REFERENCES public.articu(artc_id);


--
-- Name: movstoc $3; Type: FK CONSTRAINT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.movstoc
    ADD CONSTRAINT "$3" FOREIGN KEY (movc_idastoh) REFERENCES public.astoh(ash_id);


--
-- Name: stock $3; Type: FK CONSTRAINT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.stock
    ADD CONSTRAINT "$3" FOREIGN KEY (stc_idartc) REFERENCES public.articu(artc_id);


--
-- Name: contratos $3; Type: FK CONSTRAINT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.contratos
    ADD CONSTRAINT "$3" FOREIGN KEY (ctr_idptov) REFERENCES public.ptovta(ptv_id);


--
-- Name: astoctas $4; Type: FK CONSTRAINT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.astoctas
    ADD CONSTRAINT "$4" FOREIGN KEY (astc_idastoh) REFERENCES public.astoh(ash_id);


--
-- Name: cheques $4; Type: FK CONSTRAINT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.cheques
    ADD CONSTRAINT "$4" FOREIGN KEY (chq_moneda) REFERENCES public.moneda(mon_codigo);


--
-- Name: compra $4; Type: FK CONSTRAINT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.compra
    ADD CONSTRAINT "$4" FOREIGN KEY (cpra_idastoh) REFERENCES public.astoh(ash_id);


--
-- Name: stock $4; Type: FK CONSTRAINT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.stock
    ADD CONSTRAINT "$4" FOREIGN KEY (stc_idemiti) REFERENCES public.emiti(emi_id);


--
-- Name: astoctas $5; Type: FK CONSTRAINT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.astoctas
    ADD CONSTRAINT "$5" FOREIGN KEY (astc_moneda) REFERENCES public.moneda(mon_codigo);


--
-- Name: stock $5; Type: FK CONSTRAINT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.stock
    ADD CONSTRAINT "$5" FOREIGN KEY (stc_idcompra) REFERENCES public.compra(cpra_id);


--
-- Name: stock $6; Type: FK CONSTRAINT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.stock
    ADD CONSTRAINT "$6" FOREIGN KEY (stc_idastoh) REFERENCES public.astoh(ash_id);


--
-- Name: stock $7; Type: FK CONSTRAINT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.stock
    ADD CONSTRAINT "$7" FOREIGN KEY (stc_idmovsto) REFERENCES public.movsto(movs_id);


--
-- Name: stock $8; Type: FK CONSTRAINT; Schema: public; Owner: rulemanes
--

ALTER TABLE ONLY public.stock
    ADD CONSTRAINT "$8" FOREIGN KEY (stc_idmovstoc) REFERENCES public.movstoc(movc_id);


--
-- Name: art_aplicaciones art_aplicaciones_app_artid_fkey; Type: FK CONSTRAINT; Schema: public; Owner: zona
--

ALTER TABLE ONLY public.art_aplicaciones
    ADD CONSTRAINT art_aplicaciones_app_artid_fkey FOREIGN KEY (app_artid) REFERENCES public.articvta(art_id) ON DELETE CASCADE;


--
-- Name: art_aplicaciones art_aplicaciones_app_marca_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: zona
--

ALTER TABLE ONLY public.art_aplicaciones
    ADD CONSTRAINT art_aplicaciones_app_marca_id_fkey FOREIGN KEY (app_marca_id) REFERENCES public.marcas_auto(maraut_id) ON DELETE SET NULL;


--
-- Name: art_aplicaciones art_aplicaciones_app_modelo_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: zona
--

ALTER TABLE ONLY public.art_aplicaciones
    ADD CONSTRAINT art_aplicaciones_app_modelo_id_fkey FOREIGN KEY (app_modelo_id) REFERENCES public.modelos_auto(modaut_id) ON DELETE SET NULL;


--
-- Name: art_aplicaciones art_aplicaciones_app_posicion_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: zona
--

ALTER TABLE ONLY public.art_aplicaciones
    ADD CONSTRAINT art_aplicaciones_app_posicion_id_fkey FOREIGN KEY (app_posicion_id) REFERENCES public.posiciones(pos_id) ON DELETE SET NULL;


--
-- Name: art_atrib_val art_atrib_val_aav_artid_fkey; Type: FK CONSTRAINT; Schema: public; Owner: zona
--

ALTER TABLE ONLY public.art_atrib_val
    ADD CONSTRAINT art_atrib_val_aav_artid_fkey FOREIGN KEY (aav_artid) REFERENCES public.articvta(art_id) ON DELETE CASCADE;


--
-- Name: art_atrib_val art_atrib_val_aav_defid_fkey; Type: FK CONSTRAINT; Schema: public; Owner: zona
--

ALTER TABLE ONLY public.art_atrib_val
    ADD CONSTRAINT art_atrib_val_aav_defid_fkey FOREIGN KEY (aav_defid) REFERENCES public.art_atrib_def(aad_id) ON DELETE CASCADE;


--
-- Name: art_atrib_valm art_atrib_valm_aavm_artid_fkey; Type: FK CONSTRAINT; Schema: public; Owner: zona
--

ALTER TABLE ONLY public.art_atrib_valm
    ADD CONSTRAINT art_atrib_valm_aavm_artid_fkey FOREIGN KEY (aavm_artid) REFERENCES public.articvta(art_id) ON DELETE CASCADE;


--
-- Name: art_atrib_valm art_atrib_valm_aavm_defid_fkey; Type: FK CONSTRAINT; Schema: public; Owner: zona
--

ALTER TABLE ONLY public.art_atrib_valm
    ADD CONSTRAINT art_atrib_valm_aavm_defid_fkey FOREIGN KEY (aavm_defid) REFERENCES public.art_atrib_def(aad_id) ON DELETE CASCADE;


--
-- Name: descuentos_rubro descuentos_rubro_dr_cliente_fkey; Type: FK CONSTRAINT; Schema: public; Owner: zona
--

ALTER TABLE ONLY public.descuentos_rubro
    ADD CONSTRAINT descuentos_rubro_dr_cliente_fkey FOREIGN KEY (dr_cliente) REFERENCES public.clientes(cli_id);


--
-- Name: descuentos_rubro descuentos_rubro_dr_rubro_fkey; Type: FK CONSTRAINT; Schema: public; Owner: zona
--

ALTER TABLE ONLY public.descuentos_rubro
    ADD CONSTRAINT descuentos_rubro_dr_rubro_fkey FOREIGN KEY (dr_rubro) REFERENCES public.rubros(rub_id);


--
-- Name: cta_nivel fk_cn_emp; Type: FK CONSTRAINT; Schema: public; Owner: zona
--

ALTER TABLE ONLY public.cta_nivel
    ADD CONSTRAINT fk_cn_emp FOREIGN KEY (cn_codemp) REFERENCES public.empresa(emp_codigo);


--
-- Name: cta_nivel fk_cn_padre; Type: FK CONSTRAINT; Schema: public; Owner: zona
--

ALTER TABLE ONLY public.cta_nivel
    ADD CONSTRAINT fk_cn_padre FOREIGN KEY (cn_padre) REFERENCES public.cta_nivel(cn_id);


--
-- Name: reclamos fk_reclamo_cliente; Type: FK CONSTRAINT; Schema: public; Owner: zona
--

ALTER TABLE ONLY public.reclamos
    ADD CONSTRAINT fk_reclamo_cliente FOREIGN KEY (rec_cliente_id) REFERENCES public.clientes(cli_id);


--
-- Name: reclamos fk_reclamo_pedido; Type: FK CONSTRAINT; Schema: public; Owner: zona
--

ALTER TABLE ONLY public.reclamos
    ADD CONSTRAINT fk_reclamo_pedido FOREIGN KEY (rec_pedido_id) REFERENCES public.emiti(emi_id);


--
-- Name: modelos_auto modelos_auto_modaut_marca_fkey; Type: FK CONSTRAINT; Schema: public; Owner: zona
--

ALTER TABLE ONLY public.modelos_auto
    ADD CONSTRAINT modelos_auto_modaut_marca_fkey FOREIGN KEY (modaut_marca) REFERENCES public.marcas_auto(maraut_id) ON DELETE CASCADE;


--
-- Name: operadores_modulos operadores_modulos_om_modulo_fkey; Type: FK CONSTRAINT; Schema: public; Owner: zona
--

ALTER TABLE ONLY public.operadores_modulos
    ADD CONSTRAINT operadores_modulos_om_modulo_fkey FOREIGN KEY (om_modulo) REFERENCES public.modulos_sistema(mod_id);


--
-- Name: operadores_modulos operadores_modulos_om_operador_fkey; Type: FK CONSTRAINT; Schema: public; Owner: zona
--

ALTER TABLE ONLY public.operadores_modulos
    ADD CONSTRAINT operadores_modulos_om_operador_fkey FOREIGN KEY (om_operador) REFERENCES public.operadores(operad_id);


--
-- Name: reclamos_adjuntos reclamos_adjuntos_adj_reclamo_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: zona
--

ALTER TABLE ONLY public.reclamos_adjuntos
    ADD CONSTRAINT reclamos_adjuntos_adj_reclamo_id_fkey FOREIGN KEY (adj_reclamo_id) REFERENCES public.reclamos(rec_id) ON DELETE CASCADE;


--
-- Name: reclamos reclamos_rec_estado_fkey; Type: FK CONSTRAINT; Schema: public; Owner: zona
--

ALTER TABLE ONLY public.reclamos
    ADD CONSTRAINT reclamos_rec_estado_fkey FOREIGN KEY (rec_estado) REFERENCES public.estados_reclamos(est_id);


--
-- Name: reclamos reclamos_rec_tipo_fkey; Type: FK CONSTRAINT; Schema: public; Owner: zona
--

ALTER TABLE ONLY public.reclamos
    ADD CONSTRAINT reclamos_rec_tipo_fkey FOREIGN KEY (rec_tipo) REFERENCES public.tipos_reclamos(tpr_id);


--
-- Name: reclamos_seguimiento reclamos_seguimiento_seg_estado_anterior_fkey; Type: FK CONSTRAINT; Schema: public; Owner: zona
--

ALTER TABLE ONLY public.reclamos_seguimiento
    ADD CONSTRAINT reclamos_seguimiento_seg_estado_anterior_fkey FOREIGN KEY (seg_estado_anterior) REFERENCES public.estados_reclamos(est_id);


--
-- Name: reclamos_seguimiento reclamos_seguimiento_seg_estado_nuevo_fkey; Type: FK CONSTRAINT; Schema: public; Owner: zona
--

ALTER TABLE ONLY public.reclamos_seguimiento
    ADD CONSTRAINT reclamos_seguimiento_seg_estado_nuevo_fkey FOREIGN KEY (seg_estado_nuevo) REFERENCES public.estados_reclamos(est_id);


--
-- Name: reclamos_seguimiento reclamos_seguimiento_seg_reclamo_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: zona
--

ALTER TABLE ONLY public.reclamos_seguimiento
    ADD CONSTRAINT reclamos_seguimiento_seg_reclamo_id_fkey FOREIGN KEY (seg_reclamo_id) REFERENCES public.reclamos(rec_id) ON DELETE CASCADE;


--
-- Name: TABLE agrupa; Type: ACL; Schema: public; Owner: rulemanes
--

GRANT ALL ON TABLE public.agrupa TO nobody;


--
-- Name: SEQUENCE agrupa_ag_id_seq; Type: ACL; Schema: public; Owner: rulemanes
--

GRANT UPDATE ON SEQUENCE public.agrupa_ag_id_seq TO nobody;


--
-- Name: TABLE articu; Type: ACL; Schema: public; Owner: rulemanes
--

GRANT ALL ON TABLE public.articu TO nobody;


--
-- Name: SEQUENCE articu_artc_id_seq; Type: ACL; Schema: public; Owner: rulemanes
--

GRANT UPDATE ON SEQUENCE public.articu_artc_id_seq TO nobody;


--
-- Name: TABLE articvta; Type: ACL; Schema: public; Owner: rulemanes
--

GRANT ALL ON TABLE public.articvta TO nobody;


--
-- Name: SEQUENCE articvta_art_id_seq; Type: ACL; Schema: public; Owner: rulemanes
--

GRANT UPDATE ON SEQUENCE public.articvta_art_id_seq TO nobody;


--
-- Name: TABLE astoctas; Type: ACL; Schema: public; Owner: rulemanes
--

GRANT ALL ON TABLE public.astoctas TO nobody;


--
-- Name: SEQUENCE astoctas_astc_id_seq; Type: ACL; Schema: public; Owner: rulemanes
--

GRANT UPDATE ON SEQUENCE public.astoctas_astc_id_seq TO nobody;


--
-- Name: TABLE astoh; Type: ACL; Schema: public; Owner: rulemanes
--

GRANT ALL ON TABLE public.astoh TO nobody;


--
-- Name: SEQUENCE astoh_ash_id_seq; Type: ACL; Schema: public; Owner: rulemanes
--

GRANT UPDATE ON SEQUENCE public.astoh_ash_id_seq TO nobody;


--
-- Name: TABLE auxfe; Type: ACL; Schema: public; Owner: zona
--

GRANT ALL ON TABLE public.auxfe TO nobody;


--
-- Name: SEQUENCE auxfe_fe_id_seq; Type: ACL; Schema: public; Owner: zona
--

GRANT UPDATE ON SEQUENCE public.auxfe_fe_id_seq TO nobody;


--
-- Name: TABLE bancos; Type: ACL; Schema: public; Owner: rulemanes
--

GRANT ALL ON TABLE public.bancos TO nobody;


--
-- Name: TABLE cblexasi; Type: ACL; Schema: public; Owner: rulemanes
--

GRANT ALL ON TABLE public.cblexasi TO nobody;


--
-- Name: TABLE cbteasoc; Type: ACL; Schema: public; Owner: rulemanes
--

GRANT ALL ON TABLE public.cbteasoc TO nobody;


--
-- Name: SEQUENCE cbteasoc_cb_id_seq; Type: ACL; Schema: public; Owner: rulemanes
--

GRANT UPDATE ON SEQUENCE public.cbteasoc_cb_id_seq TO nobody;


--
-- Name: TABLE chat; Type: ACL; Schema: public; Owner: zona
--

GRANT ALL ON TABLE public.chat TO nobody;


--
-- Name: SEQUENCE chat_c_id_seq; Type: ACL; Schema: public; Owner: zona
--

GRANT UPDATE ON SEQUENCE public.chat_c_id_seq TO nobody;


--
-- Name: TABLE cheques; Type: ACL; Schema: public; Owner: rulemanes
--

GRANT ALL ON TABLE public.cheques TO nobody;


--
-- Name: SEQUENCE cheques_chq_id_seq; Type: ACL; Schema: public; Owner: rulemanes
--

GRANT UPDATE ON SEQUENCE public.cheques_chq_id_seq TO nobody;


--
-- Name: TABLE clientes; Type: ACL; Schema: public; Owner: rulemanes
--

GRANT ALL ON TABLE public.clientes TO nobody;


--
-- Name: SEQUENCE clientes_cli_id_seq; Type: ACL; Schema: public; Owner: rulemanes
--

GRANT UPDATE ON SEQUENCE public.clientes_cli_id_seq TO nobody;


--
-- Name: TABLE compra; Type: ACL; Schema: public; Owner: rulemanes
--

GRANT ALL ON TABLE public.compra TO nobody;


--
-- Name: SEQUENCE compra_cpra_id_seq; Type: ACL; Schema: public; Owner: rulemanes
--

GRANT UPDATE ON SEQUENCE public.compra_cpra_id_seq TO nobody;


--
-- Name: TABLE comprob; Type: ACL; Schema: public; Owner: rulemanes
--

GRANT ALL ON TABLE public.comprob TO nobody;


--
-- Name: SEQUENCE comprob_comp_id_seq; Type: ACL; Schema: public; Owner: rulemanes
--

GRANT UPDATE ON SEQUENCE public.comprob_comp_id_seq TO nobody;


--
-- Name: TABLE concilia; Type: ACL; Schema: public; Owner: rulemanes
--

GRANT ALL ON TABLE public.concilia TO nobody;


--
-- Name: SEQUENCE concilia_clia_id_seq; Type: ACL; Schema: public; Owner: rulemanes
--

GRANT UPDATE ON SEQUENCE public.concilia_clia_id_seq TO nobody;


--
-- Name: TABLE coniva; Type: ACL; Schema: public; Owner: rulemanes
--

GRANT ALL ON TABLE public.coniva TO nobody;


--
-- Name: TABLE conpag; Type: ACL; Schema: public; Owner: rulemanes
--

GRANT ALL ON TABLE public.conpag TO nobody;


--
-- Name: SEQUENCE conpag_cdp_codigo_seq; Type: ACL; Schema: public; Owner: rulemanes
--

GRANT UPDATE ON SEQUENCE public.conpag_cdp_codigo_seq TO nobody;


--
-- Name: TABLE contratos; Type: ACL; Schema: public; Owner: rulemanes
--

GRANT ALL ON TABLE public.contratos TO nobody;


--
-- Name: SEQUENCE contratos_ctr_id_seq; Type: ACL; Schema: public; Owner: rulemanes
--

GRANT UPDATE ON SEQUENCE public.contratos_ctr_id_seq TO nobody;


--
-- Name: TABLE contrb; Type: ACL; Schema: public; Owner: rulemanes
--

GRANT ALL ON TABLE public.contrb TO nobody;


--
-- Name: SEQUENCE contrb_ctrb_id_seq; Type: ACL; Schema: public; Owner: rulemanes
--

GRANT UPDATE ON SEQUENCE public.contrb_ctrb_id_seq TO nobody;


--
-- Name: TABLE conxclia; Type: ACL; Schema: public; Owner: rulemanes
--

GRANT ALL ON TABLE public.conxclia TO nobody;


--
-- Name: SEQUENCE conxcxlia_cxlia_id_seq; Type: ACL; Schema: public; Owner: rulemanes
--

GRANT UPDATE ON SEQUENCE public.conxcxlia_cxlia_id_seq TO nobody;


--
-- Name: TABLE ctacble; Type: ACL; Schema: public; Owner: rulemanes
--

GRANT ALL ON TABLE public.ctacble TO nobody;


--
-- Name: SEQUENCE ctacble_cbl_id_seq; Type: ACL; Schema: public; Owner: rulemanes
--

GRANT UPDATE ON SEQUENCE public.ctacble_cbl_id_seq TO nobody;


--
-- Name: TABLE ctes; Type: ACL; Schema: public; Owner: rulemanes
--

GRANT ALL ON TABLE public.ctes TO nobody;


--
-- Name: SEQUENCE ctes_cte_id_seq; Type: ACL; Schema: public; Owner: rulemanes
--

GRANT UPDATE ON SEQUENCE public.ctes_cte_id_seq TO nobody;


--
-- Name: TABLE debitos; Type: ACL; Schema: public; Owner: rulemanes
--

GRANT ALL ON TABLE public.debitos TO nobody;


--
-- Name: SEQUENCE debitos_db_id_seq; Type: ACL; Schema: public; Owner: rulemanes
--

GRANT UPDATE ON SEQUENCE public.debitos_db_id_seq TO nobody;


--
-- Name: TABLE deudas; Type: ACL; Schema: public; Owner: rulemanes
--

GRANT ALL ON TABLE public.deudas TO nobody;


--
-- Name: SEQUENCE deudas_dd_id_seq; Type: ACL; Schema: public; Owner: rulemanes
--

GRANT UPDATE ON SEQUENCE public.deudas_dd_id_seq TO nobody;


--
-- Name: TABLE dispenser; Type: ACL; Schema: public; Owner: rulemanes
--

GRANT ALL ON TABLE public.dispenser TO nobody;


--
-- Name: SEQUENCE dispenser_dis_id_seq; Type: ACL; Schema: public; Owner: rulemanes
--

GRANT UPDATE ON SEQUENCE public.dispenser_dis_id_seq TO nobody;


--
-- Name: TABLE docum; Type: ACL; Schema: public; Owner: rulemanes
--

GRANT ALL ON TABLE public.docum TO nobody;


--
-- Name: SEQUENCE docum_doc_id_seq; Type: ACL; Schema: public; Owner: rulemanes
--

GRANT UPDATE ON SEQUENCE public.docum_doc_id_seq TO nobody;


--
-- Name: TABLE emiti; Type: ACL; Schema: public; Owner: rulemanes
--

GRANT ALL ON TABLE public.emiti TO nobody;


--
-- Name: SEQUENCE emiti_emi_id_seq; Type: ACL; Schema: public; Owner: rulemanes
--

GRANT UPDATE ON SEQUENCE public.emiti_emi_id_seq TO nobody;


--
-- Name: TABLE empresa; Type: ACL; Schema: public; Owner: rulemanes
--

GRANT ALL ON TABLE public.empresa TO nobody;


--
-- Name: TABLE estcivil; Type: ACL; Schema: public; Owner: rulemanes
--

GRANT ALL ON TABLE public.estcivil TO nobody;


--
-- Name: TABLE fact_vencidas; Type: ACL; Schema: public; Owner: rulemanes
--

GRANT ALL ON TABLE public.fact_vencidas TO nobody;


--
-- Name: SEQUENCE fact_vencidas_fv_id_seq; Type: ACL; Schema: public; Owner: rulemanes
--

GRANT UPDATE ON SEQUENCE public.fact_vencidas_fv_id_seq TO nobody;


--
-- Name: TABLE firman; Type: ACL; Schema: public; Owner: rulemanes
--

GRANT ALL ON TABLE public.firman TO nobody;


--
-- Name: SEQUENCE firman_fir_id_seq; Type: ACL; Schema: public; Owner: rulemanes
--

GRANT UPDATE ON SEQUENCE public.firman_fir_id_seq TO nobody;


--
-- Name: TABLE grupos; Type: ACL; Schema: public; Owner: rulemanes
--

GRANT ALL ON TABLE public.grupos TO nobody;


--
-- Name: SEQUENCE grupos_gpo_id_seq; Type: ACL; Schema: public; Owner: rulemanes
--

GRANT UPDATE ON SEQUENCE public.grupos_gpo_id_seq TO nobody;


--
-- Name: TABLE hojaruta; Type: ACL; Schema: public; Owner: rulemanes
--

GRANT ALL ON TABLE public.hojaruta TO nobody;


--
-- Name: SEQUENCE hojaruta_hr_id_seq; Type: ACL; Schema: public; Owner: rulemanes
--

GRANT UPDATE ON SEQUENCE public.hojaruta_hr_id_seq TO nobody;


--
-- Name: TABLE iibbfile; Type: ACL; Schema: public; Owner: rulemanes
--

GRANT ALL ON TABLE public.iibbfile TO nobody;


--
-- Name: SEQUENCE iibbfile_if_id_seq; Type: ACL; Schema: public; Owner: rulemanes
--

GRANT UPDATE ON SEQUENCE public.iibbfile_if_id_seq TO nobody;


--
-- Name: TABLE indices; Type: ACL; Schema: public; Owner: rulemanes
--

GRANT ALL ON TABLE public.indices TO nobody;


--
-- Name: TABLE interes; Type: ACL; Schema: public; Owner: rulemanes
--

GRANT ALL ON TABLE public.interes TO nobody;


--
-- Name: TABLE listas; Type: ACL; Schema: public; Owner: rulemanes
--

GRANT ALL ON TABLE public.listas TO nobody;


--
-- Name: SEQUENCE listas_li_codigo_seq; Type: ACL; Schema: public; Owner: rulemanes
--

GRANT UPDATE ON SEQUENCE public.listas_li_codigo_seq TO nobody;


--
-- Name: TABLE listpre; Type: ACL; Schema: public; Owner: rulemanes
--

GRANT ALL ON TABLE public.listpre TO nobody;


--
-- Name: SEQUENCE listpre_ltp_id_seq; Type: ACL; Schema: public; Owner: rulemanes
--

GRANT UPDATE ON SEQUENCE public.listpre_ltp_id_seq TO nobody;


--
-- Name: TABLE logs; Type: ACL; Schema: public; Owner: rulemanes
--

GRANT ALL ON TABLE public.logs TO nobody;


--
-- Name: SEQUENCE logs_lg_id_seq; Type: ACL; Schema: public; Owner: rulemanes
--

GRANT UPDATE ON SEQUENCE public.logs_lg_id_seq TO nobody;


--
-- Name: TABLE margenxarticu; Type: ACL; Schema: public; Owner: estlm
--

GRANT ALL ON TABLE public.margenxarticu TO nobody;


--
-- Name: SEQUENCE margenxarticu_mxa_id_seq; Type: ACL; Schema: public; Owner: estlm
--

GRANT UPDATE ON SEQUENCE public.margenxarticu_mxa_id_seq TO nobody;


--
-- Name: TABLE menues; Type: ACL; Schema: public; Owner: rulemanes
--

GRANT ALL ON TABLE public.menues TO nobody;


--
-- Name: SEQUENCE menues_men_id_seq; Type: ACL; Schema: public; Owner: rulemanes
--

GRANT UPDATE ON SEQUENCE public.menues_men_id_seq TO nobody;


--
-- Name: TABLE modlogs; Type: ACL; Schema: public; Owner: zona
--

GRANT ALL ON TABLE public.modlogs TO nobody;


--
-- Name: SEQUENCE modlogs_log_id_seq; Type: ACL; Schema: public; Owner: zona
--

GRANT ALL ON SEQUENCE public.modlogs_log_id_seq TO sanjor;
GRANT UPDATE ON SEQUENCE public.modlogs_log_id_seq TO nobody;


--
-- Name: TABLE moneda; Type: ACL; Schema: public; Owner: rulemanes
--

GRANT ALL ON TABLE public.moneda TO nobody;


--
-- Name: TABLE monex; Type: ACL; Schema: public; Owner: rulemanes
--

GRANT ALL ON TABLE public.monex TO nobody;


--
-- Name: SEQUENCE monex_mne_id_seq; Type: ACL; Schema: public; Owner: rulemanes
--

GRANT UPDATE ON SEQUENCE public.monex_mne_id_seq TO nobody;


--
-- Name: TABLE movsleyen; Type: ACL; Schema: public; Owner: zona
--

GRANT ALL ON TABLE public.movsleyen TO nobody;


--
-- Name: SEQUENCE movsleyen_mvl_id_seq; Type: ACL; Schema: public; Owner: zona
--

GRANT UPDATE ON SEQUENCE public.movsleyen_mvl_id_seq TO nobody;


--
-- Name: TABLE movsto; Type: ACL; Schema: public; Owner: rulemanes
--

GRANT ALL ON TABLE public.movsto TO nobody;


--
-- Name: SEQUENCE movsto_movs_id_seq; Type: ACL; Schema: public; Owner: rulemanes
--

GRANT UPDATE ON SEQUENCE public.movsto_movs_id_seq TO nobody;


--
-- Name: TABLE movstoc; Type: ACL; Schema: public; Owner: rulemanes
--

GRANT ALL ON TABLE public.movstoc TO nobody;


--
-- Name: SEQUENCE movstoc_movc_id_seq; Type: ACL; Schema: public; Owner: rulemanes
--

GRANT UPDATE ON SEQUENCE public.movstoc_movc_id_seq TO nobody;


--
-- Name: TABLE operadores; Type: ACL; Schema: public; Owner: rulemanes
--

GRANT ALL ON TABLE public.operadores TO nobody;


--
-- Name: SEQUENCE operadores_operad_id_seq; Type: ACL; Schema: public; Owner: rulemanes
--

GRANT UPDATE ON SEQUENCE public.operadores_operad_id_seq TO nobody;


--
-- Name: TABLE pais; Type: ACL; Schema: public; Owner: rulemanes
--

GRANT ALL ON TABLE public.pais TO nobody;


--
-- Name: TABLE prove; Type: ACL; Schema: public; Owner: rulemanes
--

GRANT ALL ON TABLE public.prove TO nobody;


--
-- Name: SEQUENCE prove_prov_id_seq; Type: ACL; Schema: public; Owner: rulemanes
--

GRANT UPDATE ON SEQUENCE public.prove_prov_id_seq TO nobody;


--
-- Name: TABLE provin; Type: ACL; Schema: public; Owner: rulemanes
--

GRANT ALL ON TABLE public.provin TO nobody;


--
-- Name: SEQUENCE provin_pro_codigo_seq; Type: ACL; Schema: public; Owner: rulemanes
--

GRANT UPDATE ON SEQUENCE public.provin_pro_codigo_seq TO nobody;


--
-- Name: TABLE ptovta; Type: ACL; Schema: public; Owner: rulemanes
--

GRANT ALL ON TABLE public.ptovta TO nobody;


--
-- Name: SEQUENCE ptovta_ptv_id_seq; Type: ACL; Schema: public; Owner: rulemanes
--

GRANT UPDATE ON SEQUENCE public.ptovta_ptv_id_seq TO nobody;


--
-- Name: TABLE pxarticu; Type: ACL; Schema: public; Owner: estlm
--

GRANT ALL ON TABLE public.pxarticu TO nobody;


--
-- Name: SEQUENCE pxarticu_pxa_id_seq; Type: ACL; Schema: public; Owner: estlm
--

GRANT UPDATE ON SEQUENCE public.pxarticu_pxa_id_seq TO nobody;


--
-- Name: TABLE receta; Type: ACL; Schema: public; Owner: rulemanes
--

GRANT ALL ON TABLE public.receta TO nobody;


--
-- Name: SEQUENCE receta_re_id_seq; Type: ACL; Schema: public; Owner: rulemanes
--

GRANT UPDATE ON SEQUENCE public.receta_re_id_seq TO nobody;


--
-- Name: TABLE relcotnot; Type: ACL; Schema: public; Owner: rulemanes
--

GRANT ALL ON TABLE public.relcotnot TO nobody;


--
-- Name: SEQUENCE relcotnot_rn_id_seq; Type: ACL; Schema: public; Owner: rulemanes
--

GRANT UPDATE ON SEQUENCE public.relcotnot_rn_id_seq TO nobody;


--
-- Name: TABLE relmenu; Type: ACL; Schema: public; Owner: rulemanes
--

GRANT ALL ON TABLE public.relmenu TO nobody;


--
-- Name: SEQUENCE relmenu_rmen_id_seq; Type: ACL; Schema: public; Owner: rulemanes
--

GRANT UPDATE ON SEQUENCE public.relmenu_rmen_id_seq TO nobody;


--
-- Name: TABLE relnotp; Type: ACL; Schema: public; Owner: rulemanes
--

GRANT ALL ON TABLE public.relnotp TO nobody;


--
-- Name: SEQUENCE relnotp_rn_id_seq; Type: ACL; Schema: public; Owner: rulemanes
--

GRANT UPDATE ON SEQUENCE public.relnotp_rn_id_seq TO nobody;


--
-- Name: TABLE relrem; Type: ACL; Schema: public; Owner: rulemanes
--

GRANT ALL ON TABLE public.relrem TO nobody;


--
-- Name: SEQUENCE relrem_rr_id_seq; Type: ACL; Schema: public; Owner: rulemanes
--

GRANT UPDATE ON SEQUENCE public.relrem_rr_id_seq TO nobody;


--
-- Name: TABLE relremc; Type: ACL; Schema: public; Owner: rulemanes
--

GRANT ALL ON TABLE public.relremc TO nobody;


--
-- Name: SEQUENCE relremc_rr_id_seq; Type: ACL; Schema: public; Owner: rulemanes
--

GRANT UPDATE ON SEQUENCE public.relremc_rr_id_seq TO nobody;


--
-- Name: TABLE rtoxcontr; Type: ACL; Schema: public; Owner: rulemanes
--

GRANT ALL ON TABLE public.rtoxcontr TO nobody;


--
-- Name: TABLE rubros; Type: ACL; Schema: public; Owner: rulemanes
--

GRANT ALL ON TABLE public.rubros TO nobody;


--
-- Name: SEQUENCE rubros_rub_id_seq; Type: ACL; Schema: public; Owner: rulemanes
--

GRANT UPDATE ON SEQUENCE public.rubros_rub_id_seq TO nobody;


--
-- Name: TABLE rutaxastoh; Type: ACL; Schema: public; Owner: rulemanes
--

GRANT ALL ON TABLE public.rutaxastoh TO nobody;


--
-- Name: SEQUENCE rutaxastoh_ra_id_seq; Type: ACL; Schema: public; Owner: rulemanes
--

GRANT UPDATE ON SEQUENCE public.rutaxastoh_ra_id_seq TO nobody;


--
-- Name: TABLE stock; Type: ACL; Schema: public; Owner: rulemanes
--

GRANT ALL ON TABLE public.stock TO nobody;


--
-- Name: SEQUENCE stock_stc_id_seq; Type: ACL; Schema: public; Owner: rulemanes
--

GRANT UPDATE ON SEQUENCE public.stock_stc_id_seq TO nobody;


--
-- Name: TABLE stockinicial; Type: ACL; Schema: public; Owner: zona
--

GRANT ALL ON TABLE public.stockinicial TO nobody;


--
-- Name: SEQUENCE stockinicial_stk_id_seq; Type: ACL; Schema: public; Owner: zona
--

GRANT UPDATE ON SEQUENCE public.stockinicial_stk_id_seq TO nobody;


--
-- Name: TABLE subd; Type: ACL; Schema: public; Owner: rulemanes
--

GRANT ALL ON TABLE public.subd TO nobody;


--
-- Name: SEQUENCE subd_subd_id_seq; Type: ACL; Schema: public; Owner: rulemanes
--

GRANT UPDATE ON SEQUENCE public.subd_subd_id_seq TO nobody;


--
-- Name: TABLE subxcomp; Type: ACL; Schema: public; Owner: rulemanes
--

GRANT ALL ON TABLE public.subxcomp TO nobody;


--
-- Name: TABLE sucursalc; Type: ACL; Schema: public; Owner: rulemanes
--

GRANT ALL ON TABLE public.sucursalc TO nobody;


--
-- Name: TABLE sucursalp; Type: ACL; Schema: public; Owner: rulemanes
--

GRANT ALL ON TABLE public.sucursalp TO nobody;


--
-- Name: TABLE talon; Type: ACL; Schema: public; Owner: rulemanes
--

GRANT ALL ON TABLE public.talon TO nobody;


--
-- Name: SEQUENCE talon_tal_id_seq; Type: ACL; Schema: public; Owner: rulemanes
--

GRANT UPDATE ON SEQUENCE public.talon_tal_id_seq TO nobody;


--
-- Name: TABLE telefon; Type: ACL; Schema: public; Owner: rulemanes
--

GRANT ALL ON TABLE public.telefon TO nobody;


--
-- Name: SEQUENCE telefon_tel_id_seq; Type: ACL; Schema: public; Owner: rulemanes
--

GRANT UPDATE ON SEQUENCE public.telefon_tel_id_seq TO nobody;


--
-- Name: TABLE tipasi; Type: ACL; Schema: public; Owner: rulemanes
--

GRANT ALL ON TABLE public.tipasi TO nobody;


--
-- Name: TABLE tipcta; Type: ACL; Schema: public; Owner: rulemanes
--

GRANT ALL ON TABLE public.tipcta TO nobody;


--
-- Name: SEQUENCE tipcta_tct_codigo_seq; Type: ACL; Schema: public; Owner: rulemanes
--

GRANT UPDATE ON SEQUENCE public.tipcta_tct_codigo_seq TO nobody;


--
-- Name: TABLE tipdocu; Type: ACL; Schema: public; Owner: rulemanes
--

GRANT ALL ON TABLE public.tipdocu TO nobody;


--
-- Name: TABLE tipemp; Type: ACL; Schema: public; Owner: rulemanes
--

GRANT ALL ON TABLE public.tipemp TO nobody;


--
-- Name: TABLE tipentid; Type: ACL; Schema: public; Owner: rulemanes
--

GRANT ALL ON TABLE public.tipentid TO nobody;


--
-- Name: TABLE tiptel; Type: ACL; Schema: public; Owner: rulemanes
--

GRANT ALL ON TABLE public.tiptel TO nobody;


--
-- Name: SEQUENCE tiptel_ttl_codigo_seq; Type: ACL; Schema: public; Owner: rulemanes
--

GRANT UPDATE ON SEQUENCE public.tiptel_ttl_codigo_seq TO nobody;


--
-- Name: TABLE tributosfe; Type: ACL; Schema: public; Owner: rulemanes
--

GRANT ALL ON TABLE public.tributosfe TO nobody;


--
-- Name: SEQUENCE tributosfe_t_id_seq; Type: ACL; Schema: public; Owner: rulemanes
--

GRANT UPDATE ON SEQUENCE public.tributosfe_t_id_seq TO nobody;


--
-- Name: TABLE unimed; Type: ACL; Schema: public; Owner: rulemanes
--

GRANT ALL ON TABLE public.unimed TO nobody;


--
-- Name: TABLE usermenu; Type: ACL; Schema: public; Owner: rulemanes
--

GRANT ALL ON TABLE public.usermenu TO nobody;


--
-- Name: SEQUENCE usermenu_umen_id_seq; Type: ACL; Schema: public; Owner: rulemanes
--

GRANT UPDATE ON SEQUENCE public.usermenu_umen_id_seq TO nobody;


--
-- Name: TABLE varambi; Type: ACL; Schema: public; Owner: rulemanes
--

GRANT ALL ON TABLE public.varambi TO nobody;


--
-- Name: TABLE vendedor; Type: ACL; Schema: public; Owner: rulemanes
--

GRANT ALL ON TABLE public.vendedor TO nobody;


--
-- Name: SEQUENCE vendedor_ven_id_seq; Type: ACL; Schema: public; Owner: rulemanes
--

GRANT UPDATE ON SEQUENCE public.vendedor_ven_id_seq TO nobody;


--
-- Name: SEQUENCE zona_zon_codigo_seq; Type: ACL; Schema: public; Owner: rulemanes
--

GRANT UPDATE ON SEQUENCE public.zona_zon_codigo_seq TO nobody;


--
--

