CREATE OR REPLACE FUNCTION food600.preveddatazozasoby(
	cdata integer,
	rozdiel bigint)
    RETURNS integer
    LANGUAGE 'plpgsql'
    COST 100
    VOLATILE
AS $BODY$
DECLARE cislo BIGINT;
BEGIN
cislo:=food600.m_max(rozdiel::integer, 'c_dokl');
UPDATE food600.obr_alk SET c_dokl=c_dokl+rozdiel WHERE c_data=cdata and operace='PE' AND (c_dokl in (select distinct c_dokl from food600.dokl_buf WHERE operace='PE' and c_data=cdata));
UPDATE food600.obr_alk SET c_dokl=c_dokl+rozdiel WHERE c_data=cdata and operace='VE' AND (c_dokl in (select distinct c_dokl from food600.dokl_buf WHERE operace='VE' and c_data=cdata));
UPDATE food600.exp_faktura SET c_dokl=c_dokl+rozdiel, zasoby=0 WHERE c_data=cdata AND zasoby=1 AND (c_dokl in (select distinct c_dokl from food600.dokl_buf WHERE c_data=cdata));
UPDATE food600.dokl_buf SET pz2=(food600.tvor_cislo(pz2)+rozdiel)::character(15) WHERE (operace='PV' OR operace='VV') AND food600.tvor_cislo(pz2)>0;
INSERT INTO food600.doklady (c_dokl, c_stredisk, c_partner, doklad, datum_dokl, operace, pz1, pz2, platba, trzba1, splatnost, sleva_abs, sleva_proc, cen_hlad, zaloha, halere, provedl, uctsuma, c_mena, kdy, c_data, uctexport) SELECT c_dokl+rozdiel as c_dokl, c_stredisk, c_partner, doklad, datum_dokl, operace, pz1, pz2, platba, trzba1, splatnost, sleva_abs, sleva_proc, cen_hlad, zaloha, halere, provedl, uctsuma, c_mena, kdy, c_data, uctexport FROM food600.dokl_buf WHERE operace<>'PR' and c_data=cdata;
INSERT INTO food600.obraty (c_stredisk, operace, c_dokl, c_karty, mnozstvi, cena, cena_part, zustatek, cena_prum, dan, serial_no, poradi, c_data, spdan, spstupne) SELECT c_stredisk, operace, c_dokl+rozdiel as c_dokl, c_karty, mnozstvi, cena, cena_part, 99999 as zustatek, cena_prum, dan, serial_no, poradi, c_data, spdan, spstupne FROM food600.obra_buf WHERE operace<>'PR' and c_data=cdata ORDER BY c_dokl, poradi;
DELETE FROM food600.dokl_buf WHERE c_data=cdata;
ALTER TABLE food600.obra_buf DISABLE TRIGGER ALL;
DELETE FROM food600.obra_buf WHERE c_data=cdata;
ALTER TABLE food600.obra_buf ENABLE TRIGGER ALL;
DELETE FROM food600.skl_buf WHERE c_data=cdata;
RETURN 1;
END
$BODY$;
