CREATE OR REPLACE FUNCTION food600.vymaz_kartu(
	ckarty integer,
	cdata integer,
	odstran integer)
    RETURNS integer
    LANGUAGE 'plpgsql'
    COST 100
    VOLATILE 
AS $BODY$

BEGIN
DELETE FROM food600.polozky WHERE c_poloz = ckarty AND c_data=cdata;
DELETE FROM food600.sklad WHERE c_karty = ckarty AND c_data=cdata;
DELETE FROM food600.davky WHERE c_karty = ckarty AND c_data=cdata;
DELETE FROM food600.nlimity WHERE c_karty = ckarty AND c_data=cdata;
DELETE FROM food600.nlimitsablonypol WHERE c_karty = ckarty AND c_data=cdata;
DELETE FROM food600.eany WHERE c_karty = ckarty AND c_data=cdata;
DELETE FROM food600.bourak WHERE c_karty = ckarty AND c_data=cdata;
DELETE FROM food600.bourak WHERE c_data=cdata AND c_karty IN (SELECT DISTINCT c_karty FROM food600.bourak WHERE c_karty2 = ckarty AND c_data=cdata);
DELETE FROM food600.umisteni WHERE c_karty = ckarty AND c_data=cdata;
IF odstran THEN
	DELETE FROM food600.karty WHERE c_karty = ckarty AND c_data=cdata;
ELSE 
	UPDATE food600.karty SET nazev1='xxx SMAZANO xxx', bar_kod1='', bar_kod2='' WHERE c_karty = ckarty AND c_data=cdata;
END IF;
UPDATE food600.dluhy SET stornovano=1, provedl='MAZ', kdy=current_timestamp WHERE c_karty = ckarty AND c_data=cdata AND stornovano=0;
RETURN 1;
END
$BODY$;

