CREATE OR REPLACE FUNCTION food600.odpocet_nateraz_stop(
	mmeno character varying,
	cdata integer,
	mcstredisk integer,
	mmenaakt integer,
	ajinventura smallint)
    RETURNS integer
    LANGUAGE 'plpgsql'
    COST 100
    VOLATILE 
AS $BODY$
DECLARE mchl CHARACTER(2);
	pocetriadkov INTEGER DEFAULT 1000;
	pocetvsk INTEGER;
	mdone INTEGER DEFAULT 0;
	midman CHARACTER(2);
	vystup INTEGER;
	cmenu INTEGER;
	mmnozstvi numeric(20,10);
	odpminus CURSOR FOR SELECT sk__pol.c_karty, sum(sk__pol.mnozstvi) as mnozstvi FROM food600.sk__pol INNER JOIN food600.karty ON sk__pol.c_karty=karty.c_karty INNER JOIN food600.skupiny ON karty.c_druhk=skupiny.c_druh WHERE skupiny.spart<>'MNU' AND skupiny.spart<>'FMN'  AND skupiny.spart_kas<>'SRV' AND (sk__pol.c_uzaverka in (SELECT c_stredisk FROM food600.food_dat where pgm='K32') or sk__pol.c_uzaverka=-987654) AND sk__pol.c_stredisk=mcstredisk AND sk__pol.c_data=cdata AND sk__pol.k_odpoctu<>'' AND sk__pol.k_odpoctu<>'F' GROUP BY sk__pol.c_karty;
	menicka CURSOR FOR SELECT sk__pol.c_karty, sum(sk__pol.mnozstvi) as mnozstvi FROM food600.sk__pol INNER JOIN food600.karty ON sk__pol.c_karty=karty.c_karty INNER JOIN food600.skupiny ON karty.c_druhk=skupiny.c_druh WHERE (skupiny.spart='MNU' OR skupiny.spart='FMN') AND (sk__pol.c_uzaverka in (SELECT c_stredisk FROM food600.food_dat where pgm='K32') or sk__pol.c_uzaverka=-987654) AND sk__pol.c_stredisk=mcstredisk AND sk__pol.c_data=cdata AND sk__pol.k_odpoctu<>'' AND sk__pol.k_odpoctu<>'F' GROUP BY sk__pol.c_karty;

--	menicka CURSOR FOR SELECT c_karty, mnozstvi FROM odpminus INNER JOIN food600.karty ON odpminus.c_karty=karty.c_karty INNER JOIN food600.skupiny ON karty.c_druhk=skupiny.c_druh WHERE skupiny.spart='MNU' OR skupiny.spart='FMN';
BEGIN
SELECT id INTO midman FROM food600.food_dat WHERE c_stredisk=mcstredisk AND c_data=cdata;
DELETE FROM food600.sk1 WHERE c_data=cdata AND menobuf=mmeno;

--Odpocet storien v neodpocitanych uzavierkach
INSERT INTO food600.sk1 (menobuf, c_data, c_karty, mnozstvi) SELECT mmeno as menobuf, sk__pol.c_data, sk__pol.c_karty, SUM(sk__pol.mnozstvi) AS mnozstvi FROM food600.sk__pol WHERE  (sk__pol.c_uzaverka in (SELECT c_stredisk FROM food600.food_dat where pgm='K32') or sk__pol.c_uzaverka=-987654) AND sk__pol.c_data = cdata AND  sk__pol.c_stredisk = mcstredisk AND sk__pol.k_odpoctu <> '' AND sk__pol.k_odpoctu <> 'F' AND sk__pol.mnozstvi<0 AND sk__pol.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 sk__pol.c_karty, sk__pol.c_data, menobuf;
SELECT COUNT(*) INTO pocetvsk FROM food600.sk1 WHERE menobuf=mmeno AND c_data=cdata;
IF pocetvsk>0 THEN
	SELECT food600.kalkuluj(mmeno, mcstredisk, mmenaakt, cdata, '', '', '', ajinventura::smallint) INTO vystup;
	DELETE FROM food600.sk1 WHERE menobuf=mmeno AND c_data=cdata;
END IF;
--Odpocet plusov v neodpocitanych uzavierkach
INSERT INTO food600.sk1 (menobuf, c_data, c_karty, mnozstvi) SELECT mmeno as menobuf, sk__pol.c_data,  sk__pol.c_karty, SUM(sk__pol.mnozstvi) AS mnozstvi FROM food600.sk__pol WHERE  (sk__pol.c_uzaverka in (SELECT c_stredisk FROM food600.food_dat where pgm='K32') or sk__pol.c_uzaverka=-987654) AND sk__pol.c_data = cdata AND  sk__pol.c_stredisk = mcstredisk AND sk__pol.k_odpoctu <> '' AND sk__pol.k_odpoctu <> 'F' AND sk__pol.mnozstvi>0 AND sk__pol.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 sk__pol.c_karty, sk__pol.c_data, menobuf;
SELECT COUNT(*) INTO pocetvsk FROM food600.sk1 WHERE menobuf=mmeno AND c_data=cdata;
IF pocetvsk>0 THEN
	SELECT food600.kalkuluj(mmeno, mcstredisk, mmenaakt, cdata, '', '', '', ajinventura::smallint) INTO vystup;
	DELETE FROM food600.sk1 WHERE menobuf=mmeno AND c_data=cdata;
END IF;
--Neuzavrete ucty - hladanie menu
OPEN menicka;
WHILE (mdone<pocetriadkov) LOOP
	mdone:=mdone+1;
	FETCH menicka INTO cmenu, mmnozstvi;
	IF cmenu ISNULL THEN
		pocetriadkov:=0;
	END IF;
	IF mdone<=pocetriadkov THEN
		INSERT INTO food600.sk1 (menobuf, c_data, c_karty, mnozstvi) SELECT mmeno as menobuf, cdata as c_data, c_poloz AS c_karty, mmnozstvi*polozky.hruba/karty.pocet AS mnozstvi FROM food600.polozky, food600.karty WHERE polozky.id_man=midman AND polozky.c_karty=cmenu AND polozky.c_data=cdata AND karty.c_karty=cmenu AND karty.c_data=cdata;
	END IF;
END LOOP;
CLOSE menicka;

/*mdone:=0;
pocetriadkov:=1000;
OPEN odpminus;
WHILE (mdone<pocetriadkov) LOOP
	mdone:=mdone+1;
	FETCH odpminus INTO cmenu, mmnozstvi;
	IF cmenu ISNULL THEN
		pocetriadkov:=0;
	END IF;
	IF mdone<=pocetriadkov THEN
		INSERT INTO food600.sk1 (menobuf, c_data, c_karty, mnozstvi) VALUES (mmeno, cdata, cmenu, mmnozstvi);
	END IF;
END LOOP;
CLOSE odpminus;
mdone:=0;
pocetriadkov:=1000;
SELECT COUNT(*) INTO pocetvsk FROM food600.sk1 WHERE menobuf=mmeno AND c_data=cdata;
IF pocetvsk>0 THEN
	SELECT food600.kalkuluj(mmeno, mcstredisk, mmenaakt, cdata, '', '', '', ajinventura::smallint) INTO vystup;
	DELETE FROM food600.sk1 WHERE menobuf=mmeno AND c_data=cdata;
END IF;*/
		
--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=mmeno AND buffer.c_data=cdata;
--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;
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$;
