562 lines
17 KiB
C#
562 lines
17 KiB
C#
using Newtonsoft.Json;
|
|
using RestSharp;
|
|
using SteamWare;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
|
|
namespace SHERPA_SYNC
|
|
{
|
|
/// <summary>
|
|
/// Classe gestione integrazioen con Fatture In Cloud
|
|
/// </summary>
|
|
public class FattInCloud
|
|
{
|
|
/// <summary>
|
|
/// URL base interfaccia FIC
|
|
/// </summary>
|
|
protected string baseUrl;
|
|
/// <summary>
|
|
/// Api utente
|
|
/// </summary>
|
|
protected string apiUID;
|
|
/// <summary>
|
|
/// Key utente
|
|
/// </summary>
|
|
protected string apiKey;
|
|
/// <summary>
|
|
/// Init classe
|
|
/// </summary>
|
|
protected FattInCloud()
|
|
{
|
|
baseUrl = memLayer.ML.cdv("FIC_BASE_URL");
|
|
apiUID = memLayer.ML.cdv("FIC_API_UID");
|
|
apiKey = memLayer.ML.cdv("FIC_API_Key");
|
|
}
|
|
/// <summary>
|
|
/// Singleton gestione integrazione
|
|
/// </summary>
|
|
public static FattInCloud man = new FattInCloud();
|
|
|
|
public string listaDoc
|
|
{
|
|
get
|
|
{
|
|
string answ = "";
|
|
|
|
return answ;
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// Effettua upload del doc richiesto
|
|
/// </summary>
|
|
/// <param name="idxDoc"></param>
|
|
/// <returns></returns>
|
|
public bool uploadDoc(int idxDoc)
|
|
{
|
|
bool fatto = false;
|
|
bool trovata = false;
|
|
// in primis CONTROLLO il tipo di documento che sia FATTURA...
|
|
DS_Billing.DocumentiDataTable tabDoc = BillingProxy.man.taDocs.getByKey(idxDoc);
|
|
if (tabDoc.Rows.Count > 0)
|
|
{
|
|
trovata = true;
|
|
}
|
|
if (trovata)
|
|
{
|
|
DS_Billing.DocumentiRow rigaDoc = tabDoc[0];
|
|
// cerco il cliente...
|
|
var rigaCli = BillingProxy.man.taClienti.getByKey(rigaDoc.idxCli)[0];
|
|
string codCliFIC = checkCodCli(rigaCli.P_iva, rigaCli.C_Fisc);
|
|
if (codCliFIC == "")
|
|
{
|
|
// creo NUOVO RECORD
|
|
codCliFIC = createCli(rigaDoc.idxCli);
|
|
}
|
|
// carico documento...
|
|
if (codCliFIC != "")
|
|
{
|
|
// controllo se ha valore token... se esiste modifico
|
|
if (tabDoc[0].id_ext != "")
|
|
{
|
|
updateDoc(idxDoc, codCliFIC);
|
|
}
|
|
// altrimenti CREO
|
|
else
|
|
{
|
|
uploadNewDoc(idxDoc, codCliFIC);
|
|
}
|
|
}
|
|
}
|
|
|
|
return fatto;
|
|
}
|
|
/// <summary>
|
|
/// Effettua modifica di un documento esistente
|
|
/// </summary>
|
|
/// <param name="idxDoc"></param>
|
|
/// <param name="idCliente"></param>
|
|
private string updateDoc(int idxDoc, String idCliente)
|
|
{
|
|
string answ = "";
|
|
// cerco il documento...
|
|
var rDocumento = BillingProxy.man.taDocs.getByKey(idxDoc)[0];
|
|
// recupero tutte le righe (=articoli)
|
|
var tabArticoli = BillingProxy.man.taRows.getByIdxDoc(idxDoc);
|
|
|
|
// init classe REST call
|
|
var client = new RestClient(baseUrl);
|
|
string tipo = "";
|
|
switch (rDocumento.tipo.ToUpper())
|
|
{
|
|
case "FATTURE":
|
|
case "FATTURA":
|
|
tipo = "fatture";
|
|
break;
|
|
case "DDT/BOLLA DI CONSEGNA":
|
|
tipo = "ddt";
|
|
break;
|
|
case "NOTA DI CREDITO":
|
|
tipo = "ndc";
|
|
break;
|
|
default:
|
|
tipo = "proforma";
|
|
break;
|
|
}
|
|
var request = new RestRequest(string.Format("{0}/modifica", tipo), Method.POST);
|
|
// add parametri json
|
|
request.RequestFormat = DataFormat.Json;
|
|
// compongo righe doc
|
|
List<DocNuovoArticolo> elArticoli = new List<DocNuovoArticolo>();
|
|
DocNuovoArticolo currArt = new DocNuovoArticolo();
|
|
int idx = 0;
|
|
foreach (var item in tabArticoli)
|
|
{
|
|
currArt = new DocNuovoArticolo
|
|
{
|
|
um = item.um,
|
|
quantita = Math.Abs(item.qta),
|
|
descrizione = item.Descrizione,
|
|
prezzo_netto = (double)item.prezzoUm,
|
|
cod_iva = 0, // 0 = 22% standard, vedere note in DataStruct
|
|
tassabile = true,
|
|
sconto = item.sconto * 100, // sconto è in percentuale CON virgola
|
|
sconto_rosso = 1,
|
|
ordine = idx
|
|
};
|
|
elArticoli.Add(currArt);
|
|
idx++;
|
|
}
|
|
// imposto un UNICA scadenza standard a 30gg DFFM
|
|
DateTime scadenza = rDocumento.emesso.AddMonths(2).AddDays(-DateTime.Today.Day);
|
|
List<DocNuovoPagamento> elPagam = new List<DocNuovoPagamento>();
|
|
DocNuovoPagamento singlePagam = new DocNuovoPagamento
|
|
{
|
|
data_scadenza = scadenza.ToString("dd/MM/yyyy"),
|
|
importo = "auto",
|
|
metodo = "not"
|
|
};
|
|
// verifico saldo
|
|
DateTime saldo;
|
|
// se è stato pagato registro saldo...
|
|
if (rDocumento.totPagato > 0 && rDocumento.totScadenze == 0)
|
|
{
|
|
try
|
|
{
|
|
saldo = rDocumento.dataPagam;
|
|
}
|
|
catch
|
|
{
|
|
saldo = scadenza;
|
|
}
|
|
singlePagam.data_saldo = saldo.ToString("dd/MM/yyyy");
|
|
singlePagam.metodo = "UBI 71905"; // inserisco il conto come saldo su quello...
|
|
}
|
|
// registro pagamento...
|
|
elPagam.Add(singlePagam);
|
|
|
|
// compongo doc...
|
|
DocModificaRequest req = new DocModificaRequest
|
|
{
|
|
api_uid = apiUID,
|
|
api_key = apiKey,
|
|
token = rDocumento.id_ext,
|
|
id_cliente = idCliente,
|
|
autocompila_anagrafica = true,
|
|
salva_anagrafica = false,
|
|
numero = string.Format("{0}", rDocumento.num),
|
|
data = rDocumento.emesso.ToString("dd/MM/yyyy"),
|
|
prezzi_ivati = false,
|
|
oggetto_visibile = rDocumento.tipo.ToUpper(),
|
|
oggetto_interno = rDocumento.Descrizione
|
|
};
|
|
// includo righe SE modificato righe...
|
|
if ((rDocumento.changed & 2) != 0)
|
|
{
|
|
req.lista_articoli = elArticoli;
|
|
}
|
|
// includo righe SE modificato righe...
|
|
if ((rDocumento.changed & 4) != 0)
|
|
{
|
|
req.lista_pagamenti = elPagam;
|
|
}
|
|
|
|
string jsonPar = SimpleJson.SerializeObject(req);
|
|
request.AddParameter("application/json", jsonPar, ParameterType.RequestBody);
|
|
|
|
// esecuzione richiesta
|
|
IRestResponse response = client.Execute(request);
|
|
|
|
// controllo risultato
|
|
var risultato = JsonConvert.DeserializeObject<DocNuovoResponse>(response.Content);
|
|
if (risultato.success)
|
|
{
|
|
// salvo
|
|
BillingProxy.man.taDocs.updateCodExt(idxDoc, risultato.token, risultato.new_id);
|
|
answ = risultato.token;
|
|
}
|
|
else
|
|
{
|
|
logger.lg.scriviLog(string.Format("idxDoc: {0} | {1}", idxDoc, response.Content), tipoLog.ERROR);
|
|
answ = response.Content;
|
|
}
|
|
return answ;
|
|
}
|
|
/// <summary>
|
|
/// Effettua upload nuovo documento
|
|
/// </summary>
|
|
/// <param name="idxDoc"></param>
|
|
/// <param name="idCliente"></param>
|
|
private string uploadNewDoc(int idxDoc, String idCliente)
|
|
{
|
|
string answ = "";
|
|
|
|
// cerco il documento...
|
|
var rDocumento = BillingProxy.man.taDocs.getByKey(idxDoc)[0];
|
|
// recupero tutte le righe (=articoli)
|
|
var tabArticoli = BillingProxy.man.taRows.getByIdxDoc(idxDoc);
|
|
|
|
// init classe REST call
|
|
var client = new RestClient(baseUrl);
|
|
string tipo = "";
|
|
switch (rDocumento.tipo.ToUpper())
|
|
{
|
|
case "FATTURE":
|
|
case "FATTURA":
|
|
tipo = "fatture";
|
|
break;
|
|
case "DDT/BOLLA DI CONSEGNA":
|
|
tipo = "ddt";
|
|
break;
|
|
case "NOTA DI CREDITO":
|
|
tipo = "ndc";
|
|
break;
|
|
case "PREVENTIVO":
|
|
tipo = "preventivi";
|
|
break;
|
|
default:
|
|
tipo = "proforma";
|
|
break;
|
|
}
|
|
var request = new RestRequest(string.Format("{0}/nuovo", tipo), Method.POST);
|
|
// add parametri json
|
|
request.RequestFormat = DataFormat.Json;
|
|
// compongo righe doc
|
|
List<DocNuovoArticolo> elArticoli = new List<DocNuovoArticolo>();
|
|
DocNuovoArticolo currArt = new DocNuovoArticolo();
|
|
int idx = 0;
|
|
foreach (var item in tabArticoli)
|
|
{
|
|
currArt = new DocNuovoArticolo
|
|
{
|
|
um = item.um,
|
|
quantita = Math.Abs(item.qta),
|
|
descrizione = item.Descrizione,
|
|
prezzo_netto = Math.Abs((double)item.prezzoUm),
|
|
cod_iva = 0, // 0 = 22% standard, vedere note in DataStruct
|
|
tassabile = true,
|
|
sconto = item.sconto * 100, // sconto è in percentuale CON virgola
|
|
sconto_rosso = 1,
|
|
ordine = idx
|
|
};
|
|
elArticoli.Add(currArt);
|
|
idx++;
|
|
}
|
|
// imposto un UNICA scadenza standard a 30gg DFFM
|
|
DateTime scadenza = rDocumento.emesso.AddMonths(2).AddDays(-DateTime.Today.Day);
|
|
List<DocNuovoPagamento> elPagam = new List<DocNuovoPagamento>();
|
|
DocNuovoPagamento singlePagam = new DocNuovoPagamento
|
|
{
|
|
data_scadenza = scadenza.ToString("dd/MM/yyyy"),
|
|
importo = "auto",
|
|
metodo = "not"
|
|
};
|
|
// verifico saldo
|
|
DateTime saldo;
|
|
// se è stato pagato registro saldo...
|
|
if (rDocumento.totPagato > 0 && rDocumento.totScadenze == 0)
|
|
{
|
|
try
|
|
{
|
|
saldo = rDocumento.dataPagam;
|
|
}
|
|
catch
|
|
{
|
|
saldo = scadenza;
|
|
}
|
|
singlePagam.data_saldo = saldo.ToString("dd/MM/yyyy");
|
|
singlePagam.metodo = "UBI 71905"; // inserisco il conto come saldo su quello...
|
|
}
|
|
// registro pagamento...
|
|
elPagam.Add(singlePagam);
|
|
|
|
// compongo doc
|
|
DocNuovoRequest req = new DocNuovoRequest
|
|
{
|
|
api_uid = apiUID,
|
|
api_key = apiKey,
|
|
id_cliente = idCliente,
|
|
autocompila_anagrafica = true,
|
|
salva_anagrafica = false,
|
|
numero = string.Format("{0}", rDocumento.num),
|
|
data = rDocumento.emesso.ToString("dd/MM/yyyy"),
|
|
prezzi_ivati = false,
|
|
oggetto_visibile = rDocumento.tipo.ToUpper(),
|
|
oggetto_interno = rDocumento.Descrizione,
|
|
mostra_info_pagamento = true,
|
|
PA = false,
|
|
split_payment = false,
|
|
lista_articoli = elArticoli,
|
|
lista_pagamenti = elPagam
|
|
};
|
|
// se è ddt imposto parametri...
|
|
if (tipo == "ddt")
|
|
{
|
|
req.ddt = true;
|
|
}
|
|
string jsonPar = SimpleJson.SerializeObject(req);
|
|
request.AddParameter("application/json", jsonPar, ParameterType.RequestBody);
|
|
|
|
// esecuzione richiesta
|
|
IRestResponse response = client.Execute(request);
|
|
|
|
// controllo risultato
|
|
var risultato = JsonConvert.DeserializeObject<DocNuovoResponse>(response.Content);
|
|
if (risultato.success)
|
|
{
|
|
// salvo
|
|
BillingProxy.man.taDocs.updateCodExt(idxDoc, risultato.token, risultato.new_id);
|
|
answ = risultato.token;
|
|
}
|
|
else
|
|
{
|
|
logger.lg.scriviLog(response.Content, tipoLog.ERROR);
|
|
answ = response.Content;
|
|
|
|
}
|
|
return answ;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Effettua una chiamata x recuperare il cod cliente di FIC data la sua P.OIva / Cf
|
|
/// </summary>
|
|
/// <param name="pi"></param>
|
|
/// <param name="cfisc"></param>
|
|
/// <returns></returns>
|
|
protected string checkCodCli(string pi, string cfisc)
|
|
{
|
|
string answ = "";
|
|
|
|
// init classe REST call
|
|
var client = new RestClient(baseUrl);
|
|
var request = new RestRequest("clienti/lista", Method.POST);
|
|
// add parametri json
|
|
request.RequestFormat = DataFormat.Json;
|
|
AnagraficaListaRequest req = new AnagraficaListaRequest
|
|
{
|
|
api_uid = apiUID,
|
|
api_key = apiKey,
|
|
cf = cfisc
|
|
};
|
|
string jsonPar = SimpleJson.SerializeObject(req);
|
|
request.AddParameter("application/json", jsonPar, ParameterType.RequestBody);
|
|
|
|
// esecuzione richiesta
|
|
IRestResponse response = client.Execute(request);
|
|
|
|
// controllo risultato
|
|
var risultato = JsonConvert.DeserializeObject<AnagraficaListaResponse>(response.Content);
|
|
if (risultato.success)
|
|
{
|
|
// cerco se ho un cod cliente...
|
|
if (risultato.lista_clienti.Length > 0)
|
|
{
|
|
answ = risultato.lista_clienti[0].id;
|
|
}
|
|
}
|
|
return answ;
|
|
}
|
|
/// <summary>
|
|
/// Crea un nuovo record codice cliente su FIC e restitusice suo ID...
|
|
/// </summary>
|
|
/// <param name="idxCli"></param>
|
|
/// <returns></returns>
|
|
protected string createCli(int idxCli)
|
|
{
|
|
string answ = "";
|
|
|
|
// cerco il cliente...
|
|
var rigaCli = BillingProxy.man.taClienti.getByKey(idxCli)[0];
|
|
|
|
// init classe REST call
|
|
var client = new RestClient(baseUrl);
|
|
var request = new RestRequest("clienti/nuovo", Method.POST);
|
|
// add parametri json
|
|
request.RequestFormat = DataFormat.Json;
|
|
AnagraficaNuovoSingoloRequest req = new AnagraficaNuovoSingoloRequest
|
|
{
|
|
api_uid = apiUID,
|
|
api_key = apiKey,
|
|
extra = idxCli.ToString(),
|
|
nome = rigaCli.RagSoc,
|
|
indirizzo_via = rigaCli.via,
|
|
indirizzo_cap = rigaCli.cap,
|
|
indirizzo_citta = rigaCli.località,
|
|
indirizzo_provincia = rigaCli.provincia??"",
|
|
piva = rigaCli.P_iva,
|
|
cf = rigaCli.C_Fisc
|
|
};
|
|
string jsonPar = SimpleJson.SerializeObject(req);
|
|
request.AddParameter("application/json", jsonPar, ParameterType.RequestBody);
|
|
|
|
// esecuzione richiesta
|
|
IRestResponse response = client.Execute(request);
|
|
|
|
// controllo risultato
|
|
var risultato = JsonConvert.DeserializeObject<AnagraficaNuovoSingoloResponse>(response.Content);
|
|
if (risultato.success)
|
|
{
|
|
answ = risultato.id.ToString();
|
|
}
|
|
return answ;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Effettua upload dei DOCS dell'anno richiesto
|
|
/// </summary>
|
|
/// <param name="anno"></param>
|
|
/// <returns></returns>
|
|
public string uploadDocsModAnno(string tipo, int anno)
|
|
{
|
|
string fatto = "";
|
|
string codCliFIC = "";
|
|
string codDocFIC = "";
|
|
int maxCall = 30;
|
|
int numCall = 0;
|
|
// recupero elenco clienti dell'anno con modifiche SOLO testata
|
|
DS_Billing.DocumentiDataTable elDoc = BillingProxy.man.taDocs.getByAnno(tipo, anno, false, true);
|
|
if (elDoc.Count > 0)
|
|
{
|
|
foreach (DS_Billing.DocumentiRow item in elDoc)
|
|
{
|
|
// recupero codCli...
|
|
var tabCli = BillingProxy.man.taClienti.getByKey(item.idxCli);
|
|
if (tabCli.Rows.Count > 0)
|
|
{
|
|
codCliFIC = tabCli[0].id_ext;
|
|
// AGGIORNO doc esistente
|
|
codDocFIC = updateDoc(item.idxDoc, codCliFIC);
|
|
// controllo eventuale errore
|
|
if (codDocFIC.Contains("error_code") && codDocFIC.Contains("2002"))
|
|
{
|
|
fatto = codDocFIC;
|
|
maxCall = 1;
|
|
}
|
|
else
|
|
{
|
|
fatto = "OK";
|
|
}
|
|
maxCall--;
|
|
numCall++;
|
|
}
|
|
if (maxCall == 0)
|
|
{
|
|
return string.Format("{0} - {1} call", fatto, numCall);
|
|
}
|
|
}
|
|
}
|
|
return string.Format("{0} - {1} call", fatto, numCall);
|
|
}
|
|
/// <summary>
|
|
/// Effettua upload dei DOCS dell'anno richiesto
|
|
/// </summary>
|
|
/// <param name="anno"></param>
|
|
/// <returns></returns>
|
|
public string uploadDocsAnno(string tipo, int anno)
|
|
{
|
|
string fatto = "";
|
|
string codCliFIC = "";
|
|
string codDocFIC = "";
|
|
int maxCall = 30;
|
|
int numCall = 0;
|
|
// recupero elenco clienti dell'anno NON caricati...
|
|
var elDoc = BillingProxy.man.taDocs.getByAnno(tipo, anno, true, false);
|
|
if (elDoc.Count > 0)
|
|
{
|
|
foreach (var item in elDoc)
|
|
{
|
|
// recupero codCli...
|
|
var tabCli = BillingProxy.man.taClienti.getByKey(item.idxCli);
|
|
if (tabCli.Rows.Count > 0)
|
|
{
|
|
codCliFIC = tabCli[0].id_ext;
|
|
// creo NUOVO RECORD
|
|
codDocFIC = uploadNewDoc(item.idxDoc, codCliFIC);
|
|
// controllo eventuale errore
|
|
if (codDocFIC.Contains("error_code") && codDocFIC.Contains("2002"))
|
|
{
|
|
fatto = codDocFIC;
|
|
maxCall = 1;
|
|
}
|
|
else
|
|
{
|
|
fatto = "OK";
|
|
}
|
|
maxCall--;
|
|
numCall++;
|
|
}
|
|
if (maxCall == 0)
|
|
{
|
|
return string.Format("{0} - {1} call", fatto, numCall);
|
|
}
|
|
}
|
|
}
|
|
return string.Format("{0} - {1} call", fatto, numCall);
|
|
}
|
|
/// <summary>
|
|
/// Effettua upload dei CLIENTI dell'anno richiesto
|
|
/// </summary>
|
|
/// <param name="tipo"></param>
|
|
/// <param name="anno"></param>
|
|
/// <returns></returns>
|
|
public bool uploadCliAnno(string tipo, int anno)
|
|
{
|
|
bool fatto = false;
|
|
string codCliFIC = "";
|
|
// recupero elenco clienti dell'anno NON caricati...
|
|
var elCli = BillingProxy.man.taClienti.getByAnno(tipo, anno, true);
|
|
if (elCli.Count > 0)
|
|
{
|
|
foreach (var item in elCli)
|
|
{
|
|
// creo NUOVO RECORD
|
|
codCliFIC = createCli(item.idxCli);
|
|
// aggiorno cliente...
|
|
BillingProxy.man.taClienti.updateCodExt(item.idxCli, codCliFIC);
|
|
fatto = true;
|
|
}
|
|
}
|
|
return fatto;
|
|
}
|
|
}
|
|
}
|