 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
+    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;
+        -- 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';

    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.';

    DECLARE
        _line_version_id integer;
    BEGIN
        UPDATE line_version SET end_date = current_date WHERE end_date IS NULL AND line_id = _line_id;
        INSERT INTO line_version (line_id, version, start_date, end_date, planned_end_date, name, forward_direction, backward_direction, bg_color, bg_hexa_color, fg_color, fg_hexa_color, accessibility, air_conditioned, certified, comment, depot) VALUES (_line_id, _version, _start_date, _end_date, _planned_end_date, _name, _forward_direction, _backward_direction, _bg_color, _bg_hexa_color, _fg_color, _fg_hexa_color, _accessibility, _air_conditioned, _certified, _comment, _depot) RETURNING line_version.id INTO _line_version_id;
        INSERT INTO line_version_datasource (line_version_id, datasource_id, code) VALUES (_line_version_id, _datasource, _code);
        RETURN _line_version_id;
