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

    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;
