return: SETOF record
lang: plpgsql
parameters:
  p0:
    type: bigint
    name: i_rapport_id
  p1:
    type: text
    name: i_option
src: |
  DECLARE	
  	sqlcmd text;
  	
  	
  	_maxind integer;
  	
  	row  RECORD;
  BEGIN
  	
  	_maxind = activite.cti_prepare_rapport_maxoid(i_rapport_id);
  	
  	
  	IF (i_option <> 'COMP' AND i_rapport_id <> 0) THEN
  		FOR row IN 
  			SELECT 
  				'INDICATEURS_' || subview_s.table_name || '#' || subview_s.dimension_date, 
  				CASE WHEN SUM(subview.oid) > 0
  				THEN base.cti_group_concat(CASE WHEN subview.oid IS NOT NULL THEN total_function || '(' || activite.cti_prepare_rapport_get_column(column_name,'') || ')' ELSE 'NULL::numeric' END)
  				ELSE '' END
  			FROM 
  				(SELECT v_rapports_tables_indicateurs_1.code AS table_name, generate_series AS indicateur_id , dimension_date
  				FROM activite.v_rapports_tables_indicateurs_1, generate_series(1, _maxind), base.cti_unnest(Array['1','2','3','4','5']) AS dimension_date
  				WHERE dimensions_date LIKE '%' || dimension_date || '%') subview_s
  			LEFT JOIN 
  			(
  			
  			SELECT *
  			FROM activite.t_indicateurs 
  			WHERE oid IN (
  			SELECT base.cti_unnest(Array[indicateur_id, indicateur_associe_1_id, indicateur_associe_2_id, indicateur_associe_3_id, indicateur_associe_4_id, indicateur_associe_5_id, indicateur_associe_6_id, indicateur_associe_7_id])
  			FROM activite.t_rapports 
  				JOIN activite.t_rapports_rubriques ON t_rapports.oid = t_rapports_rubriques.rapport_id AND rapport_id = i_rapport_id
  				JOIN activite.t_indicateurs ON t_rapports_rubriques.indicateur_id = t_indicateurs.oid) 
  			) subview ON subview_s.indicateur_id = subview.oid AND subview.table_name = subview_s.table_name AND subview.dimension_date = subview_s.dimension_date
  			GROUP BY 1
  			UNION
  			SELECT 
  				'INDICATEURS_BUDGET'::text, 
  				CASE WHEN SUM(subview.oid) > 0
  				THEN base.cti_group_concat(CASE WHEN subview.oid IS NOT NULL THEN 'SUM' || '(' || activite.cti_prepare_rapport_get_column('CASE WHEN indicateur_id = ' || subview.oid::text || ' THEN budget ELSE 0 END','') || ')' ELSE 'NULL::numeric' END)
  				ELSE '' END
  			FROM 
  				(
  				SELECT *
  				FROM
  				(SELECT generate_series AS indicateur_id , ''::text AS dimension_date
  				FROM generate_series(1, _maxind)
  				ORDER BY 1) subview_s
  				LEFT JOIN 
  				(
  				
  				SELECT *
  				FROM activite.t_indicateurs 
  				WHERE oid IN (
  				SELECT base.cti_unnest(Array[indicateur_id, indicateur_associe_1_id, indicateur_associe_2_id, indicateur_associe_3_id, indicateur_associe_4_id, indicateur_associe_5_id, indicateur_associe_6_id, indicateur_associe_7_id])
  				FROM activite.t_rapports 
  					JOIN activite.t_rapports_rubriques ON t_rapports.oid = t_rapports_rubriques.rapport_id AND rapport_id = i_rapport_id
  					JOIN activite.t_indicateurs ON t_rapports_rubriques.indicateur_id = t_indicateurs.oid) 
  				AND oid IN (SELECT indicateur_id FROM activite.t_budget_cle_budget)
  				) subview ON subview_s.indicateur_id = subview.oid 
  				ORDER BY 1
  				) subview
  				GROUP BY 1
  			
  			ORDER BY 1
  			
  			
  			
  		LOOP
  			RETURN NEXT row;
  		END LOOP;
  	
  	END IF;
  		FOR row IN 
  			SELECT 
  				'INDICATEURS_WHERE_' || subview_s.table_name || '#' || subview_s.dimension_date, 
  				CASE WHEN SUM(subview.oid) > 0
  				THEN 
  					'AND (1<>1 ' || base.cti_group_concat_without_sep(
  						CASE WHEN subview.oid IS NOT NULL 
  						THEN ' OR ' || 
  							CASE WHEN total_function <> 'COUNT' THEN 'COALESCE(' || activite.cti_prepare_rapport_get_column(column_name,'') || ',0) <> 0' 
  							ELSE replace(activite.cti_prepare_rapport_get_column(column_name,''),'DISTINCT', '') || ' IS NOT NULL' END
  						ELSE '' END) || ')'
  				ELSE '' END
  			FROM 
  				(SELECT v_rapports_tables_indicateurs_1.code AS table_name, generate_series AS indicateur_id , dimension_date
  				FROM activite.v_rapports_tables_indicateurs_1, generate_series(1,_maxind), base.cti_unnest(Array['1','2','3','4','5']) AS dimension_date
  				WHERE dimensions_date LIKE '%' || dimension_date || '%') subview_s
  			LEFT JOIN 
  			(
  			
  			SELECT *
  			FROM activite.t_indicateurs 
  			WHERE oid IN (
  			SELECT base.cti_unnest(Array[indicateur_id, indicateur_associe_1_id, indicateur_associe_2_id, indicateur_associe_3_id, indicateur_associe_4_id, indicateur_associe_5_id, indicateur_associe_6_id, indicateur_associe_7_id])
  			FROM activite.t_rapports 
  				JOIN activite.t_rapports_rubriques ON t_rapports.oid = t_rapports_rubriques.rapport_id AND rapport_id = i_rapport_id
  				JOIN activite.t_indicateurs ON t_rapports_rubriques.indicateur_id = t_indicateurs.oid) 
  			) subview ON subview_s.indicateur_id = subview.oid AND subview.table_name = subview_s.table_name AND subview.dimension_date = subview_s.dimension_date
  			GROUP BY 1
  			UNION
  			SELECT 
  				'INDICATEURS_WHERE_BUDGET'::text, 
  				CASE WHEN SUM(subview.oid) > 0
  				THEN 
  					'AND (1<>1 ' || base.cti_group_concat_without_sep(
  						CASE WHEN subview.oid IS NOT NULL 
  						THEN ' OR ' || 
  							'COALESCE(' ||  activite.cti_prepare_rapport_get_column('CASE WHEN indicateur_id = ' || subview.oid || ' THEN budget ELSE 0 END','') || ',0) <> 0' 
  						ELSE '' END) || ')'
  				ELSE '' END
  			FROM 
  				(SELECT generate_series AS indicateur_id , ''::text AS dimension_date
  				FROM generate_series(1, _maxind)
  				GROUP BY 1 ORDER BY 1) subview_s
  			LEFT JOIN 
  			(
  			
  			SELECT *
  			FROM activite.t_indicateurs 
  			WHERE oid IN (
  			SELECT base.cti_unnest(Array[indicateur_id, indicateur_associe_1_id, indicateur_associe_2_id, indicateur_associe_3_id, indicateur_associe_4_id, indicateur_associe_5_id, indicateur_associe_6_id, indicateur_associe_7_id])
  			FROM activite.t_rapports 
  				JOIN activite.t_rapports_rubriques ON t_rapports.oid = t_rapports_rubriques.rapport_id AND rapport_id = i_rapport_id
  				JOIN activite.t_indicateurs ON t_rapports_rubriques.indicateur_id = t_indicateurs.oid) 
  			AND oid IN (SELECT indicateur_id FROM activite.t_budget_cle_budget)
  			) subview ON subview_s.indicateur_id = subview.oid 
  			GROUP BY 1
  			ORDER BY 1
  			
  			
  			
  		LOOP
  			RETURN NEXT row;
  		END LOOP;
  	IF (i_option = 'MONTH' OR i_option = 'COMP') AND i_rapport_id <> 0 THEN
  		FOR row IN 
  			SELECT 
  				'INDICATEURS_COLUMNS_' || subview_s.table_name || '#' || subview_s.dimension_date, 
  				CASE WHEN SUM(subview.oid) > 0
  				THEN base.cti_group_concat(CASE WHEN subview.oid IS NOT NULL THEN total_function || '(' || activite.cti_prepare_rapport_get_column(column_name,'MONTH') || ')' ELSE 'NULL::numeric' END)
  				ELSE '' END
  			FROM 
  				(SELECT v_rapports_tables_indicateurs_1.code AS table_name, generate_series AS indicateur_id , dimension_date
  				FROM activite.v_rapports_tables_indicateurs_1, generate_series(1, _maxind), base.cti_unnest(Array['1','2','3','4','5']) AS dimension_date
  				WHERE dimensions_date LIKE '%' || dimension_date || '%') subview_s
  			LEFT JOIN 
  			(
  			
  			SELECT *
  			FROM activite.t_indicateurs 
  			WHERE oid IN (
  			SELECT base.cti_unnest(Array[indicateur_id, indicateur_associe_1_id, indicateur_associe_2_id, indicateur_associe_3_id, indicateur_associe_4_id, indicateur_associe_5_id, indicateur_associe_6_id, indicateur_associe_7_id])
  			FROM activite.t_rapports 
  				JOIN activite.t_rapports_rubriques ON t_rapports.oid = t_rapports_rubriques.rapport_id AND rapport_id = i_rapport_id
  				JOIN activite.t_indicateurs ON t_rapports_rubriques.indicateur_id = t_indicateurs.oid) 
  			) subview ON subview_s.indicateur_id = subview.oid AND subview.table_name = subview_s.table_name AND subview.dimension_date = subview_s.dimension_date
  			GROUP BY 1
  			UNION
  			SELECT 
  				'INDICATEURS_COLUMNS_BUDGET'::text, 
  				CASE WHEN SUM(subview.oid) > 0
  				THEN base.cti_group_concat(CASE WHEN subview.oid IS NOT NULL THEN 'SUM' || '(' || activite.cti_prepare_rapport_get_column('CASE WHEN indicateur_id = ' || subview.oid::text || ' THEN budget ELSE 0 END','MONTH') || ')' ELSE 'NULL::numeric' END)
  				ELSE '' END
  			FROM 
  				(SELECT subview_s.indicateur_id, subview_s.dimension_date, subview.oid
  				FROM
  					(SELECT generate_series AS indicateur_id , ''::text AS dimension_date
  					FROM generate_series(1, _maxind)
  					GROUP BY 1 ORDER BY 1) subview_s
  				LEFT JOIN 
  				(
  				
  				SELECT *
  				FROM activite.t_indicateurs 
  				WHERE oid IN (
  				SELECT base.cti_unnest(Array[indicateur_id, indicateur_associe_1_id, indicateur_associe_2_id, indicateur_associe_3_id, indicateur_associe_4_id, indicateur_associe_5_id, indicateur_associe_6_id, indicateur_associe_7_id])
  				FROM activite.t_rapports 
  					JOIN activite.t_rapports_rubriques ON t_rapports.oid = t_rapports_rubriques.rapport_id AND rapport_id = i_rapport_id
  					JOIN activite.t_indicateurs ON t_rapports_rubriques.indicateur_id = t_indicateurs.oid) 
  				AND oid IN (SELECT indicateur_id FROM activite.t_budget_cle_budget)
  				) subview ON subview_s.indicateur_id = subview.oid
  				ORDER BY 1 
  				) subview
  			GROUP BY 1
  			ORDER BY 1
  			
  			
  			
  		LOOP
  			RETURN NEXT row;
  		END LOOP;
  	
  	END IF;
  	IF (i_option = 'DASHBOARD') THEN
  		FOR row IN 
  			SELECT 
  				'INDICATEURS_' || subview_s.table_name || '#' || subview_s.dimension_date, 
  				CASE WHEN SUM(subview.oid) > 0
  				THEN base.cti_group_concat(CASE WHEN subview.oid IS NOT NULL THEN total_function || '(' || activite.cti_prepare_rapport_get_column(column_name,'') || ')' ELSE 'NULL::numeric' END)
  				ELSE '' END
  			FROM 
  				(SELECT v_rapports_tables_indicateurs_1.code AS table_name, generate_series AS indicateur_id , dimension_date
  				FROM activite.v_rapports_tables_indicateurs_1, generate_series(1, _maxind), base.cti_unnest(Array['1','2','3','4','5']) AS dimension_date
  				WHERE dimensions_date LIKE '%' || dimension_date || '%') subview_s
  			LEFT JOIN 
  			(
  			
  			SELECT *
  			FROM activite.t_indicateurs 
  			WHERE oid IN (
  			SELECT base.cti_unnest(Array[oid, indicateur_associe_1_id, indicateur_associe_2_id, indicateur_associe_3_id, indicateur_associe_4_id, indicateur_associe_5_id])
  			FROM activite.t_indicateurs WHERE code <> '' AND t_indicateurs.oid > 0 AND (indicateur_reference_id <> 0 OR COALESCE(dimension_date_2,'') = '')) 
  			) subview ON subview_s.indicateur_id = subview.oid AND subview.table_name = subview_s.table_name AND subview.dimension_date = subview_s.dimension_date
  			GROUP BY 1
  			ORDER BY 1
  			
  			
  			
  		LOOP
  			RETURN NEXT row;
  		END LOOP;
  	
  	END IF;
  	
  	RETURN ;
  END;