CREATE OR REPLACE FUNCTION food600.archivaciaskladu(
	cdata integer,
	cstredisk integer,
	menobuf character varying,
	mmena_akt integer,
	ku_dokladu integer)
    RETURNS smallint
    LANGUAGE 'plpgsql'
    COST 100
    VOLATILE
AS $BODY$
DECLARE teraz TIMESTAMP DEFAULT current_timestamp;
	moperace CHARACTER(2) DEFAULT '';
	mdatumdokl DATE DEFAULT current_date;
	mdone SMALLINT DEFAULT 1;
	midriadok INTEGER DEFAULT 0;
	minoperace CURSOR FOR SELECT idriadok FROM food600.obraty_a  WHERE c_data=cdata AND c_dokl IN (SELECT MIN(c_dokl) AS c_dokl FROM food600.obraty_a WHERE c_stredisk=cstredisk and operace='_P' and c_data=cdata GROUP BY c_karty);
BEGIN
SELECT INTO moperace, mdatumdokl operace, datum_dokl  FROM food600.doklady WHERE c_data=cdata AND c_dokl=ku_dokladu LIMIT 1;
PERFORM food600.gen_stav( cstredisk, ku_dokladu, mmena_akt, menobuf, cdata);
INSERT INTO food600.obraty_a (c_data, c_stredisk, operace, c_dokl, c_karty, mnozstvi, cena, cena_part, zustatek, cena_prum, dan, serial_no, poradi, spdan, spstupne) SELECT c_data, c_stredisk, operace, c_dokl, c_karty, mnozstvi, cena, cena_part, zustatek, cena_prum, dan, serial_no, poradi, spdan, spstupne FROM food600.obraty WHERE c_stredisk = cstredisk AND c_dokl < ku_dokladu AND c_data=cdata;
UPDATE food600.obraty_a SET operace='_P' WHERE operace='PR' AND c_data=cdata;
OPEN minoperace;
WHILE (mdone>0) LOOP
	FETCH minoperace INTO midriadok;
	IF midriadok ISNULL THEN
		mdone:=0;
	ELSE
		UPDATE food600.obraty_a SET operace='PR' WHERE obraty_a.idriadok=midriadok;
	END IF;
END LOOP;
CLOSE minoperace;

IF moperace = 'KK' THEN
	INSERT INTO food600.doklady_a (c_data, c_stredisk, c_dokl, c_partner, doklad, datum_dokl, operace, pz1, pz2, platba, trzba1, splatnost, sleva_abs, sleva_proc, cen_hlad, zaloha, halere, provedl, uctsuma, c_mena, kdy, uctexport, typ_pohybu) SELECT c_data, c_stredisk, c_dokl, c_partner, doklad, datum_dokl, operace, pz1, pz2, platba, trzba1, splatnost, sleva_abs, sleva_proc, cen_hlad, zaloha, halere, provedl, uctsuma, c_mena, kdy, uctexport, typ_pohybu FROM food600.doklady WHERE c_stredisk = cstredisk AND c_dokl <= ku_dokladu AND c_data=cdata;
ELSE
	INSERT INTO food600.doklady_a (c_data, c_stredisk, c_dokl, c_partner, doklad, datum_dokl, operace, pz1, pz2, platba, trzba1, splatnost, sleva_abs, sleva_proc, cen_hlad, zaloha, halere, provedl, uctsuma, c_mena, kdy, uctexport, typ_pohybu) SELECT c_data, c_stredisk, c_dokl, c_partner, doklad, datum_dokl, operace, pz1, pz2, platba, trzba1, splatnost, sleva_abs, sleva_proc, cen_hlad, zaloha, halere, provedl, uctsuma, c_mena, kdy, uctexport, typ_pohybu FROM food600.doklady WHERE c_stredisk = cstredisk AND c_dokl < ku_dokladu AND c_data=cdata;
END IF;

ALTER TABLE food600.obraty DISABLE TRIGGER ALL;
DELETE FROM food600.obraty WHERE c_stredisk = cstredisk AND c_dokl < ku_dokladu AND c_data=cdata;
DELETE FROM food600.doklady WHERE c_stredisk = cstredisk AND c_dokl < ku_dokladu AND c_data=cdata;
--IF moperace = 'KK' THEN
	ku_dokladu:=ku_dokladu-1;
--END IF;
INSERT INTO food600.doklady (c_data, c_stredisk, c_dokl, operace, datum_dokl, c_mena, c_partner, doklad, provedl, kdy, typ_pohybu) VALUES (cdata, cstredisk, ku_dokladu, 'PR', mdatumdokl, mmena_akt, cstredisk, 1, '', teraz,0);
INSERT INTO food600.obraty (c_data, c_stredisk, operace, c_dokl, c_karty, mnozstvi, cena, zustatek, cena_prum,  poradi) SELECT c_data, c_stredisk, 'PR' as operace, ku_dokladu as c_dokl, c_karty, zustatek as mnozstvi, cena_prum as cena, zustatek, cena_prum, c_karty FROM food600.genstav WHERE c_data=cdata AND menostlp=menobuf;
ALTER TABLE food600.obraty ENABLE TRIGGER ALL;

DELETE FROM food600.genstav WHERE menostlp=menobuf AND c_data=cdata;
RETURN 1;

END
$BODY$;
