537 lines
22 KiB
C#

using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Configuration;
using System.Web;
using Consulenza.PDFLettera.LetteraAccompagnamento;
namespace Consulenza.PDFLettera
{
public class GestoreLettera {
static int idLettera = -1;
static string testo = string.Empty;
static LetteraBenvenuto w = null;
static string indirizzo = string.Empty;
static string nome = string.Empty;
static string cognome = string.Empty;
//string codiceContratto = string.Empty;
static string appellativo = string.Empty;
static string oggetto = string.Empty;
static string firma = string.Empty;
static int codiceLettera = 0;
public static void RestGestoreLettera() {
idLettera = -1;
testo = string.Empty;
w = null;
indirizzo = string.Empty;
nome = string.Empty;
cognome = string.Empty;
//string codiceContratto = string.Empty;
appellativo = string.Empty;
oggetto = string.Empty;
firma = string.Empty;
codiceLettera = 0;
}
//public static byte[] componiLetteraBenvenuto(string cf, string rete, TipoLettera tipo,TipoContratto tc, TipoReport tr, int idReport)
//{
// leggiDaDB(cf, rete, tipo, tc, tr, idReport,false);
// switch (codiceLettera)
// {
// case 1: case 2: case 200: case 201: case 204: case 205:
// w = new LetteraAccettazioneConAttivazioneSei(rete, idLettera, testo, nome, cognome, indirizzo, appellativo, oggetto, firma);
// break;
// case 3: case 4: case 202: case 203: case 206: case 207:
// w = new LetteraAccettazioneAttivazioneSeiPosticipata(rete, idLettera, testo, nome, cognome, indirizzo, appellativo, oggetto, firma);
// break;
// case 5: case 6:
// w = new LetteraRistampa(rete, idLettera, testo, nome, cognome, indirizzo, appellativo, oggetto, firma);
// break;
// }
// testout += " " + cf + " " + rete + " " + codiceLettera;
// w.writePDF();
// return w.Data;
//}
public static string testout = "";
//public static byte[] componiLetteraBenvenuto(string cf, string rete, TipoLettera tipo,TipoContratto tc, TipoReport tr) {
// return componiLetteraBenvenuto(cf, rete, tipo,tc, tr, -1);
//}
public static byte[] componiLetteraAccompagnamento (string cf, string rete)
{
int nTipoContratto = readTipoContratto(cf, rete);
readInfoLettera(cf, rete, nTipoContratto);
//if (rete == "S")
// w = new LetteraAccompagnamentoPeriodico(rete,idLettera,testo,nome,cognome,indirizzo,appellativo,oggetto,firma);
// else
w = new LetteraAccompagnamentoPeriodico(rete,idLettera,testo,nome,cognome,indirizzo,appellativo,oggetto,firma);
w.writePDF();
return w.Data;
}
//public static byte[] componiLetteraRistampa(string cf, string rete, TipoReport tr, int idReport, bool periodico)
//{
// leggiDaDB(cf, rete, TipoLettera.RistampaLettera, tr, idReport, periodico);
// w = new LetteraRistampa(rete, idLettera, testo, nome, cognome, indirizzo, appellativo, oggetto, firma);
// w.writePDF();
// return w.Data;
//}
private static int readTipoContratto(string cf, string rete)
{
int nRet = 0;
DataTable table1 = new DataTable();
try
{
string sSql = "SELECT TIP_CONTRATTO FROM C6Mart.VCONTRATTI ";
sSql = sSql + "WHERE COD_FISCALE ='" + cf + "' ";
sSql = sSql + "AND rete='" + rete + "'";
table1 = DataAccess.ExecuteDataTableSqlStatement(DBProvider.SqlServerStampeC6, sSql, null);
if (table1.Rows.Count == 0)
{
if (rete == "F")
nRet = 500;
else
nRet = 503;
}
else
{
switch (table1.Rows[0][0].ToString())
{
case "0":
if (rete == "F")
nRet = 500;
else
nRet = 503;
break;
case "1":
if (rete == "F")
nRet = 501;
else
nRet = 504;
break;
case "2":
if (rete == "F")
nRet = 502;
else
nRet = 505;
break;
}
}
return nRet;
}
catch (Exception ex)
{
string errmes = "Errore nel recupero Tipo Contratto da vContratti: " + getException(ex) + " codfiscale " + cf + " rete " + rete;
Console.WriteLine(errmes);
throw ex;
}
}
private static void readInfoLettera(string cf, string rete, int lettera)
{
codiceLettera = lettera;
string nomestored = "[C6MartPeriodicoImmobiliare].[GESTIONE_SELECT_LETTERE_DA_INVIARE]";
try
{
List<Parametro> parametri = new List<Parametro>();
Parametro p = new Parametro();
p.DbType = DbType.AnsiString;
p.Value = rete;
p.ParameterName = "rete";
parametri.Add(p);
p = new Parametro();
p.DbType = DbType.AnsiString;
p.Value = cf;
p.ParameterName = "cf";
parametri.Add(p);
p = new Parametro();
p.DbType = DbType.Int16;
p.Value = codiceLettera;
p.ParameterName = "codiceLettera";
parametri.Add(p);
p = new Parametro();
p.DbType = DbType.AnsiString;
p.Value = -1;
p.ParameterName = "idReport";
parametri.Add(p);
p = new Parametro();
p.DbType = DbType.AnsiString;
p.Value = 'M';
p.ParameterName = "tipoReport";
parametri.Add(p);
IDataReader lettere = DataAccess.ExecuteDataReaderStoredProcedure(DBProvider.SqlServerStampeC6, nomestored, parametri);
if (lettere.ToString() == "" && lettere == null )
{
testo = "";
indirizzo = "";
nome = "";
cognome = "";
appellativo = "";
oggetto = "";
firma = "";
}
else
{
if (lettere.Read())
{
testo = lettere["testo"].ToString();
indirizzo = lettere["via"].ToString() + "\r\n";
if (!string.IsNullOrEmpty( lettere["cap"].ToString()))
indirizzo += lettere["cap"].ToString() + " " ;
else
indirizzo += lettere["cap"].ToString();
indirizzo += lettere["localita"].ToString();
// indirizzo = lettere["via"].ToString() + "\r\n" +
//lettere["cap"].ToString() + " " +
// lettere["localita"].ToString();
if (!string.IsNullOrEmpty(lettere["provincia"].ToString()))
indirizzo += " (" + lettere["provincia"].ToString() + ")";
nome = lettere["nome"].ToString();
cognome = lettere["cognome"].ToString();
appellativo = lettere["appellativo"].ToString();
oggetto = lettere["oggetto"].ToString();
firma = lettere["firma"].ToString();
//V
if (!lettere.IsClosed)
{
lettere.Close();
lettere.Dispose();
}
}
else
//testo = "";
//indirizzo = rete + " - " + codiceLettera + " - " + cf;
//nome = "";
//cognome = "";
//appellativo = "";
//oggetto = "";
//firma = "";
throw new Exception("DataReader vuoto dopo l'esecuzione della stored procedure");
}
}
catch (Exception ex) {
string errmes = "Errore nel Generatore della Lettera di Benvenuto: " + getException(ex) + " codfiscale " + cf + " rete " + rete + " lettera " + codiceLettera;
Console.WriteLine(errmes);
throw ex;
//testo = "";
//indirizzo = rete + " - " + codiceLettera + " - " + cf;
//nome = "";
//cognome = "";
//appellativo = "";
//oggetto = "";
//firma = "";
}
}
/*
private static void leggiDaDB(string cf, string rete, TipoLettera tipo, TipoReport tr, int idReport, bool periodico)
{
List<Parametro> parametri = new List<Parametro>();
Parametro p = new Parametro();
p.DbType = DbType.AnsiString;
p.Value = rete;
p.ParameterName = "rete";
parametri.Add(p);
p = new Parametro();
p.DbType = DbType.AnsiString;
p.Value = cf;
p.ParameterName = "cf";
parametri.Add(p);
p = new Parametro();
p.DbType = DbType.AnsiString;
p.Value = idReport;
p.ParameterName = "idReport";
parametri.Add(p);
if (tipo == TipoLettera.LetteraAccettazioneConAttivazioneSei)
if (rete.ToUpper() == "S")
codiceLettera = 1;
else
codiceLettera = 2;
if (tipo == TipoLettera.LetteraAccettazioneAttivazioneSeiPosticipata)
if (rete.ToUpper() == "S")
codiceLettera = 3;
else
codiceLettera = 4;
if (tipo == TipoLettera.RistampaLettera)
if (rete.ToUpper() == "S")
codiceLettera = 5;
else
codiceLettera = 6;
p = new Parametro();
p.DbType = DbType.AnsiString;
p.ParameterName = "tipoReport";
parametri.Add(p);
p.Value = string.Empty;
if (periodico)
{
if (tr == TipoReport.Diagnosi)
p.Value = "Diagnosi";
else
p.Value = "Monitoraggio";
if (rete.ToUpper() == "S")
codiceLettera = 7;
else
codiceLettera = 8;
}
else
p.Value = "Diagnosi";
p = new Parametro();
p.DbType = DbType.Int16;
p.Value = codiceLettera;
p.ParameterName = "codiceLettera";
parametri.Add(p);
//START
try
{
IDataReader lettere;
if (periodico)
lettere = DataAccess.ExecuteDataReaderStoredProcedure(DBProvider.SqlServerStampeC6, "[C6MartPeriodico].[GESTIONE_SELECT_LETTERE_DA_INVIARE]", parametri);
else
lettere = DataAccess.ExecuteDataReaderStoredProcedure(DBProvider.SqlServerStampeC6, "[C6Mart].[GESTIONE_SELECT_LETTERE_DA_INVIARE]", parametri);
if (lettere.Read())
{
//idLettera = Convert.ToInt32(lettere["id"]);
testo = lettere["testo"].ToString();
indirizzo = lettere["via"].ToString() + "\r\n" +
lettere["cap"].ToString() + " " +
lettere["localita"].ToString();
if (!string.IsNullOrEmpty(lettere["provincia"].ToString()))
indirizzo += " (" + lettere["provincia"].ToString() + ")";
nome = lettere["nome"].ToString();
cognome = lettere["cognome"].ToString();
appellativo = lettere["appellativo"].ToString();
oggetto = lettere["oggetto"].ToString();
firma = lettere["firma"].ToString();
//V
if (!lettere.IsClosed)
{
lettere.Close();
lettere.Dispose();
}
//
}
else
throw new Exception("DataReader vuoto dopo l'esecuzione della stored procedure: [C6Mart].[GESTIONE_SELECT_LETTERE_DA_INVIARE]");
}
catch (DataBaseException ex)
{
//StartTng(ambiente, cawTo_Bin, argomentiCawToErrore);
Console.WriteLine(ex.Message);
if (HttpContext.Current == null)
ScriviErroreNelDB(-292929, "Errore nel Generatore della Lettera di Benvenuto: " + getDataBaseException(ex), -292929, "Generatore Lettera Benvenuto", "Generatore Lettera Benvenuto");
else
throw ex;
//return null;
}
catch (Exception ex)
{
//StartTng(ambiente, cawTo_Bin, argomentiCawToErrore);
Console.WriteLine(ex.Message);
if (HttpContext.Current == null)
ScriviErroreNelDB(-292929, "Errore nel Generatore della Lettera di Benvenuto: " + getException(ex), -292929, "Generatore Lettera Benvenuto", "Generatore Lettera Benvenuto");
else
throw ex;
}
}
*/
private static string getDataBaseException(DataBaseException ecc) {
const string aCapo = "#/n#";
string ritorno = "";
ritorno += aCapo;
ritorno += " Informazioni Comando SQL:" + aCapo;
ritorno += " sql file name: " + ecc.SqlFileName + aCapo;
ritorno += " connection string: " + ecc.ConnectionStringWithoutCredentials + aCapo;
//ritorno += " command text: " + ecc.CommandText + aCapo;
//controllo se ci sono i parametri
if (ecc.Parameters != null) {
foreach (Parametro parametro in ecc.Parameters) {
ritorno += aCapo;
ritorno += " parametro: " + parametro.ParameterName + aCapo;
ritorno += " dimensione: " + parametro.Size.ToString() + aCapo;
ritorno += " direzione: " + parametro.Direction.ToString() + aCapo;
ritorno += " tipo: " + parametro.DbType.ToString() + aCapo;
if (parametro.Value == null)
ritorno += " valore: null" + aCapo;
else {
string valoreParametro = "";
if (parametro.Size > 500) {
valoreParametro = " valore: null(sono visualizzati solo i primi 500 bytes): " + parametro.Value.ToString().Substring(0, 500);
}
else {
valoreParametro = " valore: " + parametro.Value.ToString();
}
ritorno += valoreParametro + aCapo;
}
}
}
ritorno += aCapo;
ritorno += " tipo: DataBaseException" + aCapo;
ritorno += " descrizione: " + ecc.Eccezione.Message + aCapo;
ritorno += " sorgente: " + ecc.Eccezione.Source + aCapo;
ritorno += " traccia: " + FormattaErrore(ecc.StackTrace) + aCapo;
return ritorno;
}
private static string getException(Exception ecc) {
const string aCapo = "#/n#";
string ritorno = "";
ritorno += aCapo;
ritorno += " tipo: " + ecc.GetType().ToString() + aCapo;
ritorno += " descrizione: " + ecc.Message + aCapo;
ritorno += " sorgente: " + ecc.Source + aCapo;
if (ecc.InnerException != null) {
ritorno += " descrizione Inner: " + ecc.InnerException.Message + aCapo;
ritorno += " sorgente Inner: " + ecc.InnerException.Source + aCapo;
}
ritorno += " traccia: " + FormattaErrore(ecc.StackTrace) + aCapo;
return ritorno;
}
private static string FormattaErrore(string errore) {
const string aCapo = "#/n#";
const string spazio = " ";
string separatore1 = " at ";
string separatore2 = " in ";
string separatore3 = ":line ";
string erroreInterno = errore.Replace(separatore1, "@");
string ritorno = aCapo;
//nome metodo e tutto il resto
string[] messaggioErrore = erroreInterno.Split('@');
string messaggioSenzaSpazi;
string[] messaggioInterno;
foreach (string messaggio in messaggioErrore) {
messaggioSenzaSpazi = messaggio.Trim();
if (messaggioSenzaSpazi != "") {
messaggioSenzaSpazi = messaggioSenzaSpazi.Replace(separatore2, "@");
messaggioInterno = messaggioSenzaSpazi.Split('@');
if (messaggioInterno.GetUpperBound(0) == 1) {
ritorno += spazio + "metodo: " + messaggioInterno[0] + aCapo;
string fileLineaSingolo = messaggioInterno[1].Replace(separatore3, "@");
string[] fileLinea = fileLineaSingolo.Split('@');
ritorno += spazio + "percorsoFile: " + fileLinea[0] + aCapo;
ritorno += spazio + "linea: " + fileLinea[1] + aCapo;
ritorno += aCapo;
}
else {
ritorno += spazio + "metodo: " + messaggioInterno[0] + aCapo;
}
}
}
return ritorno;
}
private static void ScriviErroreNelDB(int codiceErrore, string descrizioErrore, int localeId, string nomePackage, string descrizionePackage) {
int scriviErroreNelDB = int.Parse(ConfigurationManager.AppSettings["scriviErroreNelDB"]);
if (scriviErroreNelDB == 1) {
try {
List<Parametro> parametri = new List<Parametro>();
Parametro parametro = new Parametro();
parametro.DbType = DbType.Int32;
parametro.ParameterName = "codiceErrore";
parametro.Value = codiceErrore;
parametri.Add(parametro);
parametro = new Parametro();
parametro.DbType = DbType.String;
parametro.ParameterName = "descrizioErrore";
parametro.Value = descrizioErrore;
parametri.Add(parametro);
parametro = new Parametro();
parametro.DbType = DbType.DateTime;
parametro.ParameterName = "dataTime";
parametro.Value = DateTime.Now;
parametri.Add(parametro);
parametro = new Parametro();
parametro.DbType = DbType.String;
parametro.ParameterName = "localeID";
if (localeId == 0)
parametro.Value = System.DBNull.Value;
else
parametro.Value = localeId;
parametri.Add(parametro);
parametro = new Parametro();
parametro.DbType = DbType.String;
parametro.ParameterName = "descrizionePackage";
parametro.Value = descrizionePackage;
parametri.Add(parametro);
parametro = new Parametro();
parametro.DbType = DbType.String;
parametro.ParameterName = "nomePackage";
parametro.Value = nomePackage;
parametri.Add(parametro);
DataAccess.ExecuteNonQueryStoredProcedure(DBProvider.SqlServerStampeC6, "[C6Mart].[UT_INSERT_ERROR]", parametri);
}
catch (Exception ex) {
DataBaseException dataBaseException = (DataBaseException)ex;
Console.WriteLine(dataBaseException.Message);
}
}
}
}
}