249 lines
8.8 KiB
C#
249 lines
8.8 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using System.Net.Mime;
|
||
using System.Text;
|
||
using Kreta.Framework.Util;
|
||
|
||
namespace Kreta.BusinessLogic.Classes
|
||
{
|
||
/// <summary>
|
||
/// Summary description for StringFormat
|
||
/// </summary>
|
||
public static class SDAFormat
|
||
{
|
||
public const string ShortDate = "yyyy. MM. dd.";
|
||
public const string ShortTime = "HH:mm";
|
||
public const string ShortDateAndTime = ShortDate + " " + ShortTime;
|
||
|
||
public enum FormatType
|
||
{
|
||
[StringValue("{0:F0}")]
|
||
Decimal_0_Places,
|
||
[StringValue("{0:F1}")]
|
||
Decimal_1_Places,
|
||
[StringValue("{0:F2}")]
|
||
Decimal_2_Places,
|
||
[StringValue("{0:F3}")]
|
||
Decimal_3_Places,
|
||
[StringValue("{0:F4}")]
|
||
Decimal_4_Places,
|
||
[StringValue("{0:yyyy. MM. dd.}")]
|
||
ShortDate,
|
||
[StringValue("{0:yyyy. MM. dd. HH:mm}")]
|
||
ShortDateAndTime,
|
||
[StringValue("BankAccount")]
|
||
BankAccount,
|
||
[StringValue("")]
|
||
Empty,
|
||
[StringValue("{0} kB")]
|
||
KiloByte,
|
||
[StringValue("{0:HH:mm}")]
|
||
ShortTime
|
||
}
|
||
public static Dictionary<FormatType, string> Format = new Dictionary<FormatType, string>()
|
||
{
|
||
{FormatType.Decimal_0_Places,"{0:F0}"},
|
||
{FormatType.Decimal_1_Places,"{0:F1}"},
|
||
{FormatType.Decimal_2_Places,"{0:F2}"},
|
||
{FormatType.Decimal_3_Places,"{0:F3}"},
|
||
{FormatType.Decimal_4_Places,"{0:F4}"},
|
||
{FormatType.ShortDate,"{0:yyyy. MM. dd.}"},
|
||
{FormatType.ShortDateAndTime,"{0:yyyy. MM. dd. HH:mm}"},
|
||
{FormatType.BankAccount,"BankAccount"},
|
||
{FormatType.Empty,""},
|
||
{FormatType.KiloByte,"{0} kB"},
|
||
{FormatType.ShortTime,"{0:HH:mm}"}
|
||
};
|
||
|
||
public static string GetInCurrencyFormat(decimal value)
|
||
{
|
||
System.Globalization.CultureInfo.CurrentCulture.NumberFormat.CurrencyDecimalDigits = 0;
|
||
return string.Format("{0:C}", value);
|
||
}
|
||
|
||
/// <summary>
|
||
/// A WEB-en a Culture információval kapcsolatos dolgok a Neptun nyelvbeállításától függenek,
|
||
/// ezért a Currency (pénznem) beállítása, a string.Format({0:C},pénzérték) –en keresztül nem
|
||
/// szerencsés mert a formázáson kívül a pénznemet is kiírja így a hallgatónak nyelvfüggõen
|
||
/// lesz monnyuk 10000 Ft-ja vagy 10000 Eurója. Monnyuk nem rossz üzlet… Ezért inkább használjuk ezt.
|
||
/// </summary>
|
||
public static string SetHUFCurrencyFormat(double value)
|
||
{
|
||
return $"{value:0,0.00} Ft";
|
||
}
|
||
|
||
public static string SetHUFCurrencyFormat(decimal value)
|
||
{
|
||
return $"{value:0,0.00} Ft";
|
||
}
|
||
|
||
/// <summary>
|
||
/// Pénzösszeg formázása
|
||
/// </summary>
|
||
/// <param name="value">Összeg</param>
|
||
/// <param name="currency">Pénznem</param>
|
||
/// <returns></returns>
|
||
public static string SetCurrencyFormat(double value, string currency)
|
||
{
|
||
return string.Format("{0:#,#.##} {1}", value, currency);
|
||
}
|
||
|
||
/// <summary>
|
||
/// A Bankaccount-okat 16 és 24 hosszan tároljuk általában ezekeknek a formázását
|
||
/// oldja meg így pl:11111111-11111111-11111111 de ha nem sikerül átalakítani akkor
|
||
/// magasról tesz rá és visszaadja az eredeti beadott szöveget,
|
||
/// felkészülve arra, ha egy adatbázis konverzió feldobja az ürüléket a felszínre
|
||
/// pl. ha véletlen még egy ETR-es intézmény átáll Neptunra és mi mit tudjuk, hogyan
|
||
/// van tárolva a Bankszámlaszám mert egyébként a kliens felvitelnél ügyel a formátumra
|
||
/// </summary>
|
||
public static string SetBankAccountFormat(string value)
|
||
{
|
||
decimal bankAccount = 0;
|
||
try
|
||
{
|
||
|
||
if (decimal.TryParse(value, out bankAccount))
|
||
bankAccount = Convert.ToDecimal(value);
|
||
else
|
||
return value;
|
||
}
|
||
catch
|
||
{
|
||
//ha nem megy hát nem megy
|
||
return value;
|
||
}
|
||
switch (value.Length)
|
||
{
|
||
case 16:
|
||
return string.Format("{0:########-########}", bankAccount);
|
||
case 24:
|
||
return string.Format("{0:########-########-########}", bankAccount);
|
||
default:
|
||
return value;
|
||
}
|
||
}
|
||
|
||
public static string GetInCurrencyFormat(double value)
|
||
{
|
||
System.Globalization.CultureInfo.CurrentCulture.NumberFormat.CurrencyDecimalDigits = 0;
|
||
return string.Format("{0:C}", value);
|
||
}
|
||
public static string GetShortDateFormat(DateTime value)
|
||
{
|
||
return string.Format(Format[FormatType.ShortDate], value);
|
||
}
|
||
|
||
public static string GetShortTimeFormat(DateTime value)
|
||
{
|
||
return string.Format(Format[FormatType.ShortTime], value);
|
||
}
|
||
|
||
public static string GetShortDateFormat(object value)
|
||
{
|
||
string Result = "";
|
||
try
|
||
{
|
||
Result = string.Format(Format[FormatType.ShortDate], value);
|
||
}
|
||
catch { }
|
||
return Result;
|
||
}
|
||
}
|
||
|
||
public static class ContentDispositionUtil
|
||
{
|
||
private const string HexDigits = "0123456789ABCDEF";
|
||
|
||
private static void AddByteToStringBuilder(byte b, StringBuilder builder)
|
||
{
|
||
builder.Append('%');
|
||
|
||
int i = b;
|
||
AddHexDigitToStringBuilder(i >> 4, builder);
|
||
AddHexDigitToStringBuilder(i % 16, builder);
|
||
}
|
||
|
||
private static void AddHexDigitToStringBuilder(int digit, StringBuilder builder)
|
||
{
|
||
builder.Append(HexDigits[digit]);
|
||
}
|
||
|
||
private static string CreateRfc2231HeaderValue(string filename)
|
||
{
|
||
StringBuilder builder = new StringBuilder("attachment; filename*=UTF-8''");
|
||
|
||
byte[] filenameBytes = Encoding.UTF8.GetBytes(filename);
|
||
foreach (byte b in filenameBytes)
|
||
{
|
||
if (IsByteValidHeaderValueCharacter(b))
|
||
{
|
||
builder.Append((char)b);
|
||
}
|
||
else
|
||
{
|
||
AddByteToStringBuilder(b, builder);
|
||
}
|
||
}
|
||
|
||
return builder.ToString();
|
||
}
|
||
|
||
public static string GetHeaderValue(string fileName)
|
||
{
|
||
// If fileName contains any Unicode characters, encode according
|
||
// to RFC 2231 (with clarifications from RFC 5987)
|
||
foreach (char c in fileName)
|
||
{
|
||
if ((int)c > 127)
|
||
{
|
||
return CreateRfc2231HeaderValue(fileName);
|
||
}
|
||
}
|
||
|
||
// Knowing there are no Unicode characters in this fileName, rely on
|
||
// ContentDisposition.ToString() to encode properly.
|
||
// In .Net 4.0, ContentDisposition.ToString() throws FormatException if
|
||
// the file name contains Unicode characters.
|
||
// In .Net 4.5, ContentDisposition.ToString() no longer throws FormatException
|
||
// if it contains Unicode, and it will not encode Unicode as we require here.
|
||
// The Unicode test above is identical to the 4.0 FormatException test,
|
||
// allowing this helper to give the same results in 4.0 and 4.5.
|
||
ContentDisposition disposition = new ContentDisposition() { FileName = fileName };
|
||
return disposition.ToString();
|
||
}
|
||
|
||
// Application of RFC 2231 Encoding to Hypertext Transfer Protocol (HTTP) Header Fields, sec. 3.2
|
||
// http://greenbytes.de/tech/webdav/draft-reschke-rfc2231-in-http-latest.html
|
||
private static bool IsByteValidHeaderValueCharacter(byte b)
|
||
{
|
||
if ((byte)'0' <= b && b <= (byte)'9')
|
||
{
|
||
return true; // is digit
|
||
}
|
||
if ((byte)'a' <= b && b <= (byte)'z')
|
||
{
|
||
return true; // lowercase letter
|
||
}
|
||
if ((byte)'A' <= b && b <= (byte)'Z')
|
||
{
|
||
return true; // uppercase letter
|
||
}
|
||
|
||
switch (b)
|
||
{
|
||
case (byte)'-':
|
||
case (byte)'.':
|
||
case (byte)'_':
|
||
case (byte)'~':
|
||
case (byte)':':
|
||
case (byte)'!':
|
||
case (byte)'$':
|
||
case (byte)'&':
|
||
case (byte)'+':
|
||
return true;
|
||
}
|
||
|
||
return false;
|
||
}
|
||
}
|
||
}
|