CREATE OR REPLACE FUNCTION food600.prep_1_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);
	moldmnozstvi 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);
	old_idriadok INTEGER;
	new_idriadok INTEGER;
	new_mnozstvi 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;
	zaokruhlenie_mn INTEGER DEFAULT 4;
	buranierovnakych INTEGER DEFAULT 0;
	buranierovnakych_old INTEGER DEFAULT 0;
	buranierovnakych_new INTEGER DEFAULT 0;
BEGIN
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;
mopq:=substring (moperace,1,1);
IF (TG_OP = 'UPDATE') THEN
	old_opq:=substring(OLD.operace,1,1);
	IF zapisuj THEN
		insert into food600.test (cas,text) values (clock_timestamp(),'1U TG_OP '||TG_OP);
		insert into food600.test (cas,text) values (clock_timestamp(),'1U new.c_dokl '||new.c_dokl);
		insert into food600.test (cas,text) values (clock_timestamp(),'1U old_opq '||old_opq);
		insert into food600.test (cas,text) values (clock_timestamp(),'1U mopq '||mopq);
		insert into food600.test (cas,text) values (clock_timestamp(),'1U old.ckarty '||old.c_karty);
		insert into food600.test (cas,text) values (clock_timestamp(),'1U old.mnozstvi '||old.mnozstvi);
		raise notice '1U TG_OP:%',TG_OP;
		raise notice '1U new.c_dokl:%',new.c_dokl::bpchar;
		raise notice '1U old_opq:%',old_opq::bpchar;
		raise notice '1U mopq:%',mopq::bpchar;
		raise notice '1U moperace:%',moperace::bpchar;
		raise notice '1U old.c_karty:%',old.c_karty::bpchar;
		raise notice '1U old.mnozstvi:%',old.mnozstvi::bpchar;
	END IF;
	IF (mopq='P' OR mopq='V' OR old_opq='P' OR old_opq='V') THEN
		IF moperace='VV' THEN
			cpz2:=food600.tvor_cislo(mpz2);
		END IF;
	ELSE
		RETURN NEW;
	END IF;
	IF OLD.c_karty<>NEW.c_karty OR OLD.c_stredisk<>NEW.c_stredisk OR OLD.poradi<>NEW.poradi OR OLD.operace<>NEW.operace OR OLD.c_dokl<>NEW.c_dokl THEN
		INSERT INTO food600.prikazy_prepocet (prikaz, riadok) VALUES ('DELETE FROM food600.obra_buf WHERE idriadok='||OLD.idriadok,OLD.idriadok);
		INSERT INTO food600.prikazy_prepocet (prikaz, riadok) VALUES ('INSERT INTO food600.obra_buf (c_data, c_stredisk, c_dokl, c_karty, operace, mnozstvi, cena, cena_prum, zustatek, cena_part, poradi, spdan) VALUES ('||NEW.c_data||','||NEW.c_stredisk||','||NEW.c_dokl||','||NEW.c_karty||','''||NEW.operace||''','||NEW.mnozstvi||','||NEW.cena||','||NEW.cena_prum||','||NEW.zustatek||','||NEW.cena_part||','||NEW.poradi||','||NEW.spdan||')',OLD.idriadok);
		RETURN OLD;
	ELSE
		buranierovnakych:=0;
		IF (NEW.zust_old IS NOT NULL AND NEW.zust_old<>0) OR (NEW.cena_prum_old IS NOT NULL AND NEW.cena_prum_old<>0) OR (NEW.c_mena_old IS NOT NULL AND NEW.c_mena_old<>0) OR (NEW.mnoz_old IS NOT NULL AND NEW.mnoz_old<>0) THEN
			IF NEW.operace='PB' THEN
				IF NEW.mnozstvi>=0 THEN
					SELECT MAX(poradi) INTO old_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 AND mnozstvi<0;
					IF old_poradi IS NOT NULL THEN
					buranierovnakych:=1;
					END IF;
				END IF;
			END IF;
			moldzustatek:=ROUND(CAST(NEW.zust_old AS NUMERIC), zaokruhlenie_mn);
			moldcena_prum:=ROUND(CAST(NEW.cena_prum_old AS NUMERIC), zaokruhlenie);
			moldmnozstvi:=ROUND(CAST(NEW.mnoz_old AS NUMERIC), zaokruhlenie_mn);
			mc_mena_old:=NEW.c_mena_old;
			NEW.c_mena_old:=null;
			NEW.zust_old:=null;
			NEW.cena_prum_old:=null;
			NEW.mnoz_old:=null;
		ELSE 
			IF NEW.operace='PC' and NEW.mnozstvi>=0 THEN
				old_poradi:=NEW.poradi;
			ELSE
				IF NEW.operace='PB' THEN
					IF NEW.mnozstvi>=0 THEN
						SELECT MAX(poradi) INTO old_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 AND mnozstvi<0;
						IF old_poradi IS NULL THEN
							SELECT MAX(poradi) INTO old_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;
						ELSE
							buranierovnakych:=1;
						END IF;
					ELSE 
						SELECT MAX(poradi) INTO old_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;
					END IF;
				ELSE
					IF NEW.operace='PP' THEN
						SELECT MAX(poradi) INTO old_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 AND mnozstvi<0;
						IF old_poradi IS NOT NULL THEN
							buranierovnakych:=1;
						END IF;
					ELSE 
						SELECT MAX(poradi) INTO old_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;
					END IF;
				END IF;
			END IF;
			IF zapisuj THEN
				IF old_poradi IS NULL THEN
					insert into food600.test (cas,text) values (clock_timestamp(),'1U old_poradi =NULL');
				ELSE
					insert into food600.test (cas,text) values (clock_timestamp(),'1U old_poradi '||old_poradi);
				END IF;
				insert into food600.test (cas,text) values (clock_timestamp(),'1U buranie rovnakych kariet '||buranierovnakych);
			END IF;
			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=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');
				IF mc_dokl_pred IS NULL THEN
					-- na tomto stredisku sa dana karta este nevyskytuje. 
					moldzustatek:=0;
					moldcena_prum:=0;
					moldmnozstvi:=0;
					mc_mena_old:=mc_mena;
				ELSE
					SELECT c_mena, operace INTO mc_mena_old, old_operace FROM food600.dokl_buf WHERE c_data=NEW.c_data AND c_stredisk=NEW.c_stredisk AND c_dokl=mc_dokl_pred;
					SELECT MAX(poradi) INTO old_poradi FROM food600.obra_buf WHERE c_data=NEW.c_data AND c_stredisk=NEW.c_stredisk AND c_dokl=mc_dokl_pred AND c_karty=NEW.c_karty;
					IF zapisuj THEN
						insert into food600.test (cas,text) values (clock_timestamp(),'1U c_dokl_pred '||mc_dokl_pred);
						insert into food600.test (cas,text) values (clock_timestamp(),'1U old_operace '||old_operace);
						insert into food600.test (cas,text) values (clock_timestamp(),'1U old_poradi '||old_poradi);
					END IF;
					IF old_operace='PC' THEN
						SELECT zustatek, cena_prum, mnozstvi INTO moldzustatek, moldcena_prum, moldmnozstvi FROM food600.obra_buf WHERE  c_data=NEW.c_data AND c_stredisk=NEW.c_stredisk AND c_dokl=mc_dokl_pred AND c_karty=NEW.c_karty AND poradi=old_poradi AND mnozstvi>=0;
					ELSE
						IF old_operace='PB' THEN
							--- Stary doklad je buranie, hladam najprv kladne mnozstva
							SELECT idriadok INTO old_idriadok FROM food600.obra_buf WHERE  c_data=NEW.c_data AND c_stredisk=NEW.c_stredisk AND c_dokl=mc_dokl_pred AND c_karty=NEW.c_karty AND poradi=old_poradi AND mnozstvi>=0;
							IF old_idriadok IS NULL THEN
								--- Ked sa kladne burania nenasli, najdem posledne zaporne buranie a jeho zustatek a cena_prum
								SELECT zustatek, cena_prum, mnozstvi INTO moldzustatek, moldcena_prum, moldmnozstvi FROM food600.obra_buf WHERE  c_data=NEW.c_data AND c_stredisk=NEW.c_stredisk AND c_dokl=mc_dokl_pred AND c_karty=NEW.c_karty AND poradi=old_poradi AND mnozstvi<0;
								IF zapisuj THEN
									insert into food600.test (cas,text) values (clock_timestamp(),'1U moldmnozstvi buranie minus '||moldmnozstvi);
									insert into food600.test (cas,text) values (clock_timestamp(),'1U moldzustatek buranie minus '||moldzustatek);
									insert into food600.test (cas,text) values (clock_timestamp(),'1U moldcena_prum buranie minus '||moldcena_prum);
								END IF;
							ELSE
							--- Kladne buranie sa naslo, takze si nacitam jeho zustatke a cena_prum
								SELECT zustatek, cena_prum, mnozstvi INTO moldzustatek, moldcena_prum, moldmnozstvi FROM food600.obra_buf WHERE c_data=NEW.c_data AND c_stredisk=NEW.c_stredisk AND c_dokl=mc_dokl_pred AND c_karty=NEW.c_karty AND poradi=old_poradi AND mnozstvi>=0;
								IF zapisuj THEN
									insert into food600.test (cas,text) values (clock_timestamp(),'1U moldmnozstvi buranie plus '||moldmnozstvi);
									insert into food600.test (cas,text) values (clock_timestamp(),'1U moldzustatek buranie plus '||moldzustatek);
									insert into food600.test (cas,text) values (clock_timestamp(),'1U moldcena_prum buranie plus '||moldcena_prum);
								END IF;
							END IF;
						ELSE
							IF old_operace='VP' OR old_operace='PP' THEN 
								SELECT idriadok INTO old_idriadok FROM food600.obra_buf WHERE c_data=NEW.c_data AND c_stredisk=NEW.c_stredisk AND c_dokl=mc_dokl_pred AND c_karty=NEW.c_karty AND poradi=old_poradi AND mnozstvi>=0;
								IF old_idriadok IS NULL THEN
									SELECT zustatek, cena_prum, mnozstvi INTO moldzustatek, moldcena_prum, moldmnozstvi FROM food600.obra_buf WHERE c_data=NEW.c_data AND c_stredisk=NEW.c_stredisk AND c_dokl=mc_dokl_pred AND c_karty=NEW.c_karty AND poradi=old_poradi AND mnozstvi<0;
									IF zapisuj THEN
										insert into food600.test (cas,text) values (clock_timestamp(),'1U moldmnozstvi polotovary minus '||moldmnozstvi);
										insert into food600.test (cas,text) values (clock_timestamp(),'1U moldzustatek polotovary minus '||moldzustatek);
										insert into food600.test (cas,text) values (clock_timestamp(),'1U moldcena_prum polotovary minus '||moldcena_prum);
									END IF;
								ELSE
									SELECT zustatek, cena_prum, mnozstvi INTO moldzustatek, moldcena_prum, moldmnozstvi FROM food600.obra_buf WHERE c_data=NEW.c_data AND c_stredisk=NEW.c_stredisk AND c_dokl=mc_dokl_pred AND c_karty=NEW.c_karty AND poradi=old_poradi AND mnozstvi>=0;
									IF zapisuj THEN
										insert into food600.test (cas,text) values (clock_timestamp(),'1U moldmnozstvi polotovary plus '||moldmnozstvi);
										insert into food600.test (cas,text) values (clock_timestamp(),'1U moldzustatek polotovary plus '||moldzustatek);
										insert into food600.test (cas,text) values (clock_timestamp(),'1U moldcena_prum polotovary plus '||moldcena_prum);
									END IF;
								END IF;
							ELSE 
								SELECT zustatek, cena_prum, mnozstvi INTO moldzustatek, moldcena_prum, moldmnozstvi FROM food600.obra_buf WHERE c_data=NEW.c_data AND c_stredisk=NEW.c_stredisk AND c_dokl=mc_dokl_pred AND c_karty=NEW.c_karty AND poradi=old_poradi;
								IF zapisuj THEN
									insert into food600.test (cas,text) values (clock_timestamp(),'1U moldmnozstvi '||moldmnozstvi);
									insert into food600.test (cas,text) values (clock_timestamp(),'1U moldzustatek '||moldzustatek);
									insert into food600.test (cas,text) values (clock_timestamp(),'1U moldcena_prum '||moldcena_prum);
								END IF;
							END IF;
						END IF;
					END IF;
				END IF;
			ELSE
				--- karta sa vyskytla este raz na tom istom doklade
				IF NEW.operace='PC' and NEW.mnozstvi>=0 THEN
					SELECT zustatek, cena_prum, mnozstvi INTO moldzustatek, moldcena_prum, moldmnozstvi 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=old_poradi AND mnozstvi<0;
				ELSE 
					IF NEW.operace='PB' AND NEW.mnozstvi>=0 AND buranierovnakych=1 THEN
						SELECT zustatek, cena_prum, mnozstvi INTO moldzustatek, moldcena_prum, moldmnozstvi 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=old_poradi AND mnozstvi<0;
					ELSE
						IF NEW.operace='PB' AND NEW.mnozstvi<0 AND NEW.poradi=old_poradi THEN
							-- Len pre istotu. Taky stav nemoze nastat. 
							SELECT zustatek, cena_prum, mnozstvi INTO moldzustatek, moldcena_prum, moldmnozstvi 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=old_poradi AND mnozstvi<0;
						ELSE
							IF NEW.operace='PP' AND buranierovnakych=1 THEN
								SELECT zustatek, cena_prum, mnozstvi INTO moldzustatek, moldcena_prum, moldmnozstvi 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=old_poradi AND mnozstvi<0;
							ELSE
								IF NEW.operace='PP' AND buranierovnakych=1 THEN
									SELECT zustatek, cena_prum, mnozstvi INTO moldzustatek, moldcena_prum, moldmnozstvi 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=old_poradi AND mnozstvi<0;
								ELSE
									IF NEW.operace='VP' THEN
										SELECT zustatek, cena_prum, mnozstvi INTO moldzustatek, moldcena_prum, moldmnozstvi FROM food600.obra_buf WHERE  c_dokl=NEW.c_dokl AND c_karty=NEW.c_karty AND c_data=NEW.c_data AND poradi=old_poradi AND mnozstvi>0;
										IF moldzustatek IS NULL THEN
											SELECT zustatek, cena_prum, mnozstvi INTO moldzustatek, moldcena_prum, moldmnozstvi FROM food600.obra_buf WHERE  c_dokl=NEW.c_dokl AND c_karty=NEW.c_karty AND c_data=NEW.c_data AND poradi=old_poradi AND mnozstvi<0;
										END IF;
									ELSE
										SELECT zustatek, cena_prum, mnozstvi INTO moldzustatek, moldcena_prum, moldmnozstvi FROM food600.obra_buf WHERE  c_dokl=NEW.c_dokl AND c_karty=NEW.c_karty AND c_data=NEW.c_data AND poradi=old_poradi;
									END IF;
								END IF; 
							END IF;
						END IF;
					END IF; 
				END IF;
				IF zapisuj THEN
					insert into food600.test (cas,text) values (clock_timestamp(),'1U Druhy vyskyt moldmnozstvi '||moldmnozstvi);
					insert into food600.test (cas,text) values (clock_timestamp(),'1U Druhy vyskyt moldzustatek '||moldzustatek);
					insert into food600.test (cas,text) values (clock_timestamp(),'1U Druhy vyskyt moldcena_prum '||moldcena_prum);
				END IF;
			END IF;
		END IF;
		IF mc_mena_old<>mc_mena THEN
			moldcena_prum:=ROUND(CAST(food600.prep_meny(mc_mena_old,mc_mena,moldcena_prum) AS NUMERIC),zaokruhlenie);
		END IF;
		IF zapisuj THEN
			insert into food600.test (cas,text) values (clock_timestamp(),'1U Rovnaka karta');
			IF old_poradi IS NULL THEN
				insert into food600.test (cas,text) values (clock_timestamp(),'1U old_poradi=NULL');
			ELSE
				insert into food600.test (cas,text) values (clock_timestamp(),'1U old_poradi '||old_poradi);
			END IF;
			IF mc_dokl_pred IS NULL THEN
				insert into food600.test (cas,text) values (clock_timestamp(),'1U mc_dokl_pred=NULL');
			ELSE
				insert into food600.test (cas,text) values (clock_timestamp(),'1U mc_dokl_pred '||mc_dokl_pred);
			END IF;
			insert into food600.test (cas,text) values (clock_timestamp(),'1U moldzustatek '||moldzustatek);
			insert into food600.test (cas,text) values (clock_timestamp(),'1U moldcena_prum '||moldcena_prum);
			insert into food600.test (cas,text) values (clock_timestamp(),'1U OLD.cena '||old.cena);
			insert into food600.test (cas,text) values (clock_timestamp(),'1U NEW.cena '||new.cena);
			insert into food600.test (cas,text) values (clock_timestamp(),'1U OLD.cena_prum '||old.cena_prum);
			insert into food600.test (cas,text) values (clock_timestamp(),'1U NEW.cena_prum '||new.cena_prum);
			insert into food600.test (cas,text) values (clock_timestamp(),'1U OLD.mnozstvi '||old.mnozstvi);
			insert into food600.test (cas,text) values (clock_timestamp(),'1U NEW.mnozstvi '||new.mnozstvi);
			insert into food600.test (cas,text) values (clock_timestamp(),'1U OLD.zustatek '||old.zustatek);
			insert into food600.test (cas,text) values (clock_timestamp(),'1U NEW.zustatek '||new.zustatek);
		END IF;
		IF ROUND(CAST(OLD.cena AS DECIMAL(20,10)), zaokruhlenie)<>ROUND(CAST(NEW.cena AS DECIMAL(20,10)), zaokruhlenie) OR ROUND(CAST(OLD.mnozstvi AS DECIMAL(20,10)), zaokruhlenie_mn)<>ROUND(CAST(NEW.mnozstvi AS DECIMAL(20,10)), zaokruhlenie) OR ROUND(CAST(OLD.cena_prum AS DECIMAL(20,10)), zaokruhlenie)<>ROUND(CAST(NEW.cena_prum AS DECIMAL(20,10)), zaokruhlenie) OR ROUND(CAST(OLD.zustatek AS DECIMAL(20,10)), zaokruhlenie_mn)<>ROUND(CAST(NEW.zustatek AS DECIMAL(20,10)), zaokruhlenie_mn) 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(),'1U Zmena');
			END IF;
			CASE
				WHEN moperace = 'PR' THEN
					mcena_prum:=ROUND(CAST(NEW.cena AS NUMERIC), zaokruhlenie);
					mzustatek:=ROUND(CAST(NEW.mnozstvi AS NUMERIC), zaokruhlenie_mn);
					NEW.cena_prum:=mcena_prum;
					NEW.zustatek:=ROUND(CAST(NEW.mnozstvi AS NUMERIC), zaokruhlenie_mn);
				WHEN moperace = 'PI' THEN
					NEW.mnozstvi:=ROUND(CAST(NEW.zustatek-moldzustatek AS NUMERIC), zaokruhlenie_mn);
					NEW.cena:=moldcena_prum;
					NEW.cena_prum:=moldcena_prum;
					mzustatek:=ROUND(CAST(NEW.zustatek AS NUMERIC), zaokruhlenie_mn);
					mcena_prum:=moldcena_prum;
				WHEN moperace='PC' AND NEW.mnozstvi<0 THEN
					NEW.cena:=moldcena_prum;
					NEW.cena_prum:=moldcena_prum;
					NEW.zustatek:=0;
					NEW.mnozstvi:=-moldzustatek;
					mzustatek:=0;
					mcena_prum:=moldcena_prum;
					SELECT idriadok INTO new_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;
					UPDATE food600.obra_buf SET mnozstvi=-NEW.mnozstvi WHERE idriadok=new_idriadok;
				WHEN moperace='PC' AND NEW.mnozstvi>=0 THEN
					NEW.cena_prum:=ROUND(CAST(NEW.cena AS NUMERIC), zaokruhlenie);
					NEW.mnozstvi:=-moldmnozstvi;
					NEW.zustatek:=-moldmnozstvi;
					mzustatek:=ROUND(CAST(NEW.mnozstvi AS NUMERIC), zaokruhlenie_mn);
					mcena_prum:=ROUND(CAST(NEW.cena AS NUMERIC), zaokruhlenie);
				WHEN NEW.mnozstvi<0 THEN
					NEW.cena:=moldcena_prum;
					NEW.cena_prum:=moldcena_prum;
					NEW.zustatek:=ROUND(CAST(moldzustatek+NEW.mnozstvi AS NUMERIC), zaokruhlenie_mn);
					mzustatek:=ROUND(CAST(NEW.zustatek AS NUMERIC), zaokruhlenie_mn);
					mcena_prum:=moldcena_prum;
				WHEN (NEW.mnozstvi + moldzustatek)<=0 THEN
					mcena_prum:=ROUND(CAST(NEW.cena AS NUMERIC), zaokruhlenie);
					NEW.cena_prum:=ROUND(CAST(NEW.cena AS NUMERIC), zaokruhlenie);
					NEW.zustatek:=ROUND(CAST(moldzustatek+NEW.mnozstvi AS NUMERIC), zaokruhlenie_mn);
					mzustatek:=ROUND(CAST(NEW.zustatek AS NUMERIC), zaokruhlenie_mn);
				WHEN  moldzustatek<=0 THEN
					mcena_prum := ROUND(CAST(NEW.cena AS NUMERIC), zaokruhlenie);
					mzustatek:=ROUND(CAST(moldzustatek+NEW.mnozstvi AS NUMERIC), zaokruhlenie_mn);
					NEW.zustatek:=mzustatek;
					NEW.cena:=mcena_prum;
					NEW.cena_prum:=mcena_prum;
				ELSE
					IF NEW.operace='PV' THEN
						IF zapisuj THEN
							insert into food600.test (cas,text) values (clock_timestamp(),'1U interny prijem');
						END IF;
						IF NEW.cena_part=-123456789 THEN
							NEW.cena_part:=OLD.cena_part;
							IF zapisuj THEN
								insert into food600.test (cas,text) values (clock_timestamp(),'1U NEW.cena sa neprepisuje - prisla z predch.prepoctu');
							END IF;
						ELSE
							IF ROUND(CAST(OLD.mnozstvi*OLD.cena AS NUMERIC),zaokruhlenie)<>ROUND(CAST(NEW.mnozstvi*NEW.cena AS NUMERIC),zaokruhlenie) THEN
								NEW.cena:=ROUND(CAST(OLD.mnozstvi*OLD.cena/NEW.mnozstvi AS NUMERIC), zaokruhlenie);
								IF zapisuj THEN
									insert into food600.test (cas,text) values (clock_timestamp(),'1U NEW.cena '||NEW.cena);
								END IF;
							ELSE
								IF zapisuj THEN
									insert into food600.test (cas,text) values (clock_timestamp(),'1U stare prachy '||ROUND(CAST(OLD.mnozstvi*OLD.cena AS NUMERIC),zaokruhlenie));
									insert into food600.test (cas,text) values (clock_timestamp(),'1U nove prachy '||ROUND(CAST(NEW.mnozstvi*NEW.cena AS NUMERIC),zaokruhlenie));
								END IF;
							END IF;
						END IF;
					END IF;
					mcena_prum:=ROUND(CAST((moldzustatek*moldcena_prum+NEW.mnozstvi*NEW.cena)/(NEW.mnozstvi+moldzustatek) AS DECIMAL(20,10)),zaokruhlenie);
					IF mcena_prum > 99999999 THEN
						mcena_prum := 99999999.9999;
					END IF;
					mzustatek:=ROUND(CAST(moldzustatek+NEW.mnozstvi AS NUMERIC), zaokruhlenie_mn);
					NEW.zustatek:=mzustatek;
					NEW.cena_prum:=mcena_prum;
			END CASE;
			IF zapisuj THEN
				insert into food600.test (cas,text) values (clock_timestamp(),'1U Operace '||moperace);
				insert into food600.test (cas,text) values (clock_timestamp(),'1U Vypocitany zustatek '||mzustatek);
				insert into food600.test (cas,text) values (clock_timestamp(),'1U Vypocitana cena_prum '||mcena_prum);
			END IF;
		ELSE
			-- nic podstatne sa nezmenilo, nic netreba robit
		END IF;
	END IF;
END IF;
IF (TG_OP = 'INSERT') THEN 
	IF zapisuj THEN
		insert into food600.test (cas,text) values (clock_timestamp(),'1I TG_OP '||TG_OP);
		insert into food600.test (cas,text) values (clock_timestamp(),'1I new.c_dokl '||new.c_dokl);
		insert into food600.test (cas,text) values (clock_timestamp(),'1I mopq '||mopq);
		insert into food600.test (cas,text) values (clock_timestamp(),'1I c_karty '||new.c_karty);
	END IF;
	IF (mopq='P' OR mopq='V' OR old_opq='P' OR old_opq='V') THEN
		IF moperace='VV' THEN
			cpz2:=food600.tvor_cislo(mpz2);
		END IF;
	ELSE
		RETURN NEW;
	END IF;
	buranierovnakych:=0;
	IF (NEW.zust_old IS NOT NULL AND NEW.zust_old<>0) OR (NEW.cena_prum_old IS NOT NULL AND NEW.cena_prum_old<>0) OR (NEW.c_mena_old IS NOT NULL AND NEW.c_mena_old<>0) OR (NEW.mnoz_old IS NOT NULL AND NEW.mnoz_old<>0) THEN
		IF NEW.operace='PB' THEN
			IF NEW.mnozstvi>=0 THEN
				SELECT MAX(poradi) INTO old_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 AND mnozstvi<0;
				IF old_poradi IS NOT NULL THEN
				buranierovnakych:=1;
				END IF;
			END IF;
		END IF;
		moldzustatek:=ROUND(CAST(NEW.zust_old AS NUMERIC), zaokruhlenie_mn);
		moldcena_prum:=ROUND(CAST(NEW.cena_prum_old AS NUMERIC), zaokruhlenie);
		moldmnozstvi:=ROUND(CAST(NEW.mnoz_old AS NUMERIC), zaokruhlenie_mn);
		mc_mena_old:=NEW.c_mena_old;
		NEW.c_mena_old:=null;
		NEW.zust_old:=null;
		NEW.cena_prum_old:=null;
		NEW.mnoz_old:=null;
	ELSE
		IF NEW.operace='PC' and NEW.mnozstvi>=0 THEN
			old_poradi:=NEW.poradi;
		ELSE
			IF NEW.operace='PB' and NEW.mnozstvi>=0 THEN
				SELECT MAX(poradi) INTO old_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 AND mnozstvi<0;
				IF old_poradi IS NULL THEN
					SELECT MAX(poradi) INTO old_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;
					IF zapisuj THEN
						IF old_poradi IS NULL THEN
							insert into food600.test (cas,text) values (clock_timestamp(),'1I PB minus sa nenaslo old_poradi=NULL');
						ELSE
							insert into food600.test (cas,text) values (clock_timestamp(),'1I PB minus sa naslo '||old_poradi);
						END IF;
					END IF;
				ELSE 
					buranierovnakych:=1;
				END IF;
			ELSE
				IF NEW.operace='PP' THEN
					SELECT MAX(poradi) INTO old_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 AND mnozstvi<0;
					IF old_poradi IS NOT NULL THEN
						buranierovnakych:=1;
					END IF;
				ELSE 
					SELECT MAX(poradi) INTO old_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;
				END IF;
			END IF;
		END IF;
		IF zapisuj THEN
			IF old_poradi IS NULL THEN
				insert into food600.test (cas,text) values (clock_timestamp(),'1I old_poradi=NULL');
			ELSE
				insert into food600.test (cas,text) values (clock_timestamp(),'1I old_poradi '||old_poradi);
			END IF;
		END IF;
		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=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');
			IF mc_dokl_pred IS NULL THEN
				-- na tomto stredisku sa dana karta este nevyskytuje. 
				moldzustatek:=0;
				moldcena_prum:=0;
				moldmnozstvi:=0;
				mc_mena_old:=mc_mena;
			ELSE
				SELECT c_mena, operace INTO mc_mena_old, old_operace FROM food600.dokl_buf WHERE c_data=NEW.c_data AND c_stredisk=NEW.c_stredisk AND c_dokl=mc_dokl_pred;
				SELECT MAX(poradi) INTO old_poradi FROM food600.obra_buf WHERE c_data=NEW.c_data AND c_stredisk=NEW.c_stredisk AND c_dokl=mc_dokl_pred AND c_karty=NEW.c_karty;
				IF zapisuj THEN
					insert into food600.test (cas,text) values (clock_timestamp(),'1I old_operace '||old_operace);
					insert into food600.test (cas,text) values (clock_timestamp(),'1I old_poradi '||old_poradi);
				END IF;
				IF old_operace='PC' THEN
					SELECT zustatek, cena_prum, mnozstvi INTO moldzustatek, moldcena_prum, moldmnozstvi FROM food600.obra_buf WHERE  c_data=NEW.c_data AND c_stredisk=NEW.c_stredisk AND c_dokl=mc_dokl_pred AND c_karty=NEW.c_karty AND poradi=old_poradi AND mnozstvi>=0;
				ELSE
					IF old_operace='PB' THEN 
						SELECT idriadok INTO old_idriadok FROM food600.obra_buf WHERE  c_data=NEW.c_data AND c_stredisk=NEW.c_stredisk AND c_dokl=mc_dokl_pred AND c_karty=NEW.c_karty AND poradi=old_poradi AND mnozstvi>=0;
						IF old_idriadok IS NULL THEN
							SELECT zustatek, cena_prum, mnozstvi INTO moldzustatek, moldcena_prum, moldmnozstvi FROM food600.obra_buf WHERE c_data=NEW.c_data AND c_stredisk=NEW.c_stredisk AND c_dokl=mc_dokl_pred AND c_karty=NEW.c_karty AND poradi=old_poradi AND mnozstvi<0;
							IF zapisuj THEN
								insert into food600.test (cas,text) values (clock_timestamp(),'1I moldmnozstvi buranie minus '||moldmnozstvi);
								insert into food600.test (cas,text) values (clock_timestamp(),'1I moldzustatek buranie minus '||moldzustatek);
								insert into food600.test (cas,text) values (clock_timestamp(),'1I moldcena_prum buranie minus '||moldcena_prum);
							END IF;
						ELSE
							SELECT zustatek, cena_prum, mnozstvi INTO moldzustatek, moldcena_prum, moldmnozstvi FROM food600.obra_buf WHERE c_data=NEW.c_data AND c_stredisk=NEW.c_stredisk AND c_dokl=mc_dokl_pred AND c_karty=NEW.c_karty AND poradi=old_poradi AND mnozstvi>=0;
							IF zapisuj THEN
								insert into food600.test (cas,text) values (clock_timestamp(),'1I moldmnozstvi buranie plus '||moldmnozstvi);
								insert into food600.test (cas,text) values (clock_timestamp(),'1I moldzustatek buranie plus '||moldzustatek);
								insert into food600.test (cas,text) values (clock_timestamp(),'1I moldcena_prum buranie plus '||moldcena_prum);
							END IF;
						END IF;
					ELSE
						IF old_operace='VP' OR old_operace='PP' THEN 
							SELECT idriadok INTO old_idriadok FROM food600.obra_buf WHERE c_data=NEW.c_data AND c_stredisk=NEW.c_stredisk AND c_dokl=mc_dokl_pred AND c_karty=NEW.c_karty AND poradi=old_poradi AND mnozstvi>=0;
							IF old_idriadok IS NULL THEN
								SELECT zustatek, cena_prum, mnozstvi INTO moldzustatek, moldcena_prum, moldmnozstvi FROM food600.obra_buf WHERE c_data=NEW.c_data AND c_stredisk=NEW.c_stredisk AND c_dokl=mc_dokl_pred AND c_karty=NEW.c_karty AND poradi=old_poradi AND mnozstvi<0;
								IF zapisuj THEN
									insert into food600.test (cas,text) values (clock_timestamp(),'1I moldmnozstvi polotovary minus '||moldmnozstvi);
									insert into food600.test (cas,text) values (clock_timestamp(),'1I moldzustatek polotovary minus '||moldzustatek);
									insert into food600.test (cas,text) values (clock_timestamp(),'1I moldcena_prum polotovary minus '||moldcena_prum);
								END IF;
							ELSE
								SELECT zustatek, cena_prum, mnozstvi INTO moldzustatek, moldcena_prum, moldmnozstvi FROM food600.obra_buf WHERE c_data=NEW.c_data AND c_stredisk=NEW.c_stredisk AND c_dokl=mc_dokl_pred AND c_karty=NEW.c_karty AND poradi=old_poradi AND mnozstvi>=0;
								IF zapisuj THEN
									insert into food600.test (cas,text) values (clock_timestamp(),'1I moldmnozstvi polotovary plus '||moldmnozstvi);
									insert into food600.test (cas,text) values (clock_timestamp(),'1I moldzustatek polotovary plus '||moldzustatek);
									insert into food600.test (cas,text) values (clock_timestamp(),'1I moldcena_prum polotovary plus '||moldcena_prum);
								END IF;
							END IF;
						ELSE 
							SELECT zustatek, cena_prum, mnozstvi INTO moldzustatek, moldcena_prum, moldmnozstvi FROM food600.obra_buf WHERE c_data=NEW.c_data AND c_stredisk=NEW.c_stredisk AND c_dokl=mc_dokl_pred AND c_karty=NEW.c_karty AND poradi=old_poradi;
							IF zapisuj THEN
								insert into food600.test (cas,text) values (clock_timestamp(),'1I moldmnozstvi '||moldmnozstvi);
								insert into food600.test (cas,text) values (clock_timestamp(),'1I moldzustatek '||moldzustatek);
								insert into food600.test (cas,text) values (clock_timestamp(),'1I moldcena_prum '||moldcena_prum);
							END IF;
						END IF;
					END IF;
				END IF;
			END IF;
		ELSE
			IF NEW.operace='PC' and NEW.mnozstvi>=0 THEN
				SELECT zustatek, cena_prum, mnozstvi INTO moldzustatek, moldcena_prum, moldmnozstvi 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=old_poradi AND mnozstvi<0;
			ELSE 
				IF NEW.operace='PB' AND NEW.mnozstvi>=0 THEN
					IF NEW.poradi=old_poradi THEN
						SELECT zustatek, cena_prum, mnozstvi INTO moldzustatek, moldcena_prum, moldmnozstvi 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=old_poradi AND mnozstvi<0;
					ELSE
						-- Na tomto doklade bolo este jedno buranie tejto karty
						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=old_poradi AND mnozstvi>=0;
						IF old_idriadok IS NULL THEN
							SELECT zustatek, cena_prum, mnozstvi INTO moldzustatek, moldcena_prum, moldmnozstvi 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=old_poradi AND mnozstvi<0;
							IF zapisuj THEN
								insert into food600.test (cas,text) values (clock_timestamp(),'1I moldmnozstvi buranie minus '||moldmnozstvi);
								insert into food600.test (cas,text) values (clock_timestamp(),'1I moldzustatek buranie minus '||moldzustatek);
								insert into food600.test (cas,text) values (clock_timestamp(),'1I moldcena_prum buranie minus '||moldcena_prum);
							END IF;
						ELSE
							SELECT zustatek, cena_prum, mnozstvi INTO moldzustatek, moldcena_prum, moldmnozstvi 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=old_poradi AND mnozstvi>=0;
							IF zapisuj THEN
								insert into food600.test (cas,text) values (clock_timestamp(),'1I moldmnozstvi buranie plus '||moldmnozstvi);
								insert into food600.test (cas,text) values (clock_timestamp(),'1I moldzustatek buranie plus '||moldzustatek);
								insert into food600.test (cas,text) values (clock_timestamp(),'1I moldcena_prum buranie plus '||moldcena_prum);
							END IF;
						END IF;
					END IF;
				ELSE
					IF NEW.operace='PB' AND NEW.mnozstvi<0 THEN
						-- Na tomto doklade bolo este jedno buranie tejto karty
						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=old_poradi AND mnozstvi>=0;
						IF old_idriadok IS NULL THEN
							SELECT zustatek, cena_prum, mnozstvi INTO moldzustatek, moldcena_prum, moldmnozstvi 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=old_poradi AND mnozstvi<0;
							IF zapisuj THEN
								insert into food600.test (cas,text) values (clock_timestamp(),'1I moldmnozstvi buranie minus '||moldmnozstvi);
								insert into food600.test (cas,text) values (clock_timestamp(),'1I moldzustatek buranie minus '||moldzustatek);
								insert into food600.test (cas,text) values (clock_timestamp(),'1I moldcena_prum buranie minus '||moldcena_prum);
							END IF;
						ELSE
							SELECT zustatek, cena_prum, mnozstvi INTO moldzustatek, moldcena_prum, moldmnozstvi 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=old_poradi AND mnozstvi>=0;
							IF zapisuj THEN
								insert into food600.test (cas,text) values (clock_timestamp(),'1I moldmnozstvi buranie plus '||moldmnozstvi);
								insert into food600.test (cas,text) values (clock_timestamp(),'1I moldzustatek buranie plus '||moldzustatek);
								insert into food600.test (cas,text) values (clock_timestamp(),'1I moldcena_prum buranie plus '||moldcena_prum);
							END IF;
						END IF;
					ELSE
						IF NEW.operace='PP' AND buranierovnakych=1 THEN
							SELECT zustatek, cena_prum, mnozstvi INTO moldzustatek, moldcena_prum, moldmnozstvi 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=old_poradi AND mnozstvi<0;
						ELSE 
							SELECT zustatek, cena_prum, mnozstvi INTO moldzustatek, moldcena_prum, moldmnozstvi 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=old_poradi;
						END IF;
					END IF;
				END IF; 
			END IF;
			IF zapisuj THEN
				insert into food600.test (cas,text) values (clock_timestamp(),'1I Druhy vyskyt moldmnozstvi '||moldmnozstvi);
				insert into food600.test (cas,text) values (clock_timestamp(),'1I Druhy vyskyt moldzustatek '||moldzustatek);
				insert into food600.test (cas,text) values (clock_timestamp(),'1I Druhy vyskyt moldcena_prum '||moldcena_prum);
			END IF;
		END IF;
	END IF;
	IF mc_mena_old<>mc_mena THEN
		moldcena_prum:=ROUND(CAST(food600.prep_meny(mc_mena_old,mc_mena,moldcena_prum) AS NUMERIC),zaokruhlenie);
	END IF;
	IF moperace='PB' AND NEW.mnozstvi>0 AND NEW.cena_part=NEW.poradi THEN
		IF zapisuj THEN
			insert into food600.test (cas,text) values (clock_timestamp(),'1I NEW.cena pred prepoctom '||new.cena);
			insert into food600.test (cas,text) values (clock_timestamp(),'1I NEW.mnozstvi prep prepoctom '||new.mnozstvi);
		END IF;
		SELECT SUM(mnozstvi*cena_part),SUM(mnozstvi) INTO mprachy_pp_uz, mpocet_plusov_doteraz  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 AND idriadok<>NEW.idriadok LIMIT 1;
		IF zapisuj THEN
			IF mpocet_plusov_doteraz IS NULL THEN
				insert into food600.test (cas,text) values (clock_timestamp(),'1I PB+ pocet plusov doteraz = NULL');
			ELSE
				insert into food600.test (cas,text) values (clock_timestamp(),'1I PB+ pocet plusov doteraz '||mpocet_plusov_doteraz);
			END IF;
		END IF;
		IF mpocet_plusov_doteraz=0 OR mpocet_plusov_doteraz IS NULL   THEN
			IF zapisuj THEN
				insert into food600.test (cas,text) values (clock_timestamp(),'1I PB+ prvy plus');
			END IF;
			-- este nie je na doklade ziadny iny plus, teda zatial ide o buranie 1 k 1
			mprachy_pp_uz:=0.0000;
		ELSE
			IF zapisuj THEN
				insert into food600.test (cas,text) values (clock_timestamp(),'1I PB+ suma doteraz '||mprachy_pp_uz);
			END IF;
		END IF;
		mprachy_pp:=NEW.mnozstvi*NEW.cena;
		IF zapisuj THEN
			insert into food600.test (cas,text) values (clock_timestamp(),'1I PB+ suma aktualneho riadku '||mprachy_pp);
		END IF;
		mprachy_vv:=0.0000;
		SELECT SUM(-mnozstvi*cena) INTO mprachy_vv 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 zapisuj THEN
			insert into food600.test (cas,text) values (clock_timestamp(),'1I PB+ suma vydaja '||mprachy_vv);
		END IF;
		IF ROUND(CAST(mprachy_pp+mprachy_pp_uz AS NUMERIC),zaokruhlenie)<>ROUND(CAST(mprachy_vv AS NUMERIC),zaokruhlenie) THEN
			IF mpocet_plusov_doteraz=0 OR mpocet_plusov_doteraz IS NULL  THEN
				-- este nie je na doklade ziadny iny plus, teda zatial ide o buranie 1 k 1
				IF NEW.cena<>ROUND(CAST(mprachy_vv/NEW.mnozstvi AS NUMERIC),zaokruhlenie) THEN
					NEW.cena:=ROUND(CAST(mprachy_vv/NEW.mnozstvi AS NUMERIC),zaokruhlenie);
					IF zapisuj THEN
						insert into food600.test (cas,text) values (clock_timestamp(),'1I PB+ nova cena pre prvy plus '||new.cena);
					END IF;
				END IF;
			ELSE
				-- minimalne druhy plus v tomto doklade a poradi - buranie 1 ku x
				IF zapisuj THEN
					insert into food600.test (cas,text) values (clock_timestamp(),'1I PB+ dalsi plus');
				END IF;
				IF mprachy_vv<>0 THEN
					SELECT SUM(mnozstvi*cena) INTO mprachy_pp_uz 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 NEW.cena<>ROUND(CAST((mprachy_vv-mprachy_pp_uz)/NEW.mnozstvi AS NUMERIC),zaokruhlenie) THEN
						NEW.cena:=ROUND(CAST((mprachy_vv-mprachy_pp_uz)/NEW.mnozstvi AS NUMERIC),zaokruhlenie);
						IF zapisuj THEN
							insert into food600.test (cas,text) values (clock_timestamp(),'1I nova cena pre dalsi plus s nenulovym vydajom '||new.cena);
						END IF;
					END IF;
				ELSE
					UPDATE food600.obra_buf SET cena=0.0000  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;
					NEW.cena:=0.0000;
					IF zapisuj THEN
						insert into food600.test (cas,text) values (clock_timestamp(),'1I nova cena pre dalsi plus s nulovym vydajom '||new.cena);
					END IF;
				END IF;
			END IF;
		END IF;
	END IF;
	IF moperace='PP' AND NEW.mnozstvi>0 THEN
		insert into food600.test (cas,text) values (clock_timestamp(),'1I NEW.cena pred prepoctom '||new.cena);
		insert into food600.test (cas,text) values (clock_timestamp(),'1I NEW.mnozstvi pre prepoctom '||new.mnozstvi);
		mprachy_vv:=0.0000;
		SELECT SUM(-mnozstvi*cena) INTO mprachy_vv 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 zapisuj THEN
			insert into food600.test (cas,text) values (clock_timestamp(),'1I PP+ suma vydaja '||mprachy_vv);
		END IF;
		IF ROUND(CAST(NEW.cena*NEW.mnozstvi AS NUMERIC),4)<>ROUND(CAST(mprachy_vv AS NUMERIC),4) THEN
			IF NEW.cena<>ROUND(CAST(mprachy_vv/NEW.mnozstvi AS NUMERIC),zaokruhlenie) THEN
				NEW.cena:=ROUND(CAST(mprachy_vv/NEW.mnozstvi AS NUMERIC),zaokruhlenie);
				IF zapisuj THEN
					insert into food600.test (cas,text) values (clock_timestamp(),'1I PP+ nova cena pre pp '||new.cena);
				END IF;
			END IF;
		END IF;
	END IF;

	-- vypocet noveho zustatek a cena_prum podla operacie pri novom pohybe
	CASE
		WHEN moperace = 'PR' THEN
			mcena_prum:=ROUND(CAST(NEW.cena AS NUMERIC), zaokruhlenie);
			mzustatek:=ROUND(CAST(NEW.mnozstvi AS NUMERIC), zaokruhlenie_mn);
			NEW.cena_prum:=mcena_prum;
			NEW.zustatek:=mzustatek;
			NEW.zustatek:=ROUND(CAST(NEW.mnozstvi AS NUMERIC), zaokruhlenie_mn);
			IF zapisuj THEN
				insert into food600.test (cas,text) values (clock_timestamp(),'1I vypocty PR ');
			END IF;
		WHEN moperace = 'PI' THEN
			NEW.mnozstvi:=ROUND(CAST(NEW.zustatek-moldzustatek AS NUMERIC), zaokruhlenie_mn);
			NEW.cena:=moldcena_prum;
			NEW.cena_prum:=moldcena_prum;
			mzustatek:=ROUND(CAST(NEW.zustatek AS NUMERIC), zaokruhlenie_mn);
			mcena_prum:=moldcena_prum;
			IF zapisuj THEN
				insert into food600.test (cas,text) values (clock_timestamp(),'1I vypocty PI ');
			END IF;
		WHEN moperace='PC' AND NEW.mnozstvi<0 THEN
			NEW.cena:=moldcena_prum;
			NEW.cena_prum:=moldcena_prum;
			NEW.zustatek:=0;
			NEW.mnozstvi:=-moldzustatek;
			mzustatek:=0;
			mcena_prum:=moldcena_prum;
			SELECT idriadok INTO new_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_idriadok IS NULL THEN
			ELSE
				UPDATE food600.obra_buf SET mnozstvi=moldzustatek WHERE idriadok=new_idriadok;
			END IF;
			IF zapisuj THEN
				insert into food600.test (cas,text) values (clock_timestamp(),'1I vypocty PC- ');
			END IF;
		WHEN moperace='PC' AND NEW.mnozstvi>=0 THEN
			NEW.cena_prum:=ROUND(CAST(NEW.cena AS NUMERIC), zaokruhlenie);
			NEW.zustatek:=ROUND(CAST(NEW.mnozstvi AS NUMERIC), zaokruhlenie_mn);
			mzustatek:=ROUND(CAST(NEW.mnozstvi AS NUMERIC), zaokruhlenie_mn);
			mcena_prum:=ROUND(CAST(NEW.cena AS NUMERIC), zaokruhlenie);
			IF zapisuj THEN
				insert into food600.test (cas,text) values (clock_timestamp(),'1I vypocty PC+ ');
			END IF;
		WHEN NEW.mnozstvi<0 THEN
			NEW.cena:=moldcena_prum;
			NEW.cena_prum:=moldcena_prum;
			NEW.zustatek:=ROUND(CAST(moldzustatek+NEW.mnozstvi AS NUMERIC), zaokruhlenie_mn);
			mzustatek:=ROUND(CAST(NEW.zustatek AS NUMERIC), zaokruhlenie);
			mcena_prum:=moldcena_prum;
			IF zapisuj THEN
				insert into food600.test (cas,text) values (clock_timestamp(),'1I vypocty zaporne mnozstvi ');
			END IF;
		WHEN (NEW.mnozstvi + moldzustatek)<=0 THEN
			mcena_prum:=ROUND(CAST(NEW.cena AS NUMERIC), zaokruhlenie);
			NEW.cena_prum:=ROUND(CAST(NEW.cena AS NUMERIC), zaokruhlenie);
			NEW.zustatek:=ROUND(CAST(moldzustatek+NEW.mnozstvi AS NUMERIC), zaokruhlenie_mn);
			mzustatek:=NEW.zustatek;
			IF zapisuj THEN
				insert into food600.test (cas,text) values (clock_timestamp(),'1I vypocty zaporny alebo nulovy novy zustatek ');
			END IF;
		WHEN  moldzustatek<=0 THEN
			mcena_prum := ROUND(CAST(NEW.cena AS NUMERIC), zaokruhlenie);
			mzustatek:=ROUND(CAST(moldzustatek+NEW.mnozstvi AS NUMERIC), zaokruhlenie_mn);
			NEW.zustatek:=mzustatek;
			NEW.cena:=mcena_prum;
			NEW.cena_prum:=mcena_prum;
			IF zapisuj THEN
				insert into food600.test (cas,text) values (clock_timestamp(),'1I vypocty zaporny albo nulovy stary zustatek ');
			END IF;
		ELSE
			IF zapisuj THEN
				insert into food600.test (cas,text) values (clock_timestamp(),'1I vypocty ostatne ');
			END IF;
			mcena_prum:=ROUND(CAST((moldzustatek*moldcena_prum+NEW.mnozstvi*NEW.cena)/(NEW.mnozstvi+moldzustatek) AS DECIMAL(20,10)),zaokruhlenie);
			IF mcena_prum > 99999999 THEN
				mcena_prum := 99999999.9999;
			END IF;
			mzustatek:=ROUND(CAST(moldzustatek+NEW.mnozstvi AS NUMERIC), zaokruhlenie_mn);
			NEW.zustatek:=mzustatek;
			NEW.cena_prum:=mcena_prum;
	END CASE;
	IF zapisuj THEN
		IF old_operace IS NULL THEN
			insert into food600.test (cas,text) values (clock_timestamp(),'1I mc_dokl_pred=NULL');
		ELSE
			insert into food600.test (cas,text) values (clock_timestamp(),'1I mc_dokl_pred '||mc_dokl_pred);
		END IF;
		insert into food600.test (cas,text) values (clock_timestamp(),'1I moldzustatek '||moldzustatek);
		insert into food600.test (cas,text) values (clock_timestamp(),'1I moldcena_prum '||moldcena_prum);
		insert into food600.test (cas,text) values (clock_timestamp(),'1I NEW.cena '||new.cena);
		insert into food600.test (cas,text) values (clock_timestamp(),'1I NEW.cena_prum '||new.cena_prum);
		insert into food600.test (cas,text) values (clock_timestamp(),'1I NEW.mnozstvi '||new.mnozstvi);
		insert into food600.test (cas,text) values (clock_timestamp(),'1I NEW.zustatek '||new.zustatek);
	END IF;
END IF;
RETURN NEW;
END;
$BODY$;
