CREATE OR REPLACE FUNCTION food600.food_dat_zmena()
    RETURNS trigger
    LANGUAGE 'plpgsql'
    COST 100
    VOLATILE NOT LEAKPROOF
AS $BODY$
DECLARE
ret CHARACTER VARYING;
ret_master CHARACTER VARYING;
polia CHARACTER VARYING DEFAULT '';
polia_master CHARACTER VARYING DEFAULT '';
hodnoty CHARACTER VARYING DEFAULT '';
hodnoty_master CHARACTER VARYING DEFAULT '';
pocetzmien INTEGER DEFAULT 0;
robsync SMALLINT DEFAULT 0;
mp1 CHARACTER VARYING DEFAULT '';
mp2 CHARACTER VARYING DEFAULT '';
mp3 CHARACTER VARYING DEFAULT '';
mp4 CHARACTER VARYING DEFAULT '';
mp5 CHARACTER VARYING DEFAULT '';
mp6 CHARACTER VARYING DEFAULT '';
mp1_master CHARACTER VARYING DEFAULT '';
mp2_master CHARACTER VARYING DEFAULT '';
mp3_master CHARACTER VARYING DEFAULT '';
mp4_master CHARACTER VARYING DEFAULT '';
mp5_master CHARACTER VARYING DEFAULT '';
mp6_master CHARACTER VARYING DEFAULT '';
mojeid INTEGER;
mojtyp INTEGER;
mojmaster INTEGER;
sommaster INTEGER DEFAULT 0;
posunhore INTEGER DEFAULT 0;
idnazapis_slavom INTEGER;
idnazapis_mastrovi INTEGER;
BEGIN
SELECT idriadok, id_master INTO mojeid, mojmaster  FROM food600.sync_set WHERE tosomja>0;
IF mojeid IS NULL THEN
	--- V nastavovacej tabulke sa nenachadza moj zaznam, synchronizacia sa nerobi
	mojeid:=0;
	robsync:=0;
	mojmaster:=0;
	sommaster:=0;
ELSE 
	--- V nastavovacej tabulke sa nenachadza moj zaznam, nejako je vyplneny aj mojmaster
	SELECT idriadok INTO sommaster FROM food600.sync_set WHERE tosomja=0 AND id_master=mojeid;
	--- Zistim, ci som master pre nejaky iny server
	IF sommaster IS NULL THEN
		sommaster:=0;
	END IF;
	robsync:=sommaster+mojmaster;
	--- Synchronizaciu robim ak som master, alebo mam mastera
	IF robsync > 0 THEN 
		--- Este treba overit, ci tuto tabulku vobec treba synchronizovat
		SELECT smer INTO robsync FROM food600.sync_tables WHERE LOWER(BTRIM(tabulka))=LOWER(TG_TABLE_NAME);
		IF robsync IS NULL THEN
			robsync:=0;
		END IF;
	END IF;
END IF;       
IF (TG_OP = 'UPDATE') THEN
	IF robsync>0 THEN
		IF NEW.id_sync IS NULL OR NEW.id_sync=0 THEN
			--- Ak nebol vyplneny id_sync, islo o zaznam, ktory vznikol na tomto PC (z FoodManu)
			idnazapis_slavom:=mojeid;
			idnazapis_mastrovi:=mojeid;
		ELSE
			--- Ak bol id_sync vyplneny, musel zaznam prist od slave alebo od mastera. Dalsim slavom budem posuvat originalne id, aby si povodny autor zaznamu uz tuto zmenu nemusel stahovat
			idnazapis_slavom:=NEW.id_sync;
			IF mojmaster>0 THEN
				IF idnazapis_slavom IN (SELECT idriadok FROM food600.sync_set WHERE id_master=mojeid) THEN
					idnazapis_mastrovi:=mojeid;
				ELSE
					idnazapis_mastrovi:=idnazapis_slavom;
				END IF;
			END IF;
		END IF;
		--- Mastrovi sa posuvaju moje zaznamy a zaznamy mojich slavov a posuvam mu moje id (nevracaju sa nazad zaznamy, ktore prisli zhora)
		IF mojmaster>0 THEN
			IF mojeid=idnazapis_slavom THEN
				posunhore:=1;
			ELSE
				IF idnazapis_slavom IN (SELECT idriadok FROM food600.sync_set WHERE id_master=mojeid) THEN
					posunhore:=1;
				ELSE
					posunhore:=0;
				END IF;
			END IF;
		ELSE
			posunhore:=0;
		END IF;
		--- id_sync na riadok aj tak nezapisem
		NEW.id_sync:=0;
		ret:='UPDATE food600.food_dat SET ';
		ret_master:=ret;
		SELECT * FROM food600.updatepolastring(OLD.id, NEW.id, pocetzmien, ret, ret_master, 'id') INTO pocetzmien, ret, ret_master;
		SELECT * FROM food600.updatepolastring(OLD.id_menu, NEW.id_menu, pocetzmien, ret, ret_master, 'id_menu') INTO pocetzmien, ret, ret_master;
		SELECT * FROM food600.updatepolastring(OLD.pgm, NEW.pgm, pocetzmien, ret, ret_master, 'pgm') INTO pocetzmien, ret, ret_master;
		SELECT * FROM food600.updatepolastring(OLD.pgm_param, NEW.pgm_param, pocetzmien, ret, ret_master, 'pgm_param') INTO pocetzmien, ret, ret_master;
		SELECT * FROM food600.updatepolastring(OLD.pgm_path, NEW.pgm_path, pocetzmien, ret, ret_master, 'pgm_path') INTO pocetzmien, ret, ret_master;
		SELECT * FROM food600.updatepolastring(OLD.dat_path, NEW.dat_path, pocetzmien, ret, ret_master, 'dat_path') INTO pocetzmien, ret, ret_master;
		SELECT * FROM food600.updatepolastring(OLD.cen_path, NEW.cen_path, pocetzmien, ret, ret_master, 'cen_path') INTO pocetzmien, ret, ret_master;
		SELECT * FROM food600.updatepolanumeric(btrim(OLD.c_data::CHARACTER(20)), btrim(NEW.c_data::CHARACTER(20)), pocetzmien, ret, ret_master, 'c_data') INTO pocetzmien, ret, ret_master;
		SELECT * FROM food600.updatepolastring(OLD.id_zkratka, NEW.id_zkratka, pocetzmien, ret, ret_master, 'id_zkratka') INTO pocetzmien, ret, ret_master;
		SELECT * FROM food600.updatepolanumeric(btrim(OLD.c_stredisk::CHARACTER(20)), btrim(NEW.c_stredisk::CHARACTER(20)), pocetzmien, ret, ret_master, 'c_stredisk') INTO pocetzmien, ret, ret_master;
		SELECT * FROM food600.updatepolanumeric(btrim(OLD.priority::CHARACTER(20)), btrim(NEW.priority::CHARACTER(20)), pocetzmien, ret, ret_master, 'priority') INTO pocetzmien, ret, ret_master;
		SELECT * FROM food600.updatepolastring(OLD.skupina, NEW.skupina, pocetzmien, ret, ret_master, 'skupina') INTO pocetzmien, ret, ret_master;
		SELECT * FROM food600.updatepolastring(OLD.comment, NEW.comment, pocetzmien, ret, ret_master, 'comment') INTO pocetzmien, ret, ret_master;
		SELECT * FROM food600.updatepolastring(OLD.bour_skup, NEW.bour_skup, pocetzmien, ret, ret_master, 'bour_skup') INTO pocetzmien, ret, ret_master;
		SELECT * FROM food600.updatepolastring(OLD.setup_skup, NEW.setup_skup, pocetzmien, ret, ret_master, 'setup_skup') INTO pocetzmien, ret, ret_master;
		SELECT * FROM food600.updatepolastring(OLD.prava_skup, NEW.prava_skup, pocetzmien, ret, ret_master, 'prava_skup') INTO pocetzmien, ret, ret_master;
		SELECT * FROM food600.updatepolastring(OLD.stredskup, NEW.stredskup, pocetzmien, ret, ret_master, 'stredskup') INTO pocetzmien, ret, ret_master;
		SELECT * FROM food600.updatepolastring(OLD.a1, NEW.a1, pocetzmien, ret, ret_master, 'a1') INTO pocetzmien, ret, ret_master;
		SELECT * FROM food600.updatepolastring(OLD.a2, NEW.a2, pocetzmien, ret, ret_master, 'a2') INTO pocetzmien, ret, ret_master;
		SELECT * FROM food600.updatepolastring(OLD.a3, NEW.a3, pocetzmien, ret, ret_master, 'a3') INTO pocetzmien, ret, ret_master;
		SELECT * FROM food600.updatepolastring(OLD.a4, NEW.a4, pocetzmien, ret, ret_master, 'a4') INTO pocetzmien, ret, ret_master;
		SELECT * FROM food600.updatepolastring(OLD.a5, NEW.a5, pocetzmien, ret, ret_master, 'a5') INTO pocetzmien, ret, ret_master;
		SELECT * FROM food600.updatepolastring(OLD.a6, NEW.a6, pocetzmien, ret, ret_master, 'a6') INTO pocetzmien, ret, ret_master;
		SELECT * FROM food600.updatepolastring(OLD.a7, NEW.a7, pocetzmien, ret, ret_master, 'a7') INTO pocetzmien, ret, ret_master;
		SELECT * FROM food600.updatepolastring(OLD.a8, NEW.a8, pocetzmien, ret, ret_master, 'a8') INTO pocetzmien, ret, ret_master;
		SELECT * FROM food600.updatepolastring(OLD.a9, NEW.a9, pocetzmien, ret, ret_master, 'a9') INTO pocetzmien, ret, ret_master;
		SELECT * FROM food600.updatepolastring(OLD.b1, NEW.b1, pocetzmien, ret, ret_master, 'b1') INTO pocetzmien, ret, ret_master;
		SELECT * FROM food600.updatepolastring(OLD.b2, NEW.b2, pocetzmien, ret, ret_master, 'b2') INTO pocetzmien, ret, ret_master;
		SELECT * FROM food600.updatepolastring(OLD.b3, NEW.b3, pocetzmien, ret, ret_master, 'b3') INTO pocetzmien, ret, ret_master;
		SELECT * FROM food600.updatepolastring(OLD.b4, NEW.b4, pocetzmien, ret, ret_master, 'b4') INTO pocetzmien, ret, ret_master;
		SELECT * FROM food600.updatepolastring(OLD.b5, NEW.b5, pocetzmien, ret, ret_master, 'b5') INTO pocetzmien, ret, ret_master;
		SELECT * FROM food600.updatepolastring(OLD.b6, NEW.b6, pocetzmien, ret, ret_master, 'b6') INTO pocetzmien, ret, ret_master;
		SELECT * FROM food600.updatepolastring(OLD.b7, NEW.b7, pocetzmien, ret, ret_master, 'b7') INTO pocetzmien, ret, ret_master;
		SELECT * FROM food600.updatepolastring(OLD.b8, NEW.b8, pocetzmien, ret, ret_master, 'b8') INTO pocetzmien, ret, ret_master;
		SELECT * FROM food600.updatepolastring(OLD.b9, NEW.b9, pocetzmien, ret, ret_master, 'b9') INTO pocetzmien, ret, ret_master;
		SELECT * FROM food600.updatepolastring(OLD.c1, NEW.c1, pocetzmien, ret, ret_master, 'c1') INTO pocetzmien, ret, ret_master;
		SELECT * FROM food600.updatepolastring(OLD.c2, NEW.c2, pocetzmien, ret, ret_master, 'c2') INTO pocetzmien, ret, ret_master;
		SELECT * FROM food600.updatepolastring(OLD.c3, NEW.c3, pocetzmien, ret, ret_master, 'c3') INTO pocetzmien, ret, ret_master;
		SELECT * FROM food600.updatepolastring(OLD.c4, NEW.c4, pocetzmien, ret, ret_master, 'c4') INTO pocetzmien, ret, ret_master;
		SELECT * FROM food600.updatepolastring(OLD.c5, NEW.c5, pocetzmien, ret, ret_master, 'c5') INTO pocetzmien, ret, ret_master;
		SELECT * FROM food600.updatepolastring(OLD.c6, NEW.c6, pocetzmien, ret, ret_master, 'c6') INTO pocetzmien, ret, ret_master;
		SELECT * FROM food600.updatepolastring(OLD.c7, NEW.c7, pocetzmien, ret, ret_master, 'c7') INTO pocetzmien, ret, ret_master;
		SELECT * FROM food600.updatepolastring(OLD.c8, NEW.c8, pocetzmien, ret, ret_master, 'c8') INTO pocetzmien, ret, ret_master;
		SELECT * FROM food600.updatepolastring(OLD.c9, NEW.c9, pocetzmien, ret, ret_master, 'c9') INTO pocetzmien, ret, ret_master;
		SELECT * FROM food600.updatepolastring(OLD.id_klapky, NEW.id_klapky, pocetzmien, ret, ret_master, 'id_klapky') INTO pocetzmien, ret, ret_master;
		SELECT * FROM food600.updatepolastring(OLD.pozadi, NEW.pozadi, pocetzmien, ret, ret_master, 'pozadi') INTO pocetzmien, ret, ret_master;
		SELECT * FROM food600.updatepolastring(OLD.defa_prn, NEW.defa_prn, pocetzmien, ret, ret_master, 'defa_prn') INTO pocetzmien, ret, ret_master;
		SELECT * FROM food600.updatepolastring(OLD.zadan_prn, NEW.zadan_prn, pocetzmien, ret, ret_master, 'zadan_prn') INTO pocetzmien, ret, ret_master;
		SELECT * FROM food600.updatepolastring(OLD.era_cen, NEW.era_cen, pocetzmien, ret, ret_master, 'era_cen') INTO pocetzmien, ret, ret_master;
		SELECT * FROM food600.updatepolanumeric(btrim(OLD.typ_inst::CHARACTER(20)), btrim(NEW.typ_inst::CHARACTER(20)), pocetzmien, ret, ret_master, 'typ_inst') INTO pocetzmien, ret, ret_master;
		SELECT * FROM food600.updatepolanumeric(btrim(OLD.kont_dbf::CHARACTER(20)), btrim(NEW.kont_dbf::CHARACTER(20)), pocetzmien, ret, ret_master, 'kont_dbf') INTO pocetzmien, ret, ret_master;
		SELECT * FROM food600.updatepolastring(OLD.sankcnicen, NEW.sankcnicen, pocetzmien, ret, ret_master, 'sankcnicen') INTO pocetzmien, ret, ret_master;
		SELECT * FROM food600.updatepolastring(OLD.master_kas, NEW.master_kas, pocetzmien, ret, ret_master, 'master_kas') INTO pocetzmien, ret, ret_master;
		SELECT * FROM food600.updatepolanumeric(btrim(OLD.plateny::CHARACTER(20)), btrim(NEW.plateny::CHARACTER(20)), pocetzmien, ret, ret_master, 'plateny') INTO pocetzmien, ret, ret_master;
		SELECT * FROM food600.updatepolanumeric(btrim(OLD.servis::CHARACTER(20)), btrim(NEW.servis::CHARACTER(20)), pocetzmien, ret, ret_master, 'servis') INTO pocetzmien, ret, ret_master;
		SELECT * FROM food600.updatepoladate(OLD.datinst, NEW.datinst, pocetzmien, ret, ret_master, 'datinst') INTO pocetzmien, ret, ret_master;
		SELECT * FROM food600.updatepolastring(OLD.edi_kod, NEW.edi_kod, pocetzmien, ret, ret_master, 'edi_kod') INTO pocetzmien, ret, ret_master;
		SELECT * FROM food600.updatepolastring(OLD.id_prev, NEW.id_prev, pocetzmien, ret, ret_master, 'id_prev') INTO pocetzmien, ret, ret_master;
		SELECT * FROM food600.updatepolastring(OLD.prevadzka, NEW.prevadzka, pocetzmien, ret, ret_master, 'prevadzka') INTO pocetzmien, ret, ret_master;
		SELECT * FROM food600.updatepolastring(OLD.eet_dic, NEW.eet_dic, pocetzmien, ret, ret_master, 'eet_dic') INTO pocetzmien, ret, ret_master;
		SELECT * FROM food600.updatepolastring(OLD.eet_dic2, NEW.eet_dic2, pocetzmien, ret, ret_master, 'eet_dic2') INTO pocetzmien, ret, ret_master;
		SELECT * FROM food600.updatepolastring(OLD.eet_provoz, NEW.eet_provoz, pocetzmien, ret, ret_master, 'eet_provoz') INTO pocetzmien, ret, ret_master;
		SELECT * FROM food600.updatepolastring(OLD.eet_kasa, NEW.eet_kasa, pocetzmien, ret, ret_master, 'eet_kasa') INTO pocetzmien, ret, ret_master;
		SELECT * FROM food600.updatepolastring(OLD.eet_url, NEW.eet_url, pocetzmien, ret, ret_master, 'eet_url') INTO pocetzmien, ret, ret_master;
		SELECT * FROM food600.updatepolastring(OLD.eet_url2, NEW.eet_url2, pocetzmien, ret, ret_master, 'eet_url2') INTO pocetzmien, ret, ret_master;
		SELECT * FROM food600.updatepolanumeric(btrim(OLD.id_ediprev::CHARACTER(20)), btrim(NEW.id_ediprev::CHARACTER(20)), pocetzmien, ret, ret_master, 'id_ediprev') INTO pocetzmien, ret, ret_master;
		IF pocetzmien>0 THEN
			IF mojmaster>0 AND posunhore>0 THEN 
				ret:=ret||', id_sync='||btrim(idnazapis_mastrovi::CHARACTER(20));
				IF OLD.idmaster IS NOT NULL AND btrim(OLD.idmaster)<>'' THEN
					ret:=ret||' WHERE id='''||OLD.idmaster||''';';
				ELSE
					ret:=ret||' WHERE id=''%P1%''';
					mp1:='getidfooddatmaster('''||OLD.id||''')';
				END IF;
				INSERT INTO food600.sync_idriadok (prikaz, tabulka, idriadok, operacia, nazev, p1, p2, p3, p4, p5, p6, id_sync, is_master) VALUES (ret, 'food_dat', 0, 'U', '', mp1, mp2, mp3, mp4, mp5, mp6, idnazapis_mastrovi, 0);
			END IF;
			IF sommaster>0 THEN 
				ret_master:=ret_master||', id_sync='||btrim(idnazapis_slavom::CHARACTER(20));
				ret_master:=ret_master||' WHERE idmaster='''||OLD.id||''';';
				INSERT INTO food600.sync_idriadok (prikaz, tabulka, idriadok, operacia, nazev, p1, p2, p3, p4, p5, p6, id_sync, is_master) VALUES (ret_master, 'food_dat', 0, 'U', '', mp1_master, mp2_master, mp3_master, mp4_master, mp5_master, mp6_master, idnazapis_slavom, 1);
			END IF;
		END IF;
	END IF;
	RETURN NEW;
ELSIF (TG_OP = 'INSERT') THEN
	IF robsync>0  THEN
		IF NEW.id_sync IS NULL OR NEW.id_sync=0 THEN
			--- Ak nebol vyplneny id_sync, islo o zaznam, ktory vznikol na tomto PC (z FoodManu)
			idnazapis_slavom:=mojeid;
			idnazapis_mastrovi:=mojeid;
		ELSE
			--- Ak bol id_sync vyplneny, musel zaznam prist od slave alebo od mastera. Dalsim slavom budem posuvat originalne id, aby si povodny autor zaznamu uz tuto zmenu nemusel stahovat
			idnazapis_slavom:=NEW.id_sync;
			IF mojmaster>0 THEN
				IF idnazapis_slavom IN (SELECT idriadok FROM food600.sync_set WHERE id_master=mojeid) THEN
					idnazapis_mastrovi:=mojeid;
				ELSE
					idnazapis_mastrovi:=idnazapis_slavom;
				END IF;
			END IF;
		END IF;
		--- Mastrovi sa posuvaju moje zaznamy a zaznamy mojich slavov a posuvam mu moje id (nevracaju sa nazad zaznamy, ktore prisli zhora)
		IF mojmaster>0 THEN
			IF mojeid=idnazapis_slavom THEN
				posunhore:=1;
			ELSE
				IF idnazapis_slavom IN (SELECT idriadok FROM food600.sync_set WHERE id_master=mojeid) THEN
					posunhore:=1;
				ELSE
					posunhore:=0;
				END IF;
			END IF;
		ELSE
			posunhore:=0;
		END IF;
		--- id_sync na riadok aj tak nezapisem
		NEW.id_sync:=0;
		ret:='INSERT INTO food600.food_dat (';
		ret_master:=ret;
		SELECT * FROM food600.insertpolastring(NEW.id, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'id') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;
		SELECT * FROM food600.insertpolastring(NEW.id_menu, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'id_menu') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;
		SELECT * FROM food600.insertpolastring(NEW.pgm, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'pgm') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;
		SELECT * FROM food600.insertpolastring(NEW.pgm_param, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'pgm_param') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;
		SELECT * FROM food600.insertpolastring(NEW.pgm_path, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'pgm_path') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;
		SELECT * FROM food600.insertpolastring(NEW.dat_path, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'dat_path') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;
		SELECT * FROM food600.insertpolastring(NEW.cen_path, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'cen_path') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;
		SELECT * FROM food600.insertpolanumeric(NEW.c_data, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'c_data') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;
		SELECT * FROM food600.insertpolastring(NEW.id_zkratka, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'id_zkratka') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;
		SELECT * FROM food600.insertpolanumeric(NEW.c_stredisk, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'c_stredisk') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;
		SELECT * FROM food600.insertpolanumeric(NEW.priority, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'priority') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;
		SELECT * FROM food600.insertpolastring(NEW.skupina, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'skupina') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;
		SELECT * FROM food600.insertpolastring(NEW.comment, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'comment') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;
		SELECT * FROM food600.insertpolastring(NEW.bour_skup, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'bour_skup') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;
		SELECT * FROM food600.insertpolastring(NEW.setup_skup, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'setup_skup') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;
		SELECT * FROM food600.insertpolastring(NEW.prava_skup, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'prava_skup') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;
		SELECT * FROM food600.insertpolastring(NEW.stredskup, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'stredskup') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;
		SELECT * FROM food600.insertpolastring(NEW.a1, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'a1') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;
		SELECT * FROM food600.insertpolastring(NEW.a2, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'a2') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;
		SELECT * FROM food600.insertpolastring(NEW.a3, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'a3') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;
		SELECT * FROM food600.insertpolastring(NEW.a4, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'a4') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;
		SELECT * FROM food600.insertpolastring(NEW.a5, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'a5') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;
		SELECT * FROM food600.insertpolastring(NEW.a6, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'a6') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;
		SELECT * FROM food600.insertpolastring(NEW.a7, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'a7') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;
		SELECT * FROM food600.insertpolastring(NEW.a8, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'a8') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;
		SELECT * FROM food600.insertpolastring(NEW.a9, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'a9') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;
		SELECT * FROM food600.insertpolastring(NEW.b1, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'b1') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;
		SELECT * FROM food600.insertpolastring(NEW.b2, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'b2') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;
		SELECT * FROM food600.insertpolastring(NEW.b3, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'b3') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;
		SELECT * FROM food600.insertpolastring(NEW.b4, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'b4') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;
		SELECT * FROM food600.insertpolastring(NEW.b5, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'b5') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;
		SELECT * FROM food600.insertpolastring(NEW.b6, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'b6') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;
		SELECT * FROM food600.insertpolastring(NEW.b7, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'b7') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;
		SELECT * FROM food600.insertpolastring(NEW.b8, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'b8') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;
		SELECT * FROM food600.insertpolastring(NEW.b9, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'b9') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;
		SELECT * FROM food600.insertpolastring(NEW.c1, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'c1') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;
		SELECT * FROM food600.insertpolastring(NEW.c2, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'c2') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;
		SELECT * FROM food600.insertpolastring(NEW.c3, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'c3') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;
		SELECT * FROM food600.insertpolastring(NEW.c4, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'c4') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;
		SELECT * FROM food600.insertpolastring(NEW.c5, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'c5') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;
		SELECT * FROM food600.insertpolastring(NEW.c6, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'c6') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;
		SELECT * FROM food600.insertpolastring(NEW.c7, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'c7') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;
		SELECT * FROM food600.insertpolastring(NEW.c8, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'c8') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;
		SELECT * FROM food600.insertpolastring(NEW.c9, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'c9') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;
		SELECT * FROM food600.insertpolastring(NEW.id_klapky, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'id_klapky') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;
		SELECT * FROM food600.insertpolastring(NEW.pozadi, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'pozadi') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;
		SELECT * FROM food600.insertpolastring(NEW.defa_prn, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'defa_prn') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;
		SELECT * FROM food600.insertpolastring(NEW.zadan_prn, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'zadan_prn') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;
		SELECT * FROM food600.insertpolastring(NEW.era_cen, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'era_cen') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;
		SELECT * FROM food600.insertpolanumeric(NEW.typ_inst, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'typ_inst') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;
		SELECT * FROM food600.insertpolanumeric(NEW.kont_dbf, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'kont_dbf') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;
		SELECT * FROM food600.insertpolastring(NEW.sankcnicen, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'sankcnicen') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;
		SELECT * FROM food600.insertpolastring(NEW.master_kas, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'master_kas') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;
		SELECT * FROM food600.insertpolanumeric(NEW.plateny, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'plateny') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;
		SELECT * FROM food600.insertpolanumeric(NEW.servis, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'servis') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;
		SELECT * FROM food600.insertpoladate(NEW.datinst, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'datinst') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;
		SELECT * FROM food600.insertpolastring(NEW.edi_kod, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'edi_kod') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;
		SELECT * FROM food600.insertpolastring(NEW.id_prev, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'id_prev') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;
		SELECT * FROM food600.insertpolastring(NEW.prevadzka, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'prevadzka') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;
		SELECT * FROM food600.insertpolastring(NEW.eet_dic, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'eet_dic') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;
		SELECT * FROM food600.insertpolastring(NEW.eet_dic2, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'eet_dic2') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;
		SELECT * FROM food600.insertpolastring(NEW.eet_provoz, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'eet_provoz') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;
		SELECT * FROM food600.insertpolastring(NEW.eet_kasa, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'eet_kasa') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;
		SELECT * FROM food600.insertpolastring(NEW.eet_url, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'eet_url') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;
		SELECT * FROM food600.insertpolastring(NEW.eet_url2, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'eet_url2') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;
		SELECT * FROM food600.insertpolanumeric(NEW.id_ediprev, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'id_ediprev') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;
		IF pocetzmien>0 THEN
			polia:=polia||',';
			hodnoty:=hodnoty||',';
			polia_master:=polia_master||',';
			hodnoty_master:=hodnoty_master||',';
			IF mojmaster>0 AND posunhore>0 THEN 
				polia:=polia||'id_sync';
				hodnoty:=hodnoty||btrim(idnazapis_mastrovi::CHARACTER(20));
				pocetzmien:=pocetzmien+1;
				ret:=ret||polia||') VALUES ('||hodnoty||') RETURNING id;';
				INSERT INTO food600.sync_idriadok (prikaz, tabulka, idriadok, operacia, nazev, p1, p2, p3, p4, p5, p6, id_sync, is_master, poradie) VALUES (ret,'food_dat', 0, 'I', NEW.id, mp1, mp2, mp3, mp4, mp5, mp6, idnazapis_mastrovi, 0, 99);
			END IF;
			IF sommaster>0 THEN 
				polia_master:=polia_master||'id_sync';
				hodnoty_master:=hodnoty_master||btrim(idnazapis_slavom::CHARACTER(20));
				pocetzmien:=pocetzmien+1;
				ret_master:=ret_master||polia_master||') VALUES ('||hodnoty_master||') RETURNING id;';
				INSERT INTO food600.sync_idriadok (prikaz, tabulka, idriadok, operacia, nazev, p1, p2, p3, p4, p5, p6, id_sync, is_master, poradie) VALUES (ret_master, 'food_dat', NEW.idriadok, 'I', NEW.id, mp1_master, mp2_master, mp3_master, mp4_master, mp5_master, mp6_master, idnazapis_slavom, 1, 99);
			END IF;
		END IF;
	END IF;
	RETURN NEW;
ELSIF (TG_OP = 'DELETE') THEN
	IF robsync>0 THEN
		ret:='DELETE FROM food600.food_dat WHERE id=''';
		ret_master:='DELETE FROM food600.food_dat WHERE idmaster=''';
		IF mojmaster>0 THEN 
			IF (btrim(OLD.idmaster)<>'') THEN
				ret:=ret||OLD.idmaster;
			ELSE
				ret:=ret||'%P1%';
				mp1:='getidfooddatmaster('''||OLD.id||''')';
			END IF;	
			INSERT INTO food600.sync_idriadok (prikaz, tabulka, idriadok, operacia, nazev, p1, p2, p3, p4, p5, p6, id_sync, is_master) VALUES (ret, 'food_dat', 0, 'D', '', mp1, mp2, mp3, mp4, mp5, mp6, mojeid, 0);
		END IF;
		IF sommaster>0 THEN 
			ret_master:=ret_master||OLD.id||'';
			INSERT INTO food600.sync_idriadok (prikaz, tabulka, idriadok, operacia, nazev, p1, p2, p3, p4, p5, p6, id_sync, is_master) VALUES (ret_master, 'food_dat', OLD.idriadok, 'D', '', '', '', '', '', '', '', mojeid, 1);
		END IF;
	END IF;
	RETURN OLD;
END IF;
        
END;
$BODY$;
