CREATE OR REPLACE FUNCTION food600.prepocet_buf()
    RETURNS trigger
    LANGUAGE 'plpgsql'
    COST 100
    VOLATILE NOT LEAKPROOF
AS $BODY$
DECLARE zapisuj INTEGER DEFAULT 0;
mc_dokl_pred INTEGER;
mc_dokl_po INTEGER;
mzustatek numeric(20,10);
mcena_prum numeric(20,10);
mporadie INTEGER;
mc_mena INTEGER;
mc_mena_old INTEGER;
mc_mena_new INTEGER;
moperace CHARACTER(2);
mpz2 CHARACTER(15);
cpz2 BIGINT DEFAULT 0;
moldzustatek numeric(20,10);
moldcena_prum numeric(20,10);
old_poradi INTEGER;
new_poradi INTEGER;
new_operace CHARACTER(2);
old_operace CHARACTER(2);
mopq CHARACTER(1);
new_opq CHARACTER(1);
old_opq CHARACTER(1);
new_idriadok INTEGER;
old_idriadok INTEGER;
new_mnozstvi numeric(20,10);
moldmnozstvi numeric(20,10);
new_cena numeric(20,10);
mprachy_pp numeric(20,10);
mprachy_pp_uz numeric(20,10);
mpocet_plusov_doteraz numeric(20,10);
mprachy_vv numeric(20,10);
buraciek BIGINT DEFAULT 0;
zaokruhlenie INTEGER DEFAULT 6;
zaok2 INTEGER DEFAULT 6;
zmenakarty SMALLINT DEFAULT 0;
buranierovnakych INTEGER DEFAULT 0;
buranierovnakych_old INTEGER DEFAULT 0;
buranierovnakych_new INTEGER DEFAULT 0;
idnazapisdoprikazov	INTEGER DEFAULT 0;
BEGIN
IF (TG_OP = 'UPDATE') OR (TG_OP = 'INSERT') THEN
	SELECT c_mena, operace, pz2 INTO mc_mena, moperace, mpz2 FROM food600.dokl_buf WHERE c_data=NEW.c_data AND c_stredisk=NEW.c_stredisk AND c_dokl=NEW.c_dokl;
	idnazapisdoprikazov:=NEW.idriadok;
	mopq:=food600.left(moperace,1);
	IF (TG_OP = 'UPDATE') THEN
		old_opq:=food600.left(OLD.operace,1);
	ELSE
		old_opq:=food600.left(moperace,1);
	END IF;
	IF zapisuj THEN
		insert into food600.test (cas,text) values (clock_timestamp(),'2U TG_OP '||TG_OP);
		insert into food600.test (cas,text) values (clock_timestamp(),'2U new.c_dokl '||new.c_dokl);
		insert into food600.test (cas,text) values (clock_timestamp(),'2U old_opq '||old_opq);
		insert into food600.test (cas,text) values (clock_timestamp(),'2U mopq '||mopq);
		insert into food600.test (cas,text) values (clock_timestamp(),'2U moperace '||moperace);
		insert into food600.test (cas,text) values (clock_timestamp(),'2U c_karty '||NEW.c_karty);
		insert into food600.test (cas,text) values (clock_timestamp(),'2U mnozstvi '||NEW.mnozstvi);
	END IF;	
	IF (mopq='P' OR mopq='V' OR old_opq='P' OR old_opq='V') THEN
		IF moperace IN ('VV','PV') THEN
			cpz2:=food600.tvor_cislo(mpz2);
			IF zapisuj THEN
				insert into food600.test (cas,text) values (clock_timestamp(),'2U pz2 '||cpz2);
				insert into food600.test (cas,text) values (clock_timestamp(),'2U mpz2 '||mpz2);
			END IF;
		END IF;
	ELSE
		RETURN NEW;
	END IF;
	IF (TG_OP = 'UPDATE') THEN
		IF (OLD.c_karty<>NEW.c_karty) OR (OLD.poradi<> NEW.poradi) OR (OLD.c_stredisk<>NEW.c_stredisk) OR (OLD.operace<>NEW.operace) OR (OLD.c_dokl<>NEW.c_dokl) THEN
			zmenakarty:=1;
		ELSE
			zmenakarty:=0;
		END IF;
	ELSE
		zmenakarty:=0;
	END IF;
	IF zmenakarty=1 THEN
		--- Povodny zaznam sa vymazal a vlozil novy v prep_1 ---
	ELSE
		IF zapisuj THEN
			insert into food600.test (cas,text) values (clock_timestamp(),'2U Rovnaka karta');
			IF old_poradi IS NULL THEN
				insert into food600.test (cas,text) values (clock_timestamp(),'2U old_poradi=NULL');
			ELSE
				insert into food600.test (cas,text) values (clock_timestamp(),'2U old_poradi '||old_poradi);
			END IF;
			IF mc_dokl_pred IS NULL THEN
				insert into food600.test (cas,text) values (clock_timestamp(),'2U mc_dokl_pred=NULL');
			ELSE
				insert into food600.test (cas,text) values (clock_timestamp(),'2U mc_dokl_pred '||mc_dokl_pred);
			END IF;
			IF moldzustatek IS NULL THEN
				insert into food600.test (cas,text) values (clock_timestamp(),'2U moldzustatek=NULL');
			ELSE
				insert into food600.test (cas,text) values (clock_timestamp(),'2U moldzustatek '||moldzustatek);
			END IF;
			IF moldcena_prum IS NULL THEN
				insert into food600.test (cas,text) values (clock_timestamp(),'2U moldcena_prum=NULL');
			ELSE
				insert into food600.test (cas,text) values (clock_timestamp(),'2U moldcena_prum '||moldcena_prum);
			END IF;
			IF (TG_OP = 'UPDATE') THEN
				insert into food600.test (cas,text) values (clock_timestamp(),'2U OLD.cena '||old.cena);
			END IF;
			insert into food600.test (cas,text) values (clock_timestamp(),'2U NEW.cena '||new.cena);
			IF (TG_OP = 'UPDATE') THEN
				insert into food600.test (cas,text) values (clock_timestamp(),'2U OLD.cena_prum '||old.cena_prum);
			END IF;
			insert into food600.test (cas,text) values (clock_timestamp(),'2U NEW.cena_prum '||new.cena_prum);
			IF (TG_OP = 'UPDATE') THEN
				insert into food600.test (cas,text) values (clock_timestamp(),'2U OLD.mnozstvi '||old.mnozstvi);
			END IF;
			insert into food600.test (cas,text) values (clock_timestamp(),'2U NEW.mnozstvi '||new.mnozstvi);
			IF (TG_OP = 'UPDATE') THEN
				insert into food600.test (cas,text) values (clock_timestamp(),'2U OLD.zustatek '||old.zustatek);
			END IF;
			insert into food600.test (cas,text) values (clock_timestamp(),'2U NEW.zustatek '||new.zustatek);
		END IF;
		IF (TG_OP = 'UPDATE') THEN
			IF OLD.cena<>NEW.cena OR OLD.mnozstvi<>NEW.mnozstvi OR OLD.cena_prum<>NEW.cena_prum OR OLD.zustatek<>NEW.zustatek THEN
				zmenakarty:=1;
			ELSE
				zmenakarty:=0;
			END IF;
		ELSE
			zmenakarty:=1;
		END IF;
		IF zmenakarty=1 THEN
			-- zmenili sa podstatne udaje, musim vypocitat nove cena_prum a zustatek a zistit, ci po tomto riadku este nieco na danej karte a stredisku nasleduje
			IF zapisuj THEN
				insert into food600.test (cas,text) values (clock_timestamp(),'2U Zmena');
			END IF;
			mcena_prum:=NEW.cena_prum;
			mzustatek:=NEW.zustatek;
			IF zapisuj THEN
				insert into food600.test (cas,text) values (clock_timestamp(),'2U Operace '||moperace);
				insert into food600.test (cas,text) values (clock_timestamp(),'2U Vypocitany zustatek '||mzustatek);
				insert into food600.test (cas,text) values (clock_timestamp(),'2U Vypocitana cena_prum '||mcena_prum);
				insert into food600.test (cas,text) values (clock_timestamp(),'2U NEW.mnozstvi '||NEW.mnozstvi);
			END IF;
			IF TG_OP = 'UPDATE' AND NEW.operace='PB' AND NEW.mnozstvi<0 THEN
				IF zapisuj THEN
					insert into food600.test (cas,text) values (clock_timestamp(),'2U Buranie minus');
				END IF;
				SELECT idriadok INTO old_idriadok FROM food600.obra_buf WHERE c_data=NEW.c_data AND c_stredisk=NEW.c_stredisk AND c_dokl=NEW.c_dokl AND c_karty=NEW.c_karty AND poradi=NEW.poradi AND mnozstvi>=0;
				IF old_idriadok IS NULL THEN
				ELSE
					buranierovnakych:=1;
					INSERT INTO food600.prikazy_prepocet (prikaz, riadok) VALUES ('UPDATE food600.obra_buf SET zustatek='||NEW.zustatek||'+mnozstvi WHERE idriadok='||old_idriadok, idnazapisdoprikazov);
				END IF;
				mprachy_vv:=-NEW.mnozstvi*NEW.cena;
				mprachy_pp:=0.0000;
				SELECT SUM(mnozstvi*cena) INTO mprachy_pp FROM food600.obra_buf WHERE c_data=NEW.c_data AND c_stredisk=NEW.c_stredisk AND c_dokl=NEW.c_dokl AND poradi=NEW.poradi AND mnozstvi>=0 LIMIT 1;
				IF ROUND(CAST(mprachy_pp AS DECIMAL(20,10)),zaok2)<>ROUND(CAST(mprachy_vv AS DECIMAL(20,10)),zaok2) THEN
					SELECT COUNT(*) INTO buraciek FROM food600.obra_buf WHERE c_data=NEW.c_data AND c_stredisk=NEW.c_stredisk AND c_dokl=NEW.c_dokl AND poradi=NEW.poradi AND mnozstvi>=0 LIMIT 1;
					IF buraciek=1 THEN 
						SELECT INTO new_cena, new_mnozstvi, new_idriadok cena, mnozstvi, idriadok FROM food600.obra_buf WHERE c_data=NEW.c_data AND c_stredisk=NEW.c_stredisk AND c_dokl=NEW.c_dokl AND poradi=NEW.poradi AND mnozstvi>=0;
						IF new_mnozstvi<>0 THEN
							IF new_cena<>ROUND(CAST(mprachy_vv/new_mnozstvi AS DECIMAL(20,10)),zaokruhlenie) THEN
								INSERT INTO food600.prikazy_prepocet (prikaz, riadok) VALUES ('UPDATE food600.obra_buf SET cena=ROUND(CAST('||mprachy_vv/new_mnozstvi||' AS DECIMAL(20,10)),'||zaokruhlenie||') WHERE idriadok='||new_idriadok,idnazapisdoprikazov);
							END IF;
						ELSE
							--- nulove mnozstvi by nemalo nastat
						END IF;
					ELSE
						IF mprachy_pp<>0 THEN
							INSERT INTO food600.prikazy_prepocet (prikaz, riadok) VALUES ('UPDATE food600.obra_buf SET cena=ROUND(CAST(cena*'||mprachy_vv/mprachy_pp||' AS DECIMAL(20,10)),'||zaokruhlenie||') WHERE c_data='||NEW.c_data||' AND c_stredisk='||NEW.c_stredisk||' AND c_dokl='||NEW.c_dokl||' AND poradi='||NEW.poradi||' AND mnozstvi>=0',idnazapisdoprikazov);
						ELSE
							INSERT INTO food600.prikazy_prepocet (prikaz, riadok) VALUES ('UPDATE food600.obra_buf SET cena=ROUND(CAST(cena*'||mprachy_vv||'/mnozstvi AS DECIMAL(20,10)),'||zaokruhlenie||') WHERE c_data='||NEW.c_data||' AND c_stredisk='||NEW.c_stredisk||' AND c_dokl='||NEW.c_dokl||' AND poradi='||NEW.poradi||' AND mnozstvi>=0',idnazapisdoprikazov);
							SELECT SUM(mnozstvi*cena) INTO mprachy_pp FROM food600.obra_buf WHERE c_data=NEW.c_data AND c_stredisk=NEW.c_stredisk AND c_dokl=NEW.c_dokl AND poradi=NEW.poradi AND mnozstvi>=0 LIMIT 1;
							IF mprachy_pp<>0 THEN
								INSERT INTO food600.prikazy_prepocet (prikaz, riadok) VALUES ('UPDATE food600.obra_buf SET cena=ROUND(CAST(cena*'||mprachy_vv/mprachy_pp||' AS DECIMAL(20,10)),'||zaokruhlenie||') WHERE c_data='||NEW.c_data||' AND c_stredisk='||NEW.c_stredisk||' AND c_dokl='||NEW.c_dokl||' AND poradi='||NEW.poradi||' AND mnozstvi>=0',idnazapisdoprikazov);
							END IF;
						END IF;
					END IF;
				END IF;
			END IF;
			IF NEW.operace='VV' AND NEW.mnozstvi<0 AND cpz2<>0 THEN
				IF zapisuj THEN
					insert into food600.test (cas,text) values (clock_timestamp(),'2U Vydajka');
				END IF;
				mprachy_vv:=-NEW.mnozstvi*NEW.cena;
				mprachy_pp:=0.0000;
				SELECT SUM(mnozstvi*cena) INTO mprachy_pp FROM food600.obra_buf WHERE c_data=NEW.c_data AND c_dokl=cpz2 AND poradi=NEW.poradi LIMIT 1;
				IF zapisuj THEN
					insert into food600.test (cas,text) values (clock_timestamp(),'2U cena_vv '||mprachy_vv);
					insert into food600.test (cas,text) values (clock_timestamp(),'2U cena_pp '||mprachy_pp);
				END IF;
				IF ROUND(CAST(mprachy_pp AS DECIMAL(20,10)),zaok2)<>ROUND(CAST(mprachy_vv AS DECIMAL(20,10)),zaok2) THEN
					SELECT COUNT(*) INTO buraciek FROM food600.obra_buf WHERE c_data=NEW.c_data AND c_dokl=cpz2 AND poradi=NEW.poradi LIMIT 1;
					IF zapisuj THEN
						insert into food600.test (cas,text) values (clock_timestamp(),'2U cpz2 '||cpz2);
						insert into food600.test (cas,text) values (clock_timestamp(),'2U new.poradi '||new.poradi);
						insert into food600.test (cas,text) values (clock_timestamp(),'2U pocet buraciek '||buraciek);
					END IF;
					IF buraciek=1 THEN 
						SELECT INTO new_cena, new_mnozstvi, new_idriadok cena, mnozstvi, idriadok FROM food600.obra_buf WHERE c_data=NEW.c_data AND c_dokl=cpz2 AND poradi=NEW.poradi;
						IF zapisuj THEN
							insert into food600.test (cas,text) values (clock_timestamp(),'2U new_cena '||new_cena);
							insert into food600.test (cas,text) values (clock_timestamp(),'2U new_mnozstvi '||new_mnozstvi);
							insert into food600.test (cas,text) values (clock_timestamp(),'2U new_idriadok '||new_idriadok);
						END IF;
						IF new_mnozstvi<>0 THEN
							IF new_cena<>ROUND(CAST(mprachy_vv/new_mnozstvi AS DECIMAL(20,10)),zaokruhlenie) THEN
								IF zapisuj THEN
									insert into food600.test (cas,text) values (clock_timestamp(),'2U vypocitana cena prijmu '||ROUND(CAST(mprachy_vv/new_mnozstvi AS DECIMAL(20,10)),zaokruhlenie));
								END IF;
								INSERT INTO food600.prikazy_prepocet (prikaz, riadok) VALUES ('UPDATE food600.obra_buf SET cena_part=-123456789, cena=ROUND(CAST('||mprachy_vv/new_mnozstvi||' AS DECIMAL(20,10)),'||zaokruhlenie||') WHERE idriadok='||new_idriadok,idnazapisdoprikazov);
							END IF;
						ELSE
							IF TG_OP='UPDATE' THEN
								--- pokus o znulovanie mnozstva na takomto prijme musim vratit nazad, lebo netusim, ako mam vypocitat cenu
								INSERT INTO food600.prikazy_prepocet (prikaz, riadok) VALUES ('UPDATE food600.obra_buf SET mnozstvi='||OLD.mnozstvi||') WHERE idriadok='||new_idriadok,idnazapisdoprikazov);
							END IF;
						END IF;
					ELSE
						IF mprachy_pp<>0 THEN
							INSERT INTO food600.prikazy_prepocet (prikaz, riadok) VALUES ('UPDATE food600.obra_buf SET cena_part=-123456789, cena=ROUND(CAST(cena*'||mprachy_vv/mprachy_pp||' AS DECIMAL(20,10)),'||zaokruhlenie||') WHERE c_data='||NEW.c_data||' AND c_dokl='||cpz2||' AND poradi='||NEW.poradi,idnazapisdoprikazov);
						ELSE
							INSERT INTO food600.prikazy_prepocet (prikaz, riadok) VALUES ('UPDATE food600.obra_buf SET cena_part=-123456789, cena=ROUND(CAST('||mprachy_vv||'/mnozstvi AS DECIMAL(20,10)),'||zaokruhlenie||') WHERE c_data='||NEW.c_data||' AND c_dokl='||cpz2||' AND poradi='||NEW.poradi,idnazapisdoprikazov);
						END IF;
					END IF;
				END IF;
			END IF;
			IF NEW.operace='VP' AND NEW.mnozstvi<0 THEN
				IF zapisuj THEN
					insert into food600.test (cas,text) values (clock_timestamp(),'2U Vydaj polotovarov');
				END IF;
				SELECT SUM(-mnozstvi*cena) INTO mprachy_vv FROM food600.obra_buf WHERE c_data=NEW.c_data AND c_dokl=NEW.c_dokl AND poradi=NEW.poradi AND c_stredisk=NEW.c_stredisk AND operace='VP' LIMIT 1;
				mprachy_pp:=0.0000;
				SELECT SUM(mnozstvi*cena) INTO mprachy_pp FROM food600.obra_buf WHERE c_data=NEW.c_data AND c_stredisk=NEW.c_stredisk AND c_dokl=NEW.c_dokl AND poradi=NEW.poradi AND operace='PP' LIMIT 1;
				IF ROUND(CAST(mprachy_pp AS DECIMAL(20,10)),zaok2)<>ROUND(CAST(mprachy_vv AS DECIMAL(20,10)),zaok2) THEN
					SELECT INTO new_cena, new_mnozstvi, new_idriadok cena, mnozstvi, idriadok FROM food600.obra_buf WHERE c_data=NEW.c_data AND c_stredisk=NEW.c_stredisk AND c_dokl=NEW.c_dokl AND poradi=NEW.poradi AND operace='PP';
					IF new_mnozstvi<>0 THEN
						IF new_cena<>ROUND(CAST(mprachy_vv/new_mnozstvi AS DECIMAL(20,10)),zaokruhlenie) THEN
							INSERT INTO food600.prikazy_prepocet (prikaz, riadok) VALUES ('UPDATE food600.obra_buf SET cena=ROUND(CAST('||mprachy_vv/new_mnozstvi||' AS DECIMAL(20,10)),'||zaokruhlenie||') WHERE idriadok='||new_idriadok,idnazapisdoprikazov);
						END IF;
					ELSE
						IF TG_OP='UPDATE' THEN
							--- pokus o znulovanie mnozstva na takzchto polotovaroch musim vratit nazad, lebo netusim, ako mam vypocitat cenu
							INSERT INTO food600.prikazy_prepocet (prikaz, riadok) VALUES ('UPDATE food600.obra_buf SET mnozstvi='||OLD.mnozstvi||') WHERE idriadok='||NEW.idriadok,idnazapisdoprikazov);
						END IF;
					END IF;
				END IF;
				SELECT idriadok INTO old_idriadok FROM food600.obra_buf WHERE c_data=NEW.c_data AND c_stredisk=NEW.c_stredisk AND c_dokl=NEW.c_dokl AND c_karty=NEW.c_karty AND poradi=NEW.poradi AND mnozstvi>=0;
				IF old_idriadok IS NULL THEN
				ELSE
					buranierovnakych:=1;
					INSERT INTO food600.prikazy_prepocet (prikaz, riadok) VALUES ('UPDATE food600.obra_buf SET zustatek=zustatek+'||NEW.zustatek||' WHERE idriadok='||old_idriadok,idnazapisdoprikazov);
				END IF;
			END IF;
			IF NEW.operace='PP' AND NEW.mnozstvi>0 THEN
				IF zapisuj THEN
					insert into food600.test (cas,text) values (clock_timestamp(),'2U Prijem polotovarov');
				END IF;
				SELECT SUM(-mnozstvi*cena) INTO mprachy_vv FROM food600.obra_buf WHERE c_data=NEW.c_data AND c_dokl=NEW.c_dokl AND poradi=NEW.poradi AND c_stredisk=NEW.c_stredisk AND operace='VP' LIMIT 1;
				mprachy_pp:=NEW.mnozstvi*NEW.cena;
				IF zapisuj THEN
					insert into food600.test (cas,text) values (clock_timestamp(),'2U Suma vydaj'||mprachy_vv);
					insert into food600.test (cas,text) values (clock_timestamp(),'2U Suma prijem'||mprachy_pp);
				END IF;
				IF ROUND(CAST(mprachy_pp AS DECIMAL(20,10)),zaok2)<>ROUND(CAST(mprachy_vv AS DECIMAL(20,10)),zaok2) THEN
					IF NEW.mnozstvi<>0 THEN
						IF NEW.cena<>ROUND(CAST(mprachy_vv/NEW.mnozstvi AS DECIMAL(20,10)),zaokruhlenie) THEN
							INSERT INTO food600.prikazy_prepocet (prikaz, riadok) VALUES ('UPDATE food600.obra_buf SET cena=ROUND(CAST('||mprachy_vv/NEW.mnozstvi||' AS DECIMAL(20,10)),'||zaokruhlenie||') WHERE idriadok='||NEW.idriadok,idnazapisdoprikazov);
						END IF;
					ELSE
						IF TG_OP='UPDATE' THEN
							--- pokus o znulovanie mnozstva na vyrobenych polotovaroch musim vratit nazad, lebo netusim, ako mam vypocitat cenu
							INSERT INTO food600.prikazy_prepocet (prikaz, riadok) VALUES ('UPDATE food600.obra_buf SET mnozstvi='||OLD.mnozstvi||') WHERE idriadok='||NEW.idriadok,idnazapisdoprikazov);
						END IF;
					END IF;
					IF zapisuj THEN
						insert into food600.test (cas,text) values (clock_timestamp(),'2U Nova cena prijmu'||NEW.cena);
					END IF;
				END IF;
			END IF;
			IF TG_OP='UPDATE' AND NEW.operace='PB' AND NEW.mnozstvi>0 THEN
				IF zapisuj THEN
					insert into food600.test (cas,text) values (clock_timestamp(),'2U Prijem buranim');
				END IF;
				SELECT SUM(-mnozstvi*cena) INTO mprachy_vv FROM food600.obra_buf WHERE c_data=NEW.c_data AND c_dokl=NEW.c_dokl AND poradi=NEW.poradi AND c_stredisk=NEW.c_stredisk AND operace='PB' AND mnozstvi<0 LIMIT 1;
				mprachy_pp:=NEW.mnozstvi*NEW.cena;
				IF zapisuj THEN
					insert into food600.test (cas,text) values (clock_timestamp(),'2U Suma vydaj'||mprachy_vv);
					insert into food600.test (cas,text) values (clock_timestamp(),'2U Suma prijem'||mprachy_pp);
				END IF;
				IF ROUND(CAST(mprachy_pp AS DECIMAL(20,10)),zaok2)<>ROUND(CAST(mprachy_vv AS DECIMAL(20,10)),zaok2) THEN
					SELECT COUNT(*) INTO buraciek FROM food600.obra_buf WHERE c_data=NEW.c_data AND c_stredisk=NEW.c_stredisk AND c_dokl=NEW.c_dokl AND poradi=NEW.poradi AND mnozstvi>=0 LIMIT 1;
					IF buraciek=1 THEN 
						IF NEW.mnozstvi<>0 THEN
							IF NEW.cena<>ROUND(CAST(mprachy_vv/NEW.mnozstvi AS DECIMAL(20,10)),zaokruhlenie) THEN
								INSERT INTO food600.prikazy_prepocet (prikaz, riadok) VALUES ('UPDATE food600.obra_buf SET cena=ROUND(CAST('||mprachy_vv/NEW.mnozstvi||' AS DECIMAL(20,10)),'||zaokruhlenie||') WHERE idriadok='||NEW.idriadok,idnazapisdoprikazov);
							END IF;
						ELSE
							--- pokus o znulovanie mnozstva na takomto burani musim vratit nazad, lebo netusim, ako mam vypocitat cenu
							INSERT INTO food600.prikazy_prepocet (prikaz, riadok) VALUES ('UPDATE food600.obra_buf SET mnozstvi='||OLD.mnozstvi||') WHERE idriadok='||NEW.idriadok,idnazapisdoprikazov);
						END IF;
						IF zapisuj THEN
							insert into food600.test (cas,text) values (clock_timestamp(),'2U Nova cena prijmu'||NEW.cena);
						END IF;
					ELSE
						IF NEW.mnozstvi<>0 THEN
							SELECT SUM(mnozstvi*cena) INTO mprachy_pp FROM food600.obra_buf WHERE c_data=NEW.c_data AND c_dokl=NEW.c_dokl AND poradi=NEW.poradi AND c_stredisk=NEW.c_stredisk AND operace='PB' AND mnozstvi>0 AND idriadok<>NEW.idriadok LIMIT 1;
							--- ak sa buralo na viac riadkov a mnozstvo nie je nula, staci mi prepocitat cenu na tomto riadku
							INSERT INTO food600.prikazy_prepocet (prikaz, riadok) VALUES ('UPDATE food600.obra_buf SET cena=ROUND(CAST(('||mprachy_vv-mprachy_pp||')/'||NEW.mnozstvi||' AS DECIMAL(20,10)),'||zaokruhlenie||') WHERE idriadok='||NEW.idriadok,idnazapisdoprikazov);
						ELSE
							--- pokus o znulovanie mnozstva na takomto burani musim vratit nazad, lebo netusim, ako mam rozpocitat ceny na ostatnych riadkoch
							INSERT INTO food600.prikazy_prepocet (prikaz, riadok) VALUES ('UPDATE food600.obra_buf SET mnozstvi='||OLD.mnozstvi||') WHERE idriadok='||NEW.idriadok,idnazapisdoprikazov);
						END IF;
					END IF;
				END IF;
			END IF;
			IF NEW.operace='PV' THEN
				IF zapisuj THEN
					insert into food600.test (cas,text) values (clock_timestamp(),'2U Prijem interny');
				END IF;
				SELECT SUM(-mnozstvi*cena) INTO mprachy_vv FROM food600.obra_buf WHERE c_data=NEW.c_data AND c_dokl=cpz2 AND poradi=NEW.poradi LIMIT 1;
				mprachy_pp:=NEW.mnozstvi*NEW.cena;
				IF zapisuj THEN
					insert into food600.test (cas,text) values (clock_timestamp(),'2U Suma vydaj'||mprachy_vv);
					insert into food600.test (cas,text) values (clock_timestamp(),'2U Suma prijem'||mprachy_pp);
				END IF;
				IF ROUND(CAST(mprachy_pp AS DECIMAL(20,10)),zaok2)<>ROUND(CAST(mprachy_vv AS DECIMAL(20,10)),zaok2) THEN
					SELECT COUNT(*) INTO buraciek FROM food600.obra_buf WHERE c_data=NEW.c_data AND c_stredisk=NEW.c_stredisk AND c_dokl=NEW.c_dokl AND poradi=NEW.poradi LIMIT 1;
					IF buraciek=1 THEN 
						IF NEW.mnozstvi<>0 THEN
							IF NEW.cena<>ROUND(CAST(mprachy_vv/NEW.mnozstvi AS DECIMAL(20,10)),zaokruhlenie) THEN
								INSERT INTO food600.prikazy_prepocet (prikaz, riadok) VALUES ('UPDATE food600.obra_buf SET cena=ROUND(CAST('||mprachy_vv/NEW.mnozstvi||' AS DECIMAL(20,10)),'||zaokruhlenie||') WHERE idriadok='||NEW.idriadok,idnazapisdoprikazov);
							END IF;
						ELSE
							IF TG_OP='UPDATE' THEN
								--- pokus o znulovanie mnozstva na internom prijme musim vratit nazad, lebo netusim, ako mam rozpocitat cenu
								INSERT INTO food600.prikazy_prepocet (prikaz, riadok) VALUES ('UPDATE food600.obra_buf SET mnozstvi='||OLD.mnozstvi||') WHERE idriadok='||NEW.idriadok,idnazapisdoprikazov);
							END IF;
						END IF;
						IF zapisuj THEN
							insert into food600.test (cas,text) values (clock_timestamp(),'2U Nova cena prijmu'||NEW.cena);
						END IF;
					ELSE
						IF NEW.mnozstvi<>0 THEN
							SELECT SUM(mnozstvi*cena) INTO mprachy_pp FROM food600.obra_buf WHERE c_data=NEW.c_data AND c_dokl=NEW.c_dokl AND poradi=NEW.poradi AND c_stredisk=NEW.c_stredisk AND operace='PV' AND mnozstvi>0 AND idriadok<>NEW.idriadok LIMIT 1;
							--- ak sa buralo na viac riadkov a mnozstvo nie je nula, staci mi prepocitat cenu na tomto riadku
							INSERT INTO food600.prikazy_prepocet (prikaz, riadok) VALUES ('UPDATE food600.obra_buf SET cena=ROUND(CAST(('||mprachy_vv-mprachy_pp||')/'||NEW.mnozstvi||' AS DECIMAL(20,10)),'||zaokruhlenie||') WHERE idriadok='||NEW.idriadok,idnazapisdoprikazov);
						ELSE
							--- pokus o znulovanie mnozstva na internom prijme musim vratit nazad, lebo netusim, ako mam rozpocitat ceny na ostatnych riadkoch
							INSERT INTO food600.prikazy_prepocet (prikaz, riadok) VALUES ('UPDATE food600.obra_buf SET mnozstvi='||OLD.mnozstvi||') WHERE idriadok='||NEW.idriadok,idnazapisdoprikazov);
						END IF;
					END IF;
				END IF;
			END IF;
			IF NEW.operace='PC' THEN
				IF NEW.mnozstvi<0 THEN
					IF zapisuj THEN
						insert into food600.test (cas,text) values (clock_timestamp(),'2U Precenenie minus');
					END IF;
					INSERT INTO food600.prikazy_prepocet (prikaz, riadok) VALUES ('UPDATE food600.obra_buf SET mnozstvi=-1*'||NEW.mnozstvi||' WHERE c_data='||NEW.c_data||' AND c_stredisk='||NEW.c_stredisk||' AND c_dokl='||NEW.c_dokl||' AND c_karty='||NEW.c_karty||' AND poradi='||NEW.poradi||' AND mnozstvi>0',idnazapisdoprikazov);
				ELSE
					IF zapisuj THEN
						insert into food600.test (cas,text) values (clock_timestamp(),'2U Precenenie plus');
					END IF;
					INSERT INTO food600.prikazy_prepocet (prikaz, riadok) VALUES ('UPDATE food600.obra_buf SET mnozstvi=-1*'||NEW.mnozstvi||' WHERE c_data='||NEW.c_data||' AND c_stredisk='||NEW.c_stredisk||' AND c_dokl='||NEW.c_dokl||' AND c_karty='||NEW.c_karty||' AND poradi='||NEW.poradi||' AND mnozstvi<=0',idnazapisdoprikazov);
				END IF;
			END IF;
			IF buranierovnakych=1 THEN
				IF zapisuj THEN
					insert into food600.test (cas,text) values (clock_timestamp(),'2U Nezapisujem do skl_bufu - buranie rovnakych');
				END IF;
			ELSE
				SELECT MIN(poradi) INTO new_poradi FROM food600.obra_buf WHERE  c_data=NEW.c_data AND c_stredisk=NEW.c_stredisk AND c_dokl=NEW.c_dokl AND c_karty=NEW.c_karty AND poradi>NEW.poradi LIMIT 1;
				IF zapisuj THEN
					IF new_poradi IS NULL THEN
						insert into food600.test (cas,text) values (clock_timestamp(),'2U new_poradi=NULL');
					ELSE
						insert into food600.test (cas,text) values (clock_timestamp(),'2U new_poradi'||new_poradi);
					END IF;
				END IF;
				IF new_poradi IS NULL THEN
					-- na tomto doklade sa uz dana karta nevyskytuje. Musim pohladat najblizsi doklad, na ktorom sa vyskytuje
					SELECT MIN(c_dokl) INTO mc_dokl_po FROM food600.obra_buf WHERE c_data=NEW.c_data AND c_stredisk=NEW.c_stredisk AND c_dokl>NEW.c_dokl AND c_karty=NEW.c_karty AND (SUBSTR(operace,1, 1)='V' OR SUBSTR(operace,1, 1)='P') LIMIT 1;
					IF zapisuj THEN
						IF mc_dokl_po IS NULL THEN
							insert into food600.test (cas,text) values (clock_timestamp(),'2U mc_dokl_po=NULL');
						ELSE
							insert into food600.test (cas,text) values (clock_timestamp(),'2U mc_dokl_po '||mc_dokl_po);
						END IF;
					END IF;
					IF mc_dokl_po IS NULL THEN
						-- na tomto stredisku sa dana karta uz nevyskytuje. Musim uz len urobit zapis do skl_bufu
						IF moperace='VP' AND buranierovnakych=1 THEN
							IF zapisuj THEN
								insert into food600.test (cas,text) values (clock_timestamp(),'2U Nezapisujem do skl_bufu PP buranie rovnakych');
							END IF;
						ELSE
							IF zapisuj THEN
								insert into food600.test (cas,text) values (clock_timestamp(),'2U Zapis do skl_bufu: c_karty:'||NEW.c_karty||',cena:'||mcena_prum||',zustatek:'||mzustatek);
							END IF;
							SELECT idriadok INTO new_idriadok FROM food600.skl_buf WHERE c_data=NEW.c_data AND c_stredisk=NEW.c_stredisk AND c_karty=NEW.c_karty;
							IF new_idriadok IS NULL THEN
								IF zapisuj THEN
									insert into food600.test (cas,text) values (clock_timestamp(),'2U Zapis do skl_bufu: novy riadok');
								END IF;
								INSERT INTO food600.skl_buf (c_data, c_stredisk, c_karty, zustatek, cena_prum, c_mena) VALUES (NEW.c_data, NEW.c_stredisk, NEW.c_karty, mzustatek, mcena_prum, mc_mena);
							ELSE
								UPDATE food600.skl_buf SET cena_prum=mcena_prum, zustatek=mzustatek, c_mena=mc_mena WHERE c_data=NEW.c_data AND c_stredisk=NEW.c_stredisk AND c_karty=NEW.c_karty;
								IF zapisuj THEN
									insert into food600.test (cas,text) values (clock_timestamp(),'2U Zapis do skl_bufu: povodny riadok');
								END IF;
							END IF;
						END IF;
					ELSE
						-- este sa karta na nejakom doklade vyskytuje. Najdem min poradi pre tuto kartu, menu dokladu a operaciu dokladu. Prepocitam co treba a urobim zapis
						SELECT operace, c_mena INTO new_operace, mc_mena_new FROM food600.dokl_buf WHERE c_data=NEW.c_data  AND c_stredisk=NEW.c_stredisk AND c_dokl=mc_dokl_po;
						new_opq:=food600.left(new_operace,1);
						SELECT MIN(poradi) INTO new_poradi FROM food600.obra_buf WHERE c_data=NEW.c_data AND c_stredisk=NEW.c_stredisk AND c_karty=NEW.c_karty AND c_dokl=mc_dokl_po;
						IF zapisuj THEN
							insert into food600.test (cas,text) values (clock_timestamp(),'2U new_operace '||new_operace);
							insert into food600.test (cas,text) values (clock_timestamp(),'2U new_poradi '||new_poradi);
						END IF;
						-- nasleduje uprava nasledjuceho riadku podla typu operacie
						IF mc_mena<>mc_mena_new THEN
							mcena_prum:=ROUND(CAST(food600.prep_meny(mc_mena,mc_mena_new,mcena_prum) AS DECIMAL(20,10)),zaokruhlenie);
							mc_mena:=mc_mena_new;
						END IF;
						CASE
							WHEN new_operace='PI' THEN
								INSERT INTO food600.prikazy_prepocet (prikaz, riadok) VALUES ('UPDATE food600.obra_buf SET cena='||mcena_prum||', mnozstvi=ROUND(CAST('||mzustatek||'-zustatek AS DECIMAL(20,10)),'||zaokruhlenie||') WHERE c_data='||NEW.c_data||' AND c_stredisk='||NEW.c_stredisk||' AND c_dokl='||mc_dokl_po||' AND poradi='||new_poradi,idnazapisdoprikazov);
							WHEN new_operace='PC' THEN
								INSERT INTO food600.prikazy_prepocet (prikaz, riadok) VALUES ('UPDATE food600.obra_buf SET cena='||mcena_prum||', cena_prum='||mcena_prum||', mnozstvi=ROUND(CAST(-1*'||mzustatek||' AS DECIMAL(20,10)),'||zaokruhlenie||'), zustatek=0 WHERE c_data='||NEW.c_data||' AND c_stredisk='||NEW.c_stredisk||' AND c_dokl='||mc_dokl_po||' AND poradi='||new_poradi||' AND mnozstvi<0',idnazapisdoprikazov);
							WHEN new_operace='PB' THEN
								SELECT idriadok INTO old_idriadok FROM food600.obra_buf WHERE  c_stredisk=NEW.c_stredisk AND c_dokl=mc_dokl_po AND c_karty=NEW.c_karty AND c_data=NEW.c_data AND poradi=new_poradi AND mnozstvi<0;
								IF old_idriadok IS NULL THEN
									-- nasleduci pohyb je plusove buranie
									SELECT mnozstvi INTO moldmnozstvi FROM food600.obra_buf WHERE c_data=NEW.c_data AND c_stredisk=NEW.c_stredisk AND c_karty=NEW.c_karty AND c_dokl=mc_dokl_po AND poradi=new_poradi;
									IF moldmnozstvi+mzustatek=0 THEN
										INSERT INTO food600.prikazy_prepocet (prikaz, riadok) VALUES ('UPDATE food600.obra_buf SET zustatek=ROUND(CAST(mnozstvi+'||mzustatek||' AS DECIMAL(20,10)),'||zaokruhlenie||') WHERE c_data='||NEW.c_data||' AND c_stredisk='||NEW.c_stredisk||' AND c_dokl='||mc_dokl_po||' AND poradi='||new_poradi||' AND mnozstvi>=0',idnazapisdoprikazov);
									ELSE
										INSERT INTO food600.prikazy_prepocet (prikaz, riadok) VALUES ('UPDATE food600.obra_buf SET zustatek=ROUND(CAST(mnozstvi+'||mzustatek||' AS DECIMAL(20,10)),'||zaokruhlenie||'), cena_prum=ROUND(CAST((mnozstvi*cena+'||mzustatek*mcena_prum||')/(mnozstvi+'||mzustatek||') AS DECIMAL(20,10)), '||zaokruhlenie||') WHERE c_data='||NEW.c_data||' AND c_stredisk='||NEW.c_stredisk||' AND c_dokl='||mc_dokl_po||' AND poradi='||new_poradi||' AND mnozstvi>=0',idnazapisdoprikazov);
									END IF;
								ELSE 
									-- nasleduci pohyb je minusove buranie
									INSERT INTO food600.prikazy_prepocet (prikaz, riadok) VALUES ('UPDATE food600.obra_buf SET zustatek=ROUND(CAST(mnozstvi+'||mzustatek||' AS DECIMAL(20,10)),'||zaokruhlenie||'), cena_prum='||mcena_prum||' WHERE c_data='||NEW.c_data||' AND c_stredisk='||NEW.c_stredisk||' AND c_dokl='||mc_dokl_po||' AND poradi='||new_poradi||' AND mnozstvi<0',idnazapisdoprikazov);
								END IF;
							WHEN new_operace='VP' OR new_operace='PP' THEN
								SELECT idriadok INTO old_idriadok FROM food600.obra_buf WHERE  c_stredisk=NEW.c_stredisk AND c_dokl=mc_dokl_po AND c_karty=NEW.c_karty AND c_data=NEW.c_data AND poradi=new_poradi AND mnozstvi<0;
								IF old_idriadok IS NULL THEN
									-- nasleduci pohyb su plusove polotovary
									SELECT mnozstvi INTO moldmnozstvi FROM food600.obra_buf WHERE c_data=NEW.c_data AND c_stredisk=NEW.c_stredisk AND c_karty=NEW.c_karty AND c_dokl=mc_dokl_po AND poradi=new_poradi;
									IF moldmnozstvi+mzustatek=0 THEN
										INSERT INTO food600.prikazy_prepocet (prikaz, riadok) VALUES ('UPDATE food600.obra_buf SET zustatek=ROUND(CAST(mnozstvi+'||mzustatek||' AS DECIMAL(20,10)),'||zaokruhlenie||') WHERE c_data='||NEW.c_data||' AND c_stredisk='||NEW.c_stredisk||' AND c_dokl='||mc_dokl_po||' AND poradi='||new_poradi||' AND mnozstvi>=0',idnazapisdoprikazov);
									ELSE
										INSERT INTO food600.prikazy_prepocet (prikaz, riadok) VALUES ('UPDATE food600.obra_buf SET zustatek=ROUND(CAST(mnozstvi+'||mzustatek||' AS DECIMAL(20,10)),'||zaokruhlenie||'), cena_prum=ROUND(CAST((mnozstvi*cena+'||mzustatek*mcena_prum||')/(mnozstvi+'||mzustatek||') AS DECIMAL(20,10)), '||zaokruhlenie||') WHERE c_data='||NEW.c_data||' AND c_stredisk='||NEW.c_stredisk||' AND c_dokl='||mc_dokl_po||' AND poradi='||new_poradi||' AND mnozstvi>=0',idnazapisdoprikazov);
									END IF;
								ELSE 
									-- nasleduci pohyb su minusove polotovary
									INSERT INTO food600.prikazy_prepocet (prikaz, riadok) VALUES ('UPDATE food600.obr_buf SET zustatek=ROUND(CAST(mnozstvi+'||mzustatek||' AS DECIMAL(20,10)),'||zaokruhlenie||'), cena_prum='||mcena_prum||', cena='||mcena_prum||' WHERE c_data='||NEW.c_data||' AND c_stredisk='||NEW.c_stredisk||' AND c_dokl='||mc_dokl_po||' AND poradi='||new_poradi||' AND c_karty='||NEW.c_karty||' AND mnozstvi<0',idnazapisdoprikazov);
--									UPDATE food600.obra_buf SET zustatek=ROUND(CAST(mnozstvi+mzustatek AS DECIMAL(20,10)), zaokruhlenie), cena_prum=mcena_prum, cena=mcena_prum WHERE c_stredisk=NEW.c_stredisk AND c_data=NEW.c_data AND c_dokl=mc_dokl_po AND poradi=new_poradi AND mnozstvi<0;
								END IF;
							WHEN new_opq='P' THEN
								SELECT mnozstvi INTO moldmnozstvi FROM food600.obra_buf WHERE c_data=NEW.c_data AND c_stredisk=NEW.c_stredisk AND c_karty=NEW.c_karty AND c_dokl=mc_dokl_po AND poradi=new_poradi;
								IF moldmnozstvi+mzustatek=0 THEN
									INSERT INTO food600.prikazy_prepocet (prikaz, riadok) VALUES ('UPDATE food600.obra_buf SET zustatek=ROUND(CAST(mnozstvi+'||mzustatek||' AS DECIMAL(20,10)),'||zaokruhlenie||') WHERE c_data='||NEW.c_data||' AND c_stredisk='||NEW.c_stredisk||' AND c_dokl='||mc_dokl_po||' AND poradi='||new_poradi,idnazapisdoprikazov);
								ELSE
									INSERT INTO food600.prikazy_prepocet (prikaz, riadok) VALUES ('UPDATE food600.obra_buf SET zustatek=ROUND(CAST(mnozstvi+'||mzustatek||' AS DECIMAL(20,10)),'|| zaokruhlenie||'), cena_prum=ROUND(CAST((mnozstvi*cena+'||mzustatek*mcena_prum||')/(mnozstvi+'||mzustatek||') AS DECIMAL(20,10)),'||zaokruhlenie||') WHERE c_stredisk='||NEW.c_stredisk||' AND c_data='||NEW.c_data||' AND c_dokl='||mc_dokl_po||' AND poradi='||new_poradi,idnazapisdoprikazov);
								END IF;
							WHEN new_opq='V' THEN
								IF zapisuj THEN
									insert into food600.test (cas,text) values (clock_timestamp(),'2U Update obra_buf. Zustatek=mnozstvi+'||mzustatek);
								END IF;
								INSERT INTO food600.prikazy_prepocet (prikaz,riadok) VALUES ('UPDATE food600.obra_buf SET zustatek=ROUND(CAST(mnozstvi+'||mzustatek||' AS DECIMAL(20,10)),'|| zaokruhlenie||'), cena_prum='||mcena_prum||', cena='||mcena_prum||' WHERE c_stredisk='||NEW.c_stredisk||' AND c_data='||NEW.c_data||' AND c_dokl='||mc_dokl_po||' AND poradi='||new_poradi,idnazapisdoprikazov);
							ELSE
						END CASE;
					END IF;
				ELSE
					-- Na tomto doklade sa karta este vyskytuje. Treba upravit data pre dalsi vyskyt tejto karty na danom doklade
					mc_dokl_po:=NEW.c_dokl;
					CASE
						WHEN moperace='PI' THEN
							INSERT INTO food600.prikazy_prepocet (prikaz, riadok) VALUES ('UPDATE food600.obra_buf SET cena='||mcena_prum||', cena_prum='||mcena_prum||', mnozstvi=ROUND(CAST('||mzustatek||'-zustatek AS DECIMAL(20,10)),'||zaokruhlenie||') WHERE c_data='||NEW.c_data||' AND c_stredisk='||NEW.c_stredisk||' AND c_dokl='||mc_dokl_po||' AND poradi='||new_poradi,idnazapisdoprikazov);
						WHEN moperace='PB' AND NEW.mnozstvi<0 AND NEW.poradi=new_poradi THEN
						WHEN mopq='P' THEN
							SELECT mnozstvi, cena INTO new_mnozstvi, new_cena FROM food600.obra_buf WHERE c_data=NEW.c_data  AND c_stredisk=NEW.c_stredisk AND c_dokl=mc_dokl_po AND poradi=new_poradi and c_karty=NEW.c_karty;
							IF new_mnozstvi+mzustatek<>0 THEN 
								INSERT INTO food600.prikazy_prepocet (prikaz, riadok) VALUES ('UPDATE food600.obra_buf SET zustatek='||ROUND(CAST(new_mnozstvi+mzustatek AS DECIMAL(20,10)), zaokruhlenie)||', cena_prum='||ROUND(CAST((new_mnozstvi*new_cena+mzustatek*mcena_prum)/(new_mnozstvi+mzustatek) AS DECIMAL(20,10)),zaokruhlenie)||' WHERE c_data='||NEW.c_data||' AND c_stredisk='||NEW.c_stredisk||' AND c_dokl='||mc_dokl_po||' AND poradi='||new_poradi,idnazapisdoprikazov);
							ELSE
								INSERT INTO food600.prikazy_prepocet (prikaz, riadok) VALUES ('UPDATE food600.obra_buf SET cena_prum='||new_cena||', zustatek=ROUND(CAST('||new_mnozstvi+mzustatek||' AS DECIMAL(20,10)),'||zaokruhlenie||') WHERE c_data='||NEW.c_data||' AND c_stredisk='||NEW.c_stredisk||' AND c_dokl='||mc_dokl_po||' AND poradi='||new_poradi,idnazapisdoprikazov);
							END IF;
						WHEN mopq='V' THEN
							INSERT INTO food600.prikazy_prepocet (prikaz, riadok) VALUES ('UPDATE food600.obra_buf SET cena='||mcena_prum||', cena_prum='||mcena_prum||', zustatek=ROUND(CAST(mnozstvi+'||mzustatek||' AS DECIMAL(20,10)),'||zaokruhlenie||') WHERE c_data='||NEW.c_data||' AND c_stredisk='||NEW.c_stredisk||' AND c_dokl='||mc_dokl_po||' AND poradi='||new_poradi,idnazapisdoprikazov);
						ELSE
					END CASE;
				END IF;
			END IF;
		ELSE
			-- nic podstatne sa nezmenilo, nic netreba robit
		END IF;
	END IF;
END IF;
IF (TG_OP = 'DELETE') THEN
	idnazapisdoprikazov:=OLD.idriadok;
	IF OLD.operace='SF' OR OLD.operace='ZE' OR OLD.operace='ZV' THEN
	ELSE
		IF (OLD.operace IN ('VK','VP','PP') AND OLD.dan=9) THEN
			RETURN OLD;
		END IF;
		SELECT c_mena, operace, pz2 INTO mc_mena, moperace, mpz2 FROM food600.dokl_buf WHERE c_dokl=OLD.c_dokl AND c_data=OLD.c_data;
		mopq:=food600.left(moperace,1);
		IF (mopq='P' OR mopq='V') THEN
		ELSE
			RETURN NULL;
		END IF;
		IF zapisuj THEN
			insert into food600.test (cas,text) values (clock_timestamp(),'2D TG_OP '||TG_OP);
			insert into food600.test (cas,text) values (clock_timestamp(),'2D old.c_dokl '||old.c_dokl);
			insert into food600.test (cas,text) values (clock_timestamp(),'2D mopq '||mopq);
			insert into food600.test (cas,text) values (clock_timestamp(),'2D moperace '||moperace);
			insert into food600.test (cas,text) values (clock_timestamp(),'2D c_karty '||OLD.c_karty);
			insert into food600.test (cas,text) values (clock_timestamp(),'2D mnozstvi '||OLD.mnozstvi);
			insert into food600.test (cas,text) values (clock_timestamp(),'2D mc_mena '||mc_mena);
		END IF;
		SELECT MAX(poradi) INTO old_poradi FROM food600.obra_buf WHERE c_data=OLD.c_data AND c_stredisk=OLD.c_stredisk AND c_dokl=OLD.c_dokl AND c_karty=OLD.c_karty AND poradi<OLD.poradi;
		IF old_poradi IS NULL THEN
			-- na tomto doklade sa este dana karta nevyskytuje. Musim pohladat najblizsi starsi doklad, na ktorom sa vyskytuje
			SELECT MAX(c_dokl) INTO mc_dokl_pred FROM food600.obra_buf WHERE c_data=OLD.c_data AND c_stredisk=OLD.c_stredisk AND c_dokl<OLD.c_dokl AND c_karty=OLD.c_karty AND (SUBSTR(operace,1, 1)='V' OR SUBSTR(operace,1, 1)='P');
			IF mc_dokl_pred IS NULL THEN
				-- na tomto stredisku sa dana karta este nevyskytuje. 
				moldzustatek:=0;
				moldcena_prum:=0;
				mc_mena_old:=mc_mena;
			ELSE
				SELECT c_mena, operace INTO mc_mena_old, old_operace FROM food600.dokl_buf WHERE c_data=OLD.c_data AND c_dokl=mc_dokl_pred ;
				IF mc_mena_old IS NULL THEN
					mc_mena_old:=mc_mena;
				END IF;
				SELECT MAX(poradi) INTO old_poradi FROM food600.obra_buf WHERE c_data=OLD.c_data AND c_dokl=mc_dokl_pred AND c_karty=OLD.c_karty;
				IF old_operace='PC' THEN
					SELECT zustatek, cena_prum, mnozstvi INTO moldzustatek, moldcena_prum, moldmnozstvi FROM food600.obra_buf WHERE c_data=OLD.c_data AND c_stredisk=OLD.c_stredisk AND c_dokl=mc_dokl_pred AND c_karty=OLD.c_karty AND poradi=old_poradi AND mnozstvi>=0;
				ELSE
					IF old_operace='PB' THEN
						SELECT zustatek, cena_prum, idriadok INTO moldzustatek, moldcena_prum, old_idriadok FROM food600.obra_buf WHERE c_data=OLD.c_data AND c_stredisk=OLD.c_stredisk AND c_dokl=mc_dokl_pred AND c_karty=OLD.c_karty AND poradi=old_poradi AND mnozstvi>=0;
						IF old_idriadok IS NULL THEN
							SELECT zustatek, cena_prum INTO moldzustatek, moldcena_prum FROM food600.obra_buf WHERE c_data=OLD.c_data AND c_stredisk=OLD.c_stredisk AND c_dokl=mc_dokl_pred AND c_karty=OLD.c_karty AND poradi=old_poradi AND mnozstvi<0;
						END IF;
					ELSE
						SELECT zustatek, cena_prum INTO moldzustatek, moldcena_prum FROM food600.obra_buf WHERE c_data=OLD.c_data AND c_stredisk=OLD.c_stredisk AND c_dokl=mc_dokl_pred AND c_karty=OLD.c_karty AND poradi=old_poradi;
					END IF;
				END IF;
			END IF;	
		ELSE
			mc_mena_old:=mc_mena;
			SELECT zustatek, cena_prum INTO moldzustatek, moldcena_prum FROM food600.obra_buf WHERE c_data=OLD.c_data AND c_stredisk=OLD.c_stredisk AND c_dokl=OLD.c_dokl AND c_karty=OLD.c_karty AND poradi=old_poradi;
		END IF;
		IF mc_mena_old<>mc_mena THEN
			moldcena_prum:=ROUND(CAST(food600.prep_meny(mc_mena_old,mc_mena,moldcena_prum) AS DECIMAL(20,10)),zaokruhlenie);
		END IF;
		IF zapisuj THEN
			insert into food600.test (cas,text) values (clock_timestamp(),'2D moldzustatek '||moldzustatek);
			insert into food600.test (cas,text) values (clock_timestamp(),'2D moldcena_prum '||moldcena_prum);
		END IF;
		mzustatek:=ROUND(CAST(OLD.zustatek-OLD.mnozstvi AS DECIMAL(20,10)), zaokruhlenie);
		mcena_prum:=moldcena_prum;
		IF zapisuj THEN
			insert into food600.test (cas,text) values (clock_timestamp(),'2D mzustatek '||mzustatek);
			insert into food600.test (cas,text) values (clock_timestamp(),'2D mcena_prum '||mcena_prum);
		END IF;
		SELECT MIN(poradi) INTO new_poradi FROM food600.obra_buf WHERE c_data=OLD.c_data AND c_stredisk=OLD.c_stredisk AND c_dokl=OLD.c_dokl AND c_karty=OLD.c_karty AND poradi>OLD.poradi;
		IF new_poradi IS NULL THEN
			-- na tomto doklade sa uz dana karta nevyskytuje. Musim pohladat najblizsi doklad, na ktorom sa vyskytuje
			SELECT MIN(c_dokl) INTO mc_dokl_po FROM food600.obra_buf WHERE c_data=OLD.c_data AND c_stredisk=OLD.c_stredisk AND c_dokl>OLD.c_dokl AND c_karty=OLD.c_karty AND (SUBSTR(operace,1, 1)='V' OR SUBSTR(operace,1, 1)='P');
			IF mc_dokl_po IS NULL THEN
				-- na tomto stredisku sa dana karta uz nevyskytuje. Musim uz len urobit zapis do skl_bufu
				IF zapisuj THEN
					insert into food600.test (cas,text) values (clock_timestamp(),'2D Zapis do skl_bufu: c_karty'||OLD.c_karty||',cena:'||mcena_prum||',zustatek:'||mzustatek);
				END IF;
				SELECT idriadok INTO new_idriadok FROM food600.skl_buf WHERE c_data=OLD.c_data AND c_stredisk=OLD.c_stredisk AND c_karty=OLD.c_karty;
				IF new_idriadok IS NULL THEN
					IF zapisuj THEN
						insert into food600.test (cas,text) values (clock_timestamp(),'2D Zapis do skl_bufu: novy riadok');
					END IF;
					INSERT INTO food600.skl_buf (c_data, c_stredisk, c_karty, zustatek, cena_prum, c_mena) VALUES (OLD.c_data, OLD.c_stredisk, OLD.c_karty, mzustatek, mcena_prum, mc_mena_old);
				ELSE
					IF zapisuj THEN
						insert into food600.test (cas,text) values (clock_timestamp(),'2D Zapis do skl_bufu: povodny riadok');
					END IF;
					UPDATE food600.skl_buf SET cena_prum=mcena_prum, zustatek=mzustatek, c_mena=mc_mena_old WHERE c_data=OLD.c_data AND c_stredisk=OLD.c_stredisk AND c_karty=OLD.c_karty;
				END IF;
			ELSE
				-- este sa karta na nejakom doklade vyskytuje. Najdem min poradi pre tuto kartu, menu dokladu a operaciu dokladu. Prepocitam co treba a urobim zapis
				IF zapisuj THEN
					insert into food600.test (cas,text) values (clock_timestamp(),'2D mc_dokl_po '||mc_dokl_po);
				END IF;
				SELECT operace, c_mena INTO new_operace, mc_mena_new FROM food600.dokl_buf WHERE c_data=OLD.c_data AND c_stredisk=OLD.c_stredisk AND c_dokl=mc_dokl_po;
				new_opq:=food600.left(new_operace,1);
				SELECT MIN(poradi) INTO new_poradi FROM food600.obra_buf WHERE c_data=OLD.c_data AND c_stredisk=OLD.c_stredisk AND c_dokl=mc_dokl_po AND c_karty=OLD.c_karty;
				IF zapisuj THEN
					insert into food600.test (cas,text) values (clock_timestamp(),'2D new_poradi '||new_poradi);
				END IF;
				IF mc_mena<>mc_mena_new THEN
					mcena_prum:=ROUND(CAST(food600.prep_meny(mc_mena,mc_mena_new,mcena_prum) AS DECIMAL(20,10)),zaokruhlenie);
				END IF;
				CASE
					WHEN new_operace='PI' THEN
						INSERT INTO food600.prikazy_prepocet (prikaz, riadok) VALUES ('UPDATE food600.obra_buf SET cena='||mcena_prum||', cena_prum='||mcena_prum||', mnozstvi=ROUND(CAST('||mzustatek||'-zustatek AS DECIMAL(20,10)),'||zaokruhlenie||') WHERE c_data='||OLD.c_data||' AND c_stredisk='||OLD.c_stredisk||' AND c_dokl='||mc_dokl_po||' AND poradi='||new_poradi,idnazapisdoprikazov);
					WHEN new_operace='PC' THEN
						INSERT INTO food600.prikazy_prepocet (prikaz, riadok) VALUES ('UPDATE food600.obra_buf SET cena='||mcena_prum||', cena_prum='||mcena_prum||', mnozstvi=ROUND(CAST('||mzustatek||'-zustatek AS DECIMAL(20,10)),'||zaokruhlenie||') WHERE c_data='||OLD.c_data||' AND c_stredisk='||OLD.c_stredisk||' AND c_dokl='||mc_dokl_po||' AND poradi='||new_poradi,idnazapisdoprikazov);
					WHEN new_operace='VP' or new_operace='PP' THEN
						SELECT idriadok INTO old_idriadok FROM food600.obra_buf WHERE c_data=OLD.c_data AND c_stredisk=OLD.c_stredisk AND c_dokl=mc_dokl_po AND c_karty=OLD.c_karty AND poradi=new_poradi AND mnozstvi<0;
						IF old_idriadok IS NULL THEN
							INSERT INTO food600.prikazy_prepocet (prikaz, riadok) VALUES ('UPDATE food600.obra_buf SET cena_prum=ROUND(CAST((mnozstvi*cena+'||mzustatek*mcena_prum||')/(mnozstvi+'||mzustatek||') AS DECIMAL(20,10)),'||zaokruhlenie||'), zustatek=mnozstvi+'||mzustatek||' WHERE c_data='||OLD.c_data||' AND c_stredisk='||OLD.c_stredisk||' AND c_dokl='||mc_dokl_po||' AND poradi='||new_poradi||' AND mnozstvi>=0',idnazapisdoprikazov);
						ELSE
							INSERT INTO food600.prikazy_prepocet (prikaz, riadok) VALUES ('UPDATE food600.obra_buf SET cena='||mcena_prum||', cena_prum='||mcena_prum||', zustatek=mnozstvi+'||mzustatek||' WHERE idriadok='||old_idriadok,idnazapisdoprikazov);
						END IF;
					WHEN new_operace='PB' THEN
						SELECT idriadok INTO old_idriadok FROM food600.obra_buf WHERE c_data=OLD.c_data AND c_stredisk=OLD.c_stredisk AND c_dokl=mc_dokl_po AND c_karty=OLD.c_karty AND poradi=new_poradi AND mnozstvi<0;
						IF old_idriadok IS NULL THEN
							INSERT INTO food600.prikazy_prepocet (prikaz, riadok) VALUES ('UPDATE food600.obra_buf SET cena_prum=ROUND(CAST((mnozstvi*cena+'||mzustatek*mcena_prum||')/(mnozstvi+'||mzustatek||') AS DECIMAL(20,10)),'||zaokruhlenie||'), zustatek=mnozstvi+'||mzustatek||' WHERE c_data='||OLD.c_data||' AND c_stredisk='||OLD.c_stredisk||' AND c_dokl='||mc_dokl_po||' AND poradi='||new_poradi||' AND mnozstvi>=0',idnazapisdoprikazov);
						ELSE
							INSERT INTO food600.prikazy_prepocet (prikaz, riadok) VALUES ('UPDATE food600.obra_buf SET cena='||mcena_prum||', cena_prum='||mcena_prum||', zustatek=mnozstvi+'||mzustatek||' WHERE c_data='||OLD.c_data||' AND c_stredisk='||OLD.c_stredisk||' AND c_dokl='||mc_dokl_po||' AND poradi='||new_poradi||' AND mnozstvi<0',idnazapisdoprikazov);
						END IF;
					WHEN new_opq='P' THEN
						INSERT INTO food600.prikazy_prepocet (prikaz, riadok) VALUES ('UPDATE food600.obra_buf SET cena_prum=ROUND(CAST((mnozstvi*cena+'||mzustatek*mcena_prum||')/(mnozstvi+'||mzustatek||') AS DECIMAL(20,10)),'||zaokruhlenie||'), zustatek=mnozstvi+'||mzustatek||' WHERE c_data='||OLD.c_data||' AND c_stredisk='||OLD.c_stredisk||' AND c_dokl='||mc_dokl_po||' AND poradi='||new_poradi,idnazapisdoprikazov);
					WHEN new_opq='V' THEN
						INSERT INTO food600.prikazy_prepocet (prikaz, riadok) VALUES ('UPDATE food600.obra_buf SET cena='||mcena_prum||', cena_prum='||mcena_prum||', zustatek=mnozstvi+'||mzustatek||' WHERE c_data='||OLD.c_data||' AND c_stredisk='||OLD.c_stredisk||' AND c_dokl='||mc_dokl_po||' AND poradi='||new_poradi,idnazapisdoprikazov);
					ELSE
				END CASE;
			END IF;
		ELSE
			-- Karta sa este vyskytuje na tomto doklade. Treba upravit udaje na tomto riadku.
			IF zapisuj THEN
				insert into food600.test (cas,text) values (clock_timestamp(),'2D mc_dokl_po '||old.c_dokl);
				insert into food600.test (cas,text) values (clock_timestamp(),'2D new_poradi '||new_poradi);
			END IF;
			CASE
				WHEN moperace='PI' THEN
					INSERT INTO food600.prikazy_prepocet (prikaz, riadok) VALUES ('UPDATE food600.obra_buf SET cena='||mcena_prum||', cena_prum='||mcena_prum||', mnozstvi=ROUND(CAST('||mzustatek||'-zustatek AS DECIMAL(20,10)),'||zaokruhlenie||') WHERE c_data='||OLD.c_data||' AND c_stredisk='||OLD.c_stredisk||' AND c_dokl='||OLD.c_dokl||' AND poradi='||new_poradi,idnazapisdoprikazov);
					PERFORM food600.dobehniprepocet(idnazapisdoprikazov);
				WHEN mopq='P' THEN
					INSERT INTO food600.prikazy_prepocet (prikaz, riadok) VALUES ('UPDATE food600.obra_buf SET cena_prum=ROUND(CAST((mnozstvi*cena+'||mzustatek*mcena_prum||')/(mnozstvi+'||mzustatek||') AS DECIMAL(20,10)),'||zaokruhlenie||'), zustatek=mnozstvi+'||mzustatek||' WHERE c_data='||OLD.c_data||' AND c_stredisk='||OLD.c_stredisk||' AND c_dokl='||OLD.c_dokl||' AND poradi='||new_poradi,idnazapisdoprikazov);
					PERFORM food600.dobehniprepocet(idnazapisdoprikazov);
				WHEN mopq='V' THEN
					INSERT INTO food600.prikazy_prepocet (prikaz, riadok) VALUES ('UPDATE food600.obra_buf SET cena='||mcena_prum||', cena_prum='||mcena_prum||', zustatek=mnozstvi+'||mzustatek||' WHERE c_data='||OLD.c_data||' AND c_stredisk='||OLD.c_stredisk||' AND c_dokl='||OLD.c_dokl||' AND poradi='||new_poradi,idnazapisdoprikazov);
					PERFORM food600.dobehniprepocet(idnazapisdoprikazov);
			ELSE
			END CASE;
		END IF;
	END IF;
END IF;
--PERFORM food600.dobehniprepocet(idnazapisdoprikazov);
        
RETURN NEW;
END;
$BODY$;
