CREATE OR REPLACE FUNCTION food600.prn_def_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.prn_def SET ';
		ret_master:=ret;
		SELECT * FROM food600.updatepolastring(OLD.prn_no, NEW.prn_no, pocetzmien, ret, ret_master, 'prn_no') INTO pocetzmien, ret, ret_master;
		SELECT * FROM food600.updatepolastring(OLD.prn_no_alt, NEW.prn_no_alt, pocetzmien, ret, ret_master, 'prn_no_alt') INTO pocetzmien, ret, ret_master;		
		SELECT * FROM food600.updatepolastring(OLD.prn_name, NEW.prn_name, pocetzmien, ret, ret_master, 'prn_name') INTO pocetzmien, ret, ret_master;		
		SELECT * FROM food600.updatepolastring(OLD.p_name, NEW.p_name, pocetzmien, ret, ret_master, 'p_name') 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.cmd_on, NEW.cmd_on, pocetzmien, ret, ret_master, 'cmd_on') INTO pocetzmien, ret, ret_master;		
		SELECT * FROM food600.updatepolastring(OLD.cmd_off, NEW.cmd_off, pocetzmien, ret, ret_master, 'cmd_off') INTO pocetzmien, ret, ret_master;		
		SELECT * FROM food600.updatepolastring(OLD.cmd_cs, NEW.cmd_cs, pocetzmien, ret, ret_master, 'cmd_cs') INTO pocetzmien, ret, ret_master;		
		SELECT * FROM food600.updatepolastring(OLD.p_reset, NEW.p_reset, pocetzmien, ret, ret_master, 'p_reset') INTO pocetzmien, ret, ret_master;		
		SELECT * FROM food600.updatepolastring(OLD.p_crlf, NEW.p_crlf, pocetzmien, ret, ret_master, 'p_crlf') INTO pocetzmien, ret, ret_master;		
		SELECT * FROM food600.updatepolastring(OLD.p_ff, NEW.p_ff, pocetzmien, ret, ret_master, 'p_ff') INTO pocetzmien, ret, ret_master;		
		SELECT * FROM food600.updatepolastring(OLD.p_6lpi, NEW.p_6lpi, pocetzmien, ret, ret_master, 'p_6lpi') INTO pocetzmien, ret, ret_master;		
		SELECT * FROM food600.updatepolastring(OLD.p_8lpi, NEW.p_8lpi, pocetzmien, ret, ret_master, 'p_8lpi') INTO pocetzmien, ret, ret_master;		
		SELECT * FROM food600.updatepolastring(OLD.p_10cpi, NEW.p_10cpi, pocetzmien, ret, ret_master, 'p_10cpi') INTO pocetzmien, ret, ret_master;		
		SELECT * FROM food600.updatepolastring(OLD.p_12cpi, NEW.p_12cpi, pocetzmien, ret, ret_master, 'p_12cpi') INTO pocetzmien, ret, ret_master;		
		SELECT * FROM food600.updatepolastring(OLD.p_16cpi, NEW.p_16cpi, pocetzmien, ret, ret_master, 'p_16cpi') INTO pocetzmien, ret, ret_master;		
		SELECT * FROM food600.updatepolastring(OLD.p_boldon, NEW.p_boldon, pocetzmien, ret, ret_master, 'p_boldon') INTO pocetzmien, ret, ret_master;		
		SELECT * FROM food600.updatepolastring(OLD.p_boldoff, NEW.p_boldoff, pocetzmien, ret, ret_master, 'p_boldoff') INTO pocetzmien, ret, ret_master;		
		SELECT * FROM food600.updatepolastring(OLD.p_ulineon, NEW.p_ulineon, pocetzmien, ret, ret_master, 'p_ulineon') INTO pocetzmien, ret, ret_master;		
		SELECT * FROM food600.updatepolastring(OLD.p_ulineoff, NEW.p_ulineoff, pocetzmien, ret, ret_master, 'p_ulineoff') INTO pocetzmien, ret, ret_master;		
		SELECT * FROM food600.updatepolastring(OLD.p_wideon, NEW.p_wideon, pocetzmien, ret, ret_master, 'p_wideon') INTO pocetzmien, ret, ret_master;		
		SELECT * FROM food600.updatepolastring(OLD.p_wideoff, NEW.p_wideoff, pocetzmien, ret, ret_master, 'p_wideoff') INTO pocetzmien, ret, ret_master;		
		SELECT * FROM food600.updatepolastring(OLD.p_italon, NEW.p_italon, pocetzmien, ret, ret_master, 'p_italon') INTO pocetzmien, ret, ret_master;		
		SELECT * FROM food600.updatepolastring(OLD.p_italoff, NEW.p_italoff, pocetzmien, ret, ret_master, 'p_italoff') INTO pocetzmien, ret, ret_master;		
		SELECT * FROM food600.updatepolastring(OLD.p_draft, NEW.p_draft, pocetzmien, ret, ret_master, 'p_draft') INTO pocetzmien, ret, ret_master;		
		SELECT * FROM food600.updatepolastring(OLD.p_nlq, NEW.p_nlq, pocetzmien, ret, ret_master, 'p_nlq') INTO pocetzmien, ret, ret_master;		
		SELECT * FROM food600.updatepolastring(OLD.p_fullcut, NEW.p_fullcut, pocetzmien, ret, ret_master, 'p_fullcut') INTO pocetzmien, ret, ret_master;		
		SELECT * FROM food600.updatepolastring(OLD.p_partcut, NEW.p_partcut, pocetzmien, ret, ret_master, 'p_partcut') INTO pocetzmien, ret, ret_master;		
		SELECT * FROM food600.updatepolanumeric(btrim(OLD.p_ucetfeed::CHARACTER(20)), btrim(NEW.p_ucetfeed::CHARACTER(20)), pocetzmien, ret, ret_master, 'p_ucetfeed') INTO pocetzmien, ret, ret_master;
		SELECT * FROM food600.updatepolastring(OLD.p_width, NEW.p_width, pocetzmien, ret, ret_master, 'p_width') INTO pocetzmien, ret, ret_master;		
		SELECT * FROM food600.updatepolastring(OLD.p_horiz, NEW.p_horiz, pocetzmien, ret, ret_master, 'p_horiz') INTO pocetzmien, ret, ret_master;		
		SELECT * FROM food600.updatepolastring(OLD.typ_diakr, NEW.typ_diakr, pocetzmien, ret, ret_master, 'typ_diakr') INTO pocetzmien, ret, ret_master;		
		SELECT * FROM food600.updatepolastring(OLD.cmd32_on, NEW.cmd32_on, pocetzmien, ret, ret_master, 'cmd32_on') INTO pocetzmien, ret, ret_master;		
		SELECT * FROM food600.updatepolastring(OLD.ord_link, NEW.ord_link, pocetzmien, ret, ret_master, 'ord_link') INTO pocetzmien, ret, ret_master;		
		SELECT * FROM food600.updatepolastring(OLD.ord_adr, NEW.ord_adr, pocetzmien, ret, ret_master, 'ord_adr') INTO pocetzmien, ret, ret_master;		
		SELECT * FROM food600.updatepolanumeric(btrim(OLD.p_kopii::CHARACTER(20)), btrim(NEW.p_kopii::CHARACTER(20)), pocetzmien, ret, ret_master, 'p_kopii') INTO pocetzmien, ret, ret_master;
		SELECT * FROM food600.updatepolanumeric(btrim(OLD.is_netprn::CHARACTER(20)), btrim(NEW.is_netprn::CHARACTER(20)), pocetzmien, ret, ret_master, 'is_netprn') INTO pocetzmien, ret, ret_master;
		SELECT * FROM food600.updatepolanumeric(btrim(OLD.p_header::CHARACTER(20)), btrim(NEW.p_header::CHARACTER(20)), pocetzmien, ret, ret_master, 'p_header') INTO pocetzmien, ret, ret_master;
		SELECT * FROM food600.updatepolastring(OLD.p_paragon, NEW.p_paragon, pocetzmien, ret, ret_master, 'p_paragon') INTO pocetzmien, ret, ret_master;		
		SELECT * FROM food600.updatepolastring(OLD.p_paragoff, NEW.p_paragoff, pocetzmien, ret, ret_master, 'p_paragoff') INTO pocetzmien, ret, ret_master;		
		SELECT * FROM food600.updatepolastring(OLD.typ_fiskal, NEW.typ_fiskal, pocetzmien, ret, ret_master, 'typ_fiskal') INTO pocetzmien, ret, ret_master;		
		SELECT * FROM food600.updatepolanumeric(btrim(OLD.failed_bills::CHARACTER(20)), btrim(NEW.failed_bills::CHARACTER(20)), pocetzmien, ret, ret_master, 'failed_bills') INTO pocetzmien, ret, ret_master;
		SELECT * FROM food600.updatepolanumeric(btrim(OLD.unrecoverable_bills::CHARACTER(20)), btrim(NEW.unrecoverable_bills::CHARACTER(20)), pocetzmien, ret, ret_master, 'unrecoverable_bills') 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))||',''prn_def'')';
				END IF;
				INSERT INTO food600.sync_idriadok (prikaz, tabulka, idriadok, operacia, nazev, p1, p2, p3, p4, p5, p6, id_sync, is_master) VALUES (ret, 'prn_def', 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, 'prn_def', 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.prn_def (';
		ret_master:=ret;
		SELECT * FROM food600.insertpolastring(NEW.prn_no, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'prn_no') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;
		SELECT * FROM food600.insertpolastring(NEW.prn_no_alt, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'prn_no_alt') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;		
		SELECT * FROM food600.insertpolastring(NEW.prn_name, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'prn_name') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;		
		SELECT * FROM food600.insertpolastring(NEW.p_name, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'p_name') 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.cmd_on, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'cmd_on') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;		
		SELECT * FROM food600.insertpolastring(NEW.cmd_off, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'cmd_off') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;		
		SELECT * FROM food600.insertpolastring(NEW.cmd_cs, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'cmd_cs') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;		
		SELECT * FROM food600.insertpolastring(NEW.p_reset, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'p_reset') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;		
		SELECT * FROM food600.insertpolastring(NEW.p_crlf, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'p_crlf') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;		
		SELECT * FROM food600.insertpolastring(NEW.p_ff, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'p_ff') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;		
		SELECT * FROM food600.insertpolastring(NEW.p_6lpi, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'p_6lpi') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;		
		SELECT * FROM food600.insertpolastring(NEW.p_8lpi, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'p_8lpi') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;		
		SELECT * FROM food600.insertpolastring(NEW.p_10cpi, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'p_10cpi') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;		
		SELECT * FROM food600.insertpolastring(NEW.p_12cpi, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'p_12cpi') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;		
		SELECT * FROM food600.insertpolastring(NEW.p_16cpi, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'p_16cpi') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;		
		SELECT * FROM food600.insertpolastring(NEW.p_boldon, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'p_boldon') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;		
		SELECT * FROM food600.insertpolastring(NEW.p_boldoff, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'p_boldoff') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;		
		SELECT * FROM food600.insertpolastring(NEW.p_ulineon, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'p_ulineon') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;		
		SELECT * FROM food600.insertpolastring(NEW.p_ulineoff, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'p_ulineoff') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;		
		SELECT * FROM food600.insertpolastring(NEW.p_wideon, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'p_wideon') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;		
		SELECT * FROM food600.insertpolastring(NEW.p_wideoff, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'p_wideoff') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;		
		SELECT * FROM food600.insertpolastring(NEW.p_italon, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'p_italon') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;		
		SELECT * FROM food600.insertpolastring(NEW.p_italoff, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'p_italoff') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;		
		SELECT * FROM food600.insertpolastring(NEW.p_draft, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'p_draft') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;		
		SELECT * FROM food600.insertpolastring(NEW.p_nlq, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'p_nlq') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;		
		SELECT * FROM food600.insertpolastring(NEW.p_fullcut, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'p_fullcut') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;		
		SELECT * FROM food600.insertpolastring(NEW.p_partcut, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'p_partcut') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;		
		SELECT * FROM food600.insertpolanumeric(NEW.p_ucetfeed, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'p_ucetfeed') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;
		SELECT * FROM food600.insertpolastring(NEW.p_width, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'p_width') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;		
		SELECT * FROM food600.insertpolastring(NEW.p_horiz, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'p_horiz') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;		
		SELECT * FROM food600.insertpolastring(NEW.typ_diakr, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'typ_diakr') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;		
		SELECT * FROM food600.insertpolastring(NEW.cmd32_on, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'cmd32_on') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;		
		SELECT * FROM food600.insertpolastring(NEW.ord_link, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'ord_link') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;		
		SELECT * FROM food600.insertpolastring(NEW.ord_adr, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'ord_adr') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;		
		SELECT * FROM food600.insertpolanumeric(NEW.p_kopii, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'p_kopii') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;
		SELECT * FROM food600.insertpolanumeric(NEW.is_netprn, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'is_netprn') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;
		SELECT * FROM food600.insertpolanumeric(NEW.p_header, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'p_header') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;
		SELECT * FROM food600.insertpolastring(NEW.p_paragon, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'p_paragon') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;		
		SELECT * FROM food600.insertpolastring(NEW.p_paragoff, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'p_paragoff') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;		
		SELECT * FROM food600.insertpolastring(NEW.typ_fiskal, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'typ_fiskal') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;		
		SELECT * FROM food600.insertpolanumeric(NEW.failed_bills, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'failed_bills') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;
		SELECT * FROM food600.insertpolanumeric(NEW.unrecoverable_bills, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'unrecoverable_bills') 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,'prn_def', 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, 'prn_def', 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.prn_def WHERE idriadok=';
		ret_master:='DELETE FROM food600.prn_def 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))||',''prn_def'')';
			END IF;	
			INSERT INTO food600.sync_idriadok (prikaz, tabulka, idriadok, operacia, nazev, p1, p2, p3, p4, p5, p6, id_sync, is_master) VALUES (ret, 'prn_def', 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, 'prn_def', OLD.idriadok, 'D', '', '', '', '', '', '', '', mojeid, 1);
		END IF;
	END IF;
	RETURN OLD;
END IF;
        
END;
$BODY$;
