CREATE OR REPLACE FUNCTION food600.h10_set_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.h10_set SET ';
		ret_master:=ret;
		IF (NEW.c_druh != OLD.c_druh) THEN
			IF pocetzmien>0 THEN
				ret:=ret||',';
				ret_master:=ret_master||',';
			END IF;
			IF NEW.c_druh>0 THEN
				ret:=ret||'c_druh=%P3%';
				mp3:='getcdruhmaster('||btrim(new.c_druh::CHARACTER(20))||')';
				ret_master:=ret_master||'c_druh=%P3%';
				mp3_master:='getcdruhslave('||btrim(new.c_druh::CHARACTER(20))||')';
			ELSE
				ret:=ret||'c_druh=0';
				ret_master:=ret_master||'c_druh=0';
			END IF;
			pocetzmien:=pocetzmien+1;
		END IF;		
		IF (NEW.id_hores != OLD.id_hores) THEN
			IF pocetzmien>0 THEN
				ret:=ret||',';
				ret_master:=ret_master||',';
			END IF;
			IF NEW.c_druh>0 THEN
				ret:=ret||'id_hores=%P2%';
				mp2:='getidhoresmaster('||btrim(new.id_hores::CHARACTER(20))||')';
				ret_master:=ret_master||'id_hores=%P2%';
				mp2_master:='getidhoresslave('||btrim(new.id_hores::CHARACTER(20))||')';
			ELSE
				ret:=ret||'id_hores=0';
				ret_master:=ret_master||'id_hores=0';
			END IF;
			pocetzmien:=pocetzmien+1;
		END IF;	
		SELECT * FROM food600.updatepolanumeric(btrim(OLD.id1::CHARACTER(20)), btrim(NEW.id1::CHARACTER(20)), pocetzmien, ret, ret_master, 'id1') INTO pocetzmien, ret, ret_master;
		SELECT * FROM food600.updatepolanumeric(btrim(OLD.id2::CHARACTER(20)), btrim(NEW.id2::CHARACTER(20)), pocetzmien, ret, ret_master, 'id2') INTO pocetzmien, ret, ret_master;
		SELECT * FROM food600.updatepolanumeric(btrim(OLD.id3::CHARACTER(20)), btrim(NEW.id3::CHARACTER(20)), pocetzmien, ret, ret_master, 'id3') INTO pocetzmien, ret, ret_master;
		SELECT * FROM food600.updatepolanumeric(btrim(OLD.id4::CHARACTER(20)), btrim(NEW.id4::CHARACTER(20)), pocetzmien, ret, ret_master, 'id4') INTO pocetzmien, ret, ret_master;
		SELECT * FROM food600.updatepolanumeric(btrim(OLD.id5::CHARACTER(20)), btrim(NEW.id5::CHARACTER(20)), pocetzmien, ret, ret_master, 'id5') INTO pocetzmien, ret, ret_master;
		SELECT * FROM food600.updatepolanumeric(btrim(OLD.id6::CHARACTER(20)), btrim(NEW.id6::CHARACTER(20)), pocetzmien, ret, ret_master, 'id6') INTO pocetzmien, ret, ret_master;
		SELECT * FROM food600.updatepolanumeric(btrim(OLD.id7::CHARACTER(20)), btrim(NEW.id7::CHARACTER(20)), pocetzmien, ret, ret_master, 'id7') INTO pocetzmien, ret, ret_master;
		SELECT * FROM food600.updatepolanumeric(btrim(OLD.id8::CHARACTER(20)), btrim(NEW.id8::CHARACTER(20)), pocetzmien, ret, ret_master, 'id8') INTO pocetzmien, ret, ret_master;
		SELECT * FROM food600.updatepolanumeric(btrim(OLD.id9::CHARACTER(20)), btrim(NEW.id9::CHARACTER(20)), pocetzmien, ret, ret_master, 'id9') INTO pocetzmien, ret, ret_master;
		SELECT * FROM food600.updatepolastring(OLD.kdo, NEW.kdo, pocetzmien, ret, ret_master, 'kdo') INTO pocetzmien, ret, ret_master;
		SELECT * FROM food600.updatepolastring(OLD.tmatr, NEW.tmatr, pocetzmien, ret, ret_master, 'tmatr') 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_kas, NEW.id_kas, pocetzmien, ret, ret_master, 'id_kas') INTO pocetzmien, ret, ret_master;
		SELECT * FROM food600.updatepolastring(OLD.budova, NEW.budova, pocetzmien, ret, ret_master, 'budova') 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.idriadok_master IS NOT NULL AND OLD.idriadok_master<>0 THEN
					ret:=ret||' WHERE idriadok='||btrim(OLD.idriadok_master::CHARACTER(20))||';';
				ELSE
					ret:=ret||' WHERE idriadok=%P1%';
					mp1:='getidriadokmaster('||(OLD.idriadok::CHARACTER(20))||',''h10_set'')';
				END IF;
				INSERT INTO food600.sync_idriadok (prikaz, tabulka, idriadok, operacia, nazev, p1, p2, p3, p4, p5, p6, id_sync, is_master) VALUES (ret, 'h10_set', OLD.idriadok, '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 idriadok_master='||btrim(OLD.idriadok::CHARACTER(20))||';';
				INSERT INTO food600.sync_idriadok (prikaz, tabulka, idriadok, operacia, nazev, p1, p2, p3, p4, p5, p6, id_sync, is_master) VALUES (ret_master, 'h10_set', OLD.idriadok, '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.h10_set (';
		ret_master:=ret;
		IF NEW.c_druh IS NOT NULL THEN
			IF pocetzmien>0 THEN
				polia:=polia||',';
				hodnoty:=hodnoty||',';
				polia_master:=polia_master||',';
				hodnoty_master:=hodnoty_master||',';
			END IF;
			polia:=polia||'c_druh';
			polia_master:=polia_master||'c_druh';
			IF NEW.c_druh>0 THEN
				hodnoty:=hodnoty||'%P2%';
				mp2:='getcdruhmaster('||btrim(NEW.c_druh::CHARACTER(20))||')';
				hodnoty_master:=hodnoty_master||'%P2%';
				mp2_master:='getcdruhslave('||btrim(NEW.c_druh::CHARACTER(20))||')';
			ELSE
				hodnoty:=hodnoty||'0';
				hodnoty_master:=hodnoty_master||'0';
			END IF;
			pocetzmien:=pocetzmien+1;
		END IF;		
		IF NEW.id_hores IS NOT NULL THEN
			IF pocetzmien>0 THEN
				polia:=polia||',';
				hodnoty:=hodnoty||',';
				polia_master:=polia_master||',';
				hodnoty_master:=hodnoty_master||',';
			END IF;
			polia:=polia||'id_hores';
			polia_master:=polia_master||'id_hores';
			IF NEW.c_druh>0 THEN
				hodnoty:=hodnoty||'%P3%';
				mp3:='getidhoresmaster('||btrim(NEW.id_hores::CHARACTER(20))||')';
				hodnoty_master:=hodnoty_master||'%P3%';
				mp3_master:='getidhoresslave('||btrim(NEW.id_hores::CHARACTER(20))||')';
			ELSE
				hodnoty:=hodnoty||'0';
				hodnoty_master:=hodnoty_master||'0';
			END IF;
			pocetzmien:=pocetzmien+1;
		END IF;		
		SELECT * FROM food600.insertpolanumeric(NEW.id1, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'id1') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;
		SELECT * FROM food600.insertpolanumeric(NEW.id2, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'id2') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;
		SELECT * FROM food600.insertpolanumeric(NEW.id3, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'id3') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;
		SELECT * FROM food600.insertpolanumeric(NEW.id4, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'id4') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;
		SELECT * FROM food600.insertpolanumeric(NEW.id5, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'id5') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;
		SELECT * FROM food600.insertpolanumeric(NEW.id6, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'id6') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;
		SELECT * FROM food600.insertpolanumeric(NEW.id7, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'id7') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;
		SELECT * FROM food600.insertpolanumeric(NEW.id8, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'id8') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;
		SELECT * FROM food600.insertpolanumeric(NEW.id9, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'id9') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;
		SELECT * FROM food600.insertpolastring(NEW.tmatr, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'tmatr') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;
		SELECT * FROM food600.insertpolastring(NEW.kdy, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'kdy') 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.kdo, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'kdo') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;
		SELECT * FROM food600.insertpolastring(NEW.id_kas, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'id_kas') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;
		SELECT * FROM food600.insertpolastring(NEW.budova, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'budova') 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 idriadok;';
				INSERT INTO food600.sync_idriadok (prikaz, tabulka, idriadok, operacia, nazev, p1, p2, p3, p4, p5, p6, id_sync, is_master) VALUES (ret,'h10_set', NEW.idriadok, 'I', '', mp1, mp2, mp3, mp4, mp5, mp6, idnazapis_mastrovi, 0);
			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 idriadok;';
				INSERT INTO food600.sync_idriadok (prikaz, tabulka, idriadok, operacia, nazev, p1, p2, p3, p4, p5, p6, id_sync, is_master) VALUES (ret_master, 'h10_set', NEW.idriadok, 'I', '', 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 = 'DELETE') THEN
	IF robsync>0 THEN
		ret:='DELETE FROM food600.h10_set WHERE idriadok=';
		ret_master:='DELETE FROM food600.h10_set WHERE idriadok_master=';
		IF mojmaster>0 THEN 
			IF (OLD.idriadok_master>0) THEN
				ret:=ret||OLD.idriadok_master::CHARACTER(20);
			ELSE
				ret:=ret||'%P1%';
				mp1:='getidriadokmaster('||btrim(OLD.idriadok::CHARACTER(20))||',''h10_set'')';
			END IF;	
			INSERT INTO food600.sync_idriadok (prikaz, tabulka, idriadok, operacia, nazev, p1, p2, p3, p4, p5, p6, id_sync, is_master) VALUES (ret, 'h10_set', OLD.idriadok, 'D', '', mp1, mp2, mp3, mp4, mp5, mp6, mojeid, 0);
		END IF;
		IF sommaster>0 THEN 
			ret_master:=ret_master||OLD.idriadok::CHARACTER(20);
			INSERT INTO food600.sync_idriadok (prikaz, tabulka, idriadok, operacia, nazev, p1, p2, p3, p4, p5, p6, id_sync, is_master) VALUES (ret_master, 'h10_set', OLD.idriadok, 'D', '', '', '', '', '', '', '', mojeid, 1);
		END IF;
	END IF;
	RETURN OLD;
END IF;
        
END;
$BODY$;
