     LANGUAGE plpgsql
     AS $$
     DECLARE
-		_cal record;
-		_computed_date_pair date_pair;
-    BEGIN
-		BEGIN
-			-- Calculate new start/end computed date for current calendar
-			SELECT * FROM computecalendarsstartend(_calendar_id, _start_date, _end_date, _rank, _operator, _currentElementDeletion, _bit_mask) INTO _computed_date_pair;
-			-- Launch same operation an all parent calendars (could have none)
-			-- Infinite recursion must not happen because we previously check it with 'detectcalendarinclusionloop' function
-			FOR _cal IN
-				SELECT id, calendar_id, rank, operator FROM calendar_element WHERE _calendar_id = included_calendar_id
-			LOOP
-				-- Need to update current calendar element with new calculated values
-				UPDATE calendar_element SET start_date = _computed_date_pair.start_date, end_date = _computed_date_pair.end_date WHERE id = _cal.id;
-				-- To calculate the parent calendar, we need to pass (not commited yet) new start/end date of updated calendar_element (and rand to found it)
-				-- operator is passed because of the not already added calendar element (first call of this recursive function)
-				-- We set currentElementDeletion, because just want to update fields, ignoring no calendar
-				PERFORM propagateparentcalendarsstartend(_cal.calendar_id, _cal.rank, FALSE, _computed_date_pair.start_date, _computed_date_pair.end_date, _cal.operator, _computed_date_pair.bit_mask);
-			END LOOP;
-		EXCEPTION WHEN raise_exception THEN
-			RAISE EXCEPTION '% %Parent calendar element = %',SQLERRM , chr(10), _calendar_id;
-		END;
+        _cal record;
+        _computed_date_pair date_pair;
+    BEGIN
+        BEGIN
+            -- Calculate new start/end computed date for current calendar
+            SELECT * FROM computecalendarsstartend(_calendar_id, _start_date, _end_date, _rank, _operator, _currentElementDeletion, _bit_mask) INTO _computed_date_pair;
+            -- Launch same operation an all parent calendars (could have none)
+            -- Infinite recursion must not happen because we previously check it with 'detectcalendarinclusionloop' function
+            FOR _cal IN
+                SELECT id, calendar_id, rank, operator FROM calendar_element WHERE _calendar_id = included_calendar_id
+            LOOP
+                -- Need to update current calendar element with new calculated values
+                UPDATE calendar_element SET start_date = _computed_date_pair.start_date, end_date = _computed_date_pair.end_date WHERE id = _cal.id;
+                -- To calculate the parent calendar, we need to pass (not commited yet) new start/end date of updated calendar_element (and rand to found it)
+                -- operator is passed because of the not already added calendar element (first call of this recursive function)
+                -- We set currentElementDeletion, because just want to update fields, ignoring no calendar
+                PERFORM propagateparentcalendarsstartend(_cal.calendar_id, _cal.rank, FALSE, _computed_date_pair.start_date, _computed_date_pair.end_date, _cal.operator, _computed_date_pair.bit_mask);
+            END LOOP;
+        EXCEPTION WHEN raise_exception THEN
+            RAISE EXCEPTION '% %Parent calendar element = %',SQLERRM , chr(10), _calendar_id;
+        END;
     END;
     $$;
 COMMENT ON FUNCTION propagateparentcalendarsstartend(integer, integer, boolean, date, date, calendar_operator, bit varying) IS 'This recursive function launch computecalendarsstartend and call himself on an all parents calendar';

    LANGUAGE plpgsql
    AS $$
    DECLARE
	_cal record;
    BEGIN
		RAISE DEBUG 'LOOP cal = %', _included_calendar_id;
		-- Recursion stop condition : the loop is done
		IF _included_calendar_id = _first_calendar_id THEN
			RAISE EXCEPTION 'DETECT LOOP INCLUSION %--- Loop inclusion ---',chr(10);
		END IF;
		BEGIN
			-- Check all cal elt of the calendar of id "_included_calendar_id"
			FOR _cal IN
				SELECT included_calendar_id AS id FROM calendar_element WHERE calendar_id = _included_calendar_id AND included_calendar_id IS NOT NULL
			LOOP
				PERFORM detectcalendarinclusionloop(_cal.id, _first_calendar_id);
			END LOOP;
		EXCEPTION WHEN raise_exception THEN
			RAISE EXCEPTION '% %Parent calendar element = %',SQLERRM , chr(10), _included_calendar_id;
		END;
    END;
    $$;
COMMENT ON FUNCTION detectcalendarinclusionloop(integer, integer) IS 'This recursive function can detect loop inclusion. Will raise and trace calendar loop.';

CREATE OR REPLACE FUNCTION deletecalendarelement(_calendar_element_id integer) RETURNS void
    LANGUAGE plpgsql
    AS $$
    DECLARE
        _new_rank integer;
        _other_cal_elt record;
        _cal_elt record;
        _operator calendar_operator;
    BEGIN
        -- Get rank of deleted element
        SELECT rank, calendar_id FROM calendar_element WHERE id = _calendar_element_id INTO _cal_elt;
        IF _cal_elt.rank = 1 THEN
            SELECT operator FROM calendar_element WHERE calendar_id = _cal_elt.calendar_id AND rank = 2 INTO _operator;
            IF FOUND THEN
                IF _operator != '+'::calendar_operator THEN
                    RAISE EXCEPTION 'You cannot delete this rank 1 calendar because rank 2 calendar is not of "+" operator';
                END IF;
            END IF;
        END IF;
        -- Update calendars depending of the current one
        PERFORM propagateparentcalendarsstartend(_cal_elt.calendar_id, _cal_elt.rank, TRUE);
        -- Decrease rank of all element up to the current one
        FOR _other_cal_elt IN
            SELECT id, rank FROM calendar_element WHERE calendar_id = _cal_elt.calendar_id AND rank > _cal_elt.rank
        LOOP
            _new_rank := _other_cal_elt.rank - 1;
            UPDATE calendar_element SET rank = _new_rank WHERE id = _other_cal_elt.id;
        END LOOP;
        -- Finally we delete the element
        DELETE FROM calendar_element WHERE id = _calendar_element_id;
    END;
    $$;
COMMENT ON FUNCTION deletecalendarelement (integer) IS 'Delete record in table calendar_element. Trig a recalculation of parent calendars computed start stop date';
