CREATE OR REPLACE FUNCTION food600.archivaciask(
	datumdo date,
	subor character varying,
	cdata integer)
    RETURNS integer
    LANGUAGE 'plpgsql'
    COST 100
    VOLATILE
AS $BODY$
BEGIN
CASE
	WHEN subor='TXT' THEN 
		INSERT INTO food600.sk__txt_a (c_data, ucet, txt, jmeno, adresa1, adresa2, adresa3, schvalil, akce, dic, cislo, hucet, hjmeno, hpokoj, hporad, hpref, c_uzaverka, hid, icdph, ico, poradi) SELECT c_data, ucet, txt, jmeno, adresa1, adresa2, adresa3, schvalil, akce, dic, cislo, hucet, hjmeno, hpokoj, hporad, hpref, c_uzaverka, hid, icdph, ico, poradi FROM food600.sk__txt WHERE c_data=cdata AND c_uzaverka in (SELECT DISTINCT c_uzaverka FROM food600.sk__sez  WHERE uct_den<=datumdo AND c_data=cdata);
		DELETE FROM food600.sk__txt WHERE c_data=cdata AND c_uzaverka in (SELECT DISTINCT c_uzaverka FROM food600.sk__sez  WHERE uct_den<=datumdo AND c_data=cdata);
	WHEN subor='FIN' THEN 
		INSERT INTO food600.sk__fin_a (c_data, c_uzaverka, ucet, autor, u_cas, u_minut, mutace, cas_atrib, pocet_h, stul, text_sleva, text_link, ducis, lim_datum, id_limit, round50, pohladavka) SELECT c_data, c_uzaverka, ucet, autor, u_cas, u_minut, mutace, cas_atrib, pocet_h, stul, text_sleva, text_link, ducis, lim_datum, id_limit, round50, pohladavka FROM food600.sk__fin  WHERE c_data=cdata AND c_uzaverka in (SELECT DISTINCT c_uzaverka FROM food600.sk__sez  WHERE uct_den<=datumdo AND c_data=cdata);
		DELETE FROM food600.sk__fin  WHERE c_data=cdata AND c_uzaverka in (SELECT DISTINCT c_uzaverka FROM food600.sk__sez  WHERE uct_den<=datumdo AND c_data=cdata);
	WHEN subor='POL' THEN 
		INSERT INTO food600.sk__pol_a (c_data, kompez, c_uzaverka, ucet, c_stredisk, c_karty, prachy, prachy_puv, dan, dan_sazba, mnozstvi, cen_hlad, k_odpoctu, poradi, dotacia, dil_porce, id_fmenu) SELECT c_data, kompez, c_uzaverka, ucet, c_stredisk, c_karty, prachy, prachy_puv, dan, dan_sazba, mnozstvi, cen_hlad, k_odpoctu, poradi, dotacia, dil_porce, id_fmenu FROM food600.sk__pol WHERE c_data=cdata AND c_uzaverka in (SELECT DISTINCT c_uzaverka FROM food600.sk__sez  WHERE uct_den<=datumdo AND c_data=cdata);
		DELETE FROM food600.sk__pol WHERE c_data=cdata AND c_uzaverka in (SELECT DISTINCT c_uzaverka FROM food600.sk__sez  WHERE uct_den<=datumdo AND c_data=cdata);
	WHEN subor='PLT' THEN 
		INSERT INTO food600.sk__plt_a (c_uzaverka, ucet, druh_pl, cena_pl, tip, mena, cena_mena, poradi, text, c_data) SELECT c_uzaverka, ucet, druh_pl, cena_pl, tip, mena, cena_mena, poradi, text, c_data FROM food600.sk__plt WHERE c_data=cdata AND c_uzaverka in (SELECT DISTINCT c_uzaverka FROM food600.sk__sez  WHERE uct_den<=datumdo AND c_data=cdata);
		DELETE FROM food600.sk__plt WHERE c_data=cdata AND c_uzaverka in (SELECT DISTINCT c_uzaverka FROM food600.sk__sez  WHERE uct_den<=datumdo AND c_data=cdata);
	WHEN subor='SEZ' THEN 
		INSERT INTO food600.sk__vklvy_a (c_data, nazev, datum, c_uzaverka, ucet, ciastka, mena, ciast_mena, autor, cisfplat, popis, fciarkod, fucis, komu, operacia) SELECT c_data, nazev, datum, c_uzaverka, ucet, ciastka, mena, ciast_mena, autor, cisfplat, popis, fciarkod, fucis, komu, operacia FROM food600.sk__vklvy WHERE c_data=cdata AND c_uzaverka in (SELECT DISTINCT c_uzaverka FROM food600.sk__sez  WHERE uct_den<=datumdo AND c_data=cdata);
		DELETE FROM food600.sk__vklvy WHERE c_data=cdata AND c_uzaverka in (SELECT DISTINCT c_uzaverka FROM food600.sk__sez  WHERE uct_den<=datumdo AND c_data=cdata);

		INSERT INTO food600.sk__fis_a (c_data, ucet, c_uzaverka, fucis, fuciarkod, fuzcislo, fuzdate) SELECT c_data, ucet, c_uzaverka, fucis, fuciarkod, fuzcislo, fuzdate FROM food600.sk__fis WHERE c_data=cdata AND c_uzaverka in (SELECT DISTINCT c_uzaverka FROM food600.sk__sez  WHERE uct_den<=datumdo AND c_data=cdata);
		DELETE FROM food600.sk__fis WHERE c_data=cdata AND c_uzaverka in (SELECT DISTINCT c_uzaverka FROM food600.sk__sez  WHERE uct_den<=datumdo AND c_data=cdata);

		INSERT INTO food600.sk__sez_a (c_data, id_man, id_kas, uzaverka, autor, odecteno, uz_cislo, priznak, c_mena, uctexport, uct_den, lastuzav, c_uzaverka) SELECT c_data, id_man, id_kas, uzaverka, autor, odecteno, uz_cislo, priznak, c_mena, uctexport, uct_den, lastuzav, c_uzaverka  FROM food600.sk__sez WHERE uct_den<=datumdo AND c_data=cdata;
		DELETE FROM food600.sk__sez  WHERE uct_den<=datumdo AND c_data=cdata;
END CASE;
RETURN 1;
END
$BODY$;
