dep:
  - v_unites_medicales_2
src: |
  SELECT base.cti_division(
          CASE
              WHEN p_rss_actes.activite_ccam <> '4'::bpchar THEN
              CASE
                  WHEN p_rss_actes.activite_ccam <> '5'::bpchar THEN p_rss_actes.nombre * t_actes_c.icr_1
                  ELSE 0::numeric
              END
              ELSE 0::numeric
          END, GREATEST(1, array_length(p_rss_actes.interventions, 1))::numeric) AS icr_activite_1,
      base.cti_division(
          CASE
              WHEN p_rss_actes.activite_ccam <> '4'::bpchar THEN
              CASE
                  WHEN p_rss_actes.activite_ccam <> '5'::bpchar THEN p_rss_actes.nombre * t_actes_c.icr_1
                  ELSE 0::numeric
              END
              ELSE p_rss_actes.nombre * t_actes_c.icr_4
          END, GREATEST(1, array_length(p_rss_actes.interventions, 1))::numeric) AS icr,
      base.cti_division(
          CASE
              WHEN p_rss_actes.activite_ccam <> '4'::bpchar THEN
              CASE
                  WHEN p_rss_actes.activite_ccam <> '5'::bpchar THEN p_rss_actes.nombre * t_actes_c.icr_prive_atih_1
                  ELSE 0::numeric
              END
              ELSE 0::numeric
          END, GREATEST(1, array_length(p_rss_actes.interventions, 1))::numeric) AS icr_prive_atih_activite_1,
      base.cti_division(
          CASE
              WHEN p_rss_actes.activite_ccam <> '4'::bpchar THEN
              CASE
                  WHEN p_rss_actes.activite_ccam <> '5'::bpchar THEN p_rss_actes.nombre * t_actes_c.icr_prive_atih_1
                  ELSE 0::numeric
              END
              ELSE p_rss_actes.nombre * t_actes_c.icr_prive_atih_4
          END, GREATEST(1, array_length(p_rss_actes.interventions, 1))::numeric) AS icr_prive_atih,
      base.cti_division(
          CASE
              WHEN p_rss_actes.activite_ccam <> '4'::bpchar THEN
              CASE
                  WHEN p_rss_actes.activite_ccam <> '5'::bpchar THEN p_rss_actes.nombre * t_actes_c.icr_public_atih_1
                  ELSE 0::numeric
              END
              ELSE 0::numeric
          END, GREATEST(1, array_length(p_rss_actes.interventions, 1))::numeric) AS icr_public_atih_activite_1,
      base.cti_division(
          CASE
              WHEN p_rss_actes.activite_ccam <> '4'::bpchar THEN
              CASE
                  WHEN p_rss_actes.activite_ccam <> '5'::bpchar THEN p_rss_actes.nombre * t_actes_c.icr_public_atih_1
                  ELSE 0::numeric
              END
              ELSE p_rss_actes.nombre * t_actes_c.icr_public_atih_4
          END, GREATEST(1, array_length(p_rss_actes.interventions, 1))::numeric) AS icr_public_atih,
      base.cti_division(
          CASE
              WHEN p_rss_actes.activite_ccam <> '4'::bpchar THEN
              CASE
                  WHEN p_rss_actes.activite_ccam <> '5'::bpchar THEN p_rss_actes.nombre * t_actes_c.tarif_1
                  ELSE p_rss_actes.nombre * t_actes_c.tarif_5
              END
              ELSE 0::numeric
          END, GREATEST(1, array_length(p_rss_actes.interventions, 1))::numeric) AS tarif_activite_1,
      base.cti_division(
          CASE
              WHEN p_rss_actes.activite_ccam <> '4'::bpchar THEN
              CASE
                  WHEN p_rss_actes.activite_ccam <> '5'::bpchar THEN p_rss_actes.nombre * t_actes_c.tarif_1
                  ELSE p_rss_actes.nombre * t_actes_c.tarif_5
              END
              ELSE p_rss_actes.nombre * t_actes_c.tarif_4
          END, GREATEST(1, array_length(p_rss_actes.interventions, 1))::numeric) AS tarif,
      base.cti_division(
          CASE
              WHEN p_rss_actes.activite_ccam <> '4'::bpchar THEN p_rss_actes.nombre
              ELSE 0::numeric
          END, GREATEST(1, array_length(p_rss_actes.interventions, 1))::numeric) AS nombre_activite_1,
          CASE
              WHEN p_rss_actes.activite_ccam <> '4'::bpchar THEN t_actes_c.ccam_regroupement_code_1
              ELSE t_actes_c.ccam_regroupement_code_4
          END AS acte_regroupement_code,
          CASE
              WHEN p_rss_actes.activite_ccam <> '4'::bpchar THEN t_actes_c.ccam_regroupement_id_1
              ELSE t_actes_c.ccam_regroupement_id_4
          END AS acte_regroupement_id,
          CASE
              WHEN p_rss_actes.activite_ccam <> '4'::bpchar THEN t_actes_c.ccam_regroupement_texte_1
              ELSE t_actes_c.ccam_regroupement_texte_4
          END AS acte_regroupement_texte,
      base.cti_division(
          CASE
              WHEN p_rss_actes.activite_ccam = '4'::bpchar THEN p_rss_actes.nombre * t_actes_c.icr_4
              ELSE 0::numeric
          END, GREATEST(1, array_length(p_rss_actes.interventions, 1))::numeric) AS icr_activite_4,
      base.cti_division(
          CASE
              WHEN p_rss_actes.activite_ccam = '4'::bpchar THEN p_rss_actes.nombre * t_actes_c.icr_prive_atih_4
              ELSE 0::numeric
          END, GREATEST(1, array_length(p_rss_actes.interventions, 1))::numeric) AS icr_prive_atih_activite_4,
      base.cti_division(
          CASE
              WHEN p_rss_actes.activite_ccam = '4'::bpchar THEN p_rss_actes.nombre * t_actes_c.icr_public_atih_4
              ELSE 0::numeric
          END, GREATEST(1, array_length(p_rss_actes.interventions, 1))::numeric) AS icr_public_atih_activite_4,
      base.cti_division(
          CASE
              WHEN p_rss_actes.activite_ccam = '4'::bpchar THEN p_rss_actes.nombre * t_actes_c.tarif_4
              ELSE 0::numeric
          END, GREATEST(1, array_length(p_rss_actes.interventions, 1))::numeric) AS tarif_activite_4,
      base.cti_division(
          CASE
              WHEN p_rss_actes.activite_ccam = '4'::bpchar THEN p_rss_actes.nombre
              ELSE 0::numeric
          END, GREATEST(1, array_length(p_rss_actes.interventions, 1))::numeric) AS nombre_activite_4,
          CASE
              WHEN substr(t_actes_c.chapitre_3_code, 7, 2) = '99'::text THEN t_actes_c.chapitre_2_code
              ELSE t_actes_c.chapitre_3_code
          END AS ccam_chapitre_3_code,
          CASE
              WHEN substr(t_actes_c.chapitre_3_code, 7, 2) = '99'::text THEN t_actes_c.chapitre_2_id
              ELSE t_actes_c.chapitre_3_id
          END AS ccam_chapitre_3_id,
          CASE
              WHEN substr(t_actes_c.chapitre_3_code, 7, 2) = '99'::text THEN t_actes_c.chapitre_2_texte
              ELSE t_actes_c.chapitre_3_texte
          END AS ccam_chapitre_3_texte,
          CASE
              WHEN substr(t_actes_c.chapitre_4_code, 10, 2) = '99'::text THEN
              CASE
                  WHEN substr(t_actes_c.chapitre_3_code, 7, 2) = '99'::text THEN t_actes_c.chapitre_2_code
                  ELSE t_actes_c.chapitre_3_code
              END
              ELSE t_actes_c.chapitre_4_code
          END AS ccam_chapitre_4_code,
          CASE
              WHEN substr(t_actes_c.chapitre_4_code, 10, 2) = '99'::text THEN
              CASE
                  WHEN substr(t_actes_c.chapitre_3_code, 7, 2) = '99'::text THEN t_actes_c.chapitre_2_id
                  ELSE t_actes_c.chapitre_3_id
              END
              ELSE t_actes_c.chapitre_4_id
          END AS ccam_chapitre_4_id,
          CASE
              WHEN substr(t_actes_c.chapitre_4_code, 10, 2) = '99'::text THEN
              CASE
                  WHEN substr(t_actes_c.chapitre_3_code, 7, 2) = '99'::text THEN t_actes_c.chapitre_2_texte
                  ELSE t_actes_c.chapitre_3_texte
              END
              ELSE t_actes_c.chapitre_4_texte
          END AS ccam_chapitre_4_texte,
      p_rss_actes.acte_id,
      p_rss_actes.acte_id AS acte_principal_id,
      t_actes_c.code AS acte_principal_code,
      t_actes_c.texte AS acte_principal_texte,
      p_rss_actes.activite_ccam,
      p_rss_actes.association_nonprevue_ccam,
      p_rss_actes.condition_gm_ok,
      p_rss_actes.date_acte,
      p_rss_actes.extension_ccam,
      p_rss_actes.finess,
      p_rss_actes.modificateur_ccam_1,
      p_rss_actes.modificateur_ccam_2,
      p_rss_actes.modificateur_ccam_3,
      p_rss_actes.modificateur_ccam_4,
      p_rss_actes.no_rss,
      p_rss_actes.no_rum,
      base.cti_division(p_rss_actes.nombre, GREATEST(1, array_length(p_rss_actes.interventions, 1))::numeric) AS cti_division,
      p_rss_actes.phase_ccam,
      p_rss_actes.remboursement_excp_ccam,
      p_rss_actes.rss_id,
      t_actes_c.action_code AS ccam_action_code,
      t_actes_c.action_id AS ccam_action_id,
      t_actes_c.action_texte AS ccam_action_texte,
      t_actes_c.chapitre_1_code AS ccam_chapitre_1_code,
      t_actes_c.chapitre_1_id AS ccam_chapitre_1_id,
      t_actes_c.chapitre_1_texte AS ccam_chapitre_1_texte,
      t_actes_c.chapitre_2_code AS ccam_chapitre_2_code,
      t_actes_c.chapitre_2_id AS ccam_chapitre_2_id,
      t_actes_c.chapitre_2_texte AS ccam_chapitre_2_texte,
      t_actes_c.code AS acte_code,
      t_actes_c.nomenclature AS acte_nomenclature,
      t_actes_c.section_code AS acte_section_code,
      t_actes_c.section_id AS acte_section_id,
      t_actes_c.section_texte AS acte_section_texte,
      t_actes_c.technique_code AS ccam_technique_code,
      t_actes_c.technique_id AS ccam_technique_id,
      t_actes_c.technique_texte AS ccam_technique_texte,
      t_actes_c.texte_court AS acte_texte,
      t_actes_c.topographie_1_code AS ccam_topographie_1_code,
      t_actes_c.topographie_1_id AS ccam_topographie_1_id,
      t_actes_c.topographie_1_texte AS ccam_topographie_1_texte,
      t_actes_c.topographie_2_code AS ccam_topographie_2_code,
      t_actes_c.topographie_2_id AS ccam_topographie_2_id,
      t_actes_c.topographie_2_texte AS ccam_topographie_2_texte,
      t_actes_c.type_code AS ccam_type_code,
      t_actes_c.type_id AS ccam_type_id,
      t_actes_c.type_texte AS ccam_type_texte,
          CASE
              WHEN p_rss_actes.activite_ccam <> '4'::bpchar THEN p_rss_actes.medecin_id
              ELSE NULL::bigint
          END AS medecin_id,
          CASE
              WHEN p_rss_actes.activite_ccam <> '4'::bpchar THEN t_medecins_c.code
              ELSE NULL::text
          END AS medecin_code,
          CASE
              WHEN p_rss_actes.activite_ccam <> '4'::bpchar THEN t_medecins_c.nom_prenom
              ELSE NULL::text
          END AS medecin_nom,
          CASE
              WHEN p_rss_actes.activite_ccam <> '4'::bpchar THEN t_medecins_c.specialite_id
              ELSE NULL::bigint
          END AS specialite_medecin_id,
          CASE
              WHEN p_rss_actes.activite_ccam <> '4'::bpchar THEN t_medecins_c.specialite_medecin_code
              ELSE NULL::character varying
          END AS specialite_medecin_code,
          CASE
              WHEN p_rss_actes.activite_ccam <> '4'::bpchar THEN t_medecins_c.specialite_medecin_texte
              ELSE NULL::character varying
          END AS specialite_medecin_texte,
          CASE
              WHEN p_rss_actes.activite_ccam <> '4'::bpchar THEN t_medecins_c.specialite_interne_id
              ELSE NULL::bigint
          END AS specialite_interne_medecin_id,
          CASE
              WHEN p_rss_actes.activite_ccam <> '4'::bpchar THEN t_medecins_c.specialite_interne_medecin_code
              ELSE NULL::character varying
          END AS specialite_interne_medecin_code,
          CASE
              WHEN p_rss_actes.activite_ccam <> '4'::bpchar THEN t_medecins_c.specialite_interne_medecin_texte
              ELSE NULL::character varying
          END AS specialite_interne_medecin_texte,
          CASE
              WHEN p_rss_actes.activite_ccam <> '4'::bpchar THEN t_medecins_c.medecin_reference_id
              ELSE NULL::bigint
          END AS medecin_reference_id,
          CASE
              WHEN p_rss_actes.activite_ccam <> '4'::bpchar THEN t_medecins_c.medecin_reference_code
              ELSE NULL::text
          END AS medecin_reference_code,
          CASE
              WHEN p_rss_actes.activite_ccam <> '4'::bpchar THEN t_medecins_c.medecin_reference_nom_prenom
              ELSE NULL::text
          END AS medecin_reference_nom,
          CASE
              WHEN p_rss_actes.activite_ccam <> '4'::bpchar THEN t_medecins_c.section_code
              ELSE NULL::character varying[]
          END AS medecin_reference_section_code,
          CASE
              WHEN p_rss_actes.activite_ccam <> '4'::bpchar THEN t_medecins_c.section_id
              ELSE NULL::bigint[]
          END AS medecin_reference_section_id,
          CASE
              WHEN p_rss_actes.activite_ccam <> '4'::bpchar THEN t_medecins_c.section_texte
              ELSE NULL::character varying[]
          END AS medecin_reference_section_texte,
          CASE
              WHEN p_rss_actes.activite_ccam <> '4'::bpchar THEN t_medecins_c.specialite_interne_medecin_reference_id
              ELSE NULL::bigint
          END AS specialite_interne_medecin_reference_id,
          CASE
              WHEN p_rss_actes.activite_ccam <> '4'::bpchar THEN t_medecins_c.specialite_interne_medecin_reference_code
              ELSE NULL::character varying
          END AS specialite_interne_medecin_reference_code,
          CASE
              WHEN p_rss_actes.activite_ccam <> '4'::bpchar THEN t_medecins_c.specialite_interne_medecin_reference_texte
              ELSE NULL::character varying
          END AS specialite_interne_medecin_reference_texte,
          CASE
              WHEN p_rss_actes.activite_ccam <> '4'::bpchar THEN t_medecins_c.specialite_medecin_reference_id
              ELSE NULL::bigint
          END AS specialite_medecin_reference_id,
          CASE
              WHEN p_rss_actes.activite_ccam <> '4'::bpchar THEN t_medecins_c.specialite_medecin_reference_code
              ELSE NULL::character varying
          END AS specialite_medecin_reference_code,
          CASE
              WHEN p_rss_actes.activite_ccam <> '4'::bpchar THEN t_medecins_c.specialite_medecin_reference_texte
              ELSE NULL::character varying
          END AS specialite_medecin_reference_texte,
          CASE
              WHEN p_rss_actes.activite_ccam = '4'::bpchar THEN p_rss_actes.medecin_id
              ELSE NULL::bigint
          END AS anesthesiste_id,
          CASE
              WHEN p_rss_actes.activite_ccam = '4'::bpchar THEN t_medecins_c.code
              ELSE NULL::text
          END AS anesthesiste_code,
          CASE
              WHEN p_rss_actes.activite_ccam = '4'::bpchar THEN t_medecins_c.nom_prenom
              ELSE NULL::text
          END AS anesthesiste_nom,
          CASE
              WHEN p_rss_actes.activite_ccam = '4'::bpchar THEN t_medecins_c.specialite_id
              ELSE NULL::bigint
          END AS specialite_anesthesiste_id,
          CASE
              WHEN p_rss_actes.activite_ccam = '4'::bpchar THEN t_medecins_c.specialite_medecin_code
              ELSE NULL::character varying
          END AS specialite_anesthesiste_code,
          CASE
              WHEN p_rss_actes.activite_ccam = '4'::bpchar THEN t_medecins_c.specialite_medecin_texte
              ELSE NULL::character varying
          END AS specialite_anesthesiste_texte,
          CASE
              WHEN p_rss_actes.activite_ccam = '4'::bpchar THEN t_medecins_c.specialite_interne_id
              ELSE NULL::bigint
          END AS specialite_interne_anesthesiste_id,
          CASE
              WHEN p_rss_actes.activite_ccam = '4'::bpchar THEN t_medecins_c.specialite_interne_medecin_code
              ELSE NULL::character varying
          END AS specialite_interne_anesthesiste_code,
          CASE
              WHEN p_rss_actes.activite_ccam = '4'::bpchar THEN t_medecins_c.specialite_interne_medecin_texte
              ELSE NULL::character varying
          END AS specialite_interne_anesthesiste_texte,
          CASE
              WHEN p_rss_actes.activite_ccam = '4'::bpchar THEN t_medecins_c.medecin_reference_id
              ELSE NULL::bigint
          END AS anesthesiste_reference_id,
          CASE
              WHEN p_rss_actes.activite_ccam = '4'::bpchar THEN t_medecins_c.medecin_reference_code
              ELSE NULL::text
          END AS anesthesiste_reference_code,
          CASE
              WHEN p_rss_actes.activite_ccam = '4'::bpchar THEN t_medecins_c.medecin_reference_nom_prenom
              ELSE NULL::text
          END AS anesthesiste_reference_nom,
          CASE
              WHEN p_rss_actes.activite_ccam = '4'::bpchar THEN t_medecins_c.section_code
              ELSE NULL::character varying[]
          END AS anesthesiste_reference_section_code,
          CASE
              WHEN p_rss_actes.activite_ccam = '4'::bpchar THEN t_medecins_c.section_id
              ELSE NULL::bigint[]
          END AS anesthesiste_reference_section_id,
          CASE
              WHEN p_rss_actes.activite_ccam = '4'::bpchar THEN t_medecins_c.section_texte
              ELSE NULL::character varying[]
          END AS anesthesiste_reference_section_texte,
          CASE
              WHEN p_rss_actes.activite_ccam = '4'::bpchar THEN t_medecins_c.specialite_interne_medecin_reference_id
              ELSE NULL::bigint
          END AS specialite_interne_anesthesiste_reference_id,
          CASE
              WHEN p_rss_actes.activite_ccam = '4'::bpchar THEN t_medecins_c.specialite_interne_medecin_reference_code
              ELSE NULL::character varying
          END AS specialite_interne_anesthesiste_reference_code,
          CASE
              WHEN p_rss_actes.activite_ccam = '4'::bpchar THEN t_medecins_c.specialite_interne_medecin_reference_texte
              ELSE NULL::character varying
          END AS specialite_interne_anesthesiste_reference_texte,
          CASE
              WHEN p_rss_actes.activite_ccam = '4'::bpchar THEN t_medecins_c.specialite_medecin_reference_id
              ELSE NULL::bigint
          END AS specialite_anesthesiste_reference_id,
          CASE
              WHEN p_rss_actes.activite_ccam = '4'::bpchar THEN t_medecins_c.specialite_medecin_reference_code
              ELSE NULL::character varying
          END AS specialite_anesthesiste_reference_code,
          CASE
              WHEN p_rss_actes.activite_ccam = '4'::bpchar THEN t_medecins_c.specialite_medecin_reference_texte
              ELSE NULL::character varying
          END AS specialite_anesthesiste_reference_texte,
      p_rss_actes.pole_id AS pole_rum_id,
      t_pole.code AS pole_rum_code,
      t_pole.texte AS pole_rum_texte,
      t_pole.section_id AS pole_rum_section_id,
      t_pole.section_code AS pole_rum_section_code,
      t_pole.section_texte AS pole_rum_section_texte,
      p_rss_actes.unite_fonctionnelle_id,
      t_unites_fonctionnelles.code AS unite_fonctionnelle_code,
      t_unites_fonctionnelles.texte AS unite_fonctionnelle_texte,
      t_unites_fonctionnelles.section_code AS unite_fonctionnelle_section_code,
      t_unites_fonctionnelles.section_id AS unite_fonctionnelle_section_id,
      t_unites_fonctionnelles.section_texte AS unite_fonctionnelle_section_texte,
      unnest(p_rss_actes.interventions) AS no_intervention,
      p_rss_actes.unite_medicale,
      p_rss_actes.unite_medicale_id,
      v_unites_medicales_2.code AS unite_medicale_code,
      v_unites_medicales_2.texte AS unite_medicale_texte,
      v_unites_medicales_2.type_autorisation,
      v_unites_medicales_2.type_autorisation_texte,
      v_unites_medicales_2.section_id AS unite_medicale_section_id,
      v_unites_medicales_2.section_code AS unite_medicale_section_code,
      v_unites_medicales_2.section_texte AS unite_medicale_section_texte,
      p_rss_actes.acte_avec_anesthesie,
      p_rss_actes.acte_ccam_descriptif_id
     FROM pmsi.p_rss_actes p_rss_actes
       JOIN pmsi.t_actes_c ON p_rss_actes.acte_id = t_actes_c.oid
       JOIN pmsi.t_medecins_c ON p_rss_actes.medecin_id = t_medecins_c.oid
       JOIN pmsi.v_unites_medicales_2 ON p_rss_actes.unite_medicale_id = v_unites_medicales_2.oid
       JOIN pmsi.t_unites_fonctionnelles ON p_rss_actes.unite_fonctionnelle_id = t_unites_fonctionnelles.oid
       LEFT JOIN pmsi.t_pole ON p_rss_actes.pole_id = t_pole.oid;