<?xml version="1.0" encoding="ISO-8859-15"?>
<VUE name="ACTI000106" label="Activité. Justificatif évaluation valorisation en-cours" title="= 'Justificatif évaluation valorisation en-cours (' +  SELECT.PERIODE_LABEL + ' à ' + SELECT.TOPERIODE_LABEL + ')'" database="iCTI" swf="*CTI_view1" softCode="iCTI_activite" globals="ACTI_globals.XML" componentsFile="ACTI_components.XML" 
    helpFile="iCTI_ACTI_ACTI000105.HTML"
    helpDokuWIkiDir="activite:comptabilisation_et_en_cours:evaluation_des_en_cours" 
    helpDokuWIkiFile="acti000105" 
>
	<VIEWPROPERTIES componentName="PROPERTIES_PRESTATIONS"/>
	<VIEWPROPERTIES>
		
	</VIEWPROPERTIES>
	<SELECTIONS label="Paramètres de l'analyse" displayText="= var display:String = '';
						display = display + 'Source en-cours : #B#' + SELECT.SOURCE_ENCOURS_LABEL + '#/B#';
						
						return display;
						">
		<GROUP label="Période comptable">
			<FIELD name="ALIAS_PERIODE" label="" UI="combo" width="220" default="CUM_LAST_MONTH" tree="true" globalListProvider="ALIAS_PERIODE" />
            <FIELD name="PERIODE" label="de" UI="combo" tree="true" width="200" default="2009-01-01" globalListProvider="DATE_DEBUT" >
			</FIELD>
			<FIELD name="TOPERIODE" label="à" UI="combo" tree="true" width="200" default="2009-01-31" globalListProvider="DATE_FIN" >
			</FIELD>
		</GROUP>
		<GROUP label="Source en-cours">
			<FIELD name="SOURCE_ENCOURS" label="" UI="combo" width="200" default="C" >
				<OPTION label="CTI Santé" data="C"/>
				<OPTION label="Prestataire facturation" data="P"/>
			</FIELD>
		</GROUP>
	</SELECTIONS>

	<SELECTIONS componentName="FILTRES_PRESTATIONS_SELECTIONS"/>
	<SELECTIONS componentName="FILTRES_SEJOURS_SELECTIONS"/>
	<SELECTIONS componentName="FILTRES_PASSAGES_SELECTIONS" />
	<QUERIES>
		<QUERY label="">
			<SQL optimizer="V_SEJOURS_X" optimizer2="V_FACTURES_ENCOURS_LIGNES_C_X">
				<select><![CDATA[		
					DROP TABLE IF EXISTS ACTI000106_fac;
					CREATE TEMP TABLE ACTI000106_fac AS 
					SELECT 
						p_sejours.no_patient AS w_no_patient, 
						p_factures.no_sejour AS w_no_sejour,
						p_factures_encours.no_sejour AS w_no_sejour_encours, 
						p_factures.no_facture_reference AS w_no_facture,
						p_factures_encours.date_encours AS w_date_encours,
						CASE WHEN p_factures_encours.no_facture LIKE '%EV%' THEN '1' ELSE '0' END AS w_from_vente,
						base.cti_array_accum(p_factures_encours.no_facture) AS w_no_facture_encours_array,
						MIN(COALESCE(p_factures.date_debut, p_factures_encours.date_debut)) AS date_debut,
						MAX(COALESCE(p_factures.date_fin, p_factures_encours.date_fin)) AS date_fin,
						MAX
						(
							CASE
							  WHEN p_sejours.ghs_id > 0 THEN p_sejours.date_groupage
							  ELSE '2099-12-31'::date
							END
						 ) AS date_groupage,
						MAX(COALESCE(p_factures.date_facture,'20991231')) AS date_facture,
						MAX(COALESCE(p_factures.date_vente,'20991231')) AS date_vente
					FROM activite.p_factures_encours
                        JOIN #V_FACTURES_ENCOURS_LIGNES_C_X# ON v_factures_lignes_c_4.no_facture = p_factures_encours.no_facture
						JOIN activite.p_sejours ON p_factures_encours.no_sejour = p_sejours.no_sejour
						JOIN activite.t_lieux ON p_sejours.lieu_sortie_id = t_lieux.oid
						JOIN activite.t_services_facturation ON service_facturation_id = t_services_facturation.oid
						LEFT JOIN activite.p_factures ON 
							(
								avec_facturation_intermediaire <> '1' AND p_factures.no_sejour = p_factures_encours.no_sejour OR
								avec_facturation_intermediaire = '1' AND p_factures.no_facture = p_factures_encours.no_facture_reference
							) AND
							(
							p_factures.no_facture LIKE '%.BG' AND p_factures_encours.no_facture LIKE '%.BG%' OR
							p_factures.no_facture NOT LIKE '%.BG' AND p_factures_encours.no_facture NOT LIKE '%.BG%'
							)
					WHERE p_factures_encours.code_sorti = '1' AND
						p_factures_encours.montant_encours_c <> 0 AND
						p_factures.code_vente = '1' AND
						p_factures_encours.date_encours BETWEEN '[PERIODE]' AND '[TOPERIODE]' AND
						p_factures_encours.code_origine = '[SOURCE_ENCOURS]'
						[VIEW.SEJOURS_SELECT]
						[VIEW.PASSAGES_SELECT]
					GROUP BY 1,2,3,4,5,6
					HAVING SUM(p_factures_encours.montant_encours_c) <> 0;

					ANALYZE ACTI000106_fac;
				]]></select>
			</SQL>
			<SQL optimizer="V_SEJOURS_X" optimizer2="V_FACTURES_ENCOURS_LIGNES_C_X">
				<select><![CDATA[	
					
					DROP TABLE IF EXISTS ACTI000106_mvt;
					CREATE TEMP TABLE ACTI000106_mvt AS
					SELECT
						w_no_patient AS no_patient,
						w_no_sejour_encours AS no_sejour_encours,
						p_factures_encours.no_facture_reference AS no_facture,
						w_date_encours AS date_encours,
						ACTI000106_fac.date_debut,
						ACTI000106_fac.date_fin,
						ACTI000106_fac.date_groupage,
						ACTI000106_fac.date_facture,
						ACTI000106_fac.date_vente,
						SUM(nb_rubrique * coefficient) AS nb_rubrique_encours_sorti,
						SUM(CASE WHEN w_no_facture IS NULL THEN nb_rubrique * coefficient ELSE 0 END) AS nb_rubrique_encours_sorti_non_compta,
						SUM(CASE WHEN w_no_facture IS NOT NULL THEN nb_rubrique * coefficient ELSE 0 END) AS nb_rubrique_encours_sorti_compta,
						SUM(nb_prestation * coefficient) AS nb_prestation_encours_sorti,
						SUM(CASE WHEN w_no_facture IS NULL THEN nb_prestation * coefficient ELSE 0 END) AS nb_prestation_encours_sorti_non_compta,
						SUM(CASE WHEN w_no_facture IS NOT NULL THEN nb_prestation * coefficient ELSE 0 END) AS nb_prestation_encours_sorti_compta,
						SUM(montant_encours_sorti) AS montant_encours_sorti,
						SUM(CASE WHEN w_no_facture IS NULL THEN montant_encours_sorti ELSE 0 END) AS montant_encours_sorti_non_compta,
						SUM(CASE WHEN w_no_facture IS NOT NULL THEN montant_encours_sorti ELSE 0 END) AS montant_encours_sorti_compta,
						SUM(CASE WHEN ACTI000106_fac.w_from_vente = '1' THEN nb_rubrique * coefficient ELSE 0 END) AS nb_rubrique_comptabilise,
						SUM(CASE WHEN ACTI000106_fac.w_from_vente = '1' THEN nb_prestation * coefficient ELSE 0 END) AS nb_prestation_comptabilise,
						SUM(CASE WHEN ACTI000106_fac.w_from_vente = '1' THEN montant_encours_sorti ELSE 0 END) AS montant_comptabilise
					FROM #V_FACTURES_ENCOURS_LIGNES_C_X#
					JOIN activite.p_factures_encours ON 
						v_factures_lignes_c_4.no_facture = p_factures_encours.no_facture
						JOIN ACTI000106_fac ON 
							p_factures_encours.no_sejour = w_no_sejour AND
							p_factures_encours.no_facture = ANY(w_no_facture_encours_array) AND
							p_factures_encours.date_encours = w_date_encours 
					WHERE 
						montant_encours_sorti <> 0 AND
						date_comptable BETWEEN '[PERIODE]' AND '[TOPERIODE]' AND
						p_factures_encours.code_origine = '[SOURCE_ENCOURS]'
						[VIEW.PRESTATIONS_SELECT]
					GROUP BY 1,2,3,4,5,6,7,8,9;
				]]></select>
			</SQL>
			
			<SQL optimizer="V_SEJOURS_X" optimizer2="V_FACTURES_LIGNES_C_X">
				<select><![CDATA[	
					INSERT INTO ACTI000106_mvt
					SELECT 
						w_no_patient AS no_patient,
						v_factures_lignes_c_4.no_sejour,
						p_factures.no_facture_reference AS no_facture,
						w_date_encours AS date_encours,
						ACTI000106_fac.date_debut,
						ACTI000106_fac.date_fin,
						ACTI000106_fac.date_groupage,
						ACTI000106_fac.date_facture,
						ACTI000106_fac.date_vente,
						0,0,0,0,0,0,0,0,0,
						SUM(nb_rubrique * coefficient) AS nb_rubrique_comptabilise,
						SUM(nb_prestation * coefficient) AS nb_prestation_comptabilise,
						SUM(montant_comptabilise+montant_comptabilise_budget_global_1) AS montant_comptabilise
					FROM #V_FACTURES_LIGNES_C_X#
						JOIN #V_SEJOURS_X# ON v_sejours_1.no_sejour = v_factures_lignes_c_4.no_sejour
						JOIN activite.p_factures ON v_factures_lignes_c_4.no_facture = p_factures.no_facture
						JOIN ACTI000106_fac ON 
							p_factures.no_facture_reference = w_no_facture AND
							ACTI000106_fac.w_from_vente = '0'
					WHERE 
						(montant_comptabilise <> 0 OR montant_comptabilise_budget_global_1 <> 0) 
						[VIEW.PRESTATIONS_SELECT]
					GROUP BY 1,2,3,4,5,6,7,8,9;
						
					
				]]></select>
			</SQL>
			
			<SQL>
				<select><![CDATA[		
					SELECT no_sejour_encours, 
						nom || ' '|| prenom,
                        p_patients.no_patient,
						no_facture, 
						date_debut,
						date_fin,
						date_groupage,
						date_facture,
						date_vente,
						date_encours,
						SUM(nb_prestation_encours_sorti_non_compta), 
						SUM(montant_encours_sorti_non_compta), 
						SUM(nb_prestation_encours_sorti_compta), 
						SUM(montant_encours_sorti_compta), 
						SUM(nb_prestation_comptabilise), 
						SUM(montant_comptabilise),
						SUM(montant_comptabilise) - SUM(montant_encours_sorti_compta)
					FROM ACTI000106_mvt
						LEFT JOIN activite.p_patients ON ACTI000106_mvt.no_patient = p_patients.no_patient
					GROUP BY 1,2,3,4,5,6,7,8,9,10

					UNION ALL

					SELECT '-9999999', 
						NULL, 
						NULL, 
						NULL, 
						NULL, 
						NULL, 
						NULL, 
						NULL, 
						NULL, 
						NULL, 
						SUM(nb_prestation_encours_sorti_non_compta), 
						SUM(montant_encours_sorti_non_compta), 
						SUM(nb_prestation_encours_sorti_compta), 
						SUM(montant_encours_sorti_compta), 
						SUM(nb_prestation_comptabilise), 
						SUM(montant_comptabilise),
						SUM(montant_comptabilise) - SUM(montant_encours_sorti_compta)
					FROM ACTI000106_mvt
						;
				
				
				]]></select>
				<FIELDS>
					<FIELD name="SEJ"/>
					<FIELD name="NOM"/>
					<FIELD name="NPAT"/>
					<FIELD name="FAC"/>
					<FIELD name="DDB"/>
					<FIELD name="DFI"/>
					<FIELD name="DGR"/>
					<FIELD name="DFA"/>
					<FIELD name="DVT"/>
					<FIELD name="DEN"/>
					<FIELD name="NENC"/>
					<FIELD name="MENC"/>
					<FIELD name="NEN"/>
					<FIELD name="MEN"/>
					<FIELD name="NVT"/>
					<FIELD name="MVT"/>
					<FIELD name="MEC"/>
				</FIELDS>
			</SQL>
					
		</QUERY>
		<QUERY type="propertiesLink" name="VIEWPROPERTIES" forRows="false">
		</QUERY>
	</QUERIES>
	<PRESENTATION>
		<VIEWLINKS>
			<VIEWLINK label="Détail séjour" shortLabel="Détail séjour" view="ACTI000020.XML">
				<ARG name="SEJOUR" value="ROW.SEJ"/>
			</VIEWLINK>
		</VIEWLINKS>
		<ONGLET excelLabel="Factures" label="Factures">
			<DATAGRID title="" total="true" headerHeight="48" totalRowInRows="= ROW.SEJ == '-9999999'">
				<COLUMN dataField="SEJ" frozen="left" width="100" type="Char" headerText="No séjour" textAlign="left" otherFunction="text" totalFunction="text" totalComplement="TOTAL">
				</COLUMN>
                <COLUMN dataField="NPAT" frozen="left" width="100" type="Char" visible="true" headerText="N° Patient" textAlign="left" otherFunction="text" totalFunction="text">
				</COLUMN>
				<COLUMN dataField="NOM" frozen="left" type="Char" minWidth="50" headerText="Nom" textAlign="left" totalFunction="text" totalComplement="">
				</COLUMN>
				<COLUMN dataField="FAC" width="100" type="Char" headerText="No facture" textAlign="left" otherFunction="text" totalFunction="text" >
				</COLUMN>
				<COLUMN dataField="DDB" width="60" type="Date" inputFormat="AAAA-MM-JJ" outputFormat="DD/MM/YY" fixed="false" headerText="Date entrée" textAlign="right" totalFunction="text">
				</COLUMN>
				<COLUMN dataField="DFI" width="60" type="Date" inputFormat="AAAA-MM-JJ" outputFormat="DD/MM/YY" fixed="false" headerText="Date sortie" textAlign="right" totalFunction="text">
				</COLUMN>
				<COLUMN dataField="DEN" width="60" type="Date" inputFormat="AAAA-MM-JJ" outputFormat="DD/MM/YY" fixed="false" headerText="Date encours" textAlign="right" totalFunction="text">
				</COLUMN>
				<COLUMN dataField="DGR" width="60" type="Date" inputFormat="AAAA-MM-JJ" outputFormat="DD/MM/YY" fixed="false" headerText="Date groupage" textAlign="right" totalFunction="text">
				</COLUMN>
				<COLUMN dataField="DFA" width="60" type="Date" inputFormat="AAAA-MM-JJ" outputFormat="DD/MM/YY" fixed="false" headerText="Date facture" textAlign="right" totalFunction="text">
				</COLUMN>
				<COLUMN dataField="DVT" width="60" type="Date" inputFormat="AAAA-MM-JJ" outputFormat="DD/MM/YY" fixed="false" headerText="Date compta" textAlign="right" totalFunction="text">
				</COLUMN>
				<COLUMN dataField="NENC" width="90" type="Number" outputFormat="#" visible="true" headerText="Nombre encours (non comptabilisé)" textAlign="right" totalFunction="sum" otherFunction="">
				</COLUMN>
				<COLUMN dataField="MENC" width="90" type="Number" outputFormat="#E" visible="true" headerText="Montant encours (non comptabilisé)" textAlign="right" totalFunction="sum" otherFunction="">
				</COLUMN>
				<COLUMN dataField="NEN" width="90" type="Number" outputFormat="#" visible="true" headerText="Nombre encours (comptabilisé)" textAlign="right" totalFunction="sum" otherFunction="">
				</COLUMN>
				<COLUMN dataField="MEN" width="90" type="Number" outputFormat="#E" visible="true" headerText="Montant encours (comptabilisé)" textAlign="right" totalFunction="sum" otherFunction="">
				</COLUMN>
				<COLUMN dataField="NVT" width="90" type="Number" outputFormat="#" visible="true" headerText="Nombre compta" textAlign="right" totalFunction="sum" otherFunction="">
				</COLUMN>
				<COLUMN dataField="MVT" width="90" type="Number" outputFormat="#E" visible="true" headerText="Montant compta" textAlign="right" totalFunction="sum" otherFunction="">
				</COLUMN>
				<COLUMN dataField="MEC" width="90" type="Number" outputFormat="#E" visible="true" headerText="Montant écart" textAlign="right" totalFunction="sum" otherFunction="">
				</COLUMN>
			</DATAGRID>
		</ONGLET>
	</PRESENTATION>
</VUE>
