CREATE OR REPLACE FUNCTION food600.skupiny_rozpatie()
    RETURNS trigger
    LANGUAGE 'plpgsql'
    COST 100
    VOLATILE NOT LEAKPROOF
AS $BODY$
DECLARE mcdruhk INTEGER;
	mrozp NUMERIC(8,2) DEFAULT 0;
	mhladina SMALLINT;
	nakup NUMERIC(12,4);
	predaj NUMERIC(12,4);
	cdata SMALLINT;
	newrozp NUMERIC(12,2);
	mcdruh INTEGER;
	mckarty INTEGER;
	olddan NUMERIC(5,2);
	newdan NUMERIC(5,2);
	mdone SMALLINT;
	typ SMALLINT DEFAULT 0;
	midriadok INTEGER;
	zoznamkariet CURSOR FOR SELECT c_karty FROM food600.karty WHERE c_druhk=mcdruh AND c_data=cdata;
BEGIN

IF (TG_OP = 'DELETE')THEN
	DELETE FROM food600.skupinyrozpatia WHERE c_data=OLD.c_data AND c_druh=OLD.c_druh;
	DELETE FROM food600.rozpatia WHERE c_data=OLD.c_data AND c_karty IN (SELECT c_karty FROM food600.karty WHERE c_druhk=OLD.c_druh AND c_data=OLD.c_data);
	RETURN OLD;
ELSE
	IF (TG_OP = 'INSERT')THEN
		INSERT INTO food600.skupinyrozpatia (c_data, c_druh) VALUES (NEW.c_data, NEW.c_druh);
		RETURN NEW;
	ELSE
		IF NEW.c_druh>0 THEN
			cdata:=NEW.c_data;
			mcdruh:=NEW.c_druh;
			IF cdata=0 OR cdata IS NULL THEN
				cdata:=1;
			END IF;
			mhladina:=0;
			WHILE mhladina<90 LOOP
				mhladina:=mhladina+1;
				CASE mhladina
					WHEN 1 THEN 
						SELECT rozp01 INTO newrozp FROM food600.skupinyrozpatia WHERE c_data=cdata AND c_druh=mcdruh;
						olddan:=OLD.dan01;
						newdan:=NEW.dan01;
					WHEN 2 THEN 
						SELECT rozp02 INTO newrozp FROM food600.skupinyrozpatia WHERE c_data=cdata AND c_druh=mcdruh;
						olddan:=OLD.dan02;
						newdan:=NEW.dan02;
					WHEN 3 THEN 
						SELECT rozp03 INTO newrozp FROM food600.skupinyrozpatia WHERE c_data=cdata AND c_druh=mcdruh;
						olddan:=OLD.dan03;
						newdan:=NEW.dan03;
					WHEN 4 THEN 
						SELECT rozp04 INTO newrozp FROM food600.skupinyrozpatia WHERE c_data=cdata AND c_druh=mcdruh;
						olddan:=OLD.dan04;
						newdan:=NEW.dan04;
					WHEN 5 THEN 
						SELECT rozp05 INTO newrozp FROM food600.skupinyrozpatia WHERE c_data=cdata AND c_druh=mcdruh;
						olddan:=OLD.dan05;
						newdan:=NEW.dan05;
					WHEN 6 THEN
						SELECT rozp06 INTO newrozp FROM food600.skupinyrozpatia WHERE c_data=cdata AND c_druh=mcdruh;
						olddan:=OLD.dan06;
						newdan:=NEW.dan06;
					WHEN 7 THEN 
						SELECT rozp07 INTO newrozp FROM food600.skupinyrozpatia WHERE c_data=cdata AND c_druh=mcdruh;
						olddan:=OLD.dan07;
						newdan:=NEW.dan07;
					WHEN 8 THEN
						SELECT rozp08 INTO newrozp FROM food600.skupinyrozpatia WHERE c_data=cdata AND c_druh=mcdruh;
						olddan:=OLD.dan08;
						newdan:=NEW.dan08;
					WHEN 9 THEN
						SELECT rozp09 INTO newrozp FROM food600.skupinyrozpatia WHERE c_data=cdata AND c_druh=mcdruh;
						olddan:=OLD.dan09;
						newdan:=NEW.dan09;
					WHEN 10 THEN
						SELECT rozp10 INTO newrozp FROM food600.skupinyrozpatia WHERE c_data=cdata AND c_druh=mcdruh;
						olddan:=OLD.dan10;
						newdan:=NEW.dan10;
					WHEN 11 THEN
						SELECT rozp11 INTO newrozp FROM food600.skupinyrozpatia WHERE c_data=cdata AND c_druh=mcdruh;
						olddan:=OLD.dan11;
						newdan:=NEW.dan11;
					WHEN 12 THEN
						SELECT rozp12 INTO newrozp FROM food600.skupinyrozpatia WHERE c_data=cdata AND c_druh=mcdruh;
						olddan:=OLD.dan12;
						newdan:=NEW.dan12;
					WHEN 13 THEN
						SELECT rozp13 INTO newrozp FROM food600.skupinyrozpatia WHERE c_data=cdata AND c_druh=mcdruh;
						olddan:=OLD.dan13;
						newdan:=NEW.dan13;
					WHEN 14 THEN
						SELECT rozp14 INTO newrozp FROM food600.skupinyrozpatia WHERE c_data=cdata AND c_druh=mcdruh;
						olddan:=OLD.dan14;
						newdan:=NEW.dan14;
					WHEN 15 THEN 
						SELECT rozp15 INTO newrozp FROM food600.skupinyrozpatia WHERE c_data=cdata AND c_druh=mcdruh;
						olddan:=OLD.dan15;
						newdan:=NEW.dan15;
					WHEN 16 THEN
						SELECT rozp16 INTO newrozp FROM food600.skupinyrozpatia WHERE c_data=cdata AND c_druh=mcdruh;
						olddan:=OLD.dan16;
						newdan:=NEW.dan16;
					WHEN 17 THEN
						SELECT rozp17 INTO newrozp FROM food600.skupinyrozpatia WHERE c_data=cdata AND c_druh=mcdruh;
						olddan:=OLD.dan17;
						newdan:=NEW.dan17;
					WHEN 18 THEN
						SELECT rozp18 INTO newrozp FROM food600.skupinyrozpatia WHERE c_data=cdata AND c_druh=mcdruh;
						olddan:=OLD.dan18;
						newdan:=NEW.dan18;
					WHEN 19 THEN
						SELECT rozp19 INTO newrozp FROM food600.skupinyrozpatia WHERE c_data=cdata AND c_druh=mcdruh;
						olddan:=OLD.dan19;
						newdan:=NEW.dan19;
					WHEN 20 THEN
						SELECT rozp20 INTO newrozp FROM food600.skupinyrozpatia WHERE c_data=cdata AND c_druh=mcdruh;
						olddan:=OLD.dan20;
						newdan:=NEW.dan20;
					WHEN 21 THEN
						SELECT rozp21 INTO newrozp FROM food600.skupinyrozpatia WHERE c_data=cdata AND c_druh=mcdruh;
						olddan:=OLD.dan21;
						newdan:=NEW.dan21;
					WHEN 22 THEN
						SELECT rozp22 INTO newrozp FROM food600.skupinyrozpatia WHERE c_data=cdata AND c_druh=mcdruh;
						olddan:=OLD.dan22;
						newdan:=NEW.dan22;
					WHEN 23 THEN
						SELECT rozp23 INTO newrozp FROM food600.skupinyrozpatia WHERE c_data=cdata AND c_druh=mcdruh;
						olddan:=OLD.dan23;
						newdan:=NEW.dan23;
					WHEN 24 THEN
						SELECT rozp24 INTO newrozp FROM food600.skupinyrozpatia WHERE c_data=cdata AND c_druh=mcdruh;
						olddan:=OLD.dan24;
						newdan:=NEW.dan24;
					WHEN 25 THEN
						SELECT rozp25 INTO newrozp FROM food600.skupinyrozpatia WHERE c_data=cdata AND c_druh=mcdruh;
						olddan:=OLD.dan25;
						newdan:=NEW.dan25;
					WHEN 26 THEN
						SELECT rozp26 INTO newrozp FROM food600.skupinyrozpatia WHERE c_data=cdata AND c_druh=mcdruh;
						olddan:=OLD.dan26;
						newdan:=NEW.dan26;
					WHEN 27 THEN
						SELECT rozp27 INTO newrozp FROM food600.skupinyrozpatia WHERE c_data=cdata AND c_druh=mcdruh;
						olddan:=OLD.dan27;
						newdan:=NEW.dan27;
					WHEN 28 THEN
						SELECT rozp28 INTO newrozp FROM food600.skupinyrozpatia WHERE c_data=cdata AND c_druh=mcdruh;
						olddan:=OLD.dan28;
						newdan:=NEW.dan28;
					WHEN 29 THEN
						SELECT rozp29 INTO newrozp FROM food600.skupinyrozpatia WHERE c_data=cdata AND c_druh=mcdruh;
						olddan:=OLD.dan29;
						newdan:=NEW.dan29;
					WHEN 30 THEN
						SELECT rozp30 INTO newrozp FROM food600.skupinyrozpatia WHERE c_data=cdata AND c_druh=mcdruh;
						olddan:=OLD.dan30;
						newdan:=NEW.dan30;
					WHEN 31 THEN
						SELECT rozp31 INTO newrozp FROM food600.skupinyrozpatia WHERE c_data=cdata AND c_druh=mcdruh;
						olddan:=OLD.dan31;
						newdan:=NEW.dan31;
					WHEN 32 THEN
						SELECT rozp32 INTO newrozp FROM food600.skupinyrozpatia WHERE c_data=cdata AND c_druh=mcdruh;
						olddan:=OLD.dan32;
						newdan:=NEW.dan32;
					WHEN 33 THEN
						SELECT rozp33 INTO newrozp FROM food600.skupinyrozpatia WHERE c_data=cdata AND c_druh=mcdruh;
						olddan:=OLD.dan33;
						newdan:=NEW.dan33;
					WHEN 34 THEN
						SELECT rozp34 INTO newrozp FROM food600.skupinyrozpatia WHERE c_data=cdata AND c_druh=mcdruh;
						olddan:=OLD.dan34;
						newdan:=NEW.dan34;
					WHEN 35 THEN
						SELECT rozp35 INTO newrozp FROM food600.skupinyrozpatia WHERE c_data=cdata AND c_druh=mcdruh;
						olddan:=OLD.dan35;
						newdan:=NEW.dan35;
					WHEN 36 THEN
						SELECT rozp36 INTO newrozp FROM food600.skupinyrozpatia WHERE c_data=cdata AND c_druh=mcdruh;
						olddan:=OLD.dan36;
						newdan:=NEW.dan36;
					WHEN 37 THEN
						SELECT rozp37 INTO newrozp FROM food600.skupinyrozpatia WHERE c_data=cdata AND c_druh=mcdruh;
						olddan:=OLD.dan37;
						newdan:=NEW.dan37;
					WHEN 38 THEN
						SELECT rozp38 INTO newrozp FROM food600.skupinyrozpatia WHERE c_data=cdata AND c_druh=mcdruh;
						olddan:=OLD.dan38;
						newdan:=NEW.dan38;
					WHEN 39 THEN
						SELECT rozp39 INTO newrozp FROM food600.skupinyrozpatia WHERE c_data=cdata AND c_druh=mcdruh;
						olddan:=OLD.dan39;
						newdan:=NEW.dan39;
					WHEN 40 THEN
						SELECT rozp40 INTO newrozp FROM food600.skupinyrozpatia WHERE c_data=cdata AND c_druh=mcdruh;
						olddan:=OLD.dan40;
						newdan:=NEW.dan40;
					WHEN 41 THEN
						SELECT rozpa0 INTO newrozp FROM food600.skupinyrozpatia WHERE c_data=cdata AND c_druh=mcdruh;
						olddan:=OLD.dana0;
						newdan:=NEW.dana0;
					WHEN 42 THEN
						SELECT rozpa1 INTO newrozp FROM food600.skupinyrozpatia WHERE c_data=cdata AND c_druh=mcdruh;
						olddan:=OLD.dana1;
						newdan:=NEW.dana1;
					WHEN 43 THEN
						SELECT rozpa2 INTO newrozp FROM food600.skupinyrozpatia WHERE c_data=cdata AND c_druh=mcdruh;
						olddan:=OLD.dana2;
						newdan:=NEW.dana2;
					WHEN 44 THEN
						SELECT rozpa3 INTO newrozp FROM food600.skupinyrozpatia WHERE c_data=cdata AND c_druh=mcdruh;
						olddan:=OLD.dana3;
						newdan:=NEW.dana3;
					WHEN 45 THEN
						SELECT rozpa4 INTO newrozp FROM food600.skupinyrozpatia WHERE c_data=cdata AND c_druh=mcdruh;
						olddan:=OLD.dana4;
						newdan:=NEW.dana4;
					WHEN 46 THEN
						SELECT rozpa5 INTO newrozp FROM food600.skupinyrozpatia WHERE c_data=cdata AND c_druh=mcdruh;
						olddan:=OLD.dana5;
						newdan:=NEW.dana5;
					WHEN 47 THEN
						SELECT rozpa6 INTO newrozp FROM food600.skupinyrozpatia WHERE c_data=cdata AND c_druh=mcdruh;
						olddan:=OLD.dana6;
						newdan:=NEW.dana6;
					WHEN 48 THEN
						SELECT rozpa7 INTO newrozp FROM food600.skupinyrozpatia WHERE c_data=cdata AND c_druh=mcdruh;
						olddan:=OLD.dana7;
						newdan:=NEW.dana7;
					WHEN 49 THEN
						SELECT rozpa8 INTO newrozp FROM food600.skupinyrozpatia WHERE c_data=cdata AND c_druh=mcdruh;
						olddan:=OLD.dana8;
						newdan:=NEW.dana8;
					WHEN 50 THEN
						SELECT rozpa9 INTO newrozp FROM food600.skupinyrozpatia WHERE c_data=cdata AND c_druh=mcdruh;
						olddan:=OLD.dana9;
						newdan:=NEW.dana9;
					WHEN 51 THEN
						SELECT rozpb0 INTO newrozp FROM food600.skupinyrozpatia WHERE c_data=cdata AND c_druh=mcdruh;
						olddan:=OLD.danb0;
						newdan:=NEW.danb0;
					WHEN 52 THEN
						SELECT rozpb1 INTO newrozp FROM food600.skupinyrozpatia WHERE c_data=cdata AND c_druh=mcdruh;
						olddan:=OLD.danb1;
						newdan:=NEW.danb1;
					WHEN 53 THEN
						SELECT rozpb2 INTO newrozp FROM food600.skupinyrozpatia WHERE c_data=cdata AND c_druh=mcdruh;
						olddan:=OLD.danb2;
						newdan:=NEW.danb2;
					WHEN 54 THEN
						SELECT rozpb3 INTO newrozp FROM food600.skupinyrozpatia WHERE c_data=cdata AND c_druh=mcdruh;
						olddan:=OLD.danb3;
						newdan:=NEW.danb3;
					WHEN 55 THEN
						SELECT rozpb4 INTO newrozp FROM food600.skupinyrozpatia WHERE c_data=cdata AND c_druh=mcdruh;
						olddan:=OLD.danb4;
						newdan:=NEW.danb4;
					WHEN 56 THEN
						SELECT rozpb5 INTO newrozp FROM food600.skupinyrozpatia WHERE c_data=cdata AND c_druh=mcdruh;
						olddan:=OLD.danb5;
						newdan:=NEW.danb5;
					WHEN 57 THEN
						SELECT rozpb6 INTO newrozp FROM food600.skupinyrozpatia WHERE c_data=cdata AND c_druh=mcdruh;
						olddan:=OLD.danb6;
						newdan:=NEW.danb6;
					WHEN 58 THEN
						SELECT rozpb7 INTO newrozp FROM food600.skupinyrozpatia WHERE c_data=cdata AND c_druh=mcdruh;
						olddan:=OLD.danb7;
						newdan:=NEW.danb7;
					WHEN 59 THEN
						SELECT rozpb8 INTO newrozp FROM food600.skupinyrozpatia WHERE c_data=cdata AND c_druh=mcdruh;
						olddan:=OLD.danb8;
						newdan:=NEW.danb8;
					WHEN 60 THEN
						SELECT rozpb9 INTO newrozp FROM food600.skupinyrozpatia WHERE c_data=cdata AND c_druh=mcdruh;
						olddan:=OLD.danb9;
						newdan:=NEW.danb9;
					WHEN 61 THEN
						SELECT rozpc0 INTO newrozp FROM food600.skupinyrozpatia WHERE c_data=cdata AND c_druh=mcdruh;
						olddan:=OLD.danc0;
						newdan:=NEW.danc0;
					WHEN 62 THEN
						SELECT rozpc1 INTO newrozp FROM food600.skupinyrozpatia WHERE c_data=cdata AND c_druh=mcdruh;
						olddan:=OLD.danc1;
						newdan:=NEW.danc1;
					WHEN 63 THEN
					SELECT rozpc2 INTO newrozp FROM food600.skupinyrozpatia WHERE c_data=cdata AND c_druh=mcdruh;
						olddan:=OLD.danc2;
						newdan:=NEW.danc2;
					WHEN 64 THEN
						SELECT rozpc3 INTO newrozp FROM food600.skupinyrozpatia WHERE c_data=cdata AND c_druh=mcdruh;
						olddan:=OLD.danc3;
						newdan:=NEW.danc3;
					WHEN 65 THEN
						SELECT rozpc4 INTO newrozp FROM food600.skupinyrozpatia WHERE c_data=cdata AND c_druh=mcdruh;
						olddan:=OLD.danc4;
						newdan:=NEW.danc4;
					WHEN 66 THEN
						SELECT rozpc5 INTO newrozp FROM food600.skupinyrozpatia WHERE c_data=cdata AND c_druh=mcdruh;
						olddan:=OLD.danc5;
						newdan:=NEW.danc5;
					WHEN 67 THEN
						SELECT rozpc6 INTO newrozp FROM food600.skupinyrozpatia WHERE c_data=cdata AND c_druh=mcdruh;
						olddan:=OLD.danc6;
						newdan:=NEW.danc6;
					WHEN 68 THEN
						SELECT rozpc7 INTO newrozp FROM food600.skupinyrozpatia WHERE c_data=cdata AND c_druh=mcdruh;
						olddan:=OLD.danc7;
						newdan:=NEW.danc7;
					WHEN 69 THEN
						SELECT rozpc8 INTO newrozp FROM food600.skupinyrozpatia WHERE c_data=cdata AND c_druh=mcdruh;
						olddan:=OLD.danc8;
						newdan:=NEW.danc8;
					WHEN 70 THEN
						SELECT rozpc9 INTO newrozp FROM food600.skupinyrozpatia WHERE c_data=cdata AND c_druh=mcdruh;
						olddan:=OLD.danc9;
						newdan:=NEW.danc9;
					WHEN 71 THEN
						SELECT rozpd0 INTO newrozp FROM food600.skupinyrozpatia WHERE c_data=cdata AND c_druh=mcdruh;
						olddan:=OLD.dand0;
						newdan:=NEW.dand0;
					WHEN 72 THEN
						SELECT rozpd1 INTO newrozp FROM food600.skupinyrozpatia WHERE c_data=cdata AND c_druh=mcdruh;
						olddan:=OLD.dand1;
						newdan:=NEW.dand1;
					WHEN 73 THEN
						SELECT rozpd2 INTO newrozp FROM food600.skupinyrozpatia WHERE c_data=cdata AND c_druh=mcdruh;
						olddan:=OLD.dand2;
						newdan:=NEW.dand2;
					WHEN 74 THEN
						SELECT rozpd3 INTO newrozp FROM food600.skupinyrozpatia WHERE c_data=cdata AND c_druh=mcdruh;
						olddan:=OLD.dand3;
						newdan:=NEW.dand3;
					WHEN 75 THEN
						SELECT rozpd4 INTO newrozp FROM food600.skupinyrozpatia WHERE c_data=cdata AND c_druh=mcdruh;
						olddan:=OLD.dand4;
						newdan:=NEW.dand4;
					WHEN 76 THEN
						SELECT rozpd5 INTO newrozp FROM food600.skupinyrozpatia WHERE c_data=cdata AND c_druh=mcdruh;
						olddan:=OLD.dand5;
						newdan:=NEW.dand5;
					WHEN 77 THEN
						SELECT rozpd6 INTO newrozp FROM food600.skupinyrozpatia WHERE c_data=cdata AND c_druh=mcdruh;
						olddan:=OLD.dand6;
						newdan:=NEW.dand6;
					WHEN 78 THEN
						SELECT rozpd7 INTO newrozp FROM food600.skupinyrozpatia WHERE c_data=cdata AND c_druh=mcdruh;
						olddan:=OLD.dand7;
						newdan:=NEW.dand7;
					WHEN 79 THEN
						SELECT rozpd8 INTO newrozp FROM food600.skupinyrozpatia WHERE c_data=cdata AND c_druh=mcdruh;
						olddan:=OLD.dand8;
						newdan:=NEW.dand8;
					WHEN 80 THEN
						SELECT rozpd9 INTO newrozp FROM food600.skupinyrozpatia WHERE c_data=cdata AND c_druh=mcdruh;
						olddan:=OLD.dand9;
						newdan:=NEW.dand9;
					WHEN 81 THEN
						SELECT rozpe0 INTO newrozp FROM food600.skupinyrozpatia WHERE c_data=cdata AND c_druh=mcdruh;
						olddan:=OLD.dane0;
						newdan:=NEW.dane0;
					WHEN 82 THEN
						SELECT rozpe1 INTO newrozp FROM food600.skupinyrozpatia WHERE c_data=cdata AND c_druh=mcdruh;
						olddan:=OLD.dane1;
						newdan:=NEW.dane1;
					WHEN 83 THEN
						SELECT rozpe2 INTO newrozp FROM food600.skupinyrozpatia WHERE c_data=cdata AND c_druh=mcdruh;
						olddan:=OLD.dane2;
						newdan:=NEW.dane2;
					WHEN 84 THEN
						SELECT rozpe3 INTO newrozp FROM food600.skupinyrozpatia WHERE c_data=cdata AND c_druh=mcdruh;
						olddan:=OLD.dane3;
						newdan:=NEW.dane3;
					WHEN 85 THEN
						SELECT rozpe4 INTO newrozp FROM food600.skupinyrozpatia WHERE c_data=cdata AND c_druh=mcdruh;
						olddan:=OLD.dane4;
						newdan:=NEW.dane4;
					WHEN 86 THEN
						SELECT rozpe5 INTO newrozp FROM food600.skupinyrozpatia WHERE c_data=cdata AND c_druh=mcdruh;
						olddan:=OLD.dane5;
						newdan:=NEW.dane5;
					WHEN 87 THEN 
						SELECT rozpe6 INTO newrozp FROM food600.skupinyrozpatia WHERE c_data=cdata AND c_druh=mcdruh;
						olddan:=OLD.dane6;
						newdan:=NEW.dane6;
					WHEN 88 THEN
						SELECT rozpe7 INTO newrozp FROM food600.skupinyrozpatia WHERE c_data=cdata AND c_druh=mcdruh;
						olddan:=OLD.dane7;
						newdan:=NEW.dane7;
					WHEN 89 THEN
						SELECT rozpe8 INTO newrozp FROM food600.skupinyrozpatia WHERE c_data=cdata AND c_druh=mcdruh;
						olddan:=OLD.dane8;
						newdan:=NEW.dane8;
					WHEN 90 THEN
						SELECT rozpe9 INTO newrozp FROM food600.skupinyrozpatia WHERE c_data=cdata AND c_druh=mcdruh;
						olddan:=OLD.dane9;
						newdan:=NEW.dane9;
					WHEN 91 THEN
						SELECT rozpf0 INTO newrozp FROM food600.skupinyrozpatia WHERE c_data=cdata AND c_druh=mcdruh;
						olddan:=OLD.danf0;
						newdan:=NEW.danf0;
					WHEN 92 THEN
						SELECT rozpf1 INTO newrozp FROM food600.skupinyrozpatia WHERE c_data=cdata AND c_druh=mcdruh;
						olddan:=OLD.danf1;
						newdan:=NEW.danf1;
					WHEN 93 THEN
						SELECT rozpf2 INTO newrozp FROM food600.skupinyrozpatia WHERE c_data=cdata AND c_druh=mcdruh;
						olddan:=OLD.danf2;
						newdan:=NEW.danf2;
					WHEN 94 THEN
						SELECT rozpf3 INTO newrozp FROM food600.skupinyrozpatia WHERE c_data=cdata AND c_druh=mcdruh;
						olddan:=OLD.danf3;
						newdan:=NEW.danf3;
					WHEN 95 THEN
						SELECT rozpf4 INTO newrozp FROM food600.skupinyrozpatia WHERE c_data=cdata AND c_druh=mcdruh;
						olddan:=OLD.danf4;
						newdan:=NEW.danf4;
					WHEN 96 THEN
						SELECT rozpf5 INTO newrozp FROM food600.skupinyrozpatia WHERE c_data=cdata AND c_druh=mcdruh;
						olddan:=OLD.danf5;
						newdan:=NEW.danf5;
					WHEN 97 THEN 
						SELECT rozpf6 INTO newrozp FROM food600.skupinyrozpatia WHERE c_data=cdata AND c_druh=mcdruh;
						olddan:=OLD.danf6;
						newdan:=NEW.danf6;
					WHEN 98 THEN
						SELECT rozpf7 INTO newrozp FROM food600.skupinyrozpatia WHERE c_data=cdata AND c_druh=mcdruh;
						olddan:=OLD.danf7;
						newdan:=NEW.danf7;
					WHEN 99 THEN
						SELECT rozpf8 INTO newrozp FROM food600.skupinyrozpatia WHERE c_data=cdata AND c_druh=mcdruh;
						olddan:=OLD.danf8;
						newdan:=NEW.danf8;
					WHEN 100 THEN
						SELECT rozpf9 INTO newrozp FROM food600.skupinyrozpatia WHERE c_data=cdata AND c_druh=mcdruh;
						olddan:=OLD.danf9;
						newdan:=NEW.danf9;
					WHEN 101 THEN
						SELECT rozpg0 INTO newrozp FROM food600.skupinyrozpatia WHERE c_data=cdata AND c_druh=mcdruh;
						olddan:=OLD.dang0;
						newdan:=NEW.dang0;
					WHEN 102 THEN
						SELECT rozpg1 INTO newrozp FROM food600.skupinyrozpatia WHERE c_data=cdata AND c_druh=mcdruh;
						olddan:=OLD.dang1;
						newdan:=NEW.dang1;
					WHEN 103 THEN
						SELECT rozpg2 INTO newrozp FROM food600.skupinyrozpatia WHERE c_data=cdata AND c_druh=mcdruh;
						olddan:=OLD.dang2;
						newdan:=NEW.dang2;
					WHEN 104 THEN
						SELECT rozpg3 INTO newrozp FROM food600.skupinyrozpatia WHERE c_data=cdata AND c_druh=mcdruh;
						olddan:=OLD.dang3;
						newdan:=NEW.dang3;
					WHEN 105 THEN
						SELECT rozpg4 INTO newrozp FROM food600.skupinyrozpatia WHERE c_data=cdata AND c_druh=mcdruh;
						olddan:=OLD.dang4;
						newdan:=NEW.dang4;
					WHEN 106 THEN
						SELECT rozpg5 INTO newrozp FROM food600.skupinyrozpatia WHERE c_data=cdata AND c_druh=mcdruh;
						olddan:=OLD.dang5;
						newdan:=NEW.dang5;
					WHEN 107 THEN 
						SELECT rozpg6 INTO newrozp FROM food600.skupinyrozpatia WHERE c_data=cdata AND c_druh=mcdruh;
						olddan:=OLD.dang6;
						newdan:=NEW.dang6;
					WHEN 108 THEN
						SELECT rozpg7 INTO newrozp FROM food600.skupinyrozpatia WHERE c_data=cdata AND c_druh=mcdruh;
						olddan:=OLD.dang7;
						newdan:=NEW.dang7;
					WHEN 109 THEN
						SELECT rozpg8 INTO newrozp FROM food600.skupinyrozpatia WHERE c_data=cdata AND c_druh=mcdruh;
						olddan:=OLD.dang8;
						newdan:=NEW.dang8;
					WHEN 110 THEN
						SELECT rozpg9 INTO newrozp FROM food600.skupinyrozpatia WHERE c_data=cdata AND c_druh=mcdruh;
						olddan:=OLD.dang9;
						newdan:=NEW.dang9;
					WHEN 111 THEN
						SELECT rozph0 INTO newrozp FROM food600.skupinyrozpatia WHERE c_data=cdata AND c_druh=mcdruh;
						olddan:=OLD.danh0;
						newdan:=NEW.danh0;
					WHEN 112 THEN
						SELECT rozph1 INTO newrozp FROM food600.skupinyrozpatia WHERE c_data=cdata AND c_druh=mcdruh;
						olddan:=OLD.danh1;
						newdan:=NEW.danh1;
					WHEN 113 THEN
						SELECT rozph2 INTO newrozp FROM food600.skupinyrozpatia WHERE c_data=cdata AND c_druh=mcdruh;
						olddan:=OLD.danh2;
						newdan:=NEW.danh2;
					WHEN 114 THEN
						SELECT rozph3 INTO newrozp FROM food600.skupinyrozpatia WHERE c_data=cdata AND c_druh=mcdruh;
						olddan:=OLD.danh3;
						newdan:=NEW.danh3;
					WHEN 115 THEN
						SELECT rozph4 INTO newrozp FROM food600.skupinyrozpatia WHERE c_data=cdata AND c_druh=mcdruh;
						olddan:=OLD.danh4;
						newdan:=NEW.danh4;
					WHEN 116 THEN
						SELECT rozph5 INTO newrozp FROM food600.skupinyrozpatia WHERE c_data=cdata AND c_druh=mcdruh;
						olddan:=OLD.danh5;
						newdan:=NEW.danh5;
					WHEN 117 THEN 
						SELECT rozph6 INTO newrozp FROM food600.skupinyrozpatia WHERE c_data=cdata AND c_druh=mcdruh;
						olddan:=OLD.danh6;
						newdan:=NEW.danh6;
					WHEN 118 THEN
						SELECT rozph7 INTO newrozp FROM food600.skupinyrozpatia WHERE c_data=cdata AND c_druh=mcdruh;
						olddan:=OLD.danh7;
						newdan:=NEW.danh7;
					WHEN 119 THEN
						SELECT rozph8 INTO newrozp FROM food600.skupinyrozpatia WHERE c_data=cdata AND c_druh=mcdruh;
						olddan:=OLD.danh8;
						newdan:=NEW.danh8;
					WHEN 120 THEN
						SELECT rozph9 INTO newrozp FROM food600.skupinyrozpatia WHERE c_data=cdata AND c_druh=mcdruh;
						olddan:=OLD.danh9;
						newdan:=NEW.danh9;
					WHEN 121 THEN
						SELECT rozpi0 INTO newrozp FROM food600.skupinyrozpatia WHERE c_data=cdata AND c_druh=mcdruh;
						olddan:=OLD.dani0;
						newdan:=NEW.dani0;
					WHEN 122 THEN
						SELECT rozpi1 INTO newrozp FROM food600.skupinyrozpatia WHERE c_data=cdata AND c_druh=mcdruh;
						olddan:=OLD.dani1;
						newdan:=NEW.dani1;
					WHEN 123 THEN
						SELECT rozpi2 INTO newrozp FROM food600.skupinyrozpatia WHERE c_data=cdata AND c_druh=mcdruh;
						olddan:=OLD.dani2;
						newdan:=NEW.dani2;
					WHEN 124 THEN
						SELECT rozpi3 INTO newrozp FROM food600.skupinyrozpatia WHERE c_data=cdata AND c_druh=mcdruh;
						olddan:=OLD.dani3;
						newdan:=NEW.dani3;
					WHEN 125 THEN
						SELECT rozpi4 INTO newrozp FROM food600.skupinyrozpatia WHERE c_data=cdata AND c_druh=mcdruh;
						olddan:=OLD.dani4;
						newdan:=NEW.dani4;
					WHEN 126 THEN
						SELECT rozpi5 INTO newrozp FROM food600.skupinyrozpatia WHERE c_data=cdata AND c_druh=mcdruh;
						olddan:=OLD.dani5;
						newdan:=NEW.dani5;
					WHEN 127 THEN 
						SELECT rozpi6 INTO newrozp FROM food600.skupinyrozpatia WHERE c_data=cdata AND c_druh=mcdruh;
						olddan:=OLD.dani6;
						newdan:=NEW.dani6;
					WHEN 128 THEN
						SELECT rozpi7 INTO newrozp FROM food600.skupinyrozpatia WHERE c_data=cdata AND c_druh=mcdruh;
						olddan:=OLD.dani7;
						newdan:=NEW.dani7;
					WHEN 129 THEN
						SELECT rozpi8 INTO newrozp FROM food600.skupinyrozpatia WHERE c_data=cdata AND c_druh=mcdruh;
						olddan:=OLD.dani8;
						newdan:=NEW.dani8;
					WHEN 130 THEN
						SELECT rozpi9 INTO newrozp FROM food600.skupinyrozpatia WHERE c_data=cdata AND c_druh=mcdruh;
						olddan:=OLD.dani9;
						newdan:=NEW.dani9;
					WHEN 131 THEN
						SELECT rozpj0 INTO newrozp FROM food600.skupinyrozpatia WHERE c_data=cdata AND c_druh=mcdruh;
						olddan:=OLD.danj0;
						newdan:=NEW.danj0;
					WHEN 132 THEN
						SELECT rozpj1 INTO newrozp FROM food600.skupinyrozpatia WHERE c_data=cdata AND c_druh=mcdruh;
						olddan:=OLD.danj1;
						newdan:=NEW.danj1;
					WHEN 133 THEN
						SELECT rozpj2 INTO newrozp FROM food600.skupinyrozpatia WHERE c_data=cdata AND c_druh=mcdruh;
						olddan:=OLD.danj2;
						newdan:=NEW.danj2;
					WHEN 134 THEN
						SELECT rozpj3 INTO newrozp FROM food600.skupinyrozpatia WHERE c_data=cdata AND c_druh=mcdruh;
						olddan:=OLD.danj3;
						newdan:=NEW.danj3;
					WHEN 135 THEN
						SELECT rozpj4 INTO newrozp FROM food600.skupinyrozpatia WHERE c_data=cdata AND c_druh=mcdruh;
						olddan:=OLD.danj4;
						newdan:=NEW.danj4;
					WHEN 136 THEN
						SELECT rozpj5 INTO newrozp FROM food600.skupinyrozpatia WHERE c_data=cdata AND c_druh=mcdruh;
						olddan:=OLD.danj5;
						newdan:=NEW.danj5;
					WHEN 137 THEN 
						SELECT rozpj6 INTO newrozp FROM food600.skupinyrozpatia WHERE c_data=cdata AND c_druh=mcdruh;
						olddan:=OLD.danj6;
						newdan:=NEW.danj6;
					WHEN 138 THEN
						SELECT rozpj7 INTO newrozp FROM food600.skupinyrozpatia WHERE c_data=cdata AND c_druh=mcdruh;
						olddan:=OLD.danj7;
						newdan:=NEW.danj7;
					WHEN 139 THEN
						SELECT rozpj8 INTO newrozp FROM food600.skupinyrozpatia WHERE c_data=cdata AND c_druh=mcdruh;
						olddan:=OLD.danj8;
						newdan:=NEW.danj8;
					WHEN 140 THEN
						SELECT rozpj9 INTO newrozp FROM food600.skupinyrozpatia WHERE c_data=cdata AND c_druh=mcdruh;
						olddan:=OLD.danj9;
						newdan:=NEW.danj9;
					WHEN 141 THEN
						SELECT rozpk0 INTO newrozp FROM food600.skupinyrozpatia WHERE c_data=cdata AND c_druh=mcdruh;
						olddan:=OLD.dank0;
						newdan:=NEW.dank0;
					WHEN 142 THEN
						SELECT rozpk1 INTO newrozp FROM food600.skupinyrozpatia WHERE c_data=cdata AND c_druh=mcdruh;
						olddan:=OLD.dank1;
						newdan:=NEW.dank1;
					WHEN 143 THEN
						SELECT rozpk2 INTO newrozp FROM food600.skupinyrozpatia WHERE c_data=cdata AND c_druh=mcdruh;
						olddan:=OLD.dank2;
						newdan:=NEW.dank2;
					WHEN 144 THEN
						SELECT rozpk3 INTO newrozp FROM food600.skupinyrozpatia WHERE c_data=cdata AND c_druh=mcdruh;
						olddan:=OLD.dank3;
						newdan:=NEW.dank3;
					WHEN 145 THEN
						SELECT rozpk4 INTO newrozp FROM food600.skupinyrozpatia WHERE c_data=cdata AND c_druh=mcdruh;
						olddan:=OLD.dank4;
						newdan:=NEW.dank4;
					WHEN 146 THEN
						SELECT rozpk5 INTO newrozp FROM food600.skupinyrozpatia WHERE c_data=cdata AND c_druh=mcdruh;
						olddan:=OLD.dank5;
						newdan:=NEW.dank5;
					WHEN 147 THEN 
						SELECT rozpk6 INTO newrozp FROM food600.skupinyrozpatia WHERE c_data=cdata AND c_druh=mcdruh;
						olddan:=OLD.dank6;
						newdan:=NEW.dank6;
					WHEN 148 THEN
						SELECT rozpk7 INTO newrozp FROM food600.skupinyrozpatia WHERE c_data=cdata AND c_druh=mcdruh;
						olddan:=OLD.dank7;
						newdan:=NEW.dank7;
					WHEN 149 THEN
						SELECT rozpk8 INTO newrozp FROM food600.skupinyrozpatia WHERE c_data=cdata AND c_druh=mcdruh;
						olddan:=OLD.dank8;
						newdan:=NEW.dank8;
					WHEN 150 THEN
						SELECT rozpk9 INTO newrozp FROM food600.skupinyrozpatia WHERE c_data=cdata AND c_druh=mcdruh;
						olddan:=OLD.dank9;
						newdan:=NEW.dank9;
					WHEN 151 THEN
						SELECT rozpk0 INTO newrozp FROM food600.skupinyrozpatia WHERE c_data=cdata AND c_druh=mcdruh;
						olddan:=OLD.danl0;
						newdan:=NEW.danl0;
					WHEN 152 THEN
						SELECT rozpk1 INTO newrozp FROM food600.skupinyrozpatia WHERE c_data=cdata AND c_druh=mcdruh;
						olddan:=OLD.danl1;
						newdan:=NEW.danl1;
					WHEN 153 THEN
						SELECT rozpk2 INTO newrozp FROM food600.skupinyrozpatia WHERE c_data=cdata AND c_druh=mcdruh;
						olddan:=OLD.danl2;
						newdan:=NEW.danl2;
					WHEN 154 THEN
						SELECT rozpk3 INTO newrozp FROM food600.skupinyrozpatia WHERE c_data=cdata AND c_druh=mcdruh;
						olddan:=OLD.danl3;
						newdan:=NEW.danl3;
					WHEN 155 THEN
						SELECT rozpk4 INTO newrozp FROM food600.skupinyrozpatia WHERE c_data=cdata AND c_druh=mcdruh;
						olddan:=OLD.danl4;
						newdan:=NEW.danl4;
					WHEN 156 THEN
						SELECT rozpk5 INTO newrozp FROM food600.skupinyrozpatia WHERE c_data=cdata AND c_druh=mcdruh;
						olddan:=OLD.danl5;
						newdan:=NEW.danl5;
					WHEN 157 THEN 
						SELECT rozpk6 INTO newrozp FROM food600.skupinyrozpatia WHERE c_data=cdata AND c_druh=mcdruh;
						olddan:=OLD.danl6;
						newdan:=NEW.danl6;
					WHEN 158 THEN
						SELECT rozpk7 INTO newrozp FROM food600.skupinyrozpatia WHERE c_data=cdata AND c_druh=mcdruh;
						olddan:=OLD.danl7;
						newdan:=NEW.danl7;
					WHEN 159 THEN
						SELECT rozpk8 INTO newrozp FROM food600.skupinyrozpatia WHERE c_data=cdata AND c_druh=mcdruh;
						olddan:=OLD.danl8;
						newdan:=NEW.danl8;
					WHEN 160 THEN
						SELECT rozpk9 INTO newrozp FROM food600.skupinyrozpatia WHERE c_data=cdata AND c_druh=mcdruh;
						olddan:=OLD.danl9;
						newdan:=NEW.danl9;
				END CASE;
				IF newrozp=0 THEN
					DELETE FROM food600.rozpatia WHERE hladina=mhladina AND c_data=cdata AND c_karty IN (SELECT c_karty FROM food600.karty WHERE c_druhk=mcdruh AND c_data=cdata);				
				ELSE
					IF olddan<>newdan THEN 
						OPEN zoznamkariet;
						mdone:=1;
						WHILE mdone>0 LOOP
							FETCH zoznamkariet INTO mckarty;
							IF mckarty ISNULL THEN
								mdone:=0;
							END IF;
							IF mhladina<=40 THEN
								SELECT cena_prum, CASE mhladina WHEN 1 THEN cena01
									WHEN 2 THEN cena02
									WHEN 3 THEN cena03
									WHEN 4 THEN cena04
									WHEN 5 THEN cena05
									WHEN 6 THEN cena06
									WHEN 7 THEN cena07
									WHEN 8 THEN cena08
									WHEN 9 THEN cena09
									WHEN 10 THEN cena10
									WHEN 11 THEN cena11
									WHEN 12 THEN cena12
									WHEN 13 THEN cena13
									WHEN 14 THEN cena14
									WHEN 15 THEN cena15
									WHEN 16 THEN cena16
									WHEN 17 THEN cena17
									WHEN 18 THEN cena18
									WHEN 19 THEN cena19
									WHEN 20 THEN cena20
									WHEN 21 THEN cena21
									WHEN 22 THEN cena22
									WHEN 23 THEN cena23
									WHEN 24 THEN cena24
									WHEN 25 THEN cena25
									WHEN 26 THEN cena26
									WHEN 27 THEN cena27
									WHEN 28 THEN cena28
									WHEN 29 THEN cena29
									WHEN 30 THEN cena30
									WHEN 31 THEN cena31
									WHEN 32 THEN cena32
									WHEN 33 THEN cena33
									WHEN 34 THEN cena34
									WHEN 35 THEN cena35
									WHEN 36 THEN cena36
									WHEN 37 THEN cena37
									WHEN 38 THEN cena38
									WHEN 39 THEN cena39
									WHEN 40 THEN cena40
									END
									INTO nakup, predaj FROM food600.karty WHERE c_karty=mckarty AND c_data=cdata;
							ELSE
								SELECT cena_prum INTO nakup FROM food600.karty WHERE c_karty=mckarty AND c_data=cdata;
								SELECT CASE mhladina WHEN 41 THEN cenaa0
									WHEN 42 THEN cenaa1
									WHEN 43 THEN cenaa2
									WHEN 44 THEN cenaa3
									WHEN 45 THEN cenaa4
									WHEN 46 THEN cenaa5
									WHEN 47 THEN cenaa6
									WHEN 48 THEN cenaa7
									WHEN 49 THEN cenaa8
									WHEN 50 THEN cenaa9
									WHEN 51 THEN cenab0
									WHEN 52 THEN cenab1
									WHEN 53 THEN cenab2
									WHEN 54 THEN cenab3
									WHEN 55 THEN cenab4
									WHEN 56 THEN cenab5
									WHEN 57 THEN cenab6
									WHEN 58 THEN cenab7
									WHEN 59 THEN cenab8
									WHEN 60 THEN cenab9
									WHEN 61 THEN cenac0
									WHEN 62 THEN cenac1
									WHEN 63 THEN cenac2
									WHEN 64 THEN cenac3
									WHEN 65 THEN cenac4
									WHEN 66 THEN cenac5
									WHEN 67 THEN cenac6
									WHEN 68 THEN cenac7
									WHEN 69 THEN cenac8
									WHEN 70 THEN cenac9
									WHEN 71 THEN cenad0
									WHEN 72 THEN cenad1
									WHEN 73 THEN cenad2
									WHEN 74 THEN cenad3
									WHEN 75 THEN cenad4
									WHEN 76 THEN cenad5
									WHEN 77 THEN cenad6
									WHEN 78 THEN cenad7
									WHEN 79 THEN cenad8
									WHEN 80 THEN cenad9
									WHEN 81 THEN cenae0
									WHEN 82 THEN cenae1
									WHEN 83 THEN cenae2
									WHEN 84 THEN cenae3
									WHEN 85 THEN cenae4
									WHEN 86 THEN cenae5
									WHEN 87 THEN cenae6
									WHEN 88 THEN cenae7
									WHEN 89 THEN cenae8
									WHEN 90 THEN cenae9
									WHEN 91 THEN cenaf0
									WHEN 92 THEN cenaf1
									WHEN 93 THEN cenaf2
									WHEN 94 THEN cenaf3
									WHEN 95 THEN cenaf4
									WHEN 96 THEN cenaf5
									WHEN 97 THEN cenaf6
									WHEN 98 THEN cenaf7
									WHEN 99 THEN cenaf8
									WHEN 100 THEN cenaf9
									WHEN 101 THEN cenag0
									WHEN 102 THEN cenag1
									WHEN 103 THEN cenag2
									WHEN 104 THEN cenag3
									WHEN 105 THEN cenag4
									WHEN 106 THEN cenag5
									WHEN 107 THEN cenag6
									WHEN 108 THEN cenag7
									WHEN 109 THEN cenag8
									WHEN 110 THEN cenag9
									WHEN 111 THEN cenah0
									WHEN 112 THEN cenah1
									WHEN 113 THEN cenah2
									WHEN 114 THEN cenah3
									WHEN 115 THEN cenah4
									WHEN 116 THEN cenah5
									WHEN 117 THEN cenah6
									WHEN 118 THEN cenah7
									WHEN 119 THEN cenah8
									WHEN 120 THEN cenah9
									WHEN 121 THEN cenai0
									WHEN 122 THEN cenai1
									WHEN 123 THEN cenai2
									WHEN 124 THEN cenai3
									WHEN 125 THEN cenai4
									WHEN 126 THEN cenai5
									WHEN 127 THEN cenai6
									WHEN 128 THEN cenai7
									WHEN 129 THEN cenai8
									WHEN 130 THEN cenai9
									WHEN 131 THEN cenaj0
									WHEN 132 THEN cenaj1
									WHEN 133 THEN cenaj2
									WHEN 134 THEN cenaj3
									WHEN 135 THEN cenaj4
									WHEN 136 THEN cenaj5
									WHEN 137 THEN cenaj6
									WHEN 138 THEN cenaj7
									WHEN 139 THEN cenaj8
									WHEN 140 THEN cenaj9
									WHEN 141 THEN cenak0
									WHEN 142 THEN cenak1
									WHEN 143 THEN cenak2
									WHEN 144 THEN cenak3
									WHEN 145 THEN cenak4
									WHEN 146 THEN cenak5
									WHEN 147 THEN cenak6
									WHEN 148 THEN cenak7
									WHEN 149 THEN cenak8
									WHEN 150 THEN cenak9
									WHEN 151 THEN cenal0
									WHEN 152 THEN cenal1
									WHEN 153 THEN cenal2
									WHEN 154 THEN cenal3
									WHEN 155 THEN cenal4
									WHEN 156 THEN cenal5
									WHEN 157 THEN cenal6
									WHEN 158 THEN cenal7
									WHEN 159 THEN cenal8
									WHEN 160 THEN cenal9
									END
									INTO predaj FROM food600.kceny WHERE c_karty=mckarty AND c_data=cdata;
							END IF;
							IF newdan=0 THEN
								newdan:=1;
							END IF;
							IF predaj<>0 AND nakup<>0 THEN
								IF typ=0 THEN
									IF food600.dajvarnamelogical('FOOD_SET','','is_spanne')=1 THEN
										typ:=3;
									ELSE
										IF food600.dajvarnamelogical('FOOD_SET','','isfoodcost')=1 THEN
											typ:=2;
										ELSE							
											typ:=1;
										END IF;
									END IF;
								END IF;
								mrozp:=food600.spocitajrozpatie(predaj, newdan, nakup, typ);
								IF newrozp>0 AND mrozp<newrozp THEN 
									SELECT idriadok INTO midriadok FROM food600.rozpatia WHERE c_karty=mckarty AND hladina=mhladina AND c_data=cdata;
									IF midriadok  IS NULL THEN
										INSERT INTO food600.rozpatia (c_karty, hladina, rozp, rozp_ciel, c_data) VALUES (mckarty, mhladina, mrozp, newrozp, cdata);
									ELSE
										UPDATE food600.rozpatia SET rozp=mrozp, rozp_ciel=newrozp WHERE rozpatia.c_karty=mckarty AND rozpatia.hladina=mhladina AND rozpatia.c_data=cdata;
									END IF;
								ELSE
									DELETE FROM food600.rozpatia WHERE c_karty=mckarty AND hladina=mhladina AND c_data=cdata;				
								END IF;
							ELSE
								DELETE FROM food600.rozpatia WHERE c_karty=mckarty AND hladina=mhladina AND c_data=cdata;
							END IF;
						END LOOP;
						CLOSE zoznamkariet;
					END IF;
				END IF;
			END LOOP;
		END IF;
		RETURN NEW;
	END IF;
END IF;
END
$BODY$;
