<?xml version="1.0" encoding="ISO-8859-1"?>
<VUE name="ACTI000093" 
	label="Validation chiffrier comptable" 
	database="iCTI" 
	swf="*CTI_view1" 
	softCode="iCTI_activite"
	globals="ACTI_globals.XML"
	admProvider="">
	<CACHE cachable="false" />
	<title><![CDATA[= 
			var title:String = ''
			
			if (SELECT.VAL_OPTION != 'V') {
				title = title + 'INValidation ';
			}
			else {
				title = title + 'Validation ';
			}
			title = title + ' chiffrier comptable ';
			
			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]"/>
											
		<PROPERTY name="SET_VALIDE" value="[EVAL 
												if ('[VAL_OPTION]' != 'V') {return '0';}
												return '1';
											EVAL]"/>

		
	</VIEWPROPERTIES>
	
	<SELECTIONS label="Paramètres">
	
		<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" 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" tree="true" width="200" default="-1" globalListProvider="PROVIDERS">
			</FIELD>
		</GROUP>
		
		<GROUP label="Option">
			<FIELD name="VAL_OPTION" label="" UI="combo" width="200" default="V" >
				<OPTION label="Validation" data="V"/>
				<OPTION label="INValidation" data="IV"/>
			</FIELD>
		</GROUP>
		
	</SELECTIONS>
	
	<QUERIES> 
		
		<QUERY> 
			<SQL>
				<select><![CDATA[
							
							DELETE FROM activite.t_controle_chiffrier 
							WHERE date_comptable BETWEEN '[VIEW.PERIODE]' AND '[VIEW.TOPERIODE]'
								[VIEW.PROVIDERC_SELECT];
				
							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
							) ;
					
							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
							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
							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
							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' 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
							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' 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
							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 ;
							
							
							INSERT INTO activite.t_controle_chiffrier(
					            date_comptable, 
								provider_id, 
								montant_comptabilise_d_c,
								montant_comptabilise_t_c,
								montant_comptabilise_s_c,
								montant_comptabilise_c_c,
								montant_regle_s_c,
								montant_regle_c_c,
								montant_comptabilise_d_h,
								montant_comptabilise_t_h,
								montant_comptabilise_s_h,
								montant_comptabilise_c_h,
								montant_regle_s_h,
								montant_regle_c_h,
								valide)
							SELECT
								date_comptable, 
								provider_id, 
								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),
								'[VIEW.SET_VALIDE]'::text
							FROM w_chiffrier_comptable
							GROUP BY 1,2;
							
					
					
							SELECT 
								date_comptable,
								COALESCE(t_providers.texte,''),
								CASE WHEN valide = '1' THEN 'Mois Validé' ELSE 'Mois INValidé' END
							FROM activite.t_controle_chiffrier
							LEFT JOIN activite.t_providers ON provider_id = t_providers.oid
							WHERE date_comptable BETWEEN '[VIEW.PERIODE]' AND '[VIEW.TOPERIODE]'
								[VIEW.PROVIDERC_SELECT]
							ORDER BY 1 DESC, 2;
							
					]]></select>
				<FIELDS>
					<FIELD name="DATE" />
					<FIELD name="PROV" />
					<FIELD name="VAL" />
				</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
							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>
		
		
			
	</QUERIES> 
	
	<CALCFIELDS>
	</CALCFIELDS>
	
	<PRESENTATION>
	
		<VIEWLINKS>
		
			
		</VIEWLINKS>
			
				
		
		
		<ONGLET excelLabel="Chiffrier Comptable" label="Chiffrier Comptable" queryNumber="0">
				
			<DATAGRID title="" total="false" headerHeight="54" printRatio="1">
														
				<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="PROV"
					type="Char"
					width="200"
					headerText="Solution administrative"
					align="right">
				</COLUMN>
				
				<COLUMN dataField="VAL"
					type="Char"
					minWidth="200"
					headerText="Etat validation"
					align="right">
				</COLUMN>
				
					
						
			</DATAGRID>
		
		</ONGLET>
		
		
		
				
	</PRESENTATION>
	
</VUE>

			
