CREATE OR REPLACE FUNCTION valtari(integer, integer, numeric, smallint, date) RETURNS numeric
    AS $_$
declare
usuario		 alias for $1;
articulo	 alias for $2;
canti		 alias for $3;
dias		 alias for $4;
fecha		 alias for $5;
ret 		 numeric;
paso 		 numeric;
paso5 		 numeric;
registro	 record;
regi		 record;
hay	 	 int4;
articulo5 	 int4;
coti 	 	 numeric;
importe 	 numeric;
begin

    for registro in select 
	    * from tarifas where tari_imp_dolar!=0 and tari_usuario=usuario and tari_idart=articulo and 
	    date_ge(tari_fhasta,fecha) and date_le(tari_fdesde,fecha) limit 1
	loop
	    if numeric_ne(canti,0::numeric) then
		if (registro.tari_cdesde is not null and registro.tari_chasta is not null and numeric_ge(registro.tari_chasta,canti) and numeric_le(registro.tari_cdesde,canti)) then
			paso=1;
		    else
			paso=0;
		end if;
	    end if; 

	    if int2ne(dias,0::int2) then
		if (registro.tari_ddesde is not null and registro.tari_dhasta is not null and int2ge(registro.tari_dhasta,dias) and int2le(registro.tari_ddesde,dias)) then
			paso=1;
		    else
			paso=0;
		end if;
	    end if; 

	    if paso is null then 
		paso=1; 
	    end if;

	    importe=registro.tari_imp_dolar;
	    select cot_cotiz into coti from cotiza where cot_moneda=1 order by cot_fecha desc limit 1;

	    if numeric_ge(coti,registro.tari_cot_dolar) then
		    ret= importe * coti * paso;
		else
		    ret= importe * registro.tari_cot_dolar * paso;
	    end if;
	end loop; 


    if ret is null then
    if int4ge(articulo,27) then
    if int4le(articulo,31) then
	articulo5=articulo - 5;
	for regi in select 
	    * from tarifas where tari_imp_dolar!=0 and tari_usuario=usuario and tari_idart=articulo5 and 
	    date_ge(tari_fhasta,fecha) and date_le(tari_fdesde,fecha) limit 1
	    loop
		if numeric_ne(canti,0::numeric) then
		    if (regi.tari_cdesde is not null and regi.tari_chasta is not null and numeric_ge(regi.tari_chasta,canti) and numeric_le(regi.tari_cdesde,canti)) then
			paso5=1;
		      else
			paso5=0;
		    end if;
		end if; 

		if int2ne(dias,0::int2) then
		    if (regi.tari_ddesde is not null and regi.tari_dhasta is not null and int2ge(regi.tari_dhasta,dias) and int2le(regi.tari_ddesde,dias)) then
			paso5=1;
		      else
			paso5=0;
		    end if;
		end if; 

		if paso5 is null then 
		    paso5=1; 
		end if;

		importe=regi.tari_imp_dolar;
		select cot_cotiz into coti from cotiza where cot_moneda=1 order by cot_fecha desc limit 1;

		if numeric_ge(coti,regi.tari_cot_dolar) then
		    ret= importe * coti * paso5;
		  else
		    ret= importe * regi.tari_cot_dolar * paso5;
		end if;
	    end loop; 
    end if;
    end if;
    end if;




	if ret is null then
	    select tari_importe into ret from tarifas where tari_usuario=usuario and tari_idart=articulo and 
	    date_ge(tari_fhasta,fecha) and date_le(tari_fdesde,fecha) limit 1;
	end if;

    if ret is null then
    if int4ge(articulo,27) then
    if int4le(articulo,31) then
	articulo5=articulo - 5;
	    select tari_importe into ret from tarifas where tari_usuario=usuario and tari_idart=articulo5 and 
	    date_ge(tari_fhasta,fecha) and date_le(tari_fdesde,fecha) limit 1;
    end if;
    end if;
    end if;


	if ret is null then
	    if int4eq(articulo,50) then ret=20; 
		else
		    if int4eq(articulo,35) then ret=145.16; 
			else
			    if int4eq(articulo,40) then ret=108.87; 
				else
				    ret=0; 
			    end if;
		    end if;
	    end if;
	end if;


return ret;
end;$_$
    LANGUAGE plpgsql;

