CREATE OR REPLACE FUNCTION food600.buff_out(
	meno character varying,
	cdata integer,
	mmena_akt integer,
	faktu_prefix character varying,
	faktury_cislovani character varying,
	mneriesdlhy integer)
    RETURNS void
    LANGUAGE 'plpgsql'
    COST 100
    VOLATILE
AS $BODY$
DECLARE zapisuj INT DEFAULT 0;
	akt_doklad INT DEFAULT 1;
	aktualnydoklad INT DEFAULT 1;
	akt_karta INTEGER;
	akt_stredisko INTEGER;
	nc_dokl INT;
	mc_oprava INTEGER;
	mc_dokl INTEGER;
	mc_mena INTEGER;
	mcdata INTEGER;
	mpz2 CHARACTER(20);
	mc_stredisk INTEGER;
	moperace CHARACTER(2);
	mplatba CHARACTER(20);
	ndoklad INT;
	oprava INT DEFAULT 0;
	opravy INT DEFAULT 0;
	staryprepocet INT;
	npz2 CHAR(20);
	mhalere BIGINT DEFAULT 0;
	pocetriadkov INT DEFAULT 0;
	riadkyvbufferi INTEGER;
	akt_riadok INT DEFAULT 1;
	pocetdokladov INTEGER;
	prepbufout CURSOR FOR SELECT c_karty, c_stredisk FROM food600.pombufout WHERE menobuf=meno;
	prepbufdokl CURSOR FOR SELECT bufdidr FROM food600.bufferd WHERE menobuf=meno AND c_data=cdata ORDER BY bufdidr;
	opravainventury CURSOR FOR SELECT c_karty, mnozstvi, zustatek, cena, cena_prum, poradi, idriadok FROM food600.obraty WHERE obraty.c_dokl=mc_dokl AND obraty.c_data=cdata ORDER BY poradi;
	opravainventuryzmazaneriadky CURSOR FOR SELECT c_karty, mnozstvi, zustatek, cena, cena_prum, poradi, idriadok FROM food600.obraty WHERE obraty.c_dokl=mc_dokl AND obraty.c_data=cdata AND poradi NOT IN (SELECT poradi FROM food600.buffer WHERE buffer.menobuf=meno AND buffer.bufdidr=akt_doklad AND buffer.c_data=cdata) ORDER BY poradi DESC;
	opravainventuryzmeneneriadky CURSOR FOR SELECT c_karty, mnozstvi, zustatek, cena, cena_prum, poradi, idriadok FROM food600.obraty WHERE obraty.c_dokl=mc_dokl AND obraty.c_data=cdata AND poradi  IN (SELECT poradi FROM food600.buffer WHERE buffer.menobuf=meno AND buffer.bufdidr=akt_doklad AND buffer.c_data=cdata) ORDER BY poradi ASC;
	novevbufferi CURSOR FOR SELECT poradi FROM food600.buffer WHERE buffer.menobuf=meno AND buffer.bufdidr=akt_doklad AND buffer.c_data=cdata AND NOT (buffer.poradi IN (SELECT DISTINCT poradi FROM food600.obraty WHERE obraty.c_dokl=mc_dokl AND obraty.c_data=cdata)) ORDER BY poradi;
	invkarta BIGINT;
	invmnoz numeric(20,10);
	invzust numeric(20,10);
	invcena numeric(20,10);
	invcenaprum numeric(20,10);
	invporadi INTEGER;
	invidriadok BIGINT;
	bufkarta BIGINT;
	bufmnoz numeric(20,10);
	bufzust numeric(20,10);
	bufcena numeric(20,10);
	bufcenapart numeric(20,10);
	bufcenaprum numeric(20,10);
	bufdan numeric(20,10);
	bufserial CHAR(20);
    bufsscc char(50);
    spotrebnadan numeric(20,10);
	cislofaktury INTEGER;
BEGIN
IF zapisuj=1 THEN 
	insert into food600.test (cas,text) values (clock_timestamp(),'BUFF_OUT Start');
END IF;
SELECT var_value::numeric(20,10) INTO spotrebnadan FROM food600.defsubor WHERE LOWER(modul)='food_set' and LOWER(var_name)='spotrebnadan';
IF spotrebnadan IS NULL THEN
	spotrebnadan:=0;
END IF;
IF spotrebnadan>0 THEN
	UPDATE food600.buffer SET spstupne=spdan.koeficient FROM food600.spdan WHERE buffer.c_karty=spdan.c_karty AND buffer.c_data=spdan.c_data AND spdan.zmazane=0;
	UPDATE food600.buffer SET spdan=spotrebnadan WHERE spstupne<>0;
END IF;
SELECT COUNT(*) INTO pocetdokladov FROM food600.bufferd WHERE c_dokl>0 AND bufferd.menobuf=meno AND bufferd.c_data=cdata;
IF zapisuj=1 THEN 
	insert into food600.test (cas,text) values (clock_timestamp(),'BUFF_OUT POCETOPRAV:'||pocetdokladov);
END IF;
IF pocetdokladov>0 THEN 
	SELECT MIN(DISTINCT c_dokl) INTO opravy FROM food600.bufferd WHERE c_dokl>0 AND bufferd.menobuf=meno AND bufferd.c_data=cdata;
	IF zapisuj=1 THEN 
		insert into food600.test (cas,text) values (clock_timestamp(),'BUFF_OUT OPRAVY:'||opravy);
	END IF;
END IF;
SELECT COUNT(*) INTO pocetdokladov FROM food600.bufferd WHERE bufferd.menobuf=meno AND bufferd.c_data=cdata;
IF zapisuj=1 THEN 
	insert into food600.test (cas,text) values (clock_timestamp(),'BUFF_OUT POCETDOKLADOV'||pocetdokladov);
END IF;
DELETE FROM food600.buffer WHERE c_karty=0 AND buffer.menobuf=meno AND buffer.c_data=cdata;
IF zapisuj=1 THEN 
	insert into food600.test (cas,text) values (clock_timestamp(),'BUFF_OUT 1');
END IF;
UPDATE food600.buffer SET mnozstvi=- ABS(mnozstvi) WHERE operace='VV' AND buffer.menobuf=meno AND buffer.c_data=cdata;
IF zapisuj=1 THEN 
	insert into food600.test (cas,text) values (clock_timestamp(),'BUFF_OUT 2');
END IF;
UPDATE food600.buffer SET mnozstvi=ABS(mnozstvi) WHERE operace='PV' AND buffer.menobuf=meno AND buffer.c_data=cdata;
IF zapisuj=1 THEN 
	insert into food600.test (cas,text) values (clock_timestamp(),'BUFF_OUT 3');
END IF;
UPDATE food600.buffer SET dan=1 WHERE operace='VE' AND dan=0 AND buffer.menobuf=meno AND buffer.c_data=cdata;
IF zapisuj=1 THEN 
	insert into food600.test (cas,text) values (clock_timestamp(),'BUFF_OUT 4');
END IF;
DELETE FROM food600.buffer WHERE NOT((operace='PI') OR (operace='PR')) AND mnozstvi=0 AND dluh_mnoz=0 AND buffer.menobuf=meno AND buffer.c_data=cdata;
IF zapisuj=1 THEN 
	insert into food600.test (cas,text) values (clock_timestamp(),'BUFF_OUT 5');
END IF;
UPDATE food600.buffer SET cena_part=cena WHERE operace='VV' AND c_dokl<=0 AND buffer.menobuf=meno AND buffer.c_data=cdata;
IF zapisuj=1 THEN 
	insert into food600.test (cas,text) values (clock_timestamp(),'BUFF_OUT 6');
END IF;

OPEN prepbufdokl;
WHILE aktualnydoklad<=pocetdokladov LOOP
	FETCH prepbufdokl INTO akt_doklad;
	IF zapisuj=1 THEN 
		insert into food600.test (cas,text) values (clock_timestamp(),'BUFF_OUT AKT_DOKLAD: '||akt_doklad);
	END IF;
	SELECT INTO mc_dokl,mc_stredisk,moperace,mplatba,mc_mena,mcdata,mpz2  c_dokl,c_stredisk,operace,platba,c_mena,c_data,pz2 FROM food600.bufferd WHERE bufferd.bufdidr=akt_doklad AND bufferd.menobuf=meno AND bufferd.c_data=cdata;
	IF zapisuj=1 THEN 
		insert into food600.test (cas,text) values (clock_timestamp(),'BUFF_OUT C_DOKL: '||mc_dokl);
		insert into food600.test (cas,text) values (clock_timestamp(),'BUFF_OUT C_STREDISK: '||mc_stredisk);
		insert into food600.test (cas,text) values (clock_timestamp(),'BUFF_OUT OPERACE: '||moperace);
		insert into food600.test (cas,text) values (clock_timestamp(),'BUFF_OUT PLATBA: '||mplatba);
		insert into food600.test (cas,text) values (clock_timestamp(),'BUFF_OUT C_MENA: '||mc_mena);
		insert into food600.test (cas,text) values (clock_timestamp(),'BUFF_OUT C_DATA: '||mcdata);
		insert into food600.test (cas,text) values (clock_timestamp(),'BUFF_OUT MPZ2: '||mpz2);
		insert into food600.test (cas,text) values (clock_timestamp(),'BUFF_OUT BUFDIDR: '||akt_doklad);
	END IF;
	IF mc_dokl>0 THEN
		oprava:=1;
	END IF; 
	IF oprava=0 THEN
		SELECT food600.m_max(10,'c_dokl') INTO nc_dokl;
		IF zapisuj=1 THEN 
			insert into food600.test (cas,text) values (clock_timestamp(),'BUFF_OUT NOVE C_DOKL: '||nc_dokl);
		END IF;
		CASE 
			WHEN moperace = 'VR' THEN
				SELECT food600.m_max(1,'doklad'||mc_stredisk||'PE') INTO ndoklad;
			WHEN moperace = 'SF' THEN 
				SELECT food600.m_max(1,'doklad0SF') INTO ndoklad;
			ELSE
				SELECT food600.m_max(1,'doklad'||mc_stredisk||moperace) INTO ndoklad;
		END CASE;
		IF zapisuj=1 THEN 
			insert into food600.test (cas,text) values (clock_timestamp(),'BUFF_OUT NOVE DOKLAD: '||ndoklad);
		END IF;
        
		IF 'SF' IN(mplatba) THEN 
			npz2 := '';
		ELSE 
			IF moperace = 'VE' OR moperace = 'SF' THEN
				CASE 
					WHEN position('PARTNER'::TEXT IN UPPER(faktury_cislovani))>0 THEN  /* kazde stredisko ma svou ciselnou radu faktur*/
						npz2 := faktu_prefix||LPAD(food600.m_max(1,'FAKTURA_'||btrim(UPPER(faktury_cislovani)))::CHARACTER(5),5,'0');
					WHEN position('STRED'::TEXT IN UPPER(faktury_cislovani))>0 THEN  /* kazde stredisko ma svou ciselnou radu faktur*/
						npz2 := faktu_prefix||LPAD(food600.m_max(1,'doklad'||mc_stredisk||'FAKTURA')::CHARACTER(5),5,'0');
					WHEN position('DOKLAD'::TEXT IN UPPER(faktury_cislovani))>0 THEN /* faktura ma stejne cislo, jako cislo dokladu*/
						npz2 := faktu_prefix||LPAD(ndoklad::CHARACTER(5),5,'0');
					WHEN position('NECISLO'::TEXT IN UPPER(faktury_cislovani))>0 THEN  /* pz je volne editovatelny, negeneruje se cislo faktury*/
			            npz2 := '';
                    ELSE /* vsechna strediska maji spolecnou ciselnou radu faktur*/
						npz2 := faktu_prefix||food600.m_max(1,'doklad'||0||'FAKTURA');
				END CASE;
			ELSE
				npz2:=mpz2;
			END IF; 
		END IF;
		IF zapisuj=1 THEN 
			insert into food600.test (cas,text) values (clock_timestamp(),'BUFF_OUT NOVE PZ2: '||npz2);
		END IF;
		IF npz2='' OR npz2=NULL THEN
			UPDATE food600.bufferd SET c_dokl=nc_dokl, doklad=ndoklad, kdy=clock_timestamp(), c_mena=mmena_akt WHERE bufdidr=akt_doklad AND menobuf=meno AND c_data=cdata;
		ELSE
			UPDATE food600.bufferd SET c_dokl=nc_dokl, doklad=ndoklad, pz2=npz2, kdy=clock_timestamp(), c_mena=mmena_akt WHERE bufdidr=akt_doklad AND menobuf=meno AND c_data=cdata;
		END IF;		
		IF moperace='PR' THEN
			mhalere:=nc_dokl;
		END IF;
	ELSE
	    UPDATE food600.bufferd SET uctexport=doklady.uctexport from food600.doklady WHERE bufferd.c_dokl=doklady.c_dokl AND bufferd.menobuf=meno AND bufferd.c_data=cdata;
        IF mc_mena<=0 THEN
			UPDATE food600.bufferd SET kdy=clock_timestamp(), c_mena=mmena_akt WHERE bufdidr=akt_doklad AND menobuf=meno AND c_data=cdata;
		END IF; 
	END IF;
	IF zapisuj=1 THEN 
		insert into food600.test (cas,text) values (clock_timestamp(),'BUFF_OUT UPDATE BUFFER_D ');
	END IF;

	SELECT COUNT(*) INTO riadkyvbufferi FROM food600.buffer WHERE c_dokl=mc_dokl AND buffer.menobuf=meno AND bufdidr=akt_doklad AND buffer.c_data=cdata;
	IF zapisuj=1 THEN 
		insert into food600.test (cas,text) values (clock_timestamp(),'BUFF_OUT RIADKYVBUFFERI: '||riadkyvbufferi);
	END IF;
	IF riadkyvbufferi=0 THEN
		/*INSERT INTO food600.buffer (c_dokl,menobuf) VALUES (mc_dokl,meno);*/
		IF zapisuj=1 THEN 
			insert into food600.test (cas,text) values (clock_timestamp(),'BUFF_OUT VLOZENY RIADOK DO BUFFO, C_DOKL '||mc_dokl);
		END IF;
	END IF;
	IF oprava=0 THEN
		IF moperace='PB' OR moperace='VP' THEN
			UPDATE food600.buffer SET c_dokl=nc_dokl, c_stredisk=mc_stredisk WHERE c_dokl=mc_dokl AND menobuf=meno AND bufdidr=akt_doklad AND c_data=cdata;
		ELSE 
			UPDATE food600.buffer SET c_dokl=nc_dokl, c_stredisk=mc_stredisk, operace=moperace WHERE c_dokl=mc_dokl AND menobuf=meno AND bufdidr=akt_doklad AND c_data=cdata;
		END IF;
		IF zapisuj=1 THEN 
			insert into food600.test (cas,text) values (clock_timestamp(),'BUFF_OUT UPDATE BUFFER_O ');
		END IF;
	END IF;
/*		DO zurnal_log WITH "OPERA", "BUFF_OUT", m.operace + IIF(m.lloprava, "-oprava", "") + "  " + m.gcpgm_id, m.c_dokl, DATETIME() - m.zacatek_prepoctu*/

	aktualnydoklad:=aktualnydoklad+1;

	CASE
		WHEN opravy>0 THEN
			SELECT food600.m_max(1,'dokl_sto') INTO mc_oprava;
			IF zapisuj=1 THEN 
				insert into food600.test (cas,text) values (clock_timestamp(),'BUFF_OUT MC_OPRAVA: '||mc_oprava);
			END IF;
			INSERT INTO food600.dokl_sto (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, c_oprava, poznamka, typ_pohybu) SELECT doklady.c_stredisk, doklady.c_dokl,doklady.c_partner,doklady.doklad,doklady.datum_dokl,doklady.operace,doklady.pz1,doklady.pz2,doklady.platba, doklady.trzba1, doklady.splatnost, doklady.sleva_abs, doklady.sleva_proc, doklady.cen_hlad, doklady.zaloha, doklady.halere, doklady.provedl, doklady.uctsuma, doklady.c_mena, doklady.kdy, doklady.c_data, mc_oprava AS c_oprava, poznamka, typ_pohybu FROM food600.doklady WHERE doklady.c_dokl=mc_dokl AND doklady.c_data=cdata;
			IF zapisuj=1 THEN 
				insert into food600.test (cas,text) values (clock_timestamp(),'BUFF_OUT ZAPIS DO DOKL_STO: ');
			END IF;
			INSERT INTO food600.obra_sto (c_stredisk, operace, c_dokl, c_karty, mnozstvi, cena, cena_part, zustatek, cena_prum, dan, serial_no, poradi, c_oprava, kdy, c_data, sscc) SELECT obraty.c_stredisk, obraty.operace, obraty.c_dokl,obraty.c_karty, obraty.mnozstvi, obraty.cena, obraty.cena_part, obraty.zustatek, obraty.cena_prum, obraty.dan, obraty.serial_no, obraty.poradi, mc_oprava AS c_oprava,clock_timestamp() AS kdy, obraty.c_data, obraty.sscc FROM food600.obraty WHERE obraty.c_dokl=mc_dokl AND obraty.c_data=cdata;
			IF zapisuj=1 THEN 
				insert into food600.test (cas,text) values (clock_timestamp(),'BUFF_OUT ZAPIS DO OBRA_STO: ');
			END IF;
			DELETE FROM food600.doklady WHERE doklady.c_dokl=mc_dokl AND doklady.c_data=cdata;
			IF zapisuj=1 THEN 
				insert into food600.test (cas,text) values (clock_timestamp(),'BUFF_OUT VYMAZ Z DOKLADOV: ');
			END IF;
			INSERT INTO food600.doklady (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, poznamka, typ_pohybu, uctexport) 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, 0 AS uctsuma, c_mena, clock_timestamp() AS kdy, c_data, poznamka, typ_pohybu, uctexport FROM food600.bufferd WHERE bufferd.menobuf=meno AND bufdidr=akt_doklad AND bufferd.c_data=cdata;
			IF zapisuj=1 THEN 
				insert into food600.test (cas,text) values (clock_timestamp(),'BUFF_OUT ZAPIS DO DOKLADOV: ');
			END IF;
			IF moperace IN ('VP','PP') THEN
				PERFORM food600.maz_polotovary_novy(mc_dokl, mcdata, mc_stredisk);
			END IF;
			CASE
				WHEN moperace='PI' THEN
					OPEN opravainventury;
					LOOP
						FETCH opravainventury INTO invkarta, invmnoz, invzust, invcena, invcenaprum, invporadi, invidriadok;
						EXIT WHEN NOT FOUND;
						SELECT mnozstvi, zustatek, cena INTO bufmnoz, bufzust, bufcena FROM food600.buffer WHERE buffer.menobuf=meno AND buffer.bufdidr=akt_doklad AND buffer.c_data=cdata AND buffer.c_karty=invkarta AND buffer.poradi=invporadi;
						IF invmnoz=bufmnoz AND invzust=bufzust AND invcena=bufcena THEN
						ELSE
							UPDATE food600.obraty SET mnozstvi=bufmnoz, zustatek=bufzust, cena=bufcena WHERE idriadok=invidriadok;
						END IF;
					END LOOP;
				WHEN moperace='PE' OR moperace='VE' OR moperace='VR' OR moperace='VV' THEN
					OPEN opravainventuryzmazaneriadky;
					LOOP
						FETCH opravainventuryzmazaneriadky INTO invkarta, invmnoz, invzust, invcena, invcenaprum, invporadi, invidriadok;
						EXIT WHEN NOT FOUND;
						SELECT c_karty, mnozstvi, zustatek, cena, cena_part, dan, serial_no, sscc INTO bufkarta, bufmnoz, bufzust, bufcenaprum, bufcenapart, bufdan, bufserial, bufsscc FROM food600.buffer WHERE buffer.menobuf=meno AND buffer.bufdidr=akt_doklad AND buffer.c_data=cdata AND buffer.poradi=invporadi;
						CASE
							WHEN bufkarta IS NULL THEN
								DELETE FROM food600.obraty WHERE obraty.c_dokl=mc_dokl AND c_data=cdata AND poradi=invporadi;
								IF zapisuj=1 THEN 
									insert into food600.test (cas,text) values (clock_timestamp(),'BUFF_OUT VYMAZ Z OBRATOV PORADI='||invporadi);
								END IF;
							WHEN invkarta=bufkarta THEN
								UPDATE food600.obraty SET mnozstvi=bufmnoz, zustatek=bufzust, cena=bufcenaprum, cena_part=bufcenapart, dan=bufdan, serial_no=bufserial, sscc=bufsscc WHERE idriadok=invidriadok;
								IF zapisuj=1 THEN 
									insert into food600.test (cas,text) values (clock_timestamp(),'BUFF_OUT UPDATE OBRATY PORADI='||invporadi);
								END IF;
							ELSE
								DELETE FROM food600.obraty WHERE obraty.c_dokl=mc_dokl AND c_data=cdata AND poradi=invporadi;
								IF zapisuj=1 THEN 
									insert into food600.test (cas,text) values (clock_timestamp(),'BUFF_OUT VYMAZ Z OBRATOV PORADI='||invporadi);
								END IF;
								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, sscc) SELECT c_stredisk, operace, c_dokl,c_karty, mnozstvi, cena, cena_part, zustatek, cena AS cena_prum, dan, serial_no, poradi, c_data, spdan, spstupne, sscc FROM food600.buffer WHERE buffer.menobuf=meno AND buffer.bufdidr=akt_doklad AND buffer.c_data=cdata AND poradi=invporadi;
								IF zapisuj=1 THEN 
									insert into food600.test (cas,text) values (clock_timestamp(),'BUFF_OUT ZAPIS DO OBRATOV PORADI='||invporadi);
								END IF;
						END CASE;
					END LOOP;
					CLOSE opravainventuryzmazaneriadky;
					OPEN opravainventuryzmeneneriadky;
					LOOP
						FETCH opravainventuryzmeneneriadky INTO invkarta, invmnoz, invzust, invcena, invcenaprum, invporadi, invidriadok;
						EXIT WHEN NOT FOUND;
						SELECT c_karty, mnozstvi, zustatek, cena, cena_part, dan, serial_no, sscc INTO bufkarta, bufmnoz, bufzust, bufcenaprum, bufcenapart, bufdan, bufserial, bufsscc FROM food600.buffer WHERE buffer.menobuf=meno AND buffer.bufdidr=akt_doklad AND buffer.c_data=cdata AND buffer.poradi=invporadi;
						CASE
							WHEN bufkarta IS NULL THEN
								DELETE FROM food600.obraty WHERE obraty.c_dokl=mc_dokl AND c_data=cdata AND poradi=invporadi;
								IF zapisuj=1 THEN 
									insert into food600.test (cas,text) values (clock_timestamp(),'BUFF_OUT VYMAZ Z OBRATOV PORADI='||invporadi);
								END IF;
							WHEN invkarta=bufkarta THEN
								UPDATE food600.obraty SET mnozstvi=bufmnoz, zustatek=bufzust, cena=bufcenaprum, cena_part=bufcenapart, dan=bufdan, serial_no=bufserial, sscc=bufsscc WHERE idriadok=invidriadok;
								IF zapisuj=1 THEN 
									insert into food600.test (cas,text) values (clock_timestamp(),'BUFF_OUT UPDATE OBRATY PORADI='||invporadi);
								END IF;
							ELSE
								DELETE FROM food600.obraty WHERE obraty.c_dokl=mc_dokl AND c_data=cdata AND poradi=invporadi;
								IF zapisuj=1 THEN 
									insert into food600.test (cas,text) values (clock_timestamp(),'BUFF_OUT VYMAZ Z OBRATOV PORADI='||invporadi);
								END IF;
								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, sscc) SELECT c_stredisk, operace, c_dokl,c_karty, mnozstvi, cena, cena_part, zustatek, cena AS cena_prum, dan, serial_no, poradi, c_data, spdan, spstupne, sscc FROM food600.buffer WHERE buffer.menobuf=meno AND buffer.bufdidr=akt_doklad AND buffer.c_data=cdata AND poradi=invporadi;
								IF zapisuj=1 THEN 
									insert into food600.test (cas,text) values (clock_timestamp(),'BUFF_OUT ZAPIS DO OBRATOV PORADI='||invporadi);
								END IF;
						END CASE;
					END LOOP;
					CLOSE opravainventuryzmeneneriadky;
					OPEN novevbufferi;
					LOOP
						FETCH novevbufferi INTO invporadi;
						EXIT WHEN NOT FOUND;
						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, sscc) SELECT c_stredisk, operace, c_dokl,c_karty, mnozstvi, cena, cena_part, zustatek, cena AS cena_prum, dan, serial_no, poradi, c_data, spdan, spstupne, sscc FROM food600.buffer WHERE buffer.menobuf=meno AND buffer.bufdidr=akt_doklad AND buffer.c_data=cdata AND poradi=invporadi;
						IF zapisuj=1 THEN 
							insert into food600.test (cas,text) values (clock_timestamp(),'BUFF_OUT ZAPIS DO OBRATOV PORADI='||invporadi);
						END IF;
					END LOOP;
					CLOSE novevbufferi;
				ELSE
					DELETE FROM food600.obraty WHERE idriadok IN (SELECT idriadok FROM food600.obraty WHERE obraty.c_dokl=mc_dokl AND c_data=cdata ORDER BY poradi DESC);
					IF zapisuj=1 THEN 
						insert into food600.test (cas,text) values (clock_timestamp(),'BUFF_OUT VYMAZ Z OBRATOV: ');
					END IF;
					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, sscc) SELECT c_stredisk, operace, c_dokl,c_karty, mnozstvi, cena, cena_part, zustatek, cena AS cena_prum, dan, serial_no, poradi, c_data, spdan, spstupne, sscc FROM food600.buffer WHERE buffer.menobuf=meno AND buffer.bufdidr=akt_doklad AND buffer.c_data=cdata ORDER BY poradi;
					IF zapisuj=1 THEN 
						insert into food600.test (cas,text) values (clock_timestamp(),'BUFF_OUT ZAPIS DO OBRATOV: ');
					END IF;
			END CASE;
		WHEN moperace='PI' THEN
			INSERT INTO food600.doklady (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, uctexport, c_mena, kdy, c_data, poznamka, typ_pohybu) SELECT c_stredisk, c_dokl,c_partner,doklad,datum_dokl,operace,pz1,pz2,platba, trzba1, splatnost, sleva_abs, sleva_proc, cen_hlad, zaloha, mhalere AS halere, provedl, 0 AS uctsuma, 0 AS uctexport, c_mena, clock_timestamp() AS kdy, c_data, poznamka, typ_pohybu FROM food600.bufferd WHERE bufferd.menobuf=meno AND bufferd.bufdidr=akt_doklad AND bufferd.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, sscc) SELECT c_stredisk, operace, c_dokl,c_karty, mnozstvi, cena, cena_part, zustatek, cena AS cena_prum, dan, serial_no, poradi, c_data, spdan, spstupne, sscc FROM food600.buffer WHERE buffer.menobuf=meno AND buffer.bufdidr=akt_doklad AND buffer.c_data=cdata ORDER BY poradi;
		ELSE
			INSERT INTO food600.doklady (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, poznamka, typ_pohybu) 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, 0 AS uctsuma, c_mena, clock_timestamp() AS kdy, c_data, poznamka, typ_pohybu FROM food600.bufferd WHERE bufferd.menobuf=meno AND bufferd.bufdidr=akt_doklad AND bufferd.c_data=cdata;
			IF zapisuj=1 THEN 
				insert into food600.test (cas,text) values (clock_timestamp(),'BUFF_OUT ZAPIS DOKLADY');
			END IF;
			IF moperace='VK' OR moperace='VP' OR moperace='PP' THEN
				INSERT INTO food600.obraty (c_stredisk, operace, c_dokl, c_karty, mnozstvi, cena, cena_part, zustatek, cena_prum, dan, poradi, c_data, spdan, spstupne,sscc) SELECT c_stredisk, operace, c_dokl, c_karty, mnozstvi, cena, cena_part, zustatek, cena AS cena_prum, dan, poradi, c_data, spdan, spstupne,sscc FROM food600.buffer WHERE menobuf=meno AND bufdidr=akt_doklad AND mnozstvi<>0 AND c_data=cdata ORDER BY poradi, mnozstvi;
				IF mneriesdlhy=0 THEN
					INSERT INTO food600.dluhy (c_dokl, c_karty, mnozstvi, operace, c_data, c_stredisk) SELECT c_dokl, c_karty, SUM(ALL dluh_mnoz) as mnozstvi, operace, c_data, c_stredisk FROM food600.buffer WHERE menobuf=meno AND c_data=cdata AND bufdidr=akt_doklad AND dluh_mnoz<>0 GROUP BY c_dokl, c_karty, operace, c_data, c_stredisk;
				END IF;
			ELSE
				IF moperace='VV' OR moperace='VE' OR moperace='PV' OR moperace='PB' THEN
					IF moperace='PB' THEN
						INSERT INTO food600.obraty (c_stredisk, operace, c_dokl, c_karty, mnozstvi, cena, cena_part, zustatek, cena_prum, dan, poradi, c_data, spdan, spstupne,sscc) SELECT c_stredisk, operace, c_dokl, c_karty, mnozstvi, cena, cena_part, zustatek, cena AS cena_prum, dan, poradi, c_data, spdan, spstupne,sscc FROM food600.buffer WHERE menobuf=meno AND bufdidr=akt_doklad AND mnozstvi<>0 AND c_data=cdata ORDER BY poradi, mnozstvi, cena_part;
					ELSE
						INSERT INTO food600.obraty (c_stredisk, operace, c_dokl, c_karty, mnozstvi, cena, cena_part, zustatek, cena_prum, dan, poradi, c_data, serial_no, spdan, spstupne,sscc) SELECT c_stredisk, operace, c_dokl, c_karty, mnozstvi, cena, cena_part, zustatek, cena AS cena_prum, dan, poradi, c_data, serial_no, spdan, spstupne,sscc FROM food600.buffer WHERE menobuf=meno AND bufdidr=akt_doklad AND mnozstvi<>0 AND c_data=cdata ORDER BY poradi, mnozstvi;
					END IF;
				ELSE
					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,sscc) SELECT c_stredisk, operace, c_dokl,c_karty, mnozstvi, cena, cena_part, zustatek, cena AS cena_prum, dan, serial_no, poradi, c_data, spdan, spstupne,sscc FROM food600.buffer WHERE menobuf=meno AND bufdidr=akt_doklad AND c_data=cdata ORDER BY poradi, mnozstvi;
				END IF;
			END IF;
			IF zapisuj=1 THEN 
				insert into food600.test (cas,text) values (clock_timestamp(),'BUFF_OUT ZAPIS OBRATY');
			END IF;
			DELETE FROM food600.pombufout WHERE menobuf=meno;	
			IF zapisuj=1 THEN 
				insert into food600.test (cas,text) values (clock_timestamp(),'BUFF_OUT VYMAZ POMBUFOUT');
			END IF;
			IF food600.left(moperace,1)<>'Z' AND moperace<>'SF' THEN
				INSERT INTO food600.pombufout SELECT DISTINCT c_karty, c_stredisk,meno AS menobuf FROM food600.buffer WHERE c_karty>0 AND menobuf=meno AND bufdidr=akt_doklad;
				IF zapisuj=1 THEN 
					insert into food600.test (cas,text) values (clock_timestamp(),'BUFF_OUT NAPLNENIE POMBUFOUT');
				END IF;
				SELECT COUNT(*) INTO pocetriadkov FROM food600.pombufout WHERE menobuf=meno;
				IF zapisuj=1 THEN 
					insert into food600.test (cas,text) values (clock_timestamp(),'BUFF_OUT POMBUFOUT: '|| pocetriadkov);
				END IF;
	
				IF pocetriadkov>0 THEN
					OPEN prepbufout;
					WHILE akt_riadok<=pocetriadkov LOOP
						FETCH prepbufout INTO  akt_karta, akt_stredisko;
						IF zapisuj=1 THEN 
							insert into food600.test (cas,text, c_karty, c_stredisk) values (clock_timestamp(),'BUFF_OUT', akt_karta, akt_stredisko);
						END IF;
						akt_riadok:=akt_riadok+1;
					END LOOP;
					CLOSE prepbufout;
				END IF;
				DELETE FROM food600.pombufout WHERE menobuf=meno;
			END IF;
	END CASE;
END LOOP;
CLOSE prepbufdokl;
END
$BODY$;
