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.

662 lines
24 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
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 Common.Utility;
  3. using SelectPdf;
  4. using System;
  5. using System.Collections;
  6. using System.Collections.Generic;
  7. using System.ComponentModel;
  8. using System.Configuration;
  9. using System.Data;
  10. using System.IO;
  11. using System.Text;
  12. using System.Web;
  13. using System.Web.Script.Serialization;
  14. using System.Web.UI;
  15. namespace Common.Helper
  16. {
  17. public static class GetStatic
  18. {
  19. public static string GetUser()
  20. {
  21. var user = ReadSession("admin", "");
  22. WriteSession("admin", user);
  23. //WriteSession("lastActiveTS", DateTime.Now.ToString());
  24. return user;
  25. }
  26. public static string ReadSession(string key, string defVal)
  27. {
  28. try
  29. {
  30. return HttpContext.Current.Session[key] == null ? defVal : HttpContext.Current.Session[key].ToString();
  31. }
  32. catch
  33. {
  34. return "";
  35. }
  36. }
  37. public static string URIToPDF(string uri, string user, string fileSaveLocation, string divIdToOnlySelectInURI = "")
  38. {
  39. string fileName = GetDateTimeStamp() + "_" + user + ".pdf";
  40. string fileLocation = ReadWebConfig("root", "") + "temp\\pdf\\" + fileName;
  41. HtmlToPdf converter = new HtmlToPdf();
  42. PdfDocument doc = converter.ConvertUrl(uri);
  43. doc.Save(fileLocation);
  44. doc.Close();
  45. if (!File.Exists(fileSaveLocation))
  46. Directory.CreateDirectory(fileSaveLocation);
  47. File.Move(fileLocation, fileSaveLocation + fileName);
  48. return fileName;
  49. }
  50. public static string HTMLToPDF(string htmlString, string user, string fileSaveLocation, string filePrefix = "")
  51. {
  52. string fileName = GetDateTimeStamp() + "_" + user + ".pdf";
  53. if (!string.IsNullOrEmpty(filePrefix))
  54. fileName = filePrefix + "_" + fileName;
  55. HtmlToPdf converter = new HtmlToPdf();
  56. if (!File.Exists(fileSaveLocation))
  57. Directory.CreateDirectory(fileSaveLocation);
  58. PdfDocument doc = converter.ConvertHtmlString(htmlString, ReadWebConfig("root", ""));
  59. doc.Save(fileSaveLocation + fileName);
  60. doc.Close();
  61. return fileName;
  62. }
  63. public static string GetDateTimeStamp()
  64. {
  65. return DateTime.Now.ToString("yyyyMMddHHmmssffff");
  66. }
  67. public static void WriteSession(string key, string value)
  68. {
  69. HttpContext.Current.Session[key] = value;
  70. }
  71. public static string GetVirtualDirName()
  72. {
  73. return ReadWebConfig("virtualDirName");
  74. }
  75. public static string ReadWebConfig(string key)
  76. {
  77. return ReadWebConfig(key, "");
  78. }
  79. public static string ReadWebConfig(string key, string defValue)
  80. {
  81. return ConfigurationManager.AppSettings[key] ?? defValue;
  82. }
  83. public static string GetIp()
  84. {
  85. return HttpContext.Current.Request.ClientCertificate["REMOTE_ADDR"];
  86. }
  87. public static void WriteCookie(string key, string value)
  88. {
  89. if (string.IsNullOrEmpty(value.Trim()))
  90. {
  91. DeleteCookie(key);
  92. return;
  93. }
  94. var httpCookie = new HttpCookie(key, value);
  95. httpCookie.Expires = DateTime.Now.AddDays(1);
  96. HttpContext.Current.Response.Cookies.Add(httpCookie);
  97. }
  98. public static string ReadCookie(string key, string defVal)
  99. {
  100. var cookie = HttpContext.Current.Request.Cookies[key];
  101. return cookie == null ? defVal : HttpContext.Current.Server.HtmlEncode(cookie.Value);
  102. }
  103. public static void DeleteCookie(string key)
  104. {
  105. if (HttpContext.Current.Request.Cookies[key] != null)
  106. {
  107. var aCookie = new HttpCookie(key);
  108. aCookie.Expires = DateTime.Now.AddDays(-1);
  109. HttpContext.Current.Response.Cookies.Add(aCookie);
  110. }
  111. }
  112. public static void SetMessage(DbResponse value)
  113. {
  114. HttpContext.Current.Session["message"] = value;
  115. }
  116. public static void SetMessage(string responseCode, string msg)
  117. {
  118. var dbResult = new DbResponse { ResponseCode = responseCode, Msg = msg };
  119. SetMessage(dbResult);
  120. }
  121. public static DbResponse ParseDbResult(DataTable dt)
  122. {
  123. var res = new DbResponse();
  124. if (dt.Rows.Count > 0)
  125. {
  126. res.ResponseCode = dt.Rows[0][0].ToString();
  127. res.Msg = dt.Rows[0][1].ToString();
  128. res.Id = dt.Rows[0][2].ToString();
  129. if (dt.Columns.Count > 3)
  130. {
  131. res.Extra = dt.Rows[0][3].ToString();
  132. }
  133. if (dt.Columns.Count > 4)
  134. {
  135. res.Extra2 = dt.Rows[0][4].ToString();
  136. }
  137. }
  138. return res;
  139. }
  140. public static void JsonResponse<T>(T obk, Page page)
  141. {
  142. System.Web.Script.Serialization.JavaScriptSerializer jsonData = new System.Web.Script.Serialization.JavaScriptSerializer();
  143. string jsonString = jsonData.Serialize(obk);
  144. page.Response.ContentType = "application/json";
  145. page.Response.Write(jsonString);
  146. page.Response.End();
  147. }
  148. public static void PrintMessage(Page page)
  149. {
  150. if (HttpContext.Current.Session["message"] == null)
  151. {
  152. //CallBackJs1(page, "Remove Message", "window.parent.RemoveMessageBox();");
  153. return;
  154. }
  155. var dbResult = GetMessage();
  156. CallBackJs(page, "Set Message", "window.parent.SetMessageBox(\"" + dbResult.Msg + "\",\"" + dbResult.ResponseCode + "\");");
  157. HttpContext.Current.Session.Remove("message");
  158. }
  159. public static DbResponse GetMessage()
  160. {
  161. return (DbResponse)HttpContext.Current.Session["message"];
  162. }
  163. public static void CallBackJs(Page page, String scriptName, string functionName)
  164. {
  165. ScriptManager.RegisterStartupScript(page, page.GetType(), scriptName, functionName, true);
  166. }
  167. public static void PrintMessage(Page page, DbResponse dbResult)
  168. {
  169. CallBackJs(page, "Set Message", "window.parent.SetMessageBox(\"" + dbResult.Msg + "\",\"" + dbResult.ResponseCode + "\");");
  170. }
  171. public static void PrintSuccessMessage(Page page, string msg)
  172. {
  173. PrintMessage(page, "0", msg);
  174. }
  175. public static void PrintErrorMessage(Page page, string msg)
  176. {
  177. PrintMessage(page, "1", msg);
  178. }
  179. public static void PrintMessage(Page page, string errorCode, string msg)
  180. {
  181. CallBackJs(page, "Set Message", "window.parent.SetMessageBox(\"" + msg + "\",\"" + errorCode + "\");");
  182. }
  183. public static void AlertMessage(Page page)
  184. {
  185. if (HttpContext.Current.Session["message"] == null)
  186. return;
  187. var dbResult = GetMessage();
  188. if (dbResult.Msg == "")
  189. return;
  190. CallBackJs(page, "Alert Message", "alert(\"" + FilterMessageForJs(dbResult.Msg) + "\");");
  191. HttpContext.Current.Session.Remove("message");
  192. }
  193. public static String FilterMessageForJs(string strVal)
  194. {
  195. if (strVal.ToLower() != "null")
  196. {
  197. strVal = strVal.Replace("\"", "");
  198. }
  199. return strVal;
  200. }
  201. public static string GetIpAddress(HttpRequest request)
  202. {
  203. return request.ServerVariables["remote_addr"];
  204. }
  205. public static string GetUserInfo(HttpRequest request)
  206. {
  207. HttpBrowserCapabilities browser = request.Browser;
  208. string str = " Browser Capabilities = Values -:::-"
  209. + "Type = " + browser.Type + "-:::-" //-:::-
  210. + "Name = " + browser.Browser + "-:::-"
  211. + "Version = " + browser.Version + "-:::-"
  212. + "Major Version = " + browser.MajorVersion + "-:::-"
  213. + "Minor Version = " + browser.MinorVersion + "-:::-"
  214. + "Platform = " + browser.Platform + "-:::-"
  215. + "Is Beta = " + browser.Beta + "-:::-"
  216. + "Is Crawler = " + browser.Crawler + "-:::-"
  217. + "Is AOL = " + browser.AOL + "-:::-"
  218. + "Is Win16 = " + browser.Win16 + "-:::-"
  219. + "Is Win32 = " + browser.Win32 + "-:::-"
  220. + "Supports Frames = " + browser.Frames + "-:::-"
  221. + "Supports Tables = " + browser.Tables + "-:::-"
  222. + "Supports Cookies = " + browser.Cookies + "-:::-"
  223. + "Supports VBScript = " + browser.VBScript + "-:::-"
  224. + "Supports JavaScript = " + browser.EcmaScriptVersion.ToString() + "-:::-"
  225. + "Supports Java Applets = " + browser.JavaApplets + "-:::-"
  226. + "Supports ActiveX Controls = " + browser.ActiveXControls + "-:::-"
  227. + "Supports JavaScript Version = " + browser["JavaScriptVersion"] + "-:::-"
  228. + "CDF = " + browser.CDF + "-:::-"
  229. + "IP Adress = " + request.ServerVariables["REMOTE_ADDR"] + "-:::-"
  230. + "User Agent = " + request.ServerVariables["HTTP_USER_AGENT"] + "-:::-"
  231. + "Refrerer = " + request.ServerVariables["HTTP_REFERER"] + "-:::-"
  232. + "Http Accept = " + request.ServerVariables["HTTP_ACCEPT"] + "-:::-"
  233. + "Language = " + request.ServerVariables["HTTP_ACCEPT_LANGUAGE"];
  234. return str;
  235. }
  236. public static string ReadQueryString(string key, string defVal)
  237. {
  238. return HttpContext.Current.Request.QueryString[key] ?? defVal;
  239. }
  240. public static string ReadFormData(string key, string defval)
  241. {
  242. return HttpContext.Current.Request.Form[key] ?? defval;
  243. }
  244. public static void ClearSession(string key)
  245. {
  246. HttpContext.Current.Session[key] = "";
  247. }
  248. public static string GetUrlRoot()
  249. {
  250. return ReadWebConfig("urlRoot");
  251. }
  252. public static string FormatData(string data, string dataType)
  253. {
  254. if (string.IsNullOrEmpty(data))
  255. return "&nbsp;";
  256. if (data == "-")
  257. return data;
  258. if (dataType == "D")
  259. {
  260. DateTime d;
  261. DateTime.TryParse(data, out d);
  262. return d.Year + "-" + d.Month.ToString("00") + "-" + d.Day.ToString("00");
  263. }
  264. if (dataType == "DT")
  265. {
  266. DateTime t;
  267. DateTime.TryParse(data, out t);
  268. return t.Year + "-" + t.Month.ToString("00") + "-" + t.Day.ToString("00") + " " + t.Hour.ToString("00") + ":" + t.Minute.ToString("00");
  269. }
  270. if (dataType == "M")
  271. {
  272. decimal m;
  273. decimal.TryParse(data, out m);
  274. return m.ToString("N");
  275. }
  276. return data;
  277. }
  278. public static void AlertMessage(Page page, string msg)
  279. {
  280. CallBackJs1(page, "Alert Message", "alert(\"" + FilterMessageForJs(msg) + "\");");
  281. }
  282. public static void CallBackJs1(Page page, String scriptName, string functionName)
  283. {
  284. ScriptManager.RegisterStartupScript(page, page.GetType(), scriptName, functionName, true);
  285. }
  286. public static string GetLogoutPage()
  287. {
  288. return GetUrlRoot() + "/Logout.aspx";
  289. }
  290. public static string GetAuthenticationPage()
  291. {
  292. return GetUrlRoot() + "/Authentication.aspx";
  293. }
  294. public static string GetAgent()
  295. {
  296. return ReadSession("agent", "");
  297. }
  298. public static string DataTableToJson(DataTable table)
  299. {
  300. if (table == null)
  301. return "";
  302. var list = new List<Dictionary<string, object>>();
  303. foreach (DataRow row in table.Rows)
  304. {
  305. var dict = new Dictionary<string, object>();
  306. foreach (DataColumn col in table.Columns)
  307. {
  308. dict[col.ColumnName] = string.IsNullOrEmpty(row[col].ToString()) ? "" : row[col];
  309. }
  310. list.Add(dict);
  311. }
  312. var serializer = new JavaScriptSerializer();
  313. string json = serializer.Serialize(list);
  314. return json;
  315. }
  316. public static string GetUserType()
  317. {
  318. var userType = ReadSession("userType", "");
  319. //WriteSession("lastActiveTS", DateTime.Now.ToString());
  320. return userType;
  321. }
  322. public static string GetAgentId()
  323. {
  324. return ReadSession("agentId", "");
  325. }
  326. public static string GetCustomerFilePath()
  327. {
  328. return ReadWebConfig("customerDocPath");
  329. }
  330. public static string GetAppRoot()
  331. {
  332. return ReadWebConfig("root");
  333. }
  334. public static string GetCountryId()
  335. {
  336. return ReadSession("countryId", "");
  337. }
  338. public static string GetSettlingAgent()
  339. {
  340. return ReadSession("settlingAgent", "");
  341. }
  342. public static string GetSuperAgent()
  343. {
  344. return ReadSession("superAgent", "");
  345. }
  346. public static string GetBranch()
  347. {
  348. return ReadSession("branch", "");
  349. }
  350. public static void CallJSFunction(Page page, string functionName)
  351. {
  352. ScriptManager.RegisterStartupScript(page, page.GetType(), "cb", functionName, true);
  353. }
  354. public static string UploadSignatureImage(string imageData, string registerDate, string membershipId, string customerId)
  355. {
  356. string errorCode = "1";
  357. try
  358. {
  359. string fileExtension = ".png";
  360. string fileName = customerId + "_signature_" + DateTime.Now.Hour.ToString() + DateTime.Now.Millisecond.ToString() + "_" + registerDate.Replace("-", "_") + fileExtension;
  361. string path = GetCustomerFilePath() + "CustomerDocument\\" + registerDate.Replace("-", "\\") + "\\" + membershipId;
  362. if (!Directory.Exists(path))
  363. Directory.CreateDirectory(path);
  364. string fName = path + "\\" + fileName;
  365. using (FileStream fs = new FileStream(fName, FileMode.CreateNew))
  366. {
  367. using (BinaryWriter bw = new BinaryWriter(fs))
  368. {
  369. byte[] data = Convert.FromBase64String(imageData);
  370. bw.Write(data);
  371. bw.Close();
  372. }
  373. }
  374. errorCode = fileName;
  375. }
  376. catch (Exception ex)
  377. {
  378. errorCode = "1";
  379. }
  380. return errorCode;
  381. }
  382. public static string UploadDocument(HttpPostedFile doc, string customerId, string documentTypeName, string membershipId, string registeredDate, out string fileType)
  383. {
  384. fileType = "";
  385. string fName = "";
  386. string documentExtension = "";
  387. try
  388. {
  389. string root = ConfigurationManager.AppSettings["filePath"];
  390. var saveFileLocation = root + "\\doc\\tmp\\";
  391. if (!Directory.Exists(saveFileLocation))
  392. Directory.CreateDirectory(saveFileLocation);
  393. fileType = doc.ContentType;
  394. string fileExtension = new FileInfo(doc.FileName).Extension;
  395. if (documentTypeName.ToLower() == "other_document" || documentTypeName.ToLower() == "registration_form" || documentTypeName.ToLower() == "beneficiary_registration_form")
  396. {
  397. documentExtension = GetStatic.ReadWebConfig("customerDocFileExtensionForOtherDoc", "");
  398. }
  399. else
  400. {
  401. documentExtension = GetStatic.ReadWebConfig("customerDocFileExtension", "");
  402. }
  403. if (documentExtension.ToLower().Contains(fileExtension.ToLower()))
  404. {
  405. string fileName = documentTypeName + "_" + GetStatic.GetDateTimeStamp() + "_" + GetStatic.GetUser() + fileExtension;
  406. string path = GetStatic.GetCustomerFilePath() + "CustomerDocument\\" + registeredDate.Replace("-", "\\") + "\\" + membershipId;
  407. if (!Directory.Exists(path))
  408. Directory.CreateDirectory(path);
  409. //doc.SaveAs(path + "/" + fileName);
  410. //fName = fileName;
  411. var original_imagePath = root + "\\doc\\tmp\\" + "_org_" + fileName;
  412. string saved_file_name = path + "\\" + fileName;
  413. doc.SaveAs(original_imagePath);
  414. System.IO.FileInfo fi = new System.IO.FileInfo(original_imagePath);
  415. doc.SaveAs(path + "/" + fileName);
  416. fName = fileName;
  417. }
  418. else
  419. {
  420. fName = "notValid";
  421. }
  422. }
  423. catch (Exception ex)
  424. {
  425. fName = "";
  426. }
  427. return fName;
  428. }
  429. public static string NumberToWord(string data)
  430. {
  431. var str = data.Split('.');
  432. int number = Convert.ToInt32(str[0]);
  433. int dec = 0;
  434. if (str.Length > 1)
  435. dec = Convert.ToInt32(str[1].Substring(0, 2));
  436. if (number == 0) return "Zero";
  437. if (number == -2147483648)
  438. return
  439. "Minus Two Hundred and Fourteen Crore Seventy Four Lakh Eighty Three Thousand Six Hundred and Forty Eight Rupees Fifty Paisa";
  440. int[] num = new int[4];
  441. int first = 0;
  442. int u, h, t;
  443. StringBuilder sb = new System.Text.StringBuilder();
  444. if (number < 0)
  445. {
  446. sb.Append("Minus ");
  447. number = -number;
  448. }
  449. string[] words0 = {
  450. "", "One ", "Two ", "Three ", "Four ",
  451. "Five ", "Six ", "Seven ", "Eight ", "Nine "
  452. };
  453. string[] words1 = {
  454. "Ten ", "Eleven ", "Twelve ", "Thirteen ", "Fourteen ",
  455. "Fifteen ", "Sixteen ", "Seventeen ", "Eighteen ", "Nineteen "
  456. };
  457. string[] words2 = {
  458. "Twenty ", "Thirty ", "Forty ", "Fifty ", "Sixty ",
  459. "Seventy ", "Eighty ", "Ninety "
  460. };
  461. string[] words3 = { "Thousand ", "Lakh ", "Crore " };
  462. num[0] = number % 1000; // units
  463. num[1] = number / 1000;
  464. num[2] = number / 100000;
  465. num[1] = num[1] - 100 * num[2]; // thousands
  466. num[3] = number / 10000000; // crores
  467. num[2] = num[2] - 100 * num[3]; // lakhs
  468. for (int i = 3; i > 0; i--)
  469. {
  470. if (num[i] != 0)
  471. {
  472. first = i;
  473. break;
  474. }
  475. }
  476. for (int i = first; i >= 0; i--)
  477. {
  478. if (num[i] == 0) continue;
  479. u = num[i] % 10; // ones
  480. t = num[i] / 10;
  481. h = num[i] / 100; // hundreds
  482. t = t - 10 * h; // tens
  483. if (h > 0) sb.Append(words0[h] + "Hundred ");
  484. if (u > 0 || t > 0)
  485. {
  486. if (h > 0 && i == 0) sb.Append("and ");
  487. if (t == 0)
  488. sb.Append(words0[u]);
  489. else if (t == 1)
  490. sb.Append(words1[u]);
  491. else
  492. sb.Append(words2[t - 2] + words0[u]);
  493. }
  494. if (i != 0) sb.Append(words3[i - 1]);
  495. }
  496. //sb.Append(" Rupees ");
  497. int d1 = dec / 10;
  498. int d2 = dec % 10;
  499. if (d1 == 0)
  500. sb.Append(words0[d1]);
  501. else if (d1 == 1)
  502. sb.Append(words1[d2]);
  503. else
  504. sb.Append(words2[d1 - 2] + words0[d2]);
  505. //if (dec > 0)
  506. // sb.Append(" Paisa");
  507. return sb.ToString().TrimEnd() + " only";
  508. }
  509. public static String ShowWithoutDecimal(String strVal)
  510. {
  511. if (strVal != "")
  512. return String.Format("{0:0,0}", double.Parse(strVal));
  513. else
  514. return strVal;
  515. }
  516. public static String ShowDecimal(String strVal)
  517. {
  518. if (strVal != "")
  519. return String.Format("{0:0,0.00}", double.Parse(strVal));
  520. else
  521. return strVal;
  522. }
  523. public static string GetSessionId()
  524. {
  525. return HttpContext.Current.Session.SessionID;
  526. }
  527. public static string ParseResultJsPrint(DbResult dbResult)
  528. {
  529. return dbResult.ErrorCode + "-:::-" + dbResult.Msg.Replace("'", "").Replace("<br/>", "").Replace(System.Environment.NewLine, "") + "-:::-" + dbResult.Id;
  530. }
  531. public static string PutRedBackGround(string mes)
  532. {
  533. return "<span style = \"background-color : red\">" + mes + "</span>";
  534. }
  535. public static string DataSetToJSON(DataSet ds)
  536. {
  537. ArrayList root = new ArrayList();
  538. List<Dictionary<string, object>> table;
  539. Dictionary<string, object> data;
  540. foreach (DataTable dt in ds.Tables)
  541. {
  542. table = new List<Dictionary<string, object>>();
  543. foreach (DataRow dr in dt.Rows)
  544. {
  545. data = new Dictionary<string, object>();
  546. foreach (DataColumn col in dt.Columns)
  547. {
  548. data.Add(col.ColumnName, dr[col]);
  549. }
  550. table.Add(data);
  551. }
  552. root.Add(table);
  553. }
  554. JavaScriptSerializer serializer = new JavaScriptSerializer();
  555. return serializer.Serialize(root);
  556. }
  557. public static DataTable ToDataTable<T>(this IList<T> data)
  558. {
  559. PropertyDescriptorCollection props =
  560. TypeDescriptor.GetProperties(typeof(T));
  561. DataTable table = new DataTable();
  562. for (int i = 0; i < props.Count; i++)
  563. {
  564. PropertyDescriptor prop = props[i];
  565. table.Columns.Add(prop.Name, prop.PropertyType);
  566. }
  567. object[] values = new object[props.Count];
  568. foreach (T item in data)
  569. {
  570. for (int i = 0; i < values.Length; i++)
  571. {
  572. values[i] = props[i].GetValue(item);
  573. }
  574. table.Rows.Add(values);
  575. }
  576. return table;
  577. }
  578. }
  579. }