CREATE OR REPLACE FUNCTION food600.odpocet(
	mmeno character varying,
	cdata integer,
	muzaverka character varying,
	midkas character varying,
	mcstredisk integer,
	mprvy integer,
	mposledny integer,
	mmenaakt integer)
    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;
	mcuzaverka INTEGER;
	odp_c_hl CURSOR FOR SELECT DISTINCT cen_hlad FROM food600.sk__pol WHERE c_uzaverka=mcuzaverka AND c_stredisk=mcstredisk AND c_data=cdata 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'));
BEGIN
SELECT id INTO midman FROM food600.food_dat WHERE c_stredisk=mcstredisk AND c_data=cdata;
SELECT c_uzaverka INTO mcuzaverka FROM food600.sk__sez WHERE SUBSTRING(uzaverka FROM 1 FOR 17)=muzaverka AND id_kas=midkas AND c_data=cdata AND id_man=midman;
DELETE FROM food600.sk1 WHERE c_data=cdata AND menobuf=mmeno;
IF mprvy THEN 
	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;
END IF;
OPEN odp_c_hl;
WHILE (mdone<pocetriadkov) LOOP
	mdone:=mdone+1;
	FETCH odp_c_hl INTO mchl;
	IF mchl ISNULL THEN
		pocetriadkov:=0;
	END IF;
	
	IF mdone<=pocetriadkov THEN
		DELETE FROM food600.sk1 WHERE menobuf=mmeno AND c_data=cdata;
		IF POSITION('#' in muzaverka)>0 THEN
			INSERT INTO food600.sk1 (menobuf, c_data, c_karty, mnozstvi) SELECT mmeno as menobuf, cdata as c_data, CAST(stor_mno AS INTEGER) as c_karty, SUM(mnozstvi/dil_porce) as mnozstvi FROM food600.sk__pol WHERE c_uzaverka=mcuzaverka AND c_data=cdata AND cen_hlad=mchl AND c_stredisk=mcstredisk AND mnozstvi<0 AND c_data=cdata 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;
		ELSE 
			INSERT INTO food600.sk1 (menobuf, c_data, c_karty, mnozstvi) SELECT mmeno as menobuf, cdata as c_data, c_karty, mnozstvi FROM (SELECT c_karty, SUM(mnozstvi/dil_porce) AS mnozstvi FROM food600.sk__pol WHERE cen_hlad=mchl AND c_uzaverka=mcuzaverka AND c_stredisk=mcstredisk AND c_data=cdata AND k_odpoctu<>'F' AND k_odpoctu<>'C' AND k_odpoctu<>' ' 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) aaa WHERE mnozstvi<0;
		END IF;
		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, mcstredisk, 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
		IF POSITION('#' in muzaverka)>0 THEN
			INSERT INTO food600.sk1 (menobuf, c_data, c_karty, mnozstvi) SELECT mmeno as menobuf, cdata as c_data, CAST(stor_mno AS INTEGER) AS c_karty, SUM(mnozstvi/dil_porce) as mnozstvi FROM food600.sk__pol WHERE c_uzaverka=mcuzaverka AND c_data=cdata AND cen_hlad=mchl AND c_stredisk=mcstredisk AND mnozstvi>0 AND c_data=cdata 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;
		ELSE
			INSERT INTO food600.sk1 (menobuf, c_data, c_karty, mnozstvi) SELECT mmeno as menobuf, cdata as c_data, c_karty, mnozstvi FROM (SELECT c_karty, SUM(mnozstvi/dil_porce) AS mnozstvi FROM food600.sk__pol WHERE cen_hlad=mchl AND c_uzaverka=mcuzaverka AND c_stredisk=mcstredisk AND c_data=cdata AND k_odpoctu<>'F' AND k_odpoctu<>'C' AND k_odpoctu<>' ' 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) aaa WHERE mnozstvi>0;
		END IF;
		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, mcstredisk, 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 odp_c_hl;
DELETE FROM food600.sk1 WHERE c_data=cdata AND menobuf=mmeno;
IF mposledny THEN 
	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;
	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;
END IF;
RETURN 1;
END
$BODY$;
