CREATE OR REPLACE FUNCTION food600.pociatocnystav(
	cdata integer,
	cstredisk integer,
	menobuf character varying,
	mmena_akt integer,
	mmesiac character,
	mrok character)
    RETURNS smallint
    LANGUAGE 'plpgsql'
    COST 100
    VOLATILE
AS $BODY$
DECLARE teraz TIMESTAMP DEFAULT current_timestamp;
	uzavierka SMALLINT DEFAULT 0;
	omesiac CHARACTER(2) DEFAULT '';
	orok CHARACTER(4) DEFAULT '';

BEGIN
IF mrok='' AND mmesiac='' THEN
	uzavierka:=0;
ELSE
	uzavierka:=1;
	DELETE FROM food600.doklady_pr WHERE c_stredisk=cstredisk AND c_data=cdata AND rok=mrok AND mesia<=mmesiac;
	DELETE FROM food600.obraty_pr WHERE c_stredisk=cstredisk AND c_data=cdata AND rok=mrok AND mesiac=mmesiac;
	CASE  
		WHEN mmesiac='01	' THEN
			omesiac:='10';
			orok:=cast((cast(mrok as integer)-1) AS chracter);
		WHEN mmesiac='02' THEN
			omesiac:='11';
			orok:=cast((cast(mrok as integer)-1) AS chracter);
		WHEN mmesiac='03' THEN
			omesiac:='12';
			orok:=cast((cast(mrok as integer)-1) AS chracter);
		WHEN mmesiac='04' THEN
			omesiac:='01';
			orok:=mrok;		
		WHEN mmesiac='05' THEN
			omesiac:='02';
			orok:=mrok;
		WHEN mmesiac='06' THEN
			omesiac:='03';
			orok:=mrok;
		WHEN mmesiac='07' THEN
			omesiac:='04';
			orok:=mrok;
		WHEN mmesiac='08' THEN
			omesiac:='05';
			orok:=mrok;
		WHEN mmesiac='09' THEN
			omesiac:='06';
			orok:=mrok;
		WHEN mmesiac='10' THEN
			omesiac:='07';
			orok:=mrok;
		WHEN mmesiac='11' THEN
			omesiac:='08';
			orok:=mrok;
		WHEN mmesiac='12' THEN
			omesiac:='09';
			orok:=mrok;
	END CASE ;
	
END IF;
INSERT INTO food600.doklady_pr (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, c_data, uctexport, cas_pr, mesiac, rok) SELECT 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, c_data, uctexport, teraz AS cas_pr, mmesiac, mrok FROM food600.doklady WHERE doklady.c_data=cdata and doklady.c_stredisk=cstredisk;
INSERT INTO food600.obraty_pr (c_stredisk, operace, c_dokl, c_karty, mnozstvi, cena, cena_part, zustatek, cena_prum, dan, serial_no, poradi, c_data, spdan, spstupne, cas_pr) SELECT c_stredisk, operace, c_dokl, c_karty, mnozstvi, cena, cena_part, zustatek, cena_prum, dan, serial_no, poradi, c_data, spdan, spstupne, teraz AS cas_pr FROM food600.obraty WHERE obraty.c_data=cdata and obraty.c_stredisk=cstredisk;
IF uzavierka=0 THEN
	DELETE FROM food600.doklady WHERE doklady.c_data=cdata and doklady.c_stredisk=cstredisk;
	DELETE FROM food600.obraty WHERE obraty.c_data=cdata and obraty.c_stredisk=cstredisk;
	UPDATE food600.sklad SET zustatek=0, cena_prum=0 WHERE c_data=cdata AND c_stredisk=cstredisk;
	PERFORM food600.buff_out(menobuf,cdata,mmena_akt,''::CHARACTER VARYING,''::CHARACTER VARYING,0);
ELSE
	DELETE FROM food600.doklady_pr WHERE c_stredisk=cstredisk AND c_data=cdata AND rok<=orok AND mesiac<=omesiac;
	DELETE FROM food600.obraty_pr WHERE c_stredisk=cstredisk AND c_data=cdata AND rok<=orok AND mesiac<=omesiac;
END IF;

RETURN 1;
END
$BODY$;
