src: |
  SELECT p_factures.finess,
      p_factures.no_sejour,
      p_factures.sejour_id,
      p_factures.no_facture,
      p_factures.oid AS facture_id,
      p_factures.no_facture_reference,
      p_factures.facture_reference_id,
      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_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_references.date_facture <> '2099-12-31'::date THEN 1
              ELSE 0
          END AS nb_delai_facture,
          CASE
              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,
      p_factures.ghs_bebe1_id,
      p_factures.ghs_bebe2_id,
      p_factures.ghs_bebe3_id,
      1 AS nb_factures,
          CASE
              WHEN p_factures.type_facture = '0'::bpchar THEN 0
              WHEN p_factures.type_facture = 'X'::bpchar THEN 0
              ELSE 1
          END AS nb_factures_regularisation,
      p_factures.nb_rejets AS nb_factures_rejet,
      t_types_tiers_payant_0.code AS type_tiers_payant_0_code,
      t_types_tiers_payant_0.texte AS type_tiers_payant_0_texte,
      t_tiers_payant_0.oid AS tiers_payant_0_id,
      t_tiers_payant_0.code AS tiers_payant_0_code,
      t_tiers_payant_0.texte_court AS tiers_payant_0_texte,
      t_types_tiers_payant_1.code AS type_tiers_payant_1_code,
      t_types_tiers_payant_1.texte AS type_tiers_payant_1_texte,
      t_tiers_payant_1.oid AS tiers_payant_1_id,
      t_tiers_payant_1.code AS tiers_payant_1_code,
      t_tiers_payant_1.texte_court AS tiers_payant_1_texte,
      t_types_tiers_payant_2.code AS type_tiers_payant_2_code,
      t_types_tiers_payant_2.texte AS type_tiers_payant_2_texte,
      t_tiers_payant_2.oid AS tiers_payant_2_id,
      t_tiers_payant_2.code AS tiers_payant_2_code,
      t_tiers_payant_2.texte_court AS tiers_payant_2_texte,
      t_types_tiers_payant_22.code AS type_tiers_payant_22_code,
      t_types_tiers_payant_22.texte AS type_tiers_payant_22_texte,
      t_tiers_payant_22.oid AS tiers_payant_22_id,
      t_tiers_payant_22.code AS tiers_payant_22_code,
      t_tiers_payant_22.texte_court AS tiers_payant_22_texte,
      p_factures.montant_facture_c + p_factures.montant_facture_h AS montant_facture,
      p_factures.montant_facture_c,
      p_factures.montant_facture_h,
      p_factures.montant_facture_0_c + p_factures.montant_facture_0_c AS montant_facture_0,
      p_factures.montant_facture_0_c,
      p_factures.montant_facture_0_h,
      p_factures.montant_facture_1_c + p_factures.montant_facture_1_c AS montant_facture_1,
      p_factures.montant_facture_1_c,
      p_factures.montant_facture_1_h,
      p_factures.montant_facture_2_c + p_factures.montant_facture_2_c AS montant_facture_2,
      p_factures.montant_facture_2_c,
      p_factures.montant_facture_2_h,
      p_factures.montant_facture_22_c + p_factures.montant_facture_22_c AS montant_facture_22,
      p_factures.montant_facture_22_c,
      p_factures.montant_facture_22_h,
      p_factures.montant_comptabilise_c + p_factures.montant_comptabilise_h AS montant_comptabilise,
      p_factures.montant_comptabilise_c,
      p_factures.montant_comptabilise_h,
      p_factures.montant_comptabilise_0_c + p_factures.montant_comptabilise_0_c AS montant_comptabilise_0,
      p_factures.montant_comptabilise_0_c,
      p_factures.montant_comptabilise_0_h,
      p_factures.montant_comptabilise_1_c + p_factures.montant_comptabilise_1_c AS montant_comptabilise_1,
      p_factures.montant_comptabilise_1_c,
      p_factures.montant_comptabilise_1_h,
      p_factures.montant_comptabilise_2_c + p_factures.montant_comptabilise_2_c AS montant_comptabilise_2,
      p_factures.montant_comptabilise_2_c,
      p_factures.montant_comptabilise_2_h,
      p_factures.montant_comptabilise_22_c + p_factures.montant_comptabilise_22_c AS montant_comptabilise_22,
      p_factures.montant_comptabilise_22_c,
      p_factures.montant_comptabilise_22_h,
      p_factures.montant_encours_c + p_factures.montant_encours_h AS montant_encours,
      p_factures.montant_encours_c,
      p_factures.montant_encours_h,
      p_factures.montant_encours_0_c + p_factures.montant_encours_0_c AS montant_encours_0,
      p_factures.montant_encours_0_c,
      p_factures.montant_encours_0_h,
      p_factures.montant_encours_1_c + p_factures.montant_encours_1_c AS montant_encours_1,
      p_factures.montant_encours_1_c,
      p_factures.montant_encours_1_h,
      p_factures.montant_encours_2_c + p_factures.montant_encours_2_c AS montant_encours_2,
      p_factures.montant_encours_2_c,
      p_factures.montant_encours_2_h,
      p_factures.montant_encours_22_c + p_factures.montant_encours_22_c AS montant_encours_22,
      p_factures.montant_encours_22_c,
      p_factures.montant_encours_22_h,
      p_factures.montant_regle_c + p_factures.montant_regle_h AS montant_regle,
      p_factures.montant_regle_c,
      p_factures.montant_regle_h,
      p_factures.montant_regle_0_c + p_factures.montant_regle_0_c AS montant_regle_0,
      p_factures.montant_regle_0_c,
      p_factures.montant_regle_0_h,
      p_factures.montant_regle_1_c + p_factures.montant_regle_1_c AS montant_regle_1,
      p_factures.montant_regle_1_c,
      p_factures.montant_regle_1_h,
      p_factures.montant_regle_2_c + p_factures.montant_regle_2_c AS montant_regle_2,
      p_factures.montant_regle_2_c,
      p_factures.montant_regle_2_h,
      p_factures.montant_regle_22_c + p_factures.montant_regle_22_c AS montant_regle_22,
      p_factures.montant_regle_22_c,
      p_factures.montant_regle_22_h,
      p_factures.montant_regle_c + p_factures.montant_regle_h - p_factures.montant_comptabilise_c - p_factures.montant_comptabilise_h AS montant_solde,
      p_factures.montant_regle_c - p_factures.montant_comptabilise_c AS montant_solde_c,
      p_factures.montant_regle_h - p_factures.montant_comptabilise_h AS montant_solde_h,
      p_factures.montant_regle_0_c + p_factures.montant_regle_0_c - p_factures.montant_comptabilise_0_c - p_factures.montant_comptabilise_0_h AS montant_solde_0,
      p_factures.montant_regle_0_c - p_factures.montant_comptabilise_0_c AS montant_solde_0_c,
      p_factures.montant_regle_0_h - p_factures.montant_comptabilise_0_h AS montant_solde_0_h,
      p_factures.montant_regle_1_c + p_factures.montant_regle_1_c - p_factures.montant_comptabilise_1_c - p_factures.montant_comptabilise_1_h AS montant_solde_1,
      p_factures.montant_regle_1_c - p_factures.montant_comptabilise_1_c AS montant_solde_1_c,
      p_factures.montant_regle_1_h - p_factures.montant_comptabilise_1_h AS montant_solde_1_h,
      p_factures.montant_regle_2_c + p_factures.montant_regle_2_c - p_factures.montant_comptabilise_2_c - p_factures.montant_comptabilise_2_h AS montant_solde_2,
      p_factures.montant_regle_2_c - p_factures.montant_comptabilise_2_c AS montant_solde_2_c,
      p_factures.montant_regle_2_h - p_factures.montant_comptabilise_2_h AS montant_solde_2_h,
      p_factures.montant_regle_22_c + p_factures.montant_regle_22_c - p_factures.montant_comptabilise_22_c - p_factures.montant_comptabilise_22_h AS montant_solde_22,
      p_factures.montant_regle_22_c - p_factures.montant_comptabilise_22_c AS montant_solde_22_c,
      p_factures.montant_regle_22_h - p_factures.montant_comptabilise_22_h AS montant_solde_22_h,
      p_factures.code_expedie_0,
      p_factures.code_expedie_1,
      p_factures.code_expedie_2,
      p_factures.code_expedie_22,
      p_factures.date_expedition_0,
      p_factures.date_expedition_1,
      p_factures.date_expedition_2,
      p_factures.date_expedition_22,
      p_factures.no_bordereau_0,
      p_factures.no_bordereau_1,
      p_factures.no_bordereau_2,
      p_factures.no_bordereau_22,
          CASE
              WHEN p_factures_references.date_expedition <> '2099-12-31'::date AND p_factures_references.date_expedition > p_factures_references.date_fin THEN (p_factures_references.date_expedition - p_factures_references.date_fin)::numeric
              ELSE 0::numeric
          END AS delai_expedition,
          CASE
              WHEN p_factures_references.date_expedition <> '2099-12-31'::date AND p_factures_references.date_expedition > p_factures_references.date_fin THEN 1::numeric
              ELSE 0::numeric
          END AS nb_delai_expedition,
          CASE
              WHEN p_factures_references.date_expedition_0 <> '2099-12-31'::date AND p_factures_references.date_expedition_0 > p_factures_references.date_fin THEN (p_factures_references.date_expedition_0 - p_factures_references.date_fin)::numeric
              ELSE 0::numeric
          END AS delai_expedition_0,
          CASE
              WHEN p_factures_references.date_expedition_0 <> '2099-12-31'::date AND p_factures_references.date_expedition_0 > p_factures_references.date_fin THEN 1::numeric
              ELSE 0::numeric
          END 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,
          CASE
              WHEN p_factures_references.date_expedition_2 <> '2099-12-31'::date AND p_factures_references.date_expedition_2 > p_factures_references.date_fin THEN (p_factures_references.date_expedition_2 - p_factures_references.date_fin)::numeric
              ELSE 0::numeric
          END AS delai_expedition_2,
          CASE
              WHEN p_factures_references.date_expedition_2 <> '2099-12-31'::date AND p_factures_references.date_expedition_2 > p_factures_references.date_fin THEN 1::numeric
              ELSE 0::numeric
          END AS nb_delai_expedition_2,
          CASE
              WHEN p_factures_references.date_expedition_22 <> '2099-12-31'::date AND p_factures_references.date_expedition_22 > p_factures_references.date_fin THEN (p_factures_references.date_expedition_22 - p_factures_references.date_fin)::numeric
              ELSE 0::numeric
          END AS delai_expedition_22,
          CASE
              WHEN p_factures_references.date_expedition_22 <> '2099-12-31'::date AND p_factures_references.date_expedition_22 > p_factures_references.date_fin THEN 1::numeric
              ELSE 0::numeric
          END AS nb_delai_expedition_22,
          CASE
              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 AND p_factures.montant_comptabilise_1_c <> p_factures.montant_regle_1_c OR p_factures.montant_facture_1_h <> 0::numeric AND p_factures.montant_comptabilise_1_h <> p_factures.montant_regle_1_h) THEN 1
              WHEN (p_factures.code_expedie_2 <> '1'::bpchar OR p_factures.code_expedie_2 IS NULL) AND (p_factures.montant_facture_2_c <> 0::numeric AND p_factures.montant_comptabilise_2_c <> p_factures.montant_regle_2_c OR p_factures.montant_facture_2_h <> 0::numeric AND p_factures.montant_comptabilise_2_h <> p_factures.montant_regle_2_h) THEN 1
              WHEN (p_factures.code_expedie_22 <> '1'::bpchar OR p_factures.code_expedie_22 IS NULL) AND (p_factures.montant_facture_22_c <> 0::numeric AND p_factures.montant_comptabilise_22_c <> p_factures.montant_regle_22_c OR p_factures.montant_facture_22_h <> 0::numeric AND p_factures.montant_comptabilise_22_h <> p_factures.montant_regle_22_h) THEN 1
              ELSE 0
          END AS nb_non_expedie,
          CASE
              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 AND p_factures.montant_comptabilise_1_c <> p_factures.montant_regle_1_c THEN 1
              WHEN (p_factures.code_expedie_2 <> '1'::bpchar OR p_factures.code_expedie_2 IS NULL) AND p_factures.montant_facture_2_c <> 0::numeric AND p_factures.montant_comptabilise_2_c <> p_factures.montant_regle_2_c THEN 1
              WHEN (p_factures.code_expedie_22 <> '1'::bpchar OR p_factures.code_expedie_22 IS NULL) AND p_factures.montant_facture_22_c <> 0::numeric AND p_factures.montant_comptabilise_22_c <> p_factures.montant_regle_22_c THEN 1
              ELSE 0
          END AS nb_non_expedie_c,
          CASE
              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 AND p_factures.montant_comptabilise_1_h <> p_factures.montant_regle_1_h THEN 1
              WHEN (p_factures.code_expedie_2 <> '1'::bpchar OR p_factures.code_expedie_2 IS NULL) AND p_factures.montant_facture_2_h <> 0::numeric AND p_factures.montant_comptabilise_2_h <> p_factures.montant_regle_2_h THEN 1
              WHEN (p_factures.code_expedie_22 <> '1'::bpchar OR p_factures.code_expedie_22 IS NULL) AND p_factures.montant_facture_22_h <> 0::numeric AND p_factures.montant_comptabilise_22_h <> p_factures.montant_regle_22_h 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.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.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.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,
          CASE
              WHEN (p_factures.code_expedie_2 <> '1'::bpchar OR p_factures.code_expedie_2 IS NULL) AND (p_factures.montant_facture_2_c <> 0::numeric OR p_factures.montant_facture_2_h <> 0::numeric) THEN 1
              ELSE 0
          END AS nb_non_expedie_2,
          CASE
              WHEN (p_factures.code_expedie_2 <> '1'::bpchar OR p_factures.code_expedie_2 IS NULL) AND p_factures.montant_facture_2_c <> 0::numeric THEN 1
              ELSE 0
          END AS nb_non_expedie_2_c,
          CASE
              WHEN (p_factures.code_expedie_2 <> '1'::bpchar OR p_factures.code_expedie_2 IS NULL) AND p_factures.montant_facture_2_h <> 0::numeric THEN 1
              ELSE 0
          END AS nb_non_expedie_2_h,
          CASE
              WHEN (p_factures.code_expedie_22 <> '1'::bpchar OR p_factures.code_expedie_22 IS NULL) AND (p_factures.montant_facture_22_c <> 0::numeric OR p_factures.montant_facture_22_h <> 0::numeric) THEN 1
              ELSE 0
          END AS nb_non_expedie_22,
          CASE
              WHEN (p_factures.code_expedie_22 <> '1'::bpchar OR p_factures.code_expedie_22 IS NULL) AND p_factures.montant_facture_22_c <> 0::numeric THEN 1
              ELSE 0
          END AS nb_non_expedie_22_c,
          CASE
              WHEN (p_factures.code_expedie_22 <> '1'::bpchar OR p_factures.code_expedie_22 IS NULL) AND p_factures.montant_facture_22_h <> 0::numeric THEN 1
              ELSE 0
          END AS nb_non_expedie_22_h,
          CASE
              WHEN p_factures.code_expedie_1 <> '1'::bpchar OR p_factures.code_expedie_1 IS NULL 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 +
          CASE
              WHEN p_factures.code_expedie_2 <> '1'::bpchar OR p_factures.code_expedie_2 IS NULL THEN p_factures.montant_comptabilise_2_c - p_factures.montant_regle_2_c + p_factures.montant_comptabilise_2_h - p_factures.montant_regle_2_h
              ELSE 0::numeric
          END +
          CASE
              WHEN p_factures.code_expedie_22 <> '1'::bpchar OR p_factures.code_expedie_22 IS NULL THEN p_factures.montant_comptabilise_22_c - p_factures.montant_regle_22_c + p_factures.montant_comptabilise_22_h - p_factures.montant_regle_22_h
              ELSE 0::numeric
          END AS montant_non_expedie,
          CASE
              WHEN p_factures.code_expedie_1 <> '1'::bpchar OR p_factures.code_expedie_1 IS NULL THEN p_factures.montant_comptabilise_1_c - p_factures.montant_regle_1_c
              ELSE 0::numeric
          END +
          CASE
              WHEN p_factures.code_expedie_2 <> '1'::bpchar OR p_factures.code_expedie_2 IS NULL THEN p_factures.montant_comptabilise_2_c - p_factures.montant_regle_2_c
              ELSE 0::numeric
          END +
          CASE
              WHEN p_factures.code_expedie_22 <> '1'::bpchar OR p_factures.code_expedie_22 IS NULL THEN p_factures.montant_comptabilise_22_c - p_factures.montant_regle_22_c
              ELSE 0::numeric
          END AS montant_non_expedie_c,
          CASE
              WHEN p_factures.code_expedie_1 <> '1'::bpchar OR p_factures.code_expedie_1 IS NULL THEN p_factures.montant_comptabilise_1_h - p_factures.montant_regle_1_h
              ELSE 0::numeric
          END +
          CASE
              WHEN p_factures.code_expedie_2 <> '1'::bpchar OR p_factures.code_expedie_2 IS NULL THEN p_factures.montant_comptabilise_2_h - p_factures.montant_regle_2_h
              ELSE 0::numeric
          END +
          CASE
              WHEN p_factures.code_expedie_22 <> '1'::bpchar OR p_factures.code_expedie_22 IS NULL THEN p_factures.montant_comptabilise_22_h - p_factures.montant_regle_22_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.code_expedie_1 <> '1'::bpchar OR p_factures.code_expedie_1 IS NULL 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.code_expedie_1 <> '1'::bpchar OR p_factures.code_expedie_1 IS NULL 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.code_expedie_1 <> '1'::bpchar OR p_factures.code_expedie_1 IS NULL THEN p_factures.montant_comptabilise_1_h - p_factures.montant_regle_1_h
              ELSE 0::numeric
          END AS montant_non_expedie_1_h,
          CASE
              WHEN p_factures.code_expedie_2 <> '1'::bpchar OR p_factures.code_expedie_2 IS NULL THEN p_factures.montant_comptabilise_2_c - p_factures.montant_regle_2_c + p_factures.montant_comptabilise_2_h - p_factures.montant_regle_2_h
              ELSE 0::numeric
          END AS montant_non_expedie_2,
          CASE
              WHEN p_factures.code_expedie_2 <> '1'::bpchar OR p_factures.code_expedie_2 IS NULL THEN p_factures.montant_comptabilise_2_c - p_factures.montant_regle_2_c
              ELSE 0::numeric
          END AS montant_non_expedie_2_c,
          CASE
              WHEN p_factures.code_expedie_2 <> '1'::bpchar OR p_factures.code_expedie_2 IS NULL THEN p_factures.montant_comptabilise_2_h - p_factures.montant_regle_2_h
              ELSE 0::numeric
          END AS montant_non_expedie_2_h,
          CASE
              WHEN p_factures.code_expedie_22 <> '1'::bpchar OR p_factures.code_expedie_22 IS NULL THEN p_factures.montant_comptabilise_22_c - p_factures.montant_regle_22_c + p_factures.montant_comptabilise_22_h - p_factures.montant_regle_22_h
              ELSE 0::numeric
          END AS montant_non_expedie_22,
          CASE
              WHEN p_factures.code_expedie_22 <> '1'::bpchar OR p_factures.code_expedie_22 IS NULL THEN p_factures.montant_comptabilise_22_c - p_factures.montant_regle_22_c
              ELSE 0::numeric
          END AS montant_non_expedie_22_c,
          CASE
              WHEN p_factures.code_expedie_22 <> '1'::bpchar OR p_factures.code_expedie_22 IS NULL THEN p_factures.montant_comptabilise_22_h - p_factures.montant_regle_22_h
              ELSE 0::numeric
          END AS montant_non_expedie_22_h,
      p_factures.date_solde_reference,
          CASE
              WHEN p_factures_references.date_solde_reference <> '2099-12-31'::date AND p_factures_references.date_solde_reference > p_factures_references.date_fin THEN (p_factures_references.date_solde_reference - p_factures_references.date_fin)::numeric
              ELSE 0::numeric
          END AS delai_solde_reference,
          CASE
              WHEN p_factures_references.date_solde_reference <> '2099-12-31'::date AND p_factures_references.date_solde_reference > 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_c <> '2099-12-31'::date AND p_factures_references.date_solde_reference_c > p_factures_references.date_fin THEN (p_factures_references.date_solde_reference_c - p_factures_references.date_fin)::numeric
              ELSE 0::numeric
          END AS delai_solde_reference_c,
          CASE
              WHEN p_factures_references.date_solde_reference_c <> '2099-12-31'::date AND p_factures_references.date_solde_reference_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_h <> '2099-12-31'::date AND p_factures_references.date_solde_reference_h > p_factures_references.date_fin THEN (p_factures_references.date_solde_reference_h - p_factures_references.date_fin)::numeric
              ELSE 0::numeric
          END AS delai_solde_reference_h,
          CASE
              WHEN p_factures_references.date_solde_reference_h <> '2099-12-31'::date AND p_factures_references.date_solde_reference_h > p_factures_references.date_fin THEN 1::numeric
              ELSE 0::numeric
          END AS nb_delai_solde_reference_h,
      GREATEST(p_factures_references.date_solde_reference_0_c, p_factures_references.date_solde_reference_0_h) AS date_solde_reference_0,
          CASE
              WHEN GREATEST(p_factures_references.date_solde_reference_0_c, p_factures_references.date_solde_reference_0_h) <> '2099-12-31'::date AND GREATEST(p_factures_references.date_solde_reference_0_c, p_factures_references.date_solde_reference_0_h) > p_factures_references.date_fin THEN (GREATEST(p_factures_references.date_solde_reference_0_c, p_factures_references.date_solde_reference_0_h) - p_factures_references.date_fin)::numeric
              ELSE 0::numeric
          END AS delai_solde_reference_0,
          CASE
              WHEN GREATEST(p_factures_references.date_solde_reference_0_c, p_factures_references.date_solde_reference_0_h) <> '2099-12-31'::date AND GREATEST(p_factures_references.date_solde_reference_0_c, p_factures_references.date_solde_reference_0_h) > p_factures_references.date_fin THEN 1::numeric
              ELSE 0::numeric
          END AS nb_delai_solde_reference_0,
      p_factures.date_solde_reference_0_c,
          CASE
              WHEN p_factures_references.date_solde_reference_0_c <> '2099-12-31'::date AND p_factures_references.date_solde_reference_0_c > p_factures_references.date_fin THEN (p_factures_references.date_solde_reference_0_c - p_factures_references.date_fin)::numeric
              ELSE 0::numeric
          END AS delai_solde_reference_0_c,
          CASE
              WHEN p_factures_references.date_solde_reference_0_c <> '2099-12-31'::date AND p_factures_references.date_solde_reference_0_c > p_factures_references.date_fin THEN 1::numeric
              ELSE 0::numeric
          END AS nb_delai_solde_reference_0_c,
      p_factures.date_solde_reference_0_h,
          CASE
              WHEN p_factures_references.date_solde_reference_0_h <> '2099-12-31'::date AND p_factures_references.date_solde_reference_0_h > p_factures_references.date_fin THEN (p_factures_references.date_solde_reference_0_h - p_factures_references.date_fin)::numeric
              ELSE 0::numeric
          END AS delai_solde_reference_0_h,
          CASE
              WHEN p_factures_references.date_solde_reference_0_h <> '2099-12-31'::date AND p_factures_references.date_solde_reference_0_h > p_factures_references.date_fin THEN 1::numeric
              ELSE 0::numeric
          END 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,
      GREATEST(p_factures_references.date_solde_reference_2_c, p_factures_references.date_solde_reference_2_h) AS date_solde_reference_2,
          CASE
              WHEN GREATEST(p_factures_references.date_solde_reference_2_c, p_factures_references.date_solde_reference_2_h) <> '2099-12-31'::date AND GREATEST(p_factures_references.date_solde_reference_2_c, p_factures_references.date_solde_reference_2_h) > p_factures_references.date_fin THEN (GREATEST(p_factures_references.date_solde_reference_2_c, p_factures_references.date_solde_reference_2_h) - p_factures_references.date_fin)::numeric
              ELSE 0::numeric
          END AS delai_solde_reference_2,
          CASE
              WHEN GREATEST(p_factures_references.date_solde_reference_2_c, p_factures_references.date_solde_reference_2_h) <> '2099-12-31'::date AND GREATEST(p_factures_references.date_solde_reference_2_c, p_factures_references.date_solde_reference_2_h) > p_factures_references.date_fin THEN 1::numeric
              ELSE 0::numeric
          END AS nb_delai_solde_reference_2,
      p_factures.date_solde_reference_2_c,
          CASE
              WHEN p_factures_references.date_solde_reference_2_c <> '2099-12-31'::date AND p_factures_references.date_solde_reference_2_c > p_factures_references.date_fin THEN (p_factures_references.date_solde_reference_2_c - p_factures_references.date_fin)::numeric
              ELSE 0::numeric
          END AS delai_solde_reference_2_c,
          CASE
              WHEN p_factures_references.date_solde_reference_2_c <> '2099-12-31'::date AND p_factures_references.date_solde_reference_2_c > p_factures_references.date_fin THEN 1::numeric
              ELSE 0::numeric
          END AS nb_delai_solde_reference_2_c,
      p_factures.date_solde_reference_2_h,
          CASE
              WHEN p_factures_references.date_solde_reference_2_h <> '2099-12-31'::date AND p_factures_references.date_solde_reference_2_h > p_factures_references.date_fin THEN (p_factures_references.date_solde_reference_2_h - p_factures_references.date_fin)::numeric
              ELSE 0::numeric
          END AS delai_solde_reference_2_h,
          CASE
              WHEN p_factures_references.date_solde_reference_2_h <> '2099-12-31'::date AND p_factures_references.date_solde_reference_2_h > p_factures_references.date_fin THEN 1::numeric
              ELSE 0::numeric
          END AS nb_delai_solde_reference_2_h,
      GREATEST(p_factures_references.date_solde_reference_22_c, p_factures_references.date_solde_reference_22_h) AS date_solde_reference_22,
          CASE
              WHEN GREATEST(p_factures_references.date_solde_reference_22_c, p_factures_references.date_solde_reference_22_h) <> '2099-12-31'::date AND GREATEST(p_factures_references.date_solde_reference_22_c, p_factures_references.date_solde_reference_22_h) > p_factures_references.date_fin THEN (GREATEST(p_factures_references.date_solde_reference_22_c, p_factures_references.date_solde_reference_22_h) - p_factures_references.date_fin)::numeric
              ELSE 0::numeric
          END AS delai_solde_reference_22,
          CASE
              WHEN GREATEST(p_factures_references.date_solde_reference_22_c, p_factures_references.date_solde_reference_22_h) <> '2099-12-31'::date AND GREATEST(p_factures_references.date_solde_reference_22_c, p_factures_references.date_solde_reference_22_h) > p_factures_references.date_fin THEN 1::numeric
              ELSE 0::numeric
          END AS nb_delai_solde_reference_22,
      p_factures.date_solde_reference_22_c,
          CASE
              WHEN p_factures_references.date_solde_reference_22_c <> '2099-12-31'::date AND p_factures_references.date_solde_reference_22_c > p_factures_references.date_fin THEN (p_factures_references.date_solde_reference_22_c - p_factures_references.date_fin)::numeric
              ELSE 0::numeric
          END AS delai_solde_reference_22_c,
          CASE
              WHEN p_factures_references.date_solde_reference_22_c <> '2099-12-31'::date AND p_factures_references.date_solde_reference_22_c > p_factures_references.date_fin THEN 1::numeric
              ELSE 0::numeric
          END AS nb_delai_solde_reference_22_c,
      p_factures.date_solde_reference_22_h,
          CASE
              WHEN p_factures_references.date_solde_reference_22_h <> '2099-12-31'::date AND p_factures_references.date_solde_reference_22_h > p_factures_references.date_fin THEN (p_factures_references.date_solde_reference_22_h - p_factures_references.date_fin)::numeric
              ELSE 0::numeric
          END AS delai_solde_reference_22_h,
          CASE
              WHEN p_factures_references.date_solde_reference_22_h <> '2099-12-31'::date AND p_factures_references.date_solde_reference_22_h > p_factures_references.date_fin THEN 1::numeric
              ELSE 0::numeric
          END AS nb_delai_solde_reference_22_h,
          CASE
              WHEN p_factures.montant_comptabilise_c <> p_factures.montant_regle_c THEN 1
              WHEN p_factures.montant_comptabilise_h <> p_factures.montant_regle_h THEN 1
              ELSE 0
          END AS nb_non_solde,
          CASE
              WHEN p_factures.montant_comptabilise_c <> p_factures.montant_regle_c THEN 1
              ELSE 0
          END AS nb_non_solde_c,
          CASE
              WHEN p_factures.montant_comptabilise_h <> p_factures.montant_regle_h THEN 1
              ELSE 0
          END AS nb_non_solde_h,
          CASE
              WHEN p_factures.montant_comptabilise_0_c <> p_factures.montant_regle_0_c THEN 1
              WHEN p_factures.montant_comptabilise_0_h <> p_factures.montant_regle_0_h THEN 1
              ELSE 0
          END AS nb_non_solde_0,
          CASE
              WHEN p_factures.montant_comptabilise_0_c <> p_factures.montant_regle_0_c THEN 1
              ELSE 0
          END AS nb_non_solde_0_c,
          CASE
              WHEN p_factures.montant_comptabilise_0_h <> p_factures.montant_regle_0_h THEN 1
              ELSE 0
          END AS nb_non_solde_0_h,
          CASE
              WHEN p_factures.montant_comptabilise_1_c <> p_factures.montant_regle_1_c THEN 1
              WHEN 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.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.montant_comptabilise_1_h <> p_factures.montant_regle_1_h THEN 1
              ELSE 0
          END AS nb_non_solde_1_h,
          CASE
              WHEN p_factures.montant_comptabilise_2_c <> p_factures.montant_regle_2_c THEN 1
              WHEN p_factures.montant_comptabilise_2_h <> p_factures.montant_regle_2_h THEN 1
              ELSE 0
          END AS nb_non_solde_2,
          CASE
              WHEN p_factures.montant_comptabilise_2_c <> p_factures.montant_regle_2_c THEN 1
              ELSE 0
          END AS nb_non_solde_2_c,
          CASE
              WHEN p_factures.montant_comptabilise_2_h <> p_factures.montant_regle_2_h THEN 1
              ELSE 0
          END AS nb_non_solde_2_h,
          CASE
              WHEN p_factures.montant_comptabilise_22_c <> p_factures.montant_regle_22_c THEN 1
              WHEN p_factures.montant_comptabilise_22_h <> p_factures.montant_regle_22_h THEN 1
              ELSE 0
          END AS nb_non_solde_22,
          CASE
              WHEN p_factures.montant_comptabilise_22_c <> p_factures.montant_regle_22_c THEN 1
              ELSE 0
          END AS nb_non_solde_22_c,
          CASE
              WHEN p_factures.montant_comptabilise_22_h <> p_factures.montant_regle_22_h THEN 1
              ELSE 0
          END AS nb_non_solde_22_h,
      p_factures.montant_comptabilise_c - p_factures.montant_regle_c + p_factures.montant_comptabilise_h - p_factures.montant_regle_h AS montant_non_solde,
      p_factures.montant_comptabilise_c - p_factures.montant_regle_c AS montant_non_solde_c,
      p_factures.montant_comptabilise_h - p_factures.montant_regle_h AS montant_non_solde_h
     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 t_tiers_payant_0 ON p_factures.tiers_payant_0_id = t_tiers_payant_0.oid
       JOIN activite.t_tiers_payant t_tiers_payant_1 ON p_factures.tiers_payant_1_id = t_tiers_payant_1.oid
       JOIN activite.t_tiers_payant t_tiers_payant_2 ON p_factures.tiers_payant_2_id = t_tiers_payant_2.oid
       JOIN activite.t_tiers_payant t_tiers_payant_22 ON p_factures.tiers_payant_22_id = t_tiers_payant_22.oid
       JOIN activite.t_types_tiers_payant t_types_tiers_payant_0 ON t_tiers_payant_0.type_tiers_payant = t_types_tiers_payant_0.code
       JOIN activite.t_types_tiers_payant t_types_tiers_payant_1 ON t_tiers_payant_1.type_tiers_payant = t_types_tiers_payant_1.code
       JOIN activite.t_types_tiers_payant t_types_tiers_payant_2 ON t_tiers_payant_2.type_tiers_payant = t_types_tiers_payant_2.code
       JOIN activite.t_types_tiers_payant t_types_tiers_payant_22 ON t_tiers_payant_22.type_tiers_payant = t_types_tiers_payant_22.code;