CREATE OR REPLACE FUNCTION food600.vstupdoprecenenia(
	cdata integer,
	cstredisk integer,
	mmenobuf character varying,
	mmena_akt integer,
	issklad integer)
    RETURNS void
    LANGUAGE 'plpgsql'
    COST 100
    VOLATILE
AS $BODY$
BEGIN
INSERT INTO food600.buffer (c_karty, c_stredisk, mnozstvi, zustatek, cena, menobuf, operace, c_data, serial_no) SELECT c_karty, c_stredisk, zustatek as mnozstvi, zustatek, food600.prep_meny(sklad.c_mena, mmena_akt, sklad.cena_prum) as cena,mmenobuf as menobuf,'PC' as operace, c_data,'' as serial_no  FROM food600.sklad WHERE c_data=cdata AND c_stredisk=cstredisk AND zustatek<>0;
UPDATE food600.buffer SET cena_part=cena WHERE c_data=cdata AND buffer.menobuf=mmenobuf;
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=cdata;
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=cdata;
END IF;
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_druh=0 AND buffer.c_data=cdata;
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_druh=0 AND buffer.c_data=cdata;
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_druh=0 AND buffer.c_data=cdata;
UPDATE food600.buffer SET druh=skupiny.druh, dan=skupiny.dan01 FROM food600.skupiny WHERE buffer.menobuf=mmenobuf AND buffer.c_druh=skupiny.c_druh AND skupiny.c_data=cdata;
UPDATE food600.buffer SET druh='', dan=1 WHERE buffer.menobuf=mmenobuf AND buffer.druh IS NULL;
END
$BODY$;
