CREATE OR REPLACE FUNCTION food600.sk__on_a(
	ip_uzaverka character,
	ip_c_uzav integer,
	ip_c_data bigint)
    RETURNS integer
    LANGUAGE 'plpgsql'
    COST 100
    VOLATILE 
AS $BODY$
DECLARE 
lid_karty INTEGER;
lnew_cas CHARACTER(8);
lu_open CHARACTER(5);
lu_minut INTEGER;
lucet INTEGER;
lu1 INTEGER;
lu2 INTEGER;
lu3 INTEGER;
lu4 INTEGER;
lo_cis CHARACTER(7);
lu_cis CHARACTER(7);
lautor CHARACTER(3);
lu_cas CHARACTER(14);
lmutace CHARACTER(1);
lcas_atrib CHARACTER(1); 
lpocet_h numeric(20,10);
lstul  numeric(20,10);
ltext_sleva CHARACTER(16); 
ltext_link CHARACTER(25); 
lducis CHARACTER(4);
lidslave CHARACTER(2);
llim_datum DATE;
lid_limit BIGINT;
lround50 numeric(20,10);
lpohladavka SMALLINT;
lkompez SMALLINT;
lstormno numeric(20,10);
old_plt numeric(20,10);
old_pol1 numeric(20,10);
old_dan1 numeric(20,10);
old_pol2 numeric(20,10);
old_dan2 numeric(20,10);
old_pol3 numeric(20,10);
old_dan3 numeric(20,10);
old_pol4 numeric(20,10);
old_dan4 numeric(20,10);
old_pol5 numeric(20,10);
old_dan5 numeric(20,10);
old_round50 numeric(20,10);
pol1 numeric(20,10);
dan1 numeric(20,10);
pol2 numeric(20,10);
dan2 numeric(20,10);
pol3 numeric(20,10);
dan3 numeric(20,10);
pol4 numeric(20,10);
dan4 numeric(20,10);
pol5 numeric(20,10);
dan5 numeric(20,10);
oldriadok INTEGER;
olducet INTEGER;
lid_kas CHARACTER(2); 
lvid_kas CHARACTER(2);

fin CURSOR FOR 
	SELECT u_cis, autor, u_cas,  mutace, cas_atrib, pocet_h, stul, text_sleva, text_link, ducis, lim_datum, id_limit, round50, pohladavka,u_open, id_den, id_chod, id_slave, sleva_abs
	FROM food600.sk_fin_a WHERE uzaverka = ip_uzaverka AND c_data = ip_c_data AND lim_datum is not null and id_kas=lvid_kas
	UNION ALL
	SELECT u_cis, autor, u_cas,  mutace, cas_atrib, pocet_h, stul, text_sleva, text_link, ducis, lim_datum, id_limit, round50, pohladavka,u_open, id_den, id_chod, id_slave, sleva_abs
		FROM food600.sk_fin_a WHERE uzaverka = ip_uzaverka AND c_data = ip_c_data AND lim_datum IS NULL and id_kas=lvid_kas;

lid_man CHARACTER(2); 
lc_karty INTEGER;
lnazev CHARACTER(30);  
lprachy_puv numeric(20,10); 
lmnozstvi numeric(20,10);
lcen_hlad CHARACTER(2); 
lk_odpoctu CHARACTER(1);
ldan_sazba numeric(20,10);
lu_time CHARACTER(5);
ldotacia CHARACTER(1);
lid_fmenu CHARACTER(7);
lprachy_pol numeric(20,10); 
lprachy_plt numeric(20,10); 
lslev_koef numeric(20,10); 
lsleva_abs numeric(20,10); 
lprachy numeric(20,10);
ldan_pol numeric(20,10);
ldan numeric(20,10);
lc_kartyo INTEGER;
lc_stredisk INTEGER;
lsum_prachy_pol numeric(20,10);
lrozdil numeric(20,10);
lrozdil_dan numeric(20,10);
lporadi INTEGER;
ldil_porce NUMERIC(3);
ld_p CHARACTER(1);
lid_den INTEGER;
lid_chod INTEGER;

pol CURSOR FOR SELECT id_kas, id_man,  sk_pol_a.u_cis, sk_pol_a.c_karty, sk_pol_a.prachy AS prachy_puv, 
	mnozstvi, cen_hlad, k_odpoctu, (sk_pol_a.dan-1) * 100 AS dan_sazba,
	dotacia, id_fmenu, spol_plt_a.prachy_pol, spol_plt_a.prachy_plt, spol_plt_a.slev_koef, spol_plt_a.round50,
	spol_sum_dph_a.prachy, dan_pol,karty.c_karty AS c_kartyo,
	spol_sum_a.prachy_pol AS sum_prachy_pol, sk_pol_a.kompez
	FROM food600.sk_pol_a 
	LEFT OUTER JOIN food600.spol_plt_a     ON sk_pol_a.u_cis = spol_plt_a.u_cis AND sk_pol_a.uzaverka=spol_plt_a.uzaverka AND sk_pol_a.c_data = spol_plt_a.c_data
	LEFT OUTER JOIN food600.karty        ON sk_pol_a.c_karty = karty.c_karty AND sk_pol_a.c_karty=0 AND                    karty.c_data = ip_c_data
	LEFT OUTER JOIN food600.spol_sum_dph_a ON sk_pol_a.u_cis = spol_sum_dph_a.u_cis AND sk_pol_a.dan= spol_sum_dph_a.dan AND sk_pol_a.uzaverka = spol_sum_dph_a.uzaverka  AND sk_pol_a.c_data = spol_sum_dph_a.c_data
	LEFT OUTER JOIN food600.spol_sum_a     ON sk_pol_a.u_cis = spol_sum_a.u_cis AND sk_pol_a.uzaverka = spol_sum_a.uzaverka  AND sk_pol_a.c_data =  spol_sum_a.c_data
	WHERE sk_pol_a.uzaverka=ip_uzaverka AND sk_pol_a.c_data = ip_c_data and sk_pol_a.id_kas=lvid_kas ORDER BY sk_pol_a.u_cis;

BEGIN

SELECT id_kas INTO lvid_kas FROM food600.sk__uza WHERE idriadok=ip_c_uzav;
/* ----------------------------- TXT -------------------------------------------- */
INSERT INTO food600.sk__txt_a (c_uzaverka, ucet, poradi, txt, jmeno, adresa1, adresa2, adresa3, schvalil, akce, dic, cislo, hucet, hpokoj, hjmeno, hporad, hpref, hid, icdph, ico,c_data)
	SELECT ip_c_uzav, food600.tvor_cislo(SUBSTRING(u_cis FROM 3)), poradi, txt, jmeno, adresa1, adresa2, adresa3, schvalil, akce, dic, cislo, hucet, hpokoj, hjmeno, hporad, hpref, hid, icdph, ico,ip_c_data FROM food600.sk_txt_a where uzaverka = ip_uzaverka and id_kas=lvid_kas AND POSITION('TRANS' IN u_cis)=0;
INSERT INTO food600.sk__txt_a (c_uzaverka, ucet, poradi, txt, jmeno, adresa1, adresa2, adresa3, schvalil, akce, dic, cislo, hucet, hpokoj, hjmeno, hporad, hpref, hid, icdph, ico,c_data)
	SELECT ip_c_uzav, food600.tvor_cislo(SUBSTRING(u_cis FROM 3)), poradi, txt, jmeno, adresa1, adresa2, adresa3, schvalil, akce, dic, cislo, hucet, hpokoj, hjmeno, hporad, hpref, hid, icdph, 'TRANS' as ico,ip_c_data FROM food600.sk_txt_a where uzaverka = ip_uzaverka and id_kas=lvid_kas AND POSITION('TRANS' IN u_cis)>0;

/* ----------------------------- PLT -------------------------------------------- */
INSERT INTO food600.sk__plt_a (c_uzaverka, ucet, druh_pl, cena_pl, tip, mena, cena_mena, poradi, text,c_data, kompez, fik, bkp, cas_odesl)
	      SELECT ip_c_uzav,food600.tvor_cislo(SUBSTRING(u_cis FROM 3)), druh_pl, cena_pl, tip, mena, cena_mena, poradi, '',ip_c_data, kompez, fik, bkp, cas_odesl
	              FROM food600.sk_plt_a where uzaverka  = ip_uzaverka and substring(u_cis from 1 for 2)=lvid_kas ;
/*  text, poradi */
/* ----------------------------- VKLVY -------------------------------------------- */
INSERT INTO food600.sk__vklvy_a(
	nazev, datum, c_uzaverka, ucet, ciastka, mena, ciast_mena, autor, cisfplat, popis, fciarkod, fucis, komu, operacia)
    SELECT nazev, datum, ip_c_uzav, food600.tvor_cislo(substring(u_cis from 3)), ciastka, mena, ciast_mena, autor, cisfplat, popis, fciarkod, fucis, komu, operacia from food600.sk_vklvy_a  where uzaverka = ip_uzaverka and id_kas=lvid_kas;

/* ----------------------------- FIN -------------------------------------------- */
--UPDATE food600.sk_fin_a SET round50=-sleva_abs, cena_slev=cena_celk+round50, sleva_abs=0 WHERE ABS(sleva_abs)<0.5 AND LENGTH(BTRIM(u_cas))=14 AND POSITION(':' in u_cas)=0;
--UPDATE food600.sk_fin_a SET round50=-sleva_abs, cena_slev=cena_celk, sleva_abs=0 WHERE ABS(sleva_abs)<0.03 AND LENGTH(BTRIM(u_cas))=14 AND POSITION(':' in u_cas)>0;
OPEN fin;
LOOP 
	FETCH fin INTO lu_cis, lautor, lu_cas,  lmutace, lcas_atrib, lpocet_h, lstul, ltext_sleva, ltext_link, lducis, llim_datum, lid_limit, lround50, lpohladavka,lu_open, lid_den, lid_chod, lidslave, lsleva_abs;
		EXIT WHEN NOT FOUND; 
	IF LENGTH(BTRIM(lu_cas))=14 THEN
        IF POSITION(':' in lu_cas)>0 THEN
            /* takto to mava lubo na slovensku 20211202 20:04*/ 
    		lnew_cas = SUBSTRING(lu_cas FROM 7);
	    	IF lsleva_abs<>0 AND ABS(lsleva_abs)<0.03 THEN 
		    	lround50:=-lsleva_abs;
			    lsleva_abs:=0;
		    END IF;
        ELSE
            /* takto to ma pepino v cechach 20211202200401*/
            lnew_cas := SUBSTRING(lu_cas FROM 7 FOR 2)||' '||SUBSTRING(lu_cas FROM 9 FOR 2)||':'||SUBSTRING(lu_cas FROM 11 FOR 2);
        END IF;
	ELSE
		lnew_cas = SUBSTRING(lu_cas FROM 7);
	END IF;
	IF lnew_cas IS NULL OR lu_cas='' THEN
		lnew_cas:='01 01:01';
	END IF;
	IF lu_open IS NULL OR lu_open = '' THEN
		lu_open = food600.RIGHT(lnew_cas, 5);
	END IF; 
	lucet=food600.tvor_cislo(SUBSTRING(lu_cis FROM 3));
	lu_minut = mod((food600.tvor_cislo(SUBSTRING (lnew_cas FROM 4 fOR 2)) * 60 + food600.tvor_cislo(SUBSTRING (lnew_cas FROM 7 fOR 2))) -
		(food600.tvor_cislo(food600.left(lu_open,2)) * 60 +  food600.tvor_cislo(food600.right(lu_open,2))),24*60);

	IF lu_open = 'DAVKA' THEN
		ltext_link = ltext_sleva;
	END IF;

	IF lu_minut IS NULL THEN
		lu_minut = 0;
	END IF;
	IF lid_den IS NULL THEN
		lid_den = 0;
	END IF;
	IF lid_chod IS NULL THEN
		lid_chod = 0;
	END IF;

	INSERT INTO food600.sk__fin_a (c_uzaverka, ucet, autor, u_cas, u_minut, mutace, cas_atrib, pocet_h, stul, text_sleva, text_link, ducis, lim_datum, id_limit, round50, pohladavka,c_data, id_den, id_chod, id_slave)
		VALUES (ip_c_uzav,lucet,lautor,lnew_cas,lu_minut,lmutace,lcas_atrib,lpocet_h,lstul,ltext_sleva,ltext_link,lducis,llim_datum,lid_limit,lround50,lpohladavka,ip_c_data,lid_den,lid_chod, lidslave);
       
END LOOP; 
CLOSE fin;
/* ----------------------------- POL -------------------------------------------- */

lporadi = 0; 
lo_cis='qwqwqwd';
old_plt=0;
old_pol1=0;
old_dan1=0;
old_pol2=0;
old_dan2=0;
old_pol3=0;
old_dan3=0;
old_pol4=0;
old_dan4=0;
old_pol5=0;
old_dan5=0;
old_round50=0;
pol1=0;
dan1=-1;
pol2=0;
dan2=-1;
pol3=0;
dan3=-1;
pol4=0;
dan4=-1;
pol5=0;
dan5=-1;
lround50=0;
OPEN pol;
LOOP 
    FETCH pol INTO lid_kas, lid_man, lu_cis, lc_karty, lprachy_puv, lmnozstvi, lcen_hlad, lk_odpoctu, ldan_sazba, 
		ldotacia, lid_fmenu, lprachy_pol, lprachy_plt, lslev_koef, lround50, lprachy, ldan_pol,lc_kartyo, lsum_prachy_pol, lkompez;
		EXIT WHEN NOT FOUND; 
	lucet = food600.tvor_cislo(SUBSTRING(lu_cis FROM 3));
	IF lc_karty = 0 OR lc_karty IS NULL THEN
		lc_karty = lc_kartyo;
	END IF;
 
	SELECT c_stredisk INTO lc_stredisk FROM food600.food_dat where food_dat.id = lid_man;
	IF lc_stredisk IS NULL THEN
		lc_stredisk=0;
	END IF;
	lprachy = ROUND(CAST (lprachy_puv * lslev_koef AS NUMERIC), 2);
	ldan    = ROUND(CAST (lprachy * ldan_sazba  / (100 + ldan_sazba) AS NUMERIC), 2); 
	IF lo_cis <> lu_cis THEN  -- and lc_stredisk<>0 THEN
		lporadi= 1;
		--- Meni sa cislo uctu, takze skontrolujem, ci suhlasia sumy a dopocitam pripadnu korekciu
		IF old_plt<>(pol1+pol2+pol3+pol4+pol5+old_round50)THEN
			lrozdil:=round((old_plt-(pol1+pol2+pol3+pol4+pol5+old_round50))::numeric,2);
			olducet=food600.tvor_cislo(SUBSTRING(lo_cis FROM 3));
			CASE
				WHEN dan1>0 AND pol1<>0 THEN
					SELECT max(idriadok) INTO oldriadok FROM food600.sk__pol_a WHERE c_uzaverka=ip_c_uzav AND ucet=olducet AND dan_sazba=cast(dan1 as numeric(4,1)) AND c_data=ip_c_data;
					UPDATE food600.sk__pol_a SET prachy=prachy+lrozdil WHERE idriadok=oldriadok;
				WHEN dan2>0 AND pol2<>0 THEN
					SELECT max(idriadok) INTO oldriadok FROM food600.sk__pol_a WHERE c_uzaverka=ip_c_uzav AND ucet=olducet AND dan_sazba=cast(dan2 as numeric(4,1)) AND c_data=ip_c_data;
					UPDATE food600.sk__pol_a SET prachy=prachy+lrozdil WHERE idriadok=oldriadok;
				WHEN dan3>0 AND pol3<>0 THEN
					SELECT max(idriadok) INTO oldriadok FROM food600.sk__pol_a WHERE c_uzaverka=ip_c_uzav AND ucet=olducet AND dan_sazba=cast(dan3 as numeric(4,1)) AND c_data=ip_c_data;
					UPDATE food600.sk__pol_a SET prachy=prachy+lrozdil WHERE idriadok=oldriadok;
				WHEN dan4>0 AND pol4<>0 THEN
					SELECT max(idriadok) INTO oldriadok FROM food600.sk__pol_a WHERE c_uzaverka=ip_c_uzav AND ucet=olducet AND dan_sazba=cast(dan4 as numeric(4,1)) AND c_data=ip_c_data;
				WHEN dan5>0 AND pol5<>0 THEN
					SELECT max(idriadok) INTO oldriadok FROM food600.sk__pol_a WHERE c_uzaverka=ip_c_uzav AND ucet=olducet AND dan_sazba=cast(dan5 as numeric(4,1)) AND c_data=ip_c_data;
				WHEN pol1<>0 and lc_stredisk<>0 THEN
					SELECT max(idriadok) INTO oldriadok FROM food600.sk__pol_a WHERE c_uzaverka=ip_c_uzav AND ucet=olducet AND dan_sazba=cast(dan1 as numeric(4,1)) AND c_data=ip_c_data;
					UPDATE food600.sk__pol_a SET prachy=prachy+lrozdil WHERE idriadok=oldriadok;
				WHEN pol2<>0 and lc_stredisk<>0 THEN
					SELECT max(idriadok) INTO oldriadok FROM food600.sk__pol_a WHERE c_uzaverka=ip_c_uzav AND ucet=olducet AND dan_sazba=cast(dan2 as numeric(4,1)) AND c_data=ip_c_data;
					UPDATE food600.sk__pol_a SET prachy=prachy+lrozdil WHERE idriadok=oldriadok;
				WHEN pol3<>0 and lc_stredisk<>0 THEN
					SELECT max(idriadok) INTO oldriadok FROM food600.sk__pol_a WHERE c_uzaverka=ip_c_uzav AND olducet AND dan_sazba=cast(dan3 as numeric(4,1)) AND c_data=ip_c_data;
					UPDATE food600.sk__pol_a SET prachy=prachy+lrozdil WHERE idriadok=oldriadok;
				WHEN pol4<>0 and lc_stredisk<>0 THEN
					SELECT max(idriadok) INTO oldriadok FROM food600.sk__pol_a WHERE c_uzaverka=ip_c_uzav AND olducet AND dan_sazba=cast(dan4 as numeric(4,1)) AND c_data=ip_c_data;
					UPDATE food600.sk__pol_a SET prachy=prachy+lrozdil WHERE idriadok=oldriadok;
				WHEN pol5<>0 and lc_stredisk<>0 THEN
					SELECT max(idriadok) INTO oldriadok FROM food600.sk__pol_a WHERE c_uzaverka=ip_c_uzav AND olducet AND dan_sazba=cast(dan5 as numeric(4,1)) AND c_data=ip_c_data;
					UPDATE food600.sk__pol_a SET prachy=prachy+lrozdil WHERE idriadok=oldriadok;
				ELSE
			END CASE;
		END IF;
		old_plt=0;
		old_pol1=0;
		old_dan1=0;
		old_pol2=0;
		old_dan2=0;
		old_pol3=0;
		old_dan3=0;
		old_pol4=0;
		old_dan4=0;
		old_pol5=0;
		old_dan5=0;
		pol1=0;
		dan1=-1;
		pol2=0;
		dan2=-1;
		pol3=0;
		dan3=-1;
		pol4=0;
		dan4=-1;
		pol5=0;
		dan5=-1;
	ELSE
		lporadi=lporadi+1;
	END IF; 
	lo_cis=lu_cis;
	old_plt=lprachy_plt;
	old_round50=lround50;
	if lk_odpoctu<>''::bpchar and lkompez=0 then
		CASE
			WHEN lc_stredisk=0 THEN
			WHEN dan1>=0 AND ldan_sazba=dan1 THEN
				pol1=pol1+lprachy;
			WHEN dan2>=0 AND ldan_sazba=dan2 THEN
				pol2=pol2+lprachy;
			WHEN dan3>=0 AND ldan_sazba=dan3 THEN
				pol3=pol3+lprachy;
			WHEN dan4>=0 AND ldan_sazba=dan4 THEN
				pol4=pol4+lprachy;
			WHEN dan5>=0 AND ldan_sazba=dan5 THEN
				pol5=pol5+lprachy;
			WHEN dan1<0  THEN
				pol1=pol1+lprachy;
				dan1=ldan_sazba;
			WHEN dan2<0  THEN
				pol2=pol2+lprachy;
				dan2=ldan_sazba;
			WHEN dan3<0  THEN
				pol3=pol3+lprachy;
				dan3=ldan_sazba;
			WHEN dan4<0  THEN
				pol4=pol4+lprachy;
				dan4=ldan_sazba;
			WHEN dan5<0  THEN
				pol5=pol5+lprachy;
				dan5=ldan_sazba;
			ELSE
		END CASE;
	end if;
	IF lu_cis <> '' THEN
		lu_cis = 'ZZZZZZZ' ;  
	END IF;
	ldil_porce = 1;
	IF lc_stredisk=0 AND ABS(lprachy_pol-lprachy_plt)<0.5 THEN
		INSERT INTO food600.sk__pol_a (c_uzaverka, ucet, c_stredisk, c_karty, prachy, prachy_puv, dan, dan_sazba, mnozstvi, cen_hlad, k_odpoctu, poradi, dotacia, dil_porce, id_fmenu, kompez)
			VALUES (ip_c_uzav, lucet, lc_stredisk, lc_karty, lprachy_puv, lprachy_puv, ldan, ldan_sazba, lmnozstvi, lcen_hlad, lk_odpoctu, lporadi, ldotacia, ldil_porce, lid_fmenu, lkompez);
	ELSE
		INSERT INTO food600.sk__pol_a (c_uzaverka, ucet, c_stredisk, c_karty, prachy, prachy_puv, dan, dan_sazba, mnozstvi, cen_hlad, k_odpoctu, poradi, dotacia, dil_porce, id_fmenu, kompez)
			VALUES (ip_c_uzav, lucet, lc_stredisk, lc_karty, lprachy, lprachy_puv, ldan, ldan_sazba, lmnozstvi, lcen_hlad, lk_odpoctu, lporadi, ldotacia, ldil_porce, lid_fmenu, lkompez);
	END IF;
END LOOP; 
--- Na zaver treba skontroovat, ci netreba urobit korekciu aj pre posledny ucet
IF old_plt<>(pol1+pol2+pol3+pol4+pol5+old_round50)THEN
	lrozdil:=round((old_plt-(pol1+pol2+pol3+pol4+pol5+old_round50))::numeric,2);
	olducet=food600.tvor_cislo(SUBSTRING(lo_cis FROM 3));
	CASE
		WHEN dan1>0 AND pol1<>0 THEN
			SELECT max(idriadok) INTO oldriadok FROM food600.sk__pol_a WHERE c_uzaverka=ip_c_uzav AND ucet=olducet AND dan_sazba=cast(dan1 as numeric(4,1)) AND c_data=ip_c_data;
			UPDATE food600.sk__pol_a SET prachy=prachy+lrozdil WHERE idriadok=oldriadok;
		WHEN dan2>0 AND pol2<>0 THEN
			SELECT max(idriadok) INTO oldriadok FROM food600.sk__pol_a WHERE c_uzaverka=ip_c_uzav AND ucet=olducet AND dan_sazba=cast(dan2 as numeric(4,1)) AND c_data=ip_c_data;
			UPDATE food600.sk__pol_a SET prachy=prachy+lrozdil WHERE idriadok=oldriadok;
		WHEN dan3>0 AND pol3<>0 THEN
			SELECT max(idriadok) INTO oldriadok FROM food600.sk__pol_a WHERE c_uzaverka=ip_c_uzav AND ucet=olducet AND dan_sazba=cast(dan3 as numeric(4,1)) AND c_data=ip_c_data;
			UPDATE food600.sk__pol_a SET prachy=prachy+lrozdil WHERE idriadok=oldriadok;
		WHEN dan4>0 AND pol4<>0 THEN
			SELECT max(idriadok) INTO oldriadok FROM food600.sk__pol_a WHERE c_uzaverka=ip_c_uzav AND ucet=olducet AND dan_sazba=cast(dan4 as numeric(4,1)) AND c_data=ip_c_data;
			UPDATE food600.sk__pol_a SET prachy=prachy+lrozdil WHERE idriadok=oldriadok;
		WHEN dan5>0 AND pol5<>0 THEN
			SELECT max(idriadok) INTO oldriadok FROM food600.sk__pol_a WHERE c_uzaverka=ip_c_uzav AND ucet=olducet AND dan_sazba=cast(dan5 as numeric(4,1)) AND c_data=ip_c_data;
			UPDATE food600.sk__pol_a SET prachy=prachy+lrozdil WHERE idriadok=oldriadok;
		WHEN pol1<>0 and lc_stredisk<>0 THEN
			SELECT max(idriadok) INTO oldriadok FROM food600.sk__pol_a WHERE c_uzaverka=ip_c_uzav AND ucet=olducet AND dan_sazba=cast(dan1 as numeric(4,1)) AND c_data=ip_c_data;
			UPDATE food600.sk__pol_a SET prachy=prachy+lrozdil WHERE idriadok=oldriadok;
		WHEN pol2<>0 and lc_stredisk<>0 THEN
			SELECT max(idriadok) INTO oldriadok FROM food600.sk__pol_a WHERE c_uzaverka=ip_c_uzav AND ucet=olducet AND dan_sazba=cast(dan2 as numeric(4,1)) AND c_data=ip_c_data;
			UPDATE food600.sk__pol_a SET prachy=prachy+lrozdil WHERE idriadok=oldriadok;
		WHEN pol3<>0 and lc_stredisk<>0 THEN
			SELECT max(idriadok) INTO oldriadok FROM food600.sk__pol_a WHERE c_uzaverka=ip_c_uzav AND olducet AND dan_sazba=cast(dan3 as numeric(4,1)) AND c_data=ip_c_data;
			UPDATE food600.sk__pol_a SET prachy=prachy+lrozdil WHERE idriadok=oldriadok;
		WHEN pol4<>0 and lc_stredisk<>0 THEN
			SELECT max(idriadok) INTO oldriadok FROM food600.sk__pol_a WHERE c_uzaverka=ip_c_uzav AND olducet AND dan_sazba=cast(dan4 as numeric(4,1)) AND c_data=ip_c_data;
			UPDATE food600.sk__pol_a SET prachy=prachy+lrozdil WHERE idriadok=oldriadok;
		WHEN pol5<>0 and lc_stredisk<>0 THEN
			SELECT max(idriadok) INTO oldriadok FROM food600.sk__pol_a WHERE c_uzaverka=ip_c_uzav AND olducet AND dan_sazba=cast(dan5 as numeric(4,1)) AND c_data=ip_c_data;
			UPDATE food600.sk__pol_a SET prachy=prachy+lrozdil WHERE idriadok=oldriadok;
		ELSE
	END CASE;
END IF;

CLOSE pol;
RETURN 1;
END;
$BODY$;
