--
-- Name: pay_category_delete(); Type: FUNCTION; Schema: public; Owner: postgres
--
CREATE or replace FUNCTION pay_category_delete() RETURNS trigger
    LANGUAGE plpgsql
    AS $$
BEGIN
	UPDATE FIES.T_PAY_CATEGORY 
	SET LeftIndex = LeftIndex - 2
	WHERE LeftIndex > Old.LeftIndex;
	UPDATE FIES.T_PAY_CATEGORY 
	SET RightIndex = RightIndex - 2
	WHERE RightIndex > Old.LeftIndex;
	Return Old;
END;
$$;



--
-- Name: pay_category_afterdelete; Type: TRIGGER; Schema: public; Owner: sa
--
DO 
$$ 
BEGIN 
IF EXISTS(SELECT * 
                     FROM information_schema.triggers 
                     WHERE event_object_table = lower('T_PAY_CATEGORY') 
                     AND trigger_name = lower('pay_category_afterdelete')
                     ) 
                    THEN 
                    DROP TRIGGER pay_category_afterdelete ON FIES.T_PAY_CATEGORY;
    END IF ; 

END; 
$$ ;

CREATE TRIGGER pay_category_afterdelete AFTER DELETE ON FIES.T_PAY_CATEGORY FOR EACH ROW EXECUTE PROCEDURE pay_category_delete();
