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.

506 lines
18 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. using Common.Model;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Configuration;
  5. using System.Data;
  6. using System.IO;
  7. using System.Text;
  8. using System.Web;
  9. using System.Web.Script.Serialization;
  10. using System.Web.UI;
  11. namespace Common.Helper
  12. {
  13. public static class GetStatic
  14. {
  15. public static string GetUser()
  16. {
  17. var user = ReadSession("admin", "");
  18. WriteSession("admin", user);
  19. //WriteSession("lastActiveTS", DateTime.Now.ToString());
  20. return user;
  21. }
  22. public static string ReadSession(string key, string defVal)
  23. {
  24. try
  25. {
  26. return HttpContext.Current.Session[key] == null ? defVal : HttpContext.Current.Session[key].ToString();
  27. }
  28. catch
  29. {
  30. return "";
  31. }
  32. }
  33. public static void WriteSession(string key, string value)
  34. {
  35. HttpContext.Current.Session[key] = value;
  36. }
  37. public static string GetVirtualDirName()
  38. {
  39. return ReadWebConfig("virtualDirName");
  40. }
  41. public static string ReadWebConfig(string key)
  42. {
  43. return ReadWebConfig(key, "");
  44. }
  45. public static string ReadWebConfig(string key, string defValue)
  46. {
  47. return ConfigurationManager.AppSettings[key] ?? defValue;
  48. }
  49. public static string GetIp()
  50. {
  51. return HttpContext.Current.Request.ClientCertificate["REMOTE_ADDR"];
  52. }
  53. public static void WriteCookie(string key, string value)
  54. {
  55. if (string.IsNullOrEmpty(value.Trim()))
  56. {
  57. DeleteCookie(key);
  58. return;
  59. }
  60. var httpCookie = new HttpCookie(key, value);
  61. httpCookie.Expires = DateTime.Now.AddDays(1);
  62. HttpContext.Current.Response.Cookies.Add(httpCookie);
  63. }
  64. public static string ReadCookie(string key, string defVal)
  65. {
  66. var cookie = HttpContext.Current.Request.Cookies[key];
  67. return cookie == null ? defVal : HttpContext.Current.Server.HtmlEncode(cookie.Value);
  68. }
  69. public static void DeleteCookie(string key)
  70. {
  71. if (HttpContext.Current.Request.Cookies[key] != null)
  72. {
  73. var aCookie = new HttpCookie(key);
  74. aCookie.Expires = DateTime.Now.AddDays(-1);
  75. HttpContext.Current.Response.Cookies.Add(aCookie);
  76. }
  77. }
  78. public static void SetMessage(DbResponse value)
  79. {
  80. HttpContext.Current.Session["message"] = value;
  81. }
  82. public static void SetMessage(string responseCode, string msg)
  83. {
  84. var dbResult = new DbResponse { ResponseCode = responseCode, Msg = msg };
  85. SetMessage(dbResult);
  86. }
  87. public static DbResponse ParseDbResult(DataTable dt)
  88. {
  89. var res = new DbResponse();
  90. if (dt.Rows.Count > 0)
  91. {
  92. res.ResponseCode = dt.Rows[0][0].ToString();
  93. res.Msg = dt.Rows[0][1].ToString();
  94. res.Id = dt.Rows[0][2].ToString();
  95. if (dt.Columns.Count > 3)
  96. {
  97. res.Extra = dt.Rows[0][3].ToString();
  98. }
  99. if (dt.Columns.Count > 4)
  100. {
  101. res.Extra2 = dt.Rows[0][4].ToString();
  102. }
  103. }
  104. return res;
  105. }
  106. public static void JsonResponse<T>(T obk, Page page)
  107. {
  108. System.Web.Script.Serialization.JavaScriptSerializer jsonData = new System.Web.Script.Serialization.JavaScriptSerializer();
  109. string jsonString = jsonData.Serialize(obk);
  110. page.Response.ContentType = "application/json";
  111. page.Response.Write(jsonString);
  112. page.Response.End();
  113. }
  114. public static void PrintMessage(Page page)
  115. {
  116. if (HttpContext.Current.Session["message"] == null)
  117. {
  118. //CallBackJs1(page, "Remove Message", "window.parent.RemoveMessageBox();");
  119. return;
  120. }
  121. var dbResult = GetMessage();
  122. CallBackJs(page, "Set Message", "window.parent.SetMessageBox(\"" + dbResult.Msg + "\",\"" + dbResult.ResponseCode + "\");");
  123. HttpContext.Current.Session.Remove("message");
  124. }
  125. public static DbResponse GetMessage()
  126. {
  127. return (DbResponse)HttpContext.Current.Session["message"];
  128. }
  129. public static void CallBackJs(Page page, String scriptName, string functionName)
  130. {
  131. ScriptManager.RegisterStartupScript(page, page.GetType(), scriptName, functionName, true);
  132. }
  133. public static void PrintMessage(Page page, DbResponse dbResult)
  134. {
  135. CallBackJs(page, "Set Message", "window.parent.SetMessageBox(\"" + dbResult.Msg + "\",\"" + dbResult.ResponseCode + "\");");
  136. }
  137. public static void PrintSuccessMessage(Page page, string msg)
  138. {
  139. PrintMessage(page, "0", msg);
  140. }
  141. public static void PrintErrorMessage(Page page, string msg)
  142. {
  143. PrintMessage(page, "1", msg);
  144. }
  145. public static void PrintMessage(Page page, string errorCode, string msg)
  146. {
  147. CallBackJs(page, "Set Message", "window.parent.SetMessageBox(\"" + msg + "\",\"" + errorCode + "\");");
  148. }
  149. public static void AlertMessage(Page page)
  150. {
  151. if (HttpContext.Current.Session["message"] == null)
  152. return;
  153. var dbResult = GetMessage();
  154. if (dbResult.Msg == "")
  155. return;
  156. CallBackJs(page, "Alert Message", "alert(\"" + FilterMessageForJs(dbResult.Msg) + "\");");
  157. HttpContext.Current.Session.Remove("message");
  158. }
  159. public static String FilterMessageForJs(string strVal)
  160. {
  161. if (strVal.ToLower() != "null")
  162. {
  163. strVal = strVal.Replace("\"", "");
  164. }
  165. return strVal;
  166. }
  167. public static string GetIpAddress(HttpRequest request)
  168. {
  169. return request.ServerVariables["remote_addr"];
  170. }
  171. public static string GetUserInfo(HttpRequest request)
  172. {
  173. HttpBrowserCapabilities browser = request.Browser;
  174. string str = " Browser Capabilities = Values -:::-"
  175. + "Type = " + browser.Type + "-:::-" //-:::-
  176. + "Name = " + browser.Browser + "-:::-"
  177. + "Version = " + browser.Version + "-:::-"
  178. + "Major Version = " + browser.MajorVersion + "-:::-"
  179. + "Minor Version = " + browser.MinorVersion + "-:::-"
  180. + "Platform = " + browser.Platform + "-:::-"
  181. + "Is Beta = " + browser.Beta + "-:::-"
  182. + "Is Crawler = " + browser.Crawler + "-:::-"
  183. + "Is AOL = " + browser.AOL + "-:::-"
  184. + "Is Win16 = " + browser.Win16 + "-:::-"
  185. + "Is Win32 = " + browser.Win32 + "-:::-"
  186. + "Supports Frames = " + browser.Frames + "-:::-"
  187. + "Supports Tables = " + browser.Tables + "-:::-"
  188. + "Supports Cookies = " + browser.Cookies + "-:::-"
  189. + "Supports VBScript = " + browser.VBScript + "-:::-"
  190. + "Supports JavaScript = " + browser.EcmaScriptVersion.ToString() + "-:::-"
  191. + "Supports Java Applets = " + browser.JavaApplets + "-:::-"
  192. + "Supports ActiveX Controls = " + browser.ActiveXControls + "-:::-"
  193. + "Supports JavaScript Version = " + browser["JavaScriptVersion"] + "-:::-"
  194. + "CDF = " + browser.CDF + "-:::-"
  195. + "IP Adress = " + request.ServerVariables["REMOTE_ADDR"] + "-:::-"
  196. + "User Agent = " + request.ServerVariables["HTTP_USER_AGENT"] + "-:::-"
  197. + "Refrerer = " + request.ServerVariables["HTTP_REFERER"] + "-:::-"
  198. + "Http Accept = " + request.ServerVariables["HTTP_ACCEPT"] + "-:::-"
  199. + "Language = " + request.ServerVariables["HTTP_ACCEPT_LANGUAGE"];
  200. return str;
  201. }
  202. public static string ReadQueryString(string key, string defVal)
  203. {
  204. return HttpContext.Current.Request.QueryString[key] ?? defVal;
  205. }
  206. public static string ReadFormData(string key, string defval)
  207. {
  208. return HttpContext.Current.Request.Form[key] ?? defval;
  209. }
  210. public static void ClearSession(string key)
  211. {
  212. HttpContext.Current.Session[key] = "";
  213. }
  214. public static string GetUrlRoot()
  215. {
  216. return ReadWebConfig("urlRoot");
  217. }
  218. public static string FormatData(string data, string dataType)
  219. {
  220. if (string.IsNullOrEmpty(data))
  221. return "&nbsp;";
  222. if (data == "-")
  223. return data;
  224. if (dataType == "D")
  225. {
  226. DateTime d;
  227. DateTime.TryParse(data, out d);
  228. return d.Year + "-" + d.Month.ToString("00") + "-" + d.Day.ToString("00");
  229. }
  230. if (dataType == "DT")
  231. {
  232. DateTime t;
  233. DateTime.TryParse(data, out t);
  234. return t.Year + "-" + t.Month.ToString("00") + "-" + t.Day.ToString("00") + " " + t.Hour.ToString("00") + ":" + t.Minute.ToString("00");
  235. }
  236. if (dataType == "M")
  237. {
  238. decimal m;
  239. decimal.TryParse(data, out m);
  240. return m.ToString("N");
  241. }
  242. return data;
  243. }
  244. public static void AlertMessage(Page page, string msg)
  245. {
  246. CallBackJs1(page, "Alert Message", "alert(\"" + FilterMessageForJs(msg) + "\");");
  247. }
  248. public static void CallBackJs1(Page page, String scriptName, string functionName)
  249. {
  250. ScriptManager.RegisterStartupScript(page, page.GetType(), scriptName, functionName, true);
  251. }
  252. public static string GetLogoutPage()
  253. {
  254. return GetUrlRoot() + "/Logout.aspx";
  255. }
  256. public static string GetAuthenticationPage()
  257. {
  258. return GetUrlRoot() + "/Authentication.aspx";
  259. }
  260. public static string GetAgent()
  261. {
  262. return ReadSession("agent", "");
  263. }
  264. public static string DataTableToJson(DataTable table)
  265. {
  266. if (table == null)
  267. return "";
  268. var list = new List<Dictionary<string, object>>();
  269. foreach (DataRow row in table.Rows)
  270. {
  271. var dict = new Dictionary<string, object>();
  272. foreach (DataColumn col in table.Columns)
  273. {
  274. dict[col.ColumnName] = string.IsNullOrEmpty(row[col].ToString()) ? "" : row[col];
  275. }
  276. list.Add(dict);
  277. }
  278. var serializer = new JavaScriptSerializer();
  279. string json = serializer.Serialize(list);
  280. return json;
  281. }
  282. public static string GetUserType()
  283. {
  284. var userType = ReadSession("userType", "");
  285. //WriteSession("lastActiveTS", DateTime.Now.ToString());
  286. return userType;
  287. }
  288. public static string GetAgentId()
  289. {
  290. return ReadSession("agentId", "");
  291. }
  292. public static string GetCustomerFilePath()
  293. {
  294. return ReadWebConfig("customerDocPath");
  295. }
  296. public static string GetAppRoot()
  297. {
  298. return ReadWebConfig("root");
  299. }
  300. public static string GetCountryId()
  301. {
  302. return ReadSession("countryId", "");
  303. }
  304. public static string GetSettlingAgent()
  305. {
  306. return ReadSession("settlingAgent", "");
  307. }
  308. public static string GetSuperAgent()
  309. {
  310. return ReadSession("superAgent", "");
  311. }
  312. public static string GetBranch()
  313. {
  314. return ReadSession("branch", "");
  315. }
  316. public static void CallJSFunction(Page page, string functionName)
  317. {
  318. ScriptManager.RegisterStartupScript(page, page.GetType(), "cb", functionName, true);
  319. }
  320. public static string UploadSignatureImage(string imageData, string registerDate, string membershipId, string customerId)
  321. {
  322. string errorCode = "1";
  323. try
  324. {
  325. string fileExtension = ".png";
  326. string fileName = customerId + "_signature_" + DateTime.Now.Hour.ToString() + DateTime.Now.Millisecond.ToString() + "_" + registerDate.Replace("-", "_") + fileExtension;
  327. string path = GetStatic.GetCustomerFilePath() + "CustomerDocument\\" + registerDate.Replace("-", "\\") + "\\" + membershipId;
  328. if (!Directory.Exists(path))
  329. Directory.CreateDirectory(path);
  330. string fName = path + "\\" + fileName;
  331. using (FileStream fs = new FileStream(fName, FileMode.CreateNew))
  332. {
  333. using (BinaryWriter bw = new BinaryWriter(fs))
  334. {
  335. byte[] data = Convert.FromBase64String(imageData);
  336. bw.Write(data);
  337. bw.Close();
  338. }
  339. }
  340. errorCode = fileName;
  341. }
  342. catch (Exception ex)
  343. {
  344. errorCode = "1";
  345. }
  346. return errorCode;
  347. }
  348. public static string NumberToWord(string data)
  349. {
  350. var str = data.Split('.');
  351. int number = Convert.ToInt32(str[0]);
  352. int dec = 0;
  353. if (str.Length > 1)
  354. dec = Convert.ToInt32(str[1].Substring(0, 2));
  355. if (number == 0) return "Zero";
  356. if (number == -2147483648)
  357. return
  358. "Minus Two Hundred and Fourteen Crore Seventy Four Lakh Eighty Three Thousand Six Hundred and Forty Eight Rupees Fifty Paisa";
  359. int[] num = new int[4];
  360. int first = 0;
  361. int u, h, t;
  362. StringBuilder sb = new System.Text.StringBuilder();
  363. if (number < 0)
  364. {
  365. sb.Append("Minus ");
  366. number = -number;
  367. }
  368. string[] words0 = {
  369. "", "One ", "Two ", "Three ", "Four ",
  370. "Five ", "Six ", "Seven ", "Eight ", "Nine "
  371. };
  372. string[] words1 = {
  373. "Ten ", "Eleven ", "Twelve ", "Thirteen ", "Fourteen ",
  374. "Fifteen ", "Sixteen ", "Seventeen ", "Eighteen ", "Nineteen "
  375. };
  376. string[] words2 = {
  377. "Twenty ", "Thirty ", "Forty ", "Fifty ", "Sixty ",
  378. "Seventy ", "Eighty ", "Ninety "
  379. };
  380. string[] words3 = { "Thousand ", "Lakh ", "Crore " };
  381. num[0] = number % 1000; // units
  382. num[1] = number / 1000;
  383. num[2] = number / 100000;
  384. num[1] = num[1] - 100 * num[2]; // thousands
  385. num[3] = number / 10000000; // crores
  386. num[2] = num[2] - 100 * num[3]; // lakhs
  387. for (int i = 3; i > 0; i--)
  388. {
  389. if (num[i] != 0)
  390. {
  391. first = i;
  392. break;
  393. }
  394. }
  395. for (int i = first; i >= 0; i--)
  396. {
  397. if (num[i] == 0) continue;
  398. u = num[i] % 10; // ones
  399. t = num[i] / 10;
  400. h = num[i] / 100; // hundreds
  401. t = t - 10 * h; // tens
  402. if (h > 0) sb.Append(words0[h] + "Hundred ");
  403. if (u > 0 || t > 0)
  404. {
  405. if (h > 0 && i == 0) sb.Append("and ");
  406. if (t == 0)
  407. sb.Append(words0[u]);
  408. else if (t == 1)
  409. sb.Append(words1[u]);
  410. else
  411. sb.Append(words2[t - 2] + words0[u]);
  412. }
  413. if (i != 0) sb.Append(words3[i - 1]);
  414. }
  415. //sb.Append(" Rupees ");
  416. int d1 = dec / 10;
  417. int d2 = dec % 10;
  418. if (d1 == 0)
  419. sb.Append(words0[d1]);
  420. else if (d1 == 1)
  421. sb.Append(words1[d2]);
  422. else
  423. sb.Append(words2[d1 - 2] + words0[d2]);
  424. //if (dec > 0)
  425. // sb.Append(" Paisa");
  426. return sb.ToString().TrimEnd() + " only";
  427. }
  428. public static String ShowWithoutDecimal(String strVal)
  429. {
  430. if (strVal != "")
  431. return String.Format("{0:0,0}", double.Parse(strVal));
  432. else
  433. return strVal;
  434. }
  435. public static String ShowDecimal(String strVal)
  436. {
  437. if (strVal != "")
  438. return String.Format("{0:0,0.00}", double.Parse(strVal));
  439. else
  440. return strVal;
  441. }
  442. }
  443. }