src: |
  SELECT p_factures_reference_tiers.sejour_id,
      p_factures_reference_tiers.no_sejour,
      p_factures_reference_tiers.date_entree,
      p_factures_reference_tiers.date_sortie,
      p_factures_reference_tiers.code_sorti,
      p_factures_reference_tiers.facture_reference_id,
      p_factures_reference_tiers.no_facture_reference,
      p_factures_reference_tiers.type_t2a,
      p_factures_reference_tiers.date_debut_facture,
      p_factures_reference_tiers.date_fin_facture,
      p_factures_reference_tiers.date_groupage,
      p_factures_reference_tiers.retard_groupage,
      p_factures_reference_tiers.delai_groupage,
      p_factures_reference_tiers.nb_groupe,
      p_factures_reference_tiers.nb_non_groupe,
      p_factures_reference_tiers.nb_calculable,
          CASE
              WHEN p_factures_reference_tiers.type_t2a = '1'::bpchar AND p_factures_reference_tiers.ghs_id = 0 AND p_factures_reference_tiers.code_facture <> '1'::text THEN 1
              ELSE 0
          END AS nb_non_groupes,
      p_factures_reference_tiers.ghs_id,
      p_factures_reference_tiers.ghs_bebe1_id,
      p_factures_reference_tiers.ghs_bebe2_id,
      p_factures_reference_tiers.ghs_bebe3_id,
      p_factures_reference_tiers.tiers_payant_id,
      t_tiers_payant.code AS tiers_payant_code,
      t_tiers_payant.texte_court AS tiers_payant_texte,
      t_tiers_payant.type_tiers_payant_id,
      t_tiers_payant.type_tiers_payant AS type_tiers_payant_code,
      t_tiers_payant.type_tiers_payant_texte,
      t_tiers_payant.grand_regime_id AS tiers_payant_grand_regime_id,
      t_tiers_payant.grand_regime_code AS tiers_payant_grand_regime_code,
      t_tiers_payant.grand_regime_texte AS tiers_payant_grand_regime_texte,
      t_tiers_payant.section_id AS tiers_payant_section_id,
      t_tiers_payant.section_code AS tiers_payant_section_code,
      t_tiers_payant.section_texte AS tiers_payant_section_texte,
      p_factures_reference_tiers.code_facture,
      p_factures_reference_tiers.date_facture,
      p_factures_reference_tiers.delai_facture,
      p_factures_reference_tiers.retard_facture,
      p_factures_reference_tiers.code_vente,
      p_factures_reference_tiers.date_vente,
      p_factures_reference_tiers.nb_factures,
          CASE
              WHEN p_factures_reference_tiers.nb_non_calcules = 0 THEN 1
              ELSE 0
          END AS nb_calcules,
      p_factures_reference_tiers.nb_non_calcules,
          CASE
              WHEN p_factures_reference_tiers.type_t2a = '1'::bpchar AND p_factures_reference_tiers.ghs_id <> 0 OR p_factures_reference_tiers.type_t2a <> '1'::bpchar THEN p_factures_reference_tiers.nb_non_calcules
              ELSE 0
          END AS nb_non_calcules_groupes,
          CASE
              WHEN p_factures_reference_tiers.type_t2a = '1'::bpchar AND p_factures_reference_tiers.ghs_id <> 0 OR p_factures_reference_tiers.type_t2a <> '1'::bpchar THEN 0
              ELSE p_factures_reference_tiers.nb_non_calcules
          END AS nb_non_calcules_non_groupes,
      p_factures_reference_tiers.nb_factures_regularisation,
      p_factures_reference_tiers.nb_rejets,
      p_factures_reference_tiers.code_expedie_0,
      p_factures_reference_tiers.code_expedie_1,
      p_factures_reference_tiers.code_expedie_2,
      p_factures_reference_tiers.date_expedition,
      p_factures_reference_tiers.date_expedition_0,
      p_factures_reference_tiers.date_expedition_1,
      p_factures_reference_tiers.date_expedition_2,
      p_factures_reference_tiers.no_bordereau_0,
      p_factures_reference_tiers.no_bordereau_1,
      p_factures_reference_tiers.no_bordereau_2,
      p_factures_reference_tiers.delai_expedition,
      p_factures_reference_tiers.delai_expedition_0,
      p_factures_reference_tiers.delai_expedition_1,
      p_factures_reference_tiers.delai_expedition_2,
      p_factures_reference_tiers.nb_non_expedie,
      p_factures_reference_tiers.nb_non_expedie_c,
      p_factures_reference_tiers.nb_non_expedie_h,
      p_factures_reference_tiers.nb_non_expedie_0,
      p_factures_reference_tiers.nb_non_expedie_0_c,
      p_factures_reference_tiers.nb_non_expedie_0_h,
      p_factures_reference_tiers.nb_non_expedie_1,
      p_factures_reference_tiers.nb_non_expedie_1_c,
      p_factures_reference_tiers.nb_non_expedie_1_h,
      p_factures_reference_tiers.nb_non_expedie_2,
      p_factures_reference_tiers.nb_non_expedie_2_c,
      p_factures_reference_tiers.nb_non_expedie_2_h,
          CASE
              WHEN p_factures_reference_tiers.nb_non_expedie_0 = 1::numeric THEN p_factures_reference_tiers.montant_comptabilise_0_c - p_factures_reference_tiers.montant_regle_0_c + p_factures_reference_tiers.montant_comptabilise_0_h - p_factures_reference_tiers.montant_regle_0_h
              ELSE 0::numeric
          END +
          CASE
              WHEN p_factures_reference_tiers.nb_non_expedie_1 = 1::numeric THEN p_factures_reference_tiers.montant_comptabilise_1_c - p_factures_reference_tiers.montant_regle_1_c + p_factures_reference_tiers.montant_comptabilise_1_h - p_factures_reference_tiers.montant_regle_1_h
              ELSE 0::numeric
          END +
          CASE
              WHEN p_factures_reference_tiers.nb_non_expedie_2 = 1::numeric THEN p_factures_reference_tiers.montant_comptabilise_2_c - p_factures_reference_tiers.montant_regle_2_c + p_factures_reference_tiers.montant_comptabilise_2_h - p_factures_reference_tiers.montant_regle_2_h
              ELSE 0::numeric
          END AS montant_non_expedie,
          CASE
              WHEN p_factures_reference_tiers.nb_non_expedie_0 = 1::numeric THEN p_factures_reference_tiers.montant_comptabilise_0_c - p_factures_reference_tiers.montant_regle_0_c
              ELSE 0::numeric
          END +
          CASE
              WHEN p_factures_reference_tiers.nb_non_expedie_1 = 1::numeric THEN p_factures_reference_tiers.montant_comptabilise_1_c - p_factures_reference_tiers.montant_regle_1_c
              ELSE 0::numeric
          END +
          CASE
              WHEN p_factures_reference_tiers.nb_non_expedie_2 = 1::numeric THEN p_factures_reference_tiers.montant_comptabilise_2_c - p_factures_reference_tiers.montant_regle_2_c
              ELSE 0::numeric
          END AS montant_non_expedie_c,
          CASE
              WHEN p_factures_reference_tiers.nb_non_expedie_0 = 1::numeric THEN p_factures_reference_tiers.montant_comptabilise_0_h - p_factures_reference_tiers.montant_regle_0_h
              ELSE 0::numeric
          END +
          CASE
              WHEN p_factures_reference_tiers.nb_non_expedie_1 = 1::numeric THEN p_factures_reference_tiers.montant_comptabilise_1_h - p_factures_reference_tiers.montant_regle_1_h
              ELSE 0::numeric
          END +
          CASE
              WHEN p_factures_reference_tiers.nb_non_expedie_2 = 1::numeric THEN p_factures_reference_tiers.montant_comptabilise_2_h - p_factures_reference_tiers.montant_regle_2_h
              ELSE 0::numeric
          END AS montant_non_expedie_h,
          CASE
              WHEN p_factures_reference_tiers.nb_non_expedie_0 = 1::numeric THEN p_factures_reference_tiers.montant_comptabilise_0 - p_factures_reference_tiers.montant_regle_0
              ELSE 0::numeric
          END AS montant_non_expedie_0,
          CASE
              WHEN p_factures_reference_tiers.nb_non_expedie_0 = 1::numeric THEN p_factures_reference_tiers.montant_comptabilise_0_c - p_factures_reference_tiers.montant_regle_0_c
              ELSE 0::numeric
          END AS montant_non_expedie_0_c,
          CASE
              WHEN p_factures_reference_tiers.nb_non_expedie_0 = 1::numeric THEN p_factures_reference_tiers.montant_comptabilise_0_h - p_factures_reference_tiers.montant_regle_0_h
              ELSE 0::numeric
          END AS montant_non_expedie_0_h,
          CASE
              WHEN p_factures_reference_tiers.nb_non_expedie_1 = 1::numeric THEN p_factures_reference_tiers.montant_comptabilise_1 - p_factures_reference_tiers.montant_regle_1
              ELSE 0::numeric
          END AS montant_non_expedie_1,
          CASE
              WHEN p_factures_reference_tiers.nb_non_expedie_1 = 1::numeric THEN p_factures_reference_tiers.montant_comptabilise_1_c - p_factures_reference_tiers.montant_regle_1_c
              ELSE 0::numeric
          END AS montant_non_expedie_1_c,
          CASE
              WHEN p_factures_reference_tiers.nb_non_expedie_1 = 1::numeric THEN p_factures_reference_tiers.montant_comptabilise_1_h - p_factures_reference_tiers.montant_regle_1_h
              ELSE 0::numeric
          END AS montant_non_expedie_1_h,
          CASE
              WHEN p_factures_reference_tiers.nb_non_expedie_2 = 1::numeric THEN p_factures_reference_tiers.montant_comptabilise_2 - p_factures_reference_tiers.montant_regle_2
              ELSE 0::numeric
          END AS montant_non_expedie_2,
          CASE
              WHEN p_factures_reference_tiers.nb_non_expedie_2 = 1::numeric THEN p_factures_reference_tiers.montant_comptabilise_2_c - p_factures_reference_tiers.montant_regle_2_c
              ELSE 0::numeric
          END AS montant_non_expedie_2_c,
          CASE
              WHEN p_factures_reference_tiers.nb_non_expedie_2 = 1::numeric THEN p_factures_reference_tiers.montant_comptabilise_2_h - p_factures_reference_tiers.montant_regle_2_h
              ELSE 0::numeric
          END AS montant_non_expedie_2_h,
      p_factures_reference_tiers.montant_facture,
      p_factures_reference_tiers.montant_facture_c,
      p_factures_reference_tiers.montant_facture_h,
      p_factures_reference_tiers.montant_facture_0,
      p_factures_reference_tiers.montant_facture_0_c,
      p_factures_reference_tiers.montant_facture_0_h,
      p_factures_reference_tiers.montant_facture_1,
      p_factures_reference_tiers.montant_facture_1_c,
      p_factures_reference_tiers.montant_facture_1_h,
      p_factures_reference_tiers.montant_facture_2,
      p_factures_reference_tiers.montant_facture_2_c,
      p_factures_reference_tiers.montant_facture_2_h,
      p_factures_reference_tiers.montant_facture_c_actes_inclus_dans_sejour,
      p_factures_reference_tiers.montant_facture_h_actes_inclus_dans_sejour,
      p_factures_reference_tiers.montant_encours,
      p_factures_reference_tiers.montant_encours_c,
      p_factures_reference_tiers.montant_encours_h,
      p_factures_reference_tiers.montant_encours_0,
      p_factures_reference_tiers.montant_encours_0_c,
      p_factures_reference_tiers.montant_encours_0_h,
      p_factures_reference_tiers.montant_encours_1,
      p_factures_reference_tiers.montant_encours_1_c,
      p_factures_reference_tiers.montant_encours_1_h,
      p_factures_reference_tiers.montant_encours_2,
      p_factures_reference_tiers.montant_encours_2_c,
      p_factures_reference_tiers.montant_encours_2_h,
          CASE
              WHEN p_factures_reference_tiers.nb_non_groupe = 0::numeric THEN p_factures_reference_tiers.montant_encours
              ELSE 0::numeric
          END AS montant_encours_groupes,
          CASE
              WHEN p_factures_reference_tiers.nb_non_groupe = 0::numeric THEN p_factures_reference_tiers.montant_encours_c
              ELSE 0::numeric
          END AS montant_encours_groupes_c,
          CASE
              WHEN p_factures_reference_tiers.nb_non_groupe = 0::numeric THEN p_factures_reference_tiers.montant_encours_h
              ELSE 0::numeric
          END AS montant_encours_groupes_h,
          CASE
              WHEN p_factures_reference_tiers.nb_non_groupe = 0::numeric THEN p_factures_reference_tiers.montant_encours_0
              ELSE 0::numeric
          END AS montant_encours_groupes_0,
          CASE
              WHEN p_factures_reference_tiers.nb_non_groupe = 0::numeric THEN p_factures_reference_tiers.montant_encours_0_c
              ELSE 0::numeric
          END AS montant_encours_groupes_0_c,
          CASE
              WHEN p_factures_reference_tiers.nb_non_groupe = 0::numeric THEN p_factures_reference_tiers.montant_encours_0_h
              ELSE 0::numeric
          END AS montant_encours_groupes_0_h,
          CASE
              WHEN p_factures_reference_tiers.nb_non_groupe = 0::numeric THEN p_factures_reference_tiers.montant_encours_1
              ELSE 0::numeric
          END AS montant_encours_groupes_1,
          CASE
              WHEN p_factures_reference_tiers.nb_non_groupe = 0::numeric THEN p_factures_reference_tiers.montant_encours_1_c
              ELSE 0::numeric
          END AS montant_encours_groupes_1_c,
          CASE
              WHEN p_factures_reference_tiers.nb_non_groupe = 0::numeric THEN p_factures_reference_tiers.montant_encours_1_h
              ELSE 0::numeric
          END AS montant_encours_groupes_1_h,
          CASE
              WHEN p_factures_reference_tiers.nb_non_groupe = 0::numeric THEN p_factures_reference_tiers.montant_encours_2
              ELSE 0::numeric
          END AS montant_encours_groupes_2,
          CASE
              WHEN p_factures_reference_tiers.nb_non_groupe = 0::numeric THEN p_factures_reference_tiers.montant_encours_2_c
              ELSE 0::numeric
          END AS montant_encours_groupes_2_c,
          CASE
              WHEN p_factures_reference_tiers.nb_non_groupe = 0::numeric THEN p_factures_reference_tiers.montant_encours_2_h
              ELSE 0::numeric
          END AS montant_encours_groupes_2_h,
          CASE
              WHEN p_factures_reference_tiers.nb_non_groupe = 1::numeric THEN p_factures_reference_tiers.montant_encours
              ELSE 0::numeric
          END AS montant_encours_non_groupes,
          CASE
              WHEN p_factures_reference_tiers.nb_non_groupe = 1::numeric THEN p_factures_reference_tiers.montant_encours_c
              ELSE 0::numeric
          END AS montant_encours_non_groupes_c,
          CASE
              WHEN p_factures_reference_tiers.nb_non_groupe = 1::numeric THEN p_factures_reference_tiers.montant_encours_h
              ELSE 0::numeric
          END AS montant_encours_non_groupes_h,
          CASE
              WHEN p_factures_reference_tiers.nb_non_groupe = 1::numeric THEN p_factures_reference_tiers.montant_encours_0
              ELSE 0::numeric
          END AS montant_encours_non_groupes_0,
          CASE
              WHEN p_factures_reference_tiers.nb_non_groupe = 1::numeric THEN p_factures_reference_tiers.montant_encours_0_c
              ELSE 0::numeric
          END AS montant_encours_non_groupes_0_c,
          CASE
              WHEN p_factures_reference_tiers.nb_non_groupe = 1::numeric THEN p_factures_reference_tiers.montant_encours_0_h
              ELSE 0::numeric
          END AS montant_encours_non_groupes_0_h,
          CASE
              WHEN p_factures_reference_tiers.nb_non_groupe = 1::numeric THEN p_factures_reference_tiers.montant_encours_1
              ELSE 0::numeric
          END AS montant_encours_non_groupes_1,
          CASE
              WHEN p_factures_reference_tiers.nb_non_groupe = 1::numeric THEN p_factures_reference_tiers.montant_encours_1_c
              ELSE 0::numeric
          END AS montant_encours_non_groupes_1_c,
          CASE
              WHEN p_factures_reference_tiers.nb_non_groupe = 1::numeric THEN p_factures_reference_tiers.montant_encours_1_h
              ELSE 0::numeric
          END AS montant_encours_non_groupes_1_h,
          CASE
              WHEN p_factures_reference_tiers.nb_non_groupe = 1::numeric THEN p_factures_reference_tiers.montant_encours_2
              ELSE 0::numeric
          END AS montant_encours_non_groupes_2,
          CASE
              WHEN p_factures_reference_tiers.nb_non_groupe = 1::numeric THEN p_factures_reference_tiers.montant_encours_2_c
              ELSE 0::numeric
          END AS montant_encours_non_groupes_2_c,
          CASE
              WHEN p_factures_reference_tiers.nb_non_groupe = 1::numeric THEN p_factures_reference_tiers.montant_encours_2_h
              ELSE 0::numeric
          END AS montant_encours_non_groupes_2_h,
          CASE
              WHEN p_factures_reference_tiers.nb_non_calcules > 0 THEN p_factures_reference_tiers.montant_facture + p_factures_reference_tiers.montant_encours
              ELSE 0::numeric
          END AS montant_non_calcules,
          CASE
              WHEN p_factures_reference_tiers.nb_non_calcules > 0 THEN p_factures_reference_tiers.montant_facture_c + p_factures_reference_tiers.montant_encours_c
              ELSE 0::numeric
          END AS montant_non_calcules_c,
          CASE
              WHEN p_factures_reference_tiers.nb_non_calcules > 0 THEN p_factures_reference_tiers.montant_facture_h + p_factures_reference_tiers.montant_encours_h
              ELSE 0::numeric
          END AS montant_non_calcules_h,
          CASE
              WHEN p_factures_reference_tiers.nb_non_calcules > 0 THEN p_factures_reference_tiers.montant_facture_0 + p_factures_reference_tiers.montant_encours_0
              ELSE 0::numeric
          END AS montant_non_calcules_0,
          CASE
              WHEN p_factures_reference_tiers.nb_non_calcules > 0 THEN p_factures_reference_tiers.montant_facture_0_c + p_factures_reference_tiers.montant_encours_0_c
              ELSE 0::numeric
          END AS montant_non_calcules_0_c,
          CASE
              WHEN p_factures_reference_tiers.nb_non_calcules > 0 THEN p_factures_reference_tiers.montant_facture_0_h + p_factures_reference_tiers.montant_encours_0_h
              ELSE 0::numeric
          END AS montant_non_calcules_0_h,
          CASE
              WHEN p_factures_reference_tiers.nb_non_calcules > 0 THEN p_factures_reference_tiers.montant_facture_1 + p_factures_reference_tiers.montant_encours_1
              ELSE 0::numeric
          END AS montant_non_calcules_1,
          CASE
              WHEN p_factures_reference_tiers.nb_non_calcules > 0 THEN p_factures_reference_tiers.montant_facture_1_c + p_factures_reference_tiers.montant_encours_1_c
              ELSE 0::numeric
          END AS montant_non_calcules_1_c,
          CASE
              WHEN p_factures_reference_tiers.nb_non_calcules > 0 THEN p_factures_reference_tiers.montant_facture_1_h + p_factures_reference_tiers.montant_encours_1_h
              ELSE 0::numeric
          END AS montant_non_calcules_1_h,
          CASE
              WHEN p_factures_reference_tiers.nb_non_calcules > 0 THEN p_factures_reference_tiers.montant_facture_2 + p_factures_reference_tiers.montant_encours_2
              ELSE 0::numeric
          END AS montant_non_calcules_2,
          CASE
              WHEN p_factures_reference_tiers.nb_non_calcules > 0 THEN p_factures_reference_tiers.montant_facture_2_c + p_factures_reference_tiers.montant_encours_2_c
              ELSE 0::numeric
          END AS montant_non_calcules_2_c,
          CASE
              WHEN p_factures_reference_tiers.nb_non_calcules > 0 THEN p_factures_reference_tiers.montant_facture_2_h + p_factures_reference_tiers.montant_encours_2_h
              ELSE 0::numeric
          END AS montant_non_calcules_2_h,
          CASE
              WHEN (p_factures_reference_tiers.type_t2a = '1'::bpchar AND p_factures_reference_tiers.ghs_id <> 0 OR p_factures_reference_tiers.type_t2a <> '1'::bpchar) AND p_factures_reference_tiers.nb_non_calcules > 0 THEN p_factures_reference_tiers.montant_facture + p_factures_reference_tiers.montant_encours
              ELSE 0::numeric
          END AS montant_non_calcules_groupes,
          CASE
              WHEN (p_factures_reference_tiers.type_t2a = '1'::bpchar AND p_factures_reference_tiers.ghs_id <> 0 OR p_factures_reference_tiers.type_t2a <> '1'::bpchar) AND p_factures_reference_tiers.nb_non_calcules > 0 THEN p_factures_reference_tiers.montant_facture_c + p_factures_reference_tiers.montant_encours_c
              ELSE 0::numeric
          END AS montant_non_calcules_groupes_c,
          CASE
              WHEN (p_factures_reference_tiers.type_t2a = '1'::bpchar AND p_factures_reference_tiers.ghs_id <> 0 OR p_factures_reference_tiers.type_t2a <> '1'::bpchar) AND p_factures_reference_tiers.nb_non_calcules > 0 THEN p_factures_reference_tiers.montant_facture_h + p_factures_reference_tiers.montant_encours_h
              ELSE 0::numeric
          END AS montant_non_calcules_groupes_h,
          CASE
              WHEN (p_factures_reference_tiers.type_t2a = '1'::bpchar AND p_factures_reference_tiers.ghs_id <> 0 OR p_factures_reference_tiers.type_t2a <> '1'::bpchar) AND p_factures_reference_tiers.nb_non_calcules > 0 THEN p_factures_reference_tiers.montant_facture + p_factures_reference_tiers.montant_encours_0
              ELSE 0::numeric
          END AS montant_non_calcules_groupes_0,
          CASE
              WHEN (p_factures_reference_tiers.type_t2a = '1'::bpchar AND p_factures_reference_tiers.ghs_id <> 0 OR p_factures_reference_tiers.type_t2a <> '1'::bpchar) AND p_factures_reference_tiers.nb_non_calcules > 0 THEN p_factures_reference_tiers.montant_facture_c + p_factures_reference_tiers.montant_encours_0_c
              ELSE 0::numeric
          END AS montant_non_calcules_groupes_0_c,
          CASE
              WHEN (p_factures_reference_tiers.type_t2a = '1'::bpchar AND p_factures_reference_tiers.ghs_id <> 0 OR p_factures_reference_tiers.type_t2a <> '1'::bpchar) AND p_factures_reference_tiers.nb_non_calcules > 0 THEN p_factures_reference_tiers.montant_facture_h + p_factures_reference_tiers.montant_encours_0_h
              ELSE 0::numeric
          END AS montant_non_calcules_groupes_0_h,
          CASE
              WHEN (p_factures_reference_tiers.type_t2a = '1'::bpchar AND p_factures_reference_tiers.ghs_id <> 0 OR p_factures_reference_tiers.type_t2a <> '1'::bpchar) AND p_factures_reference_tiers.nb_non_calcules > 0 THEN p_factures_reference_tiers.montant_facture + p_factures_reference_tiers.montant_encours_1
              ELSE 0::numeric
          END AS montant_non_calcules_groupes_1,
          CASE
              WHEN (p_factures_reference_tiers.type_t2a = '1'::bpchar AND p_factures_reference_tiers.ghs_id <> 0 OR p_factures_reference_tiers.type_t2a <> '1'::bpchar) AND p_factures_reference_tiers.nb_non_calcules > 0 THEN p_factures_reference_tiers.montant_facture_c + p_factures_reference_tiers.montant_encours_1_c
              ELSE 0::numeric
          END AS montant_non_calcules_groupes_1_c,
          CASE
              WHEN (p_factures_reference_tiers.type_t2a = '1'::bpchar AND p_factures_reference_tiers.ghs_id <> 0 OR p_factures_reference_tiers.type_t2a <> '1'::bpchar) AND p_factures_reference_tiers.nb_non_calcules > 0 THEN p_factures_reference_tiers.montant_facture_h + p_factures_reference_tiers.montant_encours_1_h
              ELSE 0::numeric
          END AS montant_non_calcules_groupes_1_h,
          CASE
              WHEN (p_factures_reference_tiers.type_t2a = '1'::bpchar AND p_factures_reference_tiers.ghs_id <> 0 OR p_factures_reference_tiers.type_t2a <> '1'::bpchar) AND p_factures_reference_tiers.nb_non_calcules > 0 THEN p_factures_reference_tiers.montant_facture + p_factures_reference_tiers.montant_encours_2
              ELSE 0::numeric
          END AS montant_non_calcules_groupes_2,
          CASE
              WHEN (p_factures_reference_tiers.type_t2a = '1'::bpchar AND p_factures_reference_tiers.ghs_id <> 0 OR p_factures_reference_tiers.type_t2a <> '1'::bpchar) AND p_factures_reference_tiers.nb_non_calcules > 0 THEN p_factures_reference_tiers.montant_facture_c + p_factures_reference_tiers.montant_encours_2_c
              ELSE 0::numeric
          END AS montant_non_calcules_groupes_2_c,
          CASE
              WHEN (p_factures_reference_tiers.type_t2a = '1'::bpchar AND p_factures_reference_tiers.ghs_id <> 0 OR p_factures_reference_tiers.type_t2a <> '1'::bpchar) AND p_factures_reference_tiers.nb_non_calcules > 0 THEN p_factures_reference_tiers.montant_facture_h + p_factures_reference_tiers.montant_encours_2_h
              ELSE 0::numeric
          END AS montant_non_calcules_groupes_2_h,
          CASE
              WHEN p_factures_reference_tiers.type_t2a = '1'::bpchar AND p_factures_reference_tiers.ghs_id <> 0 OR p_factures_reference_tiers.type_t2a <> '1'::bpchar THEN 0::numeric
              ELSE
              CASE
                  WHEN p_factures_reference_tiers.nb_non_calcules > 0 THEN p_factures_reference_tiers.montant_facture + p_factures_reference_tiers.montant_encours
                  ELSE 0::numeric
              END
          END AS montant_non_calcules_non_groupes,
          CASE
              WHEN p_factures_reference_tiers.type_t2a = '1'::bpchar AND p_factures_reference_tiers.ghs_id <> 0 OR p_factures_reference_tiers.type_t2a <> '1'::bpchar THEN 0::numeric
              ELSE
              CASE
                  WHEN p_factures_reference_tiers.nb_non_calcules > 0 THEN p_factures_reference_tiers.montant_facture_c + p_factures_reference_tiers.montant_encours_c
                  ELSE 0::numeric
              END
          END AS montant_non_calcules_non_groupes_c,
          CASE
              WHEN p_factures_reference_tiers.type_t2a = '1'::bpchar AND p_factures_reference_tiers.ghs_id <> 0 OR p_factures_reference_tiers.type_t2a <> '1'::bpchar THEN 0::numeric
              ELSE
              CASE
                  WHEN p_factures_reference_tiers.nb_non_calcules > 0 THEN p_factures_reference_tiers.montant_facture_h + p_factures_reference_tiers.montant_encours_h
                  ELSE 0::numeric
              END
          END AS montant_non_calcules_non_groupes_h,
          CASE
              WHEN p_factures_reference_tiers.type_t2a = '1'::bpchar AND p_factures_reference_tiers.ghs_id <> 0 OR p_factures_reference_tiers.type_t2a <> '1'::bpchar THEN 0::numeric
              ELSE
              CASE
                  WHEN p_factures_reference_tiers.nb_non_calcules > 0 THEN p_factures_reference_tiers.montant_facture_0 + p_factures_reference_tiers.montant_encours_0
                  ELSE 0::numeric
              END
          END AS montant_non_calcules_non_groupes_0,
          CASE
              WHEN p_factures_reference_tiers.type_t2a = '1'::bpchar AND p_factures_reference_tiers.ghs_id <> 0 OR p_factures_reference_tiers.type_t2a <> '1'::bpchar THEN 0::numeric
              ELSE
              CASE
                  WHEN p_factures_reference_tiers.nb_non_calcules > 0 THEN p_factures_reference_tiers.montant_facture_0_c + p_factures_reference_tiers.montant_encours_0_c
                  ELSE 0::numeric
              END
          END AS montant_non_calcules_non_groupes_0_c,
          CASE
              WHEN p_factures_reference_tiers.type_t2a = '1'::bpchar AND p_factures_reference_tiers.ghs_id <> 0 OR p_factures_reference_tiers.type_t2a <> '1'::bpchar THEN 0::numeric
              ELSE
              CASE
                  WHEN p_factures_reference_tiers.nb_non_calcules > 0 THEN p_factures_reference_tiers.montant_facture_0_h + p_factures_reference_tiers.montant_encours_0_h
                  ELSE 0::numeric
              END
          END AS montant_non_calcules_non_groupes_0_h,
          CASE
              WHEN p_factures_reference_tiers.type_t2a = '1'::bpchar AND p_factures_reference_tiers.ghs_id <> 0 OR p_factures_reference_tiers.type_t2a <> '1'::bpchar THEN 0::numeric
              ELSE
              CASE
                  WHEN p_factures_reference_tiers.nb_non_calcules > 0 THEN p_factures_reference_tiers.montant_facture_1 + p_factures_reference_tiers.montant_encours_1
                  ELSE 0::numeric
              END
          END AS montant_non_calcules_non_groupes_1,
          CASE
              WHEN p_factures_reference_tiers.type_t2a = '1'::bpchar AND p_factures_reference_tiers.ghs_id <> 0 OR p_factures_reference_tiers.type_t2a <> '1'::bpchar THEN 0::numeric
              ELSE
              CASE
                  WHEN p_factures_reference_tiers.nb_non_calcules > 0 THEN p_factures_reference_tiers.montant_facture_1_c + p_factures_reference_tiers.montant_encours_1_c
                  ELSE 0::numeric
              END
          END AS montant_non_calcules_non_groupes_1_c,
          CASE
              WHEN p_factures_reference_tiers.type_t2a = '1'::bpchar AND p_factures_reference_tiers.ghs_id <> 0 OR p_factures_reference_tiers.type_t2a <> '1'::bpchar THEN 0::numeric
              ELSE
              CASE
                  WHEN p_factures_reference_tiers.nb_non_calcules > 0 THEN p_factures_reference_tiers.montant_facture_1_h + p_factures_reference_tiers.montant_encours_1_h
                  ELSE 0::numeric
              END
          END AS montant_non_calcules_non_groupes_1_h,
          CASE
              WHEN p_factures_reference_tiers.type_t2a = '1'::bpchar AND p_factures_reference_tiers.ghs_id <> 0 OR p_factures_reference_tiers.type_t2a <> '1'::bpchar THEN 0::numeric
              ELSE
              CASE
                  WHEN p_factures_reference_tiers.nb_non_calcules > 0 THEN p_factures_reference_tiers.montant_facture_2 + p_factures_reference_tiers.montant_encours_2
                  ELSE 0::numeric
              END
          END AS montant_non_calcules_non_groupes_2,
          CASE
              WHEN p_factures_reference_tiers.type_t2a = '1'::bpchar AND p_factures_reference_tiers.ghs_id <> 0 OR p_factures_reference_tiers.type_t2a <> '1'::bpchar THEN 0::numeric
              ELSE
              CASE
                  WHEN p_factures_reference_tiers.nb_non_calcules > 0 THEN p_factures_reference_tiers.montant_facture_2_c + p_factures_reference_tiers.montant_encours_2_c
                  ELSE 0::numeric
              END
          END AS montant_non_calcules_non_groupes_2_c,
          CASE
              WHEN p_factures_reference_tiers.type_t2a = '1'::bpchar AND p_factures_reference_tiers.ghs_id <> 0 OR p_factures_reference_tiers.type_t2a <> '1'::bpchar THEN 0::numeric
              ELSE
              CASE
                  WHEN p_factures_reference_tiers.nb_non_calcules > 0 THEN p_factures_reference_tiers.montant_facture_2_h + p_factures_reference_tiers.montant_encours_2_h
                  ELSE 0::numeric
              END
          END AS montant_non_calcules_non_groupes_2_h,
      p_factures_reference_tiers.montant_comptabilise,
      p_factures_reference_tiers.montant_comptabilise_c,
      p_factures_reference_tiers.montant_comptabilise_h,
      p_factures_reference_tiers.montant_comptabilise_0,
      p_factures_reference_tiers.montant_comptabilise_0_c,
      p_factures_reference_tiers.montant_comptabilise_0_h,
      p_factures_reference_tiers.montant_comptabilise_1,
      p_factures_reference_tiers.montant_comptabilise_1_c,
      p_factures_reference_tiers.montant_comptabilise_1_h,
      p_factures_reference_tiers.montant_comptabilise_2,
      p_factures_reference_tiers.montant_comptabilise_2_c,
      p_factures_reference_tiers.montant_comptabilise_2_h,
      p_factures_reference_tiers.montant_comptabilise_budget_global_c,
      p_factures_reference_tiers.montant_regle,
      p_factures_reference_tiers.montant_regle_c,
      p_factures_reference_tiers.montant_regle_h,
      p_factures_reference_tiers.montant_regle_0,
      p_factures_reference_tiers.montant_regle_0_c,
      p_factures_reference_tiers.montant_regle_0_h,
      p_factures_reference_tiers.montant_regle_1,
      p_factures_reference_tiers.montant_regle_1_c,
      p_factures_reference_tiers.montant_regle_1_h,
      p_factures_reference_tiers.montant_regle_2,
      p_factures_reference_tiers.montant_regle_2_c,
      p_factures_reference_tiers.montant_regle_2_h,
      p_factures_reference_tiers.date_solde,
      p_factures_reference_tiers.date_solde_c,
      p_factures_reference_tiers.date_solde_h,
      p_factures_reference_tiers.date_solde_0,
      p_factures_reference_tiers.date_solde_0_c,
      p_factures_reference_tiers.date_solde_0_h,
      p_factures_reference_tiers.date_solde_1,
      p_factures_reference_tiers.date_solde_1_c,
      p_factures_reference_tiers.date_solde_1_h,
      p_factures_reference_tiers.date_solde_2,
      p_factures_reference_tiers.date_solde_2_c,
      p_factures_reference_tiers.date_solde_2_h,
      p_factures_reference_tiers.delai_solde,
      p_factures_reference_tiers.delai_solde_c,
      p_factures_reference_tiers.delai_solde_h,
      p_factures_reference_tiers.delai_solde_0,
      p_factures_reference_tiers.delai_solde_0_c,
      p_factures_reference_tiers.delai_solde_0_h,
      p_factures_reference_tiers.delai_solde_1,
      p_factures_reference_tiers.delai_solde_1_c,
      p_factures_reference_tiers.delai_solde_1_h,
      p_factures_reference_tiers.delai_solde_2,
      p_factures_reference_tiers.delai_solde_2_c,
      p_factures_reference_tiers.delai_solde_2_h,
      p_factures_reference_tiers.nb_non_solde,
      p_factures_reference_tiers.nb_non_solde_c,
      p_factures_reference_tiers.nb_non_solde_h,
      p_factures_reference_tiers.nb_non_solde_0,
      p_factures_reference_tiers.nb_non_solde_0_c,
      p_factures_reference_tiers.nb_non_solde_0_h,
      p_factures_reference_tiers.nb_non_solde_1,
      p_factures_reference_tiers.nb_non_solde_1_c,
      p_factures_reference_tiers.nb_non_solde_1_h,
      p_factures_reference_tiers.nb_non_solde_2,
      p_factures_reference_tiers.nb_non_solde_2_c,
      p_factures_reference_tiers.nb_non_solde_2_h,
      p_factures_reference_tiers.montant_comptabilise - p_factures_reference_tiers.montant_regle AS montant_solde,
      p_factures_reference_tiers.montant_comptabilise_c - p_factures_reference_tiers.montant_regle_c AS montant_solde_c,
      p_factures_reference_tiers.montant_comptabilise_h - p_factures_reference_tiers.montant_regle_h AS montant_solde_h,
      p_factures_reference_tiers.montant_comptabilise_0 - p_factures_reference_tiers.montant_regle_0 AS montant_solde_0,
      p_factures_reference_tiers.montant_comptabilise_0_c - p_factures_reference_tiers.montant_regle_0_c AS montant_solde_0_c,
      p_factures_reference_tiers.montant_comptabilise_0_h - p_factures_reference_tiers.montant_regle_0_h AS montant_solde_0_h,
      p_factures_reference_tiers.montant_comptabilise_1 - p_factures_reference_tiers.montant_regle_1 AS montant_solde_1,
      p_factures_reference_tiers.montant_comptabilise_1_c - p_factures_reference_tiers.montant_regle_1_c AS montant_solde_1_c,
      p_factures_reference_tiers.montant_comptabilise_1_h - p_factures_reference_tiers.montant_regle_1_h AS montant_solde_1_h,
      p_factures_reference_tiers.montant_comptabilise_2 - p_factures_reference_tiers.montant_regle_2 AS montant_solde_2,
      p_factures_reference_tiers.montant_comptabilise_2_c - p_factures_reference_tiers.montant_regle_2_c AS montant_solde_2_c,
      p_factures_reference_tiers.montant_comptabilise_2_h - p_factures_reference_tiers.montant_regle_2_h AS montant_solde_2_h,
      p_factures_reference_tiers.nb_non_cloture,
      p_factures_reference_tiers.nb_non_cloture_c,
      p_factures_reference_tiers.nb_non_cloture_h,
      p_factures_reference_tiers.nb_non_cloture_0,
      p_factures_reference_tiers.nb_non_cloture_0_c,
      p_factures_reference_tiers.nb_non_cloture_0_h,
      p_factures_reference_tiers.nb_non_cloture_1,
      p_factures_reference_tiers.nb_non_cloture_1_c,
      p_factures_reference_tiers.nb_non_cloture_1_h,
      p_factures_reference_tiers.nb_non_cloture_2,
      p_factures_reference_tiers.nb_non_cloture_2_c,
      p_factures_reference_tiers.nb_non_cloture_2_h,
      p_factures_reference_tiers.motif_non_facturation_id,
      p_factures_reference_tiers.motif_non_facturation_comment,
      p_factures_reference_tiers.nb_non_calcules_bg,
      p_factures_reference_tiers.nb_non_calcules_horsbg,
      p_factures_reference_tiers.pole_sortie_id AS pole_id
     FROM activite.p_factures_reference_tiers
       JOIN activite.t_tiers_payant ON p_factures_reference_tiers.tiers_payant_id = t_tiers_payant.oid;