<?xml version="1.0" encoding="ISO-8859-1"?>
<VUE name="ACTI000174" 
	label="Activité. Comparatif GME/PJ 2023/2024" 
	title="Comparatif des tarifs GME et PJ SMR pour ajustement de la valorisation"
	database="iCTI" 
	softCode="iCTI_activite" 
	globals="ACTI_globals.XML" 
	componentsFile="ACTI_components.XML" 
	swf="*CTI_view1"
>
	<QUERIES>
		<QUERY label="">
			<SQL>
				<select><![CDATA[ 
					DROP TABLE IF EXISTS w_file_valo_ssr;
					CREATE TEMP TABLE w_file_valo_ssr AS
					SELECT
							file_name_annee_import,
							num_admin_sejour,
							numsemaine,
							hosp,
							MAX(txremb) AS txremb,
							SUM(dma_br*coeffprud*coeffspe*coefftrans) AS dma_br_2023,
							SUM(
								CASE 
								WHEN hosp = 'P' THEN nbjp * t_gmt_tarif_2024.tzf1
								ELSE 
									CASE 
									WHEN nbjp >= t_gmt_tarif_2024.dzf AND t_gmt_tarif_2024.fzf - t_gmt_tarif_2024.dzf <> 20 THEN t_gmt_tarif_2024.tzf1 
									WHEN nbjp >= t_gmt_tarif_2024.dzf AND t_gmt_tarif_2024.fzf - t_gmt_tarif_2024.dzf = 20 AND nbjp <= (t_gmt_tarif_2024.dzf + 6) THEN t_gmt_tarif_2024.tzf1 
									WHEN nbjp >= t_gmt_tarif_2024.dzf AND t_gmt_tarif_2024.fzf - t_gmt_tarif_2024.dzf = 20 AND nbjp BETWEEN (t_gmt_tarif_2024.dzf + 7) AND (t_gmt_tarif_2024.dzf + 13) THEN t_gmt_tarif_2024.tzf2 
									WHEN nbjp >= t_gmt_tarif_2024.dzf AND t_gmt_tarif_2024.fzf - t_gmt_tarif_2024.dzf = 20 AND nbjp >= t_gmt_tarif_2024.dzf + 14 THEN t_gmt_tarif_2024.tzf3 
									ELSE t_gmt_tarif_2024.tzb END + 
									CASE WHEN nbj_supp_zh > 0 THEN nbj_supp_zh * t_gmt_tarif_2024.szh ELSE 0 END +
									CASE WHEN nbjp < t_gmt_tarif_2024.dzf THEN (nbjp - 1) * t_gmt_tarif_2024.szb ELSE 0 END
								END
								* coeffgeo * coeffprud * coefftrans
							)	AS dma_br_2024,
							SUM(nbjp) AS nbjp,
							count(*)
					FROM pmsissr.p_file_valo_ssr
					JOIN pmsissr.p_ssr ON p_file_valo_ssr.ssr_id = p_ssr.oid
					JOIN pmsissr.t_gmt_tarif ON 
						p_file_valo_ssr.gmt_id = t_gmt_tarif.gmt_id AND
						t_gmt_tarif.type_ets = '1' AND
						LEAST(date_fin_sejour,'20231231') BETWEEN t_gmt_tarif.date_debut AND t_gmt_tarif.date_fin
					JOIN pmsissr.t_gmt_tarif t_gmt_tarif_2024 ON 
						p_file_valo_ssr.gmt_id = t_gmt_tarif_2024.gmt_id AND
						t_gmt_tarif_2024.type_ets = '1' AND
						'20240101' BETWEEN t_gmt_tarif_2024.date_debut AND t_gmt_tarif_2024.date_fin
					WHERE file_name_annee_import = 2023 AND
							dma_br <> 0 AND gmt <> '8888'

					GROUP BY 1,2,3,4
					;


					ANALYSE w_file_valo_ssr
					;

					CREATE INDEX iw_file_valo_ssr_1 ON w_file_valo_ssr USING btree (num_admin_sejour)
					;

					DROP TABLE IF EXISTS w_annee;
					CREATE TEMP TABLE w_annee AS
					SELECT left(semaine::text,4)::numeric AS annee, min(date) AS date_debut, max(date) AS date_fin
					FROM base.p_calendrier
					GROUP BY 1
					;

					ANALYSE w_annee
					;

					DROP TABLE IF EXISTS w_sejour
					;

					CREATE TEMP TABLE w_sejour AS
					SELECT
						file_name_annee_import,
						num_admin_sejour,
						GREATEST(MIN(date_entree), MIN(date_debut)) AS date_debut,
						LEAST(MIN(date_sortie), MIN(date_fin)) AS date_fin,
						SUM(dma_br_2023) AS pmsi_mt_2023,
						SUM(dma_br_2024) AS pmsi_mt_2024,
						SUM(nbjp) AS pmsi_nb,
						0::bigint AS service_facturation_id,
						''::text AS service_facturation_code,
						''::text AS service_facturation_texte,
						'0'::text AS est_facture,
						0::numeric AS fact_mt,
						0::numeric AS fact_nb
					FROM w_file_valo_ssr p_file_valo_ssr
						JOIN activite.p_sejours ON p_file_valo_ssr.num_admin_sejour = p_sejours.no_sejour
						JOIN w_annee ON file_name_annee_import = annee
					GROUP BY 1,2
					;

					ANALYSE w_sejour
					;

					UPDATE w_sejour SET
						est_facture = '1',
						service_facturation_id = subview.service_facturation_id,
						service_facturation_code = subview.service_facturation_code,
						service_facturation_texte = subview.service_facturation_texte,
						fact_mt = subview.fact_mt,
						fact_nb = subview.fact_nb
					FROM
					(
						SELECT
							file_name_annee_import,
							p_sejours.no_sejour,
							MAX(t_lieux.service_facturation_id) AS service_facturation_id,
							MAX(t_services_facturation.code) AS service_facturation_code,
							MAX(t_services_facturation.texte) AS service_facturation_texte,
							SUM(p_factures_lignes_c.montant_facture) AS fact_mt,
							SUM(CASE WHEN t_prestations.code IN ('PJ') THEN nb_prestation ELSE 0 END) AS fact_nb,
							max(pmsi_mt_2023),
							max(pmsi_nb)
						FROM w_sejour
							JOIN activite.p_sejours ON num_admin_sejour = p_sejours.no_sejour
							JOIN activite.p_factures ON num_admin_sejour = p_factures.no_sejour
							JOIN activite.p_factures_lignes_c ON
								p_factures_lignes_c.no_facture = p_factures.no_facture AND
								p_factures_lignes_c.date_debut BETWEEN w_sejour.date_debut AND w_sejour.date_fin
							JOIN activite.t_prestations ON prestation_id = t_prestations.oid
							JOIN activite.t_lieux ON lieu_sortie_id = t_lieux.oid
							JOIN activite.t_services_facturation ON t_lieux.service_facturation_id = t_services_facturation.oid
						WHERE t_prestations.code IN ('PJ','PMS', 'SSM', 'SNS', 'FS') AND
							montant_facture <> 0
						GROUP BY 1,2
					) subview
					WHERE w_sejour.file_name_annee_import = subview.file_name_annee_import AND
						w_sejour.num_admin_sejour = subview.no_sejour
					;

					SELECT
						file_name_annee_import,
						service_facturation_code,
						service_facturation_texte,
						sum(pmsi_mt_2023)/sum(pmsi_nb),
						sum(pmsi_mt_2024)/sum(pmsi_nb),
						sum(fact_mt)/sum(fact_nb),
						base.cti_median(pmsi_mt_2023/pmsi_nb),
						base.cti_median(pmsi_mt_2024/pmsi_nb),
						base.cti_median(fact_mt/fact_nb)
					FROM w_sejour
					WHERE fact_nb <> 0
					GROUP BY 1,2,3
					;
				]]></select>
				<FIELDS>
					<FIELD name="ANNEEIMPORT" /><!-- 				1	Année d'import valossr-->
					<FIELD name="SERV_FAC_CODE" /><!-- 				2	Code service facturation -->
					<FIELD name="SERV_FAC_TEXT" /><!-- 				3	Libellé service facturation -->
					<FIELD name="PUJOURPMSI2023" /><!-- 				4	Prix unitaire moyen depuis le PMSI par rapport au GME -->
					<FIELD name="PUJOURPMSI2024" /><!-- 				4	Prix unitaire moyen depuis le PMSI par rapport au GME -->
					<FIELD name="PUFACT" /><!-- 					5	Prix unitaire moyen facturé -->
					<FIELD name="MEDIANPUPMSI2023" /><!-- 				4	Prix unitaire médian depuis le PMSI par rapport au GME -->
					<FIELD name="MEDIANPUPMSI2024" /><!-- 				4	Prix unitaire médian depuis le PMSI par rapport au GME -->
					<FIELD name="MEDIANFACT" /><!-- 				5	Prix unitaire médian facturé -->
				</FIELDS>
			</SQL>
		</QUERY>
	</QUERIES>
	<PRESENTATION>
		<ONGLET excelLabel="Etude coefficient" label="Comparatif">
			<DATAGRID title="Etudes des coefficients GME / Facturés" total="false" headerHeight="48">
				<COLUMN dataField="ANNEEIMPORT" width="100" type="Number" outputFormat="#" headerText="Année import" textAlign="right" totalFunction="sum" otherFunction="sum"
				description="Année de l'import fichier valossr" />
				<COLUMN dataField="SERV_FAC_CODE" width="120" type="Char" headerText="Code service&#xD;facturation" textAlign="left" totalFunction="text" otherFunction="text"
				description="Code du service de facturation" />
				<COLUMN dataField="SERV_FAC_TEXT" minWidth="120" type="Char" headerText="Libellé service&#xD;facturation" textAlign="left" totalFunction="text" otherFunction="text"
				description="Libellé du service de facturation" />
				
				<COLUMN dataField="PUFACT" width="100" type="Number" outputFormat="#,0E" headerText="Moyenne PU&#xD;facturé" textAlign="right" totalFunction="sum" otherFunction="sum"
				description="Prix unitaire moyen facturé 2023 " />
				<COLUMN dataField="PUJOURPMSI2023" width="100" type="Number" outputFormat="#,0E" headerText="Moyenne PU&#xD;GME 2023 (pmsi)" textAlign="right" totalFunction="sum" otherFunction="sum"
				description="Prix unitaire moyen depuis le PMSI par rapport au GME" />
				<COLUMN dataField="PUJOURPMSI2024" width="100" type="Number" outputFormat="#,0E" headerText="Moyenne PU&#xD;GME 2024 (pmsi)" textAlign="right" totalFunction="sum" otherFunction="sum"
				description="Prix unitaire moyen depuis le PMSI par rapport au GME" />
				
				<COLUMN dataField="MEDIANFACT" width="100" type="Number" outputFormat="#,0E" headerText="Médian PU&#xD;facturé 2023" textAlign="right" totalFunction="sum" otherFunction="sum"
				description="Prix unitaire médian facturé" />
				<COLUMN dataField="MEDIANPUPMSI2023" width="100" type="Number" outputFormat="#,0E" headerText="Médian PU&#xD;GME 2023 (pmsi)" textAlign="right" totalFunction="sum" otherFunction="sum"
				description="Prix unitaire médian depuis le PMSI par rapport au GME" />
				<COLUMN dataField="MEDIANPUPMSI2024" width="100" type="Number" outputFormat="#,0E" headerText="Médian PU&#xD;GME 2024 (pmsi)" textAlign="right" totalFunction="sum" otherFunction="sum"
				description="Prix unitaire médian depuis le PMSI par rapport au GME" />
			</DATAGRID>
		</ONGLET>
	</PRESENTATION>
</VUE>