CREATE OR REPLACE FUNCTION food600.karty_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;
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 
		(NEW.c_druh    != OLD.c_druh)    OR
		(NEW.c_karty   != OLD.c_karty)   OR
		(NEW.nazev     != OLD.nazev)     OR
		(NEW.c_druhs   != OLD.c_druhs)   OR
		(NEW.c_druhk   != OLD.c_druhk)   OR
		(NEW.nazev1    != OLD.nazev1)    OR
		(NEW.dph       != OLD.dph)       OR
		(NEW.lieh      != OLD.lieh)      OR
		(NEW.objem     != OLD.objem)     OR
		(NEW.pov_objem != OLD.pov_objem) OR
		(NEW.nazev1    != OLD.nazev1)    OR
		(NEW.c_mena    != OLD.c_mena)    OR
		(NEW.kod       != OLD.kod)
		THEN
		NEW.zmena := current_timestamp;
		PERFORM food600.f_zmeny_tab('karty',NEW.zmena,NEW.c_data);
	END IF;   
	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.karty SET ';
		ret_master:=ret;
		IF (NEW.c_karty != OLD.c_karty) THEN
			IF pocetzmien>0 THEN
				ret:=ret||',';
				ret_master:=ret_master||',';
			END IF;
			ret:=ret||'c_karty=%P2%';
			mp2:='getckartymaster('||btrim(NEW.c_karty::CHARACTER(20))||')';
			ret_master:=ret_master||'c_karty=%P2%';
			mp2_master:='getckartyslave('||btrim(NEW.c_karty::CHARACTER(20))||')';
			pocetzmien:=pocetzmien+1;
		END IF;	
		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.c_druhs != OLD.c_druhs) THEN
			IF pocetzmien>0 THEN
				ret:=ret||',';
				ret_master:=ret_master||',';
			END IF;
			IF NEW.c_druhs>0 THEN
				ret:=ret||'c_druhs=%P4%';
				mp4:='getcdruhmaster('||btrim(NEW.c_druhs::CHARACTER(20))||')';
				ret_master:=ret_master||'c_druhs=%P4%';
				mp4_master:='getcdruhslave('||btrim(NEW.c_druhs::CHARACTER(20))||')';
			ELSE
				ret:=ret||'c_druhs=0';
				ret_master:=ret_master||'c_druhs=0';
			END IF;			
			pocetzmien:=pocetzmien+1;
		END IF; 
		IF (NEW.c_druhk != OLD.c_druhk) THEN
			IF pocetzmien>0 THEN
				ret:=ret||',';
				ret_master:=ret_master||',';
			END IF;
			IF NEW.c_druhk>0 THEN
				ret:=ret||'c_druhk=%P5%';
				mp5:='getcdruhmaster('||btrim(NEW.c_druhk::CHARACTER(20))||')';
				ret_master:=ret_master||'c_druhk=%P5%';
				mp5_master:='getcdruhslave('||btrim(NEW.c_druhk::CHARACTER(20))||')';
			ELSE
				ret:=ret||'c_druhk=0';
				ret_master:=ret_master||'c_druhk=0';
			END IF;			
			pocetzmien:=pocetzmien+1;
		END IF; 
		SELECT * FROM food600.updatepolastring(OLD.nazev, NEW.nazev, pocetzmien, ret, ret_master, 'nazev') 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.updatepolanumeric(btrim(OLD.c_obalu::CHARACTER(20)), btrim(NEW.c_obalu::CHARACTER(20)), pocetzmien, ret, ret_master, 'c_obalu') INTO pocetzmien, ret, ret_master;
		SELECT * FROM food600.updatepolanumeric(btrim(OLD.obal_obsah::CHARACTER(20)), btrim(NEW.obal_obsah::CHARACTER(20)), pocetzmien, ret, ret_master, 'obal_obsah') INTO pocetzmien, ret, ret_master;
		SELECT * FROM food600.updatepolanumeric(btrim(OLD.cena01::CHARACTER(20)), btrim(NEW.cena01::CHARACTER(20)), pocetzmien, ret, ret_master, 'cena01') INTO pocetzmien, ret, ret_master;
		SELECT * FROM food600.updatepolanumeric(btrim(OLD.cena02::CHARACTER(20)), btrim(NEW.cena02::CHARACTER(20)), pocetzmien, ret, ret_master, 'cena02') INTO pocetzmien, ret, ret_master;
		SELECT * FROM food600.updatepolanumeric(btrim(OLD.cena03::CHARACTER(20)), btrim(NEW.cena03::CHARACTER(20)), pocetzmien, ret, ret_master, 'cena03') INTO pocetzmien, ret, ret_master;
		SELECT * FROM food600.updatepolanumeric(btrim(OLD.cena04::CHARACTER(20)), btrim(NEW.cena04::CHARACTER(20)), pocetzmien, ret, ret_master, 'cena04') INTO pocetzmien, ret, ret_master;
		SELECT * FROM food600.updatepolanumeric(btrim(OLD.cena05::CHARACTER(20)), btrim(NEW.cena05::CHARACTER(20)), pocetzmien, ret, ret_master, 'cena05') INTO pocetzmien, ret, ret_master;
		SELECT * FROM food600.updatepolanumeric(btrim(OLD.cena06::CHARACTER(20)), btrim(NEW.cena06::CHARACTER(20)), pocetzmien, ret, ret_master, 'cena06') INTO pocetzmien, ret, ret_master;
		SELECT * FROM food600.updatepolanumeric(btrim(OLD.cena07::CHARACTER(20)), btrim(NEW.cena07::CHARACTER(20)), pocetzmien, ret, ret_master, 'cena07') INTO pocetzmien, ret, ret_master;
		SELECT * FROM food600.updatepolanumeric(btrim(OLD.cena08::CHARACTER(20)), btrim(NEW.cena08::CHARACTER(20)), pocetzmien, ret, ret_master, 'cena08') INTO pocetzmien, ret, ret_master;
		SELECT * FROM food600.updatepolanumeric(btrim(OLD.cena09::CHARACTER(20)), btrim(NEW.cena09::CHARACTER(20)), pocetzmien, ret, ret_master, 'cena09') INTO pocetzmien, ret, ret_master;
		SELECT * FROM food600.updatepolanumeric(btrim(OLD.cena10::CHARACTER(20)), btrim(NEW.cena10::CHARACTER(20)), pocetzmien, ret, ret_master, 'cena10') INTO pocetzmien, ret, ret_master;
		SELECT * FROM food600.updatepolanumeric(btrim(OLD.cena11::CHARACTER(20)), btrim(NEW.cena11::CHARACTER(20)), pocetzmien, ret, ret_master, 'cena11') INTO pocetzmien, ret, ret_master;
		SELECT * FROM food600.updatepolanumeric(btrim(OLD.cena12::CHARACTER(20)), btrim(NEW.cena12::CHARACTER(20)), pocetzmien, ret, ret_master, 'cena12') INTO pocetzmien, ret, ret_master;
		SELECT * FROM food600.updatepolanumeric(btrim(OLD.cena13::CHARACTER(20)), btrim(NEW.cena13::CHARACTER(20)), pocetzmien, ret, ret_master, 'cena13') INTO pocetzmien, ret, ret_master;
		SELECT * FROM food600.updatepolanumeric(btrim(OLD.cena14::CHARACTER(20)), btrim(NEW.cena14::CHARACTER(20)), pocetzmien, ret, ret_master, 'cena14') INTO pocetzmien, ret, ret_master;
		SELECT * FROM food600.updatepolanumeric(btrim(OLD.cena15::CHARACTER(20)), btrim(NEW.cena15::CHARACTER(20)), pocetzmien, ret, ret_master, 'cena15') INTO pocetzmien, ret, ret_master;
		SELECT * FROM food600.updatepolanumeric(btrim(OLD.cena16::CHARACTER(20)), btrim(NEW.cena16::CHARACTER(20)), pocetzmien, ret, ret_master, 'cena16') INTO pocetzmien, ret, ret_master;
		SELECT * FROM food600.updatepolanumeric(btrim(OLD.cena17::CHARACTER(20)), btrim(NEW.cena17::CHARACTER(20)), pocetzmien, ret, ret_master, 'cena17') INTO pocetzmien, ret, ret_master;
		SELECT * FROM food600.updatepolanumeric(btrim(OLD.cena18::CHARACTER(20)), btrim(NEW.cena18::CHARACTER(20)), pocetzmien, ret, ret_master, 'cena18') INTO pocetzmien, ret, ret_master;
		SELECT * FROM food600.updatepolanumeric(btrim(OLD.cena19::CHARACTER(20)), btrim(NEW.cena19::CHARACTER(20)), pocetzmien, ret, ret_master, 'cena19') INTO pocetzmien, ret, ret_master;
		SELECT * FROM food600.updatepolanumeric(btrim(OLD.cena20::CHARACTER(20)), btrim(NEW.cena20::CHARACTER(20)), pocetzmien, ret, ret_master, 'cena20') INTO pocetzmien, ret, ret_master;
		SELECT * FROM food600.updatepolanumeric(btrim(OLD.cena21::CHARACTER(20)), btrim(NEW.cena21::CHARACTER(20)), pocetzmien, ret, ret_master, 'cena21') INTO pocetzmien, ret, ret_master;
		SELECT * FROM food600.updatepolanumeric(btrim(OLD.cena22::CHARACTER(20)), btrim(NEW.cena22::CHARACTER(20)), pocetzmien, ret, ret_master, 'cena22') INTO pocetzmien, ret, ret_master;
		SELECT * FROM food600.updatepolanumeric(btrim(OLD.cena23::CHARACTER(20)), btrim(NEW.cena23::CHARACTER(20)), pocetzmien, ret, ret_master, 'cena23') INTO pocetzmien, ret, ret_master;
		SELECT * FROM food600.updatepolanumeric(btrim(OLD.cena24::CHARACTER(20)), btrim(NEW.cena24::CHARACTER(20)), pocetzmien, ret, ret_master, 'cena24') INTO pocetzmien, ret, ret_master;
		SELECT * FROM food600.updatepolanumeric(btrim(OLD.cena25::CHARACTER(20)), btrim(NEW.cena25::CHARACTER(20)), pocetzmien, ret, ret_master, 'cena25') INTO pocetzmien, ret, ret_master;
		SELECT * FROM food600.updatepolanumeric(btrim(OLD.cena26::CHARACTER(20)), btrim(NEW.cena26::CHARACTER(20)), pocetzmien, ret, ret_master, 'cena26') INTO pocetzmien, ret, ret_master;
		SELECT * FROM food600.updatepolanumeric(btrim(OLD.cena27::CHARACTER(20)), btrim(NEW.cena27::CHARACTER(20)), pocetzmien, ret, ret_master, 'cena27') INTO pocetzmien, ret, ret_master;
		SELECT * FROM food600.updatepolanumeric(btrim(OLD.cena28::CHARACTER(20)), btrim(NEW.cena28::CHARACTER(20)), pocetzmien, ret, ret_master, 'cena28') INTO pocetzmien, ret, ret_master;
		SELECT * FROM food600.updatepolanumeric(btrim(OLD.cena29::CHARACTER(20)), btrim(NEW.cena29::CHARACTER(20)), pocetzmien, ret, ret_master, 'cena29') INTO pocetzmien, ret, ret_master;
		SELECT * FROM food600.updatepolanumeric(btrim(OLD.cena30::CHARACTER(20)), btrim(NEW.cena30::CHARACTER(20)), pocetzmien, ret, ret_master, 'cena30') INTO pocetzmien, ret, ret_master;
		SELECT * FROM food600.updatepolanumeric(btrim(OLD.cena31::CHARACTER(20)), btrim(NEW.cena31::CHARACTER(20)), pocetzmien, ret, ret_master, 'cena31') INTO pocetzmien, ret, ret_master;
		SELECT * FROM food600.updatepolanumeric(btrim(OLD.cena32::CHARACTER(20)), btrim(NEW.cena32::CHARACTER(20)), pocetzmien, ret, ret_master, 'cena32') INTO pocetzmien, ret, ret_master;
		SELECT * FROM food600.updatepolanumeric(btrim(OLD.cena33::CHARACTER(20)), btrim(NEW.cena33::CHARACTER(20)), pocetzmien, ret, ret_master, 'cena33') INTO pocetzmien, ret, ret_master;
		SELECT * FROM food600.updatepolanumeric(btrim(OLD.cena34::CHARACTER(20)), btrim(NEW.cena34::CHARACTER(20)), pocetzmien, ret, ret_master, 'cena34') INTO pocetzmien, ret, ret_master;
		SELECT * FROM food600.updatepolanumeric(btrim(OLD.cena35::CHARACTER(20)), btrim(NEW.cena35::CHARACTER(20)), pocetzmien, ret, ret_master, 'cena35') INTO pocetzmien, ret, ret_master;
		SELECT * FROM food600.updatepolanumeric(btrim(OLD.cena36::CHARACTER(20)), btrim(NEW.cena36::CHARACTER(20)), pocetzmien, ret, ret_master, 'cena36') INTO pocetzmien, ret, ret_master;
		SELECT * FROM food600.updatepolanumeric(btrim(OLD.cena37::CHARACTER(20)), btrim(NEW.cena37::CHARACTER(20)), pocetzmien, ret, ret_master, 'cena37') INTO pocetzmien, ret, ret_master;
		SELECT * FROM food600.updatepolanumeric(btrim(OLD.cena38::CHARACTER(20)), btrim(NEW.cena38::CHARACTER(20)), pocetzmien, ret, ret_master, 'cena38') INTO pocetzmien, ret, ret_master;
		SELECT * FROM food600.updatepolanumeric(btrim(OLD.cena39::CHARACTER(20)), btrim(NEW.cena39::CHARACTER(20)), pocetzmien, ret, ret_master, 'cena39') INTO pocetzmien, ret, ret_master;
		SELECT * FROM food600.updatepolanumeric(btrim(OLD.cena40::CHARACTER(20)), btrim(NEW.cena40::CHARACTER(20)), pocetzmien, ret, ret_master, 'cena40') INTO pocetzmien, ret, ret_master;
		SELECT * FROM food600.updatepolanumeric(btrim(OLD.pocet::CHARACTER(20)), btrim(NEW.pocet::CHARACTER(20)), pocetzmien, ret, ret_master, 'pocet') INTO pocetzmien, ret, ret_master;
		SELECT * FROM food600.updatepolastring(OLD.bar_kod1, NEW.bar_kod1, pocetzmien, ret, ret_master, 'bar_kod1') INTO pocetzmien, ret, ret_master;
		SELECT * FROM food600.updatepolastring(OLD.bar_kod2, NEW.bar_kod2, pocetzmien, ret, ret_master, 'bar_kod2') INTO pocetzmien, ret, ret_master;
		SELECT * FROM food600.updatepolastring(OLD.kod, NEW.kod, pocetzmien, ret, ret_master, 'kod') INTO pocetzmien, ret, ret_master;
		SELECT * FROM food600.updatepolastring(OLD.dan1, NEW.dan1, pocetzmien, ret, ret_master, 'dan1') INTO pocetzmien, ret, ret_master;
		SELECT * FROM food600.updatepolanumeric(btrim(OLD.dph::CHARACTER(20)), btrim(NEW.dph::CHARACTER(20)), pocetzmien, ret, ret_master, 'dph') INTO pocetzmien, ret, ret_master;
		SELECT * FROM food600.updatepolastring(OLD.nazev1, NEW.nazev1, pocetzmien, ret, ret_master, 'nazev1') INTO pocetzmien, ret, ret_master;
		SELECT * FROM food600.updatepolastring(OLD.nazev2, NEW.nazev2, pocetzmien, ret, ret_master, 'nazev2') INTO pocetzmien, ret, ret_master;
		SELECT * FROM food600.updatepolastring(OLD.nazev3, NEW.nazev3, pocetzmien, ret, ret_master, 'nazev3') INTO pocetzmien, ret, ret_master;
		SELECT * FROM food600.updatepolastring(OLD.nazev4, NEW.nazev4, pocetzmien, ret, ret_master, 'nazev4') INTO pocetzmien, ret, ret_master;
		SELECT * FROM food600.updatepolastring(OLD.nazev5, NEW.nazev5, pocetzmien, ret, ret_master, 'nazev5') INTO pocetzmien, ret, ret_master;
		SELECT * FROM food600.updatepolastring(OLD.naz1, NEW.naz1, pocetzmien, ret, ret_master, 'naz1') INTO pocetzmien, ret, ret_master;
		SELECT * FROM food600.updatepolastring(OLD.naz2, NEW.naz2, pocetzmien, ret, ret_master, 'naz2') INTO pocetzmien, ret, ret_master;
		SELECT * FROM food600.updatepolastring(OLD.naz3, NEW.naz3, pocetzmien, ret, ret_master, 'naz3') INTO pocetzmien, ret, ret_master;
		SELECT * FROM food600.updatepolastring(OLD.naz4, NEW.naz4, pocetzmien, ret, ret_master, 'naz4') INTO pocetzmien, ret, ret_master;
		SELECT * FROM food600.updatepolastring(OLD.naz5, NEW.naz5, pocetzmien, ret, ret_master, 'naz5') INTO pocetzmien, ret, ret_master;
		SELECT * FROM food600.updatepolanumeric(btrim(OLD.vaha_obalu::CHARACTER(20)), btrim(NEW.vaha_obalu::CHARACTER(20)), pocetzmien, ret, ret_master, 'vaha_obalu') INTO pocetzmien, ret, ret_master;
		SELECT * FROM food600.updatepolanumeric(btrim(OLD.vaha_koef::CHARACTER(20)), btrim(NEW.vaha_koef::CHARACTER(20)), pocetzmien, ret, ret_master, 'vaha_koef') INTO pocetzmien, ret, ret_master;
		SELECT * FROM food600.updatepolanumeric(btrim(OLD.tolerance::CHARACTER(20)), btrim(NEW.tolerance::CHARACTER(20)), pocetzmien, ret, ret_master, 'tolerance') INTO pocetzmien, ret, ret_master;
		SELECT * FROM food600.updatepolastring(btrim(OLD.vaha), btrim(NEW.vaha), pocetzmien, ret, ret_master, 'vaha') INTO pocetzmien, ret, ret_master;
		SELECT * FROM food600.updatepolanumeric(btrim(OLD.lieh::CHARACTER(20)), btrim(NEW.lieh::CHARACTER(20)), pocetzmien, ret, ret_master, 'lieh') INTO pocetzmien, ret, ret_master;
		SELECT * FROM food600.updatepolanumeric(btrim(OLD.objem::CHARACTER(20)), btrim(NEW.objem::CHARACTER(20)), pocetzmien, ret, ret_master, 'objem') INTO pocetzmien, ret, ret_master;
		SELECT * FROM food600.updatepolanumeric(btrim(OLD.pov_objem::CHARACTER(20)), btrim(NEW.pov_objem::CHARACTER(20)), pocetzmien, ret, ret_master, 'pov_objem') INTO pocetzmien, ret, ret_master;
		SELECT * FROM food600.updatepolanumeric(btrim(OLD.c_mena::CHARACTER(20)), btrim(NEW.c_mena::CHARACTER(20)), pocetzmien, ret, ret_master, 'c_mena') INTO pocetzmien, ret, ret_master;
		SELECT * FROM food600.updatepolastring(btrim(OLD.stav), btrim(NEW.stav), pocetzmien, ret, ret_master, 'stav') INTO pocetzmien, ret, ret_master;
		IF pocetzmien>0 THEN
			IF mojmaster>0 AND posunhore>0 THEN 
				ret:=ret||', id_sync='||btrim(idnazapis_mastrovi::CHARACTER(10));
				IF OLD.c_k_m IS NOT NULL AND OLD.c_k_m<>0 THEN
					ret:=ret||' WHERE c_karty='||btrim(OLD.c_k_m::CHARACTER(20))||';';
				ELSE
					ret:=ret||' WHERE c_karty=%P1%';
					mp1:='getckartymaster('||(OLD.c_karty::CHARACTER(20))||')';
				END IF;
				INSERT INTO food600.sync_karty (prikaz, tabulka, c_karty, operacia, nazev, p1, p2, p3, p4, p5, p6, id_sync, is_master) VALUES (ret, 'karty', OLD.c_karty, 'U', OLD.nazev, 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(10));
				ret_master:=ret_master||' WHERE c_k_m='||btrim(OLD.c_karty::CHARACTER(20))||';';
				INSERT INTO food600.sync_karty (prikaz, tabulka, c_karty, operacia, nazev, p1, p2, p3, p4, p5, p6, id_sync, is_master) VALUES (ret_master, 'karty', OLD.c_karty, 'U', OLD.nazev, 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
	NEW.zmena := current_timestamp;
	PERFORM food600.f_zmeny_tab('karty',NEW.zmena,NEW.c_data);
	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.karty (';
		ret_master:=ret;
		IF NEW.c_karty 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_karty';
			hodnoty:=hodnoty||'food600.m_max(0,''c_karty'')';
			polia_master:=polia_master||'c_karty';
			hodnoty_master:=hodnoty_master||'food600.m_max(0,''c_karty'')';
			pocetzmien:=pocetzmien+1;
		END IF;
		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.c_druhs 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_druhs';
			polia_master:=polia_master||'c_druhs';
			IF NEW.c_druhs>0 THEN
				hodnoty:=hodnoty||'%P3%';
				mp3:='getcdruhmaster('||btrim(new.c_druhs::CHARACTER(20))||')';
				hodnoty_master:=hodnoty_master||'%P3%';
				mp3_master:='getcdruhslave('||btrim(new.c_druhs::CHARACTER(20))||')';
			ELSE
				hodnoty:=hodnoty||'0';
				hodnoty_master:=hodnoty_master||'0';
			END IF;
			pocetzmien:=pocetzmien+1;
		END IF;
		IF NEW.c_druhk 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_druhk';
			polia_master:=polia_master||'c_druhk';
			IF NEW.c_druhk>0 THEN
				hodnoty:=hodnoty||'%P4%';
				mp4:='getcdruhmaster('||btrim(new.c_druhk::CHARACTER(20))||')';
				hodnoty_master:=hodnoty_master||'%P4%';
				mp4_master:='getcdruhslave('||btrim(new.c_druhk::CHARACTER(20))||')';
			ELSE
				hodnoty:=hodnoty||'0';
				hodnoty_master:=hodnoty_master||'0';
			END IF;
			pocetzmien:=pocetzmien+1;
		END IF;
		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.nazev, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'nazev') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;
		SELECT * FROM food600.insertpolanumeric(NEW.c_obalu, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'c_obalu') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;
		SELECT * FROM food600.insertpolanumeric(NEW.obal_obsah, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'obal_obsah') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;
		SELECT * FROM food600.insertpolanumeric(NEW.cena01, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'cena01') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;
		SELECT * FROM food600.insertpolanumeric(NEW.cena02, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'cena02') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;
		SELECT * FROM food600.insertpolanumeric(NEW.cena03, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'cena03') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;
		SELECT * FROM food600.insertpolanumeric(NEW.cena04, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'cena04') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;
		SELECT * FROM food600.insertpolanumeric(NEW.cena05, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'cena05') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;
		SELECT * FROM food600.insertpolanumeric(NEW.cena06, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'cena06') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;
		SELECT * FROM food600.insertpolanumeric(NEW.cena07, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'cena07') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;
		SELECT * FROM food600.insertpolanumeric(NEW.cena08, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'cena08') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;
		SELECT * FROM food600.insertpolanumeric(NEW.cena09, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'cena09') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;
		SELECT * FROM food600.insertpolanumeric(NEW.cena10, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'cena10') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;
		SELECT * FROM food600.insertpolanumeric(NEW.cena11, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'cena11') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;
		SELECT * FROM food600.insertpolanumeric(NEW.cena12, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'cena12') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;
		SELECT * FROM food600.insertpolanumeric(NEW.cena13, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'cena13') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;
		SELECT * FROM food600.insertpolanumeric(NEW.cena14, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'cena14') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;
		SELECT * FROM food600.insertpolanumeric(NEW.cena15, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'cena15') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;
		SELECT * FROM food600.insertpolanumeric(NEW.cena16, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'cena16') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;
		SELECT * FROM food600.insertpolanumeric(NEW.cena17, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'cena17') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;
		SELECT * FROM food600.insertpolanumeric(NEW.cena18, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'cena18') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;
		SELECT * FROM food600.insertpolanumeric(NEW.cena19, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'cena19') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;
		SELECT * FROM food600.insertpolanumeric(NEW.cena20, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'cena20') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;
		SELECT * FROM food600.insertpolanumeric(NEW.cena21, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'cena21') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;
		SELECT * FROM food600.insertpolanumeric(NEW.cena22, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'cena22') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;
		SELECT * FROM food600.insertpolanumeric(NEW.cena23, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'cena23') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;
		SELECT * FROM food600.insertpolanumeric(NEW.cena24, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'cena24') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;
		SELECT * FROM food600.insertpolanumeric(NEW.cena25, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'cena25') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;
		SELECT * FROM food600.insertpolanumeric(NEW.cena26, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'cena26') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;
		SELECT * FROM food600.insertpolanumeric(NEW.cena27, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'cena27') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;
		SELECT * FROM food600.insertpolanumeric(NEW.cena28, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'cena28') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;
		SELECT * FROM food600.insertpolanumeric(NEW.cena29, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'cena29') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;
		SELECT * FROM food600.insertpolanumeric(NEW.cena30, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'cena30') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;
		SELECT * FROM food600.insertpolanumeric(NEW.cena31, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'cena31') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;
		SELECT * FROM food600.insertpolanumeric(NEW.cena32, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'cena32') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;
		SELECT * FROM food600.insertpolanumeric(NEW.cena33, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'cena33') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;
		SELECT * FROM food600.insertpolanumeric(NEW.cena34, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'cena34') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;
		SELECT * FROM food600.insertpolanumeric(NEW.cena35, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'cena35') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;
		SELECT * FROM food600.insertpolanumeric(NEW.cena36, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'cena36') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;
		SELECT * FROM food600.insertpolanumeric(NEW.cena37, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'cena37') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;
		SELECT * FROM food600.insertpolanumeric(NEW.cena38, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'cena38') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;
		SELECT * FROM food600.insertpolanumeric(NEW.cena39, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'cena39') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;
		SELECT * FROM food600.insertpolanumeric(NEW.cena40, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'cena40') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;
		SELECT * FROM food600.insertpolanumeric(NEW.pocet, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'pocet') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;
		SELECT * FROM food600.insertpolastring(NEW.bar_kod1, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'bar_kod1') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;
		SELECT * FROM food600.insertpolastring(NEW.bar_kod2, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'bar_kod2') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;
		SELECT * FROM food600.insertpolastring(NEW.kod, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'kod') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;
		SELECT * FROM food600.insertpolastring(NEW.dan1, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'dan1') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;
		SELECT * FROM food600.insertpolanumeric(NEW.dph, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'dph') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;
		SELECT * FROM food600.insertpolastring(NEW.nazev1, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'nazev1') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;
		SELECT * FROM food600.insertpolastring(NEW.nazev2, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'nazev2') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;
		SELECT * FROM food600.insertpolastring(NEW.nazev3, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'nazev3') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;
		SELECT * FROM food600.insertpolastring(NEW.nazev4, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'nazev4') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;
		SELECT * FROM food600.insertpolastring(NEW.nazev5, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'nazev5') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;
		SELECT * FROM food600.insertpolastring(NEW.naz1, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'naz1') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;
		SELECT * FROM food600.insertpolastring(NEW.naz2, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'naz2') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;
		SELECT * FROM food600.insertpolastring(NEW.naz3, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'naz3') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;
		SELECT * FROM food600.insertpolastring(NEW.naz4, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'naz4') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;
		SELECT * FROM food600.insertpolastring(NEW.naz5, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'naz5') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;
		SELECT * FROM food600.insertpolanumeric(NEW.vaha_obalu, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'vaha_obalu') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;
		SELECT * FROM food600.insertpolanumeric(NEW.vaha_koef, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'vaha_koef') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;
		SELECT * FROM food600.insertpolanumeric(NEW.tolerance, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'tolerance') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;
		SELECT * FROM food600.insertpolastring(NEW.vaha, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'vaha') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;
		SELECT * FROM food600.insertpolanumeric(NEW.lieh, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'lieh') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;
		SELECT * FROM food600.insertpolanumeric(NEW.objem, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'objem') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;
		SELECT * FROM food600.insertpolanumeric(NEW.pov_objem, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'pov_objem') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;
		SELECT * FROM food600.insertpolanumeric(NEW.c_mena, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'c_mena') INTO pocetzmien, hodnoty, polia, hodnoty_master, polia_master;
		SELECT * FROM food600.insertpolastring(NEW.stav, pocetzmien, hodnoty, polia, hodnoty_master, polia_master, 'stav') 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));
				ret:=ret||polia||') VALUES ('||hodnoty||') RETURNING c_karty;';
				INSERT INTO food600.sync_karty (prikaz, tabulka, c_karty, operacia, nazev, p1, p2, p3, p4, p5, p6, id_sync, is_master) VALUES (ret, 'karty', NEW.c_karty, 'I', NEW.nazev, 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 c_karty;';
				INSERT INTO food600.sync_karty (prikaz, tabulka, c_karty, operacia, nazev, p1, p2, p3, p4, p5, p6, id_sync, is_master) VALUES (ret_master, 'karty', NEW.c_karty, 'I', NEW.nazev, 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.karty WHERE c_karty=';
		ret_master:='DELETE FROM food600.karty WHERE c_k_m=';
		IF mojmaster>0 THEN 
			IF (OLD.c_k_m>0) THEN
				ret:=ret||OLD.c_k_m::CHARACTER(20);
			ELSE
				ret:=ret||'%P1%';
				mp1:='getckartymaster('||btrim(OLD.c_karty::CHARACTER(20))||')';
			END IF;	
			INSERT INTO food600.sync_karty (prikaz, tabulka, c_karty, operacia, nazev, p1, p2, p3, p4, p5, p6, id_sync, is_master) VALUES (ret, 'karty', OLD.c_karty, 'D', OLD.nazev, mp1, mp2, mp3, mp4, mp5, mp6, mojeid, 0);
		END IF;
		IF sommaster>0 THEN 
			ret_master:=ret_master||OLD.c_karty::CHARACTER(20);
			INSERT INTO food600.sync_karty (prikaz, tabulka, c_karty, operacia, nazev, p1, p2, p3, p4, p5, p6, id_sync, is_master) VALUES (ret_master, 'karty', OLD.c_karty, 'D', OLD.nazev, mp1, mp2, mp3, mp4, mp5, mp6, mojeid, 1);
		END IF;
	END IF;
	RETURN OLD;
END IF;
END;
$BODY$;
