CREATE FUNCTION idcbl(integer, integer, integer) RETURNS integer
    LANGUAGE plpgsql
    AS $_$
DECLARE
    codemp		ALIAS FOR $1; 
    ptovta		ALIAS FOR $2; 
    codcble		ALIAS FOR $3; 
    retval 		varchar;
    BEGIN

    select cbl_id into retval from ctacble where cbl_codemp=codemp and cbl_idptov=ptovta and int4eq(cbl_codigo,codcble);
    if not found then
	select cbl_id into retval from ctacble where cbl_codemp=codemp and int4eq(0,cbl_idptov) and int4eq(cbl_codigo,codcble);
	if not found then
	    select cbl_id into retval from ctacble where int4eq(0,cbl_codemp) and int4eq(0,cbl_idptov) and int4eq(cbl_codigo,codcble);
	end if;
    end if;
RETURN retval;
END;$_$;


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

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

CREATE FUNCTION idcble(integer, integer, character varying) RETURNS integer
    LANGUAGE plpgsql
    AS $_$
DECLARE
    codemp		ALIAS FOR $1; 
    ptovta		ALIAS FOR $2; 
    codigo		ALIAS FOR $3; 
    codcble		integer;
    retval 		varchar;
    BEGIN

    select vari_valor into codcble 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 codcble 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 codcble from varambi where int4eq(0,vari_idptov) and int4eq(0,vari_codemp) and bpchareq(codigo,vari_codigo);
	end if;
    end if;

    select cbl_id into retval from ctacble where cbl_codemp=codemp and cbl_idptov=ptovta and int4eq(cbl_codigo,codcble::int4) ;
    if not found then
	select cbl_id into retval from ctacble where cbl_codemp=codemp and int4eq(0,cbl_idptov) and int4eq(cbl_codigo,codcble::int4) ;
	if not found then
	    select cbl_id into retval from ctacble where int4eq(0,cbl_codemp) and int4eq(0,cbl_idptov) and int4eq(cbl_codigo,codcble::int4) ;
	end if;
    end if;
RETURN retval;
END;$_$;


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