2025-06-03 15:11:16 +02:00

688 lines
24 KiB
C#

using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Configuration;
using System.Web;
using LetteraAccompagnamento;
using DataAccessLayer;
using NLog;
using System.Data.SqlClient;
using System.Linq;
public class GestoreLettera
{
public GestoreLettera() { }
//public GestoreLettera(DataAccessDE dataAccess)
//{
// this.dataAccess = dataAccess;
//}
//private DataAccessDE dataAccess;
int idLettera = -1;
string testo = string.Empty;
LetteraBenvenuto w = null;
string indirizzo = string.Empty;
string nome = string.Empty;
string cognome = string.Empty;
//string codiceContratto = string.Empty;
string appellativo = string.Empty;
string oggetto = string.Empty;
string firma = string.Empty;
int codiceLettera = 0;
/**** Fix Direct Bank 28/03/2023 - Pino ***/
public int idModello { get; set; }
/******************************************/
public object objectLock = new object();
public 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;
}
private static NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
public byte[] componiLetteraBenvenuto(string cf, string rete, TipoLettera tipo, TipoContratto tc, TipoReport tr, int idReport, bool isProffesional)
{
leggiDaDB(cf, rete, tipo, tc, tr, idReport, false, false, isProffesional);
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, tc);
break;
case 3:
case 4:
case 202:
case 203:
case 206:
case 207:
//Adriano 20171212: codici per lettere con accorpamento Contestuale / Non contestuale
case 302: // SEI SanPaolo Invest
case 303: // SEI Fideuram
case 306: // SEI Private SanPaolo Invest
case 307: // SEI Private Fideuram
case 310: // SEI IWBank Andrea Modifica
case 311: // SEI Private Andrea Modifica
case 312: // SEI Direct Bank
w = new LetteraAccettazioneAttivazioneSeiPosticipata(rete, idLettera, testo, nome, cognome, indirizzo, appellativo, oggetto, firma, tc, idModello);
break;
case 5:
case 6:
w = new LetteraRistampa(rete, idLettera, testo, nome, cognome, indirizzo, appellativo, oggetto, firma, tc);
break;
}
testout += " " + cf + " " + rete + " " + codiceLettera;
w.writePDF();
return w.Data;
}
public List<byte[]> componiLetteraCommerciale(string cf, string rete, TipoLettera tipo, TipoContratto tc, TipoReport tr, int idReport, bool isProffesional)
{
List<byte[]> pages = new List<byte[]>();
var letterPages = leggiDaDB(cf, rete, tipo, tc, tr, idReport, true, true, isProffesional);
foreach (var w in letterPages)
{
testout += " " + cf + " " + rete + " " + codiceLettera;
w.writePDF();
pages.Add(w.Data);
}
return pages;
}
public string testout = "";
public byte[] componiLetteraBenvenuto(string cf, string rete, TipoLettera tipo, TipoContratto tc, TipoReport tr, bool isProffesional)
{
return componiLetteraBenvenuto(cf, rete, tipo, tc, tr, -1, isProffesional);
}
public List<byte[]> componiLetteraCommerciale(string cf, string rete, TipoLettera tipo, TipoContratto tc, TipoReport tr, bool isProffesional)
{
return componiLetteraCommerciale(cf, rete, tipo, tc, tr, -1, isProffesional);
}
public byte[] componiLetteraAccompagnamento(string cf, string rete, TipoContratto tc, TipoReport tipo, bool isProffesional)
{
leggiDaDB(cf, rete, TipoLettera.ReportPeriodico, tc, tipo, -1, true, false, isProffesional);
while (w == null)
{
if (rete == "S")
w = new LetteraAccompagnamentoPeriodico(rete, idLettera, testo, nome, cognome, indirizzo, appellativo, oggetto, firma, tc);
else
w = new LetteraAccompagnamentoPeriodico(rete, idLettera, testo, nome, cognome, indirizzo, appellativo, oggetto, firma, tc);
}
w.writePDF();
return w.Data;
}
// 20190126 Per lettera errata corrige
public byte[] componiLetteraErrataCorrige(string cf, string rete, TipoContratto tc, TipoReport tipo, bool isProffesional)
{
leggiDaDB(cf, rete, TipoLettera.ReportPeriodico, tc, tipo, -1, true, false, isProffesional, true);
//w = new LetteraErrataCorrige(rete, idLettera, testo, nome, cognome, indirizzo, appellativo, oggetto, firma, tc, true);
w = new LetteraErrataCorrige(rete, idLettera, testo, nome, cognome, indirizzo, appellativo, oggetto, firma, tc);
testout += " " + cf + " " + rete + " " + codiceLettera;
w.writePDF();
return w.Data;
}
private List<LetteraCommerciale> leggiDaDB(string cf, string rete, TipoLettera tipo,
TipoContratto tipoContratto, TipoReport tr, int idReport, bool periodico,
bool isCommercial = false, bool isProfessional = false)
{
string nomestored = "";
List<LetteraCommerciale> results = new List<LetteraCommerciale>();
if (periodico)
{
nomestored = "[C6MartPeriodico].[GESTIONE_SELECT_LETTERE_DA_INVIARE_Rosaspina]";
if (tipoContratto == TipoContratto.Old)
{
if (tr == TipoReport.Diagnosi)
{
if (rete.ToUpper() == "S")
codiceLettera = 109;
else if (rete.ToUpper() == "W")
codiceLettera = 107;
else
codiceLettera = 106;
}
else
{
if (rete.ToUpper() == "S")
codiceLettera = 103;
else if (rete.ToUpper() == "W")
codiceLettera = 112;
else
codiceLettera = 100;
}
}
if (tipoContratto == TipoContratto.Affluent)
{
if (tr == TipoReport.Diagnosi)
{
if (rete.ToUpper() == "S")
codiceLettera = 110;
else if (rete.ToUpper() == "W")
codiceLettera = 107;
else
codiceLettera = 107;
}
else
{
if (rete.ToUpper() == "S")
codiceLettera = 104;
else if (rete.ToUpper() == "W")
codiceLettera = 112;
else
codiceLettera = 101;
}
}
if (tipoContratto == TipoContratto.Private)
{
if (isCommercial)
{
if (rete.ToUpper() == "F")
{
codiceLettera = 301;
}
}
else
{
if (tr == TipoReport.Diagnosi)
{
if (rete.ToUpper() == "S")
codiceLettera = 111;
else
codiceLettera = 118;
}
else
{
if (rete.ToUpper() == "S")
codiceLettera = 105;
else if (rete.ToUpper() == "W")
codiceLettera = 108;
else
codiceLettera = 102;
}
}
}
}
else
{
nomestored = "[C6Mart].[GESTIONE_SELECT_LETTERE_DA_INVIARE_Rosaspina]";
if (tipo == TipoLettera.RistampaLettera)
{
if (rete.ToUpper() == "S")
codiceLettera = 5;
else
codiceLettera = 6;
}
else
{
if (tipoContratto == TipoContratto.Affluent || tipoContratto == TipoContratto.Old)
{
if (rete.ToUpper().Equals("S"))
codiceLettera = 302;
else if (rete.ToUpper() == "W")
codiceLettera = 310;
else
{
if (idModello != 6)
codiceLettera = 303;
else
codiceLettera = 312;
}
}
if (tipoContratto == TipoContratto.Private)
{
if (rete.ToUpper().Equals("S"))
codiceLettera = 306;
else if (rete.ToUpper() == "W")
codiceLettera = 311;
else
{
if (idModello != 6)
codiceLettera = 307;
else
codiceLettera = 312;
}
}
if (tipoContratto == TipoContratto.Dadvisory)
{
codiceLettera = 312;
}
}
}
List<SqlParameter> parameters = new List<SqlParameter>
{
new SqlParameter
{
ParameterName="rete",
Value=rete
},
new SqlParameter
{
ParameterName="cf",
Value=cf
},
new SqlParameter
{
ParameterName="idReport",
Value=idReport
},
new SqlParameter
{
ParameterName="tipoReport",
Value=(tr == TipoReport.Diagnosi) ? "Diagnosi" : "Monitoraggio"
},
new SqlParameter
{
ParameterName="codiceLettera",
Value=codiceLettera
}
};
try
{
DataTable dt = null;
using (SQLServer db = new SQLServer(cf, rete))
{
dt = db.GetDataTableFromProcedure(nomestored, parameters);
}
if (dt != null && dt.Rows.Count > 0)
{
foreach (DataRow lettere in dt.Rows)
{
testo = lettere["testo"].ToString();
if (isProfessional)
{
testo = testo.Replace("investimenti, di collocamento e di distribuzione", "investimenti, di collocamento e di distribuzione per i clienti professionali");
}
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();
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();
int currentPageNumber = (int)lettere["pagina"];
var letterPage = new LetteraCommerciale(rete, idLettera, testo, nome, cognome, indirizzo, appellativo, oggetto, firma, tipoContratto, currentPageNumber, idModello);
results.Add(letterPage);
}
}
if (results.Count == 0)
{
logger.Traces(String.Concat("DataReader vuoto dopo l'esecuzione della stored procedure:", rete, cf));
}
}
catch (Exception ex)
{
logger.Errors(ex);
string errmes = "Errore nel Generatore della Lettera di Benvenuto: " + getException(ex) + " codfiscale " + cf + " rete " + rete + " lettera " + codiceLettera;
ScriviErroreNelDB(-292929, errmes, -292929, "Generatore Lettera Benvenuto", "Generatore Lettera Benvenuto");
//Console.WriteLine(errmes);
//logger.Error(errmes);
}
return results;
}
// 20190126 Gestione Lettera Errata Corrige
private List<LetteraCommerciale> leggiDaDB(string cf, string rete, TipoLettera tipo,
TipoContratto tipoContratto, TipoReport tr, int idReport, bool periodico,
bool isCommercial = false, bool isProfessional = false, bool errataCorrige = true)
{
string sp = "";
List<LetteraCommerciale> results = new List<LetteraCommerciale>();
try
{
codiceLettera = rete.ToUpper().Equals("F") ? 601 : 602;
sp = periodico ? "[C6MartPeriodico].[GESTIONE_SELECT_LETTERE_DA_INVIARE]" : "[C6Mart].[GESTIONE_SELECT_LETTERE_DA_INVIARE_Rosaspina]";
/*
Entrambe le procedure accettano questi parametri
@rete as varchar(1) = '',
@cf as varchar(16) = '',
@codiceLettera as smallint = null,
@idReport as int,
@tipoReport as varchar(20) = ''
*/
List<SqlParameter> parameters = new List<SqlParameter>
{
new SqlParameter
{
ParameterName="codiceLettera",
Value=codiceLettera
}
,new SqlParameter
{
ParameterName="cf",
Value=cf
}
,new SqlParameter
{
ParameterName="rete",
Value=rete
}
,new SqlParameter
{
ParameterName="idReport",
Value=idReport
}
,new SqlParameter
{
ParameterName="tipoReport",
Value=tr
}
};
DataTable dt = null;
using (SQLServer db = new SQLServer())
{
dt = db.GetDataTableFromProcedure(sp, parameters);
}
if (dt != null && dt.Rows.Count > 0)
{
foreach (DataRow row in dt.Rows)
{
testo = row["testo"].TOString();
indirizzo = row["via"].TOString() + "\r\n";
indirizzo += row["cap"].TOString();
indirizzo += row["localita"].TOString();
indirizzo += " (" + row["provincia"].TOString() + ")";
nome = row["nome"].TOString();
cognome = row["cognome"].TOString();
appellativo = row["appellativo"].TOString();
oggetto = row["oggetto"].TOString();
firma = row["firma"].TOString();
int currentPageNumber = row["pagina"].ToInt32();
var letterPage = new LetteraCommerciale(rete, idLettera, testo, nome, cognome, indirizzo, appellativo, oggetto, firma, tipoContratto, currentPageNumber, 0);
results.Add(letterPage);
}
}
if (results.Count == 0)
{
logger.Errors(string.Format("DataTable vuota dopo l'esecuzione della stored procedure: {0} CF: {1} Rete:{2} ", sp,cf,rete));
}
}
catch (Exception ex)
{
string errmes = "Errore nel Generatore della Lettera di Benvenuto: " + getException(ex) + " codfiscale " + cf + " rete " + rete + " lettera " + codiceLettera;
ScriviErroreNelDB(-292929, errmes, -292929, "Generatore Lettera Benvenuto", "Generatore Lettera Benvenuto");
//Console.WriteLine(errmes);
logger.Errors(ex);
}
return results;
}
//--20190126
//private 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 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 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 void ScriviErroreNelDB(int codiceErrore, string descrizioErrore, int localeId, string nomePackage, string descrizionePackage)
{
if (CACHE.scriviErroreNelDB)
{
//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);
//}
List<SqlParameter> parameters = new List<SqlParameter>
{
new SqlParameter
{
ParameterName = "codiceErrore",
Value=codiceErrore
},
new SqlParameter
{
ParameterName = "descrizioErrore",
Value=descrizioErrore
},
new SqlParameter
{
ParameterName = "dataTime",
Value=DateTime.Now
},
new SqlParameter
{
ParameterName = "localeID",
Value=localeId
},
new SqlParameter
{
ParameterName = "descrizionePackage",
Value=descrizionePackage
},
new SqlParameter
{
ParameterName = "nomePackage",
Value=nomePackage
}
};
if (localeId == 0)
parameters.First(x => x.ParameterName == "localeID").Value = null;
using (SQLServer db = new SQLServer())
{
db.executeNonQueryWT("[C6Mart].[UT_INSERT_ERROR]", parameters);
}
}
}
}