CREATE OR REPLACE FUNCTION food600.sk__sez_ziadanky()
    RETURNS trigger
    LANGUAGE 'plpgsql'
    COST 100
    VOLATILE NOT LEAKPROOF
AS $BODY$
DECLARE 
robziadanku SMALLINT;
robodpocet SMALLINT;
defcpartner INTEGER;
limidkas CHARACTER(2);
pocetvsk INTEGER;
vystup INTEGER;
mmeno CHARACTER(10);
msetupskup CHARACTER(2);
cdata SMALLINT;
mchl CHARACTER(2);
pocetriadkov INTEGER DEFAULT 1000;
mdone INTEGER DEFAULT 0;
sumacelkom DOUBLE PRECISION;
sumadlhov DOUBLE PRECISION;
limitdlhov NUMERIC(10,2);
odp_c_hl CURSOR FOR SELECT DISTINCT cen_hlad FROM food600.sk__pol WHERE c_uzaverka=NEW.c_uzaverka AND c_stredisk=NEW.c_stredisk AND c_data=NEW.c_data;
BEGIN
cdata:=NEW.c_data;
SELECT setup_skup INTO msetupskup FROM food600.food_dat WHERE id=NEW.id_man;
SELECT food600.dajvarnamecharacter('FOOD_SET','','lim_idkas') INTO limidkas;
IF NEW.id_kas<>limidkas AND NEW.id_kas IN (SELECT id FROM food600.food_dat WHERE pgm='KAS' OR pgm='K32') AND POSITION('#' in NEW.uzaverka)=0 THEN
	mmeno:=UPPER(SUBSTRING(md5(random()::text), 0, 10));
	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 food600.dajvarnamelogical('MAN',msetupskup,'en_auto_odpocet') INTO robodpocet;
	IF robodpocet>0 THEN 
		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;
--raise notice 'mchl:%', mchl;
			IF mdone<=pocetriadkov 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, mnozstvi FROM (SELECT c_karty, SUM(mnozstvi/dil_porce) AS mnozstvi FROM food600.sk__pol WHERE cen_hlad=mchl AND c_uzaverka=NEW.c_uzaverka AND c_stredisk=NEW.c_stredisk AND c_data=cdata AND k_odpoctu<>'F' AND k_odpoctu<>'C' AND k_odpoctu<>' ' GROUP BY c_karty) aaa WHERE mnozstvi<0;
				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, NEW.c_stredisk, NEW.c_mena, cdata, mchl, NEW.id_kas, NEW.uzaverka, 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, mnozstvi FROM (SELECT c_karty, SUM(mnozstvi/dil_porce) AS mnozstvi FROM food600.sk__pol WHERE cen_hlad=mchl AND c_uzaverka=NEW.c_uzaverka AND c_stredisk=NEW.c_stredisk AND c_data=cdata AND k_odpoctu<>'F' AND k_odpoctu<>'C' AND k_odpoctu<>' ' GROUP BY c_karty) aaa WHERE mnozstvi>0;
				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, NEW.c_stredisk, NEW.c_mena, cdata, mchl, NEW.id_kas, NEW.uzaverka, 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;
		UPDATE food600.buffer SET poradi=c_karty 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;
		UPDATE food600.buffer SET cena=food600.prep_meny(karty.c_mena_n,NEW.c_mena,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;
		SELECT SUM((mnozstvi+dluh_mnoz)*cena), SUM(dluh_mnoz*cena) INTO sumacelkom, sumadlhov FROM food600.buffer WHERE menobuf=mmeno AND c_data=cdata;
		SELECT food600.dajvarnamenumeric('MAN',msetupskup,'lim_dluhu') INTO limitdlhov;
		IF sumacelkom<>0 THEN
			IF sumadlhov/sumacelkom*100>limitdlhov THEN
				DELETE FROM food600.buffer WHERE menobuf=mmeno AND operace='VK';
				DELETE FROM food600.bufferd WHERE menobuf=mmeno AND operace='VK';
				robodpocet:=0;
			END IF;
		END IF; 
		DELETE FROM food600.odpocet_kalk WHERE menobuf=mmeno AND c_data=cdata;
	END IF;

	SELECT food600.dajvarnamelogical('FOOD_SET','','auto_ziadanka_kas') INTO robziadanku;
	IF robziadanku>0 THEN 
		SELECT food600.dajvarnameinteger('MAN',msetupskup,'def_c_partner_zv') INTO defcpartner;
		IF defcpartner < 0 THEN
--Odpocet normalnych poloziek
			INSERT INTO food600.sk1 (menobuf, c_data, c_karty, mnozstvi) SELECT mmeno as menobuf, NEW.c_data as c_data, c_karty, mnozstvi FROM (SELECT c_karty, SUM(mnozstvi/dil_porce) AS mnozstvi FROM food600.sk__pol WHERE c_uzaverka=NEW.c_uzaverka AND c_stredisk=NEW.c_stredisk AND c_data=NEW.c_data AND k_odpoctu<>'F' AND k_odpoctu<>'C' AND k_odpoctu<>' ' GROUP BY c_karty) aaa WHERE mnozstvi>0;
			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_ziadanky(mmeno, NEW.c_stredisk, NEW.c_data, NEW.id_kas, NEW.uzaverka, defcpartner) INTO vystup;
				DELETE FROM food600.sk1 WHERE menobuf=mmeno AND c_data=cdata;
			END IF;
			DELETE FROM food600.sk1 WHERE menobuf=mmeno AND c_data=cdata;
		END IF;
	END IF;
	IF robodpocet>0 OR robziadanku>0 THEN
		UPDATE food600.buffer SET mnozstvi=-mnozstvi WHERE menobuf=mmeno AND operace='VK';
		PERFORM food600.buff_out(mmeno,cdata,NEW.c_mena,'','',0);
		IF robodpocet>0 THEN
			UPDATE food600.sk__sez SET odecteno=1 WHERE c_uzaverka=NEW.c_uzaverka AND c_stredisk=NEW.c_stredisk;
		END IF;
	END IF;
	DELETE FROM food600.odpocet_askl WHERE menobuf=mmeno AND c_data=cdata;
	DELETE FROM food600.odpocet_kalk WHERE menobuf=mmeno AND c_data=cdata;
	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;
	DELETE FROM food600.sk1 WHERE menobuf=mmeno AND c_data=cdata;
END IF;
RETURN NEW;
END
$BODY$;
