CREATE OR REPLACE FUNCTION food600.koniecprepoctu(
	cestina integer DEFAULT 0,
	cdata integer DEFAULT 1)
    RETURNS integer
    LANGUAGE 'plpgsql'
    COST 100
    VOLATILE 
AS $BODY$
BEGIN
DELETE FROM food600.sklad WHERE c_data=cdata AND zustatek=0 AND c_karty IN (SELECT c_karty FROM food600.karty WHERE c_data=cdata AND nazev1 LIKE '%SMAZANO%');
IF cestina=1 THEN
	UPDATE food600.sklad SET cena_prum=karty.cena_prum FROM food600.karty WHERE zustatek=0 AND sklad.c_karty=karty.c_karty AND sklad.c_data=karty.c_data AND sklad.c_karty IN (SELECT DISTINCT c_karty FROM food600.polozky WHERE c_data=cdata);
END IF;	
ALTER TABLE food600.obraty ENABLE TRIGGER obraty_a_prepocet_pred;
ALTER TABLE food600.obraty ENABLE TRIGGER obraty_prepocet_po;

RETURN 1;

END
$BODY$;
