
<cfset StructDelete(Variables, "error_output")>
<cfset StructDelete(Variables, "fatture_ricevute")>

<cfset StructDelete(Variables, "result")>

<cfset StructDelete(Variables, "file_path")>
<cfset StructDelete(Variables, "new_file_p7m")>
<cfset StructDelete(Variables, "xml_file_name")>

  <cfquery name="lista_aziende" datasource="intranet">
    select ID_az_, dsn, nome_az, fe_invio, fe_ricezione from aziende
	where id_az_ = #session.ID_az_#
  </cfquery>

    <cfset active_ID_az_       = Trim(lista_aziende.ID_az_)>
    <cfset active_session_dsn  = Trim(lista_aziende.dsn)>
    
    <cfdirectory directory="#application.file_path#/fatture/#active_ID_az_#/import/" name="elenco_file" action="list">
        <cfloop query="elenco_file">
           
                    <cfif FileExists("#application.file_path#/fatture/#active_ID_az_#/import/#elenco_file.name#")>
                        <cffile action="read" file="#application.file_path#/fatture/#active_ID_az_#/import/#elenco_file.name#" variable="contenuto_pulito">
                        <cfset contenuto = contenuto_pulito>
                        <cfset file_xml_origin = contenuto>
                    </cfif>
                    <cfif Len(Trim(contenuto)) gt 0>
                        <cfset posizione = findnocase('<FatturaElettronicaHeader', contenuto)>
                        <cfif posizione gt 0>
                        <cfset contenuto = right(contenuto, (len(contenuto)-(posizione-1)))>
                        </cfif>
                        <cfset posizione = findnocase("</FatturaElettronicaBody>", contenuto)>
                        <cfif posizione gt 0>
                        <cfset contenuto = left(contenuto, posizione+24)>
                        </cfif>
                    
                        <cfset contenuto = '<FatturaElettronica versione="FPR12" xmlns:p="http://ivaservizi.agenziaentrate.gov.it/docs/xsd/fatture/v1.2">' & contenuto>
                        <cfset contenuto = contenuto & "<//FatturaElettronica>">
                        <cfset contenuto = Replace(contenuto,"<//","</")><!---  --->
                        <cfif IsXML(contenuto)>
                        <cfset matrice_documento = XmlParse(contenuto)>
                        <cfelse>
                        <cfset matrice_documento = contenuto>
                        </cfif>
                    </cfif>
                    <cfif IsDefined("matrice_documento") and Len(Trim(matrice_documento)) gt 0 and IsXML(matrice_documento)>
                            <cfset new_DataFile=''>
                            <cfset new_Datacreation=''>
                            <cfset new_TipoDocumento=''>
                            <cfset new_ndc=''>
                            <cfset new_Valuta=''>
                            <cfset new_Descrizione=''>
                            <cfset new_Importo=''>
                            <cfset new_IVA=''>
                            <cfset new_CondizioniPagamento=''>
                            <cfset new_ModPagamento=''>
                            <cfset new_Scadenza=''>
                            <cfset lista_update = "">
                            <cfset new_file_xml = matrice_documento>
                                <cfset dati_fatt_head = matrice_documento.FatturaElettronica.FatturaElettronicaHeader>
                                <cfset dati_fatt_body = matrice_documento.FatturaElettronica.FatturaElettronicaBody>

                                <cfset new_NumeroFattura = dati_fatt_body.DatiGenerali.DatiGeneraliDocumento.Numero.XmlText>
                                <cfset new_InvoiceData = dati_fatt_body.DatiGenerali.DatiGeneraliDocumento.Data.XmlText>
                                <cfset new_StatoFattura = "Ricevuta">
                                
                                
                                <cfif IsDefined("dati_fatt_head.DatiTrasmissione.FormatoTrasmissione")><cfset new_invoice_Type = trim(dati_fatt_head.DatiTrasmissione.FormatoTrasmissione.XmlText)><cfelse><cfset new_invoice_Type = "FPR12"></cfif>
                                <cfset new_id_SDI = trim(elenco_file.name)>
                                <cfset new_file_update = "">
                                <cfif IsDefined("dati_fatt_body.DatiGenerali.DatiGeneraliDocumento.Data")><cfset new_creationDate = trim(dati_fatt_body.DatiGenerali.DatiGeneraliDocumento.Data.XmlText)><cfelse><cfset new_creationDate = ""></cfif>
                                <cfif IsDefined("elenco_file.name")><cfset new_nome_file_SDI = trim(elenco_file.name)><cfelse><cfset new_nome_file_SDI = ""></cfif>
                                
                                <cfif IsDefined("dati_fatt_head.CedentePrestatore.DatiAnagrafici.IdFiscaleIVA.IdPaese")><cfset new_sender_countryCode = trim(dati_fatt_head.CedentePrestatore.DatiAnagrafici.IdFiscaleIVA.IdPaese.XmlText)><cfelse><cfset new_sender_countryCode = ""></cfif>
                                <cfif IsDefined("dati_fatt_head.CedentePrestatore.DatiAnagrafici.CodiceFiscale")><cfset new_sender_fiscalCode = trim(dati_fatt_head.CedentePrestatore.DatiAnagrafici.CodiceFiscale.XmlText)><cfelse><cfset new_sender_fiscalCode = ""></cfif>
                                <cfif IsDefined("dati_fatt_head.CedentePrestatore.DatiAnagrafici.IdFiscaleIVA.IdCodice")><cfset new_sender_vatCode = trim(dati_fatt_head.CedentePrestatore.DatiAnagrafici.IdFiscaleIVA.IdCodice.XmlText)><cfelse><cfset new_sender_vatCode = ""></cfif>
                                <cfif IsDefined("dati_fatt_head.CedentePrestatore.DatiAnagrafici.Anagrafica.Denominazione")><cfset new_sender_description = trim(dati_fatt_head.CedentePrestatore.DatiAnagrafici.Anagrafica.Denominazione.XmlText)><cfelse><cfset new_sender_description = ""></cfif>
                                
                            

                                <cfif IsDate(new_file_update)>
                                    <cfset new_DataFile = createODBCDatetime(new_file_update)>
                                    <cfset lista_update = listappend(lista_update, " Data_file=#createODBCDatetime(new_file_update)#")>
                                </cfif>
                                <cfif IsDate(new_creationDate)>
                                    <cfset new_Datacreation = createODBCDatetime(new_creationDate)>
                                </cfif>
                    
                                <cfset new_IDCliente = "">
                                <cfquery name="cliente" datasource="#active_session_dsn#" maxrows="1">
                                    select * from #active_session_dsn#.anagrafica 
                                    where eliminato = 0
                                    <cfif len(new_sender_vatCode) gt 0>
                                    and (
                                        UPPER(trim(partita_iva)) = UPPER('#new_sender_vatCode#')
                                        or
                                        UPPER(trim(partita_iva)) = UPPER('IT#new_sender_vatCode#')
                                    )
                                    <cfelseif len(new_sender_fiscalCode) gt 0>
                                    and UPPER(trim(codice_fiscale)) = UPPER('#new_sender_fiscalCode#')
                                    </cfif>
                                    order by IDSocieta desc
                                </cfquery>
                                <cfif cliente.recordcount is 1>
                                    <cfset new_IDCliente = cliente.IDSocieta>
                                    <cfoutput>Fornitore già presente: #cliente.IDSocieta#<br /></cfoutput>
                                    <!--- Aggiorno il campo fornitore='si' --->
                                    <cfquery datasource="#active_session_dsn#" name="cliente_esistente">
                                    call `#active_session_dsn#`.`pr_aggiorna_fornitore` ( #cliente.IDSocieta# )
                                    </cfquery>
                                    <cfoutput>Fornitore aggiornato!<br /></cfoutput>
                                <cfelse>
                                    <!--- Fornitore non presente --->
                                    <cfset new_IDCliente = 0>
                                </cfif>
                    
                                <!--- DatiSedeCliente --->
                                <cfif IsDefined("new_IDCliente") and new_IDCliente eq 0>
                                    <cfset new_sender_Indirizzo_fatturazione = ''>
                                    <cfset new_sender_CAP_fatturazione = ''>
                                    <cfset new_sender_Citta_fatturazione = ''>
                                    <cfset new_sender_StatoOProvincia_fatturazione = ''>
                                    <cfset new_sender_Paese_fatturazione = ''>
                                    <cfif IsDefined("matrice_documento.FatturaElettronica.FatturaElettronicaHeader.CedentePrestatore.Sede")>
                                    <cfset DatiSedeCliente = XmlSearch(matrice_documento, "/FatturaElettronica/FatturaElettronicaHeader/CedentePrestatore/Sede")>
                                    <cfset DatiSedeCliente_Children = DatiSedeCliente[1].XmlChildren>
                                    <cfif IsArray(DatiSedeCliente_Children)>
                                        <cfloop from="1" to="#arraylen(DatiSedeCliente_Children)#" index="t">
                                        <cfswitch expression="#trim(DatiSedeCliente_Children[t].XmlName)#">
                                            <cfcase value="Indirizzo">
                                            <cfset new_sender_Indirizzo_fatturazione = trim(DatiSedeCliente_Children[t].XmlText)>
                                            </cfcase>
                                            <cfcase value="CAP">
                                            <cfset new_sender_CAP_fatturazione = trim(DatiSedeCliente_Children[t].XmlText)>
                                            </cfcase>
                                            <cfcase value="Comune">
                                            <cfset new_sender_Citta_fatturazione = trim(DatiSedeCliente_Children[t].XmlText)>
                                            </cfcase>
                                            <cfcase value="Provincia">
                                            <cfset new_sender_StatoOProvincia_fatturazione = trim(DatiSedeCliente_Children[t].XmlText)>
                                            </cfcase>
                                            <cfcase value="Nazione">
                                            <cfset new_sender_Paese_fatturazione = trim(DatiSedeCliente_Children[t].XmlText)>
                                            </cfcase>
                                        </cfswitch>
                                        </cfloop>
                                    </cfif>
                                    </cfif>
                                    <cfquery datasource="#active_session_dsn#" name="nuovo_cliente">
                                    SELECT `#active_session_dsn#`.`fn_inserisci_fornitore`('#Left(trim(new_sender_description),100)#', '#trim(new_sender_vatCode)#', '#trim(new_sender_fiscalCode)#', '#trim(new_sender_Indirizzo_fatturazione)#', 
                                    '#trim(new_sender_Indirizzo_fatturazione)#', '#trim(new_sender_CAP_fatturazione)#', '#trim(new_sender_CAP_fatturazione)#', '#trim(new_sender_Citta_fatturazione)#', 
                                    '#trim(new_sender_Citta_fatturazione)#', '#trim(new_sender_StatoOProvincia_fatturazione)#', '#trim(new_sender_StatoOProvincia_fatturazione)#', 
                                    '#trim(new_sender_Paese_fatturazione)#', '#trim(new_sender_Paese_fatturazione)#') as IDCliente
                                    </cfquery>
                                    <cfif IsDefined("nuovo_cliente.IDCliente") and Len(Trim(nuovo_cliente.IDCliente)) gt 0>
                                    <cfset new_IDCliente = nuovo_cliente.IDCliente>
                                    </cfif>
                                    <cfoutput>Creo il nuovo fornitore... #new_IDCliente#<br /></cfoutput>
                                </cfif>
                    
                                <!--- DatiGeneraliDocumento --->
                                <cfif IsDefined("matrice_documento.FatturaElettronica.FatturaElettronicaBody.DatiGenerali.DatiGeneraliDocumento")>
                                    <cfset matrice_DatiGenerali = XmlSearch(matrice_documento, "/FatturaElettronica/FatturaElettronicaBody/DatiGenerali/DatiGeneraliDocumento")>
                                    <cfset matrice_DatiGeneraliDocumento = matrice_DatiGenerali[1].XmlChildren>
                                    <cfif IsArray(matrice_DatiGeneraliDocumento)>
                                    <cfloop from="1" to="#arraylen(matrice_DatiGeneraliDocumento)#" index="t">
                                        <cfswitch expression="#trim(matrice_DatiGeneraliDocumento[t].XmlName)#">
                                        <cfcase value="TipoDocumento">
                                            <cfset new_TipoDocumento = trim(matrice_DatiGeneraliDocumento[t].XmlText)>
                                            <cfset lista_update = listappend(lista_update, " TipoDocumento='#trim(matrice_DatiGeneraliDocumento[t].XmlText)#'")>
                                            <cfif matrice_DatiGeneraliDocumento[t].XmlText eq "TD04">
                                            <cfset new_ndc = 'ndc'>
                                            <cfset lista_update = listappend(lista_update, " ndc='ndc'")>
                                            </cfif>
                                        </cfcase>
                                        <cfcase value="Divisa">
                                            <cfset new_Valuta = trim(matrice_DatiGeneraliDocumento[t].XmlText)>
                                            <cfset lista_update = listappend(lista_update, " Valuta='#trim(matrice_DatiGeneraliDocumento[t].XmlText)#'")>
                                        </cfcase>
                                        </cfswitch>
                                    </cfloop>
                                    </cfif>
                                </cfif>
                    
                                <!--- DatiBeniServizi --->
                                <cfset loop_desc = "">
                                <cfset loop_importo = 0>
                                <cfset loop_aliquota = 0>
                                <cfset loop_importo2 = 0>  
                                <cfset loop_aliquota2 = 0>
                                <cfif IsDefined("matrice_documento.FatturaElettronica.FatturaElettronicaBody.DatiBeniServizi")>
                                    <cfset matrice_DettaglioLinee = XmlSearch(matrice_documento, "/FatturaElettronica/FatturaElettronicaBody/DatiBeniServizi/DettaglioLinee")>
                                    <cfif IsArray(matrice_DettaglioLinee)>
                                    <cfloop from="1" to="#ArrayLen(matrice_DettaglioLinee)#" index="k">
                                        <cfset riga_importo = 0>
                                        <cfset riga_aliquota = 0>
                                        <cfset matrice_DettaglioLinea = matrice_DettaglioLinee[k].XmlChildren>
                                        <cfif IsArray(matrice_DettaglioLinea)>
                                        <cfloop from="1" to="#ArrayLen(matrice_DettaglioLinea)#" index="l">
                                            <cfswitch expression="#matrice_DettaglioLinea[l].XmlName#">
                                            <cfcase value="Descrizione">
                                                <cfset riga_desc = Replace(matrice_DettaglioLinea[l].XmlText, "|", "", "all")>
                                                <cfif ListFindNoCase(loop_desc, riga_desc, "|") is 0>
                                                <cfset loop_desc = ListAppend(loop_desc, riga_desc, "|")>
                                                </cfif>
                                            </cfcase>
                                            <cfcase value="PrezzoTotale">
                                                <cfif IsNumeric(matrice_DettaglioLinea[l].XmlText)>
                                                <cfset riga_importo = matrice_DettaglioLinea[l].XmlText>
                                                </cfif>
                                            </cfcase>
                                            <cfcase value="AliquotaIVA">
                                                <cfif IsNumeric(matrice_DettaglioLinea[l].XmlText)>
                                                <cfset riga_aliquota = matrice_DettaglioLinea[l].XmlText>
                                                </cfif>
                                            </cfcase>
                                            </cfswitch>
                                        </cfloop>
                                        <cfset loop_importo = loop_importo + riga_importo>
                                        <cfset loop_aliquota = loop_aliquota + (riga_importo*riga_aliquota/100)>
                                        </cfif>
                                    </cfloop>
                                    </cfif>
                    
                                    <!--- DatiRiepilogo --->
                                    <cfset matrice_DatiRiepilogo = XmlSearch(matrice_documento, "/FatturaElettronica/FatturaElettronicaBody/DatiBeniServizi/DatiRiepilogo")>
                                    <cfif IsArray(matrice_DatiRiepilogo)>
                                    <cfloop from="1" to="#ArrayLen(matrice_DatiRiepilogo)#" index="m">
                                        <cfset riga_importo = 0>
                                        <cfset riga_aliquota = 0>
                                        <cfset matrice_DettaglioLinea = matrice_DatiRiepilogo[m].XmlChildren>
                                        <cfif IsArray(matrice_DettaglioLinea)>
                                        <cfloop from="1" to="#ArrayLen(matrice_DettaglioLinea)#" index="n">
                                            <cfswitch expression="#matrice_DettaglioLinea[n].XmlName#">
                                            <cfcase value="ImponibileImporto">
                                                <cfif IsNumeric(matrice_DettaglioLinea[n].XmlText)>
                                                <cfset riga_importo = matrice_DettaglioLinea[n].XmlText>
                                                </cfif>
                                            </cfcase>
                                            <cfcase value="Imposta">
                                                <cfif IsNumeric(matrice_DettaglioLinea[n].XmlText)>
                                                <cfset riga_aliquota = matrice_DettaglioLinea[n].XmlText>
                                                </cfif>
                                            </cfcase>
                                            </cfswitch>
                                        </cfloop>
                                        <!--- 
                                        <cfset loop_importo2 = loop_importo + riga_importo>
                                        <cfset loop_aliquota2 = loop_aliquota + riga_aliquota>
                                        --->
                                        <cfset loop_importo2 = riga_importo>
                                        <cfset loop_aliquota2 = riga_aliquota>
                                        </cfif>
                                    </cfloop>
                                    </cfif>
                                    <!--- 
                                    <cfoutput>
                                    Voci:#loop_desc#<br />
                                    Importo: #loop_importo# | #loop_importo2#<br />
                                    Aliquota: #loop_aliquota# | #loop_aliquota2#<br />
                                    </cfoutput>
                                    --->
                                    <cfset loop_desc = Replace(loop_desc, ",", " ", "all")>
                                    <cfset loop_desc = Replace(loop_desc, "|", Chr(10), "all")>
                                    <cfif Len(Trim(loop_desc)) gt 50>
                                    <cfset loop_desc = Left(Trim(loop_desc), 50) & " .....">
                                    </cfif>
                                    <cfset new_Descrizione = trim(loop_desc)>
                                    <cfset lista_update = listappend(lista_update, " Descrizione='#loop_desc#'")>
                                    <cfset new_Importo = NumberFormat(loop_importo,'.99')>
                                    <cfset lista_update = listappend(lista_update, " Importo=#loop_importo#")>
                                    <cfset new_IVA = NumberFormat(loop_aliquota,'.99')>
                                    <cfset lista_update = listappend(lista_update, " IVA=#loop_aliquota#")>
                                </cfif>
                    
                                <!--- DatiPagamento --->
                                <cfset CondizioniPagamento = "">
                                <cfset pagamento_array = ArrayNew(2)>
                                <cfset pagamento_modalita = "">
                                <cfset pagamento_scadenza = "">
                                <cfset pagamento_riga = 0>
                                <cfif IsDefined("matrice_documento.FatturaElettronica.FatturaElettronicaBody.DatiPagamento")>
                                    <cfset matrice_DatiPagamento = XmlSearch(matrice_documento, "/FatturaElettronica/FatturaElettronicaBody/DatiPagamento")>
                                    <cfif IsArray(matrice_DatiPagamento)>
                                    <cfloop from="1" to="#ArrayLen(matrice_DatiPagamento)#" index="p">
                                        <cfset matrice_DettaglioPagamento = matrice_DatiPagamento[p].XmlChildren>
                                        <cfif IsArray(matrice_DettaglioPagamento)>
                                        <cfloop from="1" to="#ArrayLen(matrice_DettaglioPagamento)#" index="q">
                                            <cfswitch expression="#matrice_DettaglioPagamento[q].XmlName#">
                                            <cfcase value="CondizioniPagamento">
                                                <cfset CondizioniPagamento = matrice_DettaglioPagamento[q].XmlText>
                                            </cfcase>
                                            <cfcase value="DettaglioPagamento">
                                                <cfset pagamento_riga = pagamento_riga + 1>
                                                <cfloop from="1" to="7" index="x">
                                                <cfset pagamento_array[pagamento_riga][x] = "">
                                                </cfloop>
                                                <cfset Matrice_CondizioniPagamento = matrice_DettaglioPagamento[q].XmlChildren>
                                                <cfif IsArray(Matrice_CondizioniPagamento)>
                                                <cfloop from="1" to="#ArrayLen(Matrice_CondizioniPagamento)#" index="r">
                                                    <cfswitch expression="#Matrice_CondizioniPagamento[r].XmlName#">
                                                    <cfcase value="ModalitaPagamento">
                                                        <cfif Len(Trim(Matrice_CondizioniPagamento[r].XmlText)) gt 0>
                                                        <cfset pagamento_array[pagamento_riga][1] = Matrice_CondizioniPagamento[r].XmlText>
                                                        <cfif Len(Trim(pagamento_modalita)) is 0 and Len(Trim(Matrice_CondizioniPagamento[r].XmlText)) gt 0>
                                                            <cfquery name="mod_pag" datasource="#active_session_dsn#" maxrows="1">
                                                            select sigla from #active_session_dsn#.modalita_pagamento where codice = '#Matrice_CondizioniPagamento[r].XmlText#'
                                                            order by predefinito desc, IDmodalita
                                                            </cfquery>
                                                            <cfif mod_pag.recordcount is 1>
                                                            <cfset pagamento_modalita = mod_pag.sigla>
                                                            </cfif>
                                                        </cfif>
                                                        </cfif>
                                                    </cfcase>
                                                    <cfcase value="DataScadenzaPagamento">
                                                        <cfif Len(Trim(Matrice_CondizioniPagamento[r].XmlText)) gt 0>
                                                        <cfset pagamento_array[pagamento_riga][2] = Matrice_CondizioniPagamento[r].XmlText>
                                                        <cfif not isdate(pagamento_scadenza) and isdate(Matrice_CondizioniPagamento[r].XmlText)>
                                                            <cfset pagamento_scadenza = Matrice_CondizioniPagamento[r].XmlText>
                                                        </cfif>
                                                        </cfif>
                                                    </cfcase>
                                                    <cfcase value="ImportoPagamento">
                                                        <cfif Len(Trim(Matrice_CondizioniPagamento[r].XmlText)) gt 0>
                                                        <cfset pagamento_array[pagamento_riga][3] = Matrice_CondizioniPagamento[r].XmlText>
                                                        </cfif>
                                                    </cfcase>
                                                    <cfcase value="CodicePagamento">
                                                        <cfif Len(Trim(Matrice_CondizioniPagamento[r].XmlText)) gt 0>
                                                        <cfset pagamento_array[pagamento_riga][4] = Matrice_CondizioniPagamento[r].XmlText>
                                                        </cfif>
                                                    </cfcase>
                                                    <cfcase value="IstitutoFinanziario">
                                                        <cfif Len(Trim(Matrice_CondizioniPagamento[r].XmlText)) gt 0>
                                                        <cfset pagamento_array[pagamento_riga][5] = Matrice_CondizioniPagamento[r].XmlText>
                                                        </cfif>
                                                    </cfcase>
                                                    <cfcase value="IBAN">
                                                        <cfif Len(Trim(Matrice_CondizioniPagamento[r].XmlText)) gt 0>
                                                        <cfset pagamento_array[pagamento_riga][6] = Matrice_CondizioniPagamento[r].XmlText>
                                                        </cfif>
                                                    </cfcase>
                                                    <cfcase value="BIC">
                                                        <cfif Len(Trim(Matrice_CondizioniPagamento[r].XmlText)) gt 0>
                                                        <cfset pagamento_array[pagamento_riga][7] = Matrice_CondizioniPagamento[r].XmlText>
                                                        </cfif>
                                                    </cfcase>
                                                    </cfswitch>                                                  
                                                </cfloop>                                              
                                                </cfif>
                                            </cfcase>
                                            </cfswitch>
                                        </cfloop>
                                        </cfif>
                                    </cfloop>
                                    </cfif>
                                </cfif>
                                <cfif len(trim(CondizioniPagamento)) gt 0>
                                    <cfset new_CondizioniPagamento = trim(CondizioniPagamento)>
                                    <cfset lista_update = listappend(lista_update, " CondizioniPagamento='#CondizioniPagamento#'")>                                
                                </cfif>
                                <cfif len(trim(pagamento_modalita)) gt 0>
                                    <cfset new_ModPagamento = trim(pagamento_modalita)>
                                    <cfset lista_update = listappend(lista_update, " Mod_pagamento='#pagamento_modalita#'")>                                
                                </cfif>
                                <cfif isdate(pagamento_scadenza)>
                                    <cfset new_Scadenza = createODBCDate(pagamento_scadenza)>
                                    <cfset lista_update = listappend(lista_update, " Scadenza=#createODBCDate(pagamento_scadenza)#")>                                
                                </cfif>
                    
                    
                                <cfset Numero_Fattura = new_NumeroFattura>
                                <!---  <cfset Data_Emissione = new_DataFile> --->
                                <cfset Ricezione_Fattura = new_Datacreation>
                                <!--- <cfset Data_Emissione_ = replace(new_InvoiceData, "T", " ", "all")> --->
                                <cfset Data_Emissione = createODBCDate(new_InvoiceData)>
                                <cfset IDSocieta = new_IDCliente>
                                <cfset Valuta = new_Valuta>
                                <cfset Cambio = 0>
                                <cfset Importo = new_Importo>
                                <cfset IVA = new_IVA>
                                <cfset Detrazione_IVA = 100>
                                <cfset Descrizione = new_Descrizione>
                                <cfset Classificazione = ''>
                                <cfset Scadenza = new_Scadenza>
                                <cfset IBAN = ''>
                                <cfset Mod_Pagamento = new_ModPagamento>
                                <cfset Stato = new_StatoFattura>
                                <cfset Pagato = 0>
                                <cfset NDC = new_ndc>
                                <cfset note_ = ''>
                                <cfset elettronica = 1>
                                <cfset Tipo_Documento = new_TipoDocumento>
                                <cfset Condizioni_Pagamento = new_CondizioniPagamento>
                                <cfset id_SDI = new_id_SDI>
                                <cfset nome_file_SDI = new_nome_file_SDI>
                                

                                <cfif IsDefined("IDSocieta") and Len(Trim(IDSocieta)) gt 0>
                                    <cfquery datasource="#active_session_dsn#" name="fattura_esistente">
                                    select IDDocumento from `#active_session_dsn#`.fatture_passivo where IDSocieta=#IDSocieta# and Numero_Fattura='#Numero_Fattura#'
                                    </cfquery>
                                    <cfif IsDefined("fattura_esistente.IDDocumento") and Len(Trim(fattura_esistente.IDDocumento)) gt 0>
                                    <cfoutput>Numero fattura già presente per il cliente... IDDocumento: #fattura_esistente.IDDocumento#<br /></cfoutput>
                                    Rimuovo la vecchia fattura e la ricreo con i dati ricevuti da SDI<br />
                                    <cfquery datasource="#active_session_dsn#" name="cancella_fattura">
                                        delete from `#active_session_dsn#`.fatture_passivo where IDDocumento=#fattura_esistente.IDDocumento#
                                    </cfquery>
                                    </cfif>
                                    <cfquery datasource="#active_session_dsn#" name="nuova_fattura">
                                    select `#active_session_dsn#`.`fn_inserisci_fattura_passiva` (
                                        '#Numero_Fattura#',
                                        #Data_Emissione#,
                                        #IDSocieta#,
                                        '#Valuta#',
                                        #Cambio#,
                                        #Importo#,
                                        #IVA#,
                                        #Detrazione_IVA#,
                                        '#Descrizione#',
                                        '#Classificazione#',
                                        <cfif Len(Trim(Scadenza)) gt 0>#Scadenza#<cfelse>#Data_Emissione#</cfif>,
                                        '#IBAN#',
                                        '#Mod_Pagamento#',
                                        '#Stato#',
                                        #Pagato#,
                                        '#NDC#',
                                        '#note_#',
                                        #elettronica#,
                                        #Ricezione_Fattura#,
                                        '#Tipo_Documento#',
                                        '#Condizioni_Pagamento#',
                                        '#id_SDI#',
                                        '#nome_file_SDI#'
                                    ) as IDFattura
                                    </cfquery>
                                    
                                    Fattura registrata!<br />
                                <cfelse>
                                    Errore: IDSocieta non valorizzato<br />
                                </cfif>
                                <cfset file_path="#application.file_path#/fatture/#active_ID_az_#/in/">
                                    <!--- Scrivo solo il file XML --->
                                    <cftry>
                                    <!--- Scrivo il file XML --->
                                         <cffile action="write" file="#file_path##nome_file_SDI#" output="#file_xml_origin#"> 
                                         <cffile action="delete" file="#application.file_path#/fatture/#active_ID_az_#/import/#name#"> 

                                    <cfoutput>File XML caricato: #file_path##nome_file_SDI#<br /></cfoutput>
                                    <cfcatch>Errore nel caricamento del file XML: <cfoutput>#cfcatch.Message#</cfoutput><br /></cfcatch>
                                    </cftry>
                                <!--- <cfdump var="#new_file#"> --->
                            <cfelse>
                            File ricevuto non in formato XML - 
                            <cfif IsDefined("matrice_documento")>
                                Variabile matrice_documento:<br /><cfdump var="#matrice_documento#"><br />
                            <cfelse>
                                Variabile matrice_documento non definita...<br />
                            </cfif>
                            </cfif>
           
        </cfloop>