<cfflush interval="10">
<cfset lista_dns = "aud,aut,cor,dal,nsv,sof,web">
<cfloop list="#lista_dns#" index="i">
	<!--- 
    <cfquery name="pagamenti" DATASOURCE="intranet_#i#">
        select * from movimenti where tipologia like '_%'
    </cfquery>
    <cfoutput query="pagamenti">
    	<cfif left(tipologia, 1) eq "_">
        <cfset tipo = right(tipologia, len(tipologia)-1)>
            #i# #IDmovimento# #tipologia# #tipo#<br />
            <cfquery datasource="intranet_#i#">
                update movimenti set tipologia = '#tipo#' where IDmovimento = #IDmovimento#
            </cfquery>
        </cfif>
    </cfoutput>
     --->
    <cfquery name="pagamenti" DATASOURCE="intranet_#i#">
        select * from movimenti where IDFattura > 0
    </cfquery>
    <cfquery name="vendite" DATASOURCE="intranet_#i#">
        select * from fatture_attivo
        order by IDdocumento
    </cfquery>
    <cfquery name="fatture" DATASOURCE="intranet_#i#">
        select * from fatture_passivo
        order by IDdocumento
    </cfquery>
    <cfquery name="spese" DATASOURCE="intranet_#i#">
        select * from spese_generali
        order by IDdocumento
    </cfquery>
    <cfoutput query="pagamenti">
    	<cfif tipologia eq "in_" or tipologia eq "out">
            <cfquery name="fattura" dbtype="query">
                select * from fatture where IDdocumento = #IDFattura#
            </cfquery>
            <cfquery name="spesa" dbtype="query">
                select * from spese where IDdocumento = #IDspesa#
            </cfquery>
            <cfif fattura.recordcount is 0 and IDFattura gt 0>
            	#i# #IDmovimento# - fattura<br />
                <cfquery datasource="intranet_#i#">
                	update movimenti set tipologia = '-#tipologia#' where IDmovimento = #IDmovimento#
                </cfquery>
            <cfelseif spesa.recordcount is 0 and IDspesa gt 0>
            	#i# #IDmovimento# - spesa<br />
                <cfquery datasource="intranet_#i#">
                	update movimenti set tipologia = '-#tipologia#' where IDmovimento = #IDmovimento#
                </cfquery>
            </cfif>
        <cfelseif tipologia eq "out_" or tipologia eq "in">
            <cfquery name="vendita" dbtype="query">
                select * from vendite where IDdocumento = #IDFattura#
            </cfquery>
            <cfif vendita.recordcount is 0 and IDFattura gt 0>
            	#i# #IDmovimento# - vendita<br />
                <cfquery datasource="intranet_#i#">
                	update movimenti set tipologia = '-#tipologia#' where IDmovimento = #IDmovimento#
                </cfquery>
            </cfif>
        </cfif>
	</cfoutput>
</cfloop>
