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"); return View(); } [HttpPost] public IActionResult SendReport(DateTime startDate, DateTime endDate) { return Ok(); } [HttpPost] public IActionResult TimeReportWeb(DateTime startDate, DateTime endDate) { if (!IsLoggedIn) return RedirectToAction("IndexNonReg"); HttpContext.Session.SetString("StartDate", startDate.ToString()); HttpContext.Session.SetString("EndDate", endDate.ToString()); return RedirectToAction("DetailTimeReport"); } [HttpGet] public IActionResult DetailTimeReport() { 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); } [HttpPost] public void DetailTimeMail() { 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"); } [HttpGet] public IActionResult DetailWorkshopChoose() { if (!IsLoggedIn) return RedirectToAction("IndexNonReg"); var details = _data.GetDetails(UserId); return View(details); } [HttpPost] public IActionResult DetailWorkshopChoose(List selectedItems, string reportType) { 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"); } public async Task ExcelGenerate() { 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"); } public async Task WordGenerate() { 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"); } [HttpGet] public IActionResult DetailWorkshopReport() { 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); } return View(new List()); } public IActionResult ReportsMenu() { return View(); } [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)] public IActionResult Error() { return View(); } } }