<?xml version="1.0" encoding="ISO-8859-1"?>
<VUE name="ACTI000077" 
	label="Doublons de patients" 
	title="Doublons de patients" 
	database="iCTI" 
	swf="*CTI_view1" 
	softCode="iCTI_activite"
	admProvider="hm"
	>
	<CACHE cachable="false" />
	<VIEWPROPERTIES>
		<PROPERTY name="PATANZ_OPTION">
			<value><![CDATA[[EVAL 
				if ('[PATANZ]' == 'PATETS') {return 'JOIN';}
								return 'LEFT JOIN';
			EVAL]]]></value>
		</PROPERTY>
	</VIEWPROPERTIES>
	
	<SELECTIONS label="Patients" displayText="= var display:String = 'Patients analysés : ';

							display = display + SELECT.PATANZ_LABEL;
						
					return display;
					" >
		<GROUP label="Patients analysés">
			<FIELD name="PATANZ" label="" UI="combo" width="400" default="-1" tree="true">
				<OPTION label="Tous les patients" data="-1"/>
				<OPTION label="Uniquement les patients référencés dans l'établissement" data="PATETS"/>
			</FIELD>
		</GROUP>
	</SELECTIONS>
	
	<QUERIES> 
	
		<QUERY> 
			<SQL select="
			
			DROP TABLE IF EXISTS w_ide_fusion_patient;
			CREATE TABLE w_ide_fusion_patient AS
			SELECT fupa_id_pati_fus, MIN(fupa_id_pati) AS fupa_id_pati  
			FROM prod_hm.ide_fusion_patient, prod_hm.ide_sejour
			WHERE fupa_id_ele = sejo_id_sejo 
			GROUP BY 1 ;
				
			DROP TABLE IF EXISTS w_ide_patient;

			CREATE TEMP TABLE w_ide_patient as
			(SELECT pati_id_pati, fupa_id_pati,	pati_nip, pati_nom_usu, pati_nom_nai, upper(pati_nom_usu) AS pati_nom , upper(pati_prenom) AS pati_prenom, 
				base.cti_soundex_nom(pati_nom_usu) AS pati_nom_soundex, base.cti_soundex_prenom(pati_prenom) AS pati_prenom_soundex, pati_sexe, date(pati_dat_nai) AS pati_dat_nai,
				MIN(date(LEAST(pati_dat_cre,CASE WHEN sejo_dat_sup IS NULL AND sejo_dat_deb &lt;= concat(date_part('year',now())::text, '1231') THEN sejo_dat_deb ELSE null END))) AS pati_dat_cre,
				MAX(CASE WHEN pati_id_etab = sejo_id_etab_con THEN 1 ELSE 0 END) AS code_creation,
				MAX(CASE WHEN sejo_dat_sup IS NULL AND sejo_dat_deb &lt;= concat(date_part('year',now())::text, '1231') THEN date(sejo_dat_deb) ELSE null END) AS sejo_last_dat_deb, 
				0::numeric (1 , 0) AS est_doublon, 
				0::bigint AS pati_first_id_pati,
				COUNT(DISTINCT CASE WHEN sejo_dat_sup IS NULL AND sejo_dat_deb &lt;= concat(date_part('year',now())::text, '1231') THEN sejo_id_sejo ELSE null END) AS nb_sejours
				FROM prod_hm.ide_patient 
					[VIEW.PATANZ_OPTION] prod_hm.ide_sejour ON (sejo_id_pati = pati_id_pati)
					LEFT JOIN w_ide_fusion_patient ON (fupa_id_pati_fus = pati_id_pati)
				WHERE pati_nip IS NOT NULL AND pati_nip &lt;&gt; ''  
				GROUP BY 1,2,3,4,5,6,7,8,9,10,11
				)
			UNION ALL
			(SELECT pati_id_pati, fupa_id_pati, pati_nip, pati_nom_usu, pati_nom_nai, upper(pati_nom_nai) AS pati_nom, upper(pati_prenom) AS pati_prenom, 
				base.cti_soundex_nom(pati_nom_nai) AS pati_nom_soundex, base.cti_soundex_prenom(pati_prenom) AS pati_prenom_soundex, pati_sexe, date(pati_dat_nai) AS pati_dat_nai,
				MIN(date(LEAST(pati_dat_cre,CASE WHEN sejo_dat_sup IS NULL AND sejo_dat_deb &lt;= concat(date_part('year',now())::text, '1231') THEN sejo_dat_deb ELSE null END))) AS pati_dat_cre,
				MAX(CASE WHEN pati_id_etab = sejo_id_etab_con THEN 1 ELSE 0 END) AS code_creation,
				MAX(CASE WHEN sejo_dat_sup IS NULL AND sejo_dat_deb &lt;= concat(date_part('year',now())::text, '1231') THEN date(sejo_dat_deb) ELSE null END) AS sejo_last_dat_deb, 
				0::numeric (1 , 0) AS est_doublon, 
				0::bigint AS pati_first_id_pati,
				COUNT(DISTINCT CASE WHEN sejo_dat_sup IS NULL AND sejo_dat_deb &lt;= concat(date_part('year',now())::text, '1231') THEN sejo_id_sejo ELSE null END) AS nb_sejours
				FROM prod_hm.ide_patient 
					[VIEW.PATANZ_OPTION] prod_hm.ide_sejour ON (sejo_id_pati = pati_id_pati)
					LEFT JOIN w_ide_fusion_patient ON (fupa_id_pati_fus = pati_id_pati)
				WHERE upper(pati_nom_nai) &lt;&gt; upper(pati_nom_usu) AND 
					pati_nip IS NOT NULL AND pati_nip &lt;&gt; '' 
				GROUP BY 1,2,3,4,5,6,7,8,9,10,11
				);

			DROP TABLE IF EXISTS w_ide_patient_doublon;

			CREATE TEMP TABLE w_ide_patient_doublon AS
			SELECT pati_nom_soundex, pati_prenom_soundex, pati_sexe, pati_dat_nai, MIN(pati_id_pati) AS pati_first_id_pati
			FROM w_ide_patient
			GROUP BY 1,2,3,4
			HAVING count(DISTINCT pati_id_pati) > 1 
			ORDER BY 1,2,3,4;


			CREATE INDEX w_ide_patient_i1
			  ON w_ide_patient
			  USING btree
			  (pati_id_pati);

			CREATE INDEX w_ide_patient_doublon_i1
			  ON w_ide_patient_doublon
			  USING btree
			  (pati_first_id_pati);


			UPDATE w_ide_patient
				SET est_doublon = 1 , pati_first_id_pati = w_ide_patient_doublon.pati_first_id_pati
			FROM w_ide_patient_doublon 
			WHERE
				w_ide_patient.pati_nom_soundex = w_ide_patient_doublon.pati_nom_soundex AND
				w_ide_patient.pati_prenom_soundex = w_ide_patient_doublon.pati_prenom_soundex AND
				w_ide_patient.pati_sexe = w_ide_patient_doublon.pati_sexe AND
				w_ide_patient.pati_dat_nai = w_ide_patient_doublon.pati_dat_nai AND
				w_ide_patient_doublon.pati_first_id_pati &lt;&gt; w_ide_patient.pati_id_pati AND
				w_ide_patient.fupa_id_pati IS NULL;

			SELECT 
				date_trunc('month', w_ide_patient.pati_dat_cre),
				COUNT(DISTINCT CASE WHEN code_creation = 1 THEN w_ide_patient.pati_id_pati ELSE null END),
				COUNT(DISTINCT CASE WHEN code_creation = 1 AND est_doublon = 1 THEN w_ide_patient.pati_id_pati ELSE null END)
			FROM w_ide_patient
			WHERE w_ide_patient.pati_dat_cre >= '20150101'
			GROUP BY 1	
			ORDER BY 1 DESC
			;
			
			" >
				<FIELDS>
					<FIELD name="MOI"  />
					<FIELD name="NBP"  />
					<FIELD name="NBD" />
				</FIELDS>
			</SQL>

			
		</QUERY>
		
		<QUERY> 
			<SQL select="
				SELECT DISTINCT
					w_ide_patient_first.pati_id_pati, 
					w_ide_patient_first.pati_nip, 
					w_ide_patient_first.pati_nom_usu || ' ' || w_ide_patient_first.pati_prenom || CASE WHEN w_ide_patient_first.pati_nom_nai &lt;&gt; w_ide_patient_first.pati_nom_usu THEN ' née ' || w_ide_patient_first.pati_nom_nai ELSE '' END, 
					w_ide_patient_first.pati_sexe, 
					w_ide_patient_first.pati_dat_nai, 
					w_ide_patient.pati_id_pati, 
					w_ide_patient.pati_nip, 
					w_ide_patient.pati_nom_usu || ' ' || w_ide_patient.pati_prenom || CASE WHEN w_ide_patient.pati_nom_nai &lt;&gt; w_ide_patient.pati_nom_usu THEN ' née ' || w_ide_patient.pati_nom_nai ELSE '' END, 
					w_ide_patient.sejo_last_dat_deb,
					w_ide_patient.nb_sejours,
					CASE WHEN w_ide_patient.code_creation = 1 THEN 'oui' ELSE '' END,
					w_ide_patient.pati_dat_cre
				FROM w_ide_patient
					LEFT JOIN w_ide_patient w_ide_patient_first  ON (w_ide_patient.pati_first_id_pati = w_ide_patient_first.pati_id_pati)
				WHERE w_ide_patient.est_doublon = 1 AND w_ide_patient.sejo_last_dat_deb IS NOT NULL
				ORDER BY 3,4,5
			;
			" >
				<FIELDS>
					<FIELD name="PID"  />
					<FIELD name="PNIP"  />
					<FIELD name="PNOM" />
					<FIELD name="PSEX" />
					<FIELD name="PDNAI" />
					<FIELD name="DID" />
					<FIELD name="DNIP" />
					<FIELD name="DNOM" />
					<FIELD name="DDUSE" />
					<FIELD name="NSEJ" />
					<FIELD name="DCCRE" />
					<FIELD name="DDCRE" />
				</FIELDS>
			</SQL>

			
		</QUERY>
		
		<QUERY> 
			<SQL select="
				SELECT DISTINCT 
					w_ide_patient_first.pati_id_pati, 
					w_ide_patient_first.pati_nip, 
					w_ide_patient_first.pati_nom_usu || ' ' || w_ide_patient_first.pati_prenom || CASE WHEN w_ide_patient_first.pati_nom_nai &lt;&gt; w_ide_patient_first.pati_nom_usu THEN ' née ' || w_ide_patient_first.pati_nom_nai ELSE '' END, 
					w_ide_patient_first.pati_sexe, 
					w_ide_patient_first.pati_dat_nai, 
					w_ide_patient.pati_id_pati, 
					w_ide_patient.pati_nip, 
					w_ide_patient.pati_nom_usu || ' ' || w_ide_patient.pati_prenom || CASE WHEN w_ide_patient.pati_nom_nai &lt;&gt; w_ide_patient.pati_nom_usu THEN ' née ' || w_ide_patient.pati_nom_nai ELSE '' END
				FROM w_ide_patient
					LEFT JOIN w_ide_patient w_ide_patient_first  ON (w_ide_patient.fupa_id_pati = w_ide_patient_first.pati_id_pati)
				WHERE w_ide_patient.fupa_id_pati IS NOT NULL  
				ORDER BY 3,4,5
			;
			" >
				<FIELDS>
					<FIELD name="PID"  />
					<FIELD name="PNIP"  />
					<FIELD name="PNOM" />
					<FIELD name="PSEX" />
					<FIELD name="PDNAI" />
					<FIELD name="DID" />
					<FIELD name="DNIP" />
					<FIELD name="DNOM" />
				</FIELDS>
			</SQL>

			
		</QUERY>
		

		
			
	</QUERIES> 
	
	<CALCFIELDS>
	</CALCFIELDS>
	
	<PRESENTATION>
	
		<VIEWLINKS>
		
			
		</VIEWLINKS>
		
		<ONGLET excelLabel="Récapitulatif" label="Récapitulatif" queryNumber="0">
				
			<DATAGRID title="" total="true" headerHeight="54" >
			
			

				<COLUMN dataField="MOI" 
					type="Date"
					inputFormat="AAAA-MM-JJ"
					outputFormat="MMMM YYYY"
					minWidth="80"
					visible="true"
					headerText="Mois" 
					textAlign="left"
					totalFunction="text"
					totalComplement="TOTAL"
				/>
				<COLUMN dataField="NBP" 
					type="Number"
					width="200"
					outputFormat="#"
					visible="true"
					headerText="Nombre de patients créés" 
					textAlign="left"
					totalFunction="sum"
					totalComplement=""
				/>
				<COLUMN dataField="NBD" 
					type="Number"
					width="200"
					visible="true"
					outputFormat="#"
					headerText="Nombre de doublons créés" 
					textAlign="left"
					totalFunction="sum"
					totalComplement=""
				/>
					
						
			</DATAGRID>
		
		</ONGLET>
		
		
		<ONGLET excelLabel="Liste des doublons" label="Doublons" queryNumber="1">
				
			<DATAGRID title="" total="false" headerHeight="54" >
			
			
				<COLUMN dataField="PNIP" 
					type="Number"
					width="80"
					visible="true"
					outputFormat="#"
					headerText="NIP" 
					textAlign="left"
					totalFunction="text"
					totalComplement=""
				/>
				<COLUMN dataField="PNOM" 
					type="Char"
					minWidth="80"
					visible="true"
					headerText="Nom" 
					textAlign="left"
					totalFunction="text"
					totalComplement=""
				/>
				<COLUMN dataField="PDNAI" 
					type="Date"
					inputFormat="AAAA-MM-JJ"
					outputFormat="DD/MM/YYYY"
					width="70"
					visible="true"
					headerText="Date naissance" 
					textAlign="left"
					totalFunction="text"
					totalComplement=""
				/>
				<COLUMN dataField="PSEX" 
					type="Char"
					width="40"
					visible="true"
					headerText="Sexe" 
					textAlign="left"
					totalFunction="text"
					totalComplement=""
				/>
				
				
				<COLUMN dataField="DNIP" 
					type="Number"
					width="80"
					outputFormat="#"
					visible="true"
					headerText="DOUBLON : NIP" 
					textAlign="left"
					totalFunction="text"
					totalComplement=""
				/>
				<COLUMN dataField="DNOM" 
					type="Char"
					minWidth="80"
					visible="true"
					headerText="DOUBLON : Nom" 
					textAlign="left"
					totalFunction="text"
					totalComplement=""
				/>
				<COLUMN dataField="DDUSE" 
					type="Date"
					inputFormat="AAAA-MM-JJ"
					outputFormat="DD/MM/YYYY"
					width="70"
					visible="true"
					headerText="DOUBLE: Date dernière entrée" 
					textAlign="left"
					totalFunction="text"
					totalComplement=""
				/>	
				<COLUMN dataField="NSEJ" 
					type="Number"
					width="60"
					outputFormat="#"
					visible="true"
					headerText="DOUBLE: Nb séjours" 
					textAlign="left"
					totalFunction="text"
					totalComplement=""
				/>
				<COLUMN dataField="DCCRE" 
					type="Char"
					width="60"
					visible="true"
					headerText="DOUBLE: Créé par Ets" 
					textAlign="left"
					totalFunction="text"
					totalComplement=""
				/>
				<COLUMN dataField="DDCRE" 
					type="Date"
					inputFormat="AAAA-MM-JJ"
					outputFormat="DD/MM/YYYY"
					width="70"
					visible="true"
					headerText="DOUBLE: Date création" 
					textAlign="left"
					totalFunction="text"
					totalComplement=""
				/>					
					
						
			</DATAGRID>
		
		</ONGLET>
		
		<ONGLET excelLabel="Fusions déjà réalisées" label="Fusions déjà réalisées" queryNumber="2">
				
			<DATAGRID title="" total="false" headerHeight="54" >
			
			
				<COLUMN dataField="PNIP" 
					type="Number"
					width="80"
					visible="true"
					outputFormat="#"
					headerText="NIP" 
					textAlign="left"
					totalFunction="text"
					totalComplement=""
				/>
				<COLUMN dataField="PNOM" 
					type="Char"
					minWidth="80"
					visible="true"
					headerText="Nom" 
					textAlign="left"
					totalFunction="text"
					totalComplement=""
				/>
				<COLUMN dataField="PDNAI" 
					type="Date"
					inputFormat="AAAA-MM-JJ"
					outputFormat="DD/MM/YYYY"
					width="70"
					visible="true"
					headerText="Date naissance" 
					textAlign="left"
					totalFunction="text"
					totalComplement=""
				/>
				<COLUMN dataField="PSEX" 
					type="Char"
					width="40"
					visible="true"
					headerText="Sexe" 
					textAlign="left"
					totalFunction="text"
					totalComplement=""
				/>
				
				
				<COLUMN dataField="DNIP" 
					type="Number"
					width="80"
					outputFormat="#"
					visible="true"
					headerText="FUSIONNE : NIP" 
					textAlign="left"
					totalFunction="text"
					totalComplement=""
				/>
				<COLUMN dataField="DNOM" 
					type="Char"
					minWidth="80"
					visible="true"
					headerText="FUSIONNE : Nom" 
					textAlign="left"
					totalFunction="text"
					totalComplement=""
				/>
								
					
						
			</DATAGRID>
		
		</ONGLET>
		
		
		
		
				
				
	</PRESENTATION>
	
</VUE>

			
