<?xml version="1.0" encoding="ISO-8859-1"?>
<VUE name="COMPTA000021" 
	label="Chiffrier CTI - Comptabilité" title="Chiffrier CTI - Comptabilité" 
	database="iCTI" swf="*CTI_view1" 
	softCode="iCTI_compta" 
	globals="COMPTA_globals.XML"
	helpFile="iCTI_COMPTA_COMPTA000021.HTML">
	<CACHE cachable="false" />
	<VIEWPROPERTIES>
		<PROPERTY name="CH_SELECT" >
			<value><![CDATA[
				[EVAL 

				if ('[CH]' != '-1') {
					return " AND clinique_honoraire = '[CH]'";
				}

				return '';
				EVAL]
				]]>
			</value>
		</PROPERTY>
		<PROPERTY dataLink="VIEWPROPERTIES" nameField="name" valueField="value"/>
	</VIEWPROPERTIES>
	
	<SELECTIONS label="Comptabilité">
		<GROUP label="Comptabilité">
			<FIELD name="CH" label="" UI="combo" width="250" default="C">
				<OPTION data="-1" label="Clinique &amp; Honoraires"/>
				<OPTION data="C" label="Clinique"/>
				<OPTION data="H" label="Honoraires"/>
			</FIELD>
		</GROUP>
		<GROUP label="Période">
			<FIELD name="MOIS" label="" UI="combo" tree="true" width="200" default="000101">
				<OPTION label="Pas de sélection" data="000101"/>
				<OPTION dataLink="MOIS" dataField="cod" labelField="txt" treeLevelField="lvl"/>
			</FIELD>
			<FIELD name="TOMOIS" label="" UI="combo" tree="true" width="200" default="209912">
				<OPTION label="Pas de sélection" data="209912"/>
				<OPTION dataLink="TOMOIS" dataField="cod" labelField="txt" treeLevelField="lvl"/>
			</FIELD>
		</GROUP>
	</SELECTIONS>
	<QUERIES>
		<QUERY>
            <SQL>
				<select><![CDATA[
					DROP TABLE IF EXISTS w_controle_chiffrier;
					CREATE TEMP TABLE w_controle_chiffrier AS

					SELECT mois_comptable, 
						clinique_honoraire, 
						site_id,
						'V'::text AS valide,
						count(*) AS nb_ecritures, 
						round(SUM(montant_debit),2) AS debit_detail, 
						round(SUM(montant_credit),2) AS credit_detail,
						0::numeric AS debit_total,
						0::numeric AS credit_total,
						0::numeric AS debit_chiffrier,
						0::numeric AS credit_chiffrier
					FROM compta.p_historique_ecritures
					WHERE ajustement NOT IN ('1','2','3') AND ecriture_consolidee <= '0' AND
						p_historique_ecritures.mois_comptable BETWEEN [MOIS] AND [TOMOIS] AND
                        is_budget <> '1'
								[VIEW.CH_SELECT]
					GROUP BY 1,2,3;

					INSERT INTO w_controle_chiffrier
					SELECT mois_comptable, 
						clinique_honoraire, 
						site_id,
						'V'::text AS valide,
						0, 
						0::numeric AS debit_detail,
						0::numeric AS credit_detail,
						round(SUM(montant_debit),2) AS debit_total, 
						round(SUM(montant_credit),2) AS credit_total,
						0::numeric AS debit_chiffrier,
						0::numeric AS credit_chiffrier
					FROM compta.p_historique_ecritures_total p_historique_ecritures
					WHERE ajustement NOT IN ('1','2','3') AND ecriture_consolidee <= '0' AND
						p_historique_ecritures.mois_comptable BETWEEN [MOIS] AND [TOMOIS] AND
                        is_budget <> '1'
								[VIEW.CH_SELECT]
					GROUP BY 1,2,3;

					INSERT INTO w_controle_chiffrier
					SELECT mois_comptable, 
						clinique_honoraire, 
						site_id,
						'V'::text AS valide,
						0, 
						0::numeric AS debit_detail,
						0::numeric AS credit_detail,
						0::numeric AS debit_total, 
						0::numeric AS credit_total,
						round(SUM(montant_debit),2) AS debit_chiffrier,
						round(SUM(montant_credit),2) AS credit_chiffrier
					FROM compta.p_chiffrier_comptable
					WHERE mois_comptable BETWEEN [MOIS] AND [TOMOIS]
								[VIEW.CH_SELECT]
					GROUP BY 1,2,3;

					UPDATE w_controle_chiffrier
					SET valide = ''
					FROM compta.t_controle_chiffrier
					WHERE w_controle_chiffrier.mois_comptable = t_controle_chiffrier.mois_comptable AND
						 w_controle_chiffrier.clinique_honoraire = t_controle_chiffrier.clinique_honoraire AND
						 w_controle_chiffrier.site_id = t_controle_chiffrier.site_id AND
						 t_controle_chiffrier.valide <> '1'; 

					CTISELECT_TREE
						GROUPBY1{[VIEW.GROUPBY_OID_FIELD],, [VIEW.GROUPBY_COD_FIELD],, [VIEW.GROUPBY_TXT_FIELD]}
						GROUPBY1MODE{TAB}
						GROUPBY2{[VIEW.GROUPBY_2_OID_FIELD],, [VIEW.GROUPBY_2_COD_FIELD],, [VIEW.GROUPBY_2_TXT_FIELD]}
						ORDERBY{indicateurs[9],, DESC}
						INDICATORS{
							SUM(nb_ecritures),, 
							SUM(debit_detail),,
							SUM(credit_detail),,
							SUM(debit_detail)-SUM(credit_detail),,
							SUM(debit_total),,
							SUM(credit_total),,
							SUM(debit_chiffrier),,
							SUM(credit_chiffrier),,
							MAX(mois_comptable),,
						}	
						INDICATORS_TEXT{
							MAX(w_controle_chiffrier.clinique_honoraire),,
							MIN(valide),,
						}	
					FROM w_controle_chiffrier
					JOIN base.p_calendrier_mois ON p_calendrier_mois.mois = mois_comptable
					LEFT JOIN compta.t_sites ON site_id = t_sites.oid


				]]></select>
				<FIELDS>
					<INSERT name="CTISELECT_TREE_FIELDS"/>
					<FIELD name="SORT" />
					<FIELD name="NB"/>
					<FIELD name="DEB"/>
					<FIELD name="CRE"/>
					<FIELD name="SOL"/>
					<FIELD name="DEBT"/>
					<FIELD name="CRET"/>
					<FIELD name="DEBC"/>
					<FIELD name="CREC"/>
					<FIELD name="FILLER" />
					<FIELD name="FILLER2" />
					<FIELD name="CH"/>
					<FIELD name="VAL"/>
				</FIELDS>
			</SQL>
			
		</QUERY>
        
        <QUERY>
            <SQL>
				<select><![CDATA[
					DROP TABLE IF EXISTS w_controle_chiffrier_compte;
					CREATE TEMP TABLE w_controle_chiffrier_compte AS

					SELECT p_source.mois_comptable, 
						p_source.clinique_honoraire, 
						p_source.site_id,
						p_source.compte_id,
						count(*) AS nb_ecritures, 
						round(SUM(p_source.montant_debit),2) AS debit_detail, 
						round(SUM(p_source.montant_credit),2) AS credit_detail,
						0::numeric AS debit_total,
						0::numeric AS credit_total,
						0::numeric AS debit_chiffrier,
						0::numeric AS credit_chiffrier
					FROM compta.p_historique_ecritures p_source
                    JOIN compta.p_chiffrier_comptable ON
                        p_source.clinique_honoraire = p_chiffrier_comptable.clinique_honoraire AND
                        p_source.site_id = p_chiffrier_comptable.site_id AND
                        p_source.mois_comptable = p_chiffrier_comptable.mois_comptable AND
                        (p_chiffrier_comptable.montant_debit <> 0 OR p_chiffrier_comptable.montant_credit <> 0)
					WHERE '[VIEW.WITH_CHIFFRIER_COMPTE]' = 'true' AND 
                        ajustement NOT IN ('1','2','3') AND ecriture_consolidee <= '0' AND
						p_source.mois_comptable BETWEEN [MOIS] AND [TOMOIS] AND
                        is_budget <> '1'
					GROUP BY 1,2,3,4;

					INSERT INTO w_controle_chiffrier_compte
					SELECT p_source.mois_comptable, 
						p_source.clinique_honoraire, 
						p_source.site_id,
						p_source.compte_id,
						0, 
						0::numeric AS debit_detail,
						0::numeric AS credit_detail,
						round(SUM(p_source.montant_debit),2) AS debit_total, 
						round(SUM(p_source.montant_credit),2) AS credit_total,
						0::numeric AS debit_chiffrier,
						0::numeric AS credit_chiffrier
					FROM compta.p_historique_ecritures_total p_source
                    JOIN compta.p_chiffrier_comptable ON
                        p_source.clinique_honoraire = p_chiffrier_comptable.clinique_honoraire AND
                        p_source.site_id = p_chiffrier_comptable.site_id AND
                        p_source.mois_comptable = p_chiffrier_comptable.mois_comptable AND
                        (p_chiffrier_comptable.montant_debit <> 0 OR p_chiffrier_comptable.montant_credit <> 0)
					WHERE '[VIEW.WITH_CHIFFRIER_COMPTE]' = 'true' AND 
                        ajustement NOT IN ('1','2','3') AND ecriture_consolidee <= '0' AND
						p_source.mois_comptable BETWEEN [MOIS] AND [TOMOIS] AND
                        is_budget <> '1'
					GROUP BY 1,2,3,4;

					INSERT INTO w_controle_chiffrier_compte
					SELECT p_source.mois_comptable, 
						p_source.clinique_honoraire, 
						p_source.site_id,
						p_source.compte_id,
						0, 
						0::numeric AS debit_detail,
						0::numeric AS credit_detail,
						0::numeric AS debit_total, 
						0::numeric AS credit_total,
						round(SUM(p_source.montant_debit),2) AS debit_chiffrier,
						round(SUM(p_source.montant_credit),2) AS credit_chiffrier
					FROM compta.p_chiffrier_comptable_compte p_source
                    JOIN compta.p_chiffrier_comptable ON
                        p_source.clinique_honoraire = p_chiffrier_comptable.clinique_honoraire AND
                        p_source.site_id = p_chiffrier_comptable.site_id AND
                        p_source.mois_comptable = p_chiffrier_comptable.mois_comptable AND
                        (p_chiffrier_comptable.montant_debit <> 0 OR p_chiffrier_comptable.montant_credit <> 0)
					WHERE '[VIEW.WITH_CHIFFRIER_COMPTE]' = 'true' AND 
                        p_source.mois_comptable BETWEEN [MOIS] AND [TOMOIS]
					GROUP BY 1,2,3,4;


					SELECT
                        mois_comptable,
                        t_sites.code || ' ' || t_sites.texte,
                        t_comptes_nonsignes.numero || ' ' || t_comptes_nonsignes.texte,
                        SUM(debit_detail),
                        SUM(credit_detail),
                        SUM(debit_total),
                        SUM(credit_total),
                        SUM(debit_chiffrier),
                        SUM(credit_chiffrier)
					FROM w_controle_chiffrier_compte
					JOIN base.p_calendrier_mois ON p_calendrier_mois.mois = mois_comptable
					LEFT JOIN compta.t_sites ON site_id = t_sites.oid
					LEFT JOIN compta.t_comptes ON compte_id = t_comptes.oid
					LEFT JOIN compta.t_comptes t_comptes_nonsignes ON t_comptes.compte_nonsigne_id = t_comptes_nonsignes.oid
					GROUP BY 1,2,3
					HAVING SUM(debit_detail) <> SUM(debit_total) OR
						SUM(debit_detail) <> SUM(debit_chiffrier) OR
						SUM(credit_detail) <> SUM(credit_total) OR
						SUM(credit_detail) <> SUM(credit_chiffrier)
                    ORDER BY 1 DESC,2,3
                    ;


				]]></select>
				<FIELDS>
					<FIELD name="MOIS" />
                    <FIELD name="SITE" />
                    <FIELD name="CPTE" />
					<FIELD name="DEB"/>
					<FIELD name="CRE"/>
					<FIELD name="DEBT"/>
					<FIELD name="CRET"/>
					<FIELD name="DEBC"/>
					<FIELD name="CREC"/>
				</FIELDS>
			</SQL>
			
		</QUERY>
		
		<QUERY type="propertiesLink" name="VIEWPROPERTIES" forSelections="false">
			<SQL>
				<select><![CDATA[
						SELECT 'GROUPBY_OID_FIELD', CASE WHEN count(DISTINCT site_id) > 1 THEN 'site_id' ELSE 'mois_comptable' END
						FROM compta.p_chiffrier_comptable
						WHERE '1'='1' [VIEW.CH_SELECT] 
						UNION
						SELECT 'GROUPBY_COD_FIELD', CASE WHEN count(DISTINCT site_id) > 1 THEN 't_sites.code' ELSE 'mois_comptable' END
						FROM compta.p_chiffrier_comptable
						WHERE '1'='1' [VIEW.CH_SELECT] 
						UNION
						SELECT 'GROUPBY_TXT_FIELD', CASE WHEN count(DISTINCT site_id) > 1 THEN 't_sites.texte' ELSE 'p_calendrier_mois.texte_court' END
						FROM compta.p_chiffrier_comptable
						WHERE '1'='1' [VIEW.CH_SELECT] 
						UNION
						SELECT 'GROUPBY_2_OID_FIELD', CASE WHEN count(DISTINCT site_id) > 1 THEN 'mois_comptable' ELSE '' END
						FROM compta.p_chiffrier_comptable
						WHERE '1'='1' [VIEW.CH_SELECT] 
						UNION
						SELECT 'GROUPBY_2_COD_FIELD', CASE WHEN count(DISTINCT site_id) > 1 THEN 'mois_comptable' ELSE '' END
						FROM compta.p_chiffrier_comptable
						WHERE '1'='1' [VIEW.CH_SELECT] 
						UNION
						SELECT 'GROUPBY_2_TXT_FIELD', CASE WHEN count(DISTINCT site_id) > 1 THEN 'p_calendrier_mois.texte_court' ELSE '' END
						FROM compta.p_chiffrier_comptable
						WHERE '1'='1' [VIEW.CH_SELECT] 
                        UNION
						SELECT 'WITH_CHIFFRIER_COMPTE', CASE WHEN count(*) > 0 THEN 'true' ELSE 'false' END
						FROM compta.p_chiffrier_comptable_compte
						WHERE '1'='1' [VIEW.CH_SELECT] 
				]]></select>
				<FIELDS>
					<FIELD name="name"/>
					<FIELD name="value"/>
				</FIELDS>
			</SQL>
		</QUERY>
		
		<QUERY type="comboLink" name="MOIS" forRows="false">
			<SQL>
				<select><![CDATA[
						SELECT substr(mois_comptable::text,1,4),substr(mois_comptable::text,1,4)||'00', substr(mois_comptable::text,1,4), '1'
						FROM compta.p_mois_comptables 
						GROUP BY 1
						UNION
						SELECT substr(mois_comptable::text,1,4), mois_comptable::text, texte, '2'
						FROM compta.p_mois_comptables 
						ORDER BY 1 DESC, 2
				]]></select>
				<FIELDS>
					<FIELD name="ann" />
					<FIELD name="cod" />
					<FIELD name="txt" />
					<FIELD name="lvl" />
				</FIELDS>
			</SQL>
		</QUERY>
		
		<QUERY type="comboLink" name="TOMOIS" forRows="false">
			<SQL>
				<select><![CDATA[
						SELECT substr(mois_comptable::text,1,4),substr(mois_comptable::text,1,4)||'99', substr(mois_comptable::text,1,4), '1'
						FROM compta.p_mois_comptables 
						GROUP BY 1
						UNION
						SELECT substr(mois_comptable::text,1,4), mois_comptable::text, texte, '2'
						FROM compta.p_mois_comptables 
						ORDER BY 1 DESC, 2 DESC
				]]></select>
				<FIELDS>
					<FIELD name="ann" />
					<FIELD name="cod" />
					<FIELD name="txt" />
					<FIELD name="lvl" />
				</FIELDS>
			</SQL>
		</QUERY>
	</QUERIES>
	<CALCFIELDS>
	</CALCFIELDS>
	<PRESENTATION>
		<VIEWLINKS>
			<VIEWLINK label="Valider Mois" shortLabel="Valider Mois" view="COMPTA000054.XML">
				<ARG name="MOIS" value="ROW.MOIS" />
				<ARG name="TOMOIS" value="ROW.MOIS" />
				<ARG name="VAL_OPTION" value="'V'" />
			</VIEWLINK>
			<VIEWLINK label="INValider Mois" shortLabel="INValider Mois" view="COMPTA000054.XML">
				<ARG name="MOIS" value="ROW.MOIS" />
				<ARG name="TOMOIS" value="ROW.MOIS" />
				<ARG name="VAL_OPTION" value="'IV'" />
			</VIEWLINK>
		</VIEWLINKS>
		
		<ONGLET excelLabel="Chiffrier" label="Chiffrier" queryNumber="0" multiTab="true">
			<DATAGRID 	title="" 
						total="false" 
						headerHeight="40" 
						variableRowHeight="true" 
						totalRowInRows="= ROW.OID == '-9999999'"> 
				
				<INSERT name="DATAGRID_MULTITAB_STYLES"/>
				

				<COLUMN dataField="OID"
					type="Number"
					visible="false" />
				<COLUMN dataField="COD" 
					type="Char" 
					width="100"
					outputFormat="#"
					headerText="Code" 
					textAlign="left" 
					totalComplement="*ROW">
					<INSERT name="COLUMN_COD_MULTITAB_STYLES"/>
				</COLUMN>
				<COLUMN dataField="TXT" 
					type="Char" 
					minWidth="110"
					headerText="Texte" 
					textAlign="left" 
					totalComplement="*ROW"/>
				<COLUMN dataField="CH" type="Char"   width="50" headerText="C/H" align="center"/>
				<COLUMN dataField="VAL"
					type="Char"
					outputFormat="#"
					width="40"
					headerText="Validé"
					align="center">
				</COLUMN>
				
				<COLUMN dataField="NB" type="Number" outputFormat="#" width="80" headerText="Nombre écritures" totalFunction="sum" align="right"/>
				<COLUMN dataField="DEB" type="Number" outputFormat="#" width="80" headerText="Débit@LF@Détail" totalFunction="sum" align="right">
					<CELLSTYLE name="cellIndicator" value="circle, 5, left top, 0xFF6600" condition="ROW.VAL != 'V' &amp;&amp; ROW.DEBC != ROW.DEB &amp;&amp; ROW.DEBC != 0"/>
				</COLUMN>
				<COLUMN dataField="CRE" type="Number" outputFormat="#" width="80" headerText="Crédit@LF@Détail" align="right" totalFunction="sum">
					<CELLSTYLE name="cellIndicator" value="circle, 5, left top, 0xFF6600" condition="ROW.VAL != 'V' &amp;&amp; ROW.CREC != ROW.CRE &amp;&amp; ROW.DEBC != 0"/>
				</COLUMN>
				<COLUMN dataField="SOL" type="Number" outputFormat="#" width="80" headerText="Solde@LF@Détail" align="right" totalFunction="sum">
					<CELLSTYLE name="cellIndicator" value="circle, 5, left top, 0xFF6600" condition="ROW.SOL != 0"/>
				</COLUMN>
				<COLUMN dataField="DEBT" type="Number" outputFormat="#" width="80" fixed="false" headerText="Débit@LF@Cumuls" totalFunction="sum" align="right">
					<CELLSTYLE name="cellIndicator" value="circle, 5, left top, 0xFF6600" condition="ROW.DEBT != ROW.DEB"/>
				</COLUMN>
				<COLUMN dataField="CRET" type="Number" outputFormat="#" width="80" fixed="false" headerText="Crédit@LF@Cumuls" align="right" totalFunction="sum">
					<CELLSTYLE name="cellIndicator" value="circle, 5, left top, 0xFF6600" condition="ROW.CRET != ROW.CRE"/>
				</COLUMN>
				
				<COLUMN dataField="DEBC" type="Number" outputFormat="#" width="80" fixed="false" headerText="Débit@LF@Prod" totalFunction="sum" align="right">
					<CELLSTYLE name="cellIndicator" value="circle, 5, left top, 0xFF6600" condition="ROW.VAL != 'V' &amp;&amp; ROW.DEBC != ROW.DEB &amp;&amp; ROW.DEBC != 0"/>
				</COLUMN>
				<COLUMN dataField="CREC" type="Number" outputFormat="#" width="80" fixed="false" headerText="Crédit@LF@Prod" align="right" totalFunction="sum">
					<CELLSTYLE name="cellIndicator" value="circle, 5, left top, 0xFF6600" condition="ROW.VAL != 'V' &amp;&amp; ROW.CREC != ROW.CRE &amp;&amp; ROW.DEBC != 0"/>
				</COLUMN>
			</DATAGRID>
			
		</ONGLET>
        
        
        
        <ONGLET excelLabel="Erreurs comptes" label="Erreurs comptes" queryNumber="1" visible="[VIEW.WITH_CHIFFRIER_COMPTE]" >
			<DATAGRID 	title="" 
						total="false" 
						headerHeight="40"  > 
				
				<COLUMN dataField="MOIS" 
					type="Char" 
					width="100"
					outputFormat="#"
					headerText="Mois" 
					textAlign="left" 
					totalComplement="*ROW">
				</COLUMN>
				<COLUMN dataField="SITE" 
					type="Char" 
					minWidth="110"
					headerText="Site" 
					textAlign="left" 
					totalComplement="*ROW"/>
                <COLUMN dataField="CPTE" 
					type="Char" 
					minWidth="110"
					headerText="Compte" 
					textAlign="left" 
					totalComplement="*ROW"/>
				
				<COLUMN dataField="DEB" type="Number" outputFormat="#" width="80" headerText="Débit@LF@Détail" totalFunction="sum" align="right">
					<CELLSTYLE name="cellIndicator" value="circle, 5, left top, 0xFF6600" condition="ROW.DEBC != ROW.DEB &amp;&amp; ROW.DEBC != 0"/>
				</COLUMN>
				<COLUMN dataField="CRE" type="Number" outputFormat="#" width="80" headerText="Crédit@LF@Détail" align="right" totalFunction="sum">
					<CELLSTYLE name="cellIndicator" value="circle, 5, left top, 0xFF6600" condition="ROW.CREC != ROW.CRE &amp;&amp; ROW.DEBC != 0"/>
				</COLUMN>
				<COLUMN dataField="DEBT" type="Number" outputFormat="#" width="80" fixed="false" headerText="Débit@LF@Cumuls" totalFunction="sum" align="right">
					<CELLSTYLE name="cellIndicator" value="circle, 5, left top, 0xFF6600" condition="ROW.DEBT != ROW.DEB"/>
				</COLUMN>
				<COLUMN dataField="CRET" type="Number" outputFormat="#" width="80" fixed="false" headerText="Crédit@LF@Cumuls" align="right" totalFunction="sum">
					<CELLSTYLE name="cellIndicator" value="circle, 5, left top, 0xFF6600" condition="ROW.CRET != ROW.CRE"/>
				</COLUMN>
				<COLUMN dataField="DEBC" type="Number" outputFormat="#" width="80" fixed="false" headerText="Débit@LF@Prod" totalFunction="sum" align="right">
					<CELLSTYLE name="cellIndicator" value="circle, 5, left top, 0xFF6600" condition="ROW.DEBC != ROW.DEB &amp;&amp; ROW.DEBC != 0"/>
				</COLUMN>
				<COLUMN dataField="CREC" type="Number" outputFormat="#" width="80" fixed="false" headerText="Crédit@LF@Prod" align="right" totalFunction="sum">
					<CELLSTYLE name="cellIndicator" value="circle, 5, left top, 0xFF6600" condition="ROW.CREC != ROW.CRE &amp;&amp; ROW.DEBC != 0"/>
				</COLUMN>
			</DATAGRID>
			
		</ONGLET>
	</PRESENTATION>
</VUE>
