src: |
  SELECT p_factures.finess,
      p_factures.sejour_id,
      p_factures.no_sejour,
      p_factures.oid AS facture_id,
      p_factures.no_facture,
      p_factures.facture_reference_id,
      p_factures.no_facture_reference,
      p_factures_references.date_debut,
      p_factures_references.date_fin,
      p_factures.type_facture,
      p_factures.code_facture,
      p_factures.date_facture,
          CASE
              WHEN p_factures.type_facture = 'E'::bpchar THEN 0
              WHEN p_factures.type_facture = 'P'::bpchar THEN 0
              WHEN p_factures_references.date_facture <> '2099-12-31'::date THEN p_factures_references.date_facture - p_factures_references.date_fin
              ELSE 0
          END AS delai_facture,
          CASE
              WHEN p_factures.type_facture = 'E'::bpchar THEN 0
              WHEN p_factures.type_facture = 'P'::bpchar THEN 0
              WHEN p_factures_references.date_facture <> '2099-12-31'::date THEN 1
              ELSE 0
          END AS nb_delai_facture,
          CASE
              WHEN p_factures.type_facture = 'E'::bpchar THEN 1
              WHEN p_factures.type_facture = 'P'::bpchar THEN 0
              WHEN p_factures.date_facture = '2099-12-31'::date THEN 1
              ELSE 0
          END AS nb_non_facture,
      p_factures.code_vente,
      p_factures.date_vente,
      p_factures.mois_vente,
      p_factures.ghs_id,
      p_factures.particularite_t2a,
      p_factures.code_cloture,
      p_factures.code_cp_demandee,
      p_factures.date_encours,
      t_types_tiers_payant.code::bigint AS type_tiers_payant_id,
      t_types_tiers_payant.code AS type_tiers_payant_code,
      t_types_tiers_payant.texte AS type_tiers_payant_texte,
      t_tiers_payant.oid AS tiers_payant_id,
      t_tiers_payant.code AS tiers_payant_code,
      t_tiers_payant.texte_court AS 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,
      1 AS nb_factures,
          CASE
              WHEN p_factures.type_facture = '0'::bpchar THEN 0
              WHEN p_factures.type_facture = 'X'::bpchar THEN 0
              WHEN p_factures.type_facture = 'E'::bpchar THEN 0
              WHEN p_factures.type_facture = 'P'::bpchar THEN 0
              ELSE 1
          END AS nb_factures_regularisation,
      p_factures.nb_rejets AS nb_factures_rejet,
      p_factures.montant_facture_1_c + p_factures.montant_facture_1_h AS montant_facture,
      p_factures.montant_facture_1_c AS montant_facture_c,
      p_factures.montant_facture_1_h AS montant_facture_h,
      0 AS montant_facture_0,
      0 AS montant_facture_0_c,
      0 AS montant_facture_0_h,
      p_factures.montant_facture_1_c + p_factures.montant_facture_1_h AS montant_facture_1,
      p_factures.montant_facture_1_c,
      p_factures.montant_facture_1_h,
      0 AS montant_facture_2,
      0 AS montant_facture_2_c,
      0 AS montant_facture_2_h,
      p_factures.montant_comptabilise_1_c + p_factures.montant_comptabilise_1_h AS montant_comptabilise,
      p_factures.montant_comptabilise_1_c AS montant_comptabilise_c,
      p_factures.montant_comptabilise_1_h AS montant_comptabilise_h,
      0 AS montant_comptabilise_0,
      0 AS montant_comptabilise_0_c,
      0 AS montant_comptabilise_0_h,
      p_factures.montant_comptabilise_1_c + p_factures.montant_comptabilise_1_h AS montant_comptabilise_1,
      p_factures.montant_comptabilise_1_c,
      p_factures.montant_comptabilise_1_h,
      0 AS montant_comptabilise_2,
      0 AS montant_comptabilise_2_c,
      0 AS montant_comptabilise_2_h,
      p_factures.montant_encours_1_c + p_factures.montant_encours_1_h AS montant_encours,
      p_factures.montant_encours_1_c AS montant_encours_c,
      p_factures.montant_encours_1_h AS montant_encours_h,
      0 AS montant_encours_0,
      0 AS montant_encours_0_c,
      0 AS montant_encours_0_h,
      p_factures.montant_encours_1_c + p_factures.montant_encours_1_h AS montant_encours_1,
      p_factures.montant_encours_1_c,
      p_factures.montant_encours_1_h,
      0 AS montant_encours_2,
      0 AS montant_encours_2_c,
      0 AS montant_encours_2_h,
      p_factures.montant_regle_1_c + p_factures.montant_regle_1_h AS montant_regle,
      p_factures.montant_regle_1_c AS montant_regle_c,
      p_factures.montant_regle_1_h AS montant_regle_h,
      0 AS montant_regle_0,
      0 AS montant_regle_0_c,
      0 AS montant_regle_0_h,
      p_factures.montant_regle_1_c + p_factures.montant_regle_1_h AS montant_regle_1,
      p_factures.montant_regle_1_c,
      p_factures.montant_regle_1_h,
      0 AS montant_regle_2,
      0 AS montant_regle_2_c,
      0 AS montant_regle_2_h,
      p_factures.montant_comptabilise_1_c + p_factures.montant_comptabilise_1_h - p_factures.montant_regle_1_c - p_factures.montant_regle_1_h AS montant_solde,
      p_factures.montant_comptabilise_1_c - p_factures.montant_regle_1_c AS montant_solde_c,
      p_factures.montant_comptabilise_1_h - p_factures.montant_regle_1_h AS montant_solde_h,
      0 AS montant_solde_0,
      0 AS montant_solde_0_c,
      0 AS montant_solde_0_h,
      p_factures.montant_comptabilise_1_c + p_factures.montant_comptabilise_1_h - p_factures.montant_regle_1_c - p_factures.montant_regle_1_h AS montant_solde_1,
      p_factures.montant_comptabilise_1_c - p_factures.montant_regle_1_c AS montant_solde_1_c,
      p_factures.montant_comptabilise_1_h - p_factures.montant_regle_1_h AS montant_solde_1_h,
      0 AS montant_solde_2,
      0 AS montant_solde_2_c,
      0 AS montant_solde_2_h,
      p_factures.code_expedie_1 AS code_expedie,
      p_factures.date_expedition_1 AS date_expedition,
      p_factures.no_bordereau_1 AS no_bordereau,
          CASE
              WHEN p_factures_references.date_expedition_1 <> '2099-12-31'::date AND p_factures_references.date_expedition_1 > p_factures_references.date_fin THEN (p_factures_references.date_expedition_1 - p_factures_references.date_fin)::numeric
              ELSE 0::numeric
          END AS delai_expedition,
          CASE
              WHEN p_factures_references.date_expedition_1 <> '2099-12-31'::date AND p_factures_references.date_expedition_1 > p_factures_references.date_fin THEN 1::numeric
              ELSE 0::numeric
          END AS nb_delai_expedition,
      0 AS delai_expedition_0,
      0 AS nb_delai_expedition_0,
          CASE
              WHEN p_factures_references.date_expedition_1 <> '2099-12-31'::date AND p_factures_references.date_expedition_1 > p_factures_references.date_fin THEN (p_factures_references.date_expedition_1 - p_factures_references.date_fin)::numeric
              ELSE 0::numeric
          END AS delai_expedition_1,
          CASE
              WHEN p_factures_references.date_expedition_1 <> '2099-12-31'::date AND p_factures_references.date_expedition_1 > p_factures_references.date_fin THEN 1::numeric
              ELSE 0::numeric
          END AS nb_delai_expedition_1,
      0 AS delai_expedition_2,
      0 AS nb_delai_expedition_2,
          CASE
              WHEN p_factures.type_facture = 'X'::bpchar THEN 0
              WHEN p_factures.type_facture = '1'::bpchar THEN 0
              WHEN p_factures.type_facture = 'E'::bpchar THEN 0
              WHEN p_factures.type_facture = 'P'::bpchar THEN 0
              WHEN p_factures.montant_comptabilise_1_c = p_factures.montant_regle_1_c AND p_factures.montant_comptabilise_1_h = p_factures.montant_regle_1_h THEN 0
              WHEN (p_factures.code_expedie_1 <> '1'::bpchar OR p_factures.code_expedie_1 IS NULL) AND (p_factures.montant_facture_1_c > 0::numeric OR p_factures.montant_facture_1_h > 0::numeric) THEN 1
              ELSE 0
          END AS nb_non_expedie,
          CASE
              WHEN p_factures.type_facture = 'X'::bpchar THEN 0
              WHEN p_factures.type_facture = '1'::bpchar THEN 0
              WHEN p_factures.type_facture = 'E'::bpchar THEN 0
              WHEN p_factures.type_facture = 'P'::bpchar THEN 0
              WHEN p_factures.montant_comptabilise_1_c = p_factures.montant_regle_1_c THEN 0
              WHEN (p_factures.code_expedie_1 <> '1'::bpchar OR p_factures.code_expedie_1 IS NULL) AND p_factures.montant_facture_1_c > 0::numeric THEN 1
              ELSE 0
          END AS nb_non_expedie_c,
          CASE
              WHEN p_factures.type_facture = 'X'::bpchar THEN 0
              WHEN p_factures.type_facture = '1'::bpchar THEN 0
              WHEN p_factures.type_facture = 'E'::bpchar THEN 0
              WHEN p_factures.type_facture = 'P'::bpchar THEN 0
              WHEN p_factures.montant_comptabilise_1_h = p_factures.montant_regle_1_h THEN 0
              WHEN (p_factures.code_expedie_1 <> '1'::bpchar OR p_factures.code_expedie_1 IS NULL) AND p_factures.montant_facture_1_h > 0::numeric THEN 1
              ELSE 0
          END AS nb_non_expedie_h,
      0 AS nb_non_expedie_0,
      0 AS nb_non_expedie_0_c,
      0 AS nb_non_expedie_0_h,
          CASE
              WHEN p_factures.type_facture = 'X'::bpchar THEN 0
              WHEN p_factures.type_facture = '1'::bpchar THEN 0
              WHEN p_factures.type_facture = 'E'::bpchar THEN 0
              WHEN p_factures.type_facture = 'P'::bpchar THEN 0
              WHEN p_factures.montant_comptabilise_1_c = p_factures.montant_regle_1_c AND p_factures.montant_comptabilise_1_h = p_factures.montant_regle_1_h THEN 0
              WHEN (p_factures.code_expedie_1 <> '1'::bpchar OR p_factures.code_expedie_1 IS NULL) AND (p_factures.montant_facture_1_c > 0::numeric OR p_factures.montant_facture_1_h > 0::numeric) THEN 1
              ELSE 0
          END AS nb_non_expedie_1,
          CASE
              WHEN p_factures.type_facture = 'X'::bpchar THEN 0
              WHEN p_factures.type_facture = '1'::bpchar THEN 0
              WHEN p_factures.type_facture = 'E'::bpchar THEN 0
              WHEN p_factures.type_facture = 'P'::bpchar THEN 0
              WHEN p_factures.montant_comptabilise_1_c = p_factures.montant_regle_1_c THEN 0
              WHEN (p_factures.code_expedie_1 <> '1'::bpchar OR p_factures.code_expedie_1 IS NULL) AND p_factures.montant_facture_1_c > 0::numeric THEN 1
              ELSE 0
          END AS nb_non_expedie_1_c,
          CASE
              WHEN p_factures.type_facture = 'X'::bpchar THEN 0
              WHEN p_factures.type_facture = '1'::bpchar THEN 0
              WHEN p_factures.type_facture = 'E'::bpchar THEN 0
              WHEN p_factures.type_facture = 'P'::bpchar THEN 0
              WHEN p_factures.montant_comptabilise_1_h = p_factures.montant_regle_1_h THEN 0
              WHEN (p_factures.code_expedie_1 <> '1'::bpchar OR p_factures.code_expedie_1 IS NULL) AND p_factures.montant_facture_1_h > 0::numeric THEN 1
              ELSE 0
          END AS nb_non_expedie_1_h,
      0 AS nb_non_expedie_2,
      0 AS nb_non_expedie_2_c,
      0 AS nb_non_expedie_2_h,
          CASE
              WHEN p_factures.type_facture = 'X'::bpchar THEN 0::numeric
              WHEN p_factures.type_facture = '1'::bpchar THEN 0::numeric
              WHEN p_factures.type_facture = 'E'::bpchar THEN 0::numeric
              WHEN p_factures.type_facture = 'P'::bpchar THEN 0::numeric
              WHEN p_factures.code_expedie_1 <> '1'::bpchar OR p_factures.code_expedie_1 IS NULL AND (p_factures.montant_facture_1_c > 0::numeric OR p_factures.montant_facture_1_h > 0::numeric) THEN p_factures.montant_comptabilise_1_c - p_factures.montant_regle_1_c + p_factures.montant_comptabilise_1_h - p_factures.montant_regle_1_h
              ELSE 0::numeric
          END AS montant_non_expedie,
          CASE
              WHEN p_factures.type_facture = 'X'::bpchar THEN 0::numeric
              WHEN p_factures.type_facture = '1'::bpchar THEN 0::numeric
              WHEN p_factures.type_facture = 'E'::bpchar THEN 0::numeric
              WHEN p_factures.type_facture = 'P'::bpchar THEN 0::numeric
              WHEN p_factures.code_expedie_1 <> '1'::bpchar OR p_factures.code_expedie_1 IS NULL AND p_factures.montant_facture_1_c > 0::numeric THEN p_factures.montant_comptabilise_1_c - p_factures.montant_regle_1_c
              ELSE 0::numeric
          END AS montant_non_expedie_c,
          CASE
              WHEN p_factures.type_facture = 'X'::bpchar THEN 0::numeric
              WHEN p_factures.type_facture = '1'::bpchar THEN 0::numeric
              WHEN p_factures.type_facture = 'E'::bpchar THEN 0::numeric
              WHEN p_factures.type_facture = 'P'::bpchar THEN 0::numeric
              WHEN p_factures.code_expedie_1 <> '1'::bpchar OR p_factures.code_expedie_1 IS NULL AND p_factures.montant_facture_1_h > 0::numeric THEN p_factures.montant_comptabilise_1_h - p_factures.montant_regle_1_h
              ELSE 0::numeric
          END AS montant_non_expedie_h,
      0 AS montant_non_expedie_0,
      0 AS montant_non_expedie_0_c,
      0 AS montant_non_expedie_0_h,
          CASE
              WHEN p_factures.type_facture = 'X'::bpchar THEN 0::numeric
              WHEN p_factures.type_facture = '1'::bpchar THEN 0::numeric
              WHEN p_factures.type_facture = 'E'::bpchar THEN 0::numeric
              WHEN p_factures.type_facture = 'P'::bpchar THEN 0::numeric
              WHEN p_factures.code_expedie_1 <> '1'::bpchar OR p_factures.code_expedie_1 IS NULL AND (p_factures.montant_facture_1_c > 0::numeric OR p_factures.montant_facture_1_h > 0::numeric) THEN p_factures.montant_comptabilise_1_c - p_factures.montant_regle_1_c + p_factures.montant_comptabilise_1_h - p_factures.montant_regle_1_h
              ELSE 0::numeric
          END AS montant_non_expedie_1,
          CASE
              WHEN p_factures.type_facture = 'X'::bpchar THEN 0::numeric
              WHEN p_factures.type_facture = '1'::bpchar THEN 0::numeric
              WHEN p_factures.type_facture = 'E'::bpchar THEN 0::numeric
              WHEN p_factures.type_facture = 'P'::bpchar THEN 0::numeric
              WHEN p_factures.code_expedie_1 <> '1'::bpchar OR p_factures.code_expedie_1 IS NULL AND p_factures.montant_facture_1_c > 0::numeric THEN p_factures.montant_comptabilise_1_c - p_factures.montant_regle_1_c
              ELSE 0::numeric
          END AS montant_non_expedie_1_c,
          CASE
              WHEN p_factures.type_facture = 'X'::bpchar THEN 0::numeric
              WHEN p_factures.type_facture = '1'::bpchar THEN 0::numeric
              WHEN p_factures.type_facture = 'E'::bpchar THEN 0::numeric
              WHEN p_factures.type_facture = 'P'::bpchar THEN 0::numeric
              WHEN p_factures.code_expedie_1 <> '1'::bpchar OR p_factures.code_expedie_1 IS NULL AND p_factures.montant_facture_1_h > 0::numeric THEN p_factures.montant_comptabilise_1_h - p_factures.montant_regle_1_h
              ELSE 0::numeric
          END AS montant_non_expedie_1_h,
      0 AS montant_non_expedie_2,
      0 AS montant_non_expedie_2_c,
      0 AS montant_non_expedie_2_h,
      GREATEST(p_factures_references.date_solde_reference_1_c, p_factures_references.date_solde_reference_1_h) AS date_solde_reference,
          CASE
              WHEN GREATEST(p_factures_references.date_solde_reference_1_c, p_factures_references.date_solde_reference_1_h) <> '2099-12-31'::date AND GREATEST(p_factures_references.date_solde_reference_1_c, p_factures_references.date_solde_reference_1_h) > p_factures_references.date_fin THEN (GREATEST(p_factures_references.date_solde_reference_1_c, p_factures_references.date_solde_reference_1_h) - p_factures_references.date_fin)::numeric
              ELSE 0::numeric
          END AS delai_solde_reference,
          CASE
              WHEN GREATEST(p_factures_references.date_solde_reference_1_c, p_factures_references.date_solde_reference_1_h) <> '2099-12-31'::date AND GREATEST(p_factures_references.date_solde_reference_1_c, p_factures_references.date_solde_reference_1_h) > p_factures_references.date_fin THEN 1::numeric
              ELSE 0::numeric
          END AS nb_delai_solde_reference,
      p_factures.date_solde_reference_c,
          CASE
              WHEN p_factures_references.date_solde_reference_1_c <> '2099-12-31'::date AND p_factures_references.date_solde_reference_1_c > p_factures_references.date_fin THEN (p_factures_references.date_solde_reference_1_c - p_factures_references.date_fin)::numeric
              ELSE 0::numeric
          END AS delai_solde_reference_c,
          CASE
              WHEN p_factures_references.date_solde_reference_1_c <> '2099-12-31'::date AND p_factures_references.date_solde_reference_1_c > p_factures_references.date_fin THEN 1::numeric
              ELSE 0::numeric
          END AS nb_delai_solde_reference_c,
      p_factures.date_solde_reference_h,
          CASE
              WHEN p_factures_references.date_solde_reference_1_h <> '2099-12-31'::date AND p_factures_references.date_solde_reference_1_h > p_factures_references.date_fin THEN (p_factures_references.date_solde_reference_1_h - p_factures_references.date_fin)::numeric
              ELSE 0::numeric
          END AS delai_solde_reference_h,
          CASE
              WHEN p_factures_references.date_solde_reference_1_h <> '2099-12-31'::date AND p_factures_references.date_solde_reference_1_h > p_factures_references.date_fin THEN 1::numeric
              ELSE 0::numeric
          END AS nb_delai_solde_reference_h,
      NULL::date AS date_solde_reference_0,
      0 AS delai_solde_reference_0,
      0 AS nb_delai_solde_reference_0,
      NULL::date AS date_solde_reference_0_c,
      0 AS delai_solde_reference_0_c,
      0 AS nb_delai_solde_reference_0_c,
      NULL::date AS date_solde_reference_0_h,
      0 AS delai_solde_reference_0_h,
      0 AS nb_delai_solde_reference_0_h,
      GREATEST(p_factures_references.date_solde_reference_1_c, p_factures_references.date_solde_reference_1_h) AS date_solde_reference_1,
          CASE
              WHEN GREATEST(p_factures_references.date_solde_reference_1_c, p_factures_references.date_solde_reference_1_h) <> '2099-12-31'::date AND GREATEST(p_factures_references.date_solde_reference_1_c, p_factures_references.date_solde_reference_1_h) > p_factures_references.date_fin THEN (GREATEST(p_factures_references.date_solde_reference_1_c, p_factures_references.date_solde_reference_1_h) - p_factures_references.date_fin)::numeric
              ELSE 0::numeric
          END AS delai_solde_reference_1,
          CASE
              WHEN GREATEST(p_factures_references.date_solde_reference_1_c, p_factures_references.date_solde_reference_1_h) <> '2099-12-31'::date AND GREATEST(p_factures_references.date_solde_reference_1_c, p_factures_references.date_solde_reference_1_h) > p_factures_references.date_fin THEN 1::numeric
              ELSE 0::numeric
          END AS nb_delai_solde_reference_1,
      p_factures.date_solde_reference_1_c,
          CASE
              WHEN p_factures_references.date_solde_reference_1_c <> '2099-12-31'::date AND p_factures_references.date_solde_reference_1_c > p_factures_references.date_fin THEN (p_factures_references.date_solde_reference_1_c - p_factures_references.date_fin)::numeric
              ELSE 0::numeric
          END AS delai_solde_reference_1_c,
          CASE
              WHEN p_factures_references.date_solde_reference_1_c <> '2099-12-31'::date AND p_factures_references.date_solde_reference_1_c > p_factures_references.date_fin THEN 1::numeric
              ELSE 0::numeric
          END AS nb_delai_solde_reference_1_c,
      p_factures.date_solde_reference_1_h,
          CASE
              WHEN p_factures_references.date_solde_reference_1_h <> '2099-12-31'::date AND p_factures_references.date_solde_reference_1_h > p_factures_references.date_fin THEN (p_factures_references.date_solde_reference_1_h - p_factures_references.date_fin)::numeric
              ELSE 0::numeric
          END AS delai_solde_reference_1_h,
          CASE
              WHEN p_factures_references.date_solde_reference_1_h <> '2099-12-31'::date AND p_factures_references.date_solde_reference_1_h > p_factures_references.date_fin THEN 1::numeric
              ELSE 0::numeric
          END AS nb_delai_solde_reference_1_h,
      NULL::date AS date_solde_reference_2,
      0 AS delai_solde_reference_2,
      0 AS nb_delai_solde_reference_2,
      NULL::date AS date_solde_reference_2_c,
      0 AS delai_solde_reference_2_c,
      0 AS nb_delai_solde_reference_2_c,
      NULL::date AS date_solde_reference_2_h,
      0 AS delai_solde_reference_2_h,
      0 AS nb_delai_solde_reference_2_h,
          CASE
              WHEN p_factures.code_facture <> '1'::bpchar THEN 0
              WHEN p_factures.tiers_payant_1_id = t_tiers_payant.oid AND p_factures.montant_comptabilise_1_c <> p_factures.montant_regle_1_c THEN 1
              WHEN p_factures.tiers_payant_1_id = t_tiers_payant.oid AND p_factures.montant_comptabilise_1_h <> p_factures.montant_regle_1_h THEN 1
              ELSE 0
          END AS nb_non_solde,
          CASE
              WHEN p_factures.code_facture <> '1'::bpchar THEN 0
              WHEN p_factures.tiers_payant_1_id = t_tiers_payant.oid AND p_factures.montant_comptabilise_1_c <> p_factures.montant_regle_1_c THEN 1
              ELSE 0
          END AS nb_non_solde_c,
          CASE
              WHEN p_factures.code_facture <> '1'::bpchar THEN 0
              WHEN p_factures.tiers_payant_1_id = t_tiers_payant.oid AND p_factures.montant_comptabilise_1_h <> p_factures.montant_regle_1_h THEN 1
              ELSE 0
          END AS nb_non_solde_h,
      0 AS nb_non_solde_0,
      0 AS nb_non_solde_0_c,
      0 AS nb_non_solde_0_h,
          CASE
              WHEN p_factures.code_facture <> '1'::bpchar THEN 0
              WHEN p_factures.tiers_payant_1_id = t_tiers_payant.oid AND p_factures.montant_comptabilise_1_c <> p_factures.montant_regle_1_c THEN 1
              WHEN p_factures.tiers_payant_1_id = t_tiers_payant.oid AND p_factures.montant_comptabilise_1_h <> p_factures.montant_regle_1_h THEN 1
              ELSE 0
          END AS nb_non_solde_1,
          CASE
              WHEN p_factures.code_facture <> '1'::bpchar THEN 0
              WHEN p_factures.tiers_payant_1_id = t_tiers_payant.oid AND p_factures.montant_comptabilise_1_c <> p_factures.montant_regle_1_c THEN 1
              ELSE 0
          END AS nb_non_solde_1_c,
          CASE
              WHEN p_factures.code_facture <> '1'::bpchar THEN 0
              WHEN p_factures.tiers_payant_1_id = t_tiers_payant.oid AND p_factures.montant_comptabilise_1_h <> p_factures.montant_regle_1_h THEN 1
              ELSE 0
          END AS nb_non_solde_1_h,
      0 AS nb_non_solde_2,
      0 AS nb_non_solde_2_c,
      0 AS nb_non_solde_2_h,
      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
     FROM activite.p_factures
       JOIN activite.p_factures p_factures_references ON p_factures.no_facture_reference = p_factures_references.no_facture
       JOIN activite.t_tiers_payant ON t_tiers_payant.oid = p_factures.tiers_payant_1_id AND t_tiers_payant.oid <> 0 AND (p_factures.montant_encours_1_c <> 0::numeric OR p_factures.montant_facture_1_c <> 0::numeric OR p_factures.montant_facture_1_h <> 0::numeric OR p_factures.montant_regle_1_c <> 0::numeric OR p_factures.montant_regle_1_h <> 0::numeric OR p_factures.type_facture = 'E'::bpchar)
       JOIN activite.t_types_tiers_payant ON t_tiers_payant.type_tiers_payant = t_types_tiers_payant.code
    WHERE p_factures.type_facture <> 'P'::bpchar AND p_factures.type_facture <> 'G'::bpchar;