src: |
  SELECT p_historique_ecritures.compte_id AS compte_signe_id,
      t_comptes_c.compte_nonsigne_id AS compte_id,
      t_comptes_c.compte_nonsigne_numero AS compte_numero,
      t_comptes_c.texte AS compte_texte,
      t_comptes_c.section_id[1] AS compte_section1_id,
      t_comptes_c.section_code[1] AS compte_section1_code,
      t_comptes_c.section_texte[1] AS compte_section1_texte,
      t_comptes_c.section_id[2] AS compte_section2_id,
      t_comptes_c.section_code[2] AS compte_section2_code,
      t_comptes_c.section_texte[2] AS compte_section2_texte,
      t_comptes_c.section_id[3] AS compte_section3_id,
      t_comptes_c.section_code[3] AS compte_section3_code,
      t_comptes_c.section_texte[3] AS compte_section3_texte,
      t_comptes_c.section_id[4] AS compte_section4_id,
      t_comptes_c.section_code[4] AS compte_section4_code,
      t_comptes_c.section_texte[4] AS compte_section4_texte,
      t_comptes_c.section_id[5] AS compte_section5_id,
      t_comptes_c.section_code[5] AS compte_section5_code,
      t_comptes_c.section_texte[5] AS compte_section5_texte,
      t_comptes_c.section_id[6] AS compte_section6_id,
      t_comptes_c.section_code[6] AS compte_section6_code,
      t_comptes_c.section_texte[6] AS compte_section6_texte,
      t_comptes_c.section_id[7] AS compte_section7_id,
      t_comptes_c.section_code[7] AS compte_section7_code,
      t_comptes_c.section_texte[7] AS compte_section7_texte,
      t_comptes_c.section_id[8] AS compte_section8_id,
      t_comptes_c.section_code[8] AS compte_section8_code,
      t_comptes_c.section_texte[8] AS compte_section8_texte,
      t_comptes_c.section_id[9] AS compte_section9_id,
      t_comptes_c.section_code[9] AS compte_section9_code,
      t_comptes_c.section_texte[9] AS compte_section9_texte,
      t_comptes_c.classe_1_id,
      t_comptes_c.classe_1_code,
      t_comptes_c.classe_1_texte,
      t_comptes_c.classe_2_id,
      t_comptes_c.classe_2_code,
      t_comptes_c.classe_2_texte,
      t_comptes_c.classe_3_id,
      t_comptes_c.classe_3_code,
      t_comptes_c.classe_3_texte,
      t_comptes_c.classe_4_id,
      t_comptes_c.classe_4_code,
      t_comptes_c.classe_4_texte,
      t_comptes_c.classe_5_id,
      t_comptes_c.classe_5_code,
      t_comptes_c.classe_5_texte,
      t_comptes_c.classe_6_id,
      t_comptes_c.classe_6_code,
      t_comptes_c.classe_6_texte,
      t_comptes_c.arbre_classe_1_id,
      t_comptes_c.arbre_classe_1_code,
      t_comptes_c.arbre_classe_1_texte,
      t_comptes_c.arbre_classe_2_id,
      t_comptes_c.arbre_classe_2_code,
      t_comptes_c.arbre_classe_2_texte,
      t_comptes_c.arbre_classe_3_id,
      t_comptes_c.arbre_classe_3_code,
      t_comptes_c.arbre_classe_3_texte,
      t_comptes_c.arbre_classe_4_id,
      t_comptes_c.arbre_classe_4_code,
      t_comptes_c.arbre_classe_4_texte,
      t_comptes_c.arbre_classe_5_id,
      t_comptes_c.arbre_classe_5_code,
      t_comptes_c.arbre_classe_5_texte,
      t_comptes_c.arbre_classe_6_id,
      t_comptes_c.arbre_classe_6_code,
      t_comptes_c.arbre_classe_6_texte,
      t_comptes_c.arbre_classe_7_id,
      t_comptes_c.arbre_classe_7_code,
      t_comptes_c.arbre_classe_7_texte,
      t_comptes_c.banque AS compte_banque,
      p_historique_ecritures.mois_comptable,
      substr(to_char(p_historique_ecritures.mois_comptable, 'FM000000'::text), 5, 2)::bigint AS numero_mois_comptable_id,
      substr(to_char(p_historique_ecritures.mois_comptable, 'FM000000'::text), 5, 2) AS numero_mois_comptable_code,
          CASE substr(to_char(p_historique_ecritures.mois_comptable, 'FM000000'::text), 5, 2)
              WHEN '01'::text THEN 'Janvier'::text
              WHEN '02'::text THEN 'Février'::text
              WHEN '03'::text THEN 'Mars'::text
              WHEN '04'::text THEN 'Avril'::text
              WHEN '05'::text THEN 'Mai'::text
              WHEN '06'::text THEN 'Juin'::text
              WHEN '07'::text THEN 'Juillet'::text
              WHEN '08'::text THEN 'Aout'::text
              WHEN '09'::text THEN 'Septembre'::text
              WHEN '10'::text THEN 'Octobre'::text
              WHEN '11'::text THEN 'Novembre'::text
              WHEN '12'::text THEN 'Décembre'::text
              ELSE ''::text
          END AS numero_mois_comptable_texte,
      p_historique_ecritures.journal_id,
      t_journaux.code AS journal_code,
      t_journaux.texte AS journal_texte,
      t_journaux.a_nouveaux AS journal_a_nouveaux,
      t_journaux.section1_id AS journal_section1_id,
      t_journaux.section1_code AS journal_section1_code,
      t_journaux.section1_texte AS journal_section1_texte,
      t_journaux.section2_id AS journal_section2_id,
      t_journaux.section2_code AS journal_section2_code,
      t_journaux.section2_texte AS journal_section2_texte,
      t_journaux.section3_id AS journal_section3_id,
      t_journaux.section3_code AS journal_section3_code,
      t_journaux.section3_texte AS journal_section3_texte,
      p_historique_ecritures.section_analytique_id,
      t_sections_analytiques.code AS section_analytique_code,
      t_sections_analytiques.texte AS section_analytique_texte,
      t_sections_analytiques.section1_id AS section_analytique_section1_id,
      t_sections_analytiques.section1_code AS section_analytique_section1_code,
      t_sections_analytiques.section1_texte AS section_analytique_section1_texte,
      t_sections_analytiques.section2_id AS section_analytique_section2_id,
      t_sections_analytiques.section2_code AS section_analytique_section2_code,
      t_sections_analytiques.section2_texte AS section_analytique_section2_texte,
      t_sections_analytiques.section3_id AS section_analytique_section3_id,
      t_sections_analytiques.section3_code AS section_analytique_section3_code,
      t_sections_analytiques.section3_texte AS section_analytique_section3_texte,
      t_sections_analytiques.section4_id AS section_analytique_section4_id,
      t_sections_analytiques.section4_code AS section_analytique_section4_code,
      t_sections_analytiques.section4_texte AS section_analytique_section4_texte,
      t_sections_analytiques.section5_id AS section_analytique_section5_id,
      t_sections_analytiques.section5_code AS section_analytique_section5_code,
      t_sections_analytiques.section5_texte AS section_analytique_section5_texte,
      t_sections_analytiques.section6_id AS section_analytique_section6_id,
      t_sections_analytiques.section6_code AS section_analytique_section6_code,
      t_sections_analytiques.section6_texte AS section_analytique_section6_texte,
      t_sections_analytiques.section7_id AS section_analytique_section7_id,
      t_sections_analytiques.section7_code AS section_analytique_section7_code,
      t_sections_analytiques.section7_texte AS section_analytique_section7_texte,
      t_sections_analytiques.section8_id AS section_analytique_section8_id,
      t_sections_analytiques.section8_code AS section_analytique_section8_code,
      t_sections_analytiques.section8_texte AS section_analytique_section8_texte,
      t_sections_analytiques.section9_id AS section_analytique_section9_id,
      t_sections_analytiques.section9_code AS section_analytique_section9_code,
      t_sections_analytiques.section9_texte AS section_analytique_section9_texte,
      p_historique_ecritures.section_analytique_2_id,
      p_historique_ecritures.section_analytique_3_id,
      p_historique_ecritures.section_analytique_4_id,
      p_historique_ecritures.section_analytique_5_id,
      p_historique_ecritures.section_analytique_6_id,
      p_historique_ecritures.section_analytique_7_id,
      p_historique_ecritures.section_analytique_8_id,
      p_historique_ecritures.section_analytique_9_id,
      p_historique_ecritures.clinique_honoraire,
      p_historique_ecritures.exercice_comptable,
      p_historique_ecritures.site_id,
      t_sites.code AS site_code,
      t_sites.texte AS site_texte,
      t_sites.section1_id AS site_section1_id,
      t_sites.section1_code AS site_section1_code,
      t_sites.section1_texte AS site_section1_texte,
      t_sites.section2_id AS site_section2_id,
      t_sites.section2_code AS site_section2_code,
      t_sites.section2_texte AS site_section2_texte,
      t_sites.section3_id AS site_section3_id,
      t_sites.section3_code AS site_section3_code,
      t_sites.section3_texte AS site_section3_texte,
      p_historique_ecritures.pole_rule_id,
      t_sites.finess_id,
      t_sites.finess_code,
      t_sites.finess_texte,
      p_historique_ecritures.partenaire_id,
      t_partenaires.code AS partenaire_code,
      t_partenaires.texte AS partenaire_texte,
      t_partenaires.section1_id AS partenaire_section1_id,
      t_partenaires.section1_code AS partenaire_section1_code,
      t_partenaires.section1_texte AS partenaire_section1_texte,
      t_partenaires.section2_id AS partenaire_section2_id,
      t_partenaires.section2_code AS partenaire_section2_code,
      t_partenaires.section2_texte AS partenaire_section2_texte,
      t_partenaires.section3_id AS partenaire_section3_id,
      t_partenaires.section3_code AS partenaire_section3_code,
      t_partenaires.section3_texte AS partenaire_section3_texte,
      t_comptes_c.type_compta_id,
      t_comptes_c.type_compta_code,
      t_comptes_c.type_compta_texte,
      t_comptes_c_extra.type_compta_id AS type_compta_extra_id,
      t_comptes_c_extra.type_compta_code AS type_compta_extra_code,
      t_comptes_c_extra.type_compta_texte AS type_compta_extra_texte,
      p_historique_ecritures.compte_extra_id,
      t_comptes_c_extra.numero AS compte_extra_numero,
      t_comptes_c_extra.texte AS compte_extra_texte,
      t_comptes_c_extra.section_id[1] AS compte_extra_section1_id,
      t_comptes_c_extra.section_code[1] AS compte_extra_section1_code,
      t_comptes_c_extra.section_texte[1] AS compte_extra_section1_texte,
      t_comptes_c_extra.section_id[2] AS compte_extra_section2_id,
      t_comptes_c_extra.section_code[2] AS compte_extra_section2_code,
      t_comptes_c_extra.section_texte[2] AS compte_extra_section2_texte,
      t_comptes_c_extra.section_id[3] AS compte_extra_section3_id,
      t_comptes_c_extra.section_code[3] AS compte_extra_section3_code,
      t_comptes_c_extra.section_texte[3] AS compte_extra_section3_texte,
      t_comptes_c_extra.section_id[4] AS compte_extra_section4_id,
      t_comptes_c_extra.section_code[4] AS compte_extra_section4_code,
      t_comptes_c_extra.section_texte[4] AS compte_extra_section4_texte,
      t_comptes_c_extra.section_id[5] AS compte_extra_section5_id,
      t_comptes_c_extra.section_code[5] AS compte_extra_section5_code,
      t_comptes_c_extra.section_texte[5] AS compte_extra_section5_texte,
      t_comptes_c_extra.section_id[6] AS compte_extra_section6_id,
      t_comptes_c_extra.section_code[6] AS compte_extra_section6_code,
      t_comptes_c_extra.section_texte[6] AS compte_extra_section6_texte,
      t_comptes_c_extra.section_id[7] AS compte_extra_section7_id,
      t_comptes_c_extra.section_code[7] AS compte_extra_section7_code,
      t_comptes_c_extra.section_texte[7] AS compte_extra_section7_texte,
      t_comptes_c_extra.section_id[8] AS compte_extra_section8_id,
      t_comptes_c_extra.section_code[8] AS compte_extra_section8_code,
      t_comptes_c_extra.section_texte[8] AS compte_extra_section8_texte,
      t_comptes_c_extra.section_id[9] AS compte_extra_section9_id,
      t_comptes_c_extra.section_code[9] AS compte_extra_section9_code,
      t_comptes_c_extra.section_texte[9] AS compte_extra_section9_texte,
      p_historique_ecritures.compte_budget_id,
      t_comptes_c_budget.numero AS compte_budget_numero,
      t_comptes_c_budget.texte AS compte_budget_texte,
      t_comptes_c_budget.section_id[1] AS compte_budget_section1_id,
      t_comptes_c_budget.section_code[1] AS compte_budget_section1_code,
      t_comptes_c_budget.section_texte[1] AS compte_budget_section1_texte,
      t_comptes_c_budget.section_id[2] AS compte_budget_section2_id,
      t_comptes_c_budget.section_code[2] AS compte_budget_section2_code,
      t_comptes_c_budget.section_texte[2] AS compte_budget_section2_texte,
      t_comptes_c_budget.section_id[3] AS compte_budget_section3_id,
      t_comptes_c_budget.section_code[3] AS compte_budget_section3_code,
      t_comptes_c_budget.section_texte[3] AS compte_budget_section3_texte,
      t_comptes_c_budget.section_id[4] AS compte_budget_section4_id,
      t_comptes_c_budget.section_code[4] AS compte_budget_section4_code,
      t_comptes_c_budget.section_texte[4] AS compte_budget_section4_texte,
      t_comptes_c_budget.section_id[5] AS compte_budget_section5_id,
      t_comptes_c_budget.section_code[5] AS compte_budget_section5_code,
      t_comptes_c_budget.section_texte[5] AS compte_budget_section5_texte,
      t_comptes_c_budget.section_id[6] AS compte_budget_section6_id,
      t_comptes_c_budget.section_code[6] AS compte_budget_section6_code,
      t_comptes_c_budget.section_texte[6] AS compte_budget_section6_texte,
      t_comptes_c_budget.section_id[7] AS compte_budget_section7_id,
      t_comptes_c_budget.section_code[7] AS compte_budget_section7_code,
      t_comptes_c_budget.section_texte[7] AS compte_budget_section7_texte,
      t_comptes_c_budget.section_id[8] AS compte_budget_section8_id,
      t_comptes_c_budget.section_code[8] AS compte_budget_section8_code,
      t_comptes_c_budget.section_texte[8] AS compte_budget_section8_texte,
      t_comptes_c_budget.section_id[9] AS compte_budget_section9_id,
      t_comptes_c_budget.section_code[9] AS compte_budget_section9_code,
      t_comptes_c_budget.section_texte[9] AS compte_budget_section9_texte,
      p_historique_ecritures.comptabilisee,
      p_historique_ecritures.fin_exercice,
      p_historique_ecritures.ecriture_consolidee,
      p_historique_ecritures.ajustement,
      p_historique_ecritures.inter_site,
      p_historique_ecritures.date_ecriture,
      p_historique_ecritures.identifiant_operation,
      p_historique_ecritures.cle_originale,
      p_historique_ecritures.compte_contrepartie_id,
      t_comptes_c_contrepartie.compte_nonsigne_numero AS compte_contrepartie_code,
      t_comptes_c_contrepartie.texte AS compte_contrepartie_texte,
      t_comptes_c_contrepartie.section_id AS compte_contrepartie_section_id,
      t_comptes_c_contrepartie.section_code AS compte_contrepartie_section_code,
      t_comptes_c_contrepartie.section_texte AS compte_contrepartie_section_texte,
      t_comptes_c_contrepartie.section_id[1] AS compte_contrepartie_section1_id,
      t_comptes_c_contrepartie.section_code[1] AS compte_contrepartie_section1_code,
      t_comptes_c_contrepartie.section_texte[1] AS compte_contrepartie_section1_texte,
      t_comptes_c_contrepartie.section_id[2] AS compte_contrepartie_section2_id,
      t_comptes_c_contrepartie.section_code[2] AS compte_contrepartie_section2_code,
      t_comptes_c_contrepartie.section_texte[2] AS compte_contrepartie_section2_texte,
      t_comptes_c_contrepartie.section_id[3] AS compte_contrepartie_section3_id,
      t_comptes_c_contrepartie.section_code[3] AS compte_contrepartie_section3_code,
      t_comptes_c_contrepartie.section_texte[3] AS compte_contrepartie_section3_texte,
      t_comptes_c_contrepartie.section_id[4] AS compte_contrepartie_section4_id,
      t_comptes_c_contrepartie.section_code[4] AS compte_contrepartie_section4_code,
      t_comptes_c_contrepartie.section_texte[4] AS compte_contrepartie_section4_texte,
      t_comptes_c_contrepartie.section_id[5] AS compte_contrepartie_section5_id,
      t_comptes_c_contrepartie.section_code[5] AS compte_contrepartie_section5_code,
      t_comptes_c_contrepartie.section_texte[5] AS compte_contrepartie_section5_texte,
      t_comptes_c_contrepartie.section_id[6] AS compte_contrepartie_section6_id,
      t_comptes_c_contrepartie.section_code[6] AS compte_contrepartie_section6_code,
      t_comptes_c_contrepartie.section_texte[6] AS compte_contrepartie_section6_texte,
      t_comptes_c_contrepartie.section_id[7] AS compte_contrepartie_section7_id,
      t_comptes_c_contrepartie.section_code[7] AS compte_contrepartie_section7_code,
      t_comptes_c_contrepartie.section_texte[7] AS compte_contrepartie_section7_texte,
      t_comptes_c_contrepartie.section_id[8] AS compte_contrepartie_section8_id,
      t_comptes_c_contrepartie.section_code[8] AS compte_contrepartie_section8_code,
      t_comptes_c_contrepartie.section_texte[8] AS compte_contrepartie_section8_texte,
      t_comptes_c_contrepartie.section_id[9] AS compte_contrepartie_section9_id,
      t_comptes_c_contrepartie.section_code[9] AS compte_contrepartie_section9_code,
      t_comptes_c_contrepartie.section_texte[9] AS compte_contrepartie_section9_texte,
      t_pole_rule_pole.pole_id,
      t_pole_rule_pole.pole_code,
      t_pole_rule_pole.pole_texte,
      t_pole_rule_pole.pole_section_id,
      t_pole_rule_pole.pole_section_code,
      t_pole_rule_pole.pole_section_texte,
      p_historique_ecritures.texte,
      p_historique_ecritures.date_facture_fournisseur,
      p_historique_ecritures.piece,
      p_historique_ecritures.dossier,
      p_historique_ecritures.date_echeance,
      p_historique_ecritures.journal_paiement_id,
      p_historique_ecritures.mode_paiement_id,
      p_historique_ecritures.numero_cheque,
      p_historique_ecritures.is_budget,
          CASE
              WHEN p_historique_ecritures.is_budget IS DISTINCT FROM '1'::text THEN p_historique_ecritures.montant_credit * t_pole_rule_pole.taux
              ELSE 0::numeric
          END AS montant_credit,
          CASE
              WHEN p_historique_ecritures.is_budget IS DISTINCT FROM '1'::text THEN p_historique_ecritures.montant_debit * t_pole_rule_pole.taux
              ELSE 0::numeric
          END AS montant_debit,
          CASE
              WHEN p_historique_ecritures.is_budget IS DISTINCT FROM '1'::text THEN p_historique_ecritures.montant_debit * t_pole_rule_pole.taux - p_historique_ecritures.montant_credit * t_pole_rule_pole.taux
              ELSE 0::numeric
          END AS montant_solde_debit,
          CASE
              WHEN p_historique_ecritures.is_budget IS DISTINCT FROM '1'::text THEN p_historique_ecritures.montant_credit * t_pole_rule_pole.taux - p_historique_ecritures.montant_debit * t_pole_rule_pole.taux
              ELSE 0::numeric
          END AS montant_solde_credit,
          CASE
              WHEN p_historique_ecritures.is_budget = '1'::text THEN p_historique_ecritures.montant_credit * t_pole_rule_pole.taux
              ELSE 0::numeric
          END AS montant_credit_budget,
          CASE
              WHEN p_historique_ecritures.is_budget = '1'::text THEN p_historique_ecritures.montant_debit * t_pole_rule_pole.taux
              ELSE 0::numeric
          END AS montant_debit_budget,
          CASE
              WHEN p_historique_ecritures.is_budget = '1'::text THEN p_historique_ecritures.montant_debit * t_pole_rule_pole.taux - p_historique_ecritures.montant_credit * t_pole_rule_pole.taux
              ELSE 0::numeric
          END AS montant_solde_debit_budget,
          CASE
              WHEN p_historique_ecritures.is_budget = '1'::text THEN p_historique_ecritures.montant_credit * t_pole_rule_pole.taux - p_historique_ecritures.montant_debit * t_pole_rule_pole.taux
              ELSE 0::numeric
          END AS montant_solde_credit_budget,
      1 AS nombre_ecritures,
          CASE
              WHEN p_historique_ecritures.montant_debit <> 0::numeric THEN floor(abs(p_historique_ecritures.montant_debit * t_pole_rule_pole.taux))
              ELSE floor(abs(p_historique_ecritures.montant_credit * t_pole_rule_pole.taux))
          END AS montant_absolu_ecriture,
      0 AS no_oid,
      ''::text AS no_code,
      ''::text AS no_texte,
      t_comptes_c.section_id AS compte_section_id,
      t_comptes_c.section_code AS compte_section_code,
      t_comptes_c.section_texte AS compte_section_texte,
      t_journaux.section_id AS journal_section_id,
      t_journaux.section_code AS journal_section_code,
      t_journaux.section_texte AS journal_section_texte,
      t_sections_analytiques.section_id AS section_analytique_section_id,
      t_sections_analytiques.section_code AS section_analytique_section_code,
      t_sections_analytiques.section_texte AS section_analytique_section_texte,
      t_sites.section_id AS site_section_id,
      t_sites.section_code AS site_section_code,
      t_sites.section_texte AS site_section_texte,
      t_partenaires.section_id AS partenaire_section_id,
      t_partenaires.section_code AS partenaire_section_code,
      t_partenaires.section_texte AS partenaire_section_texte,
      t_comptes_c_extra.section_id AS compte_extra_section_id,
      t_comptes_c_extra.section_code AS compte_extra_section_code,
      t_comptes_c_extra.section_texte AS compte_extra_section_texte,
      t_comptes_c_budget.section_id AS compte_budget_section_id,
      t_comptes_c_budget.section_code AS compte_budget_section_code,
      t_comptes_c_budget.section_texte AS compte_budget_section_texte
     FROM compta.p_historique_ecritures
       JOIN compta.t_journaux ON p_historique_ecritures.journal_id = t_journaux.oid
       JOIN compta.t_comptes_c ON p_historique_ecritures.compte_id = t_comptes_c.oid
       JOIN compta.t_comptes_c t_comptes_c_extra ON p_historique_ecritures.compte_extra_id = t_comptes_c_extra.oid
       JOIN compta.t_comptes_c t_comptes_c_contrepartie ON p_historique_ecritures.compte_contrepartie_id = t_comptes_c_contrepartie.oid
       JOIN compta.t_sites ON p_historique_ecritures.site_id = t_sites.oid
       JOIN compta.t_partenaires ON p_historique_ecritures.partenaire_id = t_partenaires.oid
       JOIN compta.t_sections_analytiques ON p_historique_ecritures.section_analytique_id = t_sections_analytiques.oid
       JOIN compta.t_comptes_c t_comptes_c_budget ON p_historique_ecritures.compte_budget_id = t_comptes_c_budget.oid
       JOIN compta.t_pole_rule_pole ON p_historique_ecritures.pole_rule_id = t_pole_rule_pole.pole_rule_id;