CREATE OR REPLACE FUNCTION food600.max_maxima(
	)
    RETURNS integer
    LANGUAGE 'plpgsql'
    COST 100
    VOLATILE 
AS $BODY$
DECLARE maxx INTEGER DEFAULT 0;
		maxx1 INTEGER DEFAULT 0;
BEGIN
SELECT MAX(c_karty) INTO maxx FROM food600.karty;
IF maxx IS NOT NULL THEN
	UPDATE food600.maxima SET i_max=maxx+1 WHERE BTRIM(soubor)='karty';
	IF NOT FOUND THEN
		INSERT INTO food600.maxima (soubor, i_max) VALUES ('karty', maxx+1);
	END IF;
END IF;
SELECT MAX(c_druh) INTO maxx FROM food600.skupiny;
IF maxx IS NOT NULL THEN
	UPDATE food600.maxima SET i_max=maxx+1 WHERE BTRIM(soubor)='c_druh';
	IF NOT FOUND THEN
		INSERT INTO food600.maxima (soubor, i_max) VALUES ('c_druh', maxx+1);
	END IF;
END IF;
SELECT MAX(c_dokl) INTO maxx FROM food600.obraty;
IF maxx IS NOT NULL THEN
	SELECT MAX(c_dokl) INTO maxx1 FROM food600.obra_buf;
	IF maxx1 IS NOT NULL AND maxx1>maxx THEN
		maxx:=maxx1;
	END IF;
	SELECT MAX(c_dokl) INTO maxx1 FROM food600.doklady;
	IF maxx1 IS NOT NULL AND maxx1>maxx THEN
		maxx:=maxx1;
	END IF;
	SELECT MAX(c_dokl) INTO maxx1 FROM food600.dokl_buf;
	IF maxx1 IS NOT NULL AND maxx1>maxx THEN
		maxx:=maxx1;
	END IF;
	UPDATE food600.maxima SET i_max=maxx+10 WHERE BTRIM(soubor)='c_dokl';
	IF NOT FOUND THEN
		INSERT INTO food600.maxima (soubor, i_max) VALUES ('c_dokl', maxx+10);
	END IF;
END IF;
SELECT MAX(c_partner) INTO maxx FROM food600.partneri;
IF maxx IS NOT NULL THEN
	UPDATE food600.maxima SET i_max=maxx+1 WHERE BTRIM(soubor)='c_partner';
	IF NOT FOUND THEN
		INSERT INTO food600.maxima (soubor, i_max) VALUES ('c_partner', maxx+1);
	END IF;
END IF;
RETURN 1;
END
$BODY$;

