using System; using System.IO; using System.Net; using System.Text; using System.Globalization; using System.Text.RegularExpressions; using System.Data; using System.Drawing; using System.Drawing.Imaging; using System.Xml; using System.Xml.Serialization; using System.ComponentModel; namespace Consulenza.ReportCommon { /// /// Classe di Helper /// public static class Helper { public static string orizzonteTemporale(string valore) { string orzTempMesiString = valore; switch (orzTempMesiString) { case "6": return "sei mesi"; case "1": case "12": return "un anno"; case "3": //aò decidiamoci da fe sono anni, qui sono mesi...ricordarsi di controllarlo case "36": return "tre anni"; case "5": case "60": return "cinque anni"; case "7": case "84": return "sette anni"; case "9": case "108": return "nove anni"; case "10": case "120": return "dieci anni"; default: return "sette anni"; } } /// /// Recupera la larghezza proporzionale in funzione dei parametri passati in input. /// La larghezza restituita è la proporzione matematica (width : value = x : maxvalue) /// /// valore da rappresentare /// Valore massimo /// larghezza /// public static float GetWidthProportional(float value, float maxvalue, float width) { return (width * value) / maxvalue; } public static string FirstCharToUpper(string input) { if (string.IsNullOrEmpty(input)) { return input; } return input.Substring(0, 1).ToUpper() + input.Substring(1, input.Length - 1).ToLower(); } public static int GetNumericStringWidth(string number) { int width = 0; foreach (char s in number) { if (s == '.' || s == ',' || s == 'i') { width += 1; } else if (s == '%') { width += 8; } else { width += 5; } } return width; } /// /// Controlla se il valore passato in input è in formato numerico e ne restituisce la sua conversione in formato valuta con 2 decimali. /// Se il controllo a numerico fallisce, viene restituita la stringa passata in input. /// /// Valore da formattare /// public static string FormatCurrency(string value) { decimal decimalValue; var stringReturn = value; if (Decimal.TryParse(value, NumberStyles.Number, null, out decimalValue)) stringReturn = Convert.ToDecimal(value).ToString("#,##0.00"); return stringReturn; } /// /// Controlla se il valore passato in input è in formato numerico e ne restituisce la sua conversione in formato valuta con 2 decimali ed il simbolo € alla fine. /// Se il controllo a numerico fallisce, viene restituita la stringa passata in input. /// /// Valore da formattare /// public static string FormatCurrencyWithSymbol(string value) { return string.Format("{0} €", FormatCurrency(value)); } /// /// Controlla se il valore passato in input è in formato numerico e ne restituisce la sua conversione in formato valuta con 2 decimali. /// Se il controllo a numerico fallisce, viene restituita la stringa passata in input. /// /// Valore da formattare /// public static string FormatInteger(string value) { decimal decimalValue; string stringReturn = value; if (Decimal.TryParse(value, NumberStyles.Number, null, out decimalValue)) stringReturn = Convert.ToDecimal(value).ToString("#,##0"); return stringReturn; } /// /// Controlla se il valore passato in input è in formato numerico e ne restituisce la sua conversione in formato valuta con tot decimali a seconda del parametro int decimals. /// Se il controllo a numerico fallisce, viene restituita la stringa passata in input. /// /// Valore da formattare /// numero di decimali /// public static string FormatDecimal(string value, int decimals) { decimal decimalValue; var stringReturn = value; var sdecimals = new string('0', decimals); if (!Decimal.TryParse(value, NumberStyles.Number, null, out decimalValue)) return stringReturn; var stringFormat = "{0:0." + String.Format("{0}", sdecimals) + "}"; stringReturn = String.Format(stringFormat, decimalValue); return stringReturn; } /// /// Formatta il valore passato in input in una stringa formato percentuale con un solo decimale. /// /// Valore da formattare /// Numero di decimali da restituire,1 o 2 unici valori ammessi. /// public static string FormatPercentage(decimal value, int decimals) { return (value / 100).ToString(string.Format("P{0}", decimals), new CultureInfo("it-IT")); } /// /// Formatta il valore passato in input in una stringa formato percentuale con un solo decimale. /// /// Valore da formattare /// Numero di decimali da restituire,1 o 2 unici valori ammessi. /// public static string FormatPercentage(double value, int decimals) { decimal returnValue = Convert.ToDecimal(value); string returnString = FormatPercentage(returnValue, decimals); return returnString; } /// /// Formatta il valore passato in input in una stringa formato percentuale con un solo decimale. /// /// Valore da formattare /// Numero di decimali da restituire,1 o 2 unici valori ammessi. /// public static string FormatPercentage(string value, int decimals) { decimal decimalValue; string stringReturn = string.Empty; if (Decimal.TryParse(value, NumberStyles.Number, null, out decimalValue)) stringReturn = FormatPercentage(decimalValue, decimals); return stringReturn; } /// /// Formatta la data passata in input nel fomat DD MMMM YYYY dove MMMM è il nome per esteso del mese. /// /// Valore da formattare /// public static string FormatDateMonthName(DateTime date) { var strDay = date.Day.ToString(); var strMonth = date.ToString("MMMM"); var strYear = date.Year.ToString(); var returnValue = strDay + " " + strMonth + " " + strYear; return returnValue; } public static string resultValueVariousString(string value) { decimal decimalValue; string stringReturn = string.Empty; if (Decimal.TryParse(value, NumberStyles.Number, null, out decimalValue)) stringReturn = decimalValue.ToString(); else { if (value.ToLower().Contains("n.c.") || value.ToLower().Contains("n.a.") || value.ToLower().Contains("n.d.")) stringReturn = value.ToLower(); else stringReturn = CapitalizeWords(value); } return stringReturn; } /// /// Formatta il valore passato in input in una stringa con la prima lettera maiuscolo. /// Se il valore in input è nullo o vuoto viene restituita una stringa vuota. /// /// Valore da formattare /// public static string CapitalizeWords(string value) { if (string.IsNullOrEmpty(value)) return string.Empty; value = value.ToLower(); var result = new StringBuilder(value); result[0] = char.ToUpper(result[0]); for (int i = 1; i < result.Length; ++i) { if (char.IsWhiteSpace(result[i - 1]) || result[i - 1].ToString() == "'") result[i] = char.ToUpper(result[i]); } return result.ToString(); } /// /// Sostituisce le variabili presenti nel testo (indicate con $/...../$ con il valore corrispondente /// /// testo da rimpiazzare /// ReportEnvironment /// public static string ReplaceVariables(string value, ReportEnvironment reportenvironment) { if (reportenvironment.PrivateBanker.CodiceRete == "F") value = value.Replace("$/Banca/$", "Fideuram"); if (reportenvironment.PrivateBanker.CodiceRete == "S") value = value.Replace("$/Banca/$", "Sanpaolo Invest"); return value; } /// /// Rimuove i tag html dal testo passato in input. /// /// /// public static string Strip_Tags(string value) { return Regex.Replace(value, @"<[^>]*>", String.Empty); } /// /// Ritorna il datatable passto in input con ordine righe invertito. /// /// /// public static DataTable ReverseDataTable(DataTable inputTable) { var outputTable = inputTable.Clone(); for (int i = inputTable.Rows.Count - 1; i >= 0; i--) { outputTable.ImportRow(inputTable.Rows[i]); } return outputTable; } /// /// Funzione per arrotondare in eccesso il valore passato in input /// /// Valore da arrotondare /// Restituisce un double public static double Round(double valore, bool secondMethod = false) { if (!secondMethod) { double tempValue; double results; int pesoArrotondamento; bool neg = false; if (valore == 0) return 0; if (valore < 0) { neg = true; valore = Math.Abs(valore); } int ordineGrandezza = Convert.ToString(Math.Truncate(Math.Abs(valore))).Length; if (ordineGrandezza >= 2) { pesoArrotondamento = Convert.ToInt32(Math.Pow(10.0, Convert.ToDouble(ordineGrandezza - 1))); tempValue = valore / Convert.ToDouble(pesoArrotondamento); } else { pesoArrotondamento = 1; tempValue = valore; } if (tempValue == 1) results = Math.Round(tempValue, MidpointRounding.AwayFromZero) * pesoArrotondamento; else results = Math.Round((tempValue + 0.6) * pesoArrotondamento, MidpointRounding.AwayFromZero); if (neg) return results * (-1); return results; } else { //int negative = valore < 0 ? -1 : 1; //string tmp = Math.Truncate(Math.Abs(valore)).ToString(); //double multiplier = tmp.Length > 2 ? Math.Pow(10, tmp.Length - 2) : Double.Parse(tmp); //string twoFirstDigits = tmp.Length > 2 ? tmp.Substring(0,2) : tmp; //double result = multiplier * (int.Parse(twoFirstDigits) + 1) * negative; //return result; int negative = 1; if (valore < 0) { negative *= -1; valore *= -1; } if (valore < 1) { return 0; } if (valore < 25) { return Math.Truncate((valore + 1) * negative); } if (valore < 70) { double valoreIncreased = valore * 105 / 100; // value * 115% double valoreRest = valoreIncreased % 5; double result = valoreIncreased - valoreRest; if (result < valore) { result += 5; } return Math.Truncate(result * negative); } if (valore <= 100) { double valoreIncreased = valore * 110 / 100; // value * 110% double valoreRest = valoreIncreased % 10; double result = valoreIncreased - valoreRest; if (result < valore) { result += 10; } return Math.Truncate(result * negative); } else // valore > 100 { string tmp = Math.Truncate(Math.Abs(valore)).ToString(); double multiplier = tmp.Length > 2 ? Math.Pow(10, tmp.Length - 2) : Double.Parse(tmp); string twoFirstDigits = tmp.Length > 2 ? tmp.Substring(0, 2) : tmp; double result = multiplier * (int.Parse(twoFirstDigits) + 1) * negative; return result; } } } /// /// Arrotonda in eccesso l'intero passato in input /// /// /// public static int RoundUp(int toRound) { return (10 - toRound % 10) + toRound; } /// /// Arrotonda in difetto l'intero passato in input /// /// /// public static int RoundDown(int toRound) { return toRound - toRound % 10; } /// /// Classe che centralizza e gestisce tutte le Decodifiche. /// public static class Decodifiche { /// /// Decodifica il valore stimato pro quota dell'immobile. /// -100 = "usufrutto". /// -0 = "-". /// 88 = piu' diritti /// /// /// public static string DecodificaValoreStimatoProQuota(decimal valore) { if (valore == 0) return "-"; return valore == -100 ? "usufrutto" : FormatCurrency(valore.ToString()); } public static string DecodificaQuotaProprieta(decimal valore) { if (valore == 0) return "-"; return valore == -100 ? "usufrutto" : FormatPercentage(valore, 2); } } /// /// Converte l'immagine in input in Stream /// /// /// /// public static Stream ToStream(this Image image, ImageFormat format) { var stream = new MemoryStream(); image.Save(stream, format); stream.Position = 0; return stream; } /// /// Recupera un' immagine a partire dall'URL passata in input /// /// /// public static Image GetImage(string url) { var request = (HttpWebRequest)WebRequest.Create(url); request.Timeout = 300000; var response = (HttpWebResponse)request.GetResponse(); return Image.FromStream(response.GetResponseStream()); } /// /// Serializza un oggetto in stringa formato XML /// /// /// /// public static string SerializeXml(T obj) { var xsSubmit = new XmlSerializer(typeof(T)); var sww = new StringWriter(); var writer = XmlWriter.Create(sww); xsSubmit.Serialize(writer, obj); return sww.ToString(); } /// /// Ritorna il Type della classe a partire da typeName passato. /// /// /// public static Type GetType(string typeName) { var type = Type.GetType(typeName); if (type != null) return type; foreach (var a in AppDomain.CurrentDomain.GetAssemblies()) { type = a.GetType(typeName); if (type != null) return type; } return null; } } /// /// Classe di utility per gli Enum. /// /// public static class HelperEnum { /// /// Ritorna la Description aggiunta all'Enum come tag DescriptionAttribute. /// /// /// public static string GetDescription(T enumValue) { var fi = enumValue.GetType().GetField(enumValue.ToString()); if (null == fi) return string.Empty; object[] attrs = fi.GetCustomAttributes(typeof(DescriptionAttribute), true); return attrs.Length > 0 ? ((DescriptionAttribute)attrs[0]).Description : string.Empty; } } }