<?xml version="1.0" encoding="ISO-8859-1"?>
<ROOT>

    <NODE name="ETAPE_1" label="CREATION DE LA TABLE SIMULATION">
            <sqlcmd><![CDATA[

                    -- Table: pmsi.[simulationTableFG]

                    DROP TABLE IF EXISTS pmsi.[simulationTableFG].,

                    CREATE TABLE pmsi.[simulationTableFG]
                    (
                      annee_sortie numeric(4,0),
                      coefficient_mco_fg0 numeric(5,4) DEFAULT 1,
                      coefficient_mco_fg1 numeric(5,4) DEFAULT 1,
                      commentaire character varying(255) DEFAULT ''::character varying,
                      erreur character varying(255) DEFAULT ''::character varying,
                      est_envoye character(1) DEFAULT '0'::bpchar,
                      est_regroupe character(1) DEFAULT '0'::bpchar,
                      finess character(9),
                      ghm_fg0_id bigint DEFAULT 0,
                      ghm_fg1_code character(6),
                      ghm_fg1_id bigint DEFAULT 0,
                      ghs_fg0_id bigint DEFAULT 0,
                      ghs_fg1_borne_basse numeric(4,0) DEFAULT 0,
                      ghs_fg1_borne_haute numeric(4,0) DEFAULT 0,
                      ghs_fg1_code numeric(4,0),
                      ghs_fg1_forfait_exb numeric(13,2) DEFAULT 0,
                      ghs_fg1_id bigint DEFAULT 0,
                      ghs_fg1_tarif_exb numeric(13,2) DEFAULT 0,
                      ghs_fg1_tarif_exh numeric(13,2) DEFAULT 0,
                      ghs_fg1_tarif_ghs numeric(13,2) DEFAULT 0,
                      ghs_minore_code text DEFAULT 0,
                      ghs_minore_text text DEFAULT 0,
                      montant_9604_fg0 numeric DEFAULT 0,
                      montant_9604_fg1 numeric DEFAULT 0,
                      montant_9605_fg0 numeric DEFAULT 0,
                      montant_9605_fg1 numeric DEFAULT 0,
                      montant_ant_fg0 numeric(13,2) DEFAULT 0,
                      montant_ant_fg1 numeric(13,2) DEFAULT 0,
                      montant_emi_fg0 numeric(13,2) DEFAULT 0,
                      montant_emi_fg1 numeric(13,2) DEFAULT 0,
                      montant_emi_fg1_deremboursees numeric(13,2) DEFAULT 0,
                      montant_eti_fg0 numeric(13,2) DEFAULT 0,
                      montant_eti_fg1 numeric(13,2) DEFAULT 0,
                      montant_eti_fg1_deremboursees numeric(13,2) DEFAULT 0,
                      montant_exb_fg0 numeric(13,2) DEFAULT 0,
                      montant_exb_fg1 numeric(13,2) DEFAULT 0,
                      montant_exh_fg0 numeric(13,2) DEFAULT 0,
                      montant_exh_fg1 numeric(13,2) DEFAULT 0,
                      montant_ghs_theorique_fg0 numeric(13,2) DEFAULT 0,
                      montant_ghs_theorique_fg1 numeric(13,2) DEFAULT 0,
                      montant_lpp_fg0 numeric(13,2) DEFAULT 0,
                      montant_lpp_fg1 numeric(13,2) DEFAULT 0,
                      montant_lpp_fg1_deremboursees numeric(13,2) DEFAULT 0,
                      montant_nn1_fg0 numeric(13,2) DEFAULT 0,
                      montant_nn1_fg1 numeric(13,2) DEFAULT 0,
                      montant_nn2_fg0 numeric(13,2) DEFAULT 0,
                      montant_nn2_fg1 numeric(13,2) DEFAULT 0,
                      montant_nn3_fg0 numeric(13,2) DEFAULT 0,
                      montant_nn3_fg1 numeric(13,2) DEFAULT 0,
                      montant_rap_fg0 numeric(13,2) DEFAULT 0,
                      montant_rap_fg1 numeric(13,2) DEFAULT 0,
                      montant_rea_fg0 numeric(13,2) DEFAULT 0,
                      montant_rea_fg1 numeric(13,2) DEFAULT 0,
                      montant_rep_fg0 numeric(13,2) DEFAULT 0,
                      montant_rep_fg1 numeric(13,2) DEFAULT 0,
                      montant_sdc_fg0 numeric(13,2) DEFAULT 0,
                      montant_sdc_fg1 numeric(13,2) DEFAULT 0,
                      montant_tde_fg0 numeric(13,2) DEFAULT 0,
                      montant_tde_fg1 numeric(13,2) DEFAULT 0,
                      montant_tse_fg0 numeric(13,2) DEFAULT 0,
                      montant_tse_fg1 numeric(13,2) DEFAULT 0,                      
                      montant_src_eligibles_fg1 numeric(13,2) DEFAULT 0,
                      montant_src_fg0 numeric(13,2) DEFAULT 0,
                      montant_src_fg1 numeric(13,2) DEFAULT 0,
                      montant_stf_fg0 numeric(13,2) DEFAULT 0,
                      montant_stf_fg1 numeric(13,2) DEFAULT 0,
                      montant_ucd_fg0 numeric(13,2) DEFAULT 0,
                      montant_ucd_fg1 numeric(13,2) DEFAULT 0,
                      montant_ucd_fg1_deremboursees numeric(13,2) DEFAULT 0,
                      nb_9604_fg0 numeric DEFAULT 0,
                      nb_9604_fg1 numeric DEFAULT 0,
                      nb_9605_fg0 numeric DEFAULT 0,
                      nb_9605_fg1 numeric DEFAULT 0,
                      nb_ant_fg0 numeric(3,0) DEFAULT 0,
                      nb_ant_fg1 numeric(3,0) DEFAULT 0,
                      nb_exb_fg1 numeric(5,1) DEFAULT 0,
                      nb_exh_fg0 numeric(5,0) DEFAULT 0,
                      nb_exh_fg1 numeric(5,0) DEFAULT 0,
                      nb_nn1_fg0 numeric(3,0) DEFAULT 0,
                      nb_nn1_fg1 numeric(3,0) DEFAULT 0,
                      nb_nn2_fg0 numeric(3,0) DEFAULT 0,
                      nb_nn2_fg1 numeric(3,0) DEFAULT 0,
                      nb_nn3_fg0 numeric(3,0) DEFAULT 0,
                      nb_nn3_fg1 numeric(3,0) DEFAULT 0,
                      nb_rap_fg0 numeric(3,0) DEFAULT 0,
                      nb_rap_fg1 numeric(3,0) DEFAULT 0,
                      nb_rea_fg0 numeric(3,0) DEFAULT 0,
                      nb_rea_fg1 numeric(3,0) DEFAULT 0,
                      nb_rep_fg0 numeric(3,0) DEFAULT 0,
                      nb_rep_fg1 numeric(3,0) DEFAULT 0,
                      nb_sdc_fg0 numeric DEFAULT 0,
                      nb_sdc_fg1 numeric DEFAULT 0,
                      nb_tde_fg0 numeric DEFAULT 0,
                      nb_tde_fg1 numeric DEFAULT 0,
                      nb_tse_fg0 numeric DEFAULT 0,
                      nb_tse_fg1 numeric DEFAULT 0,                                            
                      nb_sej_exb_fg0 numeric(1,0) DEFAULT 0,
                      nb_sej_exb_fg1 numeric(1,0) DEFAULT 0,
                      nb_sej_src_eligibles_fg1 numeric(3,0) DEFAULT 0,
                      nb_src_eligibles_fg1 numeric(3,0) DEFAULT 0,
                      nb_src_fg0 numeric(3,0) DEFAULT 0,
                      nb_src_fg1 numeric(3,0) DEFAULT 0,
                      nb_stf_fg0 numeric(3,0) DEFAULT 0,
                      nb_stf_fg1 numeric(3,0) DEFAULT 0,
                      nb_ucd_fg0 numeric DEFAULT 0,
                      no_rss bigint DEFAULT 0,
                      regle_src_eligible_fg1 character varying(255) DEFAULT ''::character varying,
                      rss_id bigint DEFAULT 0,
                      tarif_9604_fg1 numeric DEFAULT 0,
                      tarif_9605_fg1 numeric DEFAULT 0,
                      tarif_ant_fg1 numeric(13,2) DEFAULT 0,
                      tarif_nn1_fg1 numeric(13,2) DEFAULT 0,
                      tarif_nn2_fg1 numeric(13,2) DEFAULT 0,
                      tarif_nn3_fg1 numeric(13,2) DEFAULT 0,
                      tarif_rap_fg1 numeric(13,2) DEFAULT 0,
                      tarif_rea_fg1 numeric(13,2) DEFAULT 0,
                      tarif_sdc_fg0 numeric(13,2) DEFAULT 0,
                      tarif_sdc_fg1 numeric(13,2) DEFAULT 0,
                      tarif_tde_fg0 numeric(13,2) DEFAULT 0,
                      tarif_tde_fg1 numeric(13,2) DEFAULT 0,
                      tarif_tse_fg0 numeric(13,2) DEFAULT 0,
                      tarif_tse_fg1 numeric(13,2) DEFAULT 0,
                      tarif_src_fg1 numeric(13,2) DEFAULT 0,
                      tarif_stf_fg1 numeric(13,2) DEFAULT 0,
                      translation character(1) DEFAULT '0'::bpchar,
                      is_raac text DEFAULT '',
                      coefficient numeric 
                    )
                    WITH (
                      OIDS=FALSE
                    ).,
                    ALTER TABLE pmsi.[simulationTableFG]
                      OWNER TO postgres.,
                    COMMENT ON TABLE pmsi.[simulationTableFG]
                      IS '* fg0 = FGN
                    * fg1 = FGN+1'.,

                    -- Index: pmsi.i_[simulationTableFG]_1

                    -- DROP INDEX pmsi.i_[simulationTableFG]_1.,

                    CREATE INDEX i_[simulationTableFG]_1
                      ON pmsi.[simulationTableFG]
                      USING btree
                      (no_rss).,

                    -- Index: pmsi.i_[simulationTableFG]_2

                    -- DROP INDEX pmsi.i_[simulationTableFG]_2.,

                    CREATE INDEX i_[simulationTableFG]_2
                      ON pmsi.[simulationTableFG]
                      USING btree
                      (annee_sortie).,

                    -- Index: pmsi.i_[simulationTableFG]_3

                    -- DROP INDEX pmsi.i_[simulationTableFG]_3.,

                    CREATE INDEX i_[simulationTableFG]_3
                      ON pmsi.[simulationTableFG]
                      USING btree
                      (rss_id).,

                    -- Index: pmsi.i_[simulationTableFG]_4

                    -- DROP INDEX pmsi.i_[simulationTableFG]_4.,

                    CREATE INDEX i_[simulationTableFG]_4
                      ON pmsi.[simulationTableFG]
                      USING btree
                      (ghm_fg1_id).,

                    -- Index: pmsi.i_[simulationTableFG]_5

                    -- DROP INDEX pmsi.i_[simulationTableFG]_

                    CREATE INDEX i_[simulationTableFG]_5
                      ON pmsi.[simulationTableFG]
                      USING btree
                      (ghs_fg1_id).,

            ]]></sqlcmd>
    </NODE>

    <NODE name="ETAPE_2" label="Alimentation par données issues des données PMSI">
            <sqlcmd><![CDATA[


                TRUNCATE pmsi.[simulationTableFG]
                .,
                

                INSERT INTO pmsi.[simulationTableFG]
                    (
                    finess, 
                    no_rss, 
                    annee_sortie, 
                    rss_id, 
                    ghm_fg0_id, 
                    ghs_fg0_id
                    )
                    SELECT 
                        v_rss_1.finess, 
                        v_rss_1.no_rss, 
                        date_part('year',v_rss_1.date_sortie), 
                        rss_id, 
                        ghm_id, 
                        ghs_id
                    FROM pmsi.v_rss_1
                    WHERE 1=1
                        AND v_rss_1.date_sortie BETWEEN '[DATE_1]' AND '[DATE_4]'
                        AND en_cours = '0' 
                        AND mco IN ('M', 'C', 'O')
                .,

            ]]></sqlcmd>
    </NODE>
    
    <NODE name="ETAPE_3" label="Positionnement des valeurs FG0 avant simulation">
            <sqlcmd><![CDATA[
            
                UPDATE pmsi.[simulationTableFG]
                    SET 
                        coefficient_mco_fg0 = subview.coefficient_mco_fg0, 
                        montant_ghs_theorique_fg0 = subview.montant_ghs_theorique_fg0, 
                        nb_sej_exb_fg0 = subview.nb_sej_exb_fg0,
                        montant_exb_fg0 = subview.montant_exb_fg0,
                        nb_exh_fg0 = subview.nb_exh_fg0,
                        montant_exh_fg0 = subview.montant_exh_fg0,
                        nb_src_fg0 = subview.nb_src_fg0,
                        montant_src_fg0 = subview.montant_src_fg0,
                        nb_rea_fg0 = subview.nb_rea_fg0,
                        montant_rea_fg0 = subview.montant_rea_fg0,
                        nb_stf_fg0 = subview.nb_stf_fg0,
                        montant_stf_fg0 = subview.montant_stf_fg0,
                        nb_nn1_fg0 = subview.nb_nn1_fg0,
                        montant_nn1_fg0 = subview.montant_nn1_fg0,
                        nb_nn2_fg0 = subview.nb_nn2_fg0,
                        montant_nn2_fg0 = subview.montant_nn2_fg0,
                        nb_nn3_fg0 = subview.nb_nn3_fg0,
                        montant_nn3_fg0 = subview.montant_nn3_fg0,
                        nb_ant_fg0 = subview.nb_ant_fg0,
                        montant_ant_fg0 = subview.montant_ant_fg0,
                        nb_rap_fg0 = subview.nb_rap_fg0,
                        montant_rap_fg0 = subview.montant_rap_fg0,
                        nb_9605_fg0 = subview.nb_9605_fg0,
                        montant_9605_fg0 = subview.montant_9605_fg0,
                        nb_9604_fg0 = subview.nb_9604_fg0,
                        montant_9604_fg0 = subview.montant_9604_fg0,
                        nb_sdc_fg0 = subview.nb_sdc_fg0,
                        montant_sdc_fg0 = subview.montant_sdc_fg0  ,
                        nb_tde_fg0 = subview.nb_tde_fg0,
                        montant_tde_fg0 = subview.montant_tde_fg0  ,
                        nb_tse_fg0 = subview.nb_tse_fg0,
                        montant_tse_fg0 = subview.montant_tse_fg0,
                        coefficient = subview.coefficient
                        
                    FROM 
                        (
                        SELECT 
                            v_rsf_detail_1.rss_id,
                            MIN(CASE WHEN prestation_code = 'GHS' THEN coefficient_mco ELSE null END) AS coefficient_mco_fg0,
                            SUM(
                                CASE WHEN prestation_code = 'GHS' THEN                                
                                    CASE   
                                            WHEN coefficient >= 1 THEN v_rsf_detail_1.base_remboursement  
                                            WHEN coefficient = 0.5 THEN v_rsf_detail_1.base_remboursement * 0.5
                                            ELSE round(v_rsf_detail_1.nombre * v_rsf_detail_1.coefficient_mco * v_rsf_detail_1.prix_unitaire,2) 
                               
                                    END 
                               ELSE 0 END
                            ) AS montant_ghs_theorique_fg0,

                            --rehosp_meme_ghm identifie les séjours avec réentree pour meme GHM -> application d'une réduction de 50% sur le deuxieme séjour d'ou coefficeint à O.5 (plus autre coefficients )
                            COUNT(DISTINCT CASE WHEN prestation_code = 'GHS' AND coefficient < 1 AND coefficient <> 0.5 AND rehosp_meme_ghm != 1 THEN v_rsf_detail_1.rss_id ELSE null END) AS nb_sej_exb_fg0,                            
                            SUM(CASE WHEN prestation_code = 'GHS' AND coefficient < 1 AND rehosp_meme_ghm != 1 AND coefficient <> 0.5 THEN v_rsf_detail_1.base_remboursement - round(v_rsf_detail_1.nombre * v_rsf_detail_1.coefficient_mco * v_rsf_detail_1.prix_unitaire,2) ELSE 0 END) AS montant_exb_fg0,
                                                        
                            SUM(CASE WHEN prestation_code = 'EXH' THEN nombre ELSE 0 END) as nb_exh_fg0,
                            SUM(CASE WHEN prestation_code = 'EXH' THEN v_rsf_detail_1.base_remboursement ELSE 0 END) as montant_exh_fg0,
                            SUM(CASE WHEN prestation_code = 'SRC' THEN nombre ELSE 0 END) as nb_src_fg0,
                            SUM(CASE WHEN prestation_code = 'SRC' THEN v_rsf_detail_1.base_remboursement ELSE 0 END) as montant_src_fg0,
                            SUM(CASE WHEN prestation_code IN ('REA', 'SRA') THEN nombre ELSE 0 END) as nb_rea_fg0,
                            SUM(CASE WHEN prestation_code IN ('REA', 'SRA') THEN v_rsf_detail_1.base_remboursement ELSE 0 END) as montant_rea_fg0,
                            SUM(CASE WHEN prestation_code = 'STF' THEN nombre ELSE 0 END) as nb_stf_fg0,
                            SUM(CASE WHEN prestation_code = 'STF' THEN v_rsf_detail_1.base_remboursement ELSE 0 END) as montant_stf_fg0,
                            SUM(CASE WHEN prestation_code = 'NN1' THEN nombre ELSE 0 END) as nb_nn1_fg0,
                            SUM(CASE WHEN prestation_code = 'NN1' THEN v_rsf_detail_1.base_remboursement ELSE 0 END) as montant_nn1_fg0,
                            SUM(CASE WHEN prestation_code = 'NN2' THEN nombre ELSE 0 END) as nb_nn2_fg0,
                            SUM(CASE WHEN prestation_code = 'NN2' THEN v_rsf_detail_1.base_remboursement ELSE 0 END) as montant_nn2_fg0,
                            SUM(CASE WHEN prestation_code = 'NN3' THEN nombre ELSE 0 END) as nb_nn3_fg0,
                            SUM(CASE WHEN prestation_code = 'NN3' THEN v_rsf_detail_1.base_remboursement ELSE 0 END) as montant_nn3_fg0,
                            SUM(CASE WHEN prestation_code = 'ANT' THEN nombre ELSE 0 END) as nb_ant_fg0,
                            SUM(CASE WHEN prestation_code = 'ANT' THEN v_rsf_detail_1.base_remboursement ELSE 0 END) as montant_ant_fg0,
                            SUM(CASE WHEN prestation_code = 'RAP' THEN nombre ELSE 0 END) as nb_rap_fg0,
                            SUM(CASE WHEN prestation_code = 'RAP' THEN v_rsf_detail_1.base_remboursement ELSE 0 END) as montant_rap_fg0,
                            SUM(CASE WHEN prestation_code = 'GHS' AND v_rss_1.ghs_id != v_rsf_detail_1.ghs_id AND ghs_code = '9605' THEN nombre ELSE 0 END) as nb_9605_fg0,
                            SUM(CASE WHEN prestation_code = 'GHS' AND v_rss_1.ghs_id != v_rsf_detail_1.ghs_id AND ghs_code = '9605' THEN v_rsf_detail_1.base_remboursement ELSE 0 END) as montant_9605_fg0,
                            SUM(CASE WHEN prestation_code = 'GHS' AND v_rss_1.ghs_id != v_rsf_detail_1.ghs_id AND ghs_code = '9604' THEN nombre ELSE 0 END) as nb_9604_fg0,
                            SUM(CASE WHEN prestation_code = 'GHS' AND v_rss_1.ghs_id != v_rsf_detail_1.ghs_id AND ghs_code = '9604' THEN v_rsf_detail_1.base_remboursement ELSE 0 END) as montant_9604_fg0,
                            SUM(CASE WHEN prestation_code = 'SDC' THEN nombre ELSE 0 END) as nb_sdc_fg0,
                            SUM(CASE WHEN prestation_code = 'SDC' THEN v_rsf_detail_1.base_remboursement ELSE 0 END) as montant_sdc_fg0,
                            SUM(CASE WHEN prestation_code = 'TDE' THEN nombre ELSE 0 END) as nb_tde_fg0,
                            SUM(CASE WHEN prestation_code = 'TDE' THEN v_rsf_detail_1.base_remboursement ELSE 0 END) as montant_tde_fg0,
                            SUM(CASE WHEN prestation_code = 'TSE' THEN nombre ELSE 0 END) as nb_tse_fg0,
                            SUM(CASE WHEN prestation_code = 'TSE' THEN v_rsf_detail_1.base_remboursement ELSE 0 END) as montant_tse_fg0,
                            MAX(CASE WHEN prestation_code = 'GHS' AND coefficient = 0.5 THEN 0.5 ELSE 1 END) AS coefficient
                            
                        FROM pmsi.v_rss_7 AS v_rss_1
                        JOIN pmsi.v_rsf_detail_1 ON v_rsf_detail_1.rss_id = v_rss_1.rss_id
                        WHERE 1=1
                            AND date_sortie BETWEEN '[DATE_1]' AND '[DATE_4]'
                            AND prestation_code IN ('GHS', 'EXH', 'SRC', 'STF', 'REA', 'SRA', 'NN1', 'NN2', 'NN3', 'ANT', 'RAP', 'SDC', 'TDE', 'TSE')
                            AND en_cours = '0'
                        GROUP BY v_rsf_detail_1.rss_id
                        ) AS subview
                    WHERE subview.rss_id = [simulationTableFG].rss_id
                    .,



            ]]></sqlcmd>
    </NODE>
    
    <NODE name="ETAPE_4" label="Positionnement des valeurs FG1 9605 et 9604 d'apres FG0">
            <sqlcmd><![CDATA[

                    UPDATE pmsi.[simulationTableFG] 
                    SET nb_9605_fg1 = nb_9605_fg0
                    .,
                    UPDATE pmsi.[simulationTableFG] 
                    SET nb_9604_fg1 = nb_9604_fg0
                    .,

            ]]></sqlcmd>
    </NODE>

    <NODE name="ETAPE_5" label="Prestation inter-etablissement">
            <sqlcmd><![CDATA[

                    UPDATE pmsi.[simulationTableFG] 
                        SET 
                            est_envoye = '9', 
                            est_regroupe = '0',
                            commentaire = 'Prestation inter-etablissement'
                        FROM pmsi.p_rss
                        WHERE p_rss.oid = [simulationTableFG].rss_id
                            AND est_envoye <> '9'
                            AND mode_entree = '0' AND mode_sortie = '0'
                    .,       

            ]]></sqlcmd>
    </NODE>

    <NODE name="ETAPE_6" label="Montant GHS non disponible en FG N-1">
            <sqlcmd><![CDATA[

                UPDATE pmsi.[simulationTableFG] 
                    SET 
                        est_envoye = '9', 
                        est_regroupe = '0',
                        commentaire = 'Montant GHS non disponible en FG N-1'
                    FROM pmsi.t_ghm t_ghm_fg0
                    WHERE ghm_fg0_id = t_ghm_fg0.oid
                        AND est_envoye <> '9'
                        AND montant_ghs_theorique_fg0 = 0 and t_ghm_fg0.code not like '15%'
                .,
            ]]></sqlcmd>
    </NODE>

    <NODE name="ETAPE_7" label="Numéro de RSS en double dans l'année.">
        <sqlcmd><![CDATA[


                UPDATE pmsi.[simulationTableFG] 
                    SET 
                        est_envoye = '9', 
                        est_regroupe = '0',
                        commentaire = 'Numéro de RSS en double dans l''année.'
                    WHERE 
                        no_rss  IN (
                            SELECT no_rss 
                            FROM pmsi.p_rss 
                            WHERE date_sortie BETWEEN '[DATE_1]' AND '[DATE_2]'
                            GROUP BY no_rss 
                            HAVING count(no_rss) > 1
                            )
                        AND annee_sortie =  ([FG_YEAR] - 1) AND est_envoye <> '9'
                .,       
            
                UPDATE pmsi.[simulationTableFG] 
                SET 
                    est_envoye = '9', 
                    est_regroupe = '0',
                    commentaire = 'Numéro de RSS en double dans l''année.'
                WHERE 
                    no_rss  IN (
                        SELECT no_rss 
                        FROM pmsi.p_rss 
                        WHERE date_sortie BETWEEN '[DATE_3]' AND '[DATE_4]'
                        GROUP BY no_rss 
                        HAVING count(no_rss) > 1
                        )
                    AND annee_sortie = [FG_YEAR] AND est_envoye <> '9'
                .,

        ]]></sqlcmd>
    </NODE>

    <NODE name="ETAPE_8" label="Séances dialyse non traitées">
            <sqlcmd><![CDATA[

                UPDATE pmsi.[simulationTableFG]
                    SET commentaire = 'Séances dialyse non traitées'
                WHERE 
                    annee_sortie = ([FG_YEAR] - 1) 
                    AND est_envoye <> '1' 
                    AND no_rss IN (
                        SELECT 
                            p_rsf_detail.no_rss 
                        FROM pmsi.p_rsf_detail 
                        JOIN pmsi.p_rss ON p_rss.oid = p_rsf_detail.rss_id 
                        WHERE 
                            date_sortie BETWEEN '[DATE_1]' AND '[DATE_2]' 
                            AND prestation_id IN (
                                SELECT oid 
                                FROM pmsi.t_prestations 
                                WHERE code LIKE 'D%'
                            )
                    )
                .,

                UPDATE pmsi.[simulationTableFG]
                SET commentaire = 'Séances dialyse non traitées'
                WHERE 
                    annee_sortie = [FG_YEAR]
                    AND est_envoye <> '1' 
                    AND no_rss IN (
                        SELECT p_rsf_detail.no_rss 
                        FROM pmsi.p_rsf_detail 
                        JOIN pmsi.p_rss ON p_rss.oid = p_rsf_detail.rss_id 
                        WHERE 
                            date_sortie BETWEEN '[DATE_3]' AND '[DATE_4]' 
                            AND prestation_id IN (
                                SELECT oid 
                                FROM pmsi.t_prestations 
                                WHERE code LIKE 'D%'
                            )
                    )
                .,

            ]]></sqlcmd>
    </NODE>

    <NODE name="ETAPE_9" label="récupération des montant UCD (SUS)">
            <sqlcmd><![CDATA[

                UPDATE pmsi.[simulationTableFG]
                SET 
                montant_ucd_fg0 = subview.montant_ucd_fg0,
                montant_emi_fg0 = subview.montant_emi_fg0
                FROM (
                    SELECT 
                        p_rss_ucd.rss_id,
                        SUM(montant_facture) as montant_ucd_fg0,
                        SUM(montant_ecart_indemnisable) as montant_emi_fg0
                    FROM pmsi.v_rss_7 v_rss_1 
                    ,pmsi.p_rss_ucd
                    ,pmsi.t_type_ucd
                    WHERE date_sortie BETWEEN '[DATE_1]' AND '[DATE_4]' 
                        AND p_rss_ucd.rss_id = v_rss_1.rss_id
                        AND t_type_ucd.oid = p_rss_ucd.type_ucd_id
                        AND t_type_ucd.code = 'SUS'
                        -- à comenter si pb de table v_ucd absente type_ucd_code = 'SUS'                       
                        AND en_cours = '0'
                    GROUP BY p_rss_ucd.rss_id
                ) subview
                WHERE subview.rss_id = [simulationTableFG].rss_id
                    AND est_envoye <> '9'
                .,

            ]]></sqlcmd>
    </NODE>

    <NODE name="ETAPE_10" label="récupération des montant LPP">
            <sqlcmd><![CDATA[

                UPDATE pmsi.[simulationTableFG]
                    SET 
                    montant_lpp_fg0 = subview.montant_lpp_fg0
                    --,montant_eti_fg0 = subview.montant_eti_fg0
                    FROM (
                        SELECT p_rss_lpp.rss_id,
                            SUM(montant_facture) as montant_lpp_fg0
                            --,SUM(montant_ecart_indemnisable) as montant_eti_fg0
                        FROM pmsi.v_rss_7 v_rss_1, pmsi.p_rss_lpp
                        WHERE date_sortie BETWEEN '[DATE_1]' AND '[DATE_4]' 
                            AND p_rss_lpp.rss_id = v_rss_1.rss_id
                            AND en_cours = '0'
                        GROUP BY p_rss_lpp.rss_id
                    ) subview
                    WHERE subview.rss_id = [simulationTableFG].rss_id
                        AND est_envoye <> '9'
                .,

              -- Permet de récupérer les montants ETI si besoin
              --  UPDATE pmsi.[simulationTableFG]
              --      SET 
              --      montant_eti_fg0 = subview.montant_eti_fg0
              --      FROM (
              --          SELECT 
              --              SUM(v_rsf_detail_1.sejour_facture) AS montant_eti_fg0,
              --              v_rsf_detail_1.rss_id
              --          FROM  pmsi.v_rsf_detail_1
              --          JOIN pmsi.t_prestations ON prestation_id = t_prestations.oid
              --          where prestation_code ilike 'ETI'
              --          GROUP BY v_rsf_detail_1.rss_id
              --      ) subview
              --      WHERE subview.rss_id = [simulationTableFG].rss_id
              --          AND est_envoye <> '9'
              --  .,

            ]]></sqlcmd>
    </NODE>

    <NODE name="ETAPE_11" label="Alimentation des données SIMULATION">
            <sqlcmd><![CDATA[
				
				DROP TABLE IF EXISTS w_intable;
                CREATE TEMP TABLE w_intable (indata text, indata_array text[])
                .,
                
                COPY w_intable (indata)
                FROM '[realpathfileFG13]'
                .,

                UPDATE w_intable
                SET indata = replace(indata,'"','')
                WHERE indata LIKE '%"%'
                .,

                UPDATE w_intable
                SET indata_array = string_to_array(indata,';')
				.,

                DROP TABLE IF EXISTS [tempTableFG]
                .,
                CREATE TEMP TABLE [tempTableFG] AS
                SELECT 
					base.cti_to_number(indata_array[1]) AS annee_sortie,
                    base.cti_to_number(indata_array[2])::bigint AS no_rss,
                    indata_array[3]::text AS ghm_fg1_code,
                    base.cti_to_number(indata_array[4])::bigint AS ghs_fg1_code,
                    indata_array[5]::text AS erreur,
                    indata_array[6]::text AS commentaire,
                    base.cti_to_number(indata_array[11])::bigint AS nb_exh_fg1,
                    base.cti_to_number(indata_array[12])::bigint AS nb_exb_fg1,
                    base.cti_to_number(indata_array[13])::bigint AS nb_rea_fg1,
                    base.cti_to_number(indata_array[14])::bigint AS  nb_rep_fg1,
                    base.cti_to_number(indata_array[15])::bigint AS nb_src_fg1,
                    base.cti_to_number(indata_array[16])::bigint AS nb_stf_rea_fg1,
                    base.cti_to_number(indata_array[17])::bigint AS nb_stf_fg1,
                    base.cti_to_number(indata_array[18])::bigint AS nb_nn1_fg1,
                    base.cti_to_number(indata_array[19])::bigint AS nb_nn2_fg1,
                    base.cti_to_number(indata_array[20])::bigint AS nb_nn3_fg1,
                    base.cti_to_number(indata_array[21])::bigint AS nb_ant_fg1,
                    base.cti_to_number(indata_array[22])::bigint AS nb_rap_fg1,
                    indata_array[23]::text AS ghs_minore_code,
                    base.cti_to_number(indata_array[24])::bigint AS nb_sdc_fg1,
                    ''::text AS is_raac,
                    0::numeric AS nb_tde_fg1,
                    0::numeric AS nb_tds_fg1
                FROM w_intable
                WHERE length(indata_array[1]) = 4 AND
					indata_array[1] LIKE '20%'
			.,

            ]]></sqlcmd>
    </NODE>

    <NODE name="ETAPE_12" label="Simulation RAAC">
            <sqlcmd><![CDATA[



                DROP TABLE IF EXISTS [tempTableFG]_raac
                .,

                CREATE TEMP TABLE [tempTableFG]_raac
                AS 
                SELECT 
                    * 
                FROM [tempTableFG]
                WHERE TRUE 
                    AND ghm_fg1_code IN ( '04C041',
                                        '06C031',
                                        '06C041',
                                        '06C071',
                                        '06C161',
                                        '07C091',
                                        '08C241',
                                        '08C251',
                                        '08C271',
                                        '08C481',
                                        '08C521',
                                        '10C131',
                                        '11C021',
                                        '12C111',
                                        '13C031',
                                        '13C141',
                                        '13C151',
										'04C021',
										'08C021'
										)
                    AND is_raac = 'is_raac' 
                .,

                UPDATE [tempTableFG]_raac
                SET 
                    ghm_fg1_code = subq1.new_ghm_severite2,
                    ghs_fg1_code = subq1.new_ghs_severite2,
                    is_raac = 'is_raac_severite_augmentee'
                FROM (
                        SELECT  
                            t_ghm_c.code as new_ghm_severite2,  
                            substr(t_ghm_c.code, 0, 6)|| '1' AS ghm_code_initial,  
                            t_ghs.code AS new_ghs_severite2 
                        FROM pmsi.t_ghs
                        JOIN pmsi.t_ghm_c ON ghm_id = t_ghm_c.oid
                        WHERE t_ghm_c.code IN ( '04C042',
                                                '06C032',
                                                '06C042',
                                                '06C072',
                                                '06C162',
                                                '07C092',
                                                '08C242',
                                                '08C252',
                                                '08C272',
                                                '08C482',
                                                '08C522',
                                                '10C132',
                                                '11C022',
                                                '12C112',
                                                '13C032',
                                                '13C142',
                                                '13C152',
												'04C022',
												'08C022' )
                    ) subq1
                WHERE subq1.ghm_code_initial = [tempTableFG]_raac.ghm_fg1_code
                .,


                INSERT INTO [tempTableFG] (
                    annee_sortie,
                    no_rss,
                    ghm_fg1_code,
                    ghs_fg1_code,
                    erreur,
                    commentaire,
                    nb_exh_fg1,
                    nb_exb_fg1,
                    nb_rea_fg1,
                    nb_rep_fg1,
                    nb_src_fg1,
                    nb_stf_rea_fg1,
                    nb_stf_fg1,
                    nb_nn1_fg1,
                    nb_nn2_fg1,
                    nb_nn3_fg1,
                    nb_ant_fg1,
                    nb_rap_fg1,
                    ghs_minore_code,
                    nb_sdc_fg1,
                    is_raac
                )
                SELECT 
                    annee_sortie,
                    no_rss + 2000000000,
                    ghm_fg1_code,
                    ghs_fg1_code,
                    erreur,
                    commentaire,
                    nb_exh_fg1,
                    nb_exb_fg1,
                    nb_rea_fg1,
                    nb_rep_fg1,
                    nb_src_fg1,
                    nb_stf_rea_fg1,
                    nb_stf_fg1,
                    nb_nn1_fg1,
                    nb_nn2_fg1,
                    nb_nn3_fg1,
                    nb_ant_fg1,
                    nb_rap_fg1,
                    ghs_minore_code,
                    nb_sdc_fg1,
                    is_raac
                FROM [tempTableFG]_raac
                GROUP BY
                    annee_sortie,
                    no_rss,
                    ghm_fg1_code,
                    ghs_fg1_code,
                    erreur,
                    commentaire,
                    nb_exh_fg1,
                    nb_exb_fg1,
                    nb_rea_fg1,
                    nb_rep_fg1,
                    nb_src_fg1,
                    nb_stf_rea_fg1,
                    nb_stf_fg1,
                    nb_nn1_fg1,
                    nb_nn2_fg1,
                    nb_nn3_fg1,
                    nb_ant_fg1,
                    nb_rap_fg1,
                    ghs_minore_code,
                    nb_sdc_fg1,
                    is_raac
                .,

                DROP TABLE IF EXISTS [simulationTableFG]_raac
                .,

                CREATE TEMP TABLE [simulationTableFG]_raac
                AS 
                SELECT 
                    [simulationTableFG].* 
                FROM pmsi.[simulationTableFG]
                JOIN [tempTableFG]_raac ON [tempTableFG]_raac.no_rss = [simulationTableFG].no_rss
                .,

                UPDATE [simulationTableFG]_raac
                SET no_rss = no_rss + 2000000000
                .,

                INSERT INTO pmsi.[simulationTableFG] (
                        annee_sortie, coefficient_mco_fg0, coefficient_mco_fg1, commentaire, 
                        erreur, est_envoye, est_regroupe, finess, ghm_fg0_id, ghm_fg1_code, 
                        ghm_fg1_id, ghs_fg0_id, ghs_fg1_borne_basse, ghs_fg1_borne_haute, 
                        ghs_fg1_code, ghs_fg1_forfait_exb, ghs_fg1_id, ghs_fg1_tarif_exb, 
                        ghs_fg1_tarif_exh, ghs_fg1_tarif_ghs, ghs_minore_code, ghs_minore_text, 
                        montant_9604_fg0, montant_9604_fg1, montant_9605_fg0, montant_9605_fg1, 
                        montant_ant_fg0, montant_ant_fg1, montant_emi_fg0, montant_emi_fg1, 
                        montant_emi_fg1_deremboursees, montant_eti_fg0, montant_eti_fg1, 
                        montant_eti_fg1_deremboursees, montant_exb_fg0, montant_exb_fg1, 
                        montant_exh_fg0, montant_exh_fg1, montant_ghs_theorique_fg0, 
                        montant_ghs_theorique_fg1, montant_lpp_fg0, montant_lpp_fg1, 
                        montant_lpp_fg1_deremboursees, montant_nn1_fg0, montant_nn1_fg1, 
                        montant_nn2_fg0, montant_nn2_fg1, montant_nn3_fg0, montant_nn3_fg1, 
                        montant_rap_fg0, montant_rap_fg1, montant_rea_fg0, montant_rea_fg1, 
                        montant_rep_fg0, montant_rep_fg1, montant_sdc_fg0, montant_sdc_fg1, 
                        montant_tde_fg0, montant_tde_fg1, montant_tse_fg0, montant_tse_fg1, 
                        montant_src_eligibles_fg1, montant_src_fg0, montant_src_fg1, 
                        montant_stf_fg0, montant_stf_fg1, montant_ucd_fg0, montant_ucd_fg1, 
                        montant_ucd_fg1_deremboursees, nb_9604_fg0, nb_9604_fg1, nb_9605_fg0, 
                        nb_9605_fg1, nb_ant_fg0, nb_ant_fg1, nb_exb_fg1, nb_exh_fg0, 
                        nb_exh_fg1, nb_nn1_fg0, nb_nn1_fg1, nb_nn2_fg0, nb_nn2_fg1, nb_nn3_fg0, 
                        nb_nn3_fg1, nb_rap_fg0, nb_rap_fg1, nb_rea_fg0, nb_rea_fg1, nb_rep_fg0, 
                        nb_rep_fg1, nb_sdc_fg0, nb_sdc_fg1, nb_tde_fg0, nb_tde_fg1, nb_tse_fg0, 
                        nb_tse_fg1, nb_sej_exb_fg0, nb_sej_exb_fg1, nb_sej_src_eligibles_fg1, 
                        nb_src_eligibles_fg1, nb_src_fg0, nb_src_fg1, nb_stf_fg0, nb_stf_fg1, 
                        nb_ucd_fg0, no_rss, regle_src_eligible_fg1, rss_id, tarif_9604_fg1, 
                        tarif_9605_fg1, tarif_ant_fg1, tarif_nn1_fg1, tarif_nn2_fg1, 
                        tarif_nn3_fg1, tarif_rap_fg1, tarif_rea_fg1, tarif_sdc_fg0, tarif_sdc_fg1, 
                        tarif_tde_fg0, tarif_tde_fg1, tarif_tse_fg0, tarif_tse_fg1, tarif_src_fg1, 
                        tarif_stf_fg1, translation, is_raac
                )
                SELECT 
                        annee_sortie, coefficient_mco_fg0, coefficient_mco_fg1, commentaire, 
                        erreur, est_envoye, est_regroupe, finess, ghm_fg0_id, ghm_fg1_code, 
                        ghm_fg1_id, ghs_fg0_id, ghs_fg1_borne_basse, ghs_fg1_borne_haute, 
                        ghs_fg1_code, ghs_fg1_forfait_exb, ghs_fg1_id, ghs_fg1_tarif_exb, 
                        ghs_fg1_tarif_exh, ghs_fg1_tarif_ghs, ghs_minore_code, ghs_minore_text, 
                        montant_9604_fg0, montant_9604_fg1, montant_9605_fg0, montant_9605_fg1, 
                        montant_ant_fg0, montant_ant_fg1, montant_emi_fg0, montant_emi_fg1, 
                        montant_emi_fg1_deremboursees, montant_eti_fg0, montant_eti_fg1, 
                        montant_eti_fg1_deremboursees, montant_exb_fg0, montant_exb_fg1, 
                        montant_exh_fg0, montant_exh_fg1, montant_ghs_theorique_fg0, 
                        montant_ghs_theorique_fg1, montant_lpp_fg0, montant_lpp_fg1, 
                        montant_lpp_fg1_deremboursees, montant_nn1_fg0, montant_nn1_fg1, 
                        montant_nn2_fg0, montant_nn2_fg1, montant_nn3_fg0, montant_nn3_fg1, 
                        montant_rap_fg0, montant_rap_fg1, montant_rea_fg0, montant_rea_fg1, 
                        montant_rep_fg0, montant_rep_fg1, montant_sdc_fg0, montant_sdc_fg1, 
                        montant_tde_fg0, montant_tde_fg1, montant_tse_fg0, montant_tse_fg1, 
                        montant_src_eligibles_fg1, montant_src_fg0, montant_src_fg1, 
                        montant_stf_fg0, montant_stf_fg1, montant_ucd_fg0, montant_ucd_fg1, 
                        montant_ucd_fg1_deremboursees, nb_9604_fg0, nb_9604_fg1, nb_9605_fg0, 
                        nb_9605_fg1, nb_ant_fg0, nb_ant_fg1, nb_exb_fg1, nb_exh_fg0, 
                        nb_exh_fg1, nb_nn1_fg0, nb_nn1_fg1, nb_nn2_fg0, nb_nn2_fg1, nb_nn3_fg0, 
                        nb_nn3_fg1, nb_rap_fg0, nb_rap_fg1, nb_rea_fg0, nb_rea_fg1, nb_rep_fg0, 
                        nb_rep_fg1, nb_sdc_fg0, nb_sdc_fg1, nb_tde_fg0, nb_tde_fg1, nb_tse_fg0, 
                        nb_tse_fg1, nb_sej_exb_fg0, nb_sej_exb_fg1, nb_sej_src_eligibles_fg1, 
                        nb_src_eligibles_fg1, nb_src_fg0, nb_src_fg1, nb_stf_fg0, nb_stf_fg1, 
                        nb_ucd_fg0, no_rss, regle_src_eligible_fg1, rss_id, tarif_9604_fg1, 
                        tarif_9605_fg1, tarif_ant_fg1, tarif_nn1_fg1, tarif_nn2_fg1, 
                        tarif_nn3_fg1, tarif_rap_fg1, tarif_rea_fg1, tarif_sdc_fg0, tarif_sdc_fg1, 
                        tarif_tde_fg0, tarif_tde_fg1, tarif_tse_fg0, tarif_tse_fg1, tarif_src_fg1, 
                        tarif_stf_fg1, translation, is_raac
                    FROM [simulationTableFG]_raac
                .,

            ]]></sqlcmd>
    </NODE>

    <NODE name="ETAPE_14" label="récupération nb TDE et TSE depuis factures RSF">
            <sqlcmd><![CDATA[

                UPDATE pmsi.[simulationTableFG]
                    SET 
                    nb_tde_fg1 = subview.nb_tde_fg0,
                    nb_tse_fg1 = subview.nb_tse_fg0
                    FROM (
                                SELECT 
                                    rss_id,
                                    SUM (CASE WHEN prestation_code = 'TDE' THEN 1 ELSE 0 END) AS nb_tde_fg0,
                                    SUM (CASE WHEN prestation_code = 'TSE' THEN 1 ELSE 0 END) AS nb_tse_fg0
                                FROM pmsi.v_rsf_detail_1 
                                WHERE 1=1
                                    AND prestation_code IN  ( 'TDE', 'TSE') 
                                    AND date_fin BETWEEN '[DATE_1]' AND '[DATE_4]'
                                GROUP BY rss_id
                    ) subview
                    WHERE subview.rss_id = [simulationTableFG].rss_id
                        AND est_envoye <> '9'
                .,

            ]]></sqlcmd>
    </NODE>    

    <NODE name="ETAPE_15" label="Positionnement valeurs SUPPLEMENTS FG1 PRIVE">
            <condition><![CDATA[
                "[typeEts]" == "1"
            ]]></condition>
            <sqlcmd><![CDATA[


                        DROP TABLE IF EXISTS [tempTableSuppl]
                        .,
                        
                        CREATE TEMP TABLE [tempTableSuppl] AS
                            SELECT 
                                MAX(
                                    CASE WHEN code = 'SRC' THEN
                                        CASE   
                                            WHEN '[DATE_5]' >= t_prestations.date_debut_prive_1 AND '[DATE_5]' <= t_prestations.date_fin_prive_1 THEN t_prestations.tarif_prive_1   
                                            WHEN '[DATE_5]' >= t_prestations.date_debut_prive_2 AND '[DATE_5]' <= t_prestations.date_fin_prive_2 THEN t_prestations.tarif_prive_2   
                                            WHEN '[DATE_5]' >= t_prestations.date_debut_prive_3 AND '[DATE_5]' <= t_prestations.date_fin_prive_3 THEN t_prestations.tarif_prive_3   
                                            WHEN '[DATE_5]' >= t_prestations.date_debut_prive_4 AND '[DATE_5]' <= t_prestations.date_fin_prive_4 THEN t_prestations.tarif_prive_4   
                                            WHEN '[DATE_5]' >= t_prestations.date_debut_prive_5 AND '[DATE_5]' <= t_prestations.date_fin_prive_5 THEN t_prestations.tarif_prive_5   
                                            WHEN '[DATE_5]' >= t_prestations.date_debut_prive_6 AND '[DATE_5]' <= t_prestations.date_fin_prive_6 THEN t_prestations.tarif_prive_6   
                                            WHEN '[DATE_5]' >= t_prestations.date_debut_prive_7 AND '[DATE_5]' <= t_prestations.date_fin_prive_7 THEN t_prestations.tarif_prive_7   
                                            WHEN '[DATE_5]' >= t_prestations.date_debut_prive_8 AND '[DATE_5]' <= t_prestations.date_fin_prive_8 THEN t_prestations.tarif_prive_8   
                                            WHEN '[DATE_5]' >= t_prestations.date_debut_prive_9 AND '[DATE_5]' <= t_prestations.date_fin_prive_9 THEN t_prestations.tarif_prive_9   
                                            ELSE 0::numeric   
                                        END 
                                        ELSE 0 
                                    END
                                ) AS tarif_src_fg1,
                                MAX(
                                    CASE WHEN code = 'STF' THEN
                                        CASE   
                                            WHEN '[DATE_5]' >= t_prestations.date_debut_prive_1 AND '[DATE_5]' <= t_prestations.date_fin_prive_1 THEN t_prestations.tarif_prive_1   
                                            WHEN '[DATE_5]' >= t_prestations.date_debut_prive_2 AND '[DATE_5]' <= t_prestations.date_fin_prive_2 THEN t_prestations.tarif_prive_2   
                                            WHEN '[DATE_5]' >= t_prestations.date_debut_prive_3 AND '[DATE_5]' <= t_prestations.date_fin_prive_3 THEN t_prestations.tarif_prive_3   
                                            WHEN '[DATE_5]' >= t_prestations.date_debut_prive_4 AND '[DATE_5]' <= t_prestations.date_fin_prive_4 THEN t_prestations.tarif_prive_4   
                                            WHEN '[DATE_5]' >= t_prestations.date_debut_prive_5 AND '[DATE_5]' <= t_prestations.date_fin_prive_5 THEN t_prestations.tarif_prive_5   
                                            WHEN '[DATE_5]' >= t_prestations.date_debut_prive_6 AND '[DATE_5]' <= t_prestations.date_fin_prive_6 THEN t_prestations.tarif_prive_6   
                                            WHEN '[DATE_5]' >= t_prestations.date_debut_prive_7 AND '[DATE_5]' <= t_prestations.date_fin_prive_7 THEN t_prestations.tarif_prive_7   
                                            WHEN '[DATE_5]' >= t_prestations.date_debut_prive_8 AND '[DATE_5]' <= t_prestations.date_fin_prive_8 THEN t_prestations.tarif_prive_8   
                                            WHEN '[DATE_5]' >= t_prestations.date_debut_prive_9 AND '[DATE_5]' <= t_prestations.date_fin_prive_9 THEN t_prestations.tarif_prive_9   
                                            ELSE 0::numeric   
                                        END 
                                        ELSE 0 
                                    END
                                ) AS tarif_stf_fg1,
                                MAX(
                                    CASE WHEN code = prestation_rea_code THEN
                                        CASE   
                                            WHEN '[DATE_5]' >= t_prestations.date_debut_prive_1 AND '[DATE_5]' <= t_prestations.date_fin_prive_1 THEN t_prestations.tarif_prive_1   
                                            WHEN '[DATE_5]' >= t_prestations.date_debut_prive_2 AND '[DATE_5]' <= t_prestations.date_fin_prive_2 THEN t_prestations.tarif_prive_2   
                                            WHEN '[DATE_5]' >= t_prestations.date_debut_prive_3 AND '[DATE_5]' <= t_prestations.date_fin_prive_3 THEN t_prestations.tarif_prive_3   
                                            WHEN '[DATE_5]' >= t_prestations.date_debut_prive_4 AND '[DATE_5]' <= t_prestations.date_fin_prive_4 THEN t_prestations.tarif_prive_4   
                                            WHEN '[DATE_5]' >= t_prestations.date_debut_prive_5 AND '[DATE_5]' <= t_prestations.date_fin_prive_5 THEN t_prestations.tarif_prive_5   
                                            WHEN '[DATE_5]' >= t_prestations.date_debut_prive_6 AND '[DATE_5]' <= t_prestations.date_fin_prive_6 THEN t_prestations.tarif_prive_6   
                                            WHEN '[DATE_5]' >= t_prestations.date_debut_prive_7 AND '[DATE_5]' <= t_prestations.date_fin_prive_7 THEN t_prestations.tarif_prive_7   
                                            WHEN '[DATE_5]' >= t_prestations.date_debut_prive_8 AND '[DATE_5]' <= t_prestations.date_fin_prive_8 THEN t_prestations.tarif_prive_8   
                                            WHEN '[DATE_5]' >= t_prestations.date_debut_prive_9 AND '[DATE_5]' <= t_prestations.date_fin_prive_9 THEN t_prestations.tarif_prive_9   
                                            ELSE 0::numeric   
                                        END 
                                        ELSE 0 
                                    END
                                ) AS tarif_rea_fg1,
                                MAX(
                                    CASE WHEN code = 'NN1' THEN
                                        CASE   
                                            WHEN '[DATE_5]' >= t_prestations.date_debut_prive_1 AND '[DATE_5]' <= t_prestations.date_fin_prive_1 THEN t_prestations.tarif_prive_1   
                                            WHEN '[DATE_5]' >= t_prestations.date_debut_prive_2 AND '[DATE_5]' <= t_prestations.date_fin_prive_2 THEN t_prestations.tarif_prive_2   
                                            WHEN '[DATE_5]' >= t_prestations.date_debut_prive_3 AND '[DATE_5]' <= t_prestations.date_fin_prive_3 THEN t_prestations.tarif_prive_3   
                                            WHEN '[DATE_5]' >= t_prestations.date_debut_prive_4 AND '[DATE_5]' <= t_prestations.date_fin_prive_4 THEN t_prestations.tarif_prive_4   
                                            WHEN '[DATE_5]' >= t_prestations.date_debut_prive_5 AND '[DATE_5]' <= t_prestations.date_fin_prive_5 THEN t_prestations.tarif_prive_5   
                                            WHEN '[DATE_5]' >= t_prestations.date_debut_prive_6 AND '[DATE_5]' <= t_prestations.date_fin_prive_6 THEN t_prestations.tarif_prive_6   
                                            WHEN '[DATE_5]' >= t_prestations.date_debut_prive_7 AND '[DATE_5]' <= t_prestations.date_fin_prive_7 THEN t_prestations.tarif_prive_7   
                                            WHEN '[DATE_5]' >= t_prestations.date_debut_prive_8 AND '[DATE_5]' <= t_prestations.date_fin_prive_8 THEN t_prestations.tarif_prive_8   
                                            WHEN '[DATE_5]' >= t_prestations.date_debut_prive_9 AND '[DATE_5]' <= t_prestations.date_fin_prive_9 THEN t_prestations.tarif_prive_9   
                                            ELSE 0::numeric   
                                        END 
                                        ELSE 0 
                                    END
                                ) AS tarif_nn1_fg1,
                                MAX(
                                    CASE WHEN code = 'NN2' THEN
                                        CASE   
                                            WHEN '[DATE_5]' >= t_prestations.date_debut_prive_1 AND '[DATE_5]' <= t_prestations.date_fin_prive_1 THEN t_prestations.tarif_prive_1   
                                            WHEN '[DATE_5]' >= t_prestations.date_debut_prive_2 AND '[DATE_5]' <= t_prestations.date_fin_prive_2 THEN t_prestations.tarif_prive_2   
                                            WHEN '[DATE_5]' >= t_prestations.date_debut_prive_3 AND '[DATE_5]' <= t_prestations.date_fin_prive_3 THEN t_prestations.tarif_prive_3   
                                            WHEN '[DATE_5]' >= t_prestations.date_debut_prive_4 AND '[DATE_5]' <= t_prestations.date_fin_prive_4 THEN t_prestations.tarif_prive_4   
                                            WHEN '[DATE_5]' >= t_prestations.date_debut_prive_5 AND '[DATE_5]' <= t_prestations.date_fin_prive_5 THEN t_prestations.tarif_prive_5   
                                            WHEN '[DATE_5]' >= t_prestations.date_debut_prive_6 AND '[DATE_5]' <= t_prestations.date_fin_prive_6 THEN t_prestations.tarif_prive_6   
                                            WHEN '[DATE_5]' >= t_prestations.date_debut_prive_7 AND '[DATE_5]' <= t_prestations.date_fin_prive_7 THEN t_prestations.tarif_prive_7   
                                            WHEN '[DATE_5]' >= t_prestations.date_debut_prive_8 AND '[DATE_5]' <= t_prestations.date_fin_prive_8 THEN t_prestations.tarif_prive_8   
                                            WHEN '[DATE_5]' >= t_prestations.date_debut_prive_9 AND '[DATE_5]' <= t_prestations.date_fin_prive_9 THEN t_prestations.tarif_prive_9   
                                            ELSE 0::numeric   
                                        END
                                        ELSE 0 
                                    END
                                ) AS tarif_nn2_fg1,
                                MAX(
                                    CASE WHEN code = 'NN3' THEN
                                        CASE   
                                            WHEN '[DATE_5]' >= t_prestations.date_debut_prive_1 AND '[DATE_5]' <= t_prestations.date_fin_prive_1 THEN t_prestations.tarif_prive_1   
                                            WHEN '[DATE_5]' >= t_prestations.date_debut_prive_2 AND '[DATE_5]' <= t_prestations.date_fin_prive_2 THEN t_prestations.tarif_prive_2   
                                            WHEN '[DATE_5]' >= t_prestations.date_debut_prive_3 AND '[DATE_5]' <= t_prestations.date_fin_prive_3 THEN t_prestations.tarif_prive_3   
                                            WHEN '[DATE_5]' >= t_prestations.date_debut_prive_4 AND '[DATE_5]' <= t_prestations.date_fin_prive_4 THEN t_prestations.tarif_prive_4   
                                            WHEN '[DATE_5]' >= t_prestations.date_debut_prive_5 AND '[DATE_5]' <= t_prestations.date_fin_prive_5 THEN t_prestations.tarif_prive_5   
                                            WHEN '[DATE_5]' >= t_prestations.date_debut_prive_6 AND '[DATE_5]' <= t_prestations.date_fin_prive_6 THEN t_prestations.tarif_prive_6   
                                            WHEN '[DATE_5]' >= t_prestations.date_debut_prive_7 AND '[DATE_5]' <= t_prestations.date_fin_prive_7 THEN t_prestations.tarif_prive_7   
                                            WHEN '[DATE_5]' >= t_prestations.date_debut_prive_8 AND '[DATE_5]' <= t_prestations.date_fin_prive_8 THEN t_prestations.tarif_prive_8   
                                            WHEN '[DATE_5]' >= t_prestations.date_debut_prive_9 AND '[DATE_5]' <= t_prestations.date_fin_prive_9 THEN t_prestations.tarif_prive_9   
                                            ELSE 0::numeric   
                                        END
                                        ELSE 0 
                                    END
                                ) AS tarif_nn3_fg1,
                                MAX(
                                    CASE WHEN code = 'ANT' THEN
                                        CASE   
                                            WHEN '[DATE_5]' >= t_prestations.date_debut_prive_1 AND '[DATE_5]' <= t_prestations.date_fin_prive_1 THEN t_prestations.tarif_prive_1   
                                            WHEN '[DATE_5]' >= t_prestations.date_debut_prive_2 AND '[DATE_5]' <= t_prestations.date_fin_prive_2 THEN t_prestations.tarif_prive_2   
                                            WHEN '[DATE_5]' >= t_prestations.date_debut_prive_3 AND '[DATE_5]' <= t_prestations.date_fin_prive_3 THEN t_prestations.tarif_prive_3   
                                            WHEN '[DATE_5]' >= t_prestations.date_debut_prive_4 AND '[DATE_5]' <= t_prestations.date_fin_prive_4 THEN t_prestations.tarif_prive_4   
                                            WHEN '[DATE_5]' >= t_prestations.date_debut_prive_5 AND '[DATE_5]' <= t_prestations.date_fin_prive_5 THEN t_prestations.tarif_prive_5   
                                            WHEN '[DATE_5]' >= t_prestations.date_debut_prive_6 AND '[DATE_5]' <= t_prestations.date_fin_prive_6 THEN t_prestations.tarif_prive_6   
                                            WHEN '[DATE_5]' >= t_prestations.date_debut_prive_7 AND '[DATE_5]' <= t_prestations.date_fin_prive_7 THEN t_prestations.tarif_prive_7   
                                            WHEN '[DATE_5]' >= t_prestations.date_debut_prive_8 AND '[DATE_5]' <= t_prestations.date_fin_prive_8 THEN t_prestations.tarif_prive_8   
                                            WHEN '[DATE_5]' >= t_prestations.date_debut_prive_9 AND '[DATE_5]' <= t_prestations.date_fin_prive_9 THEN t_prestations.tarif_prive_9   
                                            ELSE 0::numeric   
                                        END
                                        ELSE 0 
                                    END
                                ) AS tarif_ant_fg1,
                                MAX(
                                    CASE WHEN code = 'RAP' THEN
                                        CASE   
                                            WHEN '[DATE_5]' >= t_prestations.date_debut_prive_1 AND '[DATE_5]' <= t_prestations.date_fin_prive_1 THEN t_prestations.tarif_prive_1   
                                            WHEN '[DATE_5]' >= t_prestations.date_debut_prive_2 AND '[DATE_5]' <= t_prestations.date_fin_prive_2 THEN t_prestations.tarif_prive_2   
                                            WHEN '[DATE_5]' >= t_prestations.date_debut_prive_3 AND '[DATE_5]' <= t_prestations.date_fin_prive_3 THEN t_prestations.tarif_prive_3   
                                            WHEN '[DATE_5]' >= t_prestations.date_debut_prive_4 AND '[DATE_5]' <= t_prestations.date_fin_prive_4 THEN t_prestations.tarif_prive_4   
                                            WHEN '[DATE_5]' >= t_prestations.date_debut_prive_5 AND '[DATE_5]' <= t_prestations.date_fin_prive_5 THEN t_prestations.tarif_prive_5   
                                            WHEN '[DATE_5]' >= t_prestations.date_debut_prive_6 AND '[DATE_5]' <= t_prestations.date_fin_prive_6 THEN t_prestations.tarif_prive_6   
                                            WHEN '[DATE_5]' >= t_prestations.date_debut_prive_7 AND '[DATE_5]' <= t_prestations.date_fin_prive_7 THEN t_prestations.tarif_prive_7   
                                            WHEN '[DATE_5]' >= t_prestations.date_debut_prive_8 AND '[DATE_5]' <= t_prestations.date_fin_prive_8 THEN t_prestations.tarif_prive_8   
                                            WHEN '[DATE_5]' >= t_prestations.date_debut_prive_9 AND '[DATE_5]' <= t_prestations.date_fin_prive_9 THEN t_prestations.tarif_prive_9   
                                            ELSE 0::numeric   
                                        END 
                                        ELSE 0 
                                    END
                                ) AS tarif_rap_fg1,
                                MAX(
                                    CASE WHEN code = 'SDC' THEN
                                        CASE   
                                            WHEN '[DATE_5]' >= t_prestations.date_debut_prive_1 AND '[DATE_5]' <= t_prestations.date_fin_prive_1 THEN t_prestations.tarif_prive_1   
                                            WHEN '[DATE_5]' >= t_prestations.date_debut_prive_2 AND '[DATE_5]' <= t_prestations.date_fin_prive_2 THEN t_prestations.tarif_prive_2   
                                            WHEN '[DATE_5]' >= t_prestations.date_debut_prive_3 AND '[DATE_5]' <= t_prestations.date_fin_prive_3 THEN t_prestations.tarif_prive_3   
                                            WHEN '[DATE_5]' >= t_prestations.date_debut_prive_4 AND '[DATE_5]' <= t_prestations.date_fin_prive_4 THEN t_prestations.tarif_prive_4   
                                            WHEN '[DATE_5]' >= t_prestations.date_debut_prive_5 AND '[DATE_5]' <= t_prestations.date_fin_prive_5 THEN t_prestations.tarif_prive_5   
                                            WHEN '[DATE_5]' >= t_prestations.date_debut_prive_6 AND '[DATE_5]' <= t_prestations.date_fin_prive_6 THEN t_prestations.tarif_prive_6   
                                            WHEN '[DATE_5]' >= t_prestations.date_debut_prive_7 AND '[DATE_5]' <= t_prestations.date_fin_prive_7 THEN t_prestations.tarif_prive_7   
                                            WHEN '[DATE_5]' >= t_prestations.date_debut_prive_8 AND '[DATE_5]' <= t_prestations.date_fin_prive_8 THEN t_prestations.tarif_prive_8   
                                            WHEN '[DATE_5]' >= t_prestations.date_debut_prive_9 AND '[DATE_5]' <= t_prestations.date_fin_prive_9 THEN t_prestations.tarif_prive_9   
                                            ELSE 0::numeric   
                                        END 
                                        ELSE 0 
                                    END
                                ) AS tarif_sdc_fg1,                                
                                MAX(
                                    CASE WHEN code = 'TDE' THEN
                                        CASE   
                                            WHEN '[DATE_5]' >= t_prestations.date_debut_prive_1 AND '[DATE_5]' <= t_prestations.date_fin_prive_1 THEN t_prestations.tarif_prive_1   
                                            WHEN '[DATE_5]' >= t_prestations.date_debut_prive_2 AND '[DATE_5]' <= t_prestations.date_fin_prive_2 THEN t_prestations.tarif_prive_2   
                                            WHEN '[DATE_5]' >= t_prestations.date_debut_prive_3 AND '[DATE_5]' <= t_prestations.date_fin_prive_3 THEN t_prestations.tarif_prive_3   
                                            WHEN '[DATE_5]' >= t_prestations.date_debut_prive_4 AND '[DATE_5]' <= t_prestations.date_fin_prive_4 THEN t_prestations.tarif_prive_4   
                                            WHEN '[DATE_5]' >= t_prestations.date_debut_prive_5 AND '[DATE_5]' <= t_prestations.date_fin_prive_5 THEN t_prestations.tarif_prive_5   
                                            WHEN '[DATE_5]' >= t_prestations.date_debut_prive_6 AND '[DATE_5]' <= t_prestations.date_fin_prive_6 THEN t_prestations.tarif_prive_6   
                                            WHEN '[DATE_5]' >= t_prestations.date_debut_prive_7 AND '[DATE_5]' <= t_prestations.date_fin_prive_7 THEN t_prestations.tarif_prive_7   
                                            WHEN '[DATE_5]' >= t_prestations.date_debut_prive_8 AND '[DATE_5]' <= t_prestations.date_fin_prive_8 THEN t_prestations.tarif_prive_8   
                                            WHEN '[DATE_5]' >= t_prestations.date_debut_prive_9 AND '[DATE_5]' <= t_prestations.date_fin_prive_9 THEN t_prestations.tarif_prive_9   
                                            ELSE 0::numeric   
                                        END 
                                        ELSE 0 
                                    END
                                ) AS tarif_tde_fg1,                                
                                MAX(
                                    CASE WHEN code = 'TSE' THEN
                                        CASE   
                                            WHEN '[DATE_5]' >= t_prestations.date_debut_prive_1 AND '[DATE_5]' <= t_prestations.date_fin_prive_1 THEN t_prestations.tarif_prive_1   
                                            WHEN '[DATE_5]' >= t_prestations.date_debut_prive_2 AND '[DATE_5]' <= t_prestations.date_fin_prive_2 THEN t_prestations.tarif_prive_2   
                                            WHEN '[DATE_5]' >= t_prestations.date_debut_prive_3 AND '[DATE_5]' <= t_prestations.date_fin_prive_3 THEN t_prestations.tarif_prive_3   
                                            WHEN '[DATE_5]' >= t_prestations.date_debut_prive_4 AND '[DATE_5]' <= t_prestations.date_fin_prive_4 THEN t_prestations.tarif_prive_4   
                                            WHEN '[DATE_5]' >= t_prestations.date_debut_prive_5 AND '[DATE_5]' <= t_prestations.date_fin_prive_5 THEN t_prestations.tarif_prive_5   
                                            WHEN '[DATE_5]' >= t_prestations.date_debut_prive_6 AND '[DATE_5]' <= t_prestations.date_fin_prive_6 THEN t_prestations.tarif_prive_6   
                                            WHEN '[DATE_5]' >= t_prestations.date_debut_prive_7 AND '[DATE_5]' <= t_prestations.date_fin_prive_7 THEN t_prestations.tarif_prive_7   
                                            WHEN '[DATE_5]' >= t_prestations.date_debut_prive_8 AND '[DATE_5]' <= t_prestations.date_fin_prive_8 THEN t_prestations.tarif_prive_8   
                                            WHEN '[DATE_5]' >= t_prestations.date_debut_prive_9 AND '[DATE_5]' <= t_prestations.date_fin_prive_9 THEN t_prestations.tarif_prive_9   
                                            ELSE 0::numeric   
                                        END 
                                        ELSE 0 
                                    END
                                ) AS tarif_tse_fg1,                                                                                                
                                (SELECT tarif_ghs
                                    FROM base.t_ghs
                                    JOIN base.t_ghs_tarifs ON t_ghs_tarifs.ghs_id = t_ghs.oid
                                    WHERE 1=1
                                        AND t_ghs.code = '9605'
                                        AND t_ghs_tarifs.secteur = 2
                                        AND '[DATE_5]'::date BETWEEN date_debut AND date_fin) AS tarif_9605_fg1,
                                (SELECT tarif_ghs
                                    FROM base.t_ghs
                                    JOIN base.t_ghs_tarifs ON t_ghs_tarifs.ghs_id = t_ghs.oid
                                    WHERE 1=1
                                        AND t_ghs.code = '9604'
                                        AND t_ghs_tarifs.secteur = 2
                                        AND '[DATE_5]'::date BETWEEN date_debut AND date_fin) AS tarif_9604_fg1
                            FROM 
                                pmsi.t_prestations,
                                (
                                SELECT COALESCE(MIN(prestation_code),'SRA') AS prestation_rea_code 
                                FROM pmsi.v_rsf_detail_1 
                                WHERE 1=1
                                    AND prestation_code IN ('REA', 'SRA') 
                                    AND date_fin BETWEEN '[DATE_1]' AND '[DATE_4]'
                                ) AS subview
                            WHERE code IN ('SRC', 'STF', 'REA', 'SRA', 'NN1', 'NN2', 'NN3', 'ANT', 'RAP', 'SDC', 'TDE', 'TSE')
                        .,
                    

                        UPDATE pmsi.[simulationTableFG]
                                SET 
                                    erreur = CASE WHEN [tempTableFG].erreur IS NOT NULL THEN substr(trim([tempTableFG].erreur),1,255) ELSE '' END,
                                    commentaire = CASE WHEN [tempTableFG].commentaire IS NOT NULL THEN substr(trim([tempTableFG].commentaire),1,255) ELSE '' END,
                                    ghm_fg1_code = [tempTableFG].ghm_fg1_code,
                                    ghm_fg1_id = t_ghm.oid,
                                    ghs_fg1_code = [tempTableFG].ghs_fg1_code,
                                    ghs_fg1_id = t_ghs.oid,
                                    ghs_fg1_tarif_ghs =
                                    CASE 
                                    WHEN '[DATE_5]' >= t_ghs.date_debut_prive_1 AND '[DATE_5]' <= t_ghs.date_fin_prive_1 THEN t_ghs.tarif_ghs_prive_1 
                                    WHEN '[DATE_5]' >= t_ghs.date_debut_prive_2 AND '[DATE_5]' <= t_ghs.date_fin_prive_2 THEN t_ghs.tarif_ghs_prive_2 
                                    WHEN '[DATE_5]' >= t_ghs.date_debut_prive_3 AND '[DATE_5]' <= t_ghs.date_fin_prive_3 THEN t_ghs.tarif_ghs_prive_3 
                                    WHEN '[DATE_5]' >= t_ghs.date_debut_prive_4 AND '[DATE_5]' <= t_ghs.date_fin_prive_4 THEN t_ghs.tarif_ghs_prive_4 
                                    WHEN '[DATE_5]' >= t_ghs.date_debut_prive_5 AND '[DATE_5]' <= t_ghs.date_fin_prive_5 THEN t_ghs.tarif_ghs_prive_5 
                                    WHEN '[DATE_5]' >= t_ghs.date_debut_prive_6 AND '[DATE_5]' <= t_ghs.date_fin_prive_6 THEN t_ghs.tarif_ghs_prive_6 
                                    WHEN '[DATE_5]' >= t_ghs.date_debut_prive_7 AND '[DATE_5]' <= t_ghs.date_fin_prive_7 THEN t_ghs.tarif_ghs_prive_7 
                                    WHEN '[DATE_5]' >= t_ghs.date_debut_prive_8 AND '[DATE_5]' <= t_ghs.date_fin_prive_8 THEN t_ghs.tarif_ghs_prive_8 
                                    WHEN '[DATE_5]' >= t_ghs.date_debut_prive_9 AND '[DATE_5]' <= t_ghs.date_fin_prive_9 THEN t_ghs.tarif_ghs_prive_9 
                                    ELSE 0::numeric 
                                    END,
                                    ghs_fg1_tarif_exh =
                                    CASE 
                                    WHEN '[DATE_5]' >= t_ghs.date_debut_prive_1 AND '[DATE_5]' <= t_ghs.date_fin_prive_1 THEN t_ghs.tarif_exh_prive_1 
                                    WHEN '[DATE_5]' >= t_ghs.date_debut_prive_2 AND '[DATE_5]' <= t_ghs.date_fin_prive_2 THEN t_ghs.tarif_exh_prive_2 
                                    WHEN '[DATE_5]' >= t_ghs.date_debut_prive_3 AND '[DATE_5]' <= t_ghs.date_fin_prive_3 THEN t_ghs.tarif_exh_prive_3 
                                    WHEN '[DATE_5]' >= t_ghs.date_debut_prive_4 AND '[DATE_5]' <= t_ghs.date_fin_prive_4 THEN t_ghs.tarif_exh_prive_4 
                                    WHEN '[DATE_5]' >= t_ghs.date_debut_prive_5 AND '[DATE_5]' <= t_ghs.date_fin_prive_5 THEN t_ghs.tarif_exh_prive_5 
                                    WHEN '[DATE_5]' >= t_ghs.date_debut_prive_6 AND '[DATE_5]' <= t_ghs.date_fin_prive_6 THEN t_ghs.tarif_exh_prive_6 
                                    WHEN '[DATE_5]' >= t_ghs.date_debut_prive_7 AND '[DATE_5]' <= t_ghs.date_fin_prive_7 THEN t_ghs.tarif_exh_prive_7 
                                    WHEN '[DATE_5]' >= t_ghs.date_debut_prive_8 AND '[DATE_5]' <= t_ghs.date_fin_prive_8 THEN t_ghs.tarif_exh_prive_8 
                                    WHEN '[DATE_5]' >= t_ghs.date_debut_prive_9 AND '[DATE_5]' <= t_ghs.date_fin_prive_9 THEN t_ghs.tarif_exh_prive_9 
                                    ELSE 0::numeric 
                                    END,
                                    ghs_fg1_tarif_exb =
                                    CASE 
                                    WHEN '[DATE_5]' >= t_ghs.date_debut_prive_1 AND '[DATE_5]' <= t_ghs.date_fin_prive_1 THEN t_ghs.tarif_exb_prive_1 
                                    WHEN '[DATE_5]' >= t_ghs.date_debut_prive_2 AND '[DATE_5]' <= t_ghs.date_fin_prive_2 THEN t_ghs.tarif_exb_prive_2 
                                    WHEN '[DATE_5]' >= t_ghs.date_debut_prive_3 AND '[DATE_5]' <= t_ghs.date_fin_prive_3 THEN t_ghs.tarif_exb_prive_3 
                                    WHEN '[DATE_5]' >= t_ghs.date_debut_prive_4 AND '[DATE_5]' <= t_ghs.date_fin_prive_4 THEN t_ghs.tarif_exb_prive_4 
                                    WHEN '[DATE_5]' >= t_ghs.date_debut_prive_5 AND '[DATE_5]' <= t_ghs.date_fin_prive_5 THEN t_ghs.tarif_exb_prive_5 
                                    WHEN '[DATE_5]' >= t_ghs.date_debut_prive_6 AND '[DATE_5]' <= t_ghs.date_fin_prive_6 THEN t_ghs.tarif_exb_prive_6 
                                    WHEN '[DATE_5]' >= t_ghs.date_debut_prive_7 AND '[DATE_5]' <= t_ghs.date_fin_prive_7 THEN t_ghs.tarif_exb_prive_7 
                                    WHEN '[DATE_5]' >= t_ghs.date_debut_prive_8 AND '[DATE_5]' <= t_ghs.date_fin_prive_8 THEN t_ghs.tarif_exb_prive_8 
                                    WHEN '[DATE_5]' >= t_ghs.date_debut_prive_9 AND '[DATE_5]' <= t_ghs.date_fin_prive_9 THEN t_ghs.tarif_exb_prive_9 
                                    ELSE 0::numeric 
                                    END,
                                    ghs_fg1_forfait_exb =
                                    CASE 
                                    WHEN '[DATE_5]' >= t_ghs.date_debut_prive_1 AND '[DATE_5]' <= t_ghs.date_fin_prive_1 THEN t_ghs.forfait_exb_prive_1 
                                    WHEN '[DATE_5]' >= t_ghs.date_debut_prive_2 AND '[DATE_5]' <= t_ghs.date_fin_prive_2 THEN t_ghs.forfait_exb_prive_2 
                                    WHEN '[DATE_5]' >= t_ghs.date_debut_prive_3 AND '[DATE_5]' <= t_ghs.date_fin_prive_3 THEN t_ghs.forfait_exb_prive_3 
                                    WHEN '[DATE_5]' >= t_ghs.date_debut_prive_4 AND '[DATE_5]' <= t_ghs.date_fin_prive_4 THEN t_ghs.forfait_exb_prive_4 
                                    WHEN '[DATE_5]' >= t_ghs.date_debut_prive_5 AND '[DATE_5]' <= t_ghs.date_fin_prive_5 THEN t_ghs.forfait_exb_prive_5 
                                    WHEN '[DATE_5]' >= t_ghs.date_debut_prive_6 AND '[DATE_5]' <= t_ghs.date_fin_prive_6 THEN t_ghs.forfait_exb_prive_6 
                                    WHEN '[DATE_5]' >= t_ghs.date_debut_prive_7 AND '[DATE_5]' <= t_ghs.date_fin_prive_7 THEN t_ghs.forfait_exb_prive_7 
                                    WHEN '[DATE_5]' >= t_ghs.date_debut_prive_8 AND '[DATE_5]' <= t_ghs.date_fin_prive_8 THEN t_ghs.forfait_exb_prive_8 
                                    WHEN '[DATE_5]' >= t_ghs.date_debut_prive_9 AND '[DATE_5]' <= t_ghs.date_fin_prive_9 THEN t_ghs.forfait_exb_prive_9 
                                    ELSE 0::numeric 
                                    END,
                                    ghs_fg1_borne_basse =
                                    CASE 
                                    WHEN '[DATE_5]' >= t_ghs.date_debut_prive_1 AND '[DATE_5]' <= t_ghs.date_fin_prive_1 THEN t_ghs.borne_basse_prive_1 
                                    WHEN '[DATE_5]' >= t_ghs.date_debut_prive_2 AND '[DATE_5]' <= t_ghs.date_fin_prive_2 THEN t_ghs.borne_basse_prive_2 
                                    WHEN '[DATE_5]' >= t_ghs.date_debut_prive_3 AND '[DATE_5]' <= t_ghs.date_fin_prive_3 THEN t_ghs.borne_basse_prive_3 
                                    WHEN '[DATE_5]' >= t_ghs.date_debut_prive_4 AND '[DATE_5]' <= t_ghs.date_fin_prive_4 THEN t_ghs.borne_basse_prive_4 
                                    WHEN '[DATE_5]' >= t_ghs.date_debut_prive_5 AND '[DATE_5]' <= t_ghs.date_fin_prive_5 THEN t_ghs.borne_basse_prive_5 
                                    WHEN '[DATE_5]' >= t_ghs.date_debut_prive_6 AND '[DATE_5]' <= t_ghs.date_fin_prive_6 THEN t_ghs.borne_basse_prive_6 
                                    WHEN '[DATE_5]' >= t_ghs.date_debut_prive_7 AND '[DATE_5]' <= t_ghs.date_fin_prive_7 THEN t_ghs.borne_basse_prive_7 
                                    WHEN '[DATE_5]' >= t_ghs.date_debut_prive_8 AND '[DATE_5]' <= t_ghs.date_fin_prive_8 THEN t_ghs.borne_basse_prive_8 
                                    WHEN '[DATE_5]' >= t_ghs.date_debut_prive_9 AND '[DATE_5]' <= t_ghs.date_fin_prive_9 THEN t_ghs.borne_basse_prive_9 
                                    ELSE 0::numeric 
                                    END,
                                    ghs_fg1_borne_haute =
                                    CASE 
                                    WHEN '[DATE_5]' >= t_ghs.date_debut_prive_1 AND '[DATE_5]' <= t_ghs.date_fin_prive_1 THEN t_ghs.borne_haute_prive_1 
                                    WHEN '[DATE_5]' >= t_ghs.date_debut_prive_2 AND '[DATE_5]' <= t_ghs.date_fin_prive_2 THEN t_ghs.borne_haute_prive_2 
                                    WHEN '[DATE_5]' >= t_ghs.date_debut_prive_3 AND '[DATE_5]' <= t_ghs.date_fin_prive_3 THEN t_ghs.borne_haute_prive_3 
                                    WHEN '[DATE_5]' >= t_ghs.date_debut_prive_4 AND '[DATE_5]' <= t_ghs.date_fin_prive_4 THEN t_ghs.borne_haute_prive_4 
                                    WHEN '[DATE_5]' >= t_ghs.date_debut_prive_5 AND '[DATE_5]' <= t_ghs.date_fin_prive_5 THEN t_ghs.borne_haute_prive_5 
                                    WHEN '[DATE_5]' >= t_ghs.date_debut_prive_6 AND '[DATE_5]' <= t_ghs.date_fin_prive_6 THEN t_ghs.borne_haute_prive_6 
                                    WHEN '[DATE_5]' >= t_ghs.date_debut_prive_7 AND '[DATE_5]' <= t_ghs.date_fin_prive_7 THEN t_ghs.borne_haute_prive_7 
                                    WHEN '[DATE_5]' >= t_ghs.date_debut_prive_8 AND '[DATE_5]' <= t_ghs.date_fin_prive_8 THEN t_ghs.borne_haute_prive_8 
                                    WHEN '[DATE_5]' >= t_ghs.date_debut_prive_9 AND '[DATE_5]' <= t_ghs.date_fin_prive_9 THEN t_ghs.borne_haute_prive_9 
                                    ELSE 0::numeric 
                                    END,
                                    tarif_src_fg1 = [tempTableSuppl].tarif_src_fg1,
                                    tarif_stf_fg1 = [tempTableSuppl].tarif_stf_fg1,
                                    tarif_rea_fg1 = [tempTableSuppl].tarif_rea_fg1,

                                    est_envoye = '1',
                                    est_regroupe = CASE WHEN trim([tempTableFG].erreur) = '' OR [tempTableFG].erreur IS NULL  THEN '1' ELSE '0' END,

                                    nb_exb_fg1 = [tempTableFG].nb_exb_fg1,
                                    nb_sej_exb_fg1 = CASE WHEN [tempTableFG].nb_exb_fg1 > 0 THEN 1 ELSE 0 END,
                                    nb_exh_fg1 = [tempTableFG].nb_exh_fg1,
                                    nb_rea_fg1 = [tempTableFG].nb_rea_fg1 ,
                                    nb_rep_fg1 = [tempTableFG].nb_rep_fg1,
                                    nb_src_fg1 = [tempTableFG].nb_src_fg1,
                                    nb_stf_fg1 = [tempTableFG].nb_stf_fg1 ,
                                    nb_nn1_fg1 = [tempTableFG].nb_nn1_fg1,
                                    nb_nn2_fg1 = [tempTableFG].nb_nn2_fg1,
                                    nb_nn3_fg1 = [tempTableFG].nb_nn3_fg1,
                                    nb_ant_fg1 = [tempTableFG].nb_ant_fg1,
                                    nb_rap_fg1 = [tempTableFG].nb_rap_fg1,
                                    tarif_nn1_fg1 = [tempTableSuppl].tarif_nn1_fg1,
                                    tarif_nn2_fg1 = [tempTableSuppl].tarif_nn2_fg1,
                                    tarif_nn3_fg1 = [tempTableSuppl].tarif_nn3_fg1,
                                    tarif_ant_fg1 = [tempTableSuppl].tarif_ant_fg1,
                                    tarif_rap_fg1 = [tempTableSuppl].tarif_rap_fg1,
                                    tarif_9605_fg1 = [tempTableSuppl].tarif_9605_fg1,
                                    tarif_9604_fg1 = [tempTableSuppl].tarif_9604_fg1,
                                    ghs_minore_code = [tempTableFG].ghs_minore_code,
                                    ghs_minore_text =
                                    CASE 
                                    WHEN [tempTableFG].ghs_minore_code = '0' THEN 'pas de minoration pour ex-DGF et ex-OQN'
                                    WHEN [tempTableFG].ghs_minore_code = '1' THEN 'forfaitaire pour ex-DGF et ex-OQN'
                                    WHEN [tempTableFG].ghs_minore_code = '2' THEN 'journalièsre pour ex-DGF et ex-OQN'
                                    WHEN [tempTableFG].ghs_minore_code = '3' THEN 'forfaitaire pour ex-DGF et pas de minoration pour ex-OQN'
                                    WHEN [tempTableFG].ghs_minore_code = '4' THEN 'journalièsre pour ex-DGF et pas de minoration pour ex-OQN'
                                    WHEN [tempTableFG].ghs_minore_code = '5' THEN 'pas de minoration pour ex-DGF et forfaitaire pour ex-OQN'
                                    WHEN [tempTableFG].ghs_minore_code = '6' THEN 'pas de minoration pour ex-DGF et journalièsre pour ex-OQN'
                                    END,
                                    -- nouveau supplément cardiaque
                                    nb_sdc_fg1 = [tempTableFG].nb_sdc_fg1,
                                    tarif_sdc_fg1 = [tempTableSuppl].tarif_sdc_fg1
                                    --,nb_tde_fg1 = [tempTableFG].nb_tde_fg1
                                    ,tarif_tde_fg1 = [tempTableSuppl].tarif_tde_fg1
                                    --,nb_tse_fg1 = [tempTableFG].nb_tse_fg1
                                    ,tarif_tse_fg1 = [tempTableSuppl].tarif_tse_fg1
                                    ,is_raac = [tempTableFG].is_raac

                                FROM [tempTableFG], pmsi.t_ghm, pmsi.t_ghs, [tempTableSuppl]
                                WHERE [simulationTableFG].annee_sortie = [tempTableFG].annee_sortie
                                AND [simulationTableFG].no_rss = [tempTableFG].no_rss
                                AND [simulationTableFG].est_envoye <> '9'
                                AND [tempTableFG].ghm_fg1_code = t_ghm.code
                                AND [tempTableFG].ghs_fg1_code = t_ghs.code
                                .,
                                                                

            ]]></sqlcmd>
    </NODE>
    
    <NODE name="ETAPE_15" label="Positionnement valeurs SUPPLEMENTS FG1 PUBLIC">
            <condition><![CDATA[
                "[typeEts]" <> "1"
            ]]></condition>
            <sqlcmd><![CDATA[



                        DROP TABLE IF EXISTS [tempTableSuppl]
                        .,
                        
                        CREATE TEMP TABLE [tempTableSuppl] AS
                            SELECT 
                                MAX(CASE WHEN code = 'SRC' THEN
                                CASE   
                                WHEN '[DATE_5]' >= t_prestations.date_debut_public_1 AND '[DATE_5]' <= t_prestations.date_fin_public_1 THEN t_prestations.tarif_public_1   
                                WHEN '[DATE_5]' >= t_prestations.date_debut_public_2 AND '[DATE_5]' <= t_prestations.date_fin_public_2 THEN t_prestations.tarif_public_2   
                                WHEN '[DATE_5]' >= t_prestations.date_debut_public_3 AND '[DATE_5]' <= t_prestations.date_fin_public_3 THEN t_prestations.tarif_public_3   
                                WHEN '[DATE_5]' >= t_prestations.date_debut_public_4 AND '[DATE_5]' <= t_prestations.date_fin_public_4 THEN t_prestations.tarif_public_4   
                                WHEN '[DATE_5]' >= t_prestations.date_debut_public_5 AND '[DATE_5]' <= t_prestations.date_fin_public_5 THEN t_prestations.tarif_public_5   
                                WHEN '[DATE_5]' >= t_prestations.date_debut_public_6 AND '[DATE_5]' <= t_prestations.date_fin_public_6 THEN t_prestations.tarif_public_6   
                                WHEN '[DATE_5]' >= t_prestations.date_debut_public_7 AND '[DATE_5]' <= t_prestations.date_fin_public_7 THEN t_prestations.tarif_public_7   
                                WHEN '[DATE_5]' >= t_prestations.date_debut_public_8 AND '[DATE_5]' <= t_prestations.date_fin_public_8 THEN t_prestations.tarif_public_8   
                                WHEN '[DATE_5]' >= t_prestations.date_debut_public_9 AND '[DATE_5]' <= t_prestations.date_fin_public_9 THEN t_prestations.tarif_public_9   
                                ELSE 0::numeric   
                                END ELSE 0 END) AS tarif_src_fg1,
                                MAX(CASE WHEN code = 'STF' THEN
                                CASE   
                                WHEN '[DATE_5]' >= t_prestations.date_debut_public_1 AND '[DATE_5]' <= t_prestations.date_fin_public_1 THEN t_prestations.tarif_public_1   
                                WHEN '[DATE_5]' >= t_prestations.date_debut_public_2 AND '[DATE_5]' <= t_prestations.date_fin_public_2 THEN t_prestations.tarif_public_2   
                                WHEN '[DATE_5]' >= t_prestations.date_debut_public_3 AND '[DATE_5]' <= t_prestations.date_fin_public_3 THEN t_prestations.tarif_public_3   
                                WHEN '[DATE_5]' >= t_prestations.date_debut_public_4 AND '[DATE_5]' <= t_prestations.date_fin_public_4 THEN t_prestations.tarif_public_4   
                                WHEN '[DATE_5]' >= t_prestations.date_debut_public_5 AND '[DATE_5]' <= t_prestations.date_fin_public_5 THEN t_prestations.tarif_public_5   
                                WHEN '[DATE_5]' >= t_prestations.date_debut_public_6 AND '[DATE_5]' <= t_prestations.date_fin_public_6 THEN t_prestations.tarif_public_6   
                                WHEN '[DATE_5]' >= t_prestations.date_debut_public_7 AND '[DATE_5]' <= t_prestations.date_fin_public_7 THEN t_prestations.tarif_public_7   
                                WHEN '[DATE_5]' >= t_prestations.date_debut_public_8 AND '[DATE_5]' <= t_prestations.date_fin_public_8 THEN t_prestations.tarif_public_8   
                                WHEN '[DATE_5]' >= t_prestations.date_debut_public_9 AND '[DATE_5]' <= t_prestations.date_fin_public_9 THEN t_prestations.tarif_public_9   
                                ELSE 0::numeric   
                                END ELSE 0 END) AS tarif_stf_fg1,
                                MAX(CASE WHEN code = prestation_rea_code THEN
                                CASE   
                                WHEN '[DATE_5]' >= t_prestations.date_debut_public_1 AND '[DATE_5]' <= t_prestations.date_fin_public_1 THEN t_prestations.tarif_public_1   
                                WHEN '[DATE_5]' >= t_prestations.date_debut_public_2 AND '[DATE_5]' <= t_prestations.date_fin_public_2 THEN t_prestations.tarif_public_2   
                                WHEN '[DATE_5]' >= t_prestations.date_debut_public_3 AND '[DATE_5]' <= t_prestations.date_fin_public_3 THEN t_prestations.tarif_public_3   
                                WHEN '[DATE_5]' >= t_prestations.date_debut_public_4 AND '[DATE_5]' <= t_prestations.date_fin_public_4 THEN t_prestations.tarif_public_4   
                                WHEN '[DATE_5]' >= t_prestations.date_debut_public_5 AND '[DATE_5]' <= t_prestations.date_fin_public_5 THEN t_prestations.tarif_public_5   
                                WHEN '[DATE_5]' >= t_prestations.date_debut_public_6 AND '[DATE_5]' <= t_prestations.date_fin_public_6 THEN t_prestations.tarif_public_6   
                                WHEN '[DATE_5]' >= t_prestations.date_debut_public_7 AND '[DATE_5]' <= t_prestations.date_fin_public_7 THEN t_prestations.tarif_public_7   
                                WHEN '[DATE_5]' >= t_prestations.date_debut_public_8 AND '[DATE_5]' <= t_prestations.date_fin_public_8 THEN t_prestations.tarif_public_8   
                                WHEN '[DATE_5]' >= t_prestations.date_debut_public_9 AND '[DATE_5]' <= t_prestations.date_fin_public_9 THEN t_prestations.tarif_public_9   
                                ELSE 0::numeric   
                                END ELSE 0 END) AS tarif_rea_fg1,
                                MAX(CASE WHEN code = 'NN1' THEN
                                CASE   
                                WHEN '[DATE_5]' >= t_prestations.date_debut_public_1 AND '[DATE_5]' <= t_prestations.date_fin_public_1 THEN t_prestations.tarif_public_1   
                                WHEN '[DATE_5]' >= t_prestations.date_debut_public_2 AND '[DATE_5]' <= t_prestations.date_fin_public_2 THEN t_prestations.tarif_public_2   
                                WHEN '[DATE_5]' >= t_prestations.date_debut_public_3 AND '[DATE_5]' <= t_prestations.date_fin_public_3 THEN t_prestations.tarif_public_3   
                                WHEN '[DATE_5]' >= t_prestations.date_debut_public_4 AND '[DATE_5]' <= t_prestations.date_fin_public_4 THEN t_prestations.tarif_public_4   
                                WHEN '[DATE_5]' >= t_prestations.date_debut_public_5 AND '[DATE_5]' <= t_prestations.date_fin_public_5 THEN t_prestations.tarif_public_5   
                                WHEN '[DATE_5]' >= t_prestations.date_debut_public_6 AND '[DATE_5]' <= t_prestations.date_fin_public_6 THEN t_prestations.tarif_public_6   
                                WHEN '[DATE_5]' >= t_prestations.date_debut_public_7 AND '[DATE_5]' <= t_prestations.date_fin_public_7 THEN t_prestations.tarif_public_7   
                                WHEN '[DATE_5]' >= t_prestations.date_debut_public_8 AND '[DATE_5]' <= t_prestations.date_fin_public_8 THEN t_prestations.tarif_public_8   
                                WHEN '[DATE_5]' >= t_prestations.date_debut_public_9 AND '[DATE_5]' <= t_prestations.date_fin_public_9 THEN t_prestations.tarif_public_9   
                                ELSE 0::numeric   
                                END ELSE 0 END) AS tarif_nn1_fg1,
                                MAX(CASE WHEN code = 'NN2' THEN
                                CASE   
                                WHEN '[DATE_5]' >= t_prestations.date_debut_public_1 AND '[DATE_5]' <= t_prestations.date_fin_public_1 THEN t_prestations.tarif_public_1   
                                WHEN '[DATE_5]' >= t_prestations.date_debut_public_2 AND '[DATE_5]' <= t_prestations.date_fin_public_2 THEN t_prestations.tarif_public_2   
                                WHEN '[DATE_5]' >= t_prestations.date_debut_public_3 AND '[DATE_5]' <= t_prestations.date_fin_public_3 THEN t_prestations.tarif_public_3   
                                WHEN '[DATE_5]' >= t_prestations.date_debut_public_4 AND '[DATE_5]' <= t_prestations.date_fin_public_4 THEN t_prestations.tarif_public_4   
                                WHEN '[DATE_5]' >= t_prestations.date_debut_public_5 AND '[DATE_5]' <= t_prestations.date_fin_public_5 THEN t_prestations.tarif_public_5   
                                WHEN '[DATE_5]' >= t_prestations.date_debut_public_6 AND '[DATE_5]' <= t_prestations.date_fin_public_6 THEN t_prestations.tarif_public_6   
                                WHEN '[DATE_5]' >= t_prestations.date_debut_public_7 AND '[DATE_5]' <= t_prestations.date_fin_public_7 THEN t_prestations.tarif_public_7   
                                WHEN '[DATE_5]' >= t_prestations.date_debut_public_8 AND '[DATE_5]' <= t_prestations.date_fin_public_8 THEN t_prestations.tarif_public_8   
                                WHEN '[DATE_5]' >= t_prestations.date_debut_public_9 AND '[DATE_5]' <= t_prestations.date_fin_public_9 THEN t_prestations.tarif_public_9   
                                ELSE 0::numeric   
                                END ELSE 0 END) AS tarif_nn2_fg1,
                                MAX(CASE WHEN code = 'NN3' THEN
                                CASE   
                                WHEN '[DATE_5]' >= t_prestations.date_debut_public_1 AND '[DATE_5]' <= t_prestations.date_fin_public_1 THEN t_prestations.tarif_public_1   
                                WHEN '[DATE_5]' >= t_prestations.date_debut_public_2 AND '[DATE_5]' <= t_prestations.date_fin_public_2 THEN t_prestations.tarif_public_2   
                                WHEN '[DATE_5]' >= t_prestations.date_debut_public_3 AND '[DATE_5]' <= t_prestations.date_fin_public_3 THEN t_prestations.tarif_public_3   
                                WHEN '[DATE_5]' >= t_prestations.date_debut_public_4 AND '[DATE_5]' <= t_prestations.date_fin_public_4 THEN t_prestations.tarif_public_4   
                                WHEN '[DATE_5]' >= t_prestations.date_debut_public_5 AND '[DATE_5]' <= t_prestations.date_fin_public_5 THEN t_prestations.tarif_public_5   
                                WHEN '[DATE_5]' >= t_prestations.date_debut_public_6 AND '[DATE_5]' <= t_prestations.date_fin_public_6 THEN t_prestations.tarif_public_6   
                                WHEN '[DATE_5]' >= t_prestations.date_debut_public_7 AND '[DATE_5]' <= t_prestations.date_fin_public_7 THEN t_prestations.tarif_public_7   
                                WHEN '[DATE_5]' >= t_prestations.date_debut_public_8 AND '[DATE_5]' <= t_prestations.date_fin_public_8 THEN t_prestations.tarif_public_8   
                                WHEN '[DATE_5]' >= t_prestations.date_debut_public_9 AND '[DATE_5]' <= t_prestations.date_fin_public_9 THEN t_prestations.tarif_public_9   
                                ELSE 0::numeric   
                                END ELSE 0 END) AS tarif_nn3_fg1,
                                MAX(CASE WHEN code = 'ANT' THEN
                                CASE   
                                WHEN '[DATE_5]' >= t_prestations.date_debut_public_1 AND '[DATE_5]' <= t_prestations.date_fin_public_1 THEN t_prestations.tarif_public_1   
                                WHEN '[DATE_5]' >= t_prestations.date_debut_public_2 AND '[DATE_5]' <= t_prestations.date_fin_public_2 THEN t_prestations.tarif_public_2   
                                WHEN '[DATE_5]' >= t_prestations.date_debut_public_3 AND '[DATE_5]' <= t_prestations.date_fin_public_3 THEN t_prestations.tarif_public_3   
                                WHEN '[DATE_5]' >= t_prestations.date_debut_public_4 AND '[DATE_5]' <= t_prestations.date_fin_public_4 THEN t_prestations.tarif_public_4   
                                WHEN '[DATE_5]' >= t_prestations.date_debut_public_5 AND '[DATE_5]' <= t_prestations.date_fin_public_5 THEN t_prestations.tarif_public_5   
                                WHEN '[DATE_5]' >= t_prestations.date_debut_public_6 AND '[DATE_5]' <= t_prestations.date_fin_public_6 THEN t_prestations.tarif_public_6   
                                WHEN '[DATE_5]' >= t_prestations.date_debut_public_7 AND '[DATE_5]' <= t_prestations.date_fin_public_7 THEN t_prestations.tarif_public_7   
                                WHEN '[DATE_5]' >= t_prestations.date_debut_public_8 AND '[DATE_5]' <= t_prestations.date_fin_public_8 THEN t_prestations.tarif_public_8   
                                WHEN '[DATE_5]' >= t_prestations.date_debut_public_9 AND '[DATE_5]' <= t_prestations.date_fin_public_9 THEN t_prestations.tarif_public_9   
                                ELSE 0::numeric   
                                END ELSE 0 END) AS tarif_ant_fg1,
                                MAX(CASE WHEN code = 'RAP' THEN
                                CASE   
                                WHEN '[DATE_5]' >= t_prestations.date_debut_public_1 AND '[DATE_5]' <= t_prestations.date_fin_public_1 THEN t_prestations.tarif_public_1   
                                WHEN '[DATE_5]' >= t_prestations.date_debut_public_2 AND '[DATE_5]' <= t_prestations.date_fin_public_2 THEN t_prestations.tarif_public_2   
                                WHEN '[DATE_5]' >= t_prestations.date_debut_public_3 AND '[DATE_5]' <= t_prestations.date_fin_public_3 THEN t_prestations.tarif_public_3   
                                WHEN '[DATE_5]' >= t_prestations.date_debut_public_4 AND '[DATE_5]' <= t_prestations.date_fin_public_4 THEN t_prestations.tarif_public_4   
                                WHEN '[DATE_5]' >= t_prestations.date_debut_public_5 AND '[DATE_5]' <= t_prestations.date_fin_public_5 THEN t_prestations.tarif_public_5   
                                WHEN '[DATE_5]' >= t_prestations.date_debut_public_6 AND '[DATE_5]' <= t_prestations.date_fin_public_6 THEN t_prestations.tarif_public_6   
                                WHEN '[DATE_5]' >= t_prestations.date_debut_public_7 AND '[DATE_5]' <= t_prestations.date_fin_public_7 THEN t_prestations.tarif_public_7   
                                WHEN '[DATE_5]' >= t_prestations.date_debut_public_8 AND '[DATE_5]' <= t_prestations.date_fin_public_8 THEN t_prestations.tarif_public_8   
                                WHEN '[DATE_5]' >= t_prestations.date_debut_public_9 AND '[DATE_5]' <= t_prestations.date_fin_public_9 THEN t_prestations.tarif_public_9   
                                ELSE 0::numeric   
                                END ELSE 0 END) AS tarif_rap_fg1,
                                MAX(
                                    CASE WHEN code = 'SDC' THEN
                                        CASE   
                                            WHEN '[DATE_5]' >= t_prestations.date_debut_public_1 AND '[DATE_5]' <= t_prestations.date_fin_public_1 THEN t_prestations.tarif_public_1   
                                            WHEN '[DATE_5]' >= t_prestations.date_debut_public_2 AND '[DATE_5]' <= t_prestations.date_fin_public_2 THEN t_prestations.tarif_public_2   
                                            WHEN '[DATE_5]' >= t_prestations.date_debut_public_3 AND '[DATE_5]' <= t_prestations.date_fin_public_3 THEN t_prestations.tarif_public_3   
                                            WHEN '[DATE_5]' >= t_prestations.date_debut_public_4 AND '[DATE_5]' <= t_prestations.date_fin_public_4 THEN t_prestations.tarif_public_4   
                                            WHEN '[DATE_5]' >= t_prestations.date_debut_public_5 AND '[DATE_5]' <= t_prestations.date_fin_public_5 THEN t_prestations.tarif_public_5   
                                            WHEN '[DATE_5]' >= t_prestations.date_debut_public_6 AND '[DATE_5]' <= t_prestations.date_fin_public_6 THEN t_prestations.tarif_public_6   
                                            WHEN '[DATE_5]' >= t_prestations.date_debut_public_7 AND '[DATE_5]' <= t_prestations.date_fin_public_7 THEN t_prestations.tarif_public_7   
                                            WHEN '[DATE_5]' >= t_prestations.date_debut_public_8 AND '[DATE_5]' <= t_prestations.date_fin_public_8 THEN t_prestations.tarif_public_8   
                                            WHEN '[DATE_5]' >= t_prestations.date_debut_public_9 AND '[DATE_5]' <= t_prestations.date_fin_public_9 THEN t_prestations.tarif_public_9   
                                            ELSE 0::numeric   
                                        END 
                                        ELSE 0 
                                    END
                                ) AS tarif_sdc_fg1,
                                MAX(
                                    CASE WHEN code = 'TDE' THEN
                                        CASE   
                                            WHEN '[DATE_5]' >= t_prestations.date_debut_public_1 AND '[DATE_5]' <= t_prestations.date_fin_public_1 THEN t_prestations.tarif_public_1   
                                            WHEN '[DATE_5]' >= t_prestations.date_debut_public_2 AND '[DATE_5]' <= t_prestations.date_fin_public_2 THEN t_prestations.tarif_public_2   
                                            WHEN '[DATE_5]' >= t_prestations.date_debut_public_3 AND '[DATE_5]' <= t_prestations.date_fin_public_3 THEN t_prestations.tarif_public_3   
                                            WHEN '[DATE_5]' >= t_prestations.date_debut_public_4 AND '[DATE_5]' <= t_prestations.date_fin_public_4 THEN t_prestations.tarif_public_4   
                                            WHEN '[DATE_5]' >= t_prestations.date_debut_public_5 AND '[DATE_5]' <= t_prestations.date_fin_public_5 THEN t_prestations.tarif_public_5   
                                            WHEN '[DATE_5]' >= t_prestations.date_debut_public_6 AND '[DATE_5]' <= t_prestations.date_fin_public_6 THEN t_prestations.tarif_public_6   
                                            WHEN '[DATE_5]' >= t_prestations.date_debut_public_7 AND '[DATE_5]' <= t_prestations.date_fin_public_7 THEN t_prestations.tarif_public_7   
                                            WHEN '[DATE_5]' >= t_prestations.date_debut_public_8 AND '[DATE_5]' <= t_prestations.date_fin_public_8 THEN t_prestations.tarif_public_8   
                                            WHEN '[DATE_5]' >= t_prestations.date_debut_public_9 AND '[DATE_5]' <= t_prestations.date_fin_public_9 THEN t_prestations.tarif_public_9   
                                            ELSE 0::numeric   
                                        END 
                                        ELSE 0 
                                    END
                                ) AS tarif_tde_fg1,   
                                MAX(
                                    CASE WHEN code = 'TSE' THEN
                                        CASE   
                                            WHEN '[DATE_5]' >= t_prestations.date_debut_public_1 AND '[DATE_5]' <= t_prestations.date_fin_public_1 THEN t_prestations.tarif_public_1   
                                            WHEN '[DATE_5]' >= t_prestations.date_debut_public_2 AND '[DATE_5]' <= t_prestations.date_fin_public_2 THEN t_prestations.tarif_public_2   
                                            WHEN '[DATE_5]' >= t_prestations.date_debut_public_3 AND '[DATE_5]' <= t_prestations.date_fin_public_3 THEN t_prestations.tarif_public_3   
                                            WHEN '[DATE_5]' >= t_prestations.date_debut_public_4 AND '[DATE_5]' <= t_prestations.date_fin_public_4 THEN t_prestations.tarif_public_4   
                                            WHEN '[DATE_5]' >= t_prestations.date_debut_public_5 AND '[DATE_5]' <= t_prestations.date_fin_public_5 THEN t_prestations.tarif_public_5   
                                            WHEN '[DATE_5]' >= t_prestations.date_debut_public_6 AND '[DATE_5]' <= t_prestations.date_fin_public_6 THEN t_prestations.tarif_public_6   
                                            WHEN '[DATE_5]' >= t_prestations.date_debut_public_7 AND '[DATE_5]' <= t_prestations.date_fin_public_7 THEN t_prestations.tarif_public_7   
                                            WHEN '[DATE_5]' >= t_prestations.date_debut_public_8 AND '[DATE_5]' <= t_prestations.date_fin_public_8 THEN t_prestations.tarif_public_8   
                                            WHEN '[DATE_5]' >= t_prestations.date_debut_public_9 AND '[DATE_5]' <= t_prestations.date_fin_public_9 THEN t_prestations.tarif_public_9   
                                            ELSE 0::numeric   
                                        END 
                                        ELSE 0 
                                    END
                                ) AS tarif_tse_fg1,                                                                   
                                (SELECT tarif_ghs
                                    FROM base.t_ghs
                                    JOIN base.t_ghs_tarifs ON t_ghs_tarifs.ghs_id = t_ghs.oid
                                    WHERE 1=1
                                        AND t_ghs.code = '9605'
                                        AND t_ghs_tarifs.secteur = 1
                                        AND '[DATE_5]'::date BETWEEN date_debut AND date_fin) AS tarif_9605_fg1,
                                (SELECT tarif_ghs
                                    FROM base.t_ghs
                                    JOIN base.t_ghs_tarifs ON t_ghs_tarifs.ghs_id = t_ghs.oid
                                    WHERE 1=1
                                        AND t_ghs.code = '9604'
                                        AND t_ghs_tarifs.secteur = 1
                                        AND '[DATE_5]'::date BETWEEN date_debut AND date_fin) AS tarif_9604_fg1
                            FROM 
                                pmsi.t_prestations,
                                (
                                SELECT COALESCE(MIN(prestation_code),'SRA') AS prestation_rea_code 
                                FROM pmsi.v_rsf_detail_1 
                                WHERE 1=1
                                    AND prestation_code In ('REA', 'SRA') 
                                    AND date_fin BETWEEN '[DATE_1]' AND '[DATE_4]'
                                ) AS subview
                            WHERE code IN ('SRC', 'STF', 'REA', 'SRA', 'NN1', 'NN2', 'NN3', 'ANT', 'RAP', 'SDC', 'TDE', 'TSE')
                        .,
                    

                        UPDATE pmsi.[simulationTableFG]
                                    SET 
                                        erreur = CASE WHEN [tempTableFG].erreur IS NOT NULL THEN substr(trim([tempTableFG].erreur),1,255) ELSE '' END,
                                        commentaire = CASE WHEN [tempTableFG].commentaire IS NOT NULL THEN substr(trim([tempTableFG].commentaire),1,255) ELSE '' END,
                                        ghm_fg1_code = [tempTableFG].ghm_fg1_code,
                                        ghm_fg1_id = t_ghm.oid,
                                        ghs_fg1_code = [tempTableFG].ghs_fg1_code,
                                        ghs_fg1_id = t_ghs.oid,
                                        ghs_fg1_tarif_ghs =
                                        CASE 
                                        WHEN '[DATE_5]' >= t_ghs.date_debut_public_1 AND '[DATE_5]' <= t_ghs.date_fin_public_1 THEN t_ghs.tarif_ghs_public_1 
                                        WHEN '[DATE_5]' >= t_ghs.date_debut_public_2 AND '[DATE_5]' <= t_ghs.date_fin_public_2 THEN t_ghs.tarif_ghs_public_2 
                                        WHEN '[DATE_5]' >= t_ghs.date_debut_public_3 AND '[DATE_5]' <= t_ghs.date_fin_public_3 THEN t_ghs.tarif_ghs_public_3 
                                        WHEN '[DATE_5]' >= t_ghs.date_debut_public_4 AND '[DATE_5]' <= t_ghs.date_fin_public_4 THEN t_ghs.tarif_ghs_public_4 
                                        WHEN '[DATE_5]' >= t_ghs.date_debut_public_5 AND '[DATE_5]' <= t_ghs.date_fin_public_5 THEN t_ghs.tarif_ghs_public_5 
                                        WHEN '[DATE_5]' >= t_ghs.date_debut_public_6 AND '[DATE_5]' <= t_ghs.date_fin_public_6 THEN t_ghs.tarif_ghs_public_6 
                                        WHEN '[DATE_5]' >= t_ghs.date_debut_public_7 AND '[DATE_5]' <= t_ghs.date_fin_public_7 THEN t_ghs.tarif_ghs_public_7 
                                        WHEN '[DATE_5]' >= t_ghs.date_debut_public_8 AND '[DATE_5]' <= t_ghs.date_fin_public_8 THEN t_ghs.tarif_ghs_public_8 
                                        WHEN '[DATE_5]' >= t_ghs.date_debut_public_9 AND '[DATE_5]' <= t_ghs.date_fin_public_9 THEN t_ghs.tarif_ghs_public_9 
                                        ELSE 0::numeric 
                                        END,
                                        ghs_fg1_tarif_exh =
                                        CASE 
                                        WHEN '[DATE_5]' >= t_ghs.date_debut_public_1 AND '[DATE_5]' <= t_ghs.date_fin_public_1 THEN t_ghs.tarif_exh_public_1 
                                        WHEN '[DATE_5]' >= t_ghs.date_debut_public_2 AND '[DATE_5]' <= t_ghs.date_fin_public_2 THEN t_ghs.tarif_exh_public_2 
                                        WHEN '[DATE_5]' >= t_ghs.date_debut_public_3 AND '[DATE_5]' <= t_ghs.date_fin_public_3 THEN t_ghs.tarif_exh_public_3 
                                        WHEN '[DATE_5]' >= t_ghs.date_debut_public_4 AND '[DATE_5]' <= t_ghs.date_fin_public_4 THEN t_ghs.tarif_exh_public_4 
                                        WHEN '[DATE_5]' >= t_ghs.date_debut_public_5 AND '[DATE_5]' <= t_ghs.date_fin_public_5 THEN t_ghs.tarif_exh_public_5 
                                        WHEN '[DATE_5]' >= t_ghs.date_debut_public_6 AND '[DATE_5]' <= t_ghs.date_fin_public_6 THEN t_ghs.tarif_exh_public_6 
                                        WHEN '[DATE_5]' >= t_ghs.date_debut_public_7 AND '[DATE_5]' <= t_ghs.date_fin_public_7 THEN t_ghs.tarif_exh_public_7 
                                        WHEN '[DATE_5]' >= t_ghs.date_debut_public_8 AND '[DATE_5]' <= t_ghs.date_fin_public_8 THEN t_ghs.tarif_exh_public_8 
                                        WHEN '[DATE_5]' >= t_ghs.date_debut_public_9 AND '[DATE_5]' <= t_ghs.date_fin_public_9 THEN t_ghs.tarif_exh_public_9 
                                        ELSE 0::numeric 
                                        END,
                                        ghs_fg1_tarif_exb =
                                        CASE 
                                        WHEN '[DATE_5]' >= t_ghs.date_debut_public_1 AND '[DATE_5]' <= t_ghs.date_fin_public_1 THEN t_ghs.tarif_exb_public_1 
                                        WHEN '[DATE_5]' >= t_ghs.date_debut_public_2 AND '[DATE_5]' <= t_ghs.date_fin_public_2 THEN t_ghs.tarif_exb_public_2 
                                        WHEN '[DATE_5]' >= t_ghs.date_debut_public_3 AND '[DATE_5]' <= t_ghs.date_fin_public_3 THEN t_ghs.tarif_exb_public_3 
                                        WHEN '[DATE_5]' >= t_ghs.date_debut_public_4 AND '[DATE_5]' <= t_ghs.date_fin_public_4 THEN t_ghs.tarif_exb_public_4 
                                        WHEN '[DATE_5]' >= t_ghs.date_debut_public_5 AND '[DATE_5]' <= t_ghs.date_fin_public_5 THEN t_ghs.tarif_exb_public_5 
                                        WHEN '[DATE_5]' >= t_ghs.date_debut_public_6 AND '[DATE_5]' <= t_ghs.date_fin_public_6 THEN t_ghs.tarif_exb_public_6 
                                        WHEN '[DATE_5]' >= t_ghs.date_debut_public_7 AND '[DATE_5]' <= t_ghs.date_fin_public_7 THEN t_ghs.tarif_exb_public_7 
                                        WHEN '[DATE_5]' >= t_ghs.date_debut_public_8 AND '[DATE_5]' <= t_ghs.date_fin_public_8 THEN t_ghs.tarif_exb_public_8 
                                        WHEN '[DATE_5]' >= t_ghs.date_debut_public_9 AND '[DATE_5]' <= t_ghs.date_fin_public_9 THEN t_ghs.tarif_exb_public_9 
                                        ELSE 0::numeric 
                                        END,
                                        ghs_fg1_forfait_exb =
                                        CASE 
                                        WHEN '[DATE_5]' >= t_ghs.date_debut_public_1 AND '[DATE_5]' <= t_ghs.date_fin_public_1 THEN t_ghs.forfait_exb_public_1 
                                        WHEN '[DATE_5]' >= t_ghs.date_debut_public_2 AND '[DATE_5]' <= t_ghs.date_fin_public_2 THEN t_ghs.forfait_exb_public_2 
                                        WHEN '[DATE_5]' >= t_ghs.date_debut_public_3 AND '[DATE_5]' <= t_ghs.date_fin_public_3 THEN t_ghs.forfait_exb_public_3 
                                        WHEN '[DATE_5]' >= t_ghs.date_debut_public_4 AND '[DATE_5]' <= t_ghs.date_fin_public_4 THEN t_ghs.forfait_exb_public_4 
                                        WHEN '[DATE_5]' >= t_ghs.date_debut_public_5 AND '[DATE_5]' <= t_ghs.date_fin_public_5 THEN t_ghs.forfait_exb_public_5 
                                        WHEN '[DATE_5]' >= t_ghs.date_debut_public_6 AND '[DATE_5]' <= t_ghs.date_fin_public_6 THEN t_ghs.forfait_exb_public_6 
                                        WHEN '[DATE_5]' >= t_ghs.date_debut_public_7 AND '[DATE_5]' <= t_ghs.date_fin_public_7 THEN t_ghs.forfait_exb_public_7 
                                        WHEN '[DATE_5]' >= t_ghs.date_debut_public_8 AND '[DATE_5]' <= t_ghs.date_fin_public_8 THEN t_ghs.forfait_exb_public_8 
                                        WHEN '[DATE_5]' >= t_ghs.date_debut_public_9 AND '[DATE_5]' <= t_ghs.date_fin_public_9 THEN t_ghs.forfait_exb_public_9 
                                        ELSE 0::numeric 
                                        END,
                                        ghs_fg1_borne_basse =
                                        CASE 
                                        WHEN '[DATE_5]' >= t_ghs.date_debut_public_1 AND '[DATE_5]' <= t_ghs.date_fin_public_1 THEN t_ghs.borne_basse_public_1 
                                        WHEN '[DATE_5]' >= t_ghs.date_debut_public_2 AND '[DATE_5]' <= t_ghs.date_fin_public_2 THEN t_ghs.borne_basse_public_2 
                                        WHEN '[DATE_5]' >= t_ghs.date_debut_public_3 AND '[DATE_5]' <= t_ghs.date_fin_public_3 THEN t_ghs.borne_basse_public_3 
                                        WHEN '[DATE_5]' >= t_ghs.date_debut_public_4 AND '[DATE_5]' <= t_ghs.date_fin_public_4 THEN t_ghs.borne_basse_public_4 
                                        WHEN '[DATE_5]' >= t_ghs.date_debut_public_5 AND '[DATE_5]' <= t_ghs.date_fin_public_5 THEN t_ghs.borne_basse_public_5 
                                        WHEN '[DATE_5]' >= t_ghs.date_debut_public_6 AND '[DATE_5]' <= t_ghs.date_fin_public_6 THEN t_ghs.borne_basse_public_6 
                                        WHEN '[DATE_5]' >= t_ghs.date_debut_public_7 AND '[DATE_5]' <= t_ghs.date_fin_public_7 THEN t_ghs.borne_basse_public_7 
                                        WHEN '[DATE_5]' >= t_ghs.date_debut_public_8 AND '[DATE_5]' <= t_ghs.date_fin_public_8 THEN t_ghs.borne_basse_public_8 
                                        WHEN '[DATE_5]' >= t_ghs.date_debut_public_9 AND '[DATE_5]' <= t_ghs.date_fin_public_9 THEN t_ghs.borne_basse_public_9 
                                        ELSE 0::numeric 
                                        END,
                                        ghs_fg1_borne_haute =
                                        CASE 
                                        WHEN '[DATE_5]' >= t_ghs.date_debut_public_1 AND '[DATE_5]' <= t_ghs.date_fin_public_1 THEN t_ghs.borne_haute_public_1 
                                        WHEN '[DATE_5]' >= t_ghs.date_debut_public_2 AND '[DATE_5]' <= t_ghs.date_fin_public_2 THEN t_ghs.borne_haute_public_2 
                                        WHEN '[DATE_5]' >= t_ghs.date_debut_public_3 AND '[DATE_5]' <= t_ghs.date_fin_public_3 THEN t_ghs.borne_haute_public_3 
                                        WHEN '[DATE_5]' >= t_ghs.date_debut_public_4 AND '[DATE_5]' <= t_ghs.date_fin_public_4 THEN t_ghs.borne_haute_public_4 
                                        WHEN '[DATE_5]' >= t_ghs.date_debut_public_5 AND '[DATE_5]' <= t_ghs.date_fin_public_5 THEN t_ghs.borne_haute_public_5 
                                        WHEN '[DATE_5]' >= t_ghs.date_debut_public_6 AND '[DATE_5]' <= t_ghs.date_fin_public_6 THEN t_ghs.borne_haute_public_6 
                                        WHEN '[DATE_5]' >= t_ghs.date_debut_public_7 AND '[DATE_5]' <= t_ghs.date_fin_public_7 THEN t_ghs.borne_haute_public_7 
                                        WHEN '[DATE_5]' >= t_ghs.date_debut_public_8 AND '[DATE_5]' <= t_ghs.date_fin_public_8 THEN t_ghs.borne_haute_public_8 
                                        WHEN '[DATE_5]' >= t_ghs.date_debut_public_9 AND '[DATE_5]' <= t_ghs.date_fin_public_9 THEN t_ghs.borne_haute_public_9 
                                        ELSE 0::numeric 
                                        END,
                                        tarif_src_fg1 = [tempTableSuppl].tarif_src_fg1,
                                        tarif_stf_fg1 = [tempTableSuppl].tarif_stf_fg1,
                                        tarif_rea_fg1 = [tempTableSuppl].tarif_rea_fg1,

                                        est_envoye = '1',
                                        est_regroupe = CASE WHEN trim([tempTableFG].erreur) = '' OR [tempTableFG].erreur IS NULL  THEN '1' ELSE '0' END,

                                        nb_exb_fg1 = [tempTableFG].nb_exb_fg1,
                                        nb_sej_exb_fg1 = CASE WHEN [tempTableFG].nb_exb_fg1 > 0 THEN 1 ELSE 0 END,
                                        nb_exh_fg1 = [tempTableFG].nb_exh_fg1,
                                        nb_rea_fg1 = [tempTableFG].nb_rea_fg1,
                                        nb_rep_fg1 = [tempTableFG].nb_rep_fg1,
                                        nb_src_fg1 = [tempTableFG].nb_src_fg1,
                                        nb_stf_fg1 = [tempTableFG].nb_stf_fg1,
                                        nb_nn1_fg1 = [tempTableFG].nb_nn1_fg1,
                                        nb_nn2_fg1 = [tempTableFG].nb_nn2_fg1,
                                        nb_nn3_fg1 = [tempTableFG].nb_nn3_fg1,
                                        nb_ant_fg1 = [tempTableFG].nb_ant_fg1,
                                        nb_rap_fg1 = [tempTableFG].nb_rap_fg1,
                                        tarif_nn1_fg1 = [tempTableSuppl].tarif_nn1_fg1,
                                        tarif_nn2_fg1 = [tempTableSuppl].tarif_nn2_fg1,
                                        tarif_nn3_fg1 = [tempTableSuppl].tarif_nn3_fg1,
                                        tarif_ant_fg1 = [tempTableSuppl].tarif_ant_fg1,
                                        tarif_rap_fg1 = [tempTableSuppl].tarif_rap_fg1,
                                        tarif_9605_fg1 = [tempTableSuppl].tarif_9605_fg1,
                                        tarif_9604_fg1 = [tempTableSuppl].tarif_9604_fg1,
                                        ghs_minore_code = [tempTableFG].ghs_minore_code,
                                        ghs_minore_text =
                                        CASE 
                                        WHEN [tempTableFG].ghs_minore_code = '0' THEN 'pas de minoration pour ex-DGF et ex-OQN'
                                        WHEN [tempTableFG].ghs_minore_code = '1' THEN 'forfaitaire pour ex-DGF et ex-OQN'
                                        WHEN [tempTableFG].ghs_minore_code = '2' THEN 'journalièsre pour ex-DGF et ex-OQN'
                                        WHEN [tempTableFG].ghs_minore_code = '3' THEN 'forfaitaire pour ex-DGF et pas de minoration pour ex-OQN'
                                        WHEN [tempTableFG].ghs_minore_code = '4' THEN 'journalièsre pour ex-DGF et pas de minoration pour ex-OQN'
                                        WHEN [tempTableFG].ghs_minore_code = '5' THEN 'pas de minoration pour ex-DGF et forfaitaire pour ex-OQN'
                                        WHEN [tempTableFG].ghs_minore_code = '6' THEN 'pas de minoration pour ex-DGF et journalièsre pour ex-OQN'
                                        END,
                                        -- nouveau supplément cardiaque
                                        nb_sdc_fg1 = [tempTableFG].nb_sdc_fg1,
                                        tarif_sdc_fg1 = [tempTableSuppl].tarif_sdc_fg1
                                        --,nb_tde_fg1 = [tempTableFG].nb_tde_fg1
                                        ,tarif_tde_fg1 = [tempTableSuppl].tarif_tde_fg1
                                        --,nb_tse_fg1 = [tempTableFG].nb_tse_fg1
                                        ,tarif_tse_fg1 = [tempTableSuppl].tarif_tse_fg1 
                                        ,is_raac = [tempTableFG].is_raac

                                    FROM [tempTableFG], pmsi.t_ghm, pmsi.t_ghs, [tempTableSuppl]
                                    WHERE [simulationTableFG].annee_sortie = [tempTableFG].annee_sortie
                                    AND [simulationTableFG].no_rss = [tempTableFG].no_rss
                                    AND [simulationTableFG].est_envoye <> '9'
                                    AND [tempTableFG].ghm_fg1_code = t_ghm.code
                                    AND [tempTableFG].ghs_fg1_code = t_ghs.code
                        .,

            ]]></sqlcmd>
    </NODE>

    <NODE name="ETAPE_16" label="RECUPERATION DES SRC ELIGIBLES">
            <sqlcmd><![CDATA[


                -- SRC eligibles
                -- gestion SRC 1


                DROP TABLE IF EXISTS w_simulation_src_1
                .,

                CREATE TEMP TABLE w_simulation_src_1 AS  
                    SELECT
                        [simulationTableFG].rss_id,
                        p_rss_rum.diagnostic_principal_id, 
                        t_diagnostics.code as diagnostic_principal_code,
                        'DP RUM : ' || t_diagnostics.code as regle_justificatif
                    FROM pmsi.p_rss_rum
                    JOIN pmsi.[simulationTableFG] ON p_rss_rum.rss_id = [simulationTableFG].rss_id
                    JOIN pmsi.t_diagnostics ON p_rss_rum.diagnostic_principal_id = t_diagnostics.oid 
                    WHERE 1=1
                        AND p_rss_rum.diagnostic_principal_id IN (SELECT to_id FROM pmsi.v_listes_3 WHERE liste_code = 'CTI_SRC22_81D')
                        AND [simulationTableFG].rss_id NOT IN (SELECT DISTINCT rss_id FROM pmsi.v_rss_rum_1 WHERE type_autorisation IN ('02', '03'))
                        AND [simulationTableFG].nb_src_fg1 = 0
                .,

                
                DELETE from w_simulation_src_1 WHERE 
                    diagnostic_principal_code IN ('S2700', 'S2710', 'S2720', 'S2730') 
                    AND rss_id != ALL(ARRAY(    SELECT 
                                                    DISTINCT w_simulation_src_1_bis.rss_id 
                                                FROM 
                                                    pmsi.p_rss_actes 
                                                    JOIN w_simulation_src_1 w_simulation_src_1_bis ON w_simulation_src_1_bis.rss_id = p_rss_actes.rss_id
                                                    JOIN pmsi.t_actes ON t_actes.oid = p_rss_actes.acte_id
                                                WHERE
                                                    t_actes.code IN ('AFLB007', 'AFLB017', 'GGJB001')
                                            )::bigint[])
                .,
                    
                DELETE from w_simulation_src_1 WHERE 
                    diagnostic_principal_code IN ('J90', 'J91', 'J930', 'J931', 'J938', 'J939') 
                    AND rss_id != ALL(ARRAY(    SELECT 
                                                    DISTINCT w_simulation_src_1_bis.rss_id 
                                                FROM 
                                                    pmsi.p_rss_actes 
                                                    JOIN w_simulation_src_1 w_simulation_src_1_bis ON w_simulation_src_1_bis.rss_id = p_rss_actes.rss_id
                                                    JOIN pmsi.t_actes ON t_actes.oid = p_rss_actes.acte_id
                                                WHERE 
                                                    t_actes.code = 'GGJB001'
                                            )::bigint[])
                ., 
                    
                DELETE from w_simulation_src_1 WHERE 
                    diagnostic_principal_code = 'T754'
                    AND rss_id != ALL(ARRAY(    SELECT 
                                                    DISTINCT w_simulation_src_1_bis.rss_id 
                                                FROM 
                                                    pmsi.p_rss_actes
                                                    JOIN w_simulation_src_1 w_simulation_src_1_bis ON w_simulation_src_1_bis.rss_id = p_rss_actes.rss_id
                                                    JOIN pmsi.t_actes ON t_actes.oid = p_rss_actes.acte_id
                                                WHERE 
                                                    t_actes.code = 'DEQP007'
                                            )::bigint[])
                ., 
          
                DELETE from w_simulation_src_1 WHERE 
                    diagnostic_principal_code = 'R571'
                    AND rss_id != ALL(ARRAY(    SELECT
                                                    DISTINCT w_simulation_src_1_bis.rss_id 
                                                FROM 
                                                    pmsi.p_rss_actes
                                                    JOIN w_simulation_src_1 w_simulation_src_1_bis ON w_simulation_src_1_bis.rss_id = p_rss_actes.rss_id
                                                    JOIN pmsi.t_actes ON t_actes.oid = p_rss_actes.acte_id
                                                WHERE
                                                    t_actes.code IN ('ENLF001', 'EPLF002', 'EQLF002')
                                            )::bigint[])
                ., 
                    
                DELETE from w_simulation_src_1 WHERE 
                    diagnostic_principal_code IN ('T311', 'T321') 
                    AND rss_id != ALL(ARRAY(    SELECT 
                                                    DISTINCT w_simulation_src_1_bis.rss_id 
                                                FROM 
                                                    pmsi.p_rss_actes
                                                    JOIN w_simulation_src_1 w_simulation_src_1_bis ON w_simulation_src_1_bis.rss_id = p_rss_actes.rss_id
                                                    JOIN pmsi.t_actes ON t_actes.oid = p_rss_actes.acte_id
                                                WHERE 
                                                    t_actes.code IN ('EQLF002', 'HSLD002')
                                            )::bigint[])
                ., 
                    
                DELETE from w_simulation_src_1 WHERE 
                    diagnostic_principal_code IN ('R570', 'R578') 
                    AND rss_id != ALL(ARRAY(    SELECT 
                                                    DISTINCT w_simulation_src_1_bis.rss_id 
                                                FROM 
                                                    pmsi.p_rss_actes
                                                    JOIN w_simulation_src_1 w_simulation_src_1_bis ON w_simulation_src_1_bis.rss_id = p_rss_actes.rss_id
                                                    JOIN pmsi.t_actes ON t_actes.oid = p_rss_actes.acte_id
                                                WHERE 
                                                    t_actes.code = 'EQLF001'
                                            )::bigint[])
                ., 
                    
                DELETE from w_simulation_src_1 WHERE 
                    diagnostic_principal_code IN ('D62', 'I850', 'I983', 'K250', 'K252', 'K254', 'K260', 'K262', 'K264', 'K920', 'K921', 'K922', 'O720', 'O721', 'O722') 
                    AND rss_id != ALL(ARRAY(    SELECT 
                                                    DISTINCT w_simulation_src_1_bis.rss_id 
                                                FROM 
                                                    pmsi.p_rss_actes
                                                    JOIN w_simulation_src_1 w_simulation_src_1_bis ON w_simulation_src_1_bis.rss_id = p_rss_actes.rss_id
                                                    JOIN pmsi.t_actes ON t_actes.oid = p_rss_actes.acte_id
                                                WHERE
                                                    t_actes.code = 'FELF011'
                                            )::bigint[])
                ., 
                    
                DELETE from w_simulation_src_1 WHERE 
                    diagnostic_principal_code IN ('J680','T202', 'T203', 'T206', 'T207') 
                    AND rss_id != ALL(ARRAY(    SELECT 
                                                    DISTINCT w_simulation_src_1_bis.rss_id 
                                                FROM 
                                                    pmsi.p_rss_actes
                                                    JOIN w_simulation_src_1 w_simulation_src_1_bis ON w_simulation_src_1_bis.rss_id = p_rss_actes.rss_id
                                                    JOIN pmsi.t_actes ON t_actes.oid = p_rss_actes.acte_id
                                                WHERE 
                                                    t_actes.code = 'GELD005'
                                            )::bigint[])
                ., 
                    
                DELETE from w_simulation_src_1 WHERE 
                    diagnostic_principal_code = 'I501'
                    AND rss_id != ALL(ARRAY(    SELECT 
                                                    DISTINCT w_simulation_src_1_bis.rss_id 
                                                FROM 
                                                    pmsi.p_rss_actes
                                                    JOIN w_simulation_src_1 w_simulation_src_1_bis ON w_simulation_src_1_bis.rss_id = p_rss_actes.rss_id
                                                    JOIN pmsi.t_actes ON t_actes.oid = p_rss_actes.acte_id
                                                WHERE 
                                                    t_actes.code IN ('GLLD003', 'GLLD017')
                                            )::bigint[])
                ., 
                    
                DELETE from w_simulation_src_1 WHERE 
                    diagnostic_principal_code IN ('J960', 'J9600', 'J9601', 'J9609') 
                    AND rss_id != ALL(ARRAY(    SELECT 
                                                    DISTINCT w_simulation_src_1_bis.rss_id 
                                                FROM 
                                                    pmsi.p_rss_actes
                                                    JOIN w_simulation_src_1 w_simulation_src_1_bis ON w_simulation_src_1_bis.rss_id = p_rss_actes.rss_id
                                                    JOIN pmsi.t_actes ON t_actes.oid = p_rss_actes.acte_id
                                                WHERE 
                                                    t_actes.code IN ('GLLD003', 'GLLD015', 'GLLD017')
                                            )::bigint[])
                ., 
                    
                DELETE from w_simulation_src_1 WHERE 
                    diagnostic_principal_code IN ('K500', 'K501', 'K508', 'K509', 'K510', 'K512', 'K513', 'K514', 'K515', 'K518', 'K519') 
                    AND rss_id != ALL(ARRAY(    SELECT 
                                                    DISTINCT w_simulation_src_1_bis.rss_id 
                                                FROM 
                                                    pmsi.p_rss_actes
                                                    JOIN w_simulation_src_1 w_simulation_src_1_bis ON w_simulation_src_1_bis.rss_id = p_rss_actes.rss_id
                                                    JOIN pmsi.t_actes ON t_actes.oid = p_rss_actes.acte_id
                                                WHERE 
                                                    t_actes.code IN ('HSLF001', 'HSLF002', 'HSLF003')
                                            )::bigint[])
                .,


                --  'RUMs dont l''association DP/acte apparait dans la liste 3 de l''annexe 8'
                                    
                CREATE TEMP TABLE w_simulation_src_2 AS  
                    SELECT
                        [simulationTableFG].rss_id,
                        p_rss_rum.diagnostic_principal_id, 
                        t_diagnostics.code as diagnostic_principal_code,
                        'DP RUM : ' || t_diagnostics.code || ' (< 18 ans)' as regle_justificatif
                    FROM pmsi.p_rss_rum
                    JOIN pmsi.[simulationTableFG] ON p_rss_rum.rss_id = [simulationTableFG].rss_id
                    JOIN pmsi.t_diagnostics ON p_rss_rum.diagnostic_principal_id = t_diagnostics.oid 
                    JOIN pmsi.p_rss ON p_rss_rum.rss_id = p_rss.oid
                    WHERE 1=1
                        AND p_rss.age < 18
                        AND p_rss_rum.diagnostic_principal_id IN (SELECT to_id FROM pmsi.v_listes_3 WHERE liste_code = 'CTI_SRC17_83D')
                        AND [simulationTableFG].rss_id NOT IN (SELECT DISTINCT rss_id FROM pmsi.v_rss_rum_1 WHERE type_autorisation IN ('02', '03'))
                        AND [simulationTableFG].nb_src_fg1 = 0
                .,

                
                DELETE from w_simulation_src_2 WHERE 
                    diagnostic_principal_code IN ('E743', 'K523', 'K529', 'K560', 'K904', 'K908', 'K912', 'K93828', 'Q431', 'Q432', 'Q793') 
                    AND rss_id != ALL(ARRAY(    SELECT 
                                                    DISTINCT w_simulation_src_2_bis.rss_id 
                                                FROM 
                                                    pmsi.p_rss_actes 
                                                    JOIN w_simulation_src_2 w_simulation_src_2_bis ON w_simulation_src_2_bis.rss_id = p_rss_actes.rss_id 
                                                    JOIN pmsi.t_actes ON t_actes.oid = p_rss_actes.acte_id
                                                WHERE 
                                                    t_actes.code = 'HSLF003'
                                            )::bigint[])
                ., 
                
                DELETE from w_simulation_src_2 WHERE 
                    diagnostic_principal_code IN ('J188', 'J840', 'J841','J848') 
                    AND rss_id != ALL(ARRAY(    SELECT 
                                                    DISTINCT w_simulation_src_2_bis.rss_id 
                                                FROM 
                                                    pmsi.p_rss_actes
                                                    JOIN w_simulation_src_2 w_simulation_src_2_bis ON w_simulation_src_2_bis.rss_id = p_rss_actes.rss_id 
                                                    JOIN pmsi.t_actes ON t_actes.oid = p_rss_actes.acte_id
                                                WHERE 
                                                    t_actes.code = 'GEQE004'
                                            )::bigint[])
                ., 
                
                DELETE from w_simulation_src_2 WHERE 
                    diagnostic_principal_code = 'K560' 
                    AND rss_id != ALL(ARRAY(    SELECT 
                                                    DISTINCT w_simulation_src_2_bis.rss_id 
                                                FROM 
                                                    pmsi.p_rss_actes
                                                    JOIN w_simulation_src_2 w_simulation_src_2_bis ON w_simulation_src_2_bis.rss_id = p_rss_actes.rss_id 
                                                    JOIN pmsi.t_actes ON t_actes.oid = p_rss_actes.acte_id
                                                WHERE 
                                                    t_actes.code = 'HSLF002'
                                            )::bigint[])
                .,  
                
                DELETE from w_simulation_src_2 WHERE 
                    diagnostic_principal_code = 'R040' 
                    AND rss_id != ALL(ARRAY(    SELECT 
                                                    DISTINCT w_simulation_src_2_bis.rss_id 
                                                FROM 
                                                    pmsi.p_rss_actes
                                                    JOIN w_simulation_src_2 w_simulation_src_2_bis ON w_simulation_src_2_bis.rss_id = p_rss_actes.rss_id 
                                                    JOIN pmsi.t_actes ON t_actes.oid = p_rss_actes.acte_id
                                                WHERE 
                                                    t_actes.code IN ('GABD001', 'GASE001', 'GASE002')
                                            )::bigint[])
                ., 
                
                DELETE from w_simulation_src_2 WHERE 
                    diagnostic_principal_code IN ('J960', 'J9600', 'J9601', 'J9609') 
                    AND rss_id != ALL(ARRAY(    SELECT 
                                                    DISTINCT w_simulation_src_2_bis.rss_id 
                                                FROM 
                                                    pmsi.p_rss_actes
                                                    JOIN w_simulation_src_2 w_simulation_src_2_bis ON w_simulation_src_2_bis.rss_id = p_rss_actes.rss_id 
                                                    JOIN pmsi.t_actes ON t_actes.oid = p_rss_actes.acte_id
                                                WHERE 
                                                    t_actes.code = 'GLLD019'
                                            )::bigint[])
                ., 
                
                DELETE from w_simulation_src_2 WHERE 
                    diagnostic_principal_code IN ('J960', 'J9600', 'J9601', 'J9609') 
                    AND rss_id != ALL(ARRAY(    SELECT 
                                                    DISTINCT w_simulation_src_2_bis.rss_id 
                                                FROM 
                                                    pmsi.p_rss_actes
                                                    JOIN w_simulation_src_2 w_simulation_src_2_bis ON w_simulation_src_2_bis.rss_id = p_rss_actes.rss_id 
                                                    JOIN pmsi.t_actes ON t_actes.oid = p_rss_actes.acte_id
                                                WHERE 
                                                    t_actes.code = 'GLLD019'
                                            )::bigint[])
                .,  
                
                DELETE from w_simulation_src_2 WHERE 
                    diagnostic_principal_code IN ('G934', 'K220', 'K224') 
                    AND rss_id != ALL(ARRAY(    SELECT 
                                                    DISTINCT w_simulation_src_2_bis.rss_id 
                                                FROM 
                                                    pmsi.p_rss_actes
                                                    JOIN w_simulation_src_2 w_simulation_src_2_bis ON w_simulation_src_2_bis.rss_id = p_rss_actes.rss_id 
                                                    JOIN pmsi.t_actes ON t_actes.oid = p_rss_actes.acte_id
                                                WHERE 
                                                    t_actes.code = 'HSLD002'
                                            )::bigint[])
                ., 

             
                -- 'RUMs avec acte dans la liste 2 de l''annexe 8'
                
                DROP TABLE IF EXISTS w_simulation_src_3
                .,

                CREATE TEMP TABLE w_simulation_src_3 AS  
                SELECT 
                    [simulationTableFG].rss_id,
                    'Acte : ' || base.cti_group_concat(DISTINCT t_actes.code) as regle_justificatif
                FROM 
                    pmsi.p_rss_actes
                    JOIN pmsi.t_actes ON p_rss_actes.acte_id = t_actes.oid
                    JOIN pmsi.[simulationTableFG] ON p_rss_actes.rss_id = [simulationTableFG].rss_id
                WHERE 1=1
                        AND p_rss_actes.activite_ccam <> '4'
                        AND p_rss_actes.acte_id IN (SELECT to_id FROM pmsi.v_listes_3 WHERE liste_code = 'CTI_SRC23_82A')
                        AND [simulationTableFG].rss_id NOT IN (SELECT DISTINCT rss_id FROM pmsi.v_rss_rum_1 WHERE type_autorisation IN ('02', '03'))
                        AND [simulationTableFG].nb_src_fg1 = 0
                GROUP BY 1., 


                -- MAJ d'apres les SRC eligibles    

                UPDATE pmsi.[simulationTableFG] 
                SET nb_sej_src_eligibles_fg1 = 1 ,
                    regle_src_eligible_fg1 = w_simulation_src_1.regle_justificatif
                FROM w_simulation_src_1
                WHERE 1=1
                    AND [simulationTableFG].rss_id = w_simulation_src_1.rss_id
                    AND nb_sej_src_eligibles_fg1 <> 1
                .,

                UPDATE pmsi.[simulationTableFG] 
                SET nb_sej_src_eligibles_fg1 = 1 ,
                    regle_src_eligible_fg1 = w_simulation_src_2.regle_justificatif
                FROM w_simulation_src_2
                WHERE 1=1
                    AND [simulationTableFG].rss_id = w_simulation_src_2.rss_id
                    AND nb_sej_src_eligibles_fg1 <> 2
                .,

                UPDATE pmsi.[simulationTableFG] 
                SET nb_sej_src_eligibles_fg1 = 1 ,
                    regle_src_eligible_fg1 = w_simulation_src_3.regle_justificatif
                FROM w_simulation_src_3
                WHERE 1=1
                    AND [simulationTableFG].rss_id = w_simulation_src_3.rss_id
                    AND nb_sej_src_eligibles_fg1 <> 2
                .,

            ]]></sqlcmd>
    </NODE>

    <NODE name="ETAPE_17" label="Modification du coefficient prudentiel privé - SAISIR VARIABLES DANS base.t_finess">
            <sqlcmd><![CDATA[

                -- prise en compte du nouveau coefficient (pacte de responsabilité annexe XIV de l'arrêté tarifaire) / -3% pour privés
                DROP TABLE IF EXISTS coeff_pacte_resp
                .,

                CREATE TEMP TABLE coeff_pacte_resp
                AS 
                    SELECT 
                        code AS finess, 
                        MAX(CASE 
                            WHEN date_debut_coefficient_pacte_resp_1 = '2023-03-01' THEN coefficient_pacte_resp_1
                            WHEN date_debut_coefficient_pacte_resp_2 = '2023-03-01' THEN coefficient_pacte_resp_2
                            WHEN date_debut_coefficient_pacte_resp_3 = '2023-03-01' THEN coefficient_pacte_resp_3
                            WHEN date_debut_coefficient_pacte_resp_4 = '2023-03-01' THEN coefficient_pacte_resp_4
                            WHEN date_debut_coefficient_pacte_resp_5 = '2023-03-01' THEN coefficient_pacte_resp_5
                            WHEN date_debut_coefficient_pacte_resp_6 = '2023-03-01' THEN coefficient_pacte_resp_6
                            WHEN date_debut_coefficient_pacte_resp_7 = '2023-03-01' THEN coefficient_pacte_resp_7
                            WHEN date_debut_coefficient_pacte_resp_8 = '2023-03-01' THEN coefficient_pacte_resp_8
                            WHEN date_debut_coefficient_pacte_resp_9 = '2023-03-01' THEN coefficient_pacte_resp_9
                        ELSE 0.9718
                        END) AS coefficient_pacte_resp
                        ,
                        MAX(CASE 
                            WHEN t_finess.date_debut_coefficient_mco_9 = '2023-03-01' THEN t_finess.coefficient_geo_9 
                            WHEN t_finess.date_debut_coefficient_mco_8 = '2023-03-01' THEN t_finess.coefficient_geo_8 
                            WHEN t_finess.date_debut_coefficient_mco_7 = '2023-03-01' THEN t_finess.coefficient_geo_7 
                            WHEN t_finess.date_debut_coefficient_mco_6 = '2023-03-01' THEN t_finess.coefficient_geo_6 
                            WHEN t_finess.date_debut_coefficient_mco_5 = '2023-03-01' THEN t_finess.coefficient_geo_5 
                            WHEN t_finess.date_debut_coefficient_mco_4 = '2023-03-01' THEN t_finess.coefficient_geo_4 
                            WHEN t_finess.date_debut_coefficient_mco_3 = '2023-03-01' THEN t_finess.coefficient_geo_3 
                            WHEN t_finess.date_debut_coefficient_mco_2 = '2023-03-01' THEN t_finess.coefficient_geo_2 
                            WHEN t_finess.date_debut_coefficient_mco_1 = '2023-03-01' THEN t_finess.coefficient_geo_1                         
                        ELSE 1
                        END) AS coefficient_geographique,
                        MAX(round(
							CASE 
							WHEN substr(t_finess.code,1,2) IN ('75','77','78','91','92','93','94','95') THEN 1.07
							WHEN substr(t_finess.code,1,2) IN ('20','2A','2B') THEN 1.11
							WHEN substr(t_finess.code,1,2) IN ('97') THEN 1.27
							ELSE 1 END
							* 
							0.993
							*
							CASE 
							WHEN type_etablissement = '1' THEN 0.9766
							ELSE 0.9853 END
							*
							CASE 
							WHEN code = '590782421' THEN 1.0019
							WHEN type_etablissement = '1' THEN 0.9989
							ELSE 0.9897 END
                        ,4)) AS coefficient_mco_calcule  
                    FROM base.t_finess
                    GROUP BY 1
                .,

                -- Prudentiel non modifié en 2023 reste 0.993
                -- CICE PRIVE deviens 0.9766
                -- CICE ESPIC deviens 0.9853
                -- Si non paramétré on positionne celui du privé soit le plus fréquent de nos clients

                UPDATE pmsi.[simulationTableFG]
                SET coefficient_mco_fg1 = round( round(0.993 * COALESCE(coefficient_pacte_resp, 0.9718) , 4) * COALESCE(coefficient_geographique, 1) , 4)
                FROM coeff_pacte_resp
                WHERE [simulationTableFG].finess = coeff_pacte_resp.finess
                .,
				
				UPDATE pmsi.[simulationTableFG]
                SET coefficient_mco_fg1 = coefficient_mco_calcule  
                FROM coeff_pacte_resp
                WHERE [simulationTableFG].finess = coeff_pacte_resp.finess
                .,

                UPDATE pmsi.[simulationTableFG]
                SET est_envoye = '0'
                WHERE est_envoye = '9'
                .,

            ]]></sqlcmd>
    </NODE>

    <NODE name="ETAPE_18" label="Calcul des montants simulés">
            <sqlcmd><![CDATA[

                -- Calcul des montants simulés.

                UPDATE pmsi.[simulationTableFG]
                SET 
                    montant_ghs_theorique_fg1 = round( ghs_fg1_tarif_ghs * CASE WHEN p_rss.nb_seances <= 1 OR t_ghs.code IN (9626, 9628, 9630) THEN 1 ELSE p_rss.nb_seances END * coefficient_mco_fg1 * COALESCE(coefficient, 1)
                                                        ,2
                                                      )
                                                + 
                                                    round(tarif_9605_fg1 * nb_9605_fg1 * coefficient_mco_fg1,2)
                                                +   
                                                    round(tarif_9604_fg1 * nb_9604_fg1 * coefficient_mco_fg1,2),
                    montant_exh_fg1 = round(ghs_fg1_tarif_exh * nb_exh_fg1 * coefficient_mco_fg1,2),
                    montant_src_fg1 = round(tarif_src_fg1 * nb_src_fg1 * coefficient_mco_fg1,2),
                    montant_stf_fg1 = round(tarif_stf_fg1 * nb_stf_fg1 * coefficient_mco_fg1,2),
                    montant_rea_fg1 = round(tarif_rea_fg1 * nb_rea_fg1 * coefficient_mco_fg1,2),
                    montant_nn1_fg1 = round(tarif_nn1_fg1 * nb_nn1_fg1 * coefficient_mco_fg1,2),
                    montant_nn2_fg1 = round(tarif_nn2_fg1 * nb_nn2_fg1 * coefficient_mco_fg1,2),
                    montant_nn3_fg1 = round(tarif_nn3_fg1 * nb_nn3_fg1 * coefficient_mco_fg1,2),
                    montant_ant_fg1 = round(tarif_ant_fg1 * nb_ant_fg1 * coefficient_mco_fg1,2),
                    montant_rap_fg1 = round(tarif_rap_fg1 * nb_rap_fg1 * coefficient_mco_fg1,2),
                    -- Ce montant montant_9605_fg1 n'est pas exploité dans les vues XML.
                    -- Il est là  pour info.
                    -- Il est ajouté dans cette même requète au montant_ghs_theorique_fg1.
                    montant_9605_fg1 = round(tarif_9605_fg1 * nb_9605_fg1 * coefficient_mco_fg1,2),
                    montant_9604_fg1 = round(tarif_9604_fg1 * nb_9604_fg1 * coefficient_mco_fg1,2),
                    montant_sdc_fg1  = round(tarif_sdc_fg1 * nb_sdc_fg1 * coefficient_mco_fg1,2),
                    montant_tde_fg1  = round(tarif_tde_fg1 * nb_tde_fg1 * coefficient_mco_fg1,2),
                    montant_tse_fg1  = round(tarif_tse_fg1 * nb_tse_fg1 * coefficient_mco_fg1,2)                                                            
                FROM pmsi.p_rss, pmsi.t_ghs
                WHERE 1=1
                    AND p_rss.oid = [simulationTableFG].rss_id
                    AND [simulationTableFG].ghs_fg0_id = t_ghs.oid
                    AND [simulationTableFG].est_envoye = '1'
                .,
				
				-- Tarifs TDE inchangés
				UPDATE pmsi.[simulationTableFG]
				SET montant_tde_fg1  = round(montant_tde_fg0 / coefficient_mco_fg0 * coefficient_mco_fg1,2),
                    montant_tse_fg1  = round(montant_tse_fg0 / coefficient_mco_fg0 * coefficient_mco_fg1,2)
				WHERE montant_tde_fg1 <> 0 OR montant_tde_fg0 <> 0 OR montant_tse_fg0 <> 0 OR montant_tse_fg1 <> 0
				.,

            ]]></sqlcmd>
    </NODE>

    <NODE name="ETAPE_19" label="Montant EXB FG1">
            <sqlcmd><![CDATA[

                        UPDATE pmsi.[simulationTableFG]
                            SET montant_exb_fg1 =   CASE WHEN ghs_fg1_forfait_exb > 0 
                                                    THEN 0 - round(ghs_fg1_forfait_exb * coefficient_mco_fg1,2) 
                                                    ELSE 0 - round(nb_exb_fg1 * ghs_fg1_tarif_exb * coefficient_mco_fg1,2) 
                                                    END
                        WHERE nb_exb_fg1 > 0 AND [simulationTableFG].est_envoye = '1'
                        .,

            ]]></sqlcmd>
    </NODE>
    
    <NODE name="ETAPE_20" label=" Traitement des UCD/EMI DEREMBOURSES">
        <sqlcmd><![CDATA[

                -- Traitement des UCD/EMI

                DROP TABLE IF EXISTS [tempTableUCD]
                .,
                
                CREATE TEMP TABLE [tempTableUCD] AS
                    SELECT t_ucd.code as ucd_code 
                    FROM pmsi.t_listes
                        JOIN pmsi.t_listes_contenu ON t_listes_contenu.liste_id = t_listes.oid 
                        JOIN pmsi.t_listes_tables ON t_listes_tables.oid = t_listes.table_id
                        JOIN pmsi.t_ucd ON t_ucd.oid = t_listes_contenu.to_id
                    WHERE 1=1
                        AND t_listes.is_cti = '1' 
                        AND t_listes_tables.name = 't_ucd' 
                        AND t_listes.code = 'CTI_DR23_1U'
                .,
                
                UPDATE pmsi.[simulationTableFG]
                SET
                    montant_ucd_fg1_deremboursees = subview.montant_ucd_fg1_deremboursees,
                    montant_emi_fg1_deremboursees = subview.montant_emi_fg1_deremboursees
                FROM
                    (
                    SELECT 
                        rss_id, 
                        SUM(CASE WHEN ucd_code IN (SELECT ucd_code FROM [tempTableUCD]) THEN montant_facture ELSE 0 END) AS montant_ucd_fg1_deremboursees,
                        SUM(CASE WHEN ucd_code IN (SELECT ucd_code FROM [tempTableUCD]) THEN montant_ecart_indemnisable ELSE 0 END) AS montant_emi_fg1_deremboursees
                    FROM pmsi.v_rss_ucd_1
                    WHERE 1=1
                    AND type_ucd_code = 'SUS'
                    -- à comenter si pb de table v_ucd absente type_ucd_code = 'SUS'
                    GROUP BY rss_id
                    ) AS subview
                WHERE 1=1
                    AND [simulationTableFG].rss_id = subview.rss_id 
                    AND [simulationTableFG].est_envoye = '1'
                .,


                UPDATE pmsi.[simulationTableFG]
                SET montant_ucd_fg1 = montant_ucd_fg0 - montant_ucd_fg1_deremboursees,
                    montant_emi_fg1 = montant_emi_fg0 - montant_emi_fg1_deremboursees
                WHERE 1=1
                    AND (1!=1
                        OR montant_ucd_fg1 <> montant_ucd_fg0 - montant_ucd_fg1_deremboursees
                        OR montant_emi_fg1 <> montant_emi_fg0 - montant_emi_fg1_deremboursees)
                    AND [simulationTableFG].est_envoye = '1'
            .,
            ]]></sqlcmd>
    </NODE>

    <NODE name="ETAPE_21" label=" Traitement des LPP/ETI DEREMBOURSES">
        <sqlcmd><![CDATA[

            --Traitement des LPP/ETI

            DROP TABLE IF EXISTS [tempTableLPP]
            .,
            
            CREATE TEMP TABLE [tempTableLPP] AS
                SELECT t_lpp.code as lpp_code 
                FROM pmsi.t_listes
                    JOIN pmsi.t_listes_contenu ON t_listes_contenu.liste_id = t_listes.oid 
                    JOIN pmsi.t_listes_tables ON t_listes_tables.oid = t_listes.table_id
                    JOIN pmsi.t_lpp ON t_lpp.oid = t_listes_contenu.to_id
                WHERE 1=1
                    AND t_listes.is_cti = '1' 
                    AND t_listes_tables.name = 't_lpp' 
                    AND t_listes.code = 'CTI_DR23_1L'
            .,
            
            UPDATE pmsi.[simulationTableFG]
            SET 
                montant_lpp_fg1_deremboursees = subview.montant_lpp_fg1_deremboursees
                --,montant_eti_fg1_deremboursees = subview.montant_eti_fg1_deremboursees
            FROM
                (
                SELECT
                    rss_id, 
                    SUM(CASE WHEN lpp_code IN (SELECT lpp_code FROM [tempTableLPP])  THEN montant_facture ELSE 0 END) AS montant_lpp_fg1_deremboursees
                    --,SUM(CASE WHEN lpp_code IN (SELECT lpp_code FROM [tempTableLPP])  THEN montant_ecart_indemnisable ELSE 0 END) AS montant_eti_fg1_deremboursees
                FROM pmsi.v_rss_lpp_1
                GROUP BY rss_id
                ) AS subview
            WHERE 1=1
                AND [simulationTableFG].rss_id = subview.rss_id 
                AND [simulationTableFG].est_envoye = '1'
            .,

            UPDATE pmsi.[simulationTableFG]
                SET montant_lpp_fg1 = montant_lpp_fg0 - montant_lpp_fg1_deremboursees
                --,montant_eti_fg1 = montant_eti_fg0 - montant_eti_fg1_deremboursees
            WHERE 
                (
                    montant_lpp_fg1 <> montant_lpp_fg0 - montant_lpp_fg1_deremboursees
                    --OR
                    --montant_eti_fg1 <> montant_eti_fg0 - montant_eti_fg1_deremboursees
                )
            AND [simulationTableFG].est_envoye = '1'
            .,

            ]]></sqlcmd>
    </NODE>

    <NODE name="ETAPE_23" label="Montant GHS théorique">
        <sqlcmd><![CDATA[

            -- Enlever le montant des séances en hospit

            UPDATE pmsi.[simulationTableFG]  SET
                montant_ghs_theorique_fg0 = montant_ghs_theorique_fg0 - montant_9605_fg0 - montant_9604_fg0,
                montant_ghs_theorique_fg1 = montant_ghs_theorique_fg1 - montant_9605_fg1 - montant_9604_fg1
                WHERE nb_9604_fg0 <> 0 OR nb_9605_fg0 <> 0
            .,           
            
        ]]></sqlcmd>
    </NODE>

    <NODE name="ETAPE_24" label="Statut séjours">
        <sqlcmd><![CDATA[

            UPDATE pmsi.[simulationTableFG] 
                SET erreur = 'RSS en double', est_envoye = '0', est_regroupe = '0'
                WHERE no_rss IN (SELECT no_rss FROM pmsi.[simulationTableFG] GROUP BY 1 HAVING count(*) > 1)
            .,


            -- Ne pas prendre en compte les doublons de RRAC pour simulation severite augementee
            UPDATE pmsi.[simulationTableFG] 
                SET erreur = 'Simulation RAAC sévérité 2',  est_envoye = '2', est_regroupe = '2'
                WHERE is_raac = 'is_raac_severite_augmentee'
            .,

         ]]></sqlcmd>
    </NODE>

    <NODE name="ETAPE_25" label="Maintenance table">
        <sqlcmd><![CDATA[

            SELECT base.cti_vacuum_table('pmsi', '[simulationTableFG]');

         ]]></sqlcmd>
    </NODE>
                                    
</ROOT>