CREATE OR REPLACE FUNCTION food600.suma_dokladu(
	ip_c_dokl integer,
	ip_c_stredisk integer,
	ip_c_data integer,
	ip_archiv integer)
    RETURNS numeric
    LANGUAGE 'plpgsql'
    COST 100
    VOLATILE
AS $BODY$
DECLARE ltotal numeric (12,4);
BEGIN

IF ip_archiv = 1 THEN
 SELECT sum(obraty_a.cena * obraty_a.mnozstvi) AS total into ltotal
  FROM food600.obraty_a
  WHERE obraty_a.c_dokl = ip_c_dokl and obraty_a.c_stredisk = ip_c_stredisk and obraty_a.c_data = ip_c_data;
ELSE
 SELECT sum(obraty.cena * obraty.mnozstvi) AS total into ltotal
  FROM food600.obraty
  WHERE obraty.c_dokl = ip_c_dokl and obraty.c_stredisk = ip_c_stredisk and obraty.c_data = ip_c_data;
END IF;

 return ltotal;

END
$BODY$;
