<?xml version="1.0" encoding="ISO-8859-15"?>
<VUE name="ACTI000107" label="Activité. Justificatif comptabilisation et en-cours" title="= 'Justificatif comptabilisation et 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_ACTI000104.HTML"
    helpDokuWIkiDir="activite:comptabilisation_et_en_cours:encours" 
    helpDokuWIkiFile="acti000104" 
>
	<VIEWPROPERTIES componentName="PROPERTIES_PRESTATIONS"/>
	<VIEWPROPERTIES>
		<PROPERTY name="OPTION2_WHERE">
			<value><![CDATA[[EVAL 
								switch ('[OPTION2]') {
								case 'SOR':
									return "AND montant_encours_sorti <> 0";
									break;
								default:
									return '';
								}
					EVAL]]]></value>
		</PROPERTY>
	</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="Options">
			<FIELD name="OPTION2" label="" UI="combo" width="200" default="-1" >
				<OPTION label="Présents et Sortis" data="-1"/>
				<OPTION label="Sortis uniquement" data="SOR"/>
				<OPTION label="En-cours uniquement" data="ENC"/>
			</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>
        <GROUP visible="false">
			<FIELD name="RUBRIQUE_CA" label="C.A." UI="combo" tree="true" width="200" default="-1"  globalListProvider="RUBRIQUES_CA" showIfNotDefault="true" />
			<FIELD name="RUBRIQUE_CODES" label="Codes" UI="text" width="150" default="" showIfNotDefault="true" visible="= SELECT.RUBRIQUE == 'SAISIECODE' " />
		</GROUP>
		<GROUP label="">
			<FIELD name="GROUPBY_2" label="Autres colonnes" UI="combo" default="-1" width="200" tree="true">
				<OPTION label="" data="-1"/>
				<OPTION label="PRESTATIONS_ENCOURS_GROUPBY" data="" globalListProvider="PRESTATIONS_ENCOURS_GROUPBY"/>
			</FIELD>
		</GROUP>
		<GROUP label="">
			<FIELD name="GROUPBY_3" label="" UI="combo" default="-1" width="200" tree="true">
				<OPTION label="" data="-1"/>
				<OPTION label="PRESTATIONS_ENCOURS_GROUPBY" data="" globalListProvider="PRESTATIONS_ENCOURS_GROUPBY"/>
			</FIELD>
		</GROUP>
	</SELECTIONS>

	<SELECTIONS componentName="FILTRES_PRESTATIONS_SELECTIONS"/>
	<SELECTIONS componentName="FILTRES_SEJOURS_SELECTIONS"/>
	<QUERIES>
		<QUERY label="">
			<SQL optimizer="V_SEJOURS_X" optimizer2="V_FACTURES_ENCOURS_LIGNES_C_X">
				<select><![CDATA[		
					
					
					DROP TABLE IF EXISTS ACTI000107_mvt;
					CREATE TEMP TABLE ACTI000107_mvt AS
					SELECT
						v_factures_lignes_c_4.no_sejour AS no_sejour,
						CASE WHEN v_factures_lignes_c_4.encours_date_groupage <> '20991231' THEN v_factures_lignes_c_4.encours_date_groupage ELSE NULL END AS encours_date_groupage,
						
						[VIEW.GROUPBY_2_OID_FIELD]::bigint AS oid2,
						[VIEW.GROUPBY_2_COD_FIELD]::text AS code2,
						[VIEW.GROUPBY_2_TXT_FIELD]::text AS texte2,
						[VIEW.GROUPBY_3_OID_FIELD]::bigint AS oid3,
						[VIEW.GROUPBY_3_COD_FIELD]::text AS code3,
						[VIEW.GROUPBY_3_TXT_FIELD]::text AS texte3,
						
						0::numeric AS montant_report_encours,
						0::numeric AS montant_report_encours_sorti,
						0::numeric AS montant_report_encours_present,
						
						0::numeric AS montant_comptabilise,
						
						SUM(montant_encours) AS montant_encours,
						SUM(montant_encours_sorti) AS montant_encours_sorti,
						SUM(montant_encours_present) AS montant_encours_present,
						SUM(montant_encours_groupe) AS montant_encours_groupe,
						SUM(montant_encours_non_groupe) AS montant_encours_non_groupe,
						SUM(montant_encours_non_groupe_sorti) AS montant_encours_non_groupe_sorti,
						SUM(montant_encours_non_groupe_present) AS montant_encours_non_groupe_present
						
					FROM #V_FACTURES_ENCOURS_LIGNES_C_X#
					WHERE 
						date_trunc('month', date_comptable) = date_trunc('month', '[TOPERIODE]'::date) AND
						code_origine = '[SOURCE_ENCOURS]'
						[VIEW.OPTION2_WHERE]
						[VIEW.SEJOURS_SELECT]
						[VIEW.PRESTATIONS_SELECT]
						
					GROUP BY 1,2,3,4,5,6,7,8
					;
						
						
					INSERT INTO ACTI000107_mvt
					SELECT
						v_factures_lignes_c_4.no_sejour AS no_sejour,
						NULL::date AS date_groupage,
						
						[VIEW.GROUPBY_2_OID_FIELD]::bigint AS oid2,
						[VIEW.GROUPBY_2_COD_FIELD]::text AS code2,
						[VIEW.GROUPBY_2_TXT_FIELD]::text AS texte2,
						[VIEW.GROUPBY_3_OID_FIELD]::bigint AS oid3,
						[VIEW.GROUPBY_3_COD_FIELD]::text AS code3,
						[VIEW.GROUPBY_3_TXT_FIELD]::text AS texte3,
						
						SUM(montant_encours) AS montant_report_encours,
						SUM(montant_encours_sorti) AS montant_report_encours_sorti,
						SUM(montant_encours_present) AS montant_report_encours_present,
						
						0::numeric AS montant_comptabilise,
						
						0::numeric AS montant_encours,
						0::numeric AS montant_encours_sorti,
						0::numeric AS montant_encours_present,
						0::numeric AS montant_encours_groupe,
						0::numeric AS montant_encours_non_groupe,
						0::numeric AS montant_encours_non_groupe_sorti,
						0::numeric AS montant_encours_non_groupe_present
						
					FROM #V_FACTURES_ENCOURS_LIGNES_C_X#
					WHERE 
						date_trunc('month', date_comptable) = date_trunc('month', '[PERIODE]'::date - interval '1 month') AND
						code_origine = '[SOURCE_ENCOURS]' AND
						'[OPTION2]' <> 'ENC'
						[VIEW.OPTION2_WHERE]
						[VIEW.SEJOURS_SELECT]
						[VIEW.PRESTATIONS_SELECT]
						
					GROUP BY 1,2,3,4,5,6,7,8
					;
						
					
				]]></select>
			</SQL>
			<SQL optimizer="V_SEJOURS_X" optimizer2="V_FACTURES_LIGNES_C_X">
				<select><![CDATA[		
										
					INSERT INTO ACTI000107_mvt
					SELECT
						v_sejours_1.no_sejour AS no_sejour,
						NULL::date AS date_groupage,
						
						[VIEW.GROUPBY_2_OID_FIELD]::bigint AS oid2,
						[VIEW.GROUPBY_2_COD_FIELD]::text AS code2,
						[VIEW.GROUPBY_2_TXT_FIELD]::text AS texte2,
						[VIEW.GROUPBY_3_OID_FIELD]::bigint AS oid3,
						[VIEW.GROUPBY_3_COD_FIELD]::text AS code3,
						[VIEW.GROUPBY_3_TXT_FIELD]::text AS texte3,
						
						0::numeric AS montant_report_encours,
						0::numeric AS montant_report_encours_sorti,
						0::numeric AS montant_report_encours_present,
						
						SUM(montant_comptabilise) AS montant_comptabilise,
						
						0::numeric AS montant_encours,
						0::numeric AS montant_encours_sorti,
						0::numeric AS montant_encours_present,
						0::numeric AS montant_encours_groupe,
						0::numeric AS montant_encours_non_groupe,
						0::numeric AS montant_encours_non_groupe_sorti,
						0::numeric AS montant_encours_non_groupe_present
						
					FROM #V_FACTURES_LIGNES_C_X#
						JOIN #V_SEJOURS_X# ON v_sejours_1.no_sejour = v_factures_lignes_c_4.no_sejour
					WHERE 
						date_trunc('month', date_vente) BETWEEN '[PERIODE]' AND '[TOPERIODE]'AND
						'[OPTION2]' <> 'ENC'
						[VIEW.SEJOURS_SELECT]
						[VIEW.PRESTATIONS_SELECT]
						
					GROUP BY 1,2,3,4,5,6,7,8
					;
						
					
				]]></select>
			</SQL>
			
			<SQL optimizer="V_SEJOURS_X">
				<select><![CDATA[		
					SELECT 
						ACTI000107_mvt.no_sejour, 
						nom || ' '|| prenom,
                        v_sejours_1.no_patient,
						date_entree,
						date_sortie,
						
						oid2,
						code2,
						texte2,
						oid3,
						code3,
						texte3,
												
						MAX(encours_date_groupage),
						
						SUM(montant_report_encours),
						SUM(montant_report_encours_sorti),
						SUM(montant_report_encours_present),
						
						SUM(montant_comptabilise),
						
						SUM(montant_encours),
						SUM(montant_encours_sorti),
						SUM(montant_encours_present),
	
						SUM(montant_encours_groupe),
						SUM(montant_encours_non_groupe),
						SUM(montant_encours_non_groupe_sorti),
						SUM(montant_encours_non_groupe_present),
						
						SUM(montant_comptabilise) - SUM(montant_report_encours) + SUM(montant_encours)
						
					FROM ACTI000107_mvt
						JOIN #V_SEJOURS_X# ON ACTI000107_mvt.no_sejour = v_sejours_1.no_sejour
						LEFT JOIN activite.p_patients ON v_sejours_1.no_patient = p_patients.no_patient
					GROUP BY 1,2,3,4,5,6,7,8,9,10,11

					UNION ALL

					SELECT 
						'-9999999', 
						NULL, 
						NULL, 
						NULL, 
						NULL, 
						
						NULL, 
						NULL, 
						NULL, 
						NULL,
						NULL, 
						NULL, 
						
						NULL, 
						
						SUM(montant_report_encours),
						SUM(montant_report_encours_sorti),
						SUM(montant_report_encours_present),
						
						SUM(montant_comptabilise),
						
						SUM(montant_encours),
						SUM(montant_encours_sorti),
						SUM(montant_encours_present),
						
						SUM(montant_encours_groupe),
						SUM(montant_encours_non_groupe),
						SUM(montant_encours_non_groupe_sorti),
						SUM(montant_encours_non_groupe_present),
						
						SUM(montant_comptabilise) - SUM(montant_report_encours) + SUM(montant_encours)
					FROM ACTI000107_mvt
						;
				
				
				]]></select>
				<FIELDS>
					<FIELD name="SEJ"/>
					<FIELD name="NOM" ifHideNames="Non autorisé"/>
					<FIELD name="NPAT"/>
					<FIELD name="DEN"/>
					<FIELD name="DSO"/>
					<FIELD name="OID2"/>
					<FIELD name="COD2"/>
					<FIELD name="TXT2"/>
					<FIELD name="OID3"/>
					<FIELD name="COD3"/>
					<FIELD name="TXT3"/>
					<FIELD name="DGR"/>
					<FIELD name="R_MT"/>
					<FIELD name="R_MTS"/>
					<FIELD name="R_MTP"/>
					<FIELD name="V_MT"/>
					<FIELD name="E_MT"/>
					<FIELD name="E_MTS"/>
					<FIELD name="E_MTP"/>
					<FIELD name="E_MTG"/>
					<FIELD name="E_MTNG"/>
					<FIELD name="E_MTNGS"/>
					<FIELD name="E_MTNGP"/>
					<FIELD name="VE_MT"/>
				</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="54" 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="DEN" 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="DSO" width="60" type="Date" inputFormat="AAAA-MM-JJ" outputFormat="DD/MM/YY" fixed="false" headerText="Date sortie" textAlign="right" totalFunction="text">
				</COLUMN>
				
				<COLUMN dataField="DGR" width="60" type="Date" inputFormat="AAAA-MM-JJ" visibleCondition="SELECT.OPTION2 == 'ENC'" outputFormat="DD/MM/YY" fixed="false" headerText="Date groupage" textAlign="right" totalFunction="text">
				</COLUMN>
				
				<COLUMN dataField="COD2"
                    width="70"
                    type="Char"
                    visible="true"
                    headerText="SELECT.GROUPBY_2_LABEL + '\r(code)'"
                    headerTextCalc="true"
                    textAlign="left"
                    otherFunction="text"
                    totalFunction="text"
                    totalComplement=""
                    visibleCondition="SELECT.GROUPBY_2 != -1">
                    <HEADERCELLSTYLE name="textAlign" value="left"/>
				</COLUMN> 
				<COLUMN dataField="TXT2"
                    type="Char"
                    minWidth="150"
                    maxWidth="300"
                    headerText="SELECT.GROUPBY_2_LABEL "
                    headerTextCalc="true"
                    textAlign="left"
                    totalFunction="text"
                    otherFunction="text"
                    otherComplement="*ROW"
                    visibleCondition="SELECT.GROUPBY_2 != -1">
                    <HEADERCELLSTYLE name="textAlign" value="left"/>
				</COLUMN>
                <COLUMN dataField="COD3"
                    width="70"
                    type="Char"
                    visible="true"
                    headerText="SELECT.GROUPBY_3_LABEL + '\r(code)'"
                    headerTextCalc="true"
                    textAlign="left"
                    otherFunction="text"
                    totalFunction="text"
                    totalComplement=""
                    visibleCondition="SELECT.GROUPBY_3 != -1">
                    <HEADERCELLSTYLE name="textAlign" value="left"/>
				</COLUMN>
				<COLUMN dataField="TXT3"
                    type="Char"
                    minWidth="150"
                    maxWidth="300"
                    headerText="SELECT.GROUPBY_3_LABEL + '\r(texte)'"
                    headerTextCalc="true"
                    textAlign="left"
                    totalFunction="text"
                    otherFunction="text"
                    otherComplement="*ROW"
                    visibleCondition="SELECT.GROUPBY_3 != -1">
                    <HEADERCELLSTYLE name="textAlign" value="left"/>
				</COLUMN>
				
				<COLUMN dataField="R_MT" width="90" type="Number" outputFormat="#E" visibleCondition="SELECT.OPTION2 != 'ENC'" headerText="Report@LF@En-cours@LF@Montant@LF@A" textAlign="right" totalFunction="sum" otherFunction="">					
				</COLUMN>
			
				<COLUMN dataField="R_MTS" width="90" type="Number" outputFormat="#E" visibleCondition="SELECT.OPTION2 == '-1'" headerText="Report@LF@En-cours@LF@Dont sortis" textAlign="right" totalFunction="sum" otherFunction="">					
				</COLUMN>
			
				<COLUMN dataField="R_MTP" width="90" type="Number" outputFormat="#E" visibleCondition="SELECT.OPTION2 == '-1'" headerText="Report@LF@En-cours@LF@Dont présents" textAlign="right" totalFunction="sum" otherFunction="">					
				</COLUMN>
				
				<COLUMN dataField="V_MT" width="90" type="Number" outputFormat="#E" visibleCondition="SELECT.OPTION2 != 'ENC'" headerText="Ventes@LF@@LF@Montant@LF@B" textAlign="right" totalFunction="sum" otherFunction="">
				</COLUMN>
				
				<COLUMN dataField="E_MT" width="90" type="Number" outputFormat="#E" visibleCondition="(SELECT.OPTION2 == '-1') || (SELECT.OPTION2 == 'ENC')" headerText="En-cours@LF@@LF@Montant@LF@C" textAlign="right" totalFunction="sum" otherFunction="">					
				</COLUMN>
				
				<COLUMN dataField="E_MTS" width="90" type="Number" outputFormat="#E" visibleCondition="(SELECT.OPTION2 == '-1') || (SELECT.OPTION2 == 'ENC')" headerText="En-cours@LF@@LF@Dont sortis" textAlign="right" totalFunction="sum" otherFunction="">		
				</COLUMN>
				
				<COLUMN dataField="E_MTP" width="90" type="Number" outputFormat="#E" visibleCondition="(SELECT.OPTION2 == '-1') || (SELECT.OPTION2 == 'ENC')" headerText="En-cours@LF@@LF@Dont présents" textAlign="right" totalFunction="sum" otherFunction="">	
				</COLUMN>
				
				<COLUMN dataField="VE_MT" width="90" type="Number" outputFormat="#E"  visibleCondition="SELECT.OPTION2 != 'ENC'" headerText="Ventes@LF@+ En-cours@LF@Montant@LF@B+C-A" textAlign="right" totalFunction="sum" otherFunction="">		
				</COLUMN>
				
				<COLUMN dataField="E_MTG" width="90" type="Number" outputFormat="#E" visibleCondition="SELECT.OPTION2 == 'ENC'" headerText="Dont Groupés ou Hors PMSI" textAlign="right" totalFunction="sum" otherFunction="">
				</COLUMN>
			
				<COLUMN dataField="E_MTNGS" width="90" type="Number" outputFormat="#E" visibleCondition="SELECT.OPTION2 == 'ENC'" headerText="Dont Sortis Non groupés" textAlign="right" totalFunction="sum" otherFunction="">
				</COLUMN>		
				
			</DATAGRID>
		</ONGLET>
	</PRESENTATION>
</VUE>
