using Contracts.ViewModels; using DocumentFormat.OpenXml.Spreadsheet; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; namespace ImplementerApp.Controllers { public class ReportController : Controller { private readonly ILogger _logger; private readonly ImplementerData _data; public ReportController(ILogger logger, ImplementerData data) { _logger = logger; _data = data; } private bool IsLoggedIn { get { return UserImplementer.user != null; } } private int UserId { get { return UserImplementer.user!.Id; } } [HttpGet] public IActionResult DetailTimeChoose() { if (!IsLoggedIn) return RedirectToAction("IndexNonReg", "Home"); return View(); } [HttpPost] public IActionResult TimeReportWeb(DateTime startDate, DateTime endDate) { HttpContext.Session.SetString("StartDate", startDate.ToString()); HttpContext.Session.SetString("EndDate", endDate.ToString()); return RedirectToAction("DetailTimeReport"); } [HttpGet] public IActionResult DetailTimeReport() { if (!IsLoggedIn) return RedirectToAction("IndexNonReg", "Home"); try { var startDateStr = HttpContext.Session.GetString("StartDate"); var endDateStr = HttpContext.Session.GetString("EndDate"); var startDate = DateTime.Parse(startDateStr); var endDate = DateTime.Parse(endDateStr).AddDays(1); var values = _data.GetTimeReport(startDate, endDate, UserId); ViewBag.StartDate = startDate; ViewBag.EndDate = endDate; return View(values); } catch { return RedirectToAction("Index", "Home"); } } [HttpPost] public void DetailTimeMail() { try { var startDateStr = HttpContext.Session.GetString("StartDate"); var endDateStr = HttpContext.Session.GetString("EndDate"); var startDate = DateTime.Parse(startDateStr); var endDate = DateTime.Parse(endDateStr).AddDays(1); using (MemoryStream memoryStream = new MemoryStream()) { _data.SendMailReport(startDate, endDate, UserId, memoryStream); } Response.Redirect("DetailTimeReport"); } catch { Response.Redirect("Error"); } } [HttpGet] public IActionResult DetailWorkshopChoose() { if (!IsLoggedIn) return RedirectToAction("IndexNonReg", "Home"); try { var details = _data.GetDetails(UserId); return View(details); } catch { return RedirectToAction("Error"); } } [HttpPost] public IActionResult DetailWorkshopChoose(List selectedItems, string reportType) { try { string value = string.Join("/", selectedItems); HttpContext.Session.SetString("Details", value); if (reportType.Equals("default")) return RedirectToAction("DetailWorkshopReport"); else if (reportType.Equals("excel")) return RedirectToAction("ExcelGenerate"); else return RedirectToAction("WordGenerate"); } catch { return RedirectToAction("Error"); } } public async Task ExcelGenerate() { try { var value = HttpContext.Session.GetString("Details"); if (value != null) { List rawReports = value!.Split('/').Select(x => int.Parse(x)).ToList(); using (MemoryStream memoryStream = new MemoryStream()) { _data.SaveReportExcel(rawReports, memoryStream); memoryStream.Seek(0, SeekOrigin.Begin); var outputStream = new MemoryStream(); await memoryStream.CopyToAsync(outputStream); outputStream.Seek(0, SeekOrigin.Begin); return File(outputStream, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "ReportExcel.xlsx"); } } return RedirectToAction("DetailWorkshopChoose"); } catch { return RedirectToAction("Error"); } } public async Task WordGenerate() { try { var value = HttpContext.Session.GetString("Details"); if (value != null) { List rawReports = value!.Split('/').Select(x => int.Parse(x)).ToList(); using (MemoryStream memoryStream = new MemoryStream()) { _data.SaveReportWord(rawReports, memoryStream); memoryStream.Seek(0, SeekOrigin.Begin); var outputStream = new MemoryStream(); await memoryStream.CopyToAsync(outputStream); outputStream.Seek(0, SeekOrigin.Begin); return File(outputStream, "application/vnd.openxmlformats-officedocument.wordprocessingml.document", "ReportWord.docx"); } } return RedirectToAction("DetailWorkshopChoose"); } catch { return RedirectToAction("Error"); } } [HttpGet] public IActionResult DetailWorkshopReport() { if (!IsLoggedIn) return RedirectToAction("IndexNonReg", "Home"); try { var value = HttpContext.Session.GetString("Details"); if (value != null) { List rawReports = value!.Split('/').Select(x => int.Parse(x)).ToList(); var reports = _data.GetWorkshopReports(rawReports); return View(reports); } } catch { return RedirectToAction("Error"); } return View(new List()); } public IActionResult ReportsMenu() { return View(); } [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)] public IActionResult Error() { return RedirectToAction("IndexNonReg", "Home"); } } }