SupportController.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. using DocumentFormat.OpenXml.Packaging;
  2. using Microsoft.AspNetCore.Hosting;
  3. using Microsoft.AspNetCore.Http;
  4. using Microsoft.AspNetCore.Mvc;
  5. using Microsoft.AspNetCore.StaticFiles;
  6. using NPOI.HSSF.UserModel;
  7. using NPOI.SS.UserModel;
  8. using NPOI.XSSF.UserModel;
  9. using SuperCms.Extensions;
  10. using SuperCms.Models;
  11. using Syncfusion.DocIO;
  12. using Syncfusion.DocIO.DLS;
  13. using System;
  14. using System.Collections.Generic;
  15. using System.Globalization;
  16. using System.IO;
  17. using System.IO.Compression;
  18. using System.Linq;
  19. using System.Net;
  20. using System.Net.Http;
  21. using System.Net.Http.Headers;
  22. using System.Text;
  23. using System.Threading.Tasks;
  24. using System.Xml.Linq;
  25. using BorderStyle = Syncfusion.DocIO.DLS.BorderStyle;
  26. using HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment;
  27. namespace SuperCms.Controllers
  28. {
  29. public class SupportController : BaseController
  30. {
  31. private static readonly log4net.ILog log = log4net.LogManager.GetLogger(typeof(Program));
  32. private IHostingEnvironment _hostingEnvironment;
  33. OracleController oracle;
  34. public SupportController(IHostingEnvironment hostingEnvironment)
  35. {
  36. _hostingEnvironment = hostingEnvironment;
  37. }
  38. public IActionResult Index(String tableType, String actionFile)
  39. {
  40. try
  41. {
  42. if (!CheckAuthToken())
  43. {
  44. return Redirect("/Home/Login");
  45. }
  46. //UtilsController.PaymentConverter(null);
  47. SupportViewModel model = new SupportViewModel();
  48. model.typeTable = tableType != null ? tableType : UtilsController.Constant.PAYMENT_FILE;
  49. model.actionFile = actionFile != null ? actionFile : UtilsController.Constant.SHOW_FILE;
  50. // get file to show
  51. return View("Index", model);
  52. }
  53. catch (Exception ex)
  54. {
  55. log.Error("Exception: ", ex);
  56. return Redirect("/Home");
  57. }
  58. }
  59. public ActionResult Download(String tableType)
  60. {
  61. try
  62. {
  63. String zipPath = "";
  64. if (tableType == UtilsController.Constant.PAYMENT_FILE)
  65. {
  66. string startPath = "./Files/Payments/" + DateTime.Now.ToString("dd_MM_yyyy");
  67. zipPath = "./Files/Payments/" + "Payments_" + DateTime.Now.ToString("dd_MM_yyyy") + ".zip";
  68. if (UtilsController.CheckFile(zipPath))
  69. {
  70. // delete file
  71. UtilsController.DeleteFile(zipPath);
  72. }
  73. ZipFile.CreateFromDirectory(startPath, zipPath);
  74. }
  75. else
  76. {
  77. string startPath = "./Files/Invoices/" + DateTime.Now.ToString("dd_MM_yyyy");
  78. zipPath = "./Files/Invoices/" + "Invoices_" + DateTime.Now.ToString("dd_MM_yyyy") + ".zip";
  79. if (UtilsController.CheckFile(zipPath))
  80. {
  81. // delete file
  82. UtilsController.DeleteFile(zipPath);
  83. }
  84. ZipFile.CreateFromDirectory(startPath, zipPath);
  85. }
  86. var provider = new FileExtensionContentTypeProvider();
  87. if (!provider.TryGetContentType(zipPath, out var contentType))
  88. {
  89. contentType = "application/octet-stream";
  90. }
  91. var bytes = UtilsController.GetFileByte(zipPath);
  92. return File(bytes, contentType, Path.GetFileName(zipPath));
  93. }
  94. catch (Exception ex)
  95. {
  96. log.Error("Exception: ", ex);
  97. return null;
  98. }
  99. }
  100. [ValidateAntiForgeryToken]
  101. public ActionResult UploadAction(String typeTable)
  102. {
  103. try
  104. {
  105. if (!CheckAuthToken())
  106. {
  107. return Redirect("/Home/Login");
  108. }
  109. IFormFile file = Request.Form.Files[0];
  110. string folderName = "UploadExcel";
  111. string webRootPath = _hostingEnvironment.WebRootPath;
  112. string newPath = Path.Combine(webRootPath, folderName);
  113. StringBuilder sb = new StringBuilder();
  114. if (!Directory.Exists(newPath))
  115. {
  116. Directory.CreateDirectory(newPath);
  117. }
  118. if (file.Length > 0)
  119. {
  120. string sFileExtension = Path.GetExtension(file.FileName).ToLower();
  121. ISheet sheet;
  122. string fullPath = Path.Combine(newPath, file.FileName);
  123. using (var stream = new FileStream(fullPath, FileMode.Create))
  124. {
  125. file.CopyTo(stream);
  126. stream.Position = 0;
  127. if (sFileExtension == ".xls")
  128. {
  129. HSSFWorkbook hssfwb = new HSSFWorkbook(stream); //This will read the Excel 97-2000 formats
  130. sheet = hssfwb.GetSheetAt(0); //get first sheet from workbook
  131. }
  132. else
  133. {
  134. XSSFWorkbook hssfwb = new XSSFWorkbook(stream); //This will read 2007 Excel format
  135. sheet = hssfwb.GetSheetAt(0); //get first sheet from workbook
  136. }
  137. IRow headerRow = sheet.GetRow(0); //Get Header Row
  138. int cellCount = headerRow.LastCellNum;
  139. sb.Append("<table class='table table-bordered'><tr>");
  140. for (int j = 0; j < cellCount; j++)
  141. {
  142. NPOI.SS.UserModel.ICell cell = headerRow.GetCell(j);
  143. if (cell == null || string.IsNullOrWhiteSpace(cell.ToString())) continue;
  144. sb.Append("<th>" + cell.ToString() + "</th>");
  145. }
  146. sb.Append("</tr>");
  147. sb.AppendLine("<tr>");
  148. List<Contract> contracts = new List<Contract>();
  149. for (int i = (sheet.FirstRowNum + 1); i <= sheet.LastRowNum; i++) //Read Excel File
  150. {
  151. IRow row = sheet.GetRow(i);
  152. if (row == null)
  153. {
  154. continue;
  155. }
  156. if (row.Cells.All(d => d.CellType == CellType.Blank))
  157. {
  158. continue;
  159. };
  160. for (int j = row.FirstCellNum; j < cellCount; j++)
  161. {
  162. if (row.GetCell(j) != null)
  163. {
  164. sb.Append("<td>" + row.GetCell(j).ToString() + "</td>");
  165. }
  166. else
  167. {
  168. sb.Append("<td> </td>");
  169. }
  170. }
  171. sb.AppendLine("</tr>");
  172. Contract contract = new Contract();
  173. contract.id = row.GetCell(0) != null ? row.GetCell(0).ToString() : "";
  174. contract.telco = row.GetCell(13) != null ? row.GetCell(13).ToString() : "";
  175. contract.syntaxTelco = row.GetCell(2) != null ? row.GetCell(2).ToString() : "";
  176. contract.company = row.GetCell(1) != null ? row.GetCell(1).ToString() : "";
  177. contract.service = row.GetCell(3) != null ? row.GetCell(3).ToString() : "";
  178. contract.contractCode = row.GetCell(4) != null ? row.GetCell(4).ToString() : "";
  179. contract.address = row.GetCell(8) != null ? row.GetCell(8).ToString() : "";
  180. contract.account = row.GetCell(9) != null ? row.GetCell(9).ToString() : "";
  181. contract.swiftCode = row.GetCell(11) != null ? row.GetCell(11).ToString() : "";
  182. contract.time = row.GetCell(5) != null ? row.GetCell(5).ToString() : "";
  183. contract.money = row.GetCell(6) != null ? row.GetCell(6).ToString() : "";
  184. contract.beneficiary = row.GetCell(7) != null ? row.GetCell(7).ToString() : "";
  185. contract.syntaxConpany = row.GetCell(12) != null ? row.GetCell(12).ToString() : "";
  186. contract.bank = row.GetCell(10) != null ? row.GetCell(10).ToString() : "";
  187. contract.addressTelco = row.GetCell(14) != null ? row.GetCell(14).ToString() : "";
  188. contract.taxCode = row.GetCell(15) != null ? row.GetCell(15).ToString() : "";
  189. contracts.Add(contract);
  190. }
  191. HttpContext.Session.SetComplexData("contractsImport", contracts);
  192. sb.Append("</table>");
  193. }
  194. }
  195. return this.Content(sb.ToString());
  196. }
  197. catch (Exception ex)
  198. {
  199. log.Error("Exception: ", ex);
  200. return Redirect("/Home");
  201. }
  202. }
  203. [ValidateAntiForgeryToken]
  204. public JsonResult ImportAction(String typeTable, String timeNow)
  205. {
  206. if (!CheckAuthToken())
  207. {
  208. return Json(new
  209. {
  210. code = UtilsController.Constant.ERROR,
  211. href = "/Home/Login"
  212. });
  213. }
  214. try
  215. {
  216. bool check = false;
  217. List<Contract> contracts = HttpContext.Session.GetComplexData<List<Contract>>("contractsImport");
  218. List<Contract> listITL = new List<Contract>();
  219. for (int i = 0; i < contracts.Count; i++)
  220. {
  221. if (typeTable == UtilsController.Constant.PAYMENT_FILE)
  222. {
  223. // following by telco
  224. if (contracts[i].syntaxTelco == "NATCOM")
  225. {
  226. check = UtilsController.PaymentNatcomAllDumpt(contracts[i]);
  227. }
  228. else
  229. {
  230. check = UtilsController.PaymentDumpt(contracts[i]);
  231. }
  232. }
  233. else
  234. {
  235. if (contracts[i].syntaxTelco == "NATCOM")
  236. {
  237. check = UtilsController.InvoiceNatcomInterlandDumpt(contracts[i]);
  238. }
  239. else
  240. {
  241. if (contracts[i].syntaxConpany == "GTS")
  242. {
  243. check = UtilsController.InvoiceGTSDumpt(contracts[i]);
  244. }
  245. else if (contracts[i].syntaxConpany == "VTECH" || contracts[i].syntaxConpany == "VIETTECH")
  246. {
  247. check = UtilsController.InvoiceVTECHDumpt(contracts[i]);
  248. }
  249. else if (contracts[i].syntaxConpany == "ITL" || contracts[i].syntaxConpany == "INTERLAND")
  250. {
  251. check = UtilsController.InvoiceITLDumpt(contracts[i]);
  252. }
  253. else
  254. {
  255. log.Error("No match syntax company " + contracts[i].syntaxConpany);
  256. }
  257. }
  258. }
  259. }
  260. DateTime startTimeNowDateTime = DateTime.Parse(timeNow, CultureInfo.InvariantCulture);
  261. DateTime startdate = new DateTime(startTimeNowDateTime.Year, startTimeNowDateTime.Month, startTimeNowDateTime.Day, 0, 0, 0);
  262. DateTime enddate = new DateTime(startTimeNowDateTime.Year, startTimeNowDateTime.Month, startTimeNowDateTime.Day, 23, 59, 59);
  263. ////convert a doc file to html
  264. //UtilsController.ReadDocFileToHtml();
  265. HttpContext.Session.RemoveComplexData("contractsImport");
  266. return check ? Json(new
  267. {
  268. code = UtilsController.Constant.SUCCESS,
  269. message = "Import Successful",
  270. href = "/Import"
  271. }) : Json(new
  272. {
  273. code = UtilsController.Constant.ERROR,
  274. message = "Import Fails",
  275. href = "/Import"
  276. });
  277. }
  278. catch (Exception ex)
  279. {
  280. log.Error("Exp: " + ex);
  281. }
  282. return Json(new
  283. {
  284. code = UtilsController.Constant.ERROR,
  285. message = "Import Fails",
  286. href = "/Import"
  287. });
  288. }
  289. [ValidateAntiForgeryToken]
  290. public IActionResult DownloadTemplate()
  291. {
  292. String file = "./Files/Template/template.xlsx";
  293. var provider = new FileExtensionContentTypeProvider();
  294. if (!provider.TryGetContentType(file, out var contentType))
  295. {
  296. contentType = "application/octet-stream";
  297. }
  298. var bytes = UtilsController.GetFileByte(file);
  299. return File(bytes, contentType, Path.GetFileName(file));
  300. }
  301. }
  302. }