return: text
lang: plpgsql
src: |
  DECLARE	
    
    sqlcmd text;
    def_cursor refcursor;
    _schemaname text;
    result TEXT;
  BEGIN
  
    -- Etablissements
    DROP TABLE IF EXISTS w_indicateur_condition;
    CREATE TEMP TABLE w_indicateur_condition AS
    SELECT 
      'CTI_FINESS#' || v_source.oid::text AS code,
      COALESCE(t_classes.texte || '. ' || t_classes_sections.texte,'Etablissement ' || v_source.texte::text) AS texte,
      v_source.texte AS texte_court,
      'finess_id'::text AS column_name,
      ''::text AS rule,
      v_source.oid::text AS value,
      'FINESS='||v_source.oid AS view_select_rule,
      ARRAY[
        CASE 
          WHEN v_source.oid LIKE 'LS%' THEN '1-Classe' || substr(v_source.oid,3,2)
          WHEN v_source.is_cti = '1' AND (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') THEN '2-Liste CTI' 
          WHEN v_source.is_cti <> '1' AND (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') THEN '3-Liste CTI' 
          ELSE '9' END,
        CASE 
          WHEN v_source.oid LIKE 'LS%' THEN t_classes_sections.code
          ELSE v_source.oid END
          
      ] AS arbre_code,
      ARRAY[
        CASE 
          WHEN v_source.oid LIKE 'LS%' THEN t_classes.texte
          WHEN v_source.is_cti = '1' AND (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') THEN 'Listes CTI' 
          WHEN v_source.is_cti <> '1' AND (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') THEN 'Listes Etablissement' 
          ELSE 'Valeurs' END,
        CASE 
          WHEN v_source.oid LIKE 'LS%' THEN t_classes_sections.texte
          ELSE v_source.texte END
          
      ] AS arbre_texte
    FROM activite.v_finess_1 v_source
      LEFT JOIN base.t_classes_sections ON v_source.oid LIKE 'LS%' AND
        t_classes_sections.oid::text = substr(v_source.oid,5)
      LEFT JOIN base.t_classes ON t_classes.oid = t_classes_sections.classe_id
      LEFT JOIN base.t_listes_tables t_listes_tables_classes ON t_classes.table_id = t_listes_tables_classes.oid
      LEFT JOIN base.t_listes ON (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') AND
        t_listes.oid::text = substr(v_source.oid,3) AND
        t_listes.code <> ''
      LEFT JOIN activite.t_listes_tables t_listes_tables_listes ON t_listes.table_id = t_listes_tables_listes.oid
    WHERE v_source.oid NOT LIKE 'LC%' AND
      v_source.oid <> 'DT0' AND
      (v_source.oid NOT LIKE 'LS%' OR substr(v_source.oid,5) <> '0') AND
      v_source.oid <> '-1'
    ;

    -- Unités médicales
    INSERT INTO w_indicateur_condition
    SELECT 
      'CTI_UMMOUVEMENT#' || v_source.oid::text AS code,
      COALESCE(t_classes.texte || '. ' || t_classes_sections.texte,'UM ' || v_source.texte::text) AS texte,
      v_source.texte AS texte_court,
      'lieu_unite_medicale_id'::text AS column_name,
      ''::text AS rule,
      v_source.oid::text AS value,
      'UM_MOUVEMENT='||v_source.oid AS view_select_rule,
      ARRAY[
        CASE 
          WHEN v_source.oid LIKE 'LS%' THEN '1-Classe' || substr(v_source.oid,3,2)
          WHEN v_source.is_cti = '1' AND (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') THEN '2-Liste CTI' 
          WHEN v_source.is_cti <> '1' AND (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') THEN '3-Liste CTI' 
          ELSE '9' END,
        CASE 
          WHEN v_source.oid LIKE 'LS%' THEN 'LS#'||t_listes_tables_classes.code||':'||CASE WHEN t_classes.code <> '' THEN t_classes.code ELSE '*CLS'||to_char(t_classes.sequence,'FM00') END||':'||t_classes_sections.code
          WHEN v_source.oid LIKE 'LI%' THEN 'LI#'||t_listes_tables_listes.code||':'||CASE WHEN t_listes.code <> '' THEN t_listes.code ELSE '*TXT:'||upper(t_listes.texte) END
          WHEN v_source.oid LIKE 'SL%' THEN 'SL#'||t_listes_tables_listes.code||':'||CASE WHEN t_listes.code <> '' THEN t_listes.code ELSE '*TXT:'||upper(t_listes.texte) END
          WHEN v_source.oid LIKE 'UM%' THEN COALESCE('UM#'||t_source.code,v_source.texte)
          ELSE '###'||v_source.oid END
          
      ] AS arbre_code,
      ARRAY[
        CASE 
          WHEN v_source.oid LIKE 'LS%' THEN t_classes.texte
          WHEN v_source.is_cti = '1' AND (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') THEN 'Listes CTI' 
          WHEN v_source.is_cti <> '1' AND (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') THEN 'Listes Etablissement' 
          ELSE 'Valeurs' END,
        v_source.texte
      ] AS arbre_texte
    FROM activite.v_unites_medicales_2 v_source
      LEFT JOIN activite.t_classes_sections ON v_source.oid LIKE 'LS%' AND
      t_classes_sections.oid::text = substr(v_source.oid,5)
      LEFT JOIN activite.t_classes ON t_classes.oid = t_classes_sections.classe_id
      LEFT JOIN activite.t_listes_tables t_listes_tables_classes ON t_classes.table_id = t_listes_tables_classes.oid
      LEFT JOIN activite.t_listes ON (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') AND
        t_listes.oid::text = substr(v_source.oid,3) AND
        t_listes.code <> ''
      LEFT JOIN activite.t_listes_tables t_listes_tables_listes ON t_listes.table_id = t_listes_tables_listes.oid
      LEFT JOIN activite.t_unites_medicales t_source ON v_source.oid LIKE 'UM%' AND t_source.oid = substr(v_source.oid,3)
    WHERE v_source.oid NOT LIKE 'LC%' AND
      (v_source.oid NOT LIKE 'LS%' OR substr(v_source.oid,5) <> '0') AND
      v_source.oid <> '-1' AND
      (v_source.oid LIKE 'UM%' OR v_source.oid LIKE 'LS%' OR v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%')AND
      v_source.oid <> 'UM0'
    ;

    -- Etablissements
    INSERT INTO w_indicateur_condition
    SELECT 
      'CTI_POLE#' || v_source.oid::text AS code,
      COALESCE(t_classes.texte || '. ' || t_classes_sections.texte,'Pole ' || v_source.texte::text) AS texte,
      v_source.texte AS texte_court,
      'pole_id'::text AS column_name,
      ''::text AS rule,
      v_source.oid::text AS value,
      'POLE='||v_source.oid AS view_select_rule,
      ARRAY[
        CASE 
          WHEN v_source.oid LIKE 'LS%' THEN '1-Classe' || substr(v_source.oid,3,2)
          WHEN v_source.is_cti = '1' AND (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') THEN '2-Liste CTI' 
          WHEN v_source.is_cti <> '1' AND (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') THEN '3-Liste CTI' 
          ELSE '9' END,
        CASE 
          WHEN v_source.oid LIKE 'LS%' THEN t_classes_sections.code
          ELSE v_source.oid END
          
      ] AS arbre_code,
      ARRAY[
        CASE 
          WHEN v_source.oid LIKE 'LS%' THEN t_classes.texte
          WHEN v_source.is_cti = '1' AND (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') THEN 'Listes CTI' 
          WHEN v_source.is_cti <> '1' AND (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') THEN 'Listes Etablissement' 
          ELSE 'Valeurs' END,
        CASE 
          WHEN v_source.oid LIKE 'LS%' THEN t_classes_sections.texte
          ELSE v_source.texte END
          
      ] AS arbre_texte
    FROM activite.v_pole_2 v_source
      LEFT JOIN base.t_classes_sections ON v_source.oid LIKE 'LS%' AND
        t_classes_sections.oid::text = substr(v_source.oid,5)
      LEFT JOIN base.t_classes ON t_classes.oid = t_classes_sections.classe_id
      LEFT JOIN base.t_listes_tables t_listes_tables_classes ON t_classes.table_id = t_listes_tables_classes.oid
      LEFT JOIN base.t_listes ON (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') AND
        t_listes.oid::text = substr(v_source.oid,3) AND
        t_listes.code <> ''
      LEFT JOIN activite.t_listes_tables t_listes_tables_listes ON t_listes.table_id = t_listes_tables_listes.oid
    WHERE v_source.oid NOT LIKE 'LC%' AND
      v_source.oid <> 'DT0' AND
      (v_source.oid NOT LIKE 'LS%' OR substr(v_source.oid,5) <> '0') AND
      v_source.oid <> '-1'
    ;
    
    -- Formes d'activité
    INSERT INTO w_indicateur_condition
    SELECT 
      'CTI_FORMEACTIVITE#' || v_source.oid::text AS code,
      COALESCE(t_classes.texte || '. ' || t_classes_sections.texte,'Forme d''activité ' || v_source.texte::text) AS texte,
      v_source.texte AS texte_court,
      'forme_activite_id'::text AS column_name,
      ''::text AS rule,
      v_source.oid::text AS value,
      'FORMEACTIVITE='||v_source.oid AS view_select_rule,
      ARRAY[
        CASE 
          WHEN v_source.oid LIKE 'LS%' THEN '1-Classe' || substr(v_source.oid,3,2)
          WHEN v_source.is_cti = '1' AND (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') THEN '2-Liste CTI' 
          WHEN v_source.is_cti <> '1' AND (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') THEN '3-Liste CTI' 
          ELSE '9' END,
        CASE 
          WHEN v_source.oid LIKE 'LS%' THEN 'LS#'||t_listes_tables_classes.code||':'||CASE WHEN t_classes.code <> '' THEN t_classes.code ELSE '*CLS'||to_char(t_classes.sequence,'FM00') END||':'||t_classes_sections.code
          WHEN v_source.oid LIKE 'LI%' THEN 'LI#'||t_listes_tables_listes.code||':'||CASE WHEN t_listes.code <> '' THEN t_listes.code ELSE '*TXT:'||upper(t_listes.texte) END
          WHEN v_source.oid LIKE 'SL%' THEN 'SL#'||t_listes_tables_listes.code||':'||CASE WHEN t_listes.code <> '' THEN t_listes.code ELSE '*TXT:'||upper(t_listes.texte) END
          WHEN v_source.oid LIKE 'DT%' THEN COALESCE('DT#'||t_source.code,v_source.oid)
          ELSE '###'||v_source.oid END
          
      ] AS arbre_code,
      ARRAY[
        CASE 
          WHEN v_source.oid LIKE 'LS%' THEN t_classes.texte
          WHEN v_source.is_cti = '1' AND (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') THEN 'Listes CTI' 
          WHEN v_source.is_cti <> '1' AND (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') THEN 'Listes Etablissement' 
          ELSE 'Valeurs' END,
        CASE 
          WHEN v_source.oid LIKE 'LS%' THEN t_classes_sections.texte
          ELSE v_source.texte END
          
      ] AS arbre_texte
    FROM activite.v_forme_activite_2 v_source
      LEFT JOIN activite.t_classes_sections ON v_source.oid LIKE 'LS%' AND
        t_classes_sections.oid::text = substr(v_source.oid,5)
      LEFT JOIN activite.t_classes ON t_classes.oid = t_classes_sections.classe_id
      LEFT JOIN activite.t_listes_tables t_listes_tables_classes ON t_classes.table_id = t_listes_tables_classes.oid
      LEFT JOIN activite.t_listes ON (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') AND
        t_listes.oid::text = substr(v_source.oid,3) AND
        t_listes.code <> ''
      LEFT JOIN activite.t_listes_tables t_listes_tables_listes ON t_listes.table_id = t_listes_tables_listes.oid
      LEFT JOIN activite.t_forme_activite t_source ON v_source.oid LIKE 'DT%' AND t_source.oid = substr(v_source.oid,3)
    WHERE v_source.oid NOT LIKE 'LC%' AND
      (v_source.oid NOT LIKE 'LS%' OR substr(v_source.oid,5) <> '0') AND
      v_source.oid <> '-1'
    ;
  
    -- Filières de soin
    INSERT INTO w_indicateur_condition
    SELECT 
      'CTI_FILIERESOIN#' || v_source.oid::text AS code,
      COALESCE(t_classes.texte || '. ' || t_classes_sections.texte,'Filiere soins ' || v_source.texte::text) AS texte,
      v_source.texte AS texte_court,
      'filiere_soin_id'::text AS column_name,
      ''::text AS rule,
      v_source.oid::text AS value,
      'FILIERESOINSEJOUR='||v_source.oid AS view_select_rule,
      ARRAY[
        CASE 
          WHEN v_source.oid LIKE 'LS%' THEN '1-Classe' || substr(v_source.oid,3,2)
          WHEN v_source.is_cti = '1' AND (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') THEN '2-Liste CTI' 
          WHEN v_source.is_cti <> '1' AND (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') THEN '3-Liste CTI' 
          ELSE '9' END,
        CASE 
          WHEN v_source.oid LIKE 'LS%' THEN 'LS#'||t_listes_tables_classes.code||':'||CASE WHEN t_classes.code <> '' THEN t_classes.code ELSE '*CLS'||to_char(t_classes.sequence,'FM00') END||':'||t_classes_sections.code
          WHEN v_source.oid LIKE 'LI%' THEN 'LI#'||t_listes_tables_listes.code||':'||CASE WHEN t_listes.code <> '' THEN t_listes.code ELSE '*TXT:'||upper(t_listes.texte) END
          WHEN v_source.oid LIKE 'SL%' THEN 'SL#'||t_listes_tables_listes.code||':'||CASE WHEN t_listes.code <> '' THEN t_listes.code ELSE '*TXT:'||upper(t_listes.texte) END
          WHEN v_source.oid LIKE 'DT%' THEN COALESCE('DT#'||t_source.code,v_source.oid)
          ELSE '###'||v_source.oid END
          
      ] AS arbre_code,
      ARRAY[
        CASE 
          WHEN v_source.oid LIKE 'LS%' THEN t_classes.texte
          WHEN v_source.is_cti = '1' AND (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') THEN 'Listes CTI' 
          WHEN v_source.is_cti <> '1' AND (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') THEN 'Listes Etablissement' 
          ELSE 'Valeurs' END,
        CASE 
          WHEN v_source.oid LIKE 'LS%' THEN t_classes_sections.texte
          ELSE v_source.texte END
          
      ] AS arbre_texte
    FROM activite.v_filiere_soin_2 v_source
      LEFT JOIN activite.t_classes_sections ON v_source.oid LIKE 'LS%' AND
        t_classes_sections.oid::text = substr(v_source.oid,5)
      LEFT JOIN activite.t_classes ON t_classes.oid = t_classes_sections.classe_id
      LEFT JOIN activite.t_listes_tables t_listes_tables_classes ON t_classes.table_id = t_listes_tables_classes.oid
      LEFT JOIN activite.t_listes ON (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') AND
        t_listes.oid::text = substr(v_source.oid,3) AND
        t_listes.code <> ''
      LEFT JOIN activite.t_listes_tables t_listes_tables_listes ON t_listes.table_id = t_listes_tables_listes.oid
      LEFT JOIN activite.t_filiere_soin t_source ON v_source.oid LIKE 'DT%' AND t_source.oid = substr(v_source.oid,3)
    WHERE v_source.oid NOT LIKE 'LC%' AND
      (v_source.oid NOT LIKE 'LS%' OR substr(v_source.oid,5) <> '0') AND
      v_source.oid <> '-1'
    ;
  
    -- Types de séjour
    INSERT INTO w_indicateur_condition
    SELECT 
      'CTI_TYPESEJOUR#' || v_source.oid::text AS code,
      COALESCE(t_classes.texte || '. ' || t_classes_sections.texte,'Type de séjour ' || v_source.texte::text) AS texte,
      v_source.texte AS texte_court,
      'type_sejour_id'::text AS column_name,
      ''::text AS rule,
      v_source.oid::text AS value,
      'TYPESEJOUR='||v_source.oid AS view_select_rule,
      ARRAY[
        CASE 
          WHEN v_source.oid LIKE 'LS%' THEN '1-Classe' || substr(v_source.oid,3,2)
          WHEN v_source.is_cti = '1' AND (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') THEN '2-Liste CTI' 
          WHEN v_source.is_cti <> '1' AND (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') THEN '3-Liste CTI' 
          ELSE '9' END,
        CASE 
          WHEN v_source.oid LIKE 'LS%' THEN 'LS#'||t_listes_tables_classes.code||':'||CASE WHEN t_classes.code <> '' THEN t_classes.code ELSE '*CLS'||to_char(t_classes.sequence,'FM00') END||':'||t_classes_sections.code
          WHEN v_source.oid LIKE 'LI%' THEN 'LI#'||t_listes_tables_listes.code||':'||CASE WHEN t_listes.code <> '' THEN t_listes.code ELSE '*TXT:'||upper(t_listes.texte) END
          WHEN v_source.oid LIKE 'SL%' THEN 'SL#'||t_listes_tables_listes.code||':'||CASE WHEN t_listes.code <> '' THEN t_listes.code ELSE '*TXT:'||upper(t_listes.texte) END
          WHEN v_source.oid LIKE 'DT%' THEN v_source.oid
          ELSE '###'||v_source.oid END
          
      ] AS arbre_code,
      ARRAY[
        CASE 
          WHEN v_source.oid LIKE 'LS%' THEN t_classes.texte
          WHEN v_source.is_cti = '1' AND (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') THEN 'Listes CTI' 
          WHEN v_source.is_cti <> '1' AND (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') THEN 'Listes Etablissement' 
          ELSE 'Valeurs' END,
        CASE 
          WHEN v_source.oid LIKE 'LS%' THEN t_classes_sections.texte
          ELSE v_source.texte END
      ] AS arbre_texte
    FROM activite.v_type_sejour_1 v_source
      LEFT JOIN activite.t_classes_sections ON v_source.oid LIKE 'LS%' AND
      t_classes_sections.oid::text = substr(v_source.oid,5)
      LEFT JOIN activite.t_classes ON t_classes.oid = t_classes_sections.classe_id
      LEFT JOIN activite.t_listes_tables t_listes_tables_classes ON t_classes.table_id = t_listes_tables_classes.oid
      LEFT JOIN activite.t_listes ON (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') AND
        t_listes.oid::text = substr(v_source.oid,3) AND
        t_listes.code <> ''
      LEFT JOIN activite.t_listes_tables t_listes_tables_listes ON t_listes.table_id = t_listes_tables_listes.oid
    WHERE v_source.oid NOT LIKE 'LC%' AND
      (v_source.oid NOT LIKE 'LS%' OR substr(v_source.oid,5) <> '0') AND
      v_source.oid <> '-1'
    ;
  
    -- Durée séjour
    INSERT INTO w_indicateur_condition
    SELECT 
      'CTI_DUREESEJOUR#' || v_source.oid::text AS code,
      COALESCE(t_classes.texte || '. ' || t_classes_sections.texte,'Durée séjour ' || v_source.texte::text) AS texte,
      v_source.texte AS texte_court,
      'duree_sejour_id'::text AS column_name,
      ''::text AS rule,
      v_source.oid::text AS value,
      'DUREESEJOUR='||v_source.oid AS view_select_rule,
      ARRAY[
        CASE 
          WHEN v_source.oid LIKE 'LS%' THEN '1-Classe' || substr(v_source.oid,3,2)
          WHEN v_source.is_cti = '1' AND (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') THEN '2-Liste CTI' 
          WHEN v_source.is_cti <> '1' AND (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') THEN '3-Liste CTI' 
          ELSE '9' END,
        CASE 
          WHEN v_source.oid LIKE 'LS%' THEN 'LS#'||t_listes_tables_classes.code||':'||CASE WHEN t_classes.code <> '' THEN t_classes.code ELSE '*CLS'||to_char(t_classes.sequence,'FM00') END||':'||t_classes_sections.code
          WHEN v_source.oid LIKE 'LI%' THEN 'LI#'||t_listes_tables_listes.code||':'||CASE WHEN t_listes.code <> '' THEN t_listes.code ELSE '*TXT:'||upper(t_listes.texte) END
          WHEN v_source.oid LIKE 'SL%' THEN 'SL#'||t_listes_tables_listes.code||':'||CASE WHEN t_listes.code <> '' THEN t_listes.code ELSE '*TXT:'||upper(t_listes.texte) END
          WHEN v_source.oid LIKE 'DT%' THEN v_source.oid
          ELSE '###'||v_source.oid END
          
      ] AS arbre_code,
      ARRAY[
        CASE 
          WHEN v_source.oid LIKE 'LS%' THEN t_classes.texte
          WHEN v_source.is_cti = '1' AND (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') THEN 'Listes CTI' 
          WHEN v_source.is_cti <> '1' AND (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') THEN 'Listes Etablissement' 
          ELSE 'Valeurs' END,
        CASE 
          WHEN v_source.oid LIKE 'LS%' THEN t_classes_sections.texte
          ELSE v_source.texte END
      ] AS arbre_texte
    FROM activite.v_durees_sejour_2 v_source
      LEFT JOIN activite.t_classes_sections ON v_source.oid LIKE 'LS%' AND
      t_classes_sections.oid::text = substr(v_source.oid,5)
      LEFT JOIN activite.t_classes ON t_classes.oid = t_classes_sections.classe_id
      LEFT JOIN activite.t_listes_tables t_listes_tables_classes ON t_classes.table_id = t_listes_tables_classes.oid
      LEFT JOIN activite.t_listes ON (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') AND
        t_listes.oid::text = substr(v_source.oid,3) AND
        t_listes.code <> ''
      LEFT JOIN activite.t_listes_tables t_listes_tables_listes ON t_listes.table_id = t_listes_tables_listes.oid
    WHERE v_source.oid NOT LIKE 'LC%' AND
      v_source.oid NOT LIKE 'DU%' AND
      (v_source.oid NOT LIKE 'LS%' OR substr(v_source.oid,5) <> '0') AND
      v_source.oid <> '-1'
    ;
  
    -- Etages
    INSERT INTO w_indicateur_condition
    SELECT 
      'CTI_ETAGEMOUVEMENT#' || v_source.oid::text AS code,
      COALESCE(t_classes.texte || '. ' || t_classes_sections.texte,'Etage. ' || v_source.texte::text) AS texte,
      v_source.texte AS texte_court,
      'lieu_etage_id'::text AS column_name,
      ''::text AS rule,
      v_source.oid::text AS value,
      'ETAGE_MOUVEMENT='||v_source.oid AS view_select_rule,
      ARRAY[
        CASE 
          WHEN v_source.oid LIKE 'LS%' THEN '1-Classe' || substr(v_source.oid,3,2)
          WHEN v_source.is_cti = '1' AND (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') THEN '2-Liste CTI' 
          WHEN v_source.is_cti <> '1' AND (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') THEN '3-Liste CTI' 
          ELSE '9' END,
        CASE 
          WHEN v_source.oid LIKE 'LS%' THEN 'LS#'||t_listes_tables_classes.code||':'||CASE WHEN t_classes.code <> '' THEN t_classes.code ELSE '*CLS'||to_char(t_classes.sequence,'FM00') END||':'||t_classes_sections.code
          WHEN v_source.oid LIKE 'LI%' THEN 'LI#'||t_listes_tables_listes.code||':'||CASE WHEN t_listes.code <> '' THEN t_listes.code ELSE '*TXT:'||upper(t_listes.texte) END
          WHEN v_source.oid LIKE 'SL%' THEN 'SL#'||t_listes_tables_listes.code||':'||CASE WHEN t_listes.code <> '' THEN t_listes.code ELSE '*TXT:'||upper(t_listes.texte) END
          WHEN v_source.oid LIKE 'ET%' THEN COALESCE('ET#'||t_source.code,v_source.oid)
          ELSE '###'||v_source.oid END
          
      ] AS arbre_code,
      ARRAY[
        CASE 
          WHEN v_source.oid LIKE 'LS%' THEN t_classes.texte
          WHEN v_source.is_cti = '1' AND (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') THEN 'Listes CTI' 
          WHEN v_source.is_cti <> '1' AND (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') THEN 'Listes Etablissement' 
          ELSE 'Valeurs' END,
        v_source.texte
      ] AS arbre_texte
    FROM activite.v_etages_2 v_source
      LEFT JOIN activite.t_classes_sections ON v_source.oid LIKE 'LS%' AND
      t_classes_sections.oid::text = substr(v_source.oid,5)
      LEFT JOIN activite.t_classes ON t_classes.oid = t_classes_sections.classe_id
      LEFT JOIN activite.t_listes_tables t_listes_tables_classes ON t_classes.table_id = t_listes_tables_classes.oid
      LEFT JOIN activite.t_listes ON (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') AND
        t_listes.oid::text = substr(v_source.oid,3) AND
        t_listes.code <> ''
      LEFT JOIN activite.t_listes_tables t_listes_tables_listes ON t_listes.table_id = t_listes_tables_listes.oid
      LEFT JOIN activite.t_etages t_source ON v_source.oid LIKE 'ET%' AND t_source.oid = substr(v_source.oid,3)
    WHERE v_source.oid NOT LIKE 'LC%' AND
      (v_source.oid NOT LIKE 'LS%' OR substr(v_source.oid,5) <> '0') AND
      v_source.oid <> '-1' AND
      v_source.oid <> 'ET0'
    ;
  
    -- Services
    INSERT INTO w_indicateur_condition
    SELECT 
      'CTI_SERVICEMOUVEMENT#' || v_source.oid::text AS code,
      COALESCE(t_classes.texte || '. ' || t_classes_sections.texte,'Service ' || v_source.texte::text) AS texte,
      v_source.texte AS texte_court,
      'lieu_service_id'::text AS column_name,
      ''::text AS rule,
      v_source.oid::text AS value,
      'SERVICE_MOUVEMENT='||v_source.oid AS view_select_rule,
      ARRAY[
        CASE 
          WHEN v_source.oid LIKE 'LS%' THEN '1-Classe' || substr(v_source.oid,3,2)
          WHEN v_source.is_cti = '1' AND (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') THEN '2-Liste CTI' 
          WHEN v_source.is_cti <> '1' AND (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') THEN '3-Liste CTI' 
          ELSE '9' END,
        CASE 
          WHEN v_source.oid LIKE 'LS%' THEN 'LS#'||t_listes_tables_classes.code||':'||CASE WHEN t_classes.code <> '' THEN t_classes.code ELSE '*CLS'||to_char(t_classes.sequence,'FM00') END||':'||t_classes_sections.code
          WHEN v_source.oid LIKE 'LI%' THEN 'LI#'||t_listes_tables_listes.code||':'||CASE WHEN t_listes.code <> '' THEN t_listes.code ELSE '*TXT:'||upper(t_listes.texte) END
          WHEN v_source.oid LIKE 'SL%' THEN 'SL#'||t_listes_tables_listes.code||':'||CASE WHEN t_listes.code <> '' THEN t_listes.code ELSE '*TXT:'||upper(t_listes.texte) END
          WHEN v_source.oid LIKE 'SF%' THEN COALESCE('SF#'||t_source.code,v_source.texte)
          ELSE '###'||v_source.oid END
          
      ] AS arbre_code,
      ARRAY[
        CASE 
          WHEN v_source.oid LIKE 'LS%' THEN t_classes.texte
          WHEN v_source.is_cti = '1' AND (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') THEN 'Listes CTI' 
          WHEN v_source.is_cti <> '1' AND (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') THEN 'Listes Etablissement' 
          ELSE 'Valeurs' END,
        v_source.texte
      ] AS arbre_texte
    FROM activite.v_services_facturation_2 v_source
      LEFT JOIN activite.t_classes_sections ON v_source.oid LIKE 'LS%' AND
      t_classes_sections.oid::text = substr(v_source.oid,5)
      LEFT JOIN activite.t_classes ON t_classes.oid = t_classes_sections.classe_id
      LEFT JOIN activite.t_listes_tables t_listes_tables_classes ON t_classes.table_id = t_listes_tables_classes.oid
      LEFT JOIN activite.t_listes ON (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') AND
        t_listes.oid::text = substr(v_source.oid,3) AND
        t_listes.code <> ''
      LEFT JOIN activite.t_listes_tables t_listes_tables_listes ON t_listes.table_id = t_listes_tables_listes.oid
      LEFT JOIN activite.t_services_facturation t_source ON v_source.oid LIKE 'SF%' AND t_source.oid = substr(v_source.oid,3)
    WHERE v_source.oid NOT LIKE 'LC%' AND
      (v_source.oid NOT LIKE 'LS%' OR substr(v_source.oid,5) <> '0') AND
      v_source.oid <> '-1' AND
      v_source.oid <> 'SF0'
    ;
  
    -- Activités
    INSERT INTO w_indicateur_condition
    SELECT 
      'CTI_ACTIVITEMOUVEMENT#' || v_source.oid::text AS code,
      COALESCE(t_classes.texte || '. ' || t_classes_sections.texte,'Activite ' || v_source.texte::text) AS texte,
      v_source.texte AS texte_court,
      'lieu_activite_id'::text AS column_name,
      ''::text AS rule,
      v_source.oid::text AS value,
      'ACTIVITE_MOUVEMENT='||v_source.oid AS view_select_rule,
      ARRAY[
        CASE 
          WHEN v_source.oid LIKE 'LS%' THEN '1-Classe' || substr(v_source.oid,3,2)
          WHEN v_source.is_cti = '1' AND (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') THEN '2-Liste CTI' 
          WHEN v_source.is_cti <> '1' AND (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') THEN '3-Liste CTI' 
          ELSE '9' END,
        CASE 
          WHEN v_source.oid LIKE 'LS%' THEN 'LS#'||t_listes_tables_classes.code||':'||CASE WHEN t_classes.code <> '' THEN t_classes.code ELSE '*CLS'||to_char(t_classes.sequence,'FM00') END||':'||t_classes_sections.code
          WHEN v_source.oid LIKE 'LI%' THEN 'LI#'||t_listes_tables_listes.code||':'||CASE WHEN t_listes.code <> '' THEN t_listes.code ELSE '*TXT:'||upper(t_listes.texte) END
          WHEN v_source.oid LIKE 'SL%' THEN 'SL#'||t_listes_tables_listes.code||':'||CASE WHEN t_listes.code <> '' THEN t_listes.code ELSE '*TXT:'||upper(t_listes.texte) END
          WHEN v_source.oid LIKE 'SF%' THEN COALESCE('SF#'||t_source.code,v_source.texte)
          ELSE '###'||v_source.oid END
          
      ] AS arbre_code,
      ARRAY[
        CASE 
          WHEN v_source.oid LIKE 'LS%' THEN t_classes.texte
          WHEN v_source.is_cti = '1' AND (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') THEN 'Listes CTI' 
          WHEN v_source.is_cti <> '1' AND (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') THEN 'Listes Etablissement' 
          ELSE 'Valeurs' END,
        v_source.texte
      ] AS arbre_texte
    FROM activite.v_activites_2 v_source
      LEFT JOIN activite.t_classes_sections ON v_source.oid LIKE 'LS%' AND
      t_classes_sections.oid::text = substr(v_source.oid,5)
      LEFT JOIN activite.t_classes ON t_classes.oid = t_classes_sections.classe_id
      LEFT JOIN activite.t_listes_tables t_listes_tables_classes ON t_classes.table_id = t_listes_tables_classes.oid
      LEFT JOIN activite.t_listes ON (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') AND
        t_listes.oid::text = substr(v_source.oid,3) AND
        t_listes.code <> ''
      LEFT JOIN activite.t_listes_tables t_listes_tables_listes ON t_listes.table_id = t_listes_tables_listes.oid
      LEFT JOIN activite.t_activites t_source ON v_source.oid LIKE 'AC%' AND t_source.oid = substr(v_source.oid,3)
    WHERE v_source.oid NOT LIKE 'LC%' AND
      (v_source.oid NOT LIKE 'LS%' OR substr(v_source.oid,5) <> '0') AND
      v_source.oid <> '-1' AND
      v_source.oid <> 'AC0'
    ;
  
    -- Unités fonctionnelles
    INSERT INTO w_indicateur_condition
    SELECT 
      'CTI_UFMOUVEMENT#' || v_source.oid::text AS code,
      COALESCE(t_classes.texte || '. ' || t_classes_sections.texte,'UF ' || v_source.texte::text) AS texte,
      v_source.texte AS texte_court,
      'lieu_unite_fonctionnelle_id'::text AS column_name,
      ''::text AS rule,
      v_source.oid::text AS value,
      'UF_MOUVEMENT='||v_source.oid AS view_select_rule,
      ARRAY[
        CASE 
          WHEN v_source.oid LIKE 'LS%' THEN '1-Classe' || substr(v_source.oid,3,2)
          WHEN v_source.is_cti = '1' AND (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') THEN '2-Liste CTI' 
          WHEN v_source.is_cti <> '1' AND (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') THEN '3-Liste CTI' 
          ELSE '9' END,
        CASE 
          WHEN v_source.oid LIKE 'LS%' THEN 'LS#'||t_listes_tables_classes.code||':'||CASE WHEN t_classes.code <> '' THEN t_classes.code ELSE '*CLS'||to_char(t_classes.sequence,'FM00') END||':'||t_classes_sections.code
          WHEN v_source.oid LIKE 'LI%' THEN 'LI#'||t_listes_tables_listes.code||':'||CASE WHEN t_listes.code <> '' THEN t_listes.code ELSE '*TXT:'||upper(t_listes.texte) END
          WHEN v_source.oid LIKE 'SL%' THEN 'SL#'||t_listes_tables_listes.code||':'||CASE WHEN t_listes.code <> '' THEN t_listes.code ELSE '*TXT:'||upper(t_listes.texte) END
          WHEN v_source.oid LIKE 'UF%' THEN COALESCE('UF#'||t_source.code,v_source.texte)
          ELSE '###'||v_source.oid END
          
      ] AS arbre_code,
      ARRAY[
        CASE 
          WHEN v_source.oid LIKE 'LS%' THEN t_classes.texte
          WHEN v_source.is_cti = '1' AND (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') THEN 'Listes CTI' 
          WHEN v_source.is_cti <> '1' AND (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') THEN 'Listes Etablissement' 
          ELSE 'Valeurs' END,
        v_source.texte
      ] AS arbre_texte
    FROM activite.v_unites_fonctionnelles_2 v_source
      LEFT JOIN activite.t_classes_sections ON v_source.oid LIKE 'LS%' AND
      t_classes_sections.oid::text = substr(v_source.oid,5)
      LEFT JOIN activite.t_classes ON t_classes.oid = t_classes_sections.classe_id
      LEFT JOIN activite.t_listes_tables t_listes_tables_classes ON t_classes.table_id = t_listes_tables_classes.oid
      LEFT JOIN activite.t_listes ON (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') AND
        t_listes.oid::text = substr(v_source.oid,3) AND
        t_listes.code <> ''
      LEFT JOIN activite.t_listes_tables t_listes_tables_listes ON t_listes.table_id = t_listes_tables_listes.oid
      LEFT JOIN activite.t_unites_fonctionnelles t_source ON v_source.oid LIKE 'UF%' AND t_source.oid = substr(v_source.oid,3)
    WHERE v_source.oid NOT LIKE 'LC%' AND
      (v_source.oid NOT LIKE 'LS%' OR substr(v_source.oid,5) <> '0') AND
      v_source.oid <> '-1' AND
      v_source.oid <> 'UF0'
    ;
  
    -- Médecins séjours
    INSERT INTO w_indicateur_condition
    SELECT 
      'CTI_MEDECINSEJOUR#' || v_source.oid::text AS code,
      COALESCE(t_classes.texte || '. ' || t_classes_sections.texte,
        CASE WHEN v_source.oid LIKE 'SP%' THEN 'Spécialité ' ELSE 'Médecin ' END || v_source.texte::text) AS texte,
      v_source.texte AS texte_court,
      CASE WHEN v_source.oid LIKE 'SP%' THEN 'medecin_sejour_specialite_id' ELSE 'medecin_adm_sejour_id' END AS column_name,
      ''::text AS rule,
      v_source.oid::text AS value,
      'MEDECIN_SEJOUR='||v_source.oid||'&MEDECIN_MOUVEMENT='||v_source.oid AS view_select_rule,
      ARRAY[
        CASE 
          WHEN v_source.oid LIKE 'LS%' THEN '1-Classe' || substr(v_source.oid,3,2)
          WHEN v_source.is_cti = '1' AND (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') THEN '2-Liste CTI' 
          WHEN v_source.is_cti <> '1' AND (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') THEN '3-Liste CTI' 
          ELSE '9' END,
        CASE 
          WHEN v_source.oid LIKE 'LS%' THEN 'LS#'||t_listes_tables_classes.code||':'||CASE WHEN t_classes.code <> '' THEN t_classes.code ELSE '*CLS'||to_char(t_classes.sequence,'FM00') END||':'||t_classes_sections.code
          WHEN v_source.oid LIKE 'LI%' THEN 'LI#'||t_listes_tables_listes.code||':'||CASE WHEN t_listes.code <> '' THEN t_listes.code ELSE '*TXT:'||upper(t_listes.texte) END
          WHEN v_source.oid LIKE 'SL%' THEN 'SL#'||t_listes_tables_listes.code||':'||CASE WHEN t_listes.code <> '' THEN t_listes.code ELSE '*TXT:'||upper(t_listes.texte) END
          WHEN v_source.oid LIKE 'SP%' THEN COALESCE('SP#'||t_source.code,v_source.texte)
          ELSE '###'||v_source.oid END
          
      ] AS arbre_code,
      ARRAY[
        CASE 
          WHEN v_source.oid LIKE 'LS%' THEN t_classes.texte
          WHEN v_source.is_cti = '1' AND (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') THEN 'Listes CTI' 
          WHEN v_source.is_cti <> '1' AND (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') THEN 'Listes Etablissement' 
          ELSE 'Valeurs' END,
        v_source.texte
          
      ] AS arbre_texte
    FROM activite.v_medecins_5 v_source
      LEFT JOIN activite.t_classes_sections ON v_source.oid LIKE 'LS%' AND
      t_classes_sections.oid::text = substr(v_source.oid,5)
      LEFT JOIN activite.t_classes ON t_classes.oid = t_classes_sections.classe_id
      LEFT JOIN activite.t_listes_tables t_listes_tables_classes ON t_classes.table_id = t_listes_tables_classes.oid
      LEFT JOIN activite.t_listes ON (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') AND
        t_listes.oid::text = substr(v_source.oid,3) AND
        t_listes.code <> ''
      LEFT JOIN activite.t_listes_tables t_listes_tables_listes ON t_listes.table_id = t_listes_tables_listes.oid
      LEFT JOIN base.t_specialites_medecin t_source ON v_source.oid LIKE 'SP%' AND t_source.oid = substr(v_source.oid,3)
    WHERE v_source.oid NOT LIKE 'LC%' AND
      (v_source.oid NOT LIKE 'LS%' OR substr(v_source.oid,5) <> '0') AND
      v_source.oid <> '-1' AND
      v_source.oid NOT LIKE 'MD%' AND
      v_source.oid NOT LIKE 'MA%'
    ;
  
  
    -- Médecins séjours
    INSERT INTO w_indicateur_condition
    SELECT 
      'CTI_MEDECINFACTURE#' || v_source.oid::text AS code,
      COALESCE(t_classes.texte || '. ' || t_classes_sections.texte,
        CASE WHEN v_source.oid LIKE 'SP%' THEN 'Spécialité ' ELSE 'Médecin facturé ' END || v_source.texte::text) AS texte,
      v_source.texte AS texte_court,
      CASE WHEN v_source.oid LIKE 'SP%' THEN 'medecin_facture_specialite_id' ELSE 'medecin_adm_facture_id' END AS column_name,
      ''::text AS rule,
      v_source.oid::text AS value,
      'MEDECINEXECUTANT='||v_source.oid AS view_select_rule,
      ARRAY[
        CASE 
          WHEN v_source.oid LIKE 'LS%' THEN '1-Classe' || substr(v_source.oid,3,2)
          WHEN v_source.is_cti = '1' AND (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') THEN '2-Liste CTI' 
          WHEN v_source.is_cti <> '1' AND (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') THEN '3-Liste CTI' 
          ELSE '9' END,
        CASE 
          WHEN v_source.oid LIKE 'LS%' THEN 'LS#'||t_listes_tables_classes.code||':'||CASE WHEN t_classes.code <> '' THEN t_classes.code ELSE '*CLS'||to_char(t_classes.sequence,'FM00') END||':'||t_classes_sections.code
          WHEN v_source.oid LIKE 'LI%' THEN 'LI#'||t_listes_tables_listes.code||':'||CASE WHEN t_listes.code <> '' THEN t_listes.code ELSE '*TXT:'||upper(t_listes.texte) END
          WHEN v_source.oid LIKE 'SL%' THEN 'SL#'||t_listes_tables_listes.code||':'||CASE WHEN t_listes.code <> '' THEN t_listes.code ELSE '*TXT:'||upper(t_listes.texte) END
          WHEN v_source.oid LIKE 'SP%' THEN COALESCE('SP#'||t_source.code,v_source.texte)
          ELSE '###'||v_source.oid END
          
      ] AS arbre_code,
      ARRAY[
        CASE 
          WHEN v_source.oid LIKE 'LS%' THEN t_classes.texte
          WHEN v_source.is_cti = '1' AND (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') THEN 'Listes CTI' 
          WHEN v_source.is_cti <> '1' AND (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') THEN 'Listes Etablissement' 
          ELSE 'Valeurs' END,
        v_source.texte
          
      ] AS arbre_texte
    FROM activite.v_medecins_6 v_source
      LEFT JOIN activite.t_classes_sections ON v_source.oid LIKE 'LS%' AND
      t_classes_sections.oid::text = substr(v_source.oid,5)
      LEFT JOIN activite.t_classes ON t_classes.oid = t_classes_sections.classe_id
      LEFT JOIN activite.t_listes_tables t_listes_tables_classes ON t_classes.table_id = t_listes_tables_classes.oid
      LEFT JOIN activite.t_listes ON (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') AND
        t_listes.oid::text = substr(v_source.oid,3) AND
        t_listes.code <> ''
      LEFT JOIN activite.t_listes_tables t_listes_tables_listes ON t_listes.table_id = t_listes_tables_listes.oid
      LEFT JOIN base.t_specialites_medecin t_source ON v_source.oid LIKE 'SP%' AND t_source.oid = substr(v_source.oid,3)
    WHERE v_source.oid NOT LIKE 'LC%' AND
      (v_source.oid NOT LIKE 'LS%' OR substr(v_source.oid,5) <> '0') AND
      v_source.oid <> '-1' AND
      v_source.oid NOT LIKE 'MD%' AND
      v_source.oid NOT LIKE 'MA%'
    ;
  
  
  
    -- Modes entrees
    INSERT INTO w_indicateur_condition
    SELECT 
      'CTI_MODEENTREE#' || v_source.code::text AS code,
      'Mode entree ' || v_source.texte::text AS texte,
      v_source.texte AS texte_court,
      'mode_entree'::text AS column_name,
      ''::text AS rule,
      v_source.code::text AS value,
      'MODE_ENTREE='||v_source.code AS view_select_rule,
      ARRAY[
        '9'::text,
        'ME#'||t_source.code
          
      ] AS arbre_code,
      ARRAY[
        'Valeurs'::text,
        v_source.texte
      ] AS arbre_texte
    FROM activite.v_modes_entree_2 v_source
      JOIN base.t_modes_entree t_source ON t_source.code = substr(v_source.code,3)
    WHERE 
      v_source.code <> 'ME' 
    ;
  
    -- Modes sortie
    INSERT INTO w_indicateur_condition
    SELECT 
      'CTI_MODESORTIE#' || v_source.code::text AS code,
      'Mode sortie ' || v_source.texte::text AS texte,
      v_source.texte AS texte_court,
      'mode_sortie'::text AS column_name,
      ''::text AS rule,
      v_source.code::text AS value,
      'MODE_SORTIE='||v_source.code AS view_select_rule,
      ARRAY[
        '9'::text,
        'MS#'||t_source.code
          
      ] AS arbre_code,
      ARRAY[
        'Valeurs'::text,
        v_source.texte
      ] AS arbre_texte
    FROM activite.v_modes_sortie_2 v_source
      JOIN base.t_modes_sortie t_source ON t_source.code = substr(v_source.code,3)
    WHERE 
      v_source.code <> 'MS' 
    ;
  
  
  
  
    -- codes postaux
    INSERT INTO w_indicateur_condition
    SELECT 
      'CTI_CODEPOSTAL#' || v_source.oid::text AS code,
      COALESCE(t_classes.texte || '. ' || t_classes_sections.texte,
        CASE WHEN v_source.oid LIKE 'DP%' THEN 'Département ' ELSE 'Code postal ' END || v_source.texte::text) AS texte,
      v_source.texte AS texte_court,
      CASE WHEN v_source.oid LIKE 'DP%' THEN 'departement_id' ELSE 'code_postal_id' END AS column_name,
      ''::text AS rule,
      v_source.oid::text AS value,
      'ORIGINEGEO_PATIENT='||v_source.oid AS view_select_rule,
      ARRAY[
        CASE 
          WHEN v_source.oid LIKE 'LS%' THEN '1-Classe' || substr(v_source.oid,3,2)
          WHEN v_source.is_cti = '1' AND (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') THEN '2-Liste CTI' 
          WHEN v_source.is_cti <> '1' AND (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') THEN '3-Liste CTI' 
          ELSE '9' END,
        CASE 
          WHEN v_source.oid LIKE 'LS%' THEN 'LS#'||t_listes_tables_classes.code||':'||CASE WHEN t_classes.code <> '' THEN t_classes.code ELSE '*CLS'||to_char(t_classes.sequence,'FM00') END||':'||t_classes_sections.code
          WHEN v_source.oid LIKE 'LI%' THEN 'LI#'||t_listes_tables_listes.code||':'||CASE WHEN t_listes.code <> '' THEN t_listes.code ELSE '*TXT:'||upper(t_listes.texte) END
          WHEN v_source.oid LIKE 'SL%' THEN 'SL#'||t_listes_tables_listes.code||':'||CASE WHEN t_listes.code <> '' THEN t_listes.code ELSE '*TXT:'||upper(t_listes.texte) END
          WHEN v_source.oid LIKE 'DP%' THEN COALESCE('DP#'||t_source.code,v_source.texte)
          ELSE '###'||v_source.oid END
          
      ] AS arbre_code,
      ARRAY[
        CASE 
          WHEN v_source.oid LIKE 'LS%' THEN t_classes.texte
          WHEN v_source.is_cti = '1' AND (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') THEN 'Listes CTI' 
          WHEN v_source.is_cti <> '1' AND (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') THEN 'Listes Etablissement' 
          ELSE 'Valeurs' END,
        v_source.texte
          
      ] AS arbre_texte
    FROM activite.v_codes_postaux_3 v_source
      LEFT JOIN activite.t_classes_sections ON v_source.oid LIKE 'LS%' AND
      t_classes_sections.oid::text = substr(v_source.oid,5)
      LEFT JOIN activite.t_classes ON t_classes.oid = t_classes_sections.classe_id
      LEFT JOIN activite.t_listes_tables t_listes_tables_classes ON t_classes.table_id = t_listes_tables_classes.oid
      LEFT JOIN activite.t_listes ON (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') AND
        t_listes.oid::text = substr(v_source.oid,3) AND
        t_listes.code <> ''
      LEFT JOIN activite.t_listes_tables t_listes_tables_listes ON t_listes.table_id = t_listes_tables_listes.oid
      LEFT JOIN base.t_departements t_source ON v_source.oid LIKE 'SP%' AND t_source.oid = substr(v_source.oid,3)
    WHERE v_source.oid NOT LIKE 'LC%' AND
      (v_source.oid NOT LIKE 'LS%' OR substr(v_source.oid,5) <> '0') AND
      v_source.oid <> '-1' AND
      v_source.oid <> 'DP0' AND
      v_source.oid NOT LIKE 'CP%' AND
      v_source.oid NOT LIKE 'DG%' AND
      v_source.oid NOT LIKE 'GP%' AND
      v_source.oid NOT LIKE 'TS%' AND
      v_source.code <> '-1' AND
      (trim(v_source.code) <> '' OR v_source.oid LIKE 'LS%' OR v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%')
    ;
  
  
    -- codes postaux
    INSERT INTO w_indicateur_condition
    SELECT 
      'CTI_GHM#' || v_source.oid::text AS code,
      COALESCE(t_classes.texte || '. ' || t_classes_sections.texte,
        CASE WHEN v_source.oid LIKE 'GM%' THEN 'GHM ' ELSE 'GHM ' END || v_source.texte::text) AS texte,
      v_source.texte AS texte_court,
      CASE WHEN v_source.oid LIKE 'GM%' THEN 'ghm_id' ELSE 'ghm_id' END AS column_name,
      ''::text AS rule,
      v_source.oid::text AS value,
      'GHM='||v_source.oid AS view_select_rule,
      ARRAY[
        CASE 
          WHEN v_source.oid LIKE 'LS%' THEN '1-Classe' || substr(v_source.oid,3,2)
          WHEN v_source.is_cti = '1' AND (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') THEN '2-Liste CTI' 
          WHEN v_source.is_cti <> '1' AND (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') THEN '3-Liste CTI' 
          ELSE '9' END,
        CASE 
          WHEN v_source.oid LIKE 'LS%' THEN 'LS#'||t_listes_tables_classes.code||':'||CASE WHEN t_classes.code <> '' THEN t_classes.code ELSE '*CLS'||to_char(t_classes.sequence,'FM00') END||':'||t_classes_sections.code
          WHEN v_source.oid LIKE 'LI%' THEN 'LI#'||t_listes_tables_listes.code||':'||CASE WHEN t_listes.code <> '' THEN t_listes.code ELSE '*TXT:'||upper(t_listes.texte) END
          WHEN v_source.oid LIKE 'SL%' THEN 'SL#'||t_listes_tables_listes.code||':'||CASE WHEN t_listes.code <> '' THEN t_listes.code ELSE '*TXT:'||upper(t_listes.texte) END
          WHEN v_source.oid LIKE 'DP%' THEN COALESCE('DP#'||t_source.code,v_source.texte)
          ELSE '###'||v_source.oid END
          
      ] AS arbre_code,
      ARRAY[
        CASE 
          WHEN v_source.oid LIKE 'LS%' THEN t_classes.texte
          WHEN v_source.is_cti = '1' AND (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') THEN 'Listes CTI' 
          WHEN v_source.is_cti <> '1' AND (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') THEN 'Listes Etablissement' 
          ELSE 'Valeurs' END,
        v_source.texte
          
      ] AS arbre_texte
    FROM activite.v_ghm_c_1 v_source
      LEFT JOIN activite.t_classes_sections ON v_source.oid LIKE 'LS%' AND
      t_classes_sections.oid::text = substr(v_source.oid,5)
      LEFT JOIN activite.t_classes ON t_classes.oid = t_classes_sections.classe_id
      LEFT JOIN activite.t_listes_tables t_listes_tables_classes ON t_classes.table_id = t_listes_tables_classes.oid
      LEFT JOIN activite.t_listes ON (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') AND
        t_listes.oid::text = substr(v_source.oid,3) AND
        t_listes.code <> ''
      LEFT JOIN activite.t_listes_tables t_listes_tables_listes ON t_listes.table_id = t_listes_tables_listes.oid
      LEFT JOIN base.t_ghm t_source ON v_source.oid LIKE 'GM%' AND t_source.oid = substr(v_source.oid,3)
    WHERE v_source.oid NOT LIKE 'LC%' AND
      (v_source.oid NOT LIKE 'LS%' OR substr(v_source.oid,5) <> '0') AND
      v_source.oid <> '-1' AND
      (v_source.oid LIKE 'LS%' OR v_source.oid LIKE 'LI%') 
    ;
  
    -- Clés budgétaires
    INSERT INTO w_indicateur_condition
    SELECT 
      'CTI_BUDGETCLE#' || v_source.oid::text AS code,
      COALESCE(t_classes.texte || '. ' || t_classes_sections.texte,
        'Clé budgétaire ' || v_source.texte::text) AS texte,
      v_source.texte AS texte_court,
      'budget_cle_id' AS column_name,
      ''::text AS rule,
      v_source.oid::text AS value,
      'BUDGET_CLE='||v_source.oid AS view_select_rule,
      ARRAY[
        CASE 
          WHEN v_source.oid LIKE 'LS%' THEN '1-Classe' || substr(v_source.oid,3,2)
          WHEN v_source.is_cti = '1' AND (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') THEN '2-Liste CTI' 
          WHEN v_source.is_cti <> '1' AND (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') THEN '3-Liste CTI' 
          ELSE '9' END,
        CASE 
          WHEN v_source.oid LIKE 'LS%' THEN 'LS#'||t_listes_tables_classes.code||':'||CASE WHEN t_classes.code <> '' THEN t_classes.code ELSE '*CLS'||to_char(t_classes.sequence,'FM00') END||':'||t_classes_sections.code
          WHEN v_source.oid LIKE 'LI%' THEN 'LI#'||t_listes_tables_listes.code||':'||CASE WHEN t_listes.code <> '' THEN t_listes.code ELSE '*TXT:'||upper(t_listes.texte) END
          WHEN v_source.oid LIKE 'SL%' THEN 'SL#'||t_listes_tables_listes.code||':'||CASE WHEN t_listes.code <> '' THEN t_listes.code ELSE '*TXT:'||upper(t_listes.texte) END
          WHEN v_source.oid LIKE 'DP%' THEN COALESCE('DP#'||t_source.code,v_source.texte)
          ELSE '###'||v_source.oid END
          
      ] AS arbre_code,
      ARRAY[
        CASE 
          WHEN v_source.oid LIKE 'LS%' THEN t_classes.texte
          WHEN v_source.is_cti = '1' AND (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') THEN 'Listes CTI' 
          WHEN v_source.is_cti <> '1' AND (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') THEN 'Listes Etablissement' 
          ELSE 'Valeurs' END,
        v_source.texte
          
      ] AS arbre_texte
    FROM activite.v_budget_cle_1 v_source
      LEFT JOIN activite.t_classes_sections ON v_source.oid LIKE 'LS%' AND
      t_classes_sections.oid::text = substr(v_source.oid,5)
      LEFT JOIN activite.t_classes ON t_classes.oid = t_classes_sections.classe_id
      LEFT JOIN activite.t_listes_tables t_listes_tables_classes ON t_classes.table_id = t_listes_tables_classes.oid
      LEFT JOIN activite.t_listes ON (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') AND
        t_listes.oid::text = substr(v_source.oid,3) AND
        t_listes.code <> ''
      LEFT JOIN activite.t_listes_tables t_listes_tables_listes ON t_listes.table_id = t_listes_tables_listes.oid
      LEFT JOIN activite.t_budget_cle t_source ON v_source.oid LIKE 'GM%' AND t_source.oid = substr(v_source.oid,3)
    WHERE v_source.oid NOT LIKE 'LC%' AND
      (v_source.oid NOT LIKE 'LS%' OR substr(v_source.oid,5) <> '0') AND
      v_source.oid <> '-1' AND
      (v_source.oid LIKE 'LS%' OR v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%' OR v_source.oid LIKE 'DT%') 
    ;
  
    -- Rubrique facturée
    INSERT INTO w_indicateur_condition
    SELECT 
      'CTI_RUBRIQUE#' || v_source.oid::text AS code,
      COALESCE(t_classes.texte || '. ' || t_classes_sections.texte,'RUB ' || v_source.texte::text) AS texte,
      v_source.texte AS texte_court,
      'rubrique_facturation_id'::text AS column_name,
      ''::text AS rule,
      v_source.oid::text AS value,
      'RUBRIQUE='||v_source.oid AS view_select_rule,
      ARRAY[
        CASE 
          WHEN v_source.oid LIKE 'LS%' THEN '1-Classe' || substr(v_source.oid,3,2)
          WHEN v_source.is_cti = '1' AND (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') THEN '2-Liste CTI' 
          WHEN v_source.is_cti <> '1' AND (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') THEN '3-Liste CTI' 
          ELSE '9' END,
        CASE 
          WHEN v_source.oid LIKE 'LS%' THEN 'LS#'||t_listes_tables_classes.code||':'||CASE WHEN t_classes.code <> '' THEN t_classes.code ELSE '*CLS'||to_char(t_classes.sequence,'FM00') END||':'||t_classes_sections.code
          WHEN v_source.oid LIKE 'LI%' THEN 'LI#'||t_listes_tables_listes.code||':'||CASE WHEN t_listes.code <> '' THEN t_listes.code ELSE '*TXT:'||upper(t_listes.texte) END
          WHEN v_source.oid LIKE 'SL%' THEN 'SL#'||t_listes_tables_listes.code||':'||CASE WHEN t_listes.code <> '' THEN t_listes.code ELSE '*TXT:'||upper(t_listes.texte) END
          WHEN v_source.oid LIKE 'UF%' THEN COALESCE('UF#'||t_source.code,v_source.texte)
          ELSE '###'||v_source.oid END
          
      ] AS arbre_code,
      ARRAY[
        CASE 
          WHEN v_source.oid LIKE 'LS%' THEN t_classes.texte
          WHEN v_source.is_cti = '1' AND (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') THEN 'Listes CTI' 
          WHEN v_source.is_cti <> '1' AND (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') THEN 'Listes Etablissement' 
          ELSE 'Valeurs' END,
        v_source.texte
      ] AS arbre_texte
    FROM activite.v_rubriques_facturation_2 v_source
      LEFT JOIN activite.t_classes_sections ON v_source.oid LIKE 'LS%' AND
      t_classes_sections.oid::text = substr(v_source.oid,5)
      LEFT JOIN activite.t_classes ON t_classes.oid = t_classes_sections.classe_id
      LEFT JOIN activite.t_listes_tables t_listes_tables_classes ON t_classes.table_id = t_listes_tables_classes.oid
      LEFT JOIN activite.t_listes ON (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') AND
        t_listes.oid::text = substr(v_source.oid,3) AND
        t_listes.code <> ''
      LEFT JOIN activite.t_listes_tables t_listes_tables_listes ON t_listes.table_id = t_listes_tables_listes.oid
      LEFT JOIN activite.t_rubriques_facturation t_source ON v_source.oid LIKE 'RU%' AND t_source.oid = substr(v_source.oid,3)
    WHERE v_source.oid NOT LIKE 'LC%' AND
      (v_source.oid NOT LIKE 'LS%' OR substr(v_source.oid,5) <> '0') AND
      v_source.oid <> '-1' AND
      v_source.oid <> 'RU0'
    ;
  
    -- Prestation
    INSERT INTO w_indicateur_condition
    SELECT 
      'CTI_PRESTA#' || v_source.oid::text AS code,
      COALESCE(t_classes.texte || '. ' || t_classes_sections.texte,'PRESTATION ' || v_source.texte::text) AS texte,
      v_source.texte AS texte_court,
      'prestation_id'::text AS column_name,
      ''::text AS rule,
      v_source.oid::text AS value,
      'PRESTATION='||v_source.oid AS view_select_rule,
      ARRAY[
        CASE 
          WHEN v_source.oid LIKE 'LS%' THEN '1-Classe' || substr(v_source.oid,3,2)
          WHEN v_source.is_cti = '1' AND (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') THEN '2-Liste CTI' 
          WHEN v_source.is_cti <> '1' AND (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') THEN '3-Liste CTI' 
          ELSE '9' END,
        CASE 
          WHEN v_source.oid LIKE 'LS%' THEN 'LS#'||t_listes_tables_classes.code||':'||CASE WHEN t_classes.code <> '' THEN t_classes.code ELSE '*CLS'||to_char(t_classes.sequence,'FM00') END||':'||t_classes_sections.code
          WHEN v_source.oid LIKE 'LI%' THEN 'LI#'||t_listes_tables_listes.code||':'||CASE WHEN t_listes.code <> '' THEN t_listes.code ELSE '*TXT:'||upper(t_listes.texte) END
          WHEN v_source.oid LIKE 'SL%' THEN 'SL#'||t_listes_tables_listes.code||':'||CASE WHEN t_listes.code <> '' THEN t_listes.code ELSE '*TXT:'||upper(t_listes.texte) END
          WHEN v_source.oid LIKE 'UF%' THEN COALESCE('UF#'||t_source.code,v_source.texte)
          ELSE '###'||v_source.oid END
          
      ] AS arbre_code,
      ARRAY[
        CASE 
          WHEN v_source.oid LIKE 'LS%' THEN t_classes.texte
          WHEN v_source.is_cti = '1' AND (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') THEN 'Listes CTI' 
          WHEN v_source.is_cti <> '1' AND (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') THEN 'Listes Etablissement' 
          ELSE 'Valeurs' END,
        v_source.texte
      ] AS arbre_texte
    FROM activite.v_prestations_3 v_source
      LEFT JOIN activite.t_classes_sections ON v_source.oid LIKE 'LS%' AND
      t_classes_sections.oid::text = substr(v_source.oid,5)
      LEFT JOIN activite.t_classes ON t_classes.oid = t_classes_sections.classe_id
      LEFT JOIN activite.t_listes_tables t_listes_tables_classes ON t_classes.table_id = t_listes_tables_classes.oid
      LEFT JOIN activite.t_listes ON (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') AND
        t_listes.oid::text = substr(v_source.oid,3) AND
        t_listes.code <> ''
      LEFT JOIN activite.t_listes_tables t_listes_tables_listes ON t_listes.table_id = t_listes_tables_listes.oid
      LEFT JOIN activite.t_prestations t_source ON v_source.oid LIKE 'PR%' AND t_source.oid = substr(v_source.oid,3)
    WHERE v_source.oid NOT LIKE 'LC%' AND
      (v_source.oid NOT LIKE 'LS%' OR substr(v_source.oid,5) <> '0') AND
      v_source.oid <> '-1' AND
      v_source.oid <> 'PR0'
    UNION
    SELECT 
      'CTI_PRESTA#' || v_source.oid::text AS code,
      COALESCE(t_classes.texte || '. ' || t_classes_sections.texte,'PRESTATION ' || v_source.texte::text) AS texte,
      v_source.texte AS texte_court,
      'prestation_id'::text AS column_name,
      ''::text AS rule,
      v_source.oid::text AS value,
      'PRESTATION='||v_source.oid AS view_select_rule,
      ARRAY[
        CASE 
          WHEN v_source.oid LIKE 'LS%' THEN '1-Classe' || substr(v_source.oid,3,2)
          
          WHEN  (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') THEN '3-Liste ETS' 
          ELSE '9' END,
        CASE 
          WHEN v_source.oid LIKE 'LS%' THEN 'LS#'||t_listes_tables_classes.code||':'||CASE WHEN t_classes.code <> '' THEN t_classes.code ELSE '*CLS'||to_char(t_classes.sequence,'FM00') END||':'||t_classes_sections.code
          WHEN v_source.oid LIKE 'LI%' THEN 'LI#'||t_listes_tables_listes.code||':'||CASE WHEN t_listes.code <> '' THEN t_listes.code ELSE '*TXT:'||upper(t_listes.texte) END
          WHEN v_source.oid LIKE 'SL%' THEN 'SL#'||t_listes_tables_listes.code||':'||CASE WHEN t_listes.code <> '' THEN t_listes.code ELSE '*TXT:'||upper(t_listes.texte) END
          WHEN v_source.oid LIKE 'UF%' THEN COALESCE('UF#'||t_source.code,v_source.texte)
          ELSE '###'||v_source.oid END
          
      ] AS arbre_code,
      ARRAY[
        CASE 
          WHEN v_source.oid LIKE 'LS%' THEN t_classes.texte
          WHEN  (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') THEN 'Listes Etablissement' 
          ELSE 'Valeurs' END,
        v_source.texte
      ] AS arbre_texte
    FROM activite.v_prestations_4 v_source
      LEFT JOIN activite.t_classes_sections ON v_source.oid LIKE 'LS%' AND
      t_classes_sections.oid::text = substr(v_source.oid,5)
      LEFT JOIN activite.t_classes ON t_classes.oid = t_classes_sections.classe_id
      LEFT JOIN activite.t_listes_tables t_listes_tables_classes ON t_classes.table_id = t_listes_tables_classes.oid
      LEFT JOIN activite.t_listes ON (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') AND
        t_listes.oid::text = substr(v_source.oid,3) AND
        t_listes.code <> ''
      LEFT JOIN activite.t_listes_tables t_listes_tables_listes ON t_listes.table_id = t_listes_tables_listes.oid
      LEFT JOIN activite.t_prestations t_source ON v_source.oid LIKE 'PR%' AND t_source.oid = substr(v_source.oid,3)
    WHERE v_source.oid NOT LIKE 'LC%' AND
      (v_source.oid NOT LIKE 'LS%' OR substr(v_source.oid,5) <> '0') AND
      v_source.oid <> '-1' AND
      v_source.oid <> 'PR0'
      ORDER BY 1
    ;
  
  
    -- Rubrique facturée
    INSERT INTO w_indicateur_condition
    SELECT 
      'CTI_ACTE#' || v_source.oid::text AS code,
      COALESCE(t_classes.texte || '. ' || t_classes_sections.texte,'ACTE ' || v_source.texte::text) AS texte,
      v_source.texte AS texte_court,
      'acte_id'::text AS column_name,
      ''::text AS rule,
      v_source.oid::text AS value,
      'ACTE='||v_source.oid AS view_select_rule,
      ARRAY[
        CASE 
          WHEN v_source.oid LIKE 'LS%' THEN '1-Classe' || substr(v_source.oid,3,2)
          WHEN v_source.is_cti = '1' AND (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') THEN '2-Liste CTI' 
          WHEN v_source.is_cti <> '1' AND (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') THEN '3-Liste CTI' 
          ELSE '9' END,
        CASE 
          WHEN v_source.oid LIKE 'LS%' THEN 'LS#'||t_listes_tables_classes.code||':'||CASE WHEN t_classes.code <> '' THEN t_classes.code ELSE '*CLS'||to_char(t_classes.sequence,'FM00') END||':'||t_classes_sections.code
          WHEN v_source.oid LIKE 'LI%' THEN 'LI#'||t_listes_tables_listes.code||':'||CASE WHEN t_listes.code <> '' THEN t_listes.code ELSE '*TXT:'||upper(t_listes.texte) END
          WHEN v_source.oid LIKE 'SL%' THEN 'SL#'||t_listes_tables_listes.code||':'||CASE WHEN t_listes.code <> '' THEN t_listes.code ELSE '*TXT:'||upper(t_listes.texte) END
          WHEN v_source.oid LIKE 'UF%' THEN COALESCE('UF#'||t_source.code,v_source.texte)
          ELSE '###'||v_source.oid END
          
      ] AS arbre_code,
      ARRAY[
        CASE 
          WHEN v_source.oid LIKE 'LS%' THEN t_classes.texte
          WHEN v_source.is_cti = '1' AND (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') THEN 'Listes CTI' 
          WHEN v_source.is_cti <> '1' AND (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') THEN 'Listes Etablissement' 
          ELSE 'Valeurs' END,
        v_source.texte
      ] AS arbre_texte
    FROM activite.v_actes_3 v_source
      LEFT JOIN activite.t_classes_sections ON v_source.oid LIKE 'LS%' AND
      t_classes_sections.oid::text = substr(v_source.oid,5)
      LEFT JOIN activite.t_classes ON t_classes.oid = t_classes_sections.classe_id
      LEFT JOIN activite.t_listes_tables t_listes_tables_classes ON t_classes.table_id = t_listes_tables_classes.oid
      LEFT JOIN activite.t_listes ON (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') AND
        t_listes.oid::text = substr(v_source.oid,3) AND
        t_listes.code <> ''
      LEFT JOIN activite.t_listes_tables t_listes_tables_listes ON t_listes.table_id = t_listes_tables_listes.oid
      LEFT JOIN base.t_actes t_source ON v_source.oid LIKE 'RU%' AND t_source.oid = substr(v_source.oid,3)
    WHERE v_source.oid NOT LIKE 'LC%' AND
      (v_source.oid NOT LIKE 'LS%' OR substr(v_source.oid,5) <> '0') AND
      v_source.oid <> '-1' AND
      v_source.oid NOT LIKE 'AC%' AND
      v_source.oid NOT LIKE 'NO%' AND
      v_source.oid <> 'AC0'
    ;
  
    -- Comptes
    INSERT INTO w_indicateur_condition
    SELECT 
      'CTI_COMPTE#' || v_source.oid::text AS code,
      COALESCE(t_classes.texte || '. ' || t_classes_sections.texte,'CPT ' || v_source.texte::text) AS texte,
      v_source.texte AS texte_court,
      'compte_produit_id'::text AS column_name,
      ''::text AS rule,
      v_source.oid::text AS value,
      'COMPTE='||v_source.oid AS view_select_rule,
      ARRAY[
        CASE 
          WHEN v_source.oid LIKE 'LS%' THEN '1-Classe' || substr(v_source.oid,3,2)
          WHEN v_source.is_cti = '1' AND (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') THEN '2-Liste CTI' 
          WHEN v_source.is_cti <> '1' AND (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') THEN '3-Liste CTI' 
          ELSE '9' END,
        CASE 
          WHEN v_source.oid LIKE 'LS%' THEN 'LS#'||t_listes_tables_classes.code||':'||CASE WHEN t_classes.code <> '' THEN t_classes.code ELSE '*CLS'||to_char(t_classes.sequence,'FM00') END||':'||t_classes_sections.code
          WHEN v_source.oid LIKE 'LI%' THEN 'LI#'||t_listes_tables_listes.code||':'||CASE WHEN t_listes.code <> '' THEN t_listes.code ELSE '*TXT:'||upper(t_listes.texte) END
          WHEN v_source.oid LIKE 'SL%' THEN 'SL#'||t_listes_tables_listes.code||':'||CASE WHEN t_listes.code <> '' THEN t_listes.code ELSE '*TXT:'||upper(t_listes.texte) END
          WHEN v_source.oid LIKE 'UM%' THEN COALESCE('UM#'||t_source.code,v_source.texte)
          ELSE '###'||v_source.oid END
          
      ] AS arbre_code,
      ARRAY[
        CASE 
          WHEN v_source.oid LIKE 'LS%' THEN t_classes.texte
          WHEN v_source.is_cti = '1' AND (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') THEN 'Listes CTI' 
          WHEN v_source.is_cti <> '1' AND (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') THEN 'Listes Etablissement' 
          ELSE 'Valeurs' END,
        v_source.texte
      ] AS arbre_texte
    FROM activite.v_compte_1 v_source
      LEFT JOIN activite.t_classes_sections ON v_source.oid LIKE 'LS%' AND
      t_classes_sections.oid::text = substr(v_source.oid,5)
      LEFT JOIN activite.t_classes ON t_classes.oid = t_classes_sections.classe_id
      LEFT JOIN activite.t_listes_tables t_listes_tables_classes ON t_classes.table_id = t_listes_tables_classes.oid
      LEFT JOIN activite.t_listes ON (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') AND
        t_listes.oid::text = substr(v_source.oid,3) AND
        t_listes.code <> ''
      LEFT JOIN activite.t_listes_tables t_listes_tables_listes ON t_listes.table_id = t_listes_tables_listes.oid
      LEFT JOIN activite.t_unites_medicales t_source ON v_source.oid LIKE 'UM%' AND t_source.oid = substr(v_source.oid,3)
    WHERE v_source.oid NOT LIKE 'LC%' AND
      (v_source.oid NOT LIKE 'LS%' OR substr(v_source.oid,5) <> '0') AND
      v_source.oid <> '-1' AND
      (v_source.oid LIKE 'DT%' OR v_source.oid LIKE 'LS%' OR v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%')AND
      v_source.oid <> 'DT0'
    ;
  
    
    INSERT INTO activite.t_indicateur_condition(code, texte,texte_court,column_name,rule,value,view_select_rule, arbre_code, arbre_texte)
    SELECT code, texte,texte_court,column_name,rule,value,view_select_rule, arbre_code, arbre_texte
    FROM w_indicateur_condition
    WHERE code NOT IN (SELECT code FROM activite.t_indicateur_condition WHERE code IS NOT NULL)
    ;
  
    UPDATE activite.t_indicateur_condition SET
      texte = w_indicateur_condition.texte,
      texte_court = w_indicateur_condition.texte_court,
      column_name = w_indicateur_condition.column_name,
      rule = w_indicateur_condition.rule,
      value = w_indicateur_condition.value,
      view_select_rule = w_indicateur_condition.view_select_rule,
      arbre_code = w_indicateur_condition.arbre_code,
      arbre_texte = w_indicateur_condition.arbre_texte
    FROM w_indicateur_condition
    WHERE w_indicateur_condition.code = t_indicateur_condition.code AND
      (
      t_indicateur_condition.texte IS DISTINCT FROM w_indicateur_condition.texte OR
      t_indicateur_condition.texte_court IS DISTINCT FROM w_indicateur_condition.texte_court OR
      t_indicateur_condition.column_name IS DISTINCT FROM w_indicateur_condition.column_name OR
      t_indicateur_condition.rule IS DISTINCT FROM w_indicateur_condition.rule OR
      t_indicateur_condition.value IS DISTINCT FROM w_indicateur_condition.value OR
      t_indicateur_condition.view_select_rule IS DISTINCT FROM w_indicateur_condition.view_select_rule OR
      t_indicateur_condition.arbre_code IS DISTINCT FROM w_indicateur_condition.arbre_code OR
      t_indicateur_condition.arbre_texte IS DISTINCT FROM w_indicateur_condition.arbre_texte
      )
    ;
  
    DELETE FROM activite.t_indicateur_condition
    WHERE code LIKE 'CTI%' AND
      code NOT IN (SELECT code FROM w_indicateur_condition)
      ;
  
    UPDATE activite.t_indicateur_condition SET
      column_texte = CASE column_name
        WHEN 'lieu_unite_medicale_id' THEN 'Unité médicale'
        WHEN 'finess_id' THEN 'Etablissement'
        WHEN 'forme_activite_id' THEN 'Forme d''activité'
        WHEN 'type_sejour_id' THEN 'Type de séjour'
        WHEN 'lieu_etage_id' THEN 'Etage Mouvement'
        WHEN 'lieu_activite_id' THEN 'Activité Mouvement'
        WHEN 'lieu_service_id' THEN 'Service Mouvement'
        WHEN 'lieu_unite_fonctionnelle_id' THEN 'UF Mouvement'
        WHEN 'medecin_sejour_specialite_id' THEN 'Spécialité Médecin'
        WHEN 'medecin_adm_sejour_id' THEN 'Médecin séjour'
        ELSE replace(Upper(substr(split_part(column_name,'_id',1),1,1)) || substr(split_part(column_name,'_id',1),2),'_',' ') END
    WHERE 
      column_texte IS DISTINCT FROM 
        CASE column_name
        WHEN 'lieu_unite_medicale_id' THEN 'Unité médicale'
        WHEN 'finess_id' THEN 'Etablissement'
        WHEN 'forme_activite_id' THEN 'Forme d''activité'
        WHEN 'type_sejour_id' THEN 'Type de séjour'
        WHEN 'lieu_etage_id' THEN 'Etage Mouvement'
        WHEN 'lieu_activite_id' THEN 'Activité Mouvement'
        WHEN 'lieu_service_id' THEN 'Service Mouvement'
        WHEN 'lieu_unite_fonctionnelle_id' THEN 'UF Mouvement'
        WHEN 'medecin_sejour_specialite_id' THEN 'Spécialité Médecin'
        WHEN 'medecin_adm_sejour_id' THEN 'Médecin séjour'
        ELSE replace(Upper(substr(split_part(column_name,'_id',1),1,1)) || substr(split_part(column_name,'_id',1),2),'_',' ') END
    ;
  
    UPDATE activite.t_indicateur_condition
    SET external_code = split_part(code,'#',1)||'#'||arbre_code[2]
    WHERE external_code IS DISTINCT FROM (split_part(code,'#',1)||'#'||arbre_code[2])
    ;
  
    
    DROP TABLE IF EXISTS w_indicateur_condition_table;
    CREATE TEMP TABLE w_indicateur_condition_table AS
    SELECT t_indicateur_condition.oid AS indicateur_condition_id,
      ''::text As table_name,
      t_indicateur_condition.column_name || ' ' ||
      CASE 
        WHEN t_indicateur_condition.value LIKE 'LS%' THEN '= ANY(ARRAY'
        WHEN t_indicateur_condition.value LIKE 'LI%' THEN '= ANY(ARRAY'
        WHEN t_indicateur_condition.value LIKE 'SL%' THEN '<> ALL(ARRAY'
        WHEN t_indicateur_condition.value LIKE 'DT%' THEN '='
        ELSE '='
      END || ' ' ||
      CASE 
        WHEN t_indicateur_condition.value LIKE 'LS%' THEN 
          '(SELECT to_id FROM ' || CASE WHEN t_indicateur_condition.column_name <> 'finess_id' THEN 'activite' ELSE 'base' END ||'.t_classes_sections_elements WHERE section_id = ' || substr(value,5) || ')'
        WHEN t_indicateur_condition.value LIKE 'LI%' THEN 
          '(SELECT to_id FROM ' || CASE WHEN t_indicateur_condition.column_name <> 'finess_id' THEN 'activite' ELSE 'base' END ||'.t_listes_contenu WHERE liste_id = ' || substr(value,3) || ')'
        WHEN t_indicateur_condition.value LIKE 'SL%' THEN 
          '(SELECT to_id FROM ' || CASE WHEN t_indicateur_condition.column_name <> 'finess_id' THEN 'activite' ELSE 'base' END ||'.t_listes_contenu WHERE liste_id = ' || substr(value,3) || ')'
        WHEN t_indicateur_condition.value LIKE 'DT%' THEN 
          substr(value,3)
        ELSE substr(value,3)
      END || ' ' ||
      CASE 
        WHEN t_indicateur_condition.value LIKE 'LS%' THEN ')'
        WHEN t_indicateur_condition.value LIKE 'LI%' THEN ')'
        WHEN t_indicateur_condition.value LIKE 'SL%' THEN ')'
        ELSE ''
      END AS check_where
    FROM activite.t_indicateur_condition
    WHERE t_indicateur_condition.code LIKE 'CTI%' 
    ;
  
    INSERT INTO w_indicateur_condition_table
    SELECT t_indicateur_condition.oid AS indicateur_condition_id,
      'PLACES'::text As table_name,
      ''::text AS check_where
    FROM activite.t_indicateur_condition
    LEFT JOIN activite.t_divers ON t_divers.code = 'PLACESREF'
    WHERE t_indicateur_condition.code LIKE 'CTI%' AND
      (
      column_name IN ('forme_activite_id', 'medecin_sejour_specialite_id','medecin_adm_sejour_id', 'lieu_unite_medicale_id') OR
      column_name = 'lieu_service_id' AND t_divers.valeur NOT IN ('SER') OR
      column_name = 'lieu_unite_fonctionnelle_id' AND t_divers.valeur NOT IN ('UF') OR
      column_name = 'lieu_etage_id' AND t_divers.valeur IN ('SER','UF') OR
      column_name = 'budget_cle_id'
      )
    ;
    INSERT INTO w_indicateur_condition_table
    SELECT t_indicateur_condition.oid AS indicateur_condition_id,
      'PLACES'::text As table_name,
      CASE t_divers.valeur
      WHEN 'SER' THEN 'service_id'
      WHEN 'UF' THEN 'uf_id'
      ELSE 'etage_id' END  
        || ' '::text ||
      CASE 
        WHEN t_indicateur_condition.value LIKE 'LS%' THEN 'IN'
        WHEN t_indicateur_condition.value LIKE 'LI%' THEN 'IN'
        WHEN t_indicateur_condition.value LIKE 'SL%' THEN 'NOT IN'
        WHEN t_indicateur_condition.value LIKE 'DT%' THEN '='
        ELSE '='
      END || ' ' ||
      CASE 
        WHEN t_indicateur_condition.value LIKE 'LS%' THEN 
          '(SELECT to_id FROM activite.t_classes_sections_elements WHERE section_id = ' || substr(value,5) || ')'
        WHEN t_indicateur_condition.value LIKE 'LI%' THEN 
          '(SELECT to_id FROM activite.t_listes_contenu WHERE liste_id = ' || substr(value,3) || ')'
        WHEN t_indicateur_condition.value LIKE 'SL%' THEN 
          '(SELECT to_id FROM activite.t_listes_contenu WHERE liste_id = ' || substr(value,3) || ')'
        WHEN t_indicateur_condition.value LIKE 'DT%' THEN 
          substr(value,3)
        ELSE substr(value,3)
      END AS check_where
    FROM activite.t_indicateur_condition
    LEFT JOIN activite.t_divers ON t_divers.code = 'PLACESREF'
    WHERE t_indicateur_condition.code LIKE 'CTI%' AND
      (
      column_name = 'lieu_service_id' AND t_divers.valeur IN ('SER') OR
      column_name = 'lieu_unite_fonctionnelle_id' AND t_divers.valeur IN ('UF') OR
      column_name = 'lieu_etage_id' AND t_divers.valeur NOT IN ('SER','UF')
      )
    ;
  
    INSERT INTO w_indicateur_condition_table
    SELECT t_indicateur_condition.oid AS indicateur_condition_id,
      'FACTURATION_CLINIQUE'::text As table_name,
      replace(column_name,'lieu_','lieu_ligne_') || ' ' ||
      CASE 
        WHEN t_indicateur_condition.value LIKE 'LS%' THEN 'IN'
        WHEN t_indicateur_condition.value LIKE 'LI%' THEN 'IN'
        WHEN t_indicateur_condition.value LIKE 'SL%' THEN 'NOT IN'
        WHEN t_indicateur_condition.value LIKE 'DT%' THEN '='
        ELSE '='
      END || ' ' ||
      CASE 
        WHEN t_indicateur_condition.value LIKE 'LS%' THEN 
          '(SELECT to_id FROM activite.t_classes_sections_elements WHERE section_id = ' || substr(value,5) || ')'
        WHEN t_indicateur_condition.value LIKE 'LI%' THEN 
          '(SELECT to_id FROM activite.t_listes_contenu WHERE liste_id = ' || substr(value,3) || ')'
        WHEN t_indicateur_condition.value LIKE 'SL%' THEN 
          '(SELECT to_id FROM activite.t_listes_contenu WHERE liste_id = ' || substr(value,3) || ')'
        WHEN t_indicateur_condition.value LIKE 'DT%' THEN 
          substr(value,3)
        ELSE substr(value,3)
      END AS check_where
    FROM activite.t_indicateur_condition
    WHERE t_indicateur_condition.code LIKE 'CTI%' AND
      column_name IN ('lieu_etage_id', 'lieu_activite_id', 'lieu_service_id', 'lieu_unite_fonctionnelle_id', 'lieu_unite_medicale_id')
    ;
  
    INSERT INTO w_indicateur_condition_table
    SELECT t_indicateur_condition.oid AS indicateur_condition_id,
      'FACTURATION_HONORAIRE'::text As table_name,
      replace(column_name,'lieu_','lieu_ligne_') || ' ' ||
      CASE 
        WHEN t_indicateur_condition.value LIKE 'LS%' THEN 'IN'
        WHEN t_indicateur_condition.value LIKE 'LI%' THEN 'IN'
        WHEN t_indicateur_condition.value LIKE 'SL%' THEN 'NOT IN'
        WHEN t_indicateur_condition.value LIKE 'DT%' THEN '='
        ELSE '='
      END || ' ' ||
      CASE 
        WHEN t_indicateur_condition.value LIKE 'LS%' THEN 
          '(SELECT to_id FROM activite.t_classes_sections_elements WHERE section_id = ' || substr(value,5) || ')'
        WHEN t_indicateur_condition.value LIKE 'LI%' THEN 
          '(SELECT to_id FROM activite.t_listes_contenu WHERE liste_id = ' || substr(value,3) || ')'
        WHEN t_indicateur_condition.value LIKE 'SL%' THEN 
          '(SELECT to_id FROM activite.t_listes_contenu WHERE liste_id = ' || substr(value,3) || ')'
        WHEN t_indicateur_condition.value LIKE 'DT%' THEN 
          substr(value,3)
        ELSE substr(value,3)
      END AS check_where
    FROM activite.t_indicateur_condition
    WHERE t_indicateur_condition.code LIKE 'CTI%' AND
      column_name IN ('lieu_etage_id', 'lieu_activite_id', 'lieu_service_id', 'lieu_unite_fonctionnelle_id', 'lieu_unite_medicale_id')
    ;
  
    INSERT INTO w_indicateur_condition_table
    SELECT t_indicateur_condition.oid AS indicateur_condition_id,
      'EN_COURS'::text As table_name,
      replace(column_name,'lieu_','lieu_reference_') || ' ' ||
      CASE 
        WHEN t_indicateur_condition.value LIKE 'LS%' THEN 'IN'
        WHEN t_indicateur_condition.value LIKE 'LI%' THEN 'IN'
        WHEN t_indicateur_condition.value LIKE 'SL%' THEN 'NOT IN'
        WHEN t_indicateur_condition.value LIKE 'DT%' THEN '='
        ELSE '='
      END || ' ' ||
      CASE 
        WHEN t_indicateur_condition.value LIKE 'LS%' THEN 
          '(SELECT to_id FROM activite.t_classes_sections_elements WHERE section_id = ' || substr(value,5) || ')'
        WHEN t_indicateur_condition.value LIKE 'LI%' THEN 
          '(SELECT to_id FROM activite.t_listes_contenu WHERE liste_id = ' || substr(value,3) || ')'
        WHEN t_indicateur_condition.value LIKE 'SL%' THEN 
          '(SELECT to_id FROM activite.t_listes_contenu WHERE liste_id = ' || substr(value,3) || ')'
        WHEN t_indicateur_condition.value LIKE 'DT%' THEN 
          substr(value,3)
        ELSE substr(value,3)
      END AS check_where
    FROM activite.t_indicateur_condition
    WHERE t_indicateur_condition.code LIKE 'CTI%' AND
      column_name IN ('lieu_etage_id', 'lieu_activite_id', 'lieu_service_id', 'lieu_unite_fonctionnelle_id', 'lieu_unite_medicale_id')
    ;
  
    INSERT INTO w_indicateur_condition_table
    SELECT t_indicateur_condition.oid AS indicateur_condition_id,
      'SEJOURS'::text As table_name,
      replace(column_name,'lieu_','lieu_sortie_') || ' ' ||
      CASE 
        WHEN t_indicateur_condition.value LIKE 'LS%' THEN 'IN'
        WHEN t_indicateur_condition.value LIKE 'LI%' THEN 'IN'
        WHEN t_indicateur_condition.value LIKE 'SL%' THEN 'NOT IN'
        WHEN t_indicateur_condition.value LIKE 'DT%' THEN '='
        ELSE '='
      END || ' ' ||
      CASE 
        WHEN t_indicateur_condition.value LIKE 'LS%' THEN 
          '(SELECT to_id FROM activite.t_classes_sections_elements WHERE section_id = ' || substr(value,5) || ')'
        WHEN t_indicateur_condition.value LIKE 'LI%' THEN 
          '(SELECT to_id FROM activite.t_listes_contenu WHERE liste_id = ' || substr(value,3) || ')'
        WHEN t_indicateur_condition.value LIKE 'SL%' THEN 
          '(SELECT to_id FROM activite.t_listes_contenu WHERE liste_id = ' || substr(value,3) || ')'
        WHEN t_indicateur_condition.value LIKE 'DT%' THEN 
          substr(value,3)
        ELSE substr(value,3)
      END AS check_where
    FROM activite.t_indicateur_condition
    WHERE t_indicateur_condition.code LIKE 'CTI%' AND
      column_name IN ('lieu_etage_id', 'lieu_activite_id', 'lieu_service_id', 'lieu_unite_fonctionnelle_id', 'lieu_unite_medicale_id')
    ;
  
    INSERT INTO w_indicateur_condition_table
    SELECT t_indicateur_condition.oid AS indicateur_condition_id,
      'BUDGET_ACTIVITE'::text As table_name,
      '1<>1' AS check_where
    FROM activite.t_indicateur_condition
    WHERE t_indicateur_condition.code LIKE 'CTI%' AND
      column_name NOT IN ('budget_cle_id')
    ;
  
    INSERT INTO activite.t_indicateur_condition_table(indicateur_condition_id, table_name, check_where)
    SELECT indicateur_condition_id, table_name, check_where
    FROM w_indicateur_condition_table
    WHERE (indicateur_condition_id || '-' || table_name) NOT IN
      (SELECT indicateur_condition_id || '-' || table_name FROM activite.t_indicateur_condition_table WHERE table_name IS NOT NULL)
    ;
  
    UPDATE activite.t_indicateur_condition_table SET
      check_where = w_indicateur_condition_table.check_where
    FROM w_indicateur_condition_table
    WHERE t_indicateur_condition_table.indicateur_condition_id = w_indicateur_condition_table.indicateur_condition_id AND
      t_indicateur_condition_table.table_name = w_indicateur_condition_table.table_name AND
      (
      t_indicateur_condition_table.check_where IS DISTINCT FROM w_indicateur_condition_table.check_where 
      )
    ;
  
    DELETE FROM activite.t_indicateur_condition_table
    USING activite.t_indicateur_condition
    WHERE t_indicateur_condition.oid = t_indicateur_condition_table.indicateur_condition_id AND
      t_indicateur_condition.code LIKE 'CTI%' AND
      (indicateur_condition_id || '-' || table_name) NOT IN
      (SELECT indicateur_condition_id || '-' || table_name FROM w_indicateur_condition_table WHERE table_name IS NOT NULL)
    ;
  
    UPDATE activite.t_indicateur_condition_table
    SET check_where = 'type_sejour_id IN (1,2)'
    WHERE check_where ilike '%type_sejour_id = 12%'
    ;
  
    UPDATE activite.t_indicateur_condition_table
    SET check_where = replace(check_where,'duree_sejour_id','duree')
    WHERE check_where ilike '%duree_sejour_id%'
    ;
  
  
    RETURN 'OK';
  
  END;