CREATE OR REPLACE FUNCTION food600.odpocet_vyroba(
	mmeno character varying,
	cdata integer,
	mcstredisk integer,
	mmenaakt integer,
	mdavka character varying)
    RETURNS integer
    LANGUAGE 'plpgsql'
    COST 100
    VOLATILE
AS $BODY$
DECLARE pocetvsk INTEGER;
	vystup INTEGER;
BEGIN
DELETE FROM food600.sk1 WHERE c_data=cdata AND menobuf=mmeno;
DELETE FROM food600.odp_log WHERE menobuf=mmeno AND c_data=cdata;
DELETE FROM food600.sk1 WHERE menobuf=mmeno AND c_data=cdata;
INSERT INTO food600.sk1 (menobuf, c_data, c_karty, mnozstvi) SELECT mmeno as menobuf, cdata as c_data, c_karty, SUM(mnozstvi) as mnozstvi FROM food600.davky WHERE davka=mdavka AND c_data=cdata AND c_stredisk=mcstredisk AND c_karty<>0 AND c_karty NOT IN (SELECT c_karty FROM food600.karty WHERE c_druhk IN (SELECT c_druh FROM food600.skupiny WHERE spart_kas='SRV')) GROUP BY c_karty, menobuf, c_data;
SELECT COUNT(*) INTO pocetvsk FROM food600.sk1 WHERE menobuf=mmeno AND c_data=cdata;
IF pocetvsk>0 THEN
	INSERT INTO food600.odp_log (hloubka, c_karty, hruba, menobuf, c_data, o_c_karty) SELECT 0 AS hloubka, c_karty, mnozstvi AS hruba, mmeno AS menobuf, c_data, c_karty FROM food600.sk1 WHERE menobuf = mmeno AND c_data=cdata;
	SELECT food600.kalkuluj_vyroba(mmeno, mmenaakt, cdata, mcstredisk) INTO vystup;
END IF;
DELETE FROM food600.odpocet_askl WHERE menobuf=mmeno AND c_data=cdata;
DELETE FROM food600.odpocet_kalk WHERE menobuf=mmeno AND c_data=cdata;
RETURN 1;
END
$BODY$;
