return: text
lang: plpgsql
parameters:
  p0:
    type: text
    name: i_clause
  p1:
    type: text
    name: i_option
src: |
  DECLARE 
        _i int;
        _new_clause text;
        
      BEGIN
          _new_clause = i_clause;
        
          -- Mots clés dans clause Where
          IF i_option = 'where' THEN
          
            _new_clause = replace(_new_clause,'[DURSEJ]','w_rss.duree_sejour');
            _new_clause = regexp_replace(_new_clause, ' ET ', ' AND ', 'gi');
            _new_clause = regexp_replace(_new_clause, ' OU ', ' OR ', 'gi');
            _new_clause = regexp_replace(_new_clause, ' NON ', ' NOT ', 'gi');
            _new_clause = replace(_new_clause,'[PREC:','w_rss_precedent.[');
            _new_clause = replace(_new_clause,'[ETS:','w_rss.ets_');
            _new_clause = replace(_new_clause,'[AGE]','w_rss.age');
            _new_clause = replace(_new_clause,'[SEXE]','w_rss.sexe');
            _new_clause = replace(_new_clause,'[CMD]','w_rss.cmd_code');
            _new_clause = replace(_new_clause,'[GHM3]','w_rss.ghm3');
            _new_clause = replace(_new_clause,'[GHM]','w_rss.ghm_code');
            _new_clause = replace(_new_clause,'[GHMRSA]','w_rss.ghmrsa_code');
            _new_clause = replace(_new_clause,'[GHMRSAIN]','w_rss.ghmrsa_in_code');
            _new_clause = replace(_new_clause,'[GHMSEV]','w_rss.severite_ghm_code');
            _new_clause = replace(_new_clause,'[GHMCMA]','w_rss.ghm_cma');
            _new_clause = replace(_new_clause,'[GHMCONF]','w_rss.confirmation_codage');
            _new_clause = replace(_new_clause,'[GHM:datim_tghm]','w_rss.ghm_datim_tghm');
            _new_clause = replace(_new_clause,'[GHM:datim_cti_1orj]','w_rss.ghm_datim_cti_1orj');
            _new_clause = replace(_new_clause,'[GHM5:datim_conf]','w_rss.ghm5_datim_conf');
            _new_clause = replace(_new_clause,'[GHS]','w_rss.ghs_code');
            _new_clause = replace(_new_clause,'[REHOSPMEMEGHM]','w_rss.rehosp_meme_ghm');
            _new_clause = replace(_new_clause,'[TYPSEJ]','w_rss.type_sejour');
            _new_clause = replace(_new_clause,'[AGEGEST]','w_rss.age_gestationnel');
            _new_clause = replace(_new_clause,'[DATEREGLES]','w_rss.date_dernieres_regles');
            
            _new_clause = replace(_new_clause,'[DATEENTREE]','w_rss.date_entree');
            _new_clause = replace(_new_clause,'[DATESORTIE]','w_rss.date_sortie');
            _new_clause = replace(_new_clause,'[TAUX]','w_rss.taux_remboursement');
            _new_clause = replace(_new_clause,'[EPMSI]','w_rss.traitement_epmsi');
            _new_clause = replace(_new_clause,'[EXOTM]','w_rss.exoneration_tm_code');
            
            _new_clause = replace(_new_clause,'[CCAM]','acte_code');
            _new_clause = replace(_new_clause,'[CCAMP]','w_rss.acte_principal_code');
            _new_clause = replace(_new_clause,'[CCAM:','acte_');
            _new_clause = replace(_new_clause,'[MERE:CCAM]','acte_code');
            
            _new_clause = replace(_new_clause,'[DIAGP]','w_rss.diagnostic_principal_code');
            _new_clause = replace(_new_clause,'[DIAGP:','w_rss.diagnostic_principal_');
            
            _new_clause = replace(_new_clause,'[DIAGR]','w_rss.diagnostic_relie_code');
            _new_clause = replace(_new_clause,'[DIAGR:','w_rss.diagnostic_relie_');
            _new_clause = replace(_new_clause,'[DIAGDOUBLE]','w_rss.diagnostic_double_code');
            _new_clause = replace(_new_clause,'[DIAGAARRAY]','w_rss.da_array');
                  
            _new_clause = replace(_new_clause,'[MATER:acc_hors_img]','w_rss.mat_accouchement_hors_img');
            _new_clause = replace(_new_clause,'[MATER:age_gest_manquant]','w_rss.mat_age_gestationnel_manquant');
            _new_clause = replace(_new_clause,'[MATER:age_gest_inf_22sa]','w_rss.mat_age_gestationnel_inf_22sa');
            _new_clause = replace(_new_clause,'[MATER:avec_acte_acc]','w_rss.mat_avec_acte_accouchement');
            _new_clause = replace(_new_clause,'[MATER:conf_codage]','w_rss.mat_confirmation_codage');
            _new_clause = replace(_new_clause,'[MATER:ecart_age_gest]','w_rss.mat_ecart_age_gestationnel');
            _new_clause = replace(_new_clause,'[MATER:hors_cmd14]','w_rss.mat_hors_cmd_14');
            _new_clause = replace(_new_clause,'[MATER:hors_etab]','w_rss.mat_hors_etablissement');
        
            
            _new_clause = replace(_new_clause,'[DIAGA]','diagnostic_associe_code');
            _new_clause = replace(_new_clause,'[DIAGA:','diagnostic_associe_');
            _new_clause = replace(_new_clause,'[DIAGD]','diagnostic_documentaire_code');
            _new_clause = replace(_new_clause,'[DIAGD:','diagnostic_documentaire_');
            _new_clause = replace(_new_clause,'[DIAGNONCODE01]','pathologies_chroniques_non_codees{1}');
            _new_clause = replace(_new_clause,'[DIAGNONCODE02]','pathologies_chroniques_non_codees{2}');
            _new_clause = replace(_new_clause,'[DIAGNONCODE03]','pathologies_chroniques_non_codees{3}');
            _new_clause = replace(_new_clause,'[DIAGNONCODE04]','pathologies_chroniques_non_codees{4}');
            _new_clause = replace(_new_clause,'[DIAGNONCODE05]','pathologies_chroniques_non_codees{5}');
            _new_clause = replace(_new_clause,'[DIAGNONCODE06]','pathologies_chroniques_non_codees{6}');
            _new_clause = replace(_new_clause,'[DIAGNONCODE07]','pathologies_chroniques_non_codees{7}');
            _new_clause = replace(_new_clause,'[DIAGNONCODE08]','pathologies_chroniques_non_codees{8}');
            _new_clause = replace(_new_clause,'[DIAGNONCODE09]','pathologies_chroniques_non_codees{9}');
            _new_clause = replace(_new_clause,'[DIAGNONCODE99]','pathologies_chroniques_non_codees{10}');
            
            _new_clause = replace(_new_clause,'[PRESTAP]','w_rss.prestation_principale_code');
            _new_clause = replace(_new_clause,'[PRESTA]','presta_code');
            _new_clause = replace(_new_clause,'[PRESTA:','presta_');
            _new_clause = replace(_new_clause,'[VIDHOSPTM]','w_rss.vidhosp_montant_tm');
            _new_clause = replace(_new_clause,'[VIDHOSHOSBB]','w_rss.vidhosp_hospit_nouveau_ne');
            _new_clause = replace(_new_clause,'[FIDES]','w_rss.valorise_fides_code');
            
            _new_clause = replace(_new_clause,'[SPEMEDRSS]','w_rss.specialite_interne_medecin_reference_rss_id');
            _new_clause = replace(_new_clause,'[MODENT]','w_rss.mode_entree');
            _new_clause = replace(_new_clause,'[PROV]','w_rss.provenance');
            _new_clause = replace(_new_clause,'[MODSOR]','w_rss.mode_sortie');
            _new_clause = replace(_new_clause,'[DEST]','w_rss.destination');
            _new_clause = replace(_new_clause,'[DS-DE]','(w_rss.date_sortie-w_rss.date_entree)');
            _new_clause = replace(_new_clause,'[SEANCES]','w_rss.nb_seances');
            _new_clause = replace(_new_clause,'[ETAT]','w_rss.en_cours_etat');
            _new_clause = replace(_new_clause,'[RETGRP]','w_rss.code_retour_groupage');
            _new_clause = replace(_new_clause,'[CAGHS]','w_rss.ca_ghs');
            _new_clause = replace(_new_clause,'[CAGHSEXH]','(w_rss.ca_ghs + w_rss.ca_ghs)');
            _new_clause = replace(_new_clause,'[CAEXH]','w_rss.ca_exh');
            _new_clause = replace(_new_clause,'[NBEXH]','w_rss.nb_exh');
            _new_clause = replace(_new_clause,'[NBEXHRSA]','w_rss.nb_exh_rsa');
            _new_clause = replace(_new_clause,'[CAJGHSEXH]','base.cti_division(w_rss.ca_ghs + w_rss.ca_exh,duree_sejour)');
            _new_clause = replace(_new_clause,'w_rss_precedent.w_rss.','w_rss_precedent.');
            _new_clause = replace(_new_clause,'[RAAC]','w_rss.raac_id');
            _new_clause = replace(_new_clause,'[','');
            _new_clause = replace(_new_clause,']','');
            _new_clause = replace(_new_clause,'{','[');
            _new_clause = replace(_new_clause,'}',']');
          END IF;
          -- Mots clés dans justificatif
          IF i_option = 'justificatif' THEN
            _new_clause = replace(_new_clause,'][',']||'', ''||[');
            _new_clause = replace(_new_clause,'] [',']||'', ''||[');
            
            _new_clause = replace(_new_clause,'][','], [');
            _new_clause = replace(_new_clause,'[PREC:','w_rss_precedent.[');
            
            _new_clause = replace(_new_clause,'[AGE]','''AGE:'' || w_rss.age');
            _new_clause = replace(_new_clause,'[SEXE]','''SEXE:'' || w_rss.sexe');
            _new_clause = replace(_new_clause,'[CPOST]','''CPOST:'' || w_rss.code_postal');
            _new_clause = replace(_new_clause,'[CMD]','''CMD:'' || w_rss.cmd_code');
            _new_clause = replace(_new_clause,'[GHM]','''GHM:'' || w_rss.ghm_code');
            _new_clause = replace(_new_clause,'[GHM5]','''GHM5:'' || w_rss.ghm5_code');
            _new_clause = replace(_new_clause,'[GHS]','''GHS:'' || w_rss.ghs_code');
            _new_clause = replace(_new_clause,'[GHSRSA]','''GHSRSA:'' || w_rss.ghsrsa_code');
            _new_clause = replace(_new_clause,'[GHMRSA]','''GHMRSA:'' || w_rss.ghmrsa_code');
            _new_clause = replace(_new_clause,'[GHMRSAIN]','''GHMRSA IN:'' || w_rss.ghmrsa_in_code');
            _new_clause = replace(_new_clause,'[GHMSEV]','''SEV:'' || w_rss.severite_ghm_code');
            _new_clause = replace(_new_clause,'[GHMCMA]','''CMA:'' || w_rss.ghm_cma::text');
            _new_clause = replace(_new_clause,'[GHS]','''GHS:'' || w_rss.ghs_code');
            _new_clause = replace(_new_clause,'[DATEENTREE]','w_rss.date_entree::text');
            _new_clause = replace(_new_clause,'[DATESORTIE]','w_rss.date_sortie::text');
            _new_clause = replace(_new_clause,'[TAUX]','''TAUX:'' || w_rss.taux_remboursement::text');
            _new_clause = replace(_new_clause,'[EPMSI]','''EPMSI:'' || (w_rss.traitement_epmsi || '' '' || w_rss.traitement_epmsi_texte)::text');
            _new_clause = replace(_new_clause,'[EXOTM]','''ExoTM:'' || (w_rss.exoneration_tm_code || '' '' || w_rss.exoneration_tm_texte)::text');
            
            
            _new_clause = replace(_new_clause,'[CCAMP]','''CCAM:'' || w_rss.acte_principal_code');
            _new_clause = replace(_new_clause,'[DIAGP]','''DP:'' || w_rss.diagnostic_principal_code');
            _new_clause = replace(_new_clause,'[DIAGR]','''DR:'' || w_rss.diagnostic_relie_code');
            _new_clause = replace(_new_clause,'[DIAGS]','''DIAGS:'' || w_rss.liste_diagnostics');
            _new_clause = replace(_new_clause,'[DIAGDOUBLE]','''DIAG:'' || w_rss.diagnostic_double_code');
            _new_clause = replace(_new_clause,'[PRESTAP]','''PRESTA:'' || w_rss.prestation_principale_code');
            _new_clause = replace(_new_clause,'[MODENT]','''MODENT:'' || w_rss.mode_entree');
            _new_clause = replace(_new_clause,'[PROV]','''PROV:'' || w_rss.provenance');
            _new_clause = replace(_new_clause,'[MODSOR]','''MODSOR:'' || w_rss.mode_sortie');
            _new_clause = replace(_new_clause,'[DEST]','''DEST:'' || w_rss.destination');
            _new_clause = replace(_new_clause,'[DURSEJ]','''DUR:'' || w_rss.duree_sejour || ''j''');
            _new_clause = replace(_new_clause,'[SEANCES]','''SEANCES:'' || w_rss.nb_seances');
            _new_clause = replace(_new_clause,'[ETAT]','''ETAT:'' || w_rss.en_cours_etat');
            _new_clause = replace(_new_clause,'[RETGRP]','''RETGRP:'' || w_rss.code_retour_groupage');
            _new_clause = replace(_new_clause,'[CAGHS]','''CAGHS:'' || w_rss.ca_ghs::text');
            _new_clause = replace(_new_clause,'[CAGHSEXH]','''CAGHSEXH:'' || (w_rss.ca_ghs + w_rss.ca_exh)::text');
            _new_clause = replace(_new_clause,'[CAEXH]','''CAEXH:'' || w_rss.ca_exh::text');
            _new_clause = replace(_new_clause,'[NBEXH]','''NBEXH:'' || w_rss.nb_exh::text');
            _new_clause = replace(_new_clause,'[NBEXHRSA]','''EXH_RSA:'' || w_rss.nb_exh_rsa::text');
            _new_clause = replace(_new_clause,'[CAJGHSEXH]','''CAGHS/J:'' || round(base.cti_division(w_rss.ca_ghs + w_rss.ca_exh,duree_sejour),0)');
            _new_clause = replace(_new_clause,'w_rss_precedent.w_rss.','w_rss_precedent.');
            _new_clause = replace(_new_clause,'[AGEGEST]','''AGEST:'' || w_rss.age_gestationnel::text');
            _new_clause = replace(_new_clause,'[DATEREGLES]','''DREGL:'' || w_rss.date_dernieres_regles::text');
            _new_clause = replace(_new_clause,'[MATER_ECART_AGE_GEST]','''EAGEST:'' || w_rss.mat_ecart_age_gestationnel::text');
            _new_clause = replace(_new_clause,'[RAAC]','''RAAC:'' || w_rss.raac_id::text');
            IF trim(_new_clause) = '' THEN
              _new_clause = '''''';
            END IF;
            
            
          END IF;
          
          
              RETURN _new_clause;
      END;
