CREATE OR REPLACE FUNCTION food600.vstupdoinventury(
	cdata integer,
	cstredisk integer,
	mmenobuf character varying,
	pokracovat integer,
	mmena_akt integer,
	issklad integer,
	isvybrane integer)
    RETURNS void
    LANGUAGE 'plpgsql'
    COST 100
    VOLATILE
AS $BODY$
DECLARE mbourskup CHARACTER(2);
BEGIN
SELECT bour_skup INTO mbourskup FROM food600.food_dat WHERE c_stredisk=cstredisk;
IF pokracovat=1 THEN
	DELETE FROM food600.inventur WHERE inventur.c_data=cdata AND inventur.c_stredisk=cstredisk AND inventur.c_karty NOT IN (SELECT c_karty FROM food600.sklad WHERE sklad.c_data=cdata AND sklad.c_stredisk=cstredisk);
ELSE
	DELETE FROM food600.inventur WHERE inventur.c_data=cdata AND inventur.c_stredisk=cstredisk;
END IF;
DELETE FROM food600.inventur WHERE inventur.c_data=cdata AND c_karty IN (SELECT DISTINCT c_karty FROM food600.karty WHERE c_data=cdata AND nazev1='xxx SMAZANO xxx            ');

IF pokracovat=1 THEN
	IF isvybrane=1 THEN
		INSERT INTO food600.buffer (c_karty, c_stredisk, mnozstvi, menobuf, operace, c_data) SELECT c_karty, c_stredisk, mnozstvi, mmenobuf as menobuf,'PI' as operace, cdata AS c_data FROM food600.inventur WHERE inventur.c_data=cdata AND inventur.c_stredisk=cstredisk AND inventur.vk='V';
	ELSE
		INSERT INTO food600.buffer (c_karty, c_stredisk, mnozstvi, menobuf, operace, c_data) SELECT c_karty, c_stredisk, mnozstvi, mmenobuf as menobuf,'PI' as operace, cdata AS c_data FROM food600.inventur WHERE inventur.c_data=cdata AND inventur.c_stredisk=cstredisk AND inventur.vk<>'V';
	END IF;
	UPDATE food600.buffer SET na_sklade=sklad.zustatek, cena=food600.prep_meny(sklad.c_mena,mmena_akt,sklad.cena_prum) FROM food600.sklad WHERE buffer.c_stredisk=sklad.c_stredisk AND buffer.c_karty=sklad.c_karty AND sklad.c_data=cdata AND buffer.menobuf=mmenobuf;
END IF; 
IF isvybrane=0 THEN
	INSERT INTO food600.buffer (c_karty, c_stredisk, mnozstvi, menobuf, operace, na_sklade, cena, c_data) SELECT c_karty, c_stredisk, zustatek AS mnozstvi, mmenobuf as menobuf,'PI' as operace, zustatek as na_sklade, food600.prep_meny(sklad.c_mena, mmena_akt, sklad.cena_prum) as cena, sklad.c_data FROM food600.sklad WHERE sklad.c_data=cdata AND sklad.c_stredisk=cstredisk AND sklad.c_karty NOT IN (SELECT c_karty FROM food600.buffer WHERE buffer.menobuf=mmenobuf AND buffer.c_data=cdata);
END IF;
IF issklad THEN
	UPDATE food600.buffer SET nazev=karty.nazev, c_druh=karty.c_druhs, serial_no='xx' FROM food600.karty WHERE buffer.menobuf=mmenobuf AND buffer.c_karty=karty.c_karty AND buffer.c_data=karty.c_data;
ELSE
	IF isvybrane=0 THEN
		INSERT INTO food600.buffer (menobuf, c_karty, c_data, c_stredisk, operace) SELECT mmenobuf as menobuf, bourak.c_karty, cdata as c_data, cstredisk as c_stredisk, 'PI' as operace FROM food600.bourak INNER JOIN food600.sklad ON bourak.c_karty2=sklad.c_karty AND bourak.c_data=sklad.c_data WHERE bourak.c_data=cdata AND bourak.bour_skup = mbourskup AND sklad.c_stredisk=cstredisk EXCEPT SELECT DISTINCT buffer.menobuf, buffer.c_karty, buffer.c_data, buffer.c_stredisk, buffer.operace FROM food600.buffer WHERE buffer.menobuf=mmenobuf AND buffer.c_data=cdata; 
	END IF;
	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=cdata;
	UPDATE food600.buffer SET c_druh=karty.c_druhs, serial_no='** BOUR **' 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=cdata AND buffer.c_druh=0 AND karty.c_druhs>0;
	UPDATE food600.buffer SET c_druh=karty.c_druhk, serial_no='** KALK **' 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=cdata AND buffer.c_druh=0 AND karty.c_druhk>0;
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 AND skupiny.c_data=cdata;
UPDATE food600.buffer SET cena_celk = (mnozstvi - na_sklade) * cena WHERE buffer.menobuf=mmenobuf;
DELETE FROM food600.buffer WHERE buffer.menobuf=mmenobuf AND c_karty IN (SELECT DISTINCT c_karty FROM food600.karty WHERE c_data=cdata AND nazev1='xxx SMAZANO xxx            ');
END
$BODY$;
