You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

663 lines
24 KiB

using Common.Model;
using Common.Utility;
using SelectPdf;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.IO;
using System.Text;
using System.Web;
using System.Web.Script.Serialization;
using System.Web.UI;
namespace Common.Helper
{
public static class GetStatic
{
public static string GetUser()
{
var user = ReadSession("admin", "");
WriteSession("admin", user);
//WriteSession("lastActiveTS", DateTime.Now.ToString());
return user;
}
public static string ReadSession(string key, string defVal)
{
try
{
return HttpContext.Current.Session[key] == null ? defVal : HttpContext.Current.Session[key].ToString();
}
catch
{
return "";
}
}
public static string URIToPDF(string uri, string user, string fileSaveLocation, string divIdToOnlySelectInURI = "")
{
string fileName = GetDateTimeStamp() + "_" + user + ".pdf";
string fileLocation = ReadWebConfig("root", "") + "temp\\pdf\\" + fileName;
HtmlToPdf converter = new HtmlToPdf();
PdfDocument doc = converter.ConvertUrl(uri);
doc.Save(fileLocation);
doc.Close();
if (!File.Exists(fileSaveLocation))
Directory.CreateDirectory(fileSaveLocation);
File.Move(fileLocation, fileSaveLocation + fileName);
return fileName;
}
public static string HTMLToPDF(string htmlString, string user, string fileSaveLocation, string filePrefix = "")
{
string fileName = GetDateTimeStamp() + "_" + user + ".pdf";
if (!string.IsNullOrEmpty(filePrefix))
fileName = filePrefix + "_" + fileName;
HtmlToPdf converter = new HtmlToPdf();
if (!File.Exists(fileSaveLocation))
Directory.CreateDirectory(fileSaveLocation);
PdfDocument doc = converter.ConvertHtmlString(htmlString, ReadWebConfig("root", ""));
doc.Save(fileSaveLocation + fileName);
doc.Close();
return fileName;
}
public static string GetDateTimeStamp()
{
return DateTime.Now.ToString("yyyyMMddHHmmssffff");
}
public static void WriteSession(string key, string value)
{
HttpContext.Current.Session[key] = value;
}
public static string GetVirtualDirName()
{
return ReadWebConfig("virtualDirName");
}
public static string ReadWebConfig(string key)
{
return ReadWebConfig(key, "");
}
public static string ReadWebConfig(string key, string defValue)
{
return ConfigurationManager.AppSettings[key] ?? defValue;
}
public static string GetIp()
{
return HttpContext.Current.Request.ClientCertificate["REMOTE_ADDR"];
}
public static void WriteCookie(string key, string value)
{
if (string.IsNullOrEmpty(value.Trim()))
{
DeleteCookie(key);
return;
}
var httpCookie = new HttpCookie(key, value);
httpCookie.Expires = DateTime.Now.AddDays(1);
HttpContext.Current.Response.Cookies.Add(httpCookie);
}
public static string ReadCookie(string key, string defVal)
{
var cookie = HttpContext.Current.Request.Cookies[key];
return cookie == null ? defVal : HttpContext.Current.Server.HtmlEncode(cookie.Value);
}
public static void DeleteCookie(string key)
{
if (HttpContext.Current.Request.Cookies[key] != null)
{
var aCookie = new HttpCookie(key);
aCookie.Expires = DateTime.Now.AddDays(-1);
HttpContext.Current.Response.Cookies.Add(aCookie);
}
}
public static void SetMessage(DbResponse value)
{
HttpContext.Current.Session["message"] = value;
}
public static void SetMessage(string responseCode, string msg)
{
var dbResult = new DbResponse { ResponseCode = responseCode, Msg = msg };
SetMessage(dbResult);
}
public static DbResponse ParseDbResult(DataTable dt)
{
var res = new DbResponse();
if (dt.Rows.Count > 0)
{
res.ResponseCode = dt.Rows[0][0].ToString();
res.Msg = dt.Rows[0][1].ToString();
res.Id = dt.Rows[0][2].ToString();
if (dt.Columns.Count > 3)
{
res.Extra = dt.Rows[0][3].ToString();
}
if (dt.Columns.Count > 4)
{
res.Extra2 = dt.Rows[0][4].ToString();
}
}
return res;
}
public static void JsonResponse<T>(T obk, Page page)
{
System.Web.Script.Serialization.JavaScriptSerializer jsonData = new System.Web.Script.Serialization.JavaScriptSerializer();
string jsonString = jsonData.Serialize(obk);
page.Response.ContentType = "application/json";
page.Response.Write(jsonString);
page.Response.End();
}
public static void PrintMessage(Page page)
{
if (HttpContext.Current.Session["message"] == null)
{
//CallBackJs1(page, "Remove Message", "window.parent.RemoveMessageBox();");
return;
}
var dbResult = GetMessage();
CallBackJs(page, "Set Message", "window.parent.SetMessageBox(\"" + dbResult.Msg + "\",\"" + dbResult.ResponseCode + "\");");
HttpContext.Current.Session.Remove("message");
}
public static DbResponse GetMessage()
{
return (DbResponse)HttpContext.Current.Session["message"];
}
public static void CallBackJs(Page page, String scriptName, string functionName)
{
ScriptManager.RegisterStartupScript(page, page.GetType(), scriptName, functionName, true);
}
public static void PrintMessage(Page page, DbResponse dbResult)
{
CallBackJs(page, "Set Message", "window.parent.SetMessageBox(\"" + dbResult.Msg + "\",\"" + dbResult.ResponseCode + "\");");
}
public static void PrintSuccessMessage(Page page, string msg)
{
PrintMessage(page, "0", msg);
}
public static void PrintErrorMessage(Page page, string msg)
{
PrintMessage(page, "1", msg);
}
public static void PrintMessage(Page page, string errorCode, string msg)
{
CallBackJs(page, "Set Message", "window.parent.SetMessageBox(\"" + msg + "\",\"" + errorCode + "\");");
}
public static void AlertMessage(Page page)
{
if (HttpContext.Current.Session["message"] == null)
return;
var dbResult = GetMessage();
if (dbResult.Msg == "")
return;
CallBackJs(page, "Alert Message", "alert(\"" + FilterMessageForJs(dbResult.Msg) + "\");");
HttpContext.Current.Session.Remove("message");
}
public static String FilterMessageForJs(string strVal)
{
if (strVal.ToLower() != "null")
{
strVal = strVal.Replace("\"", "");
}
return strVal;
}
public static string GetIpAddress(HttpRequest request)
{
return request.ServerVariables["remote_addr"];
}
public static string GetUserInfo(HttpRequest request)
{
HttpBrowserCapabilities browser = request.Browser;
string str = " Browser Capabilities = Values -:::-"
+ "Type = " + browser.Type + "-:::-" //-:::-
+ "Name = " + browser.Browser + "-:::-"
+ "Version = " + browser.Version + "-:::-"
+ "Major Version = " + browser.MajorVersion + "-:::-"
+ "Minor Version = " + browser.MinorVersion + "-:::-"
+ "Platform = " + browser.Platform + "-:::-"
+ "Is Beta = " + browser.Beta + "-:::-"
+ "Is Crawler = " + browser.Crawler + "-:::-"
+ "Is AOL = " + browser.AOL + "-:::-"
+ "Is Win16 = " + browser.Win16 + "-:::-"
+ "Is Win32 = " + browser.Win32 + "-:::-"
+ "Supports Frames = " + browser.Frames + "-:::-"
+ "Supports Tables = " + browser.Tables + "-:::-"
+ "Supports Cookies = " + browser.Cookies + "-:::-"
+ "Supports VBScript = " + browser.VBScript + "-:::-"
+ "Supports JavaScript = " + browser.EcmaScriptVersion.ToString() + "-:::-"
+ "Supports Java Applets = " + browser.JavaApplets + "-:::-"
+ "Supports ActiveX Controls = " + browser.ActiveXControls + "-:::-"
+ "Supports JavaScript Version = " + browser["JavaScriptVersion"] + "-:::-"
+ "CDF = " + browser.CDF + "-:::-"
+ "IP Adress = " + request.ServerVariables["REMOTE_ADDR"] + "-:::-"
+ "User Agent = " + request.ServerVariables["HTTP_USER_AGENT"] + "-:::-"
+ "Refrerer = " + request.ServerVariables["HTTP_REFERER"] + "-:::-"
+ "Http Accept = " + request.ServerVariables["HTTP_ACCEPT"] + "-:::-"
+ "Language = " + request.ServerVariables["HTTP_ACCEPT_LANGUAGE"];
return str;
}
public static string ReadQueryString(string key, string defVal)
{
return HttpContext.Current.Request.QueryString[key] ?? defVal;
}
public static string ReadFormData(string key, string defval)
{
return HttpContext.Current.Request.Form[key] ?? defval;
}
public static void ClearSession(string key)
{
HttpContext.Current.Session[key] = "";
}
public static string GetUrlRoot()
{
return ReadWebConfig("urlRoot");
}
public static string FormatData(string data, string dataType)
{
if (string.IsNullOrEmpty(data))
return "&nbsp;";
if (data == "-")
return data;
if (dataType == "D")
{
DateTime d;
DateTime.TryParse(data, out d);
return d.Year + "-" + d.Month.ToString("00") + "-" + d.Day.ToString("00");
}
if (dataType == "DT")
{
DateTime t;
DateTime.TryParse(data, out t);
return t.Year + "-" + t.Month.ToString("00") + "-" + t.Day.ToString("00") + " " + t.Hour.ToString("00") + ":" + t.Minute.ToString("00");
}
if (dataType == "M")
{
decimal m;
decimal.TryParse(data, out m);
return m.ToString("N");
}
return data;
}
public static void AlertMessage(Page page, string msg)
{
CallBackJs1(page, "Alert Message", "alert(\"" + FilterMessageForJs(msg) + "\");");
}
public static void CallBackJs1(Page page, String scriptName, string functionName)
{
ScriptManager.RegisterStartupScript(page, page.GetType(), scriptName, functionName, true);
}
public static string GetLogoutPage()
{
return GetUrlRoot() + "/Logout.aspx";
}
public static string GetAuthenticationPage()
{
return GetUrlRoot() + "/Authentication.aspx";
}
public static string GetAgent()
{
return ReadSession("agent", "");
}
public static string DataTableToJson(DataTable table)
{
if (table == null)
return "";
var list = new List<Dictionary<string, object>>();
foreach (DataRow row in table.Rows)
{
var dict = new Dictionary<string, object>();
foreach (DataColumn col in table.Columns)
{
dict[col.ColumnName] = string.IsNullOrEmpty(row[col].ToString()) ? "" : row[col];
}
list.Add(dict);
}
var serializer = new JavaScriptSerializer();
string json = serializer.Serialize(list);
return json;
}
public static string GetUserType()
{
var userType = ReadSession("userType", "");
//WriteSession("lastActiveTS", DateTime.Now.ToString());
return userType;
}
public static string GetAgentId()
{
return ReadSession("agentId", "");
}
public static string GetCustomerFilePath()
{
return ReadWebConfig("customerDocPath");
}
public static string GetAppRoot()
{
return ReadWebConfig("root");
}
public static string GetCountryId()
{
return ReadSession("countryId", "");
}
public static string GetSettlingAgent()
{
return ReadSession("settlingAgent", "");
}
public static string GetSuperAgent()
{
return ReadSession("superAgent", "");
}
public static string GetBranch()
{
return ReadSession("branch", "");
}
public static void CallJSFunction(Page page, string functionName)
{
ScriptManager.RegisterStartupScript(page, page.GetType(), "cb", functionName, true);
}
public static string UploadSignatureImage(string imageData, string registerDate, string membershipId, string customerId)
{
string errorCode = "1";
try
{
string fileExtension = ".png";
string fileName = customerId + "_signature_" + DateTime.Now.Hour.ToString() + DateTime.Now.Millisecond.ToString() + "_" + registerDate.Replace("-", "_") + fileExtension;
string path = GetCustomerFilePath() + "CustomerDocument\\" + registerDate.Replace("-", "\\") + "\\" + membershipId;
if (!Directory.Exists(path))
Directory.CreateDirectory(path);
string fName = path + "\\" + fileName;
using (FileStream fs = new FileStream(fName, FileMode.CreateNew))
{
using (BinaryWriter bw = new BinaryWriter(fs))
{
byte[] data = Convert.FromBase64String(imageData);
bw.Write(data);
bw.Close();
}
}
errorCode = fileName;
}
catch (Exception ex)
{
errorCode = "1";
}
return errorCode;
}
public static string UploadDocument(HttpPostedFile doc, string customerId, string documentTypeName, string membershipId, string registeredDate, out string fileType)
{
fileType = "";
string fName = "";
string documentExtension = "";
try
{
string root = ConfigurationManager.AppSettings["filePath"];
var saveFileLocation = root + "\\doc\\tmp\\";
if (!Directory.Exists(saveFileLocation))
Directory.CreateDirectory(saveFileLocation);
fileType = doc.ContentType;
string fileExtension = new FileInfo(doc.FileName).Extension;
if (documentTypeName.ToLower() == "other_document" || documentTypeName.ToLower() == "registration_form" || documentTypeName.ToLower() == "beneficiary_registration_form")
{
documentExtension = GetStatic.ReadWebConfig("customerDocFileExtensionForOtherDoc", "");
}
else
{
documentExtension = GetStatic.ReadWebConfig("customerDocFileExtension", "");
}
if (documentExtension.ToLower().Contains(fileExtension.ToLower()))
{
string fileName = documentTypeName + "_" + GetStatic.GetDateTimeStamp() + "_" + GetStatic.GetUser() + fileExtension;
string path = GetStatic.GetCustomerFilePath() + "CustomerDocument\\" + registeredDate.Replace("-", "\\") + "\\" + membershipId;
if (!Directory.Exists(path))
Directory.CreateDirectory(path);
//doc.SaveAs(path + "/" + fileName);
//fName = fileName;
var original_imagePath = root + "\\doc\\tmp\\" + "_org_" + fileName;
string saved_file_name = path + "\\" + fileName;
doc.SaveAs(original_imagePath);
System.IO.FileInfo fi = new System.IO.FileInfo(original_imagePath);
doc.SaveAs(path + "/" + fileName);
fName = fileName;
}
else
{
fName = "notValid";
}
}
catch (Exception ex)
{
fName = "";
}
return fName;
}
public static string NumberToWord(string data)
{
var str = data.Split('.');
int number = Convert.ToInt32(str[0]);
int dec = 0;
if (str.Length > 1)
dec = Convert.ToInt32(str[1].Substring(0, 2));
if (number == 0) return "Zero";
if (number == -2147483648)
return
"Minus Two Hundred and Fourteen Crore Seventy Four Lakh Eighty Three Thousand Six Hundred and Forty Eight Rupees Fifty Paisa";
int[] num = new int[4];
int first = 0;
int u, h, t;
StringBuilder sb = new System.Text.StringBuilder();
if (number < 0)
{
sb.Append("Minus ");
number = -number;
}
string[] words0 = {
"", "One ", "Two ", "Three ", "Four ",
"Five ", "Six ", "Seven ", "Eight ", "Nine "
};
string[] words1 = {
"Ten ", "Eleven ", "Twelve ", "Thirteen ", "Fourteen ",
"Fifteen ", "Sixteen ", "Seventeen ", "Eighteen ", "Nineteen "
};
string[] words2 = {
"Twenty ", "Thirty ", "Forty ", "Fifty ", "Sixty ",
"Seventy ", "Eighty ", "Ninety "
};
string[] words3 = { "Thousand ", "Lakh ", "Crore " };
num[0] = number % 1000; // units
num[1] = number / 1000;
num[2] = number / 100000;
num[1] = num[1] - 100 * num[2]; // thousands
num[3] = number / 10000000; // crores
num[2] = num[2] - 100 * num[3]; // lakhs
for (int i = 3; i > 0; i--)
{
if (num[i] != 0)
{
first = i;
break;
}
}
for (int i = first; i >= 0; i--)
{
if (num[i] == 0) continue;
u = num[i] % 10; // ones
t = num[i] / 10;
h = num[i] / 100; // hundreds
t = t - 10 * h; // tens
if (h > 0) sb.Append(words0[h] + "Hundred ");
if (u > 0 || t > 0)
{
if (h > 0 && i == 0) sb.Append("and ");
if (t == 0)
sb.Append(words0[u]);
else if (t == 1)
sb.Append(words1[u]);
else
sb.Append(words2[t - 2] + words0[u]);
}
if (i != 0) sb.Append(words3[i - 1]);
}
//sb.Append(" Rupees ");
int d1 = dec / 10;
int d2 = dec % 10;
if (d1 == 0)
sb.Append(words0[d1]);
else if (d1 == 1)
sb.Append(words1[d2]);
else
sb.Append(words2[d1 - 2] + words0[d2]);
//if (dec > 0)
// sb.Append(" Paisa");
return sb.ToString().TrimEnd() + " only";
}
public static String ShowWithoutDecimal(String strVal)
{
if (strVal != "")
return String.Format("{0:0,0}", double.Parse(strVal));
else
return strVal;
}
public static String ShowDecimal(String strVal)
{
if (strVal != "")
return String.Format("{0:0,0.00}", double.Parse(strVal));
else
return strVal;
}
public static string GetSessionId()
{
return HttpContext.Current.Session.SessionID;
}
public static string ParseResultJsPrint(DbResult dbResult)
{
return dbResult.ErrorCode + "-:::-" + dbResult.Msg.Replace("'", "").Replace("<br/>", "").Replace(System.Environment.NewLine, "") + "-:::-" + dbResult.Id;
}
public static string PutRedBackGround(string mes)
{
return "<span style = \"background-color : red\">" + mes + "</span>";
}
public static string DataSetToJSON(DataSet ds)
{
ArrayList root = new ArrayList();
List<Dictionary<string, object>> table;
Dictionary<string, object> data;
foreach (DataTable dt in ds.Tables)
{
table = new List<Dictionary<string, object>>();
foreach (DataRow dr in dt.Rows)
{
data = new Dictionary<string, object>();
foreach (DataColumn col in dt.Columns)
{
data.Add(col.ColumnName, dr[col]);
}
table.Add(data);
}
root.Add(table);
}
JavaScriptSerializer serializer = new JavaScriptSerializer();
return serializer.Serialize(root);
}
public static DataTable ToDataTable<T>(this IList<T> data)
{
PropertyDescriptorCollection props =
TypeDescriptor.GetProperties(typeof(T));
DataTable table = new DataTable();
for (int i = 0; i < props.Count; i++)
{
PropertyDescriptor prop = props[i];
table.Columns.Add(prop.Name, prop.PropertyType);
}
object[] values = new object[props.Count];
foreach (T item in data)
{
for (int i = 0; i < values.Length; i++)
{
values[i] = props[i].GetValue(item);
}
table.Rows.Add(values);
}
return table;
}
}
}