CREATE OR REPLACE FUNCTION food600.calcceny()
    RETURNS trigger
    LANGUAGE 'plpgsql'
    COST 100
    VOLATILE NOT LEAKPROOF
AS $BODY$
DECLARE	mdone SMALLINT DEFAULT 0;
	mc_karty_old BIGINT DEFAULT 0;
	mc_karty BIGINT;
	mhruba numeric(20,10);
	mpocet numeric(20,10);
	mnovacena numeric(20,10);
	mc_mena INTEGER;
	mc_mena_na INTEGER;
	mcdruhk INTEGER;
	mspart CHARACTER(3);
	zaokruhlenie INTEGER DEFAULT 10;
    zapisuj INTEGER DEFAULT 0;
    lv_calc_ceny_pohyb	SMALLINT;
BEGIN
SELECT food600.dajvarnamelogical('FOOD_SET','','calc_ceny_pohyb') INTO lv_calc_ceny_pohyb;
IF lv_calc_ceny_pohyb=0 THEN
    RETURN NEW;
END IF;
IF NEW.cena_prum<>OLD.cena_prum OR NEW.c_mena_n<>OLD.c_mena_n THEN
	IF zapisuj THEN
		insert into food600.test (cas,text) values (clock_timestamp(),'CALCCENY ROZDIEL');
        raise notice 'calcceny rozdiel';
    END IF;
	mc_mena_na:=NEW.c_mena_n;
	WHILE mdone=0 LOOP
		SELECT MIN(c_karty) INTO mc_karty FROM food600.polozky WHERE c_data=NEW.c_data AND c_poloz=NEW.c_karty AND c_karty>mc_karty_old;
    	IF zapisuj THEN
	    	insert into food600.test (cas,text) values (clock_timestamp(),'CALCCENY:'||mc_karty);
            raise notice 'calcceny c_karty: %',mc_karty;
        END IF;
		IF mc_karty IS NULL THEN 
			mdone:=1;
		ELSE
			SELECT SUM(hruba) INTO mhruba FROM food600.polozky WHERE c_data=NEW.c_data AND c_karty=mc_karty;
    	IF zapisuj THEN
	    	insert into food600.test (cas,text) values (clock_timestamp(),'CALCCENY HRUBA:'||mhruba);
            raise notice 'calcceny hruba: %',mhruba;
        END IF;
			IF mhruba<>0 THEN
				SELECT pocet, c_mena_n, c_druhk, cena_prum INTO mpocet, mc_mena, mcdruhk, mnovacena FROM food600.karty WHERE c_karty=mc_karty AND c_data=NEW.c_data;
				IF mpocet=0 OR mpocet IS NULL THEN
					mpocet:=1;
				END IF;
    	IF zapisuj THEN
	    	insert into food600.test (cas,text) values (clock_timestamp(),'CALCCENY MPOCET:'||mpocet);
            raise notice 'calcceny mpocet: %',mpocet;
	    	insert into food600.test (cas,text) values (clock_timestamp(),'CALCCENY MCDRUHK:'||mcdruhk);
            raise notice 'calcceny mcdruhk: %',mcdruhk;
        END IF;
				IF mcdruhk<>0 THEN
					SELECT spart INTO mspart FROM food600.skupiny WHERE c_druh=mcdruhk;
					IF mspart='FMN' THEN
						-- fastfood - iny prepocet ceny
					ELSE
						IF mc_mena<>mc_mena_na THEN
							mnovacena:=ROUND(CAST(food600.prep_meny(mc_mena, mc_mena_na,mnovacena) AS NUMERIC),zaokruhlenie);
						END IF;
						IF OLD.c_mena=NEW.c_mena THEN
							mnovacena:=ROUND(CAST(mnovacena+(NEW.cena_prum-OLD.cena_prum)*mhruba/mpocet AS NUMERIC), zaokruhlenie);
						ELSE
							mnovacena:=ROUND(CAST(mnovacena+(NEW.cena_prum-food600.prep_meny(OLD.c_mena_n,mc_mena_na,OLD.cena_prum)*mhruba/mpocet) AS NUMERIC),zaokruhlenie);
						END IF;
    	IF zapisuj THEN
	    	insert into food600.test (cas,text) values (clock_timestamp(),'CALCCENY MNOVACENA:'||mnovacena);
            raise notice 'calcceny mnovacena: %',mnovacena;
       END IF;
						UPDATE food600.karty SET cena_prum=mnovacena, c_mena_n=mc_mena_na WHERE c_karty=mc_karty AND c_data=NEW.c_data;
    	IF zapisuj THEN
	    	insert into food600.test (cas,text) values (clock_timestamp(),'CALCCENY UPDATE KARTY:'||mc_karty);
            raise notice 'calcceny update karty: %',mc_karty;
        END IF;
					END IF;
				END IF;
			END IF;
			mc_karty_old:=mc_karty;
		END IF;
	END LOOP;
END IF;
RETURN NEW;
END;
$BODY$;
