BaseController.cs 75 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.IO;
  5. using System.IO.Compression;
  6. using System.Linq;
  7. using System.Net;
  8. using System.Net.Http;
  9. using System.Security.Cryptography;
  10. using System.Threading.Tasks;
  11. using Microsoft.AspNetCore.Http;
  12. using Microsoft.AspNetCore.Mvc;
  13. using SuperCms.Database;
  14. using SuperCms.Models;
  15. using Syncfusion.Drawing;
  16. using Syncfusion.XlsIO;
  17. namespace SuperCms.Controllers
  18. {
  19. public class BaseController : Controller
  20. {
  21. private static readonly log4net.ILog log = log4net.LogManager.GetLogger(typeof(Program));
  22. protected string convertToDateTimeServer(String date)
  23. {
  24. // date:
  25. DateTime oDateFrom = DateTime.Parse(date);
  26. string hour = oDateFrom.Hour < 10 ? "0" + oDateFrom.Hour : oDateFrom.Hour.ToString();
  27. string minute = oDateFrom.Minute < 10 ? "0" + oDateFrom.Minute : oDateFrom.Minute.ToString();
  28. string second = oDateFrom.Second < 10 ? "0" + oDateFrom.Second : oDateFrom.Second.ToString();
  29. string month = oDateFrom.Month < 10 ? "0" + oDateFrom.Month : oDateFrom.Month.ToString();
  30. string day = oDateFrom.Day < 10 ? "0" + oDateFrom.Day : oDateFrom.Day.ToString();
  31. string fromCheck = month + "/" + day + "/" + oDateFrom.Year + " " + hour + ":" + minute + ":" + second;
  32. return fromCheck; //yyyy-MM-dd HH24:mm:ss
  33. }
  34. protected string getStartByMonthToString(int monthLeft)
  35. {
  36. DateTime now = DateTime.Now;
  37. DateTime time = new DateTime(now.Year, now.Month, 1, 0, 0, 0);
  38. DateTime timeB = time.AddMonths(monthLeft);
  39. return timeB.ToString("MM/dd/yyyy HH:mm:ss");
  40. }
  41. protected DateTime getStartByMonthToDate(int monthLeft)
  42. {
  43. DateTime now = DateTime.Now;
  44. DateTime time = new DateTime(now.Year, now.Month, 1, 0, 0, 0);
  45. DateTime timeB = time.AddMonths(monthLeft);
  46. return timeB;
  47. }
  48. protected DateTime getEndByMonthToDate(int monthLeft)
  49. {
  50. DateTime now = DateTime.Now;
  51. DateTime time = new DateTime(now.Year, now.Month, 1, 23, 59, 59);
  52. DateTime timeB = time.AddMonths(monthLeft + 1).AddDays(-1);
  53. return timeB;
  54. }
  55. protected DateTime getStartByMonthToDate(DateTime now, int monthLeft)
  56. {
  57. DateTime time = new DateTime(now.Year, now.Month, 1, 0, 0, 0);
  58. DateTime timeB = time.AddMonths(monthLeft);
  59. return timeB;
  60. }
  61. protected DateTime getEndByMonthToDate(DateTime now, int monthLeft)
  62. {
  63. DateTime time = new DateTime(now.Year, now.Month, 1, 23, 59, 59);
  64. DateTime timeB = time.AddMonths(monthLeft + 1).AddDays(-1);
  65. return timeB;
  66. }
  67. protected string getStartByDayToString()
  68. {
  69. DateTime now = DateTime.Now;
  70. DateTime date = new DateTime(now.Year, now.Month, now.Day, 0, 0, 0);
  71. //int dayOfWeek = (int)now.DayOfWeek;
  72. return date.AddDays(-(7)).ToString("MM/dd/yyyy HH:mm:ss");
  73. }
  74. protected String getEndByDayToString()
  75. {
  76. // mm/dd/yyyy hh:MM:ss PT
  77. //DateTime now = DateTime.Now;
  78. // convert to dd/mm/yyyy
  79. //return DateTime.Now.ToString("dd/MM/yyyy");
  80. DateTime now = DateTime.Now;
  81. DateTime date = new DateTime(now.Year, now.Month, now.Day, 23, 59, 59);
  82. return date.AddDays(0).ToString("MM/dd/yyyy HH:mm:ss");
  83. }
  84. protected DateTime getStartByDayToDate()
  85. {
  86. DateTime now = DateTime.Now;
  87. DateTime date = new DateTime(now.Year, now.Month, now.Day, 0, 0, 0);
  88. //int dayOfWeek = (int)now.DayOfWeek;
  89. return date.AddDays(-(7));
  90. }
  91. protected DateTime getEndByDayToDate()
  92. {
  93. // mm/dd/yyyy hh:MM:ss PT
  94. //DateTime now = DateTime.Now;
  95. // convert to dd/mm/yyyy
  96. //return DateTime.Now.ToString("dd/MM/yyyy");
  97. DateTime now = DateTime.Now;
  98. DateTime date = new DateTime(now.Year, now.Month, now.Day, 23, 59, 59);
  99. return date.AddDays(0);
  100. }
  101. protected List<DateTime> getRangeTime(DateTime start, DateTime end)
  102. {
  103. TimeSpan difference = end - start;
  104. List<DateTime> days = new List<DateTime>();
  105. //days.Add(DateTime.Now);
  106. for (int i = difference.Days; i >= 0; i--)
  107. {
  108. DateTime dateG = start.AddDays(i);
  109. DateTime date = new DateTime(dateG.Year, dateG.Month, dateG.Day, 0, 0, 0);
  110. days.Add(date);
  111. }
  112. return days;
  113. }
  114. protected List<DateTime> getRangeTimeForExport(DateTime start, DateTime end)
  115. {
  116. TimeSpan difference = end - start;
  117. List<DateTime> days = new List<DateTime>();
  118. days.Add(end);
  119. for (int i = difference.Days; i >= 0; i--)
  120. {
  121. DateTime dateG = start.AddDays(i);
  122. DateTime date = new DateTime(dateG.Year, dateG.Month, dateG.Day, 0, 0, 0);
  123. days.Add(date);
  124. }
  125. return days;
  126. }
  127. protected List<DateTime> getRangeMonth(DateTime start, DateTime end)
  128. {
  129. DateTime startD = new DateTime(start.Year, start.Month, 1);
  130. DateTime endD = new DateTime(end.Year, end.Month, 1);
  131. int diffMonths = (endD.Month + endD.Year * 12) - (startD.Month + startD.Year * 12);
  132. List<DateTime> days = new List<DateTime>();
  133. days.Add(end);
  134. for (int i = diffMonths; i >= 0; i--)
  135. {
  136. DateTime dateG = start.AddMonths(i);
  137. DateTime date;
  138. if (i == 0)
  139. {
  140. date = new DateTime(dateG.Year, dateG.Month, dateG.Day, 0, 0, 0);
  141. }
  142. else
  143. {
  144. date = new DateTime(dateG.Year, dateG.Month, 1, 0, 0, 0);
  145. }
  146. days.Add(date);
  147. }
  148. return days;
  149. }
  150. protected List<DistributedByTelcoAndCompany> distributedByTelcoAndCompany(ConnTelcos telcos, ConnCompanies companies, ConnConfigs services, int count, List<DistributedByTelcoAndCompany> result)
  151. {
  152. if (count >= services.data.Count)
  153. {
  154. return result;
  155. }
  156. else
  157. {
  158. ConnConfig service = services.data[count];
  159. if (service.companyID == null || service.telcoID == null)
  160. {
  161. return distributedByTelcoAndCompany(telcos, companies, services, count + 1, result);
  162. }
  163. else
  164. {
  165. log.Info(service.telcoID + " " + service.serviceName);
  166. List<DistributedByTelcoAndCompany> resultt = result;
  167. DistributedByTelcoAndCompany dis = result.Find(x => x.telco.id == service.telcoID);
  168. if (dis == null)
  169. {
  170. // add telco to result
  171. DistributedByTelcoAndCompany newEle = new DistributedByTelcoAndCompany();
  172. newEle.telco = telcos.data.Find(x => x.id == service.telcoID);
  173. // add company
  174. ConnCompany company = companies.data.Find(x => x.id == service.companyID);
  175. newEle.companies = new ConnCompanies();
  176. newEle.companies.data = new List<ConnCompany>();
  177. newEle.companies.data.Add(company);
  178. // add service
  179. newEle.services = new ConnConfigs();
  180. newEle.services.data = new List<ConnConfig>();
  181. newEle.services.data.Add(service);
  182. result.Add(newEle);
  183. return distributedByTelcoAndCompany(telcos, companies, services, count + 1, result);
  184. }
  185. else
  186. {
  187. // telco existed, get telco
  188. DistributedByTelcoAndCompany disres = result.Find(x => x.telco.id == service.telcoID);
  189. ConnTelco telco = disres.telco;
  190. // check company existed ???
  191. ConnCompany com = disres.companies.data.Find(x => x.id == service.companyID);
  192. if (com == null)
  193. {
  194. // add company
  195. ConnCompany company = companies.data.Find(x => x.id == service.companyID);
  196. result.Where(S => S.telco.id == service.telcoID).Select(S => { S.companies.data.Add(company); return S; }).ToList();
  197. }
  198. // check service existed ???
  199. ConnConfig ser = disres.services.data.Find(x => x.id == service.id);
  200. if (ser == null)
  201. {
  202. // add company
  203. result.Where(S => S.telco.id == service.telcoID).Select(S => { S.services.data.Add(service); return S; }).ToList();
  204. }
  205. return distributedByTelcoAndCompany(telcos, companies, services, count + 1, result);
  206. }
  207. }
  208. }
  209. }
  210. protected IActionResult ToExcelStock(List<DateTime> listdate, Dictionary<ConnCkNganh, Dictionary<DateTime, ConnCkData>> distributionData)
  211. {
  212. try
  213. {
  214. using (ExcelEngine excelEngine = new ExcelEngine())
  215. {
  216. IApplication application = excelEngine.Excel;
  217. application.DefaultVersion = ExcelVersion.Excel2013;
  218. IWorkbook workbook = application.Workbooks.Create(1);
  219. IWorksheet worksheet = workbook.Worksheets[0];
  220. List<RowAttributed> rowAttributedList = new List<RowAttributed>();
  221. int rowCount = 1;
  222. // for header
  223. worksheet.Range["A1"].Text = "NO";
  224. worksheet.Range["B1"].Text = "NGÀNH";
  225. worksheet.Range["C1"].Text = "CHỈ SỐ";
  226. for (int d = 0; d < listdate.Count; d++)
  227. {
  228. worksheet.Range[UtilsController.GetExcelColumnName(4 + d * 2) + rowCount.ToString()].Text = listdate[d].ToString("dd/MM");
  229. worksheet.Range[UtilsController.GetExcelColumnName(5 + d * 2) + rowCount.ToString()].Text = "%";
  230. }
  231. rowCount++;
  232. List<double> listTongGiaTri = new List<double>();
  233. List<double> listTongKhoiLuong = new List<double>();
  234. List<double> listTongThayDoi = new List<double>();
  235. List<double> listTongMua = new List<double>();
  236. List<double> listTongBan = new List<double>();
  237. for (int d = 0; d < listdate.Count; d++)
  238. {
  239. // tong sum
  240. double tongGiaTri = 0.0;
  241. double tongThayDoi = 0.0;
  242. double tongKhoiLuong = 0.0;
  243. double tongMua = 0.0;
  244. double tongBan = 0.0;
  245. for (int k = 0; k < distributionData.Count; k++)
  246. {
  247. Dictionary<DateTime, ConnCkData> dataDateList = distributionData.ElementAt(k).Value;
  248. ConnCkData ckData = dataDateList.ElementAt(d).Value;
  249. if (ckData != null)
  250. {
  251. tongBan += Decimal.ToDouble(ckData.TnnBan ?? 0);
  252. tongMua += Decimal.ToDouble(ckData.TnnMua ?? 0);
  253. tongKhoiLuong += Decimal.ToDouble(ckData.KhoiLuong ?? 0);
  254. //tongThayDoi += Double.Parse(ckData.ThayDoi);
  255. tongGiaTri += Decimal.ToDouble(ckData.GiaTri ?? 0);
  256. }
  257. }
  258. listTongGiaTri.Add(tongGiaTri);
  259. listTongKhoiLuong.Add(tongKhoiLuong);
  260. listTongMua.Add(tongMua);
  261. listTongBan.Add(tongBan);
  262. //listTongThayDoi.Add(tongThayDoi);
  263. }
  264. for (int i = 0; i < distributionData.Count; i++)
  265. {
  266. ConnCkNganh nganh = distributionData.ElementAt(i).Key;
  267. worksheet.Range[UtilsController.GetExcelColumnName(1) + rowCount.ToString()].Text = (i + 1).ToString();
  268. rowAttributedList = rowHandler(rowAttributedList, new RowAttributed(UtilsController.GetExcelColumnName(1), rowCount.ToString(), true, null, Color.FromArgb(247, 220, 111), 1, 1, 1, 1, null));
  269. worksheet.Range[UtilsController.GetExcelColumnName(2) + rowCount.ToString()].Text = nganh.Name.ToString();
  270. rowAttributedList = rowHandler(rowAttributedList, new RowAttributed(UtilsController.GetExcelColumnName(2), rowCount.ToString(), true, null, Color.FromArgb(247, 220, 111), 1, 1, 1, 1, null));
  271. Dictionary<DateTime, ConnCkData> dataCkList = distributionData.ElementAt(i).Value;
  272. // chi so tang giam
  273. worksheet.Range[UtilsController.GetExcelColumnName(3) + rowCount.ToString()].Text = "Tăng / Giảm";
  274. rowAttributedList = rowHandler(rowAttributedList, new RowAttributed(UtilsController.GetExcelColumnName(3), rowCount.ToString(), true, null, Color.FromArgb(247, 220, 111), 1, 1, 1, 1, null));
  275. for (int j = 0; j < dataCkList.Count; j++)
  276. {
  277. ConnCkData dataCk = dataCkList.ElementAt(j).Value;
  278. rowAttributedList = rowHandler(rowAttributedList, new RowAttributed(UtilsController.GetExcelColumnName(j * 2 + 4), rowCount.ToString(), true, null, Color.FromArgb(247, 220, 111), 1, 1, 1, 1, null));
  279. rowAttributedList = rowHandler(rowAttributedList, new RowAttributed(UtilsController.GetExcelColumnName(j * 2 + 5), rowCount.ToString(), true, null, Color.FromArgb(247, 220, 111), 1, 1, 1, 1, null));
  280. if (dataCk != null)
  281. {
  282. worksheet.Range[UtilsController.GetExcelColumnName(j * 2 + 4) + rowCount.ToString()].Text = dataCk.TangGiam.ToString();
  283. }
  284. }
  285. rowCount++;
  286. // chi so tang giam
  287. worksheet.Range[UtilsController.GetExcelColumnName(3) + rowCount.ToString()].Text = "Thay đổi";
  288. for (int j = 0; j < dataCkList.Count; j++)
  289. {
  290. ConnCkData dataCk = dataCkList.ElementAt(j).Value;
  291. if (dataCk != null)
  292. {
  293. worksheet.Range[UtilsController.GetExcelColumnName(j * 2 + 4) + rowCount.ToString()].Text = dataCk.ThayDoi.ToString();
  294. }
  295. }
  296. rowCount++;
  297. // chi so tang giam
  298. worksheet.Range[UtilsController.GetExcelColumnName(3) + rowCount.ToString()].Text = "Khối lượng";
  299. for (int j = 0; j < dataCkList.Count; j++)
  300. {
  301. ConnCkData dataCk = dataCkList.ElementAt(j).Value;
  302. if (dataCk != null)
  303. {
  304. double percent = Math.Round(Decimal.ToDouble(dataCk.KhoiLuong ?? 0) * 100 / listTongKhoiLuong[j], 2);
  305. worksheet.Range[UtilsController.GetExcelColumnName(j * 2 + 4) + rowCount.ToString()].Text = dataCk.KhoiLuong.ToString();
  306. worksheet.Range[UtilsController.GetExcelColumnName(j * 2 + 5) + rowCount.ToString()].Text = percent.ToString() + "%";
  307. }
  308. }
  309. rowCount++;
  310. // chi so tang giam
  311. worksheet.Range[UtilsController.GetExcelColumnName(3) + rowCount.ToString()].Text = "Giá trị";
  312. for (int j = 0; j < dataCkList.Count; j++)
  313. {
  314. ConnCkData dataCk = dataCkList.ElementAt(j).Value;
  315. if (dataCk != null)
  316. {
  317. worksheet.Range[UtilsController.GetExcelColumnName(j * 2 + 4) + rowCount.ToString()].Text = dataCk.GiaTri.ToString();
  318. double percent = Math.Round(Decimal.ToDouble(dataCk.GiaTri ?? 0) * 100 / listTongGiaTri[j], 2);
  319. worksheet.Range[UtilsController.GetExcelColumnName(j * 2 + 4) + rowCount.ToString()].Text = dataCk.GiaTri.ToString();
  320. worksheet.Range[UtilsController.GetExcelColumnName(j * 2 + 5) + rowCount.ToString()].Text = percent.ToString() + "%";
  321. }
  322. }
  323. rowCount++;
  324. // chi so tang giam
  325. worksheet.Range[UtilsController.GetExcelColumnName(3) + rowCount.ToString()].Text = "Mua";
  326. for (int j = 0; j < dataCkList.Count; j++)
  327. {
  328. ConnCkData dataCk = dataCkList.ElementAt(j).Value;
  329. if (dataCk != null)
  330. {
  331. double percent = Math.Round(Decimal.ToDouble(dataCk.TnnMua ?? 0) * 100 / listTongMua[j], 2);
  332. worksheet.Range[UtilsController.GetExcelColumnName(j * 2 + 4) + rowCount.ToString()].Text = dataCk.TnnMua.ToString();
  333. worksheet.Range[UtilsController.GetExcelColumnName(j * 2 + 5) + rowCount.ToString()].Text = percent.ToString() + "%";
  334. }
  335. }
  336. rowCount++;
  337. // chi so tang giam
  338. worksheet.Range[UtilsController.GetExcelColumnName(3) + rowCount.ToString()].Text = "Bán";
  339. for (int j = 0; j < dataCkList.Count; j++)
  340. {
  341. ConnCkData dataCk = dataCkList.ElementAt(j).Value;
  342. if (dataCk != null)
  343. {
  344. double percent = Math.Round(Decimal.ToDouble(dataCk.TnnBan ?? 0) * 100 / listTongBan[j], 2);
  345. worksheet.Range[UtilsController.GetExcelColumnName(j * 2 + 4) + rowCount.ToString()].Text = dataCk.TnnBan.ToString();
  346. worksheet.Range[UtilsController.GetExcelColumnName(j * 2 + 5) + rowCount.ToString()].Text = percent.ToString() + "%";
  347. }
  348. }
  349. rowCount++;
  350. }
  351. // TONG
  352. worksheet.Range[UtilsController.GetExcelColumnName(1) + rowCount.ToString()].Text = (distributionData.Count).ToString();
  353. rowAttributedList = rowHandler(rowAttributedList, new RowAttributed(UtilsController.GetExcelColumnName(1), rowCount.ToString(), true, null, Color.FromArgb(247, 220, 111), 1, 1, 1, 1, null));
  354. worksheet.Range[UtilsController.GetExcelColumnName(2) + rowCount.ToString()].Text = "TỔNG".ToString();
  355. rowAttributedList = rowHandler(rowAttributedList, new RowAttributed(UtilsController.GetExcelColumnName(2), rowCount.ToString(), true, null, Color.FromArgb(247, 220, 111), 1, 1, 1, 1, null));
  356. // chi so tang giam
  357. worksheet.Range[UtilsController.GetExcelColumnName(3) + rowCount.ToString()].Text = "Khối lượng";
  358. rowAttributedList = rowHandler(rowAttributedList, new RowAttributed(UtilsController.GetExcelColumnName(3), rowCount.ToString(), true, null, Color.FromArgb(247, 220, 111), 1, 1, 1, 1, null));
  359. for (int j = 0; j < listTongKhoiLuong.Count; j++)
  360. {
  361. worksheet.Range[UtilsController.GetExcelColumnName(j * 2 + 4) + rowCount.ToString()].Text = listTongKhoiLuong[j].ToString();
  362. rowAttributedList = rowHandler(rowAttributedList, new RowAttributed(UtilsController.GetExcelColumnName(j * 2 + 4), rowCount.ToString(), true, null, Color.FromArgb(247, 220, 111), 1, 1, 1, 1, null));
  363. rowAttributedList = rowHandler(rowAttributedList, new RowAttributed(UtilsController.GetExcelColumnName(j * 2 + 5), rowCount.ToString(), true, null, Color.FromArgb(247, 220, 111), 1, 1, 1, 1, null));
  364. }
  365. rowCount++;
  366. // chi so tang giam
  367. worksheet.Range[UtilsController.GetExcelColumnName(3) + rowCount.ToString()].Text = "Giá trị";
  368. for (int j = 0; j < listTongGiaTri.Count; j++)
  369. {
  370. worksheet.Range[UtilsController.GetExcelColumnName(j * 2 + 4) + rowCount.ToString()].Text = listTongGiaTri[j].ToString();
  371. }
  372. rowCount++;
  373. // chi so tang giam
  374. worksheet.Range[UtilsController.GetExcelColumnName(3) + rowCount.ToString()].Text = "Mua";
  375. for (int j = 0; j < listTongMua.Count; j++)
  376. {
  377. worksheet.Range[UtilsController.GetExcelColumnName(j * 2 + 4) + rowCount.ToString()].Text = listTongMua[j].ToString();
  378. }
  379. rowCount++;
  380. // chi so tang giam
  381. worksheet.Range[UtilsController.GetExcelColumnName(3) + rowCount.ToString()].Text = "Bán";
  382. for (int j = 0; j < listTongBan.Count; j++)
  383. {
  384. worksheet.Range[UtilsController.GetExcelColumnName(j * 2 + 4) + rowCount.ToString()].Text = listTongBan[j].ToString();
  385. }
  386. rowCount++;
  387. //Formatting
  388. //Global styles should be used when the same style needs to be applied to more than one cell. This usage of a global style reduces memory usage.
  389. //Add custom colors to the palette
  390. workbook.SetPaletteColor(8, Color.FromArgb(255, 174, 33));
  391. //Defining header style
  392. IStyle headerStyle = workbook.Styles.Add("HeaderStyle" + 0.ToString());
  393. headerStyle.BeginUpdate();
  394. headerStyle.Color = Color.FromArgb(82, 190, 128);
  395. headerStyle.Font.Bold = true;
  396. headerStyle.Borders[ExcelBordersIndex.EdgeLeft].LineStyle = ExcelLineStyle.Thin;
  397. headerStyle.Borders[ExcelBordersIndex.EdgeRight].LineStyle = ExcelLineStyle.Thin;
  398. headerStyle.Borders[ExcelBordersIndex.EdgeTop].LineStyle = ExcelLineStyle.Thin;
  399. headerStyle.Borders[ExcelBordersIndex.EdgeBottom].LineStyle = ExcelLineStyle.Thin;
  400. headerStyle.EndUpdate();
  401. //Apply Header style
  402. worksheet.Rows[0].CellStyle = headerStyle;
  403. for (int g = 0; g < rowAttributedList.Count; g++)
  404. {
  405. worksheet.Range[rowAttributedList[g].character + rowAttributedList[g].number].CellStyle = setStyleRowExcel(workbook, 0, rowAttributedList[g]);
  406. }
  407. //Auto-fit the columns
  408. worksheet.UsedRange.AutofitColumns();
  409. //Saving the workbook as stream
  410. //FileStream stream = new FileStream("GlobalStyles.xlsx", FileMode.Create, FileAccess.ReadWrite);
  411. //workbook.SaveAs(stream);
  412. //stream.Dispose();
  413. using (var stream = new MemoryStream())
  414. {
  415. workbook.SaveAs(stream);
  416. var content = stream.ToArray();
  417. return File(
  418. content,
  419. "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
  420. "stock_" + DateTime.Now.ToString("dd_MM") + ".xlsx");
  421. }
  422. }
  423. }
  424. catch (Exception ex)
  425. {
  426. log.Error("Exception: ", ex);
  427. return Redirect("/Home");
  428. }
  429. }
  430. protected IActionResult Excel(DateTime endDateTime, List<DistributedDataForExcelByOneMonth> dataList, List<DistributedByTime> dataAll)
  431. {
  432. try
  433. {
  434. using (ExcelEngine excelEngine = new ExcelEngine())
  435. {
  436. IApplication application = excelEngine.Excel;
  437. application.DefaultVersion = ExcelVersion.Excel2013;
  438. IWorkbook workbook = application.Workbooks.Create(dataList.Count);
  439. for (int i = 0; i < dataList.Count; i++)
  440. {
  441. int rowCount = 1;
  442. List<DistributedByTelcoAndCompany> distributedByTelcoAndCompanyT = new List<DistributedByTelcoAndCompany>();
  443. List<DistributedByTelcoAndCompany> disByTelcoAndCompany = distributedByTelcoAndCompany(dataList[i].telcos, dataList[i].companies, dataList[i].services, 0, distributedByTelcoAndCompanyT);
  444. IWorksheet worksheet = workbook.Worksheets[i];
  445. List<RowAttributed> rowAttributedList = new List<RowAttributed>();
  446. // for header
  447. worksheet.Range["A1"].Text = "NO";
  448. worksheet.Range["B1"].Text = "VAS CODE";
  449. worksheet.Range["C1"].Text = "Acc now";
  450. worksheet.Range["D1"].Text = "Acc last month";
  451. worksheet.Range["E1"].Text = "+/-";
  452. worksheet.Range["F1"].Text = "%";
  453. rowCount++;
  454. disByTelcoAndCompany.Sort(delegate (DistributedByTelcoAndCompany x, DistributedByTelcoAndCompany y)
  455. {
  456. if (x.telco.sequence == null && y.telco.sequence == null) return 0;
  457. else if (x.telco.sequence == null) return -1;
  458. else if (y.telco.sequence == null) return 1;
  459. else return int.Parse(x.telco.sequence).CompareTo(int.Parse(y.telco.sequence));
  460. });
  461. // FOR MONEY TABLES
  462. for (int k = 0; k < disByTelcoAndCompany.Count; k++)
  463. {
  464. if (k != 0)
  465. {
  466. rowCount++;
  467. }
  468. // show telco name
  469. worksheet.Range[UtilsController.GetExcelColumnName(1) + rowCount.ToString()].Text = disByTelcoAndCompany[k].telco.telcoName;
  470. worksheet.Range[UtilsController.GetExcelColumnName(1) + rowCount.ToString() + ":" + UtilsController.GetExcelColumnName(6) + rowCount.ToString()].Merge();
  471. rowAttributedList = rowHandler(rowAttributedList, new RowAttributed(UtilsController.GetExcelColumnName(1), rowCount.ToString(), true, null, Color.FromArgb(247, 220, 111), 1, 1, 1, 1, 1));
  472. rowCount++;
  473. disByTelcoAndCompany[k].companies.data.Sort(delegate (ConnCompany x, ConnCompany y)
  474. {
  475. if (x.sequence == null && y.sequence == null) return 0;
  476. else if (x.sequence == null) return -1;
  477. else if (y.sequence == null) return 1;
  478. else return int.Parse(x.sequence).CompareTo(int.Parse(y.sequence));
  479. });
  480. for (int c = 0; c < disByTelcoAndCompany[k].companies.data.Count; c++)
  481. {
  482. // show company name
  483. ConnCompany company = disByTelcoAndCompany[k].companies.data[c];
  484. worksheet.Range[UtilsController.GetExcelColumnName(1) + rowCount.ToString()].Text = company.name;
  485. worksheet.Range[UtilsController.GetExcelColumnName(1) + rowCount.ToString() + ":" + UtilsController.GetExcelColumnName(6) + rowCount.ToString()].Merge();
  486. rowAttributedList = rowHandler(rowAttributedList, new RowAttributed(UtilsController.GetExcelColumnName(1), rowCount.ToString(), true, null, Color.FromArgb(236, 112, 99), 1, 1, 1, 1, 1));
  487. rowCount++;
  488. // show all services which is belong this this company
  489. List<ConnConfig> serviceBelong = disByTelcoAndCompany[k].services.data.FindAll(x => x.companyID == company.id);
  490. serviceBelong.Sort(delegate (ConnConfig x, ConnConfig y)
  491. {
  492. if (x.sequence == null && y.sequence == null) return 0;
  493. else if (x.sequence == null) return -1;
  494. else if (y.sequence == null) return 1;
  495. else return int.Parse(x.sequence).CompareTo(int.Parse(y.sequence));
  496. });
  497. for (int s = 0; s < serviceBelong.Count; s++)
  498. {
  499. ConnConfig service = serviceBelong[s];
  500. //worksheet.Range[GetExcelColumnName(1) + rowCount.ToString()].Text = service.serviceName;
  501. //serviceRowList.Add(rowCount);
  502. //rowCount++;
  503. // get the data of this service
  504. List<DistributedByTime> dataService = dataList[i].distributedByTimes.FindAll(x => x.service.id == service.id);
  505. DateTime now = DateTime.Now.AddDays(-1);
  506. // get date now
  507. DistributedByTime dataNow = dataService.Find(x => x.time.ToString("dd/MM/yyyy") == endDateTime.ToString("dd/MM/yyyy"));
  508. if (dataService.Count > 0)
  509. {
  510. // check
  511. if (dataService[0].time.Month == now.Month)
  512. {
  513. for (int d = 1; d < dataList[i].time.Count; d++)
  514. {
  515. worksheet.Range[UtilsController.GetExcelColumnName(d + 6) + "1"].Text = dataList[i].time[d].ToString("dd/MM");
  516. }
  517. }
  518. else
  519. {
  520. for (int d = 2; d < dataList[i].time.Count; d++)
  521. {
  522. worksheet.Range[UtilsController.GetExcelColumnName(d + 5) + "1"].Text = dataList[i].time[d].ToString("dd/MM");
  523. }
  524. }
  525. }
  526. // get all data in the last month
  527. // get month now
  528. DateTime startLastMonth = getStartByMonthToDate(dataList[i].time[dataList[i].time.Count - 1], -1);
  529. DateTime stopLastMonth = getEndByMonthToDate(dataList[i].time[dataList[i].time.Count - 1], -1);
  530. worksheet.Name = getStartByMonthToDate(dataList[i].time[dataList[i].time.Count - 1], 0).ToString("MM yyyy");
  531. List<DistributedByTime> dataLastMonth = dataAll.FindAll(x =>
  532. DateTime.Compare(startLastMonth, x.time) <= 0 && DateTime.Compare(x.time, stopLastMonth) <= 0 && x.service.id == service.id);
  533. //DateTime startNow = getStartByMonthToDate(0);
  534. //DateTime stopNow = getEndByDayToDate();
  535. //List<DistributedByTime> dataThisMonth = dataService.FindAll(x =>
  536. // DateTime.Compare(startNow, x.time) <= 0 && DateTime.Compare(x.time, stopNow) <= 0);
  537. // remove the first element
  538. List<DistributedByTime> dataThisMonth = new List<DistributedByTime>();
  539. for (int h = dataNow != null && dataNow.connDatas != null ? 1 : 0; h < dataService.Count; h++)
  540. {
  541. dataThisMonth.Add(dataService[h]);
  542. }
  543. double newSub = 0, newSubBefore = 0;
  544. double deactive = 0, deactiveBefore = 0;
  545. double revenue = 0, revenueBefore = 0;
  546. double expense = 0, expenseBefore = 0;
  547. double totalSub = 0, totalSubBefore = 0;
  548. double benefit = 0, benefitBefore = 0;
  549. double benefitP = 0, revenueP = 0, expenseP = 0;
  550. double benefitD = 0, revenueD = 0, expenseD = 0;
  551. int dayMonthNow = dataThisMonth.Count;
  552. //for (int y = 0; y < dataLastMonth.Count; y++)
  553. //{
  554. // if (dataLastMonth[y].connDatas.data.Count > 0)
  555. // {
  556. // for (int q = 0; q < dayMonthNow; q++)
  557. // {
  558. // newSubBefore += int.Parse(dataLastMonth[q].connDatas.data[0].registerNew);
  559. // deactiveBefore += int.Parse(dataLastMonth[q].connDatas.data[0].deactive);
  560. // revenueBefore += int.Parse(dataLastMonth[q].connDatas.data[0].revenue);
  561. // expenseBefore += int.Parse(dataLastMonth[q].connDatas.data[0].expense);
  562. // totalSubBefore += int.Parse(dataLastMonth[q].connDatas.data[0].totalSub);
  563. // }
  564. // }
  565. //}
  566. if (dataLastMonth.Count > 0 && dayMonthNow <= dataLastMonth.Count - 1)
  567. {
  568. for (int q = 0; q < dayMonthNow; q++)
  569. {
  570. if (dataLastMonth[dataLastMonth.Count - 1 - q].connDatas.data.Count > 0)
  571. {
  572. if (dataLastMonth[dataLastMonth.Count - 1 - q].connDatas.data[0].registerNew != null)
  573. {
  574. newSubBefore += double.Parse(dataLastMonth[dataLastMonth.Count - 1 - q].connDatas.data[0].registerNew);
  575. }
  576. if (dataLastMonth[dataLastMonth.Count - 1 - q].connDatas.data[0].deactive != null)
  577. {
  578. deactiveBefore += double.Parse(dataLastMonth[dataLastMonth.Count - 1 - q].connDatas.data[0].deactive);
  579. }
  580. if (dataLastMonth[dataLastMonth.Count - 1 - q].connDatas.data[0].revenue != null)
  581. {
  582. revenueBefore += double.Parse(dataLastMonth[dataLastMonth.Count - 1 - q].connDatas.data[0].revenue);
  583. }
  584. if (dataLastMonth[dataLastMonth.Count - 1 - q].connDatas.data[0].expense != null)
  585. {
  586. expenseBefore += double.Parse(dataLastMonth[dataLastMonth.Count - 1 - q].connDatas.data[0].expense);
  587. }
  588. if (dataLastMonth[dataLastMonth.Count - 1 - q].connDatas.data[0].totalSub != null)
  589. {
  590. totalSubBefore += double.Parse(dataLastMonth[dataLastMonth.Count - 1 - q].connDatas.data[0].totalSub);
  591. }
  592. }
  593. }
  594. }
  595. benefitBefore = revenueBefore - Math.Abs(expenseBefore);
  596. for (int y = 0; y < dataThisMonth.Count; y++)
  597. {
  598. if (dataThisMonth[y].connDatas.data.Count > 0)
  599. {
  600. if (dataThisMonth[y].connDatas.data[0].registerNew != null)
  601. {
  602. newSub += double.Parse(dataThisMonth[y].connDatas.data[0].registerNew);
  603. }
  604. if (dataThisMonth[y].connDatas.data[0].deactive != null)
  605. {
  606. deactive += double.Parse(dataThisMonth[y].connDatas.data[0].deactive);
  607. }
  608. if (dataThisMonth[y].connDatas.data[0].revenue != null)
  609. {
  610. revenue += double.Parse(dataThisMonth[y].connDatas.data[0].revenue);
  611. }
  612. if (dataThisMonth[y].connDatas.data[0].expense != null)
  613. {
  614. expense += double.Parse(dataThisMonth[y].connDatas.data[0].expense);
  615. }
  616. if (dataThisMonth[y].connDatas.data[0].totalSub != null)
  617. {
  618. totalSub += double.Parse(dataThisMonth[y].connDatas.data[0].totalSub);
  619. }
  620. }
  621. }
  622. benefit = revenue - Math.Abs(expense);
  623. revenueD = revenue - revenueBefore;
  624. expenseD = Math.Abs(expense) - Math.Abs(expenseBefore);
  625. benefitD = benefit - benefitBefore;
  626. revenueP = revenueBefore != 0 ? Math.Round(revenueD * 100 / revenueBefore, 2) : 100;
  627. expenseP = expenseBefore != 0 ? Math.Round(expenseD * 100 / expenseBefore, 2) : 100;
  628. benefitP = benefitBefore != 0 ? Math.Round(benefitD * 100 / benefitBefore, 2) : 100;
  629. // show
  630. worksheet.Range[UtilsController.GetExcelColumnName(2) + rowCount.ToString()].Text = service.serviceName + " NET";
  631. RowAttributed r3 = new RowAttributed(UtilsController.GetExcelColumnName(2), rowCount.ToString(), true, Color.FromArgb(255, 0, 0), Color.FromArgb(215, 189, 226), 1, 1, 1, 1, null);
  632. rowAttributedList = rowHandler(rowAttributedList, r3);
  633. worksheet.Range[UtilsController.GetExcelColumnName(3) + rowCount.ToString()].Text = string.Format("{0:#,0}", benefit).ToString();
  634. rowAttributedList = rowHandler(rowAttributedList, new RowAttributed(UtilsController.GetExcelColumnName(3), rowCount.ToString(), true, Color.FromArgb(255, 0, 0), null, 1, 1, 1, 1, null));
  635. worksheet.Range[UtilsController.GetExcelColumnName(4) + rowCount.ToString()].Text = string.Format("{0:#,0}", benefitBefore).ToString();
  636. rowAttributedList = rowHandler(rowAttributedList, new RowAttributed(UtilsController.GetExcelColumnName(4), rowCount.ToString(), true, Color.FromArgb(255, 0, 0), null, 1, 1, 1, 1, null));
  637. if (benefitD < 0)
  638. {
  639. worksheet.Range[UtilsController.GetExcelColumnName(5) + rowCount.ToString()].Text = "(" + string.Format("{0:#,0}", Math.Abs(benefitD)).ToString() + ")";
  640. rowAttributedList = rowHandler(rowAttributedList, new RowAttributed(UtilsController.GetExcelColumnName(5), rowCount.ToString(), true, null, null, 1, 1, 1, 1, null));
  641. worksheet.Range[UtilsController.GetExcelColumnName(6) + rowCount.ToString()].Text = string.Format("{0:#,0}", benefitP).ToString() + "%";
  642. rowAttributedList = rowHandler(rowAttributedList, new RowAttributed(UtilsController.GetExcelColumnName(6), rowCount.ToString(), true, null, null, 1, 1, 1, 1, null));
  643. }
  644. else
  645. {
  646. worksheet.Range[UtilsController.GetExcelColumnName(5) + rowCount.ToString()].Text = string.Format("{0:#,0}", benefitD).ToString();
  647. rowAttributedList = rowHandler(rowAttributedList, new RowAttributed(UtilsController.GetExcelColumnName(5), rowCount.ToString(), true, null, null, 1, 1, 1, 1, null));
  648. worksheet.Range[UtilsController.GetExcelColumnName(6) + rowCount.ToString()].Text = string.Format("{0:#,0}", benefitP).ToString() + "%";
  649. rowAttributedList = rowHandler(rowAttributedList, new RowAttributed(UtilsController.GetExcelColumnName(6), rowCount.ToString(), true, null, null, 1, 1, 1, 1, null));
  650. }
  651. if (dataNow != null && dataNow.connDatas != null && dataNow.connDatas.data.Count > 0)
  652. {
  653. double benefitN = 0;
  654. if (dataNow.connDatas.data[0].revenue != null && dataNow.connDatas.data[0].expense != null)
  655. {
  656. benefitN = double.Parse(dataNow.connDatas.data[0].revenue) - Math.Abs(double.Parse(dataNow.connDatas.data[0].expense));
  657. }
  658. worksheet.Range[UtilsController.GetExcelColumnName(7) + rowCount.ToString()].Text = string.Format("{0:#,0}", benefitN).ToString();
  659. rowAttributedList = rowHandler(rowAttributedList, new RowAttributed(UtilsController.GetExcelColumnName(7), rowCount.ToString(), true, Color.FromArgb(255, 0, 0), null, 1, 1, 1, 1, null));
  660. }
  661. for (int l = 0; l < dataThisMonth.Count; l++)
  662. {
  663. if (dataThisMonth[l].connDatas.data.Count > 0)
  664. {
  665. double benefitT = 0;
  666. if (dataThisMonth[l].connDatas.data[0].revenue != null && dataThisMonth[l].connDatas.data[0].expense != null)
  667. {
  668. benefitT = double.Parse(dataThisMonth[l].connDatas.data[0].revenue) - Math.Abs(double.Parse(dataThisMonth[l].connDatas.data[0].expense));
  669. }
  670. worksheet.Range[UtilsController.GetExcelColumnName(dataNow != null ? 7 + l + 1 : 7 + l) + rowCount.ToString()].Text = string.Format("{0:#,0}", benefitT).ToString();
  671. rowAttributedList = rowHandler(rowAttributedList, new RowAttributed(UtilsController.GetExcelColumnName(dataNow != null ? 7 + l + 1 : 7 + l), rowCount.ToString(), true, Color.FromArgb(255, 0, 0), null, 1, 1, 1, 1, null));
  672. }
  673. }
  674. rowCount++;
  675. if (service.state == UtilsController.Constant.ALL_REVEUNUE)
  676. {
  677. worksheet.Range[UtilsController.GetExcelColumnName(2) + rowCount.ToString()].Text = service.serviceName;
  678. rowAttributedList = rowHandler(rowAttributedList, new RowAttributed(UtilsController.GetExcelColumnName(2), rowCount.ToString(), true, null, Color.FromArgb(215, 189, 226), 1, 1, 1, 1, null));
  679. worksheet.Range[UtilsController.GetExcelColumnName(3) + rowCount.ToString()].Text = string.Format("{0:#,0}", revenue).ToString();
  680. rowAttributedList = rowHandler(rowAttributedList, new RowAttributed(UtilsController.GetExcelColumnName(3), rowCount.ToString(), true, null, null, 1, 1, 1, 1, null));
  681. worksheet.Range[UtilsController.GetExcelColumnName(4) + rowCount.ToString()].Text = string.Format("{0:#,0}", revenueBefore).ToString();
  682. rowAttributedList = rowHandler(rowAttributedList, new RowAttributed(UtilsController.GetExcelColumnName(4), rowCount.ToString(), true, null, null, 1, 1, 1, 1, null));
  683. if (revenueD < 0)
  684. {
  685. worksheet.Range[UtilsController.GetExcelColumnName(5) + rowCount.ToString()].Text = "(" + string.Format("{0:#,0}", Math.Abs(revenueD)).ToString() + ")";
  686. rowAttributedList = rowHandler(rowAttributedList, new RowAttributed(UtilsController.GetExcelColumnName(5), rowCount.ToString(), true, null, null, 1, 1, 1, 1, null));
  687. worksheet.Range[UtilsController.GetExcelColumnName(6) + rowCount.ToString()].Text = string.Format("{0:#,0}", revenueP).ToString() + "%";
  688. rowAttributedList = rowHandler(rowAttributedList, new RowAttributed(UtilsController.GetExcelColumnName(6), rowCount.ToString(), true, null, null, 1, 1, 1, 1, null));
  689. }
  690. else
  691. {
  692. worksheet.Range[UtilsController.GetExcelColumnName(5) + rowCount.ToString()].Text = string.Format("{0:#,0}", revenueD).ToString();
  693. rowAttributedList = rowHandler(rowAttributedList, new RowAttributed(UtilsController.GetExcelColumnName(5), rowCount.ToString(), true, null, null, 1, 1, 1, 1, null));
  694. worksheet.Range[UtilsController.GetExcelColumnName(6) + rowCount.ToString()].Text = string.Format("{0:#,0}", revenueP).ToString() + "%";
  695. rowAttributedList = rowHandler(rowAttributedList, new RowAttributed(UtilsController.GetExcelColumnName(6), rowCount.ToString(), true, null, null, 1, 1, 1, 1, null));
  696. }
  697. if (dataNow != null && dataNow.connDatas != null && dataNow.connDatas.data.Count > 0)
  698. {
  699. worksheet.Range[UtilsController.GetExcelColumnName(7) + rowCount.ToString()].Text = string.Format("{0:#,0}", double.Parse(dataNow.connDatas.data[0].revenue)).ToString();
  700. rowAttributedList = rowHandler(rowAttributedList, new RowAttributed(UtilsController.GetExcelColumnName(7), rowCount.ToString(), true, null, null, 1, 1, 1, 1, null));
  701. }
  702. for (int l = 0; l < dataThisMonth.Count; l++)
  703. {
  704. if (dataThisMonth[l].connDatas.data.Count > 0)
  705. {
  706. worksheet.Range[UtilsController.GetExcelColumnName(dataNow != null ? 7 + l + 1 : 7 + l) + rowCount.ToString()].Text = string.Format("{0:#,0}", double.Parse(dataThisMonth[l].connDatas.data[0].revenue)).ToString();
  707. rowAttributedList = rowHandler(rowAttributedList, new RowAttributed(UtilsController.GetExcelColumnName(dataNow != null ? 7 + l + 1 : 7 + l), rowCount.ToString(), true, null, null, 1, 1, 1, 1, null));
  708. }
  709. }
  710. rowCount++;
  711. worksheet.Range[UtilsController.GetExcelColumnName(2) + rowCount.ToString()].Text = service.serviceName + " COST";
  712. rowAttributedList = rowHandler(rowAttributedList, new RowAttributed(UtilsController.GetExcelColumnName(2), rowCount.ToString(), true, null, Color.FromArgb(215, 189, 226), 1, 1, 1, 1, null));
  713. worksheet.Range[UtilsController.GetExcelColumnName(3) + rowCount.ToString()].Text = string.Format("{0:#,0}", Math.Abs(expense)).ToString();
  714. rowAttributedList = rowHandler(rowAttributedList, new RowAttributed(UtilsController.GetExcelColumnName(3), rowCount.ToString(), true, null, null, 1, 1, 1, 1, null));
  715. worksheet.Range[UtilsController.GetExcelColumnName(4) + rowCount.ToString()].Text = string.Format("{0:#,0}", Math.Abs(expenseBefore)).ToString();
  716. rowAttributedList = rowHandler(rowAttributedList, new RowAttributed(UtilsController.GetExcelColumnName(4), rowCount.ToString(), true, null, null, 1, 1, 1, 1, null));
  717. if (expenseD < 0)
  718. {
  719. worksheet.Range[UtilsController.GetExcelColumnName(5) + rowCount.ToString()].Text = "(" + string.Format("{0:#,0}", Math.Abs(expenseD)).ToString() + ")";
  720. rowAttributedList = rowHandler(rowAttributedList, new RowAttributed(UtilsController.GetExcelColumnName(5), rowCount.ToString(), true, null, null, 1, 1, 1, 1, null));
  721. worksheet.Range[UtilsController.GetExcelColumnName(6) + rowCount.ToString()].Text = string.Format("{0:#,0}", expenseP).ToString() + "%";
  722. rowAttributedList = rowHandler(rowAttributedList, new RowAttributed(UtilsController.GetExcelColumnName(6), rowCount.ToString(), true, null, null, 1, 1, 1, 1, null));
  723. }
  724. else
  725. {
  726. worksheet.Range[UtilsController.GetExcelColumnName(5) + rowCount.ToString()].Text = string.Format("{0:#,0}", Math.Abs(expenseD)).ToString();
  727. rowAttributedList = rowHandler(rowAttributedList, new RowAttributed(UtilsController.GetExcelColumnName(5), rowCount.ToString(), true, null, null, 1, 1, 1, 1, null));
  728. worksheet.Range[UtilsController.GetExcelColumnName(6) + rowCount.ToString()].Text = string.Format("{0:#,0}", expenseP).ToString() + "%";
  729. rowAttributedList = rowHandler(rowAttributedList, new RowAttributed(UtilsController.GetExcelColumnName(6), rowCount.ToString(), true, null, null, 1, 1, 1, 1, null));
  730. }
  731. if (dataNow != null && dataNow.connDatas != null && dataNow.connDatas.data.Count > 0)
  732. {
  733. String expenseCheck1 = dataNow.connDatas.data[0].expense != null ? dataNow.connDatas.data[0].expense : "0";
  734. worksheet.Range[UtilsController.GetExcelColumnName(7) + rowCount.ToString()].Text = string.Format("{0:#,0}", Math.Abs(double.Parse(expenseCheck1))).ToString();
  735. rowAttributedList = rowHandler(rowAttributedList, new RowAttributed(UtilsController.GetExcelColumnName(7), rowCount.ToString(), true, null, null, 1, 1, 1, 1, null));
  736. }
  737. for (int l = 0; l < dataThisMonth.Count; l++)
  738. {
  739. if (dataThisMonth[l].connDatas.data.Count > 0)
  740. {
  741. String expenseCheck = dataThisMonth[l].connDatas.data[0].expense != null ? dataThisMonth[l].connDatas.data[0].expense : "0";
  742. worksheet.Range[UtilsController.GetExcelColumnName(dataNow != null ? 7 + l + 1 : 7 + l) + rowCount.ToString()].Text = string.Format("{0:#,0}", Math.Abs(double.Parse(expenseCheck))).ToString();
  743. rowAttributedList = rowHandler(rowAttributedList, new RowAttributed(UtilsController.GetExcelColumnName(dataNow != null ? 7 + l + 1 : 7 + l), rowCount.ToString(), true, null, null, 1, 1, 1, 1, null));
  744. }
  745. }
  746. rowCount++;
  747. }
  748. }
  749. worksheet.Range[UtilsController.GetExcelColumnName(2) + rowCount.ToString()].Text = "TOTAL";
  750. rowCount++;
  751. }
  752. }
  753. // FOR SUBSCRIBERS TABLES
  754. rowCount += 10;
  755. worksheet.Range[UtilsController.GetExcelColumnName(1) + rowCount.ToString()].Text = "TOTAL SUBSCRIBERS COMPARE LAST MONTH";
  756. worksheet.Range[UtilsController.GetExcelColumnName(1) + rowCount.ToString() + ":" + UtilsController.GetExcelColumnName(6) + rowCount.ToString()].Merge();
  757. rowAttributedList = rowHandler(rowAttributedList, new RowAttributed(UtilsController.GetExcelColumnName(1), rowCount.ToString(), true, Color.FromArgb(0, 0, 0), Color.FromArgb(255, 174, 33), 1, 1, 1, 1, 1));
  758. rowCount++;
  759. // for header
  760. worksheet.Range[UtilsController.GetExcelColumnName(1) + rowCount.ToString()].Text = "NO";
  761. worksheet.Range[UtilsController.GetExcelColumnName(2) + rowCount.ToString()].Text = "VAS CODE";
  762. worksheet.Range[UtilsController.GetExcelColumnName(3) + rowCount.ToString()].Text = "Acc now";
  763. worksheet.Range[UtilsController.GetExcelColumnName(4) + rowCount.ToString()].Text = "Acc last month";
  764. worksheet.Range[UtilsController.GetExcelColumnName(5) + rowCount.ToString()].Text = "+/-";
  765. worksheet.Range[UtilsController.GetExcelColumnName(6) + rowCount.ToString()].Text = "%";
  766. //for (int d = 1; d < dataList[i].time.Count; d++)
  767. //{
  768. // worksheet.Range[GetExcelColumnName(d + 6) + "1"].Text = dataList[i].time[d].ToString("dd/MM");
  769. //}
  770. //rowCount++;
  771. for (int k = 0; k < disByTelcoAndCompany.Count; k++)
  772. {
  773. if (k != 0)
  774. {
  775. rowCount++;
  776. }
  777. // show telco name
  778. worksheet.Range[UtilsController.GetExcelColumnName(1) + rowCount.ToString()].Text = disByTelcoAndCompany[k].telco.telcoName;
  779. worksheet.Range[UtilsController.GetExcelColumnName(1) + rowCount.ToString() + ":" + UtilsController.GetExcelColumnName(6) + rowCount.ToString()].Merge();
  780. rowAttributedList = rowHandler(rowAttributedList, new RowAttributed(UtilsController.GetExcelColumnName(1), rowCount.ToString(), true, null, Color.FromArgb(247, 220, 111), 1, 1, 1, 1, 1));
  781. rowCount++;
  782. disByTelcoAndCompany[k].companies.data.Sort(delegate (ConnCompany x, ConnCompany y)
  783. {
  784. if (x.sequence == null && y.sequence == null) return 0;
  785. else if (x.sequence == null) return -1;
  786. else if (y.sequence == null) return 1;
  787. else return int.Parse(x.sequence).CompareTo(int.Parse(y.sequence));
  788. });
  789. for (int c = 0; c < disByTelcoAndCompany[k].companies.data.Count; c++)
  790. {
  791. // show company name
  792. ConnCompany company = disByTelcoAndCompany[k].companies.data[c];
  793. worksheet.Range[UtilsController.GetExcelColumnName(1) + rowCount.ToString()].Text = company.name;
  794. worksheet.Range[UtilsController.GetExcelColumnName(1) + rowCount.ToString() + ":" + UtilsController.GetExcelColumnName(6) + rowCount.ToString()].Merge();
  795. rowAttributedList = rowHandler(rowAttributedList, new RowAttributed(UtilsController.GetExcelColumnName(1), rowCount.ToString(), true, null, Color.FromArgb(236, 112, 99), 1, 1, 1, 1, 1));
  796. rowCount++;
  797. // show all services which is belong this this company
  798. List<ConnConfig> serviceBelong = disByTelcoAndCompany[k].services.data.FindAll(x => x.companyID == company.id);
  799. serviceBelong.Sort(delegate (ConnConfig x, ConnConfig y)
  800. {
  801. if (x.sequence == null && y.sequence == null) return 0;
  802. else if (x.sequence == null) return -1;
  803. else if (y.sequence == null) return 1;
  804. else return int.Parse(x.sequence).CompareTo(int.Parse(y.sequence));
  805. });
  806. for (int s = 0; s < serviceBelong.Count; s++)
  807. {
  808. ConnConfig service = serviceBelong[s];
  809. //worksheet.Range[GetExcelColumnName(1) + rowCount.ToString()].Text = service.serviceName;
  810. //serviceRowList.Add(rowCount);
  811. //rowCount++;
  812. // get the data of this service
  813. List<DistributedByTime> dataService = dataList[i].distributedByTimes.FindAll(x => x.service.id == service.id);
  814. DateTime now = DateTime.Now.AddDays(-1);
  815. // get date now
  816. DistributedByTime dataNow = dataService.Find(x => x.time.ToString("dd/MM/yyyy") == now.ToString("dd/MM/yyyy"));
  817. // get all data in the last month
  818. // get month now
  819. DateTime startLastMonth = getStartByMonthToDate(dataList[i].time[dataList[i].time.Count - 1], -1);
  820. DateTime stopLastMonth = getEndByMonthToDate(dataList[i].time[dataList[i].time.Count - 1], -1);
  821. worksheet.Name = getStartByMonthToDate(dataList[i].time[dataList[i].time.Count - 1], 0).ToString("MM yyyy");
  822. List<DistributedByTime> dataLastMonth = dataAll.FindAll(x =>
  823. DateTime.Compare(startLastMonth, x.time) <= 0 && DateTime.Compare(x.time, stopLastMonth) <= 0 && x.service.id == service.id);
  824. //DateTime startNow = getStartByMonthToDate(0);
  825. //DateTime stopNow = getEndByDayToDate();
  826. //List<DistributedByTime> dataThisMonth = dataService.FindAll(x =>
  827. // DateTime.Compare(startNow, x.time) <= 0 && DateTime.Compare(x.time, stopNow) <= 0);
  828. //List<DistributedByTime> dataThisMonth = dataService;
  829. List<DistributedByTime> dataThisMonth = new List<DistributedByTime>();
  830. for (int h = dataNow != null && dataNow.connDatas != null ? 1 : 0; h < dataService.Count; h++)
  831. {
  832. dataThisMonth.Add(dataService[h]);
  833. }
  834. double totalSub = 0, totalSubBefore = 0;
  835. double totalSubD = 0, totalSubP = 0, totalSubN = 0;
  836. if (dataLastMonth.Count > 0 && dataLastMonth[0].connDatas.data.Count > 0)
  837. {
  838. totalSubBefore = int.Parse(dataLastMonth[0].connDatas.data[0].totalSub);
  839. }
  840. if (dataThisMonth.Count > 0 && dataThisMonth[0].connDatas.data.Count > 0)
  841. {
  842. totalSub = int.Parse(dataThisMonth[0].connDatas.data[0].totalSub);
  843. }
  844. totalSubD = totalSub - totalSubBefore;
  845. totalSubP = totalSubBefore != 0 ? Math.Round(totalSubD * 100 / totalSubBefore, 2) : 100;
  846. // show
  847. worksheet.Range[UtilsController.GetExcelColumnName(2) + rowCount.ToString()].Text = service.serviceName;
  848. RowAttributed r3 = new RowAttributed(UtilsController.GetExcelColumnName(2), rowCount.ToString(), true, Color.FromArgb(255, 0, 0), Color.FromArgb(215, 189, 226), 1, 1, 1, 1, null);
  849. rowAttributedList = rowHandler(rowAttributedList, r3);
  850. worksheet.Range[UtilsController.GetExcelColumnName(3) + rowCount.ToString()].Text = string.Format("{0:#,0}", totalSub).ToString();
  851. rowAttributedList = rowHandler(rowAttributedList, new RowAttributed(UtilsController.GetExcelColumnName(3), rowCount.ToString(), true, Color.FromArgb(255, 0, 0), null, 1, 1, 1, 1, null));
  852. worksheet.Range[UtilsController.GetExcelColumnName(4) + rowCount.ToString()].Text = string.Format("{0:#,0}", totalSubBefore).ToString();
  853. rowAttributedList = rowHandler(rowAttributedList, new RowAttributed(UtilsController.GetExcelColumnName(4), rowCount.ToString(), true, Color.FromArgb(255, 0, 0), null, 1, 1, 1, 1, null));
  854. if (totalSubD < 0)
  855. {
  856. worksheet.Range[UtilsController.GetExcelColumnName(5) + rowCount.ToString()].Text = "(" + string.Format("{0:#,0}", Math.Abs(totalSubD)).ToString() + ")";
  857. rowAttributedList = rowHandler(rowAttributedList, new RowAttributed(UtilsController.GetExcelColumnName(5), rowCount.ToString(), true, null, null, 1, 1, 1, 1, null));
  858. worksheet.Range[UtilsController.GetExcelColumnName(6) + rowCount.ToString()].Text = string.Format("{0:#,0}", totalSubP).ToString() + "%";
  859. rowAttributedList = rowHandler(rowAttributedList, new RowAttributed(UtilsController.GetExcelColumnName(6), rowCount.ToString(), true, null, null, 1, 1, 1, 1, null));
  860. }
  861. else
  862. {
  863. worksheet.Range[UtilsController.GetExcelColumnName(5) + rowCount.ToString()].Text = string.Format("{0:#,0}", totalSubD).ToString();
  864. rowAttributedList = rowHandler(rowAttributedList, new RowAttributed(UtilsController.GetExcelColumnName(5), rowCount.ToString(), true, null, null, 1, 1, 1, 1, null));
  865. worksheet.Range[UtilsController.GetExcelColumnName(6) + rowCount.ToString()].Text = string.Format("{0:#,0}", totalSubP).ToString() + "%";
  866. rowAttributedList = rowHandler(rowAttributedList, new RowAttributed(UtilsController.GetExcelColumnName(6), rowCount.ToString(), true, null, null, 1, 1, 1, 1, null));
  867. }
  868. if (dataNow != null && dataNow.connDatas.data.Count > 0)
  869. {
  870. worksheet.Range[UtilsController.GetExcelColumnName(7) + rowCount.ToString()].Text = string.Format("{0:#,0}", double.Parse(dataNow.connDatas.data[0].totalSub)).ToString();
  871. rowAttributedList = rowHandler(rowAttributedList, new RowAttributed(UtilsController.GetExcelColumnName(7), rowCount.ToString(), true, null, null, 1, 1, 1, 1, null));
  872. }
  873. for (int l = 0; l < dataThisMonth.Count; l++)
  874. {
  875. if (dataThisMonth[l].connDatas.data.Count > 0)
  876. {
  877. worksheet.Range[UtilsController.GetExcelColumnName(dataNow != null ? 7 + l + 1 : 7 + l) + rowCount.ToString()].Text = string.Format("{0:#,0}", double.Parse(dataThisMonth[l].connDatas.data[0].totalSub)).ToString();
  878. rowAttributedList = rowHandler(rowAttributedList, new RowAttributed(UtilsController.GetExcelColumnName(dataNow != null ? 7 + l + 1 : 7 + l), rowCount.ToString(), true, Color.FromArgb(255, 0, 0), null, 1, 1, 1, 1, null));
  879. }
  880. }
  881. rowCount++;
  882. }
  883. worksheet.Range[UtilsController.GetExcelColumnName(2) + rowCount.ToString()].Text = "TOTAL";
  884. rowCount++;
  885. }
  886. }
  887. //Formatting
  888. //Global styles should be used when the same style needs to be applied to more than one cell. This usage of a global style reduces memory usage.
  889. //Add custom colors to the palette
  890. workbook.SetPaletteColor(8, Color.FromArgb(255, 174, 33));
  891. //Defining header style
  892. IStyle headerStyle = workbook.Styles.Add("HeaderStyle" + i.ToString());
  893. headerStyle.BeginUpdate();
  894. headerStyle.Color = Color.FromArgb(82, 190, 128);
  895. headerStyle.Font.Bold = true;
  896. headerStyle.Borders[ExcelBordersIndex.EdgeLeft].LineStyle = ExcelLineStyle.Thin;
  897. headerStyle.Borders[ExcelBordersIndex.EdgeRight].LineStyle = ExcelLineStyle.Thin;
  898. headerStyle.Borders[ExcelBordersIndex.EdgeTop].LineStyle = ExcelLineStyle.Thin;
  899. headerStyle.Borders[ExcelBordersIndex.EdgeBottom].LineStyle = ExcelLineStyle.Thin;
  900. headerStyle.EndUpdate();
  901. //Apply Header style
  902. worksheet.Rows[0].CellStyle = headerStyle;
  903. for (int g = 0; g < rowAttributedList.Count; g++)
  904. {
  905. worksheet.Range[rowAttributedList[g].character + rowAttributedList[g].number].CellStyle = setStyleRowExcel(workbook, i, rowAttributedList[g]);
  906. }
  907. //Auto-fit the columns
  908. worksheet.UsedRange.AutofitColumns();
  909. }
  910. //Saving the workbook as stream
  911. //FileStream stream = new FileStream("GlobalStyles.xlsx", FileMode.Create, FileAccess.ReadWrite);
  912. //workbook.SaveAs(stream);
  913. //stream.Dispose();
  914. using (var stream = new MemoryStream())
  915. {
  916. workbook.SaveAs(stream);
  917. var content = stream.ToArray();
  918. return File(
  919. content,
  920. "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
  921. "report_" + DateTime.Now.ToString("dd_MM") + ".xlsx");
  922. }
  923. }
  924. }
  925. catch (Exception ex)
  926. {
  927. log.Error("Exception: ", ex);
  928. return Redirect("/Home");
  929. }
  930. }
  931. private IStyle setStyleRowExcel(IWorkbook workbook, int worksheet, RowAttributed row)
  932. {
  933. IStyle bodyStyle = workbook.Styles.Add(row.character + row.number + "Style" + worksheet.ToString());
  934. bodyStyle.BeginUpdate();
  935. if (row.color != null)
  936. {
  937. bodyStyle.Font.RGBColor = (Color)row.color;
  938. }
  939. if (row.bold != null)
  940. {
  941. bodyStyle.Font.Bold = row.bold;
  942. }
  943. if (row.background != null)
  944. {
  945. bodyStyle.Color = (Color)row.background;
  946. }
  947. if (row.thinLeft != null)
  948. {
  949. bodyStyle.Borders[ExcelBordersIndex.EdgeLeft].LineStyle = ExcelLineStyle.Thin;
  950. }
  951. if (row.thinRight != null)
  952. {
  953. bodyStyle.Borders[ExcelBordersIndex.EdgeRight].LineStyle = ExcelLineStyle.Thin;
  954. }
  955. if (row.thinTop != null)
  956. {
  957. bodyStyle.Borders[ExcelBordersIndex.EdgeTop].LineStyle = ExcelLineStyle.Thin;
  958. }
  959. if (row.thinBottom != null)
  960. {
  961. bodyStyle.Borders[ExcelBordersIndex.EdgeBottom].LineStyle = ExcelLineStyle.Thin;
  962. }
  963. if (row.center != null)
  964. {
  965. bodyStyle.HorizontalAlignment = ExcelHAlign.HAlignCenter;
  966. }
  967. else
  968. {
  969. bodyStyle.HorizontalAlignment = ExcelHAlign.HAlignLeft;
  970. }
  971. bodyStyle.EndUpdate();
  972. return bodyStyle;
  973. }
  974. private List<RowAttributed> rowHandler(List<RowAttributed> rowList, RowAttributed row)
  975. {
  976. RowAttributed getRow = rowList.Find(x => x.character == row.character && x.number == row.number);
  977. if (getRow != null)
  978. {
  979. rowList.Where(S => S.character == row.character && S.number == row.number).Select(S =>
  980. {
  981. S.color = row.color != null ? row.color : S.color;
  982. S.bold = row.bold != null ? row.bold : S.bold;
  983. S.background = row.background != null ? row.background : S.background;
  984. S.thinLeft = row.thinLeft != null ? row.thinLeft : S.thinLeft;
  985. S.thinRight = row.thinRight != null ? row.thinRight : S.thinRight;
  986. S.thinTop = row.thinTop != null ? row.thinTop : S.thinTop;
  987. S.thinBottom = row.thinBottom != null ? row.thinBottom : S.thinBottom;
  988. return S;
  989. }).ToList();
  990. }
  991. else
  992. {
  993. rowList.Add(row);
  994. }
  995. return rowList;
  996. }
  997. protected void CreateAuthToken()
  998. {
  999. // create session authen
  1000. // Create the random value we will use to secure the session.
  1001. string authId = GenerateAuthId();
  1002. // Store the value in both our Session and a Cookie.
  1003. HttpContext.Session.SetString("AuthorizationCookieId", authId);
  1004. string sessionValue = HttpContext.Session.GetString("AuthorizationCookieId");
  1005. //CookieOptions option = new CookieOptions
  1006. //{
  1007. // Expires = DateTime.Now.AddMinutes(1)
  1008. //};
  1009. //Response.Cookies.Append("Key Name", "Value", option);
  1010. CookieOptions options = new CookieOptions()
  1011. {
  1012. //Path = "/",
  1013. //HttpOnly = true,
  1014. //Secure = false,
  1015. //SameSite = SameSiteMode.None
  1016. Expires = DateTime.Now.AddMinutes(60)
  1017. };
  1018. HttpContext.Response.Cookies.Append("AuthorizationCookie", authId, options);
  1019. string cookieValue = HttpContext.Request.Cookies["AuthorizationCookie"];
  1020. }
  1021. protected bool CheckAuthToken()
  1022. {
  1023. string cookieValue = HttpContext.Request.Cookies["AuthorizationCookie"];
  1024. string sessionValue = HttpContext.Session.GetString("AuthorizationCookieId");
  1025. if (cookieValue == null || sessionValue == null || cookieValue != sessionValue)
  1026. {
  1027. // Invalidate the session and log out the current user.
  1028. return false;
  1029. }
  1030. else
  1031. {
  1032. return true;
  1033. }
  1034. }
  1035. protected bool ClearCache()
  1036. {
  1037. HttpContext.Session.Clear();
  1038. foreach (var cookieKey in HttpContext.Request.Cookies.Keys)
  1039. {
  1040. HttpContext.Response.Cookies.Delete(cookieKey);
  1041. }
  1042. return true;
  1043. }
  1044. private string GenerateAuthId()
  1045. {
  1046. using (RandomNumberGenerator rng = new RNGCryptoServiceProvider())
  1047. {
  1048. byte[] tokenData = new byte[32];
  1049. rng.GetBytes(tokenData);
  1050. return Convert.ToBase64String(tokenData);
  1051. }
  1052. }
  1053. }
  1054. }