CREATE OR REPLACE FUNCTION food600.odpocet_multidavky(
	mmeno character varying,
	cdata integer,
	muzaverka character varying,
	midkas character varying,
	mcstredisk integer,
	mmenaakt integer,
	mdavka character varying)
    RETURNS integer
    LANGUAGE 'plpgsql'
    COST 100
    VOLATILE
AS $BODY$
DECLARE mchl CHARACTER(2);
	pocetriadkov INTEGER DEFAULT 1000;
	pocetriadkov1 INTEGER DEFAULT 1000;
	pocetvsk INTEGER;
	mdone INTEGER DEFAULT 0;
	mdone1 INTEGER DEFAULT 0;
	vystup INTEGER;
	mcsklad INTEGER;
	strediska CURSOR FOR SELECT DISTINCT c_sklad FROM food600.davky WHERE davka=mdavka AND c_data=cdata AND c_stredisk=mcstredisk;
BEGIN
DELETE FROM food600.sk1 WHERE c_data=cdata AND menobuf=mmeno;
DELETE FROM food600.buffer WHERE menobuf=mmeno AND c_data=cdata;
DELETE FROM food600.bufferd WHERE menobuf=mmeno AND c_data=cdata;
DELETE FROM food600.odp_log WHERE menobuf=mmeno AND c_data=cdata;
SELECT cen_hlad INTO mchl FROM food600.davky WHERE c_data=cdata AND davka=mdavka AND c_stredisk=mcstredisk LIMIT 1;
IF mchl ISNULL THEN
	pocetriadkov:=0;
END IF;
IF mdone<=pocetriadkov THEN
	OPEN strediska;
	WHILE (mdone1<pocetriadkov1) LOOP
		FETCH strediska INTO mcsklad;
		IF mcsklad ISNULL THEN
			pocetriadkov1:=0;
		END IF;
		IF mdone1<pocetriadkov1 THEN
			DELETE FROM food600.sk1 WHERE menobuf=mmeno AND c_data=cdata;
			INSERT INTO food600.sk1 (menobuf, c_data, c_karty, mnozstvi) SELECT mmeno as menobuf, cdata as c_data, c_karty, SUM(mnozstvi) as mnozstvi FROM food600.davky WHERE davka=mdavka AND c_data=cdata AND mnozstvi<0 AND c_stredisk=mcstredisk AND c_sklad=mcsklad AND c_karty NOT IN (SELECT c_karty FROM food600.karty WHERE c_druhk IN (SELECT c_druh FROM food600.skupiny WHERE spart_kas='SRV')) GROUP BY c_karty, menobuf, c_data;
			SELECT COUNT(*) INTO pocetvsk FROM food600.sk1 WHERE menobuf=mmeno AND c_data=cdata;
--Odpocet storien
			IF pocetvsk>0 THEN
				INSERT INTO food600.odp_log (hloubka, c_karty, hruba, menobuf, c_data) SELECT 0 AS hloubka, c_karty, mnozstvi AS hruba, mmeno AS menobuf, c_data FROM food600.sk1 WHERE menobuf = mmeno AND c_data=cdata;
				SELECT food600.kalkuluj(mmeno, mcsklad, mmenaakt, cdata, mchl, midkas, muzaverka, 0::smallint) INTO vystup;
				DELETE FROM food600.sk1 WHERE menobuf=mmeno AND c_data=cdata;
			END IF;
--Odpocet normalnych poloziek
			INSERT INTO food600.sk1 (menobuf, c_data, c_karty, mnozstvi) SELECT mmeno as menobuf, cdata as c_data, c_karty, SUM(mnozstvi) as mnozstvi FROM  food600.davky WHERE davka=mdavka AND c_data=cdata AND mnozstvi>0 AND c_stredisk=mcstredisk AND c_sklad=mcsklad AND c_karty NOT IN (SELECT c_karty FROM food600.karty WHERE c_druhk IN (SELECT c_druh FROM food600.skupiny WHERE spart_kas='SRV'))  GROUP BY c_karty, menobuf, c_data;
			SELECT COUNT(*) INTO pocetvsk FROM food600.sk1 WHERE menobuf=mmeno AND c_data=cdata;
			IF pocetvsk>0 THEN
				INSERT INTO food600.odp_log (hloubka, c_karty, hruba, menobuf, c_data) SELECT 0 AS hloubka, c_karty, mnozstvi AS hruba, mmeno AS menobuf, c_data FROM food600.sk1 WHERE menobuf = mmeno AND c_data=cdata;
				SELECT food600.kalkuluj(mmeno, mcsklad, mmenaakt, cdata, mchl, midkas, muzaverka, 0::smallint) INTO vystup;
				DELETE FROM food600.sk1 WHERE menobuf=mmeno AND c_data=cdata;
			END IF;
		END IF;
	END LOOP;
	CLOSE strediska;
END IF;
DELETE FROM food600.sk1 WHERE c_data=cdata AND menobuf=mmeno;
UPDATE food600.buffer SET nazev=karty.nazev FROM food600.karty WHERE buffer.c_karty=karty.c_karty AND buffer.c_data=karty.c_data AND buffer.menobuf=mmeno AND buffer.c_data=cdata;
UPDATE food600.buffer SET cena=odpocet_askl.cena_prum, zustatek=odpocet_askl.zustatek FROM food600.odpocet_askl WHERE buffer.c_karty=odpocet_askl.c_karty AND buffer.c_data=odpocet_askl.c_data AND buffer.menobuf=odpocet_askl.menobuf AND buffer.menobuf=mmeno AND buffer.c_data=cdata AND buffer.c_stredisk=odpocet_askl.c_stredisk;
UPDATE food600.buffer SET cena=food600.prep_meny(karty.c_mena_n,mmenaakt,karty.cena_prum), serial_no='CENA0' FROM food600.karty WHERE buffer.c_karty=karty.c_karty AND buffer.c_data=karty.c_data AND buffer.menobuf=mmeno AND buffer.c_data=cdata AND mnozstvi=0 AND cena=0;
UPDATE food600.bufferd SET pz1=mdavka WHERE bufferd.menobuf=mmeno AND bufferd.c_data=cdata;
DELETE FROM food600.odpocet_askl WHERE menobuf=mmeno AND c_data=cdata;
DELETE FROM food600.odpocet_kalk WHERE menobuf=mmeno AND c_data=cdata;
RETURN 1;
END
$BODY$;
