CREATE OR REPLACE FUNCTION food600.oprava_polotovarov(
	)
    RETURNS void
    LANGUAGE 'plpgsql'
    COST 100
    VOLATILE 
AS $BODY$
DECLARE 
zoznamkoncov CURSOR FOR SELECT c_stredisk, MAX(c_dokl) AS c_dokl FROM food600.doklady WHERE operace='KK'GROUP BY c_stredisk;
mcstredisk INTEGER;
mcdokl INTEGER;
BEGIN
OPEN zoznamkoncov;
LOOP
    FETCH zoznamkoncov INTO mcstredisk, mcdokl;
    EXIT WHEN NOT FOUND; 
    UPDATE food600.obraty SET mnozstvi=mnozstvi-0.0001 WHERE operace='VP' and c_stredisk=mcstredisk AND mnozstvi<0 AND c_dokl>mcdokl;
    PERFORM food600.dorobprepocet();
    UPDATE food600.obraty SET mnozstvi=mnozstvi+0.0001 WHERE operace='VP' and c_stredisk=mcstredisk AND mnozstvi<0 AND c_dokl>mcdokl;
    PERFORM food600.dorobprepocet();
END LOOP;
END
$BODY$;
