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.

392 lines
15 KiB

  1. using Swift.API.MapAPIData;
  2. using Swift.DAL.APIDataMappingDao;
  3. using Swift.DAL.SwiftDAL;
  4. using Swift.web.Library;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Data;
  8. using System.Data.OleDb;
  9. using System.IO;
  10. using System.Linq;
  11. using System.Text;
  12. using System.Threading;
  13. using System.Windows.Forms;
  14. using System.Xml.Serialization;
  15. using static Swift.API.Common.MapAPIData.APIBankModel;
  16. namespace Swift.web.Remit.APIDataMapping.BankDataMapping
  17. {
  18. public partial class ManageBankData : System.Web.UI.Page
  19. {
  20. protected DownloadAPIData _map = new DownloadAPIData();
  21. protected APIMapping _dao = new APIMapping();
  22. protected FuzzyMatcher _match = new FuzzyMatcher();
  23. private readonly RemittanceLibrary _sl = new RemittanceLibrary();
  24. private const string ViewFunctionId = "20201800";
  25. protected void Page_Load(object sender, EventArgs e)
  26. {
  27. Authenticate();
  28. if (!IsPostBack)
  29. {
  30. ShowMasterData();
  31. }
  32. }
  33. private void Authenticate()
  34. {
  35. _sl.CheckAuthentication(ViewFunctionId);
  36. }
  37. protected void btnDownloadBank_Click(object sender, EventArgs e)
  38. {
  39. //string partner = GetPartner();
  40. //if (!string.IsNullOrEmpty(partner))
  41. //{
  42. // BankRequest request = new BankRequest()
  43. // {
  44. // CountryCode = GetCountry(),
  45. // ProviderId = partner,
  46. // SessionId = GetStatic.GetSessionId()
  47. // };
  48. // var response = _map.ThirdPartyApiGetDataOnlyNew<BankRequest, _BankResponse>(request, "api/v1/TP/bankList", "Data", "post");
  49. // if (response.ResponseCode == "0")
  50. // {
  51. // var sessionId = GetStatic.GetSessionId();
  52. // var xml = ObjectToXML(response.Data);
  53. // var res = _dao.SyncBank(GetStatic.GetUser(), xml, GetCountry(), request.ProviderId, "NPR", sessionId);
  54. // if (res.ErrorCode == "0")
  55. // {
  56. // LoadDataFromTemp(sessionId);
  57. // }
  58. // else
  59. // {
  60. // GetStatic.AlertMessage(this, res.Msg);
  61. // }
  62. // }
  63. //}
  64. }
  65. private List<string> GetMatchingData(DataRow item, DataTable dt)
  66. {
  67. int pcnt = 0;
  68. string stringToSearch = item["BANK_NAME"].ToString().Replace(".", "");
  69. string pattern = "";
  70. bool res = false;
  71. string md1 = "", md2 = "", md3 = "", ret1 = "", ret2 = "";
  72. List<string> list = new List<string>();
  73. //var masterData = item["BANK_NAME"].ToString().Replace(".", "").Split(' ');
  74. //md1 = SoundexA.Soundex(masterData[0]);
  75. //int matchCount = 0;
  76. //if(masterData.Length > 1)
  77. // md2 = SoundexA.Soundex(masterData[1]);
  78. //if (masterData.Length > 2)
  79. // md3 = SoundexA.Soundex(masterData[2]);
  80. foreach (DataRow dr in dt.Rows)
  81. {
  82. //var matchData = dr["BANK_NAME"].ToString().Replace(".", "").Split(' ');
  83. //foreach (var items in matchData)
  84. //{
  85. // string soundex = SoundexA.Soundex(items);
  86. // if (soundex == md1 || soundex == md2 || soundex == md3)
  87. // {
  88. // matchCount++;
  89. // }
  90. //}
  91. int lengthh = stringToSearch.Length;
  92. pattern = dr["BANK_NAME"].ToString().Replace(".", "");
  93. res = FuzzyMatcher.FuzzyMatch(stringToSearch, pattern, out pcnt);
  94. if (res == true && pcnt >= 0)
  95. {
  96. list.Add(dr["ROW_ID"].ToString());
  97. list.Add(dr["BANK_NAME"].ToString());
  98. break;
  99. }
  100. //matchCount = 0;
  101. }
  102. return list;
  103. }
  104. public string ObjectToXML(object input)
  105. {
  106. try
  107. {
  108. var stringwriter = new StringWriter();
  109. var serializer = new XmlSerializer(input.GetType());
  110. serializer.Serialize(stringwriter, input);
  111. return stringwriter.ToString();
  112. }
  113. catch (Exception ex)
  114. {
  115. if (ex.InnerException != null)
  116. ex = ex.InnerException;
  117. return "Could not convert: " + ex.Message;
  118. }
  119. }
  120. protected void ShowMasterData(string isAfterMap = "N")
  121. {
  122. detailsLabel.Text = "Mapping for: " + GetPartnerName() + " >> " + GetStatic.ReadQueryString("countryName", "") + " >> " + GetStatic.ReadQueryString("paymentTypeName", "");
  123. payoutPartner.Text = "Bank List For: " + GetPartnerName();
  124. if (!string.IsNullOrEmpty(GetCountry()) && !string.IsNullOrEmpty(GetPaymentMode()))
  125. {
  126. DataTable dt = null;
  127. if (isAfterMap == "N")
  128. {
  129. dt = GetStatic.ReadSessionAsTable("MasterDataList");
  130. //if (dt == null)
  131. //{
  132. dt = _dao.GetMasterDataList(GetStatic.GetUser(), GetCountry(), GetPaymentMode(), GetPartner(), GetNoOfRows());
  133. //}
  134. }
  135. else
  136. {
  137. dt = _dao.GetMasterDataList(GetStatic.GetUser(), GetCountry(), GetPaymentMode(), GetPartner(), GetNoOfRows());
  138. }
  139. if (dt != null || dt.Rows.Count > 0)
  140. {
  141. GetStatic.WriteSessionAsDataTable("MasterDataList", dt);
  142. PopulateMasterData(dt);
  143. }
  144. }
  145. }
  146. private void PopulateMasterData(DataTable dt)
  147. {
  148. //if (dt.Rows.Count == 0)
  149. //{
  150. // btnDownloadBank.Enabled = true;
  151. // btnLoadFromTemp.Enabled = false;
  152. // btnSaveMapping.Enabled = false;
  153. // btnShowMappedData.Disabled = true;
  154. //}
  155. StringBuilder sb = new StringBuilder();
  156. foreach (DataRow item in dt.Rows)
  157. {
  158. sb.AppendLine("<tr>");
  159. sb.AppendLine("<td>" + item["BANK_NAME"].ToString() + "</td>");
  160. sb.AppendLine("<td>" + item["JME_BANK_CODE"].ToString() + "</td>");
  161. sb.AppendLine("</tr>");
  162. }
  163. masterTableBody.InnerHtml = sb.ToString();
  164. }
  165. protected string GetCountry()
  166. {
  167. return GetStatic.ReadQueryString("country", "");
  168. }
  169. protected string GetPartner()
  170. {
  171. return GetStatic.ReadQueryString("partner", "");
  172. }
  173. protected string GetPartnerName()
  174. {
  175. return GetStatic.ReadQueryString("partnerName", "");
  176. }
  177. protected string GetCountryName()
  178. {
  179. return GetStatic.ReadQueryString("countryName", "");
  180. }
  181. protected string GetPaymentTypeName()
  182. {
  183. return GetStatic.ReadQueryString("paymentTypeName", "");
  184. }
  185. protected string GetPaymentMode()
  186. {
  187. return GetStatic.ReadQueryString("paymentType", "") == "" ? "0" : GetStatic.ReadQueryString("paymentType", "");
  188. }
  189. protected string GetNoOfRows()
  190. {
  191. return ddlNoOfBanks.SelectedValue;
  192. }
  193. protected void btnLoadFromTemp_Click(object sender, EventArgs e)
  194. {
  195. LoadDataFromTemp("");
  196. }
  197. protected void LoadDataFromTemp(string sessionId)
  198. {
  199. DataTable dt = _dao.GetMasterDownlodList(GetStatic.GetUser(), GetCountry(), GetPaymentMode(), GetPartner(), sessionId);
  200. if (dt.Rows.Count > 0 || dt != null)
  201. {
  202. StringBuilder sb = new StringBuilder();
  203. DataTable _masterData = _dao.GetMasterDataList(GetStatic.GetUser(), GetCountry(), GetPaymentMode(), GetPartner(), GetNoOfRows());
  204. foreach (DataRow item in _masterData.Rows)
  205. {
  206. var res = GetMatchingData(item, dt);
  207. sb.AppendLine("<tr>");
  208. if (res.Count > 0)
  209. {
  210. sb.AppendLine("<td>" + GetStatic.MakeAutoCompleteControl(item["MASTER_BANK_ID"].ToString(), "'category' : 'remit-mapBankData'", res[0], res[1] + " | " + res[0]) + "</td>");
  211. }
  212. else
  213. {
  214. sb.AppendLine("<td>" + GetStatic.MakeAutoCompleteControl(item["MASTER_BANK_ID"].ToString(), "'category' : 'remit-mapBankData'") + "</td>");
  215. }
  216. sb.AppendLine("<td>" + item["JME_BANK_CODE"].ToString() + "</td>");
  217. sb.AppendLine("</tr>");
  218. }
  219. tableBody.InnerHtml = sb.ToString();
  220. }
  221. }
  222. protected void btnSaveMapping_Click(object sender, EventArgs e)
  223. {
  224. DataTable dt = GetStatic.ReadSessionAsTable("MasterDataList");
  225. if (dt.Rows.Count == 0 || dt == null)
  226. {
  227. return;
  228. }
  229. StringBuilder sb = new StringBuilder("<root>");
  230. foreach (DataRow item in dt.Rows)
  231. {
  232. sb.AppendLine("<row");
  233. sb.Append(string.Format(" {0}=\"{1}\"", "MASTER_ID", item["MASTER_BANK_ID"].ToString()));
  234. sb.Append(string.Format(" {0}=\"{1}\"", "TEMP_ID", Request.Form[item["MASTER_BANK_ID"].ToString() + "_aValue"]));
  235. sb.Append(" />");
  236. }
  237. sb.Append("</root>");
  238. string xml = sb.ToString();
  239. _dao.SaveMappingData(GetStatic.GetUser(), xml);
  240. ShowMasterData("Y");
  241. LoadDataFromTemp("");
  242. }
  243. protected void ddlNoOfBanks_SelectedIndexChanged(object sender, EventArgs e)
  244. {
  245. ShowMasterData("Y");
  246. }
  247. protected void btnUploadBank_Click(object sender, EventArgs e)
  248. {
  249. string filePath = string.Empty;
  250. string fileExt = string.Empty;
  251. string partner = GetPartner();
  252. var sessionId = GetStatic.GetSessionId();
  253. if (!string.IsNullOrEmpty(partner))
  254. {
  255. filePath = uploadedFile.Text; //get the path of the file
  256. fileExt = Path.GetExtension(filePath); //get the file extension
  257. if (fileExt.CompareTo(".xls") == 0 || fileExt.CompareTo(".xlsx") == 0)
  258. {
  259. try
  260. {
  261. DataSet dtExcel = new DataSet();
  262. dtExcel = ReadExcel(filePath, fileExt); //read excel file
  263. var xml = ObjectToXML(dtExcel.Tables[0]);
  264. //Compare Jme BankName and partner bankName
  265. DbResult matchedResult = CompareBankName(xml);
  266. if (matchedResult.ErrorCode.ToString() != "1")
  267. {
  268. var res = _dao.SyncBankNew(GetStatic.GetUser(), xml, GetCountryName(), partner, "NPR", sessionId, GetPaymentMode());
  269. if (res.ErrorCode == "0")
  270. {
  271. ShowMasterData();
  272. LoadDataFromTemp(sessionId);
  273. }
  274. else
  275. {
  276. GetStatic.AlertMessage(this, res.Msg);
  277. }
  278. }
  279. else
  280. {
  281. GetStatic.AlertMessage(this.Page, matchedResult.Msg.ToString());
  282. }
  283. }
  284. catch (Exception ex)
  285. {
  286. MessageBox.Show(ex.Message.ToString());
  287. }
  288. }
  289. else
  290. {
  291. MessageBox.Show("Please choose .xls or .xlsx file only.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Error); //custom messageBox to show error
  292. }
  293. }
  294. }
  295. protected void chooseFile_Click(object sender, EventArgs e)
  296. {
  297. Thread t = new Thread((ThreadStart)(() =>
  298. {
  299. OpenFileDialog saveFileDialog1 = new OpenFileDialog();
  300. saveFileDialog1.Filter = "Excel Files|*.xls;*.xlsx;*.xlsm";
  301. saveFileDialog1.FilterIndex = 2;
  302. saveFileDialog1.RestoreDirectory = true;
  303. if (saveFileDialog1.ShowDialog() == DialogResult.OK)
  304. {
  305. uploadedFile.Text = saveFileDialog1.FileName;
  306. }
  307. }));
  308. // Run your code from a thread that joins the STA Thread
  309. t.SetApartmentState(ApartmentState.STA);
  310. t.Start();
  311. t.Join();
  312. }
  313. public DataSet ReadExcel(string fileName, string fileExt)
  314. {
  315. string conn = string.Empty;
  316. DataSet dtexcel = new DataSet();
  317. if (fileExt.CompareTo(".xls") == 0)
  318. conn = @"provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + fileName + ";Extended Properties='Excel 8.0;HRD=Yes;IMEX=1';"; //for below excel 2007
  319. else
  320. conn = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + fileName + ";Extended Properties=Excel 12.0;"; //for above excel 2007
  321. using (OleDbConnection con = new OleDbConnection(conn))
  322. {
  323. try
  324. {
  325. OleDbDataAdapter oleAdpt = new OleDbDataAdapter("select * from [Sheet1$]", con); //here we read data from sheet1
  326. oleAdpt.Fill(dtexcel); //fill excel data into dataTable
  327. }
  328. catch (Exception ex) { }
  329. }
  330. return dtexcel;
  331. }
  332. public DbResult CompareBankName(string xml)
  333. {
  334. DbResult result = _dao.CompareBankName(GetStatic.GetUser(), xml);
  335. return result;
  336. }
  337. public class MyObj
  338. {
  339. public string Name { get; set; }
  340. public string ID { get; set; }
  341. }
  342. protected void btnLoadFromBankList_Click(object sender, EventArgs e)
  343. {
  344. DbResult res = _dao.LoadFromBankList(GetStatic.GetUser(), GetCountry(), GetPaymentMode(), GetPartner(), "");
  345. if (res.ErrorCode == "0")
  346. {
  347. LoadDataFromTemp("");
  348. }
  349. else
  350. {
  351. GetStatic.AlertMessage(this.Page, res.Msg);
  352. }
  353. }
  354. }
  355. }