CREATE OR REPLACE FUNCTION food600.vstupdoopravyinventury(
	cdata integer,
	cstredisk integer,
	mmenobuf character varying,
	cdokl integer,
	mmena_akt integer,
	issklad integer)
    RETURNS void
    LANGUAGE 'plpgsql'
    COST 100
    VOLATILE
AS $BODY$
DECLARE kkarty CURSOR FOR SELECT c_karty, c_dokl, na_sklade FROM food600.buffer WHERE buffer.menobuf=mmenobuf AND buffer.c_data=cdata;
	ckarty INTEGER;
	ccdokl INTEGER;
	mzustatek numeric(20,10);
	nasklade numeric(20,10);
BEGIN
DELETE FROM food600.buffer WHERE c_data=cdata AND menobuf=mmenobuf;
DELETE FROM food600.bufferd WHERE c_data=cdata AND menobuf=mmenobuf;

INSERT INTO food600.bufferd (c_stredisk, c_dokl, c_partner, doklad, datum_dokl, operace, pz1, pz2, platba, halere, provedl, kdy, c_mena, menobuf, poznamka, c_data) SELECT c_stredisk, c_dokl, c_partner, doklad, datum_dokl, operace, pz1, pz2, platba, halere, provedl, kdy, c_mena, mmenobuf as menobuf, poznamka, c_data FROM food600.doklady WHERE doklady.c_data=cdata AND doklady.c_stredisk=cstredisk AND doklady.c_dokl=cdokl;
INSERT INTO food600.buffer (c_stredisk, operace, c_dokl, c_karty, mnozstvi, cena, cena_part, zustatek, spdan, spstupne, dan, serial_no,  poradi, menobuf, dluh_mnoz, na_sklade, c_data) SELECT c_stredisk, operace, c_dokl, c_karty, zustatek as mnozstvi, cena, cena_part, 0 AS zustatek, spdan, spstupne, dan, serial_no,  poradi, mmenobuf as menobuf,  mnozstvi, zustatek as na_sklade, c_data FROM food600.obraty WHERE obraty.c_data=cdata AND obraty.c_stredisk=cstredisk AND obraty.c_dokl=cdokl;
IF issklad THEN
	UPDATE food600.buffer SET nazev=karty.nazev, c_druh=karty.c_druhs FROM food600.karty WHERE buffer.menobuf=mmenobuf AND buffer.c_karty=karty.c_karty AND buffer.c_data=karty.c_data;
	UPDATE food600.buffer SET c_druh=karty.c_druhk FROM food600.karty WHERE buffer.menobuf=mmenobuf AND buffer.c_karty=karty.c_karty AND buffer.c_data=cdata AND (buffer.c_druh=0 OR buffer.c_druh IS NULL);
	UPDATE food600.buffer SET c_druh=karty.c_druh FROM food600.karty WHERE buffer.menobuf=mmenobuf AND buffer.c_karty=karty.c_karty AND buffer.c_data=cdata AND (buffer.c_druh=0 OR buffer.c_druh IS NULL);
ELSE
	UPDATE food600.buffer SET nazev=karty.nazev, c_druh=karty.c_druh FROM food600.karty WHERE buffer.menobuf=mmenobuf AND buffer.c_karty=karty.c_karty AND buffer.c_data=karty.c_data AND buffer.c_data=karty.c_data;
	UPDATE food600.buffer SET c_druh=karty.c_druhk FROM food600.karty WHERE buffer.menobuf=mmenobuf AND buffer.c_karty=karty.c_karty AND buffer.c_data=cdata AND (buffer.c_druh=0 OR buffer.c_druh IS NULL);
	UPDATE food600.buffer SET c_druh=karty.c_druhs FROM food600.karty WHERE buffer.menobuf=mmenobuf AND buffer.c_karty=karty.c_karty AND buffer.c_data=cdata AND (buffer.c_druh=0 OR buffer.c_druh IS NULL);
END IF;
UPDATE food600.buffer SET druh=skupiny.druh FROM food600.skupiny WHERE buffer.menobuf=mmenobuf AND buffer.c_druh=skupiny.c_druh AND buffer.c_data=skupiny.c_data;
UPDATE food600.buffer SET cena_celk = (mnozstvi - na_sklade) * cena WHERE buffer.menobuf=mmenobuf;
UPDATE food600.buffer SET c_dokl=aaa.c_dokl FROM (SELECT MIN(c_dokl) as c_dokl, c_karty FROM food600.obraty WHERE obraty.c_dokl>cdokl AND c_stredisk=cstredisk AND operace='PI' AND c_data=cdata GROUP BY c_karty) aaa WHERE buffer.c_karty=aaa.c_karty AND buffer.menobuf=mmenobuf AND buffer.c_data=cdata;
UPDATE food600.buffer SET c_dokl=2000111222 WHERE buffer.menobuf=mmenobuf AND buffer.c_data=cdata AND buffer.c_dokl IS NULL;
OPEN kkarty;
LOOP
	FETCH kkarty INTO ckarty, ccdokl, nasklade;
	EXIT WHEN NOT FOUND;
	SELECT MIN(zustatek) INTO mzustatek FROM food600.obraty WHERE obraty.c_stredisk=cstredisk AND obraty.c_data=cdata AND obraty.c_karty=ckarty AND (food600.left(obraty.operace,1)='P' OR food600.left(obraty.operace,1)='V') AND obraty.c_dokl>cdokl AND obraty.c_dokl<ccdokl;
	IF mzustatek IS NULL THEN
	ELSE
		IF mzustatek<nasklade THEN
			UPDATE food600.buffer SET zustatek=na_sklade-mzustatek WHERE buffer.menobuf=mmenobuf AND buffer.c_data=cdata AND buffer.c_karty=ckarty;
		END IF;
	END IF; 
END LOOP;
CLOSE kkarty;
UPDATE food600.buffer SET c_dokl=cdokl,	cena_celk = (mnozstvi - na_sklade) * cena WHERE buffer.menobuf=mmenobuf AND buffer.c_data=cdata;
END
$BODY$;
