<?xml version="1.0" encoding="ISO-8859-1"?>
<VUE name="ACTI000092" 
	label="Chiffrier comptable CTI" 
	database="iCTI" 
	swf="*CTI_view1" 
	softCode="iCTI_activite"
	globals="ACTI_globals.XML"
	admProvider="">
	<CACHE cachable="false" />
	<title><![CDATA[= 
			var title:String = 'Chiffrier comptable CTI';
			
			if (SELECT.PERIODE != '-1') {
				if (SELECT.TOPERIODE != '-1') {
					title = title + ' de ' + SELECT.PERIODE_LABEL + ' à ' + SELECT.TOPERIODE_LABEL;
				}
				else {
					title = title + ' à partir de ' + SELECT.PERIODE_LABEL;
				}
			}
			else {
				if (SELECT.TOPERIODE != '-1') {
					title = title + ' jusque ' + SELECT.TOPERIODE_LABEL;
				}
			}
				
			if (SELECT.PROVIDER != '-1') {
				title = title + ' (' + SELECT.PROVIDER_LABEL + ')'
			}
			return title;
		]]></title>
	<VIEWPROPERTIES>
		
		<PROPERTY name="PERIODE" value="[EVAL 
												if ('[PERIODE]' != '-1') {return '[PERIODE]';}
												return '20070101';
											EVAL]"/>
		<PROPERTY name="TOPERIODE" value="[EVAL 
												if ('[TOPERIODE]' != '-1') {return '[TOPERIODE]';}
												return '20201231';
											EVAL]"/>
											
		<PROPERTY name="PROVIDER_SELECT" value="[EVAL 
												if ('[PROVIDER]' != '-1') {return ' AND p_factures.provider_id = ' . substr('[PROVIDER]',2);}
												return '';
											EVAL]"/>
											
		<PROPERTY name="PROVIDERC_SELECT" value="[EVAL 
												if ('[PROVIDER]' != '-1') {return ' AND provider_id = ' . substr('[PROVIDER]',2);}
												return '';
											EVAL]"/>

		
	</VIEWPROPERTIES>
	
	<SELECTIONS label="Période">
	
		<GROUP label="Periode comptable">
			<FIELD name="PERIODE" label="" UI="combo" tree="true" width="200" default="2010-01-01">
				<OPTION label="Pas de sélection" data="-1"/>
				<OPTION dataLink="MOIS" dataField="date" data2Field="mois" treeLevelField="lvl" labelField="txt"/>
			</FIELD>
			<FIELD name="TOPERIODE" label="à" UI="combo" tree="true" width="200" default="-1">
				<OPTION label="Pas de sélection" data="-1"/>
				<OPTION dataLink="TOMOIS" dataField="date" data2Field="mois" treeLevelField="lvl" labelField="txt"/>
			</FIELD>
		</GROUP>
		
		<GROUP label="Logiciel administratif">
			<FIELD name="PROVIDER" label="" UI="combo" width="200" default="-1" globalListProvider="PROVIDERS">
			</FIELD>
		</GROUP>
		
	</SELECTIONS>
	
	<QUERIES> 
		
		<QUERY> 
			<SQL>
				<select><![CDATA[
							DROP TABLE IF EXISTS w_chiffrier_comptable;
							CREATE TEMP TABLE w_chiffrier_comptable
							(
								date_comptable date ,
								provider_id bigint,
								montant_comptabilise_d_c numeric(13,2) DEFAULT 0,
								montant_comptabilise_t_c numeric(13,2) DEFAULT 0,
								montant_comptabilise_s_c numeric(13,2) DEFAULT 0,
								montant_comptabilise_c_c numeric(13,2) DEFAULT 0,
								montant_regle_s_c numeric(13,2) DEFAULT 0,
								montant_regle_c_c numeric(13,2) DEFAULT 0,

								montant_comptabilise_d_h numeric(13,2) DEFAULT 0,
								montant_comptabilise_t_h numeric(13,2) DEFAULT 0,
								montant_comptabilise_s_h numeric(13,2) DEFAULT 0,
								montant_comptabilise_c_h numeric(13,2) DEFAULT 0,
								montant_regle_s_h numeric(13,2) DEFAULT 0,
								montant_regle_c_h numeric(13,2) DEFAULT 0,
								valide text DEFAULT '0'
							) ;
					
							INSERT INTO w_chiffrier_comptable(date_comptable, provider_id, montant_comptabilise_d_c)
							SELECT date_trunc('month',p_factures.date_vente) AS date_comptable, p_factures.provider_id, 
								SUM(p_factures_lignes_c.montant_comptabilise) AS montant_comptabilise_d_c
							FROM activite.p_factures_lignes_c
								JOIN activite.p_factures ON p_factures_lignes_c.no_facture = p_factures.no_facture
								JOIN activite.p_sejours ON p_factures.no_sejour = p_sejours.no_sejour AND p_sejours.etat = ''
							WHERE p_factures.date_vente BETWEEN '[VIEW.PERIODE]' AND '[VIEW.TOPERIODE]' AND
								p_factures.code_vente = '1'
								[VIEW.PROVIDER_SELECT]
							GROUP BY 1,2;
					
							INSERT INTO w_chiffrier_comptable(date_comptable, provider_id, montant_comptabilise_d_h)
							SELECT date_trunc('month',p_factures.date_vente) AS date_comptable, p_factures.provider_id, 
								SUM(p_factures_lignes_h.montant_comptabilise) AS montant_comptabilise_d_h
							FROM activite.p_factures_lignes_h
								JOIN activite.p_factures ON p_factures_lignes_h.no_facture = p_factures.no_facture
								JOIN activite.p_sejours ON p_factures.no_sejour = p_sejours.no_sejour AND p_sejours.etat = ''
							WHERE date_vente BETWEEN '[VIEW.PERIODE]' AND '[VIEW.TOPERIODE]' AND
								p_factures.code_vente = '1'
								[VIEW.PROVIDER_SELECT]
							GROUP BY 1,2;
					
							INSERT INTO w_chiffrier_comptable(date_comptable, provider_id, montant_comptabilise_t_c, montant_comptabilise_t_h)
							SELECT date_trunc('month',date_vente) AS date_comptable, p_factures.provider_id, 
								SUM(p_factures.montant_comptabilise_c) AS montant_comptabilise_t_c, 
								SUM(p_factures.montant_comptabilise_h) AS montant_comptabilise_t_h
							FROM activite.p_factures
								JOIN activite.p_sejours ON p_factures.no_sejour = p_sejours.no_sejour AND p_sejours.etat = ''
							WHERE date_vente BETWEEN '[VIEW.PERIODE]' AND '[VIEW.TOPERIODE]' AND
								code_vente = '1'
								[VIEW.PROVIDER_SELECT]
							GROUP BY 1,2;
					
							INSERT INTO w_chiffrier_comptable(date_comptable, provider_id, montant_comptabilise_s_c, montant_regle_s_c)
							SELECT date_trunc('month',p_facture_solde_tiers_c.date_comptable) AS date_comptable, p_factures.provider_id, 
								SUM(CASE WHEN p_factures.code_vente = '1' THEN montant_comptabilise ELSE 0 END) AS montant_comptabilise_s_c, 
								SUM(CASE WHEN od_avoir <> '1' AND od_non_comptabilise IS DISTINCT FROM '1' THEN montant_regle ELSE 0 END) AS montant_regle_s_c
							FROM activite.p_facture_solde_tiers_c
								JOIN activite.p_factures ON p_facture_solde_tiers_c.no_facture = p_factures.no_facture
								JOIN activite.p_sejours ON p_factures.no_sejour = p_sejours.no_sejour AND p_sejours.etat = ''
							WHERE date_comptable BETWEEN '[VIEW.PERIODE]' AND '[VIEW.TOPERIODE]'
								[VIEW.PROVIDER_SELECT]
							GROUP BY 1,2 ;
					
							INSERT INTO w_chiffrier_comptable(date_comptable, provider_id, montant_comptabilise_s_h, montant_regle_s_h)
							SELECT date_trunc('month',date_comptable) AS date_comptable, p_factures.provider_id, 
								SUM(CASE WHEN p_factures.code_vente = '1' THEN montant_comptabilise ELSE 0 END) AS montant_comptabilise_s_h, 
								SUM(CASE WHEN od_avoir <> '1' AND od_non_comptabilise IS DISTINCT FROM '1' THEN montant_regle ELSE 0 END) AS montant_regle_s_h
							FROM activite.p_factures_soldes_h
								JOIN activite.p_factures ON p_factures_soldes_h.no_facture = p_factures.no_facture
								JOIN activite.p_sejours ON p_factures.no_sejour = p_sejours.no_sejour AND p_sejours.etat = ''
							WHERE date_comptable BETWEEN '[VIEW.PERIODE]' AND '[VIEW.TOPERIODE]'
								[VIEW.PROVIDER_SELECT]
							GROUP BY 1,2 ;
					
							INSERT INTO w_chiffrier_comptable(date_comptable, provider_id, montant_comptabilise_c_c, montant_comptabilise_c_h, montant_regle_c_c, montant_regle_c_h)
							SELECT date_trunc('month',date_comptable) AS date_comptable, provider_id, 
								SUM(montant_ventes_c) AS montant_comptabilise_c_c, 
								SUM(montant_ventes_h) AS montant_comptabilise_c_h, 
								SUM(montant_reglements_c) AS montant_regle_c_c, 
								SUM(montant_reglements_h) AS montant_regle_c_h
							FROM activite.p_chiffrier_comptable
							WHERE date_comptable BETWEEN '[VIEW.PERIODE]' AND '[VIEW.TOPERIODE]'
								[VIEW.PROVIDERC_SELECT]
							GROUP BY 1,2 ;
							
							UPDATE w_chiffrier_comptable
							SET valide = t_controle_chiffrier.valide
							FROM activite.t_controle_chiffrier
							WHERE w_chiffrier_comptable.date_comptable = t_controle_chiffrier.date_comptable AND
								w_chiffrier_comptable.provider_id = t_controle_chiffrier.provider_id;
							
							
							
							DROP TABLE IF EXISTS w_chiffrier_comptable_erreurs;
							CREATE TEMP TABLE w_chiffrier_comptable_erreurs AS
							SELECT 
								date_trunc('month',date_comptable) AS w_date_comptable,
								provider_id AS w_provider_id,
								SUM(montant_comptabilise_c_c) AS mois_montant_comptabilise_c_c,
								SUM(montant_comptabilise_c_h) AS mois_montant_comptabilise_c_h,
								SUM(montant_regle_c_c) AS mois_montant_regle_c_c,
								SUM(montant_regle_c_h) AS mois_montant_regle_c_h
							FROM w_chiffrier_comptable
							WHERE date_comptable >= 
								(SELECT min(date_comptable) 
									FROM w_chiffrier_comptable 
									WHERE montant_comptabilise_d_c <> 0 OR
										montant_comptabilise_t_c <> 0 OR
										montant_comptabilise_s_c <> 0)  
							GROUP BY 1,2
							HAVING
								SUM(montant_comptabilise_d_c) <> SUM(montant_comptabilise_t_c) OR
								SUM(montant_comptabilise_d_c) <> SUM(montant_comptabilise_s_c) OR
								SUM(montant_comptabilise_d_c) <> SUM(montant_comptabilise_c_c) AND SUM(montant_comptabilise_c_c) <> 0 OR
								SUM(montant_comptabilise_d_h) <> SUM(montant_comptabilise_t_h) OR
								SUM(montant_comptabilise_d_h) <> SUM(montant_comptabilise_s_h) OR
								SUM(montant_comptabilise_d_h) <> SUM(montant_comptabilise_c_h) AND SUM(montant_comptabilise_c_h) <> 0 OR
								SUM(montant_regle_s_c) <> SUM(montant_regle_c_c) AND SUM(montant_regle_c_c) <> 0 OR
								SUM(montant_regle_s_h) <> SUM(montant_regle_c_h) AND SUM(montant_regle_c_h) <> 0
							ORDER BY 1 DESC;
							
							CREATE INDEX w_chiffrier_comptable_erreurs_i1
							ON w_chiffrier_comptable_erreurs
							USING btree
							(w_date_comptable);
					
					
					
							SELECT 
								provider_id,
								COALESCE(t_providers.code,'') AS provider_code,
								date_comptable ,
								date(date_trunc('month',date_comptable) + interval '1 month' - interval '1 day') AS date_comptable_fin,
								CASE WHEN MIN(VALIDE) <> '1' THEN '' ELSE 'V' END,
								SUM(montant_comptabilise_d_c),
								SUM(montant_comptabilise_t_c),
								SUM(montant_comptabilise_s_c),
								SUM(montant_comptabilise_c_c),
								SUM(montant_regle_s_c),
								SUM(montant_regle_c_c),
								SUM(montant_comptabilise_d_h),
								SUM(montant_comptabilise_t_h),
								SUM(montant_comptabilise_s_h),
								SUM(montant_comptabilise_c_h),
								SUM(montant_regle_s_h),
								SUM(montant_regle_c_h)
							FROM w_chiffrier_comptable
								LEFT JOIN activite.t_providers ON provider_id = t_providers.oid
							WHERE date_comptable >= 
								(SELECT min(date_comptable) 
									FROM w_chiffrier_comptable 
									WHERE montant_comptabilise_d_c <> 0 OR
										montant_comptabilise_t_c <> 0 OR
										montant_comptabilise_s_c <> 0)
							GROUP BY 1,2,3
							ORDER BY 1,3 DESC;
					]]></select>
				<FIELDS>
					<FIELD name="PRID"  />
					<FIELD name="PROV"  />
					<FIELD name="DATE"  />
					<FIELD name="DATF"  />
					<FIELD name="VAL" />
					<FIELD name="MF_DC" />
					<FIELD name="MF_TC" />
					<FIELD name="MF_SC" />
					<FIELD name="MF_CC" />
					<FIELD name="MR_SC" />
					<FIELD name="MR_CC" />
					<FIELD name="MF_DH" />
					<FIELD name="MF_TH" />
					<FIELD name="MF_SH" />
					<FIELD name="MF_CH" />
					<FIELD name="MR_SH" />
					<FIELD name="MR_CH" />
				</FIELDS>
			</SQL>
			
			
		</QUERY>
		
		<QUERY> 
			<SQL>
				<select><![CDATA[
							DROP TABLE IF EXISTS w_chiffrier_comptable;
							CREATE TEMP TABLE w_chiffrier_comptable
							(
								no_facture text,
								provider_id bigint,
								date_comptable date ,
								montant_comptabilise_d_c numeric(13,2) DEFAULT 0,
								montant_comptabilise_t_c numeric(13,2) DEFAULT 0,
								montant_comptabilise_s_c numeric(13,2) DEFAULT 0,
								montant_comptabilise_c_c numeric(13,2) DEFAULT 0,
								montant_regle_s_c numeric(13,2) DEFAULT 0,
								montant_regle_c_c numeric(13,2) DEFAULT 0,

								montant_comptabilise_d_h numeric(13,2) DEFAULT 0,
								montant_comptabilise_t_h numeric(13,2) DEFAULT 0,
								montant_comptabilise_s_h numeric(13,2) DEFAULT 0,
								montant_comptabilise_c_h numeric(13,2) DEFAULT 0,
								montant_regle_s_h numeric(13,2) DEFAULT 0,
								montant_regle_c_h numeric(13,2) DEFAULT 0
							) ;
					
							INSERT INTO w_chiffrier_comptable(no_facture, provider_id, date_comptable, montant_comptabilise_d_c)
							SELECT p_factures_lignes_c.no_facture, p_factures.provider_id, date_trunc('month',date_vente) AS date_comptable, 
								SUM(montant_comptabilise) AS montant_comptabilise_d_c
							FROM activite.p_factures_lignes_c
								JOIN activite.p_factures ON p_factures_lignes_c.no_facture = p_factures.no_facture
								JOIN activite.p_sejours ON p_factures.no_sejour = p_sejours.no_sejour AND p_sejours.etat = ''
								JOIN w_chiffrier_comptable_erreurs ON date_trunc('month',date_vente) = w_chiffrier_comptable_erreurs.w_date_comptable AND
									p_factures.provider_id = w_chiffrier_comptable_erreurs.w_provider_id
							WHERE date_vente BETWEEN '[VIEW.PERIODE]' AND '[VIEW.TOPERIODE]' AND 
								montant_comptabilise <> 0
								[VIEW.PROVIDER_SELECT]
							GROUP BY 1,2,3;
					
							INSERT INTO w_chiffrier_comptable(no_facture, provider_id, date_comptable, montant_comptabilise_d_h)
							SELECT p_factures_lignes_h.no_facture, p_factures.provider_id, date_trunc('month',date_vente) AS date_comptable, 
								SUM(montant_comptabilise) AS montant_comptabilise_d_h
							FROM activite.p_factures_lignes_h
								JOIN activite.p_factures ON p_factures_lignes_h.no_facture = p_factures.no_facture
								JOIN activite.p_sejours ON p_factures.no_sejour = p_sejours.no_sejour AND p_sejours.etat = ''
								JOIN w_chiffrier_comptable_erreurs ON date_trunc('month',date_vente) = w_chiffrier_comptable_erreurs.w_date_comptable AND
									p_factures.provider_id = w_chiffrier_comptable_erreurs.w_provider_id
							WHERE date_vente BETWEEN '[VIEW.PERIODE]' AND '[VIEW.TOPERIODE]' AND montant_comptabilise <> 0
								[VIEW.PROVIDER_SELECT]
							GROUP BY 1,2,3;
					
							INSERT INTO w_chiffrier_comptable(no_facture, provider_id, date_comptable, montant_comptabilise_t_c, montant_comptabilise_t_h)
							SELECT no_facture, p_factures.provider_id, date_trunc('month',date_vente) AS date_comptable, 
								SUM(p_factures.montant_comptabilise_c) AS montant_comptabilise_t_c, 
								SUM(p_factures.montant_comptabilise_h) AS montant_comptabilise_t_h
							FROM activite.p_factures
								JOIN activite.p_sejours ON p_factures.no_sejour = p_sejours.no_sejour AND p_sejours.etat = ''
								JOIN w_chiffrier_comptable_erreurs ON date_trunc('month',date_vente) = w_chiffrier_comptable_erreurs.w_date_comptable AND
									p_factures.provider_id = w_chiffrier_comptable_erreurs.w_provider_id
							WHERE date_vente BETWEEN '[VIEW.PERIODE]' AND '[VIEW.TOPERIODE]' AND (p_factures.montant_comptabilise_c <> 0 OR p_factures.montant_comptabilise_h <> 0)
								[VIEW.PROVIDER_SELECT]
							GROUP BY 1,2,3;
					
							INSERT INTO w_chiffrier_comptable(no_facture, provider_id, date_comptable, montant_comptabilise_s_c, montant_regle_s_c)
							SELECT p_facture_solde_tiers_c.no_facture, p_factures.provider_id, date_trunc('month',p_facture_solde_tiers_c.date_comptable) AS date_comptable, 
								SUM(montant_comptabilise) AS montant_comptabilise_s_c, 
								SUM(CASE WHEN od_avoir <> '1' AND od_non_comptabilise IS DISTINCT FROM '1' THEN montant_regle ELSE 0 END) AS montant_regle_s_c
							FROM activite.p_facture_solde_tiers_c
								JOIN activite.p_factures ON p_facture_solde_tiers_c.no_facture = p_factures.no_facture
								JOIN activite.p_sejours ON p_factures.no_sejour = p_sejours.no_sejour AND p_sejours.etat = ''
								JOIN w_chiffrier_comptable_erreurs ON date_trunc('month',p_facture_solde_tiers_c.date_comptable) = w_chiffrier_comptable_erreurs.w_date_comptable AND
									p_factures.provider_id = w_chiffrier_comptable_erreurs.w_provider_id
							WHERE p_facture_solde_tiers_c.date_comptable BETWEEN '[VIEW.PERIODE]' AND '[VIEW.TOPERIODE]' AND (montant_comptabilise <> 0 OR montant_regle <> 0)
								[VIEW.PROVIDER_SELECT]
							GROUP BY 1,2,3 ;
					
							INSERT INTO w_chiffrier_comptable(no_facture, provider_id, date_comptable, montant_comptabilise_s_h, montant_regle_s_h)
							SELECT p_factures_soldes_h.no_facture, p_factures.provider_id, date_trunc('month',p_factures_soldes_h.date_comptable) AS date_comptable, 
								SUM(montant_comptabilise) AS montant_comptabilise_s_h, 
								SUM(CASE WHEN od_avoir <> '1' AND od_non_comptabilise IS DISTINCT FROM '1' THEN montant_regle ELSE 0 END) AS montant_regle_s_h
							FROM activite.p_factures_soldes_h
								JOIN activite.p_factures ON p_factures_soldes_h.no_facture = p_factures.no_facture
								JOIN activite.p_sejours ON p_factures.no_sejour = p_sejours.no_sejour AND p_sejours.etat = ''
								JOIN w_chiffrier_comptable_erreurs ON date_trunc('month',p_factures_soldes_h.date_comptable) = w_chiffrier_comptable_erreurs.w_date_comptable AND
									p_factures.provider_id = w_chiffrier_comptable_erreurs.w_provider_id
							WHERE p_factures_soldes_h.date_comptable BETWEEN '[VIEW.PERIODE]' AND '[VIEW.TOPERIODE]' AND (montant_comptabilise <> 0 OR montant_regle <> 0)
								[VIEW.PROVIDER_SELECT]
							GROUP BY 1,2,3 ;
					
							INSERT INTO w_chiffrier_comptable(no_facture, provider_id, date_comptable, montant_comptabilise_c_c, montant_comptabilise_c_h, montant_regle_c_c, montant_regle_c_h)
							SELECT no_facture, provider_id, date_trunc('month',p_factures_comptables.date_comptable) AS date_comptable, 
								SUM(montant_ventes_c) AS montant_comptabilise_c_c, 
								SUM(montant_ventes_h) AS montant_comptabilise_c_h, 
								SUM(montant_reglements_c) AS montant_regle_c_c, 
								SUM(montant_reglements_h) AS montant_regle_c_h
							FROM activite.p_factures_comptables
								JOIN w_chiffrier_comptable_erreurs ON date_trunc('month',p_factures_comptables.date_comptable) = w_chiffrier_comptable_erreurs.w_date_comptable AND
									p_factures_comptables.provider_id = w_chiffrier_comptable_erreurs.w_provider_id
							WHERE p_factures_comptables.date_comptable BETWEEN '[VIEW.PERIODE]' AND '[VIEW.TOPERIODE]'
								[VIEW.PROVIDERC_SELECT]
							GROUP BY 1,2,3 ;
							
							-- Spécial QSP, transformation des avoirs
							UPDATE w_chiffrier_comptable 
							SET no_facture = '0' || substr(no_facture,2,9) 
							WHERE length(no_facture) = 8 AND 
								no_facture like '90%' AND
								'00' || substr(no_facture,3,9) IN (SELECT no_facture FROM activite.p_factures);
							
							-- Special EDLxplore. Enlever les suffixes des factures
							UPDATE w_chiffrier_comptable 
							SET no_facture = split_part(no_facture,'.',1)||'.1'
							FROM activite.t_providers
							WHERE provider_id = t_providers.oid AND
								t_providers.code = 'EDL'
							;							
							
								
							-- N'afficher que les séjours en erreur
							
							DROP TABLE IF EXISTS w_sejours_erreurs;
							CREATE TEMP TABLE w_sejours_erreurs AS							
							SELECT 
								date_comptable,
								w_chiffrier_comptable.provider_id,
								COALESCE(p_factures.no_sejour,'') AS no_sejour,
								p_patients.nom ||  ' ' || p_patients.prenom AS nom_patient,
								base.cti_group_concat('['||w_chiffrier_comptable.no_facture::text||']') AS no_factures
							FROM w_chiffrier_comptable 
								JOIN w_chiffrier_comptable_erreurs ON w_chiffrier_comptable.date_comptable = w_chiffrier_comptable_erreurs.w_date_comptable AND 
										w_chiffrier_comptable.provider_id = w_chiffrier_comptable_erreurs.w_provider_id
								LEFT JOIN activite.p_factures ON w_chiffrier_comptable.no_facture = p_factures.no_facture
								LEFT JOIN activite.p_sejours ON p_factures.no_sejour = p_sejours.no_sejour
								LEFT JOIN activite.p_patients ON p_sejours.no_patient = p_patients.no_patient
							GROUP BY 1,2,3,4
							HAVING 
								SUM(montant_comptabilise_d_c) <> SUM(montant_comptabilise_t_c) OR
								SUM(montant_comptabilise_d_h) <> SUM(montant_comptabilise_t_h) OR
								SUM(montant_comptabilise_d_c) <> SUM(montant_comptabilise_s_c) OR
								SUM(montant_comptabilise_d_h) <> SUM(montant_comptabilise_s_h) OR
								SUM(montant_comptabilise_d_c) <> SUM(montant_comptabilise_c_c) AND MAX(mois_montant_comptabilise_c_c) <> 0 OR
								SUM(montant_comptabilise_d_h) <> SUM(montant_comptabilise_c_h) AND MAX(mois_montant_comptabilise_c_h) <> 0 OR
								SUM(montant_regle_s_c) <> SUM(montant_regle_c_c) AND MAX(mois_montant_regle_c_c) <> 0 OR
								SUM(montant_regle_s_h) <> SUM(montant_regle_c_h) AND MAX(mois_montant_regle_c_h) <> 0 
							;
							
							CREATE INDEX w_sejours_erreurs_i1
							ON w_sejours_erreurs
							USING btree
							(no_sejour);
							
					
							-- Anomalies
							SELECT 
								w_chiffrier_comptable.no_facture ,
								w_chiffrier_comptable.date_comptable ,
								w_sejours_erreurs.no_sejour ,
								w_sejours_erreurs.nom_patient ,
								SUM(montant_comptabilise_d_c),
								SUM(montant_comptabilise_t_c),
								SUM(montant_comptabilise_s_c),
								SUM(montant_comptabilise_c_c),
								SUM(montant_regle_s_c),
								SUM(montant_regle_c_c),
								SUM(montant_comptabilise_d_h),
								SUM(montant_comptabilise_t_h),
								SUM(montant_comptabilise_s_h),
								SUM(montant_comptabilise_c_h),
								SUM(montant_regle_s_h),
								SUM(montant_regle_c_h)
							FROM w_chiffrier_comptable 
								JOIN w_chiffrier_comptable_erreurs ON w_chiffrier_comptable.date_comptable = w_chiffrier_comptable_erreurs.w_date_comptable AND 
										w_chiffrier_comptable.provider_id = w_chiffrier_comptable_erreurs.w_provider_id
								JOIN w_sejours_erreurs ON w_sejours_erreurs.date_comptable = w_chiffrier_comptable.date_comptable AND
											w_sejours_erreurs.no_factures LIKE '%[' || w_chiffrier_comptable.no_facture::text || ']%'
								LEFT JOIN activite.p_factures ON w_chiffrier_comptable.no_facture = p_factures.no_facture
							GROUP BY 1,2,3,4
							HAVING 
								SUM(montant_comptabilise_d_c) <> SUM(montant_comptabilise_t_c) OR
								SUM(montant_comptabilise_d_h) <> SUM(montant_comptabilise_t_h) OR
								SUM(montant_comptabilise_d_c) <> SUM(montant_comptabilise_s_c) OR
								SUM(montant_comptabilise_d_h) <> SUM(montant_comptabilise_s_h) OR
								SUM(montant_comptabilise_d_c) <> SUM(montant_comptabilise_c_c) AND MAX(mois_montant_comptabilise_c_c) <> 0 OR
								SUM(montant_comptabilise_d_h) <> SUM(montant_comptabilise_c_h) AND MAX(mois_montant_comptabilise_c_h) <> 0 OR
								SUM(montant_regle_s_c) <> SUM(montant_regle_c_c) AND MAX(mois_montant_regle_c_c) <> 0 OR
								SUM(montant_regle_s_h) <> SUM(montant_regle_c_h) AND MAX(mois_montant_regle_c_h) <> 0 
							ORDER BY 2 DESC,1
							LIMIT 1000;
							
					]]></select>
				<FIELDS>
					<FIELD name="FAC"  />
					<FIELD name="DATE"  />
					<FIELD name="SEJ"  />
					<FIELD name="NOM" ifHideNames="Non autorisé"  />
					<FIELD name="MF_DC" />
					<FIELD name="MF_TC" />
					<FIELD name="MF_SC" />
					<FIELD name="MF_CC" />
					<FIELD name="MR_SC" />
					<FIELD name="MR_CC" />
					<FIELD name="MF_DH" />
					<FIELD name="MF_TH" />
					<FIELD name="MF_SH" />
					<FIELD name="MF_CH" />
					<FIELD name="MR_SH" />
					<FIELD name="MR_CH" />
				</FIELDS>
			</SQL>
			
			
		</QUERY>
		
		
		
		<QUERY type="comboLink" name="MOIS" forRows="false">
			<SQL>
				<select><![CDATA[
						SELECT date, texte, mois, level 
						FROM activite.v_calendrier_1 
						WHERE date >= '20070101' AND
							date <= (SELECT MAX(date_comptable) FROM activite.p_chiffrier_comptable) AND
							level IN ('1', '2');
				]]></select>
				<FIELDS>
					<FIELD name="date"/>
					<FIELD name="txt"/>
					<FIELD name="mois"/>
					<FIELD name="lvl"/>
				</FIELDS>
			</SQL>
		</QUERY>
		
		<QUERY type="comboLink" name="TOMOIS" forRows="false">
			<SQL>
				<select><![CDATA[
						SELECT date, texte, mois, level
						FROM activite.v_calendrier_2
						WHERE date >= '20070101' AND
							CASE WHEN level = 2 THEN date ELSE date_trunc('year', date) END <= (SELECT date(date_trunc('year',MAX(date_comptable))+interval '1 year' - interval '1 day') FROM activite.t_controle_chiffrier) AND
							level IN ('1', '2');
				]]></select>
				<FIELDS>
					<FIELD name="date"/>
					<FIELD name="txt"/>
					<FIELD name="mois"/>
					<FIELD name="lvl"/>
				</FIELDS>
			</SQL>
		</QUERY>
		
		
			
	</QUERIES> 
	
	<CALCFIELDS>
	</CALCFIELDS>
	
	<PRESENTATION>
	
		<VIEWLINKS>
			
			<VIEWLINK label="Valider Mois" shortLabel="Valider Mois" view="ACTI000093.XML">
				<ARG name="PERIODE" value="ROW.DATE" />
				<ARG name="TOPERIODE" value="ROW.DATF" />
				<ARG name="VAL_OPTION" value="'V'" />
			</VIEWLINK>
			<VIEWLINK label="INValider Mois" shortLabel="INValider Mois" view="ACTI000093.XML">
				<ARG name="PERIODE" value="ROW.DATE" />
				<ARG name="TOPERIODE" value="ROW.DATF" />
				<ARG name="VAL_OPTION" value="'IV'" />
			</VIEWLINK>


		</VIEWLINKS>
			
				
		
		
		<ONGLET excelLabel="Chiffrier Comptable" label="Chiffrier Comptable" queryNumber="0">
				
			<DATAGRID title="" total="false" headerHeight="54" printRatio="1">
				<COLUMN dataField="PROV"
					type="Char"
					outputFormat="#"
					width="80"
					fixed="false"
					headerText="Logiciel administratif"
					align="center">
				</COLUMN>
														
				<COLUMN dataField="DATE" 
					type="Date"
					inputFormat="AAAA-MM-JJ"
					outputFormat="MMMM YYYY"
					minWidth="80"
					visible="true"
					headerText="Mois comptable" 
					textAlign="left"
					totalFunction="text"
					totalComplement="TOTAL"
				/>
				<COLUMN dataField="DATF" 
					type="Date"
					inputFormat="AAAA-MM-JJ"
					outputFormat="MMMM YYYY"
					minWidth="80"
					visible="false"
					headerText="Mois comptable" 
					textAlign="left"
					totalFunction="text"
					totalComplement="TOTAL"
				/>
				
				<COLUMN dataField="VAL"
					type="Char"
					outputFormat="#"
					width="40"
					fixed="false"
					headerText="Validé"
					align="center">
				</COLUMN>
				<COLUMN dataField="MF_DC"
					type="Number"
					outputFormat="#"
					width="80"
					fixed="false"
					headerText="CTI Facturé@LF@Détail@LF@Clinique"
					multiLine="sum"	
					totalFunction="sum"
					align="right">
					<CELLSTYLE name="cellIndicator" value="circle, 5, right top, 0xFF6600" 
							condition="ROW.MF_DC != ROW.MF_CC &amp;&amp; Number(ROW.MF_CC) != 0" />
				</COLUMN>
				<COLUMN dataField="MF_TC"
					type="Number"
					outputFormat="#"
					width="80"
					fixed="false"
					headerText="CTI Facturé@LF@Total@LF@Clinique"
					align="right"
					multiLine="sum"					
					totalFunction="sum">
					<CELLSTYLE name="cellIndicator" value="circle, 5, right top, 0xFF6600" 
							condition="ROW.MF_TC != ROW.MF_CC &amp;&amp; Number(ROW.MF_CC) != 0" />
				</COLUMN>
				<COLUMN dataField="MF_SC"
					type="Number"
					outputFormat="#"
					width="80"
					fixed="false"
					headerText="CTI Facturé@LF@Clients@LF@Clinique"
					align="right"
					multiLine="sum"					
					totalFunction="sum">
					<CELLSTYLE name="cellIndicator" value="circle, 5, right top, 0xFF6600" 
							condition="ROW.MF_SC != ROW.MF_CC &amp;&amp; Number(ROW.MF_CC) != 0" />
				</COLUMN>
				<COLUMN dataField="MF_CC"
					type="Number"
					outputFormat="#"
					width="80"
					visible="true"
					fixed="false"
					headerText="PRO Facturé@LF@Chiffrier@LF@Clinique"
					align="right"
					multiLine="sum"					
					totalFunction="sum"/>
				
				<COLUMN dataField="MR_SC"
					type="Number"
					outputFormat="#"
					width="80"
					fixed="false"
					headerText="CTI Réglé@LF@Clients@LF@Clinique"
					align="right"
					multiLine="sum"					
					totalFunction="sum">
					<CELLSTYLE name="cellIndicator" value="circle, 5, right top, 0xFF6600" 
							condition="ROW.MR_SC != ROW.MR_CC &amp;&amp; Number(ROW.MR_CC) != 0" />
				</COLUMN>
				<COLUMN dataField="MR_CC"
					type="Number"
					outputFormat="#"
					width="80"
					fixed="false"
					visible="true"
					headerText="PRO Réglé@LF@Chiffrier@LF@Clinique"
					align="right"
					multiLine="sum"					
					totalFunction="sum"/>
				
				<COLUMN dataField="MF_DH"
					type="Number"
					outputFormat="#"
					width="80"
					fixed="false"
					headerText="CTI Facturé@LF@Détail@LF@HON"
					multiLine="sum"	
					totalFunction="sum"
					align="right" >
					<CELLSTYLE name="cellIndicator" value="circle, 5, right top, 0xFF6600" 
							condition="ROW.MF_DH != ROW.MF_CH &amp;&amp; Number(ROW.MF_CH) != 0" />
				</COLUMN>
				<COLUMN dataField="MF_TH"
					type="Number"
					outputFormat="#"
					width="80"
					fixed="false"
					headerText="CTI Facturé@LF@Total@LF@HON"
					align="right"
					multiLine="sum"					
					totalFunction="sum">
					<CELLSTYLE name="cellIndicator" value="circle, 5, right top, 0xFF6600" 
							condition="ROW.MF_TH != ROW.MF_CH &amp;&amp; Number(ROW.MF_CH) != 0" />
				</COLUMN>
				<COLUMN dataField="MF_SH"
					type="Number"
					outputFormat="#"
					width="80"
					fixed="false"
					headerText="CTI Facturé@LF@Clients@LF@HON"
					align="right"
					multiLine="sum"					
					totalFunction="sum">
					<CELLSTYLE name="cellIndicator" value="circle, 5, right top, 0xFF6600" 
							condition="ROW.MF_SH != ROW.MF_CH &amp;&amp; Number(ROW.MF_CH) != 0" />
				</COLUMN>
				<COLUMN dataField="MF_CH"
					type="Number"
					outputFormat="#"
					width="80"
					fixed="false"
					visible="true"
					headerText="PRO Facturé@LF@Chiffrier@LF@HON"
					align="right"
					multiLine="sum"					
					totalFunction="sum"/>
					
				<COLUMN dataField="MR_SH"
					type="Number"
					outputFormat="#"
					width="80"
					fixed="false"
					headerText="CTI Réglé@LF@Clients@LF@HON"
					align="right"
					multiLine="sum"					
					totalFunction="sum">
					<CELLSTYLE name="cellIndicator" value="circle, 5, right top, 0xFF6600" 
							condition="ROW.MR_SH != ROW.MR_CH &amp;&amp; Number(ROW.MR_CH) != 0" />
				</COLUMN>
				<COLUMN dataField="MR_CH"
					type="Number"
					outputFormat="#"
					width="80"
					fixed="false"
					visible="true"
					headerText="PRO Réglé@LF@Chiffrier@LF@HON"
					align="right"
					multiLine="sum"					
					totalFunction="sum"/>
					
						
			</DATAGRID>
		
		</ONGLET>
		
		
		<ONGLET excelLabel="Erreurs Factures" label="Erreurs Factures" queryNumber="1">
				
			<DATAGRID title="" total="false" headerHeight="54" printRatio="1">
			
				<COLUMN dataField="FAC" 
					type="Char"
					width="100"
					visible="true"
					headerText="Facture" 
					textAlign="left"
					totalFunction="text"
					totalComplement="TOTAL"
				/>
														
				<COLUMN dataField="DATE" 
					type="Date"
					inputFormat="AAAA-MM-JJ"
					outputFormat="MM YYYY"
					width="50"
					visible="true"
					headerText="Mois" 
					textAlign="left"
					totalFunction="text"
					totalComplement="TOTAL"
				/>
				
				<COLUMN dataField="SEJ" 
					type="Char"
					width="100"
					visible="true"
					headerText="Séjour" 
					textAlign="left"
					totalFunction="text"
					totalComplement="TOTAL"
				/>
				
				<COLUMN dataField="NOM" 
					type="Char"
					minWidth="100"
					visible="true"
					headerText="Nom" 
					textAlign="left"
					totalFunction="text"
					totalComplement="TOTAL"
				/>
				
				
				<COLUMN dataField="MF_DC"
					type="Number"
					outputFormat="#,00"
					width="75"
					fixed="false"
					headerText="CTI Facturé@LF@Détail@LF@Clinique"
					multiLine="sum"	
					totalFunction="sum"
					align="right">
					<CELLSTYLE name="cellIndicator" value="circle, 5, right top, 0xFF6600" 
							condition="ROW.MF_DC != ROW.MF_CC " />
				</COLUMN>
				<COLUMN dataField="MF_TC"
					type="Number"
					outputFormat="#,00"
					width="75"
					fixed="false"
					headerText="CTI Facturé@LF@Total@LF@Clinique"
					align="right"
					multiLine="sum"					
					totalFunction="sum">
					<CELLSTYLE name="cellIndicator" value="circle, 5, right top, 0xFF6600" 
							condition="ROW.MF_TC != ROW.MF_CC " />
				</COLUMN>
				<COLUMN dataField="MF_SC"
					type="Number"
					outputFormat="#,00"
					width="75"
					fixed="false"
					headerText="CTI Facturé@LF@Clients@LF@Clinique"
					align="right"
					multiLine="sum"					
					totalFunction="sum">
					<CELLSTYLE name="cellIndicator" value="circle, 5, right top, 0xFF6600" 
							condition="ROW.MF_SC != ROW.MF_CC " />
				</COLUMN>
				<COLUMN dataField="MF_CC"
					type="Number"
					outputFormat="#,00"
					width="75"
					visible="true"
					fixed="false"
					headerText="PRO Facturé@LF@Chiffrier@LF@Clinique"
					align="right"
					multiLine="sum"					
					totalFunction="sum"/>
				
				<COLUMN dataField="MR_SC"
					type="Number"
					outputFormat="#,00"
					width="75"
					fixed="false"
					headerText="CTI Réglé@LF@Clients@LF@Clinique"
					align="right"
					multiLine="sum"					
					totalFunction="sum">
					<CELLSTYLE name="cellIndicator" value="circle, 5, right top, 0xFF6600" 
							condition="ROW.MR_SC != ROW.MR_CC " />
				</COLUMN>
				<COLUMN dataField="MR_CC"
					type="Number"
					outputFormat="#,00"
					width="75"
					fixed="false"
					visible="true"
					headerText="PRO Réglé@LF@Chiffrier@LF@Clinique"
					align="right"
					multiLine="sum"					
					totalFunction="sum"/>
				
				<COLUMN dataField="MF_DH"
					type="Number"
					outputFormat="#,00"
					width="75"
					fixed="false"
					headerText="CTI Facturé@LF@Détail@LF@HON"
					multiLine="sum"	
					totalFunction="sum"
					align="right" >
					<CELLSTYLE name="cellIndicator" value="circle, 5, right top, 0xFF6600" 
							condition="ROW.MF_DH != ROW.MF_CH " />
				</COLUMN>
				<COLUMN dataField="MF_TH"
					type="Number"
					outputFormat="#,00"
					width="75"
					fixed="false"
					headerText="CTI Facturé@LF@Total@LF@HON"
					align="right"
					multiLine="sum"					
					totalFunction="sum">
					<CELLSTYLE name="cellIndicator" value="circle, 5, right top, 0xFF6600" 
							condition="ROW.MF_TH != ROW.MF_CH " />
				</COLUMN>
				<COLUMN dataField="MF_SH"
					type="Number"
					outputFormat="#,00"
					width="75"
					fixed="false"
					headerText="CTI Facturé@LF@Clients@LF@HON"
					align="right"
					multiLine="sum"					
					totalFunction="sum">
					<CELLSTYLE name="cellIndicator" value="circle, 5, right top, 0xFF6600" 
							condition="ROW.MF_SH != ROW.MF_CH " />
				</COLUMN>
				<COLUMN dataField="MF_CH"
					type="Number"
					outputFormat="#,00"
					width="75"
					fixed="false"
					visible="true"
					headerText="PRO Facturé@LF@Chiffrier@LF@HON"
					align="right"
					multiLine="sum"					
					totalFunction="sum"/>
					
				<COLUMN dataField="MR_SH"
					type="Number"
					outputFormat="#,00"
					width="75"
					fixed="false"
					headerText="CTI Réglé@LF@Clients@LF@HON"
					align="right"
					multiLine="sum"					
					totalFunction="sum">
					<CELLSTYLE name="cellIndicator" value="circle, 5, right top, 0xFF6600" 
							condition="ROW.MR_SH != ROW.MR_CH " />
				</COLUMN>
				<COLUMN dataField="MR_CH"
					type="Number"
					outputFormat="#,00"
					width="75"
					fixed="false"
					visible="true"
					headerText="PRO Réglé@LF@Chiffrier@LF@HON"
					align="right"
					multiLine="sum"					
					totalFunction="sum"/>
					
						
			</DATAGRID>
		
		</ONGLET>
		
		
				
	</PRESENTATION>
	
</VUE>

			
