<?xml version="1.0" encoding="ISO-8859-15"?>
<VUE name="ACTI000023" label="Activité. Séjours compte client Clinique" title="= 'Clients Clinique (' + 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_ACTI000085.HTML"
    helpDokuWIkiDir="activite:compte_client:comptes _clients_clinique" 
    helpDokuWIkiFile="acti000022" 
>
    
	<VIEWPROPERTIES componentName="PROPERTIES_COMPTE_CLIENT"/>
	<SELECTIONS label="Paramètres de l'analyse" displayText="= var display:String = '';
					
					if (SELECT.PERIODEV  != '0001-01-01' || SELECT.TOPERIODEV  != '2099-12-31') {
						if (SELECT.PERIODEV  == '0001-01-01') {
							display = display + 'Date ventes : #B#jusqu à ' + SELECT.TOPERIODEV_LABEL + '#/B#';
						}
						else {
							if (SELECT.TOPERIODEV  == '2099-12-31') {
								display = display + 'Date ventes : #B#A partir de ' + SELECT.PERIODEV_LABEL + '#/B#';
							}
							else {
								display = display + 'Date ventes : #B#Entre ' + SELECT.PERIODEV_LABEL + ' et ' + SELECT.TOPERIODEV_LABEL + '#/B#';
							}
						}
					}
					
					if (SELECT.PERIODES  != '0001-01-01' || SELECT.TOPERIODES  != '2099-12-31') {
						if (SELECT.PERIODES  == '0001-01-01') {
							display = display + 'Date sortie : #B#jusqu à ' + SELECT.TOPERIODES_LABEL + '#/B#';
						}
						else {
							if (SELECT.TOPERIODES  == '2099-12-31') {
								display = display + 'Date sortie : #B#A partir de ' + SELECT.PERIODES_LABEL + '#/B#';
							}
							else {
								display = display + 'Date sortie : #B#Entre ' + SELECT.PERIODES_LABEL + ' et ' + SELECT.TOPERIODES_LABEL + '#/B#';
							}
						}
					}
					
					return display;
					">
		<GROUP label="Periode comptable">
			<FIELD name="PERIODE" label="" UI="combo" tree="true" width="200" default="2007-01-01">
				<OPTION dataLink="MOIS_D" dataField="date" data2Field="mois" treeLevelField="lvl" labelField="txt"/>
			</FIELD>
			<FIELD name="TOPERIODE" label="à" UI="combo" tree="true" width="200" default="2007-01-31">
				<OPTION dataLink="MOIS_F" dataField="date" data2Field="mois" treeLevelField="lvl" labelField="txt"/>
			</FIELD>
		</GROUP>
		<GROUP label="Periode ventes">
			<FIELD name="PERIODEV" label="" UI="combo" tree="true" width="200" default="0001-01-01">
				<OPTION label="Pas de sélection" data="0001-01-01"/>
				<OPTION dataLink="MOIS_D" dataField="date" data2Field="mois" treeLevelField="lvl" labelField="txt"/>
			</FIELD>
			<FIELD name="TOPERIODEV" label="à" UI="combo" tree="true" width="200" default="2099-12-31">
				<OPTION label="Pas de sélection" data="2099-12-31"/>
				<OPTION dataLink="MOIS_F" dataField="date" data2Field="mois" treeLevelField="lvl" labelField="txt"/>
			</FIELD>
		</GROUP>
		<GROUP label="Periode Sortie">
			<FIELD name="PERIODES" label="" UI="combo" tree="true" width="200" default="0001-01-01">
				<OPTION label="Pas de sélection" data="0001-01-01"/>
				<OPTION dataLink="MOIS_D" dataField="date" data2Field="mois" treeLevelField="lvl" labelField="txt"/>
			</FIELD>
			<FIELD name="TOPERIODES" label="à" UI="combo" tree="true" width="200" default="2099-12-31">
				<OPTION label="Pas de sélection" data="2099-12-31"/>
				<OPTION dataLink="MOIS_F" dataField="date" data2Field="mois" treeLevelField="lvl" labelField="txt"/>
			</FIELD>
		</GROUP>
		<GROUP label="Détail">
			<FIELD name="QFAC" label="Factures" UI="check" labelWidth="60" selectedValue="1" unselectedValue="0" default="1" showIfNotDefault="true"/>
			<FIELD name="QTP" label="Tiers" UI="check" labelWidth="40" selectedValue="1" unselectedValue="0" default="1" showIfNotDefault="true"/>
		</GROUP>
		<GROUP label="Montants">
			<FIELD name="QSD" label="Solde début" UI="check" labelWidth="80" selectedValue="1" unselectedValue="0" default="1" showIfNotDefault="true"/>
			<FIELD name="QMC" label="Comptabilisé" UI="check" labelWidth="80" selectedValue="1" unselectedValue="0" default="1" showIfNotDefault="true"/>
			<FIELD name="QMR" label="Réglé" UI="check" labelWidth="40" selectedValue="1" unselectedValue="0" default="1" showIfNotDefault="true"/>
			<FIELD name="QSF" label="Solde fin" UI="check" labelWidth="60" selectedValue="1" unselectedValue="0" default="1" showIfNotDefault="true"/>
		</GROUP>
	</SELECTIONS>
	<SELECTIONS componentName="FILTRES_CLIENTS_SELECTIONS"/>
	<SELECTIONS componentName="FILTRES_SEJOURS_SELECTIONS"/>
	<SELECTIONS componentName="FILTRES_PASSAGES_SELECTIONS" />
	<QUERIES>
		<QUERY label="">
			<SQL optimizer="V_SEJOURS_X" optimizer2="V_FACTURE_SOLDE_TIERS_C_X">
				<select><![CDATA[		
					SELECT 
							'',
							v_facture_solde_tiers_c_1.no_sejour,
							nom || ' ' || prenom, 
							v_sejours_1.no_patient, 
							date_entree, 
							date_sortie,
							CASE WHEN '[QFAC]' = '1' THEN v_facture_solde_tiers_c_1.no_facture ELSE '' END,
							CASE WHEN '[QTP]' = '1' THEN tiers_payant_code || ' ' || tiers_payant_texte ELSE '' END,
							CASE WHEN '[QTP]' = '1' THEN type_tiers_payant_code || ' ' || type_tiers_payant_texte ELSE '' END,
							MIN(date_vente),
							
							SUM(CASE WHEN date_comptable < '[PERIODE]' AND '[QSD]' = '1' THEN montant_comptabilise - montant_regle ELSE 0 END),
							SUM(CASE WHEN date_comptable BETWEEN '[PERIODE]' AND '[TOPERIODE]' AND '[QMC]' = '1' THEN montant_comptabilise ELSE 0 END),
							SUM(CASE WHEN date_comptable BETWEEN '[PERIODE]' AND '[TOPERIODE]' AND '[QMR]' = '1' THEN montant_regle ELSE 0 END),
							SUM(CASE WHEN date_comptable <= '[TOPERIODE]' AND '[QSF]' = '1' THEN montant_comptabilise - montant_regle ELSE 0 END)
							
						FROM #V_FACTURE_SOLDE_TIERS_C_X#
							JOIN #V_SEJOURS_X# ON v_sejours_1.no_sejour = v_facture_solde_tiers_c_1.no_sejour
							JOIN activite.p_patients ON v_sejours_1.no_patient = p_patients.no_patient
						WHERE date_comptable <= '[TOPERIODE]'
							AND date_vente BETWEEN '[PERIODEV]' AND '[TOPERIODEV]'
							AND date_fin_facture BETWEEN '[PERIODES]' AND '[TOPERIODES]'
							
						[VIEW.SEJOURS_SELECT]
						[VIEW.CLIENTS_SELECT]
						[VIEW.PASSAGES_SELECT]
						
						GROUP BY 1,2,3,4,5,6,7,8,9
						
						HAVING 
						SUM(CASE WHEN date_comptable < '[PERIODE]' AND '[QSD]' = '1' THEN montant_comptabilise - montant_regle ELSE 0 END) <> 0
						OR	SUM(CASE WHEN date_comptable BETWEEN '[PERIODE]' AND '[TOPERIODE]' AND '[QMC]' = '1' THEN montant_comptabilise ELSE 0 END) <> 0
						OR	SUM(CASE WHEN date_comptable BETWEEN '[PERIODE]' AND '[TOPERIODE]' AND '[QMR]' = '1' THEN montant_regle ELSE 0 END) <> 0
						OR	SUM(CASE WHEN date_comptable <= '[TOPERIODE]' AND '[QSF]' = '1' THEN montant_comptabilise - montant_regle ELSE 0 END) <> 0

					UNION ALL
						SELECT 
							'-9999999', 
							null,
							COUNT(DISTINCT v_sejours_1.no_sejour)::text || ' séjours, ' || COUNT(DISTINCT v_sejours_1.no_patient)::text || ' patients',
							null,
							null,
							null,
							COUNT(DISTINCT v_facture_solde_tiers_c_1.no_facture)::text || ' factures',
							null,
							null,
							null,
							SUM(CASE WHEN date_comptable < '[PERIODE]' AND '[QSD]' = '1' THEN montant_comptabilise - montant_regle ELSE 0 END),
							SUM(CASE WHEN date_comptable BETWEEN '[PERIODE]' AND '[TOPERIODE]' AND '[QMC]' = '1' THEN montant_comptabilise ELSE 0 END),
							SUM(CASE WHEN date_comptable BETWEEN '[PERIODE]' AND '[TOPERIODE]' AND '[QMR]' = '1' THEN montant_regle ELSE 0 END),
							SUM(CASE WHEN date_comptable <= '[TOPERIODE]' AND '[QSF]' = '1' THEN montant_comptabilise - montant_regle ELSE 0 END)
							
						FROM #V_FACTURE_SOLDE_TIERS_C_X#
							JOIN #V_SEJOURS_X# ON v_sejours_1.no_sejour = v_facture_solde_tiers_c_1.no_sejour
							JOIN activite.p_patients ON v_sejours_1.no_patient = p_patients.no_patient
						WHERE date_comptable <= '[TOPERIODE]'
							AND date_vente BETWEEN '[PERIODEV]' AND '[TOPERIODEV]'
							AND date_fin_facture BETWEEN '[PERIODES]' AND '[TOPERIODES]'
							
						[VIEW.SEJOURS_SELECT]
						[VIEW.CLIENTS_SELECT]
						[VIEW.PASSAGES_SELECT]
						
						
						HAVING 
						SUM(CASE WHEN date_comptable < '[PERIODE]' AND '[QSD]' = '1' THEN montant_comptabilise - montant_regle ELSE 0 END) <> 0
						OR	SUM(CASE WHEN date_comptable BETWEEN '[PERIODE]' AND '[TOPERIODE]' AND '[QMC]' = '1' THEN montant_comptabilise ELSE 0 END) <> 0
						OR	SUM(CASE WHEN date_comptable BETWEEN '[PERIODE]' AND '[TOPERIODE]' AND '[QMR]' = '1' THEN montant_regle ELSE 0 END) <> 0
						OR	SUM(CASE WHEN date_comptable <= '[TOPERIODE]' AND '[QSF]' = '1' THEN montant_comptabilise - montant_regle ELSE 0 END) <> 0
					ORDER BY 2,7,6,8
				]]></select>
				<FIELDS>
					<FIELD name="OID"/>
					<FIELD name="SEJ"/>
					<FIELD name="NOM" ifHideNames="Non autorisé"/>
					<FIELD name="NPAT"/>
					<FIELD name="DENT"/>
					<FIELD name="DSOR"/>
					<FIELD name="FAC"/>
					<FIELD name="TP"/>
					<FIELD name="TTP"/>
					<FIELD name="DVEN"/>
					<FIELD name="SD"/>
					<FIELD name="MC"/>
					<FIELD name="MR"/>
					<FIELD name="SF"/>
				</FIELDS>
			</SQL>
		</QUERY>
		<QUERY type="propertiesLink" name="VIEWPROPERTIES" forRows="false">
		</QUERY>
		<QUERY type="comboLink" name="MOIS_D" forRows="false">
			<SQL select="SELECT date, texte, mois, level FROM activite.v_calendrier_1 WHERE level IN ('1', '2')">
				<FIELDS>
					<FIELD name="date"/>
					<FIELD name="txt"/>
					<FIELD name="mois"/>
					<FIELD name="lvl"/>
				</FIELDS>
			</SQL>
		</QUERY>
		<QUERY type="comboLink" name="MOIS_F" forRows="false">
			<SQL select="SELECT date, texte, mois, level FROM activite.v_calendrier_2 WHERE level IN ('1', '2')">
				<FIELDS>
					<FIELD name="date"/>
					<FIELD name="txt"/>
					<FIELD name="mois"/>
					<FIELD name="lvl"/>
				</FIELDS>
			</SQL>
		</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="Séjours comptes clients clinique" label="Séjours comptes clients 'Clinique'">
			<DATAGRID title="" total="true" headerHeight="48" totalRowInRows="= ROW.OID == '-9999999'" >
				<COLUMN dataField="SEJ" frozen="left" width="100" type="Char" headerText="No Séjour" textAlign="left" otherFunction="text" totalFunction="text">
				</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="100" visible="true" headerText="Nom" textAlign="left" totalFunction="text" totalComplement="TOTAL">
				</COLUMN>
				<COLUMN dataField="DENT" 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="DSOR" width="60" type="Date" inputFormat="AAAA-MM-JJ" outputFormat="DD/MM/YY" fixed="false" headerText="Date sortie" textAlign="right" totalFunction="text">
				</COLUMN>
				<COLUMN dataField="FAC" width="80" type="Char" visibleCondition="SELECT.QFAC == '1'" headerText="No Facture" textAlign="left" otherFunction="text" totalFunction="text">
				</COLUMN>
				<COLUMN dataField="DVEN" width="60" type="Date" inputFormat="AAAA-MM-JJ" outputFormat="DD/MM/YY" fixed="false" headerText="Date ventes" textAlign="right" totalFunction="text">
				</COLUMN>
				<COLUMN dataField="TTP" type="Char" width="100" visibleCondition="SELECT.QTP == '1'" headerText="Type Tiers" textAlign="left" totalFunction="text" totalComplement="">
				</COLUMN>
				<COLUMN dataField="TP" type="Char" minWidth="100" visibleCondition="SELECT.QTP == '1'" headerText="Tiers" textAlign="left" totalFunction="text" totalComplement="TOTAL">
				</COLUMN>
				<COLUMN dataField="SD" width="80" visibleCondition="SELECT.QSD == '1'" type="Number" outputFormat="#E" headerText="Solde début période" textAlign="right" totalFunction="sum" otherFunction="">
				</COLUMN>
				<COLUMN dataField="MC" width="80" visibleCondition="SELECT.QMC == '1'" type="Number" outputFormat="#E" headerText="Montant comptabilisé" textAlign="right" totalFunction="sum" otherFunction="">
				</COLUMN>
				<COLUMN dataField="MR" width="80" visibleCondition="SELECT.QMR == '1'" type="Number" outputFormat="#E" headerText="Montant réglé" textAlign="right" totalFunction="sum" otherFunction="">
				</COLUMN>
				<COLUMN dataField="SF" width="80" visibleCondition="SELECT.QSF == '1'" type="Number" outputFormat="#E" headerText="Solde fin période" textAlign="right" totalFunction="sum" otherFunction="">
				</COLUMN>
			</DATAGRID>
		</ONGLET>
	</PRESENTATION>
</VUE>
