diff --git a/Controllers/RapportiniController.cs b/Controllers/RapportiniController.cs index 35d0ce4..f4d8145 100644 --- a/Controllers/RapportiniController.cs +++ b/Controllers/RapportiniController.cs @@ -1,6 +1,8 @@ -using ClosedXML.Excel; -using DocumentFormat.OpenXml.Spreadsheet; +//using AspNetCore; +using ClosedXML.Excel; +//using DocumentFormat.OpenXml.Spreadsheet; using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Mvc.Rendering; using Newtonsoft.Json; using System.Diagnostics; using VirtualTask.Models; @@ -29,8 +31,9 @@ namespace VirtualTask.Controllers apiUrl = key; } + #region INDEX - public IActionResult Index(string searchString, int? page = 1) + public IActionResult Index(string Impianto, int? page = 1) { SessionHelper helper = new SessionHelper(this); token = helper.GetStringValue("tok"); @@ -56,11 +59,15 @@ namespace VirtualTask.Controllers string data = response.Content.ReadAsStringAsync().Result; modelList = JsonConvert.DeserializeObject>(data); - if (!string.IsNullOrEmpty(searchString)) - { - modelList = modelList.Where(s => s.seriale_rapportino.ToUpper().Contains(searchString.ToUpper())).ToList(); + ViewBag.Rapportini = getImpianti(); - ViewData["CurrentFilter"] = searchString; + if (!string.IsNullOrEmpty(Impianto)) + { + modelList = modelList.Where(s => s.codice_impianto.Contains(Impianto)).ToList(); + + ViewData["CurrentFilter"] = Impianto; + + ViewBag.Impianto = Impianto; } else ViewData["CurrentFilter"] = null; @@ -86,7 +93,12 @@ namespace VirtualTask.Controllers } } - public IActionResult ExportDataToExcel(DateTime dataIni, DateTime dataFin) + #endregion INDEX + + #region metodi interni + + [HttpPost] + public IActionResult ExportDataToExcel(DateTime dataIni, DateTime dataFin, string Impianto) { SessionHelper helper = new SessionHelper(this); token = helper.GetStringValue("tok"); @@ -106,6 +118,8 @@ namespace VirtualTask.Controllers List modelList = new List(); + //modelList = lista; + HttpResponseMessage response = client.GetAsync(baseAddress).Result; if (response.IsSuccessStatusCode) @@ -114,6 +128,11 @@ namespace VirtualTask.Controllers modelList = JsonConvert.DeserializeObject>(data); + //ViewData["CurrentFilter"] = Impianto; + + modelList = modelList.Where(x => x.codice_impianto.Equals(Impianto)).ToList(); + //ViewBag.Impianto = Impianto; + if (!dataFin.ToString().Substring(0, 10).Equals("01/01/0001")) { modelList = modelList.Where(x => x.data_rapportino.GetValueOrDefault().Date >= dataIni.Date && x.data_rapportino.GetValueOrDefault().Date <= dataFin.Date).ToList(); @@ -128,12 +147,12 @@ namespace VirtualTask.Controllers var ws = workbook.Worksheet("sheetName"); int col = 1; - + //Scrivo intestazioni colonne foreach (var item in modelList[0].GetType().GetProperties()) { ws.Cell(1, col).Style = ws.Cell(1, col).Style.Font.SetBold(); - ws.Cell(1, col).Value = item.Name.ToString().Replace("_"," ").ToUpper(); + ws.Cell(1, col).Value = item.Name.ToString().Replace("_", " ").ToUpper(); col++; } @@ -149,14 +168,13 @@ namespace VirtualTask.Controllers { ws.Cell(row + 2, colonna).Value = prop.GetValue(item).ToString(); - colonna ++; + colonna++; } - row ++; + row++; } //Salvo il file Excel - var anno = DateTime.Now.Year.ToString(); var mese = DateTime.Now.Month.ToString(); var giorno = DateTime.Now.Day.ToString(); @@ -166,10 +184,9 @@ namespace VirtualTask.Controllers var dateFile = anno + mese + giorno + ore + minuti + secondi; - workbook.SaveAs($"C:\\Users\\utente\\Desktop\\ExcelVT\\rapportini_{dateFile}.xlsx"); + workbook.SaveAs($"C:\\Users\\utente\\Desktop\\ExcelVT\\buoni_{dateFile}.xlsx"); return RedirectToAction("Index"); - } else { @@ -179,6 +196,46 @@ namespace VirtualTask.Controllers } } + private List getImpianti() + { + SessionHelper helper = new SessionHelper(this); + token = helper.GetStringValue("tok"); + apiUrl = helper.GetStringValue("apiUrl"); + //urlBase = apiUrl + "impiantiListMngr"; + urlBase = apiUrl + "rapportiniList"; + urlBase = urlBase + "?token=" + token; + Uri baseAddress = new Uri(urlBase); + client = new HttpClient(); + client.BaseAddress = baseAddress; + List selectItems = new List(); + List modelList = new List(); + HttpResponseMessage response = client.GetAsync(baseAddress).Result; + if (response.IsSuccessStatusCode) + { + string data = response.Content.ReadAsStringAsync().Result; + modelList = JsonConvert.DeserializeObject>(data); + + //per gestire primo elemento tendina (deve essere vuoto) + SelectListItem listItemFirt = new SelectListItem(); + + listItemFirt.Value = string.Empty; + listItemFirt.Text = " - Impianto"; + selectItems.Add(listItemFirt); + + foreach (var role in modelList) + { + SelectListItem listItem = new SelectListItem(); + //string s = role.imcodimp + " - " + role.imdescri; + //listItem.Value = role.imcodimp; + string s = role.codice_impianto; + listItem.Value = role.codice_impianto; + listItem.Text = s; + selectItems.Add(listItem); + } + } + return selectItems; + } + [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)] public IActionResult Error() { @@ -186,5 +243,8 @@ namespace VirtualTask.Controllers string e = helper.GetStringValue("errMsg"); return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier, ErrMsg = e }); } + + #endregion metodi interni + } } diff --git a/Models/Rapportini.cs b/Models/Rapportini.cs index d7932ee..46db912 100644 --- a/Models/Rapportini.cs +++ b/Models/Rapportini.cs @@ -1,4 +1,6 @@ -namespace VirtualTask.Models +using System.ComponentModel.DataAnnotations; + +namespace VirtualTask.Models { public class Rapportini { @@ -15,6 +17,7 @@ /// /// Codice Impianto /// + [Display(Name = "Impianto")] public string? codice_impianto { get; set; } diff --git a/Views/Rapportini/Index.cshtml b/Views/Rapportini/Index.cshtml index 2a7096c..95c8c84 100644 --- a/Views/Rapportini/Index.cshtml +++ b/Views/Rapportini/Index.cshtml @@ -6,7 +6,7 @@ @{ - ViewData["Title"] = "Rapportini"; + ViewData["Title"] = "Esporta buoni su Excel"; Layout = "~/Views/Shared/_LayoutAreaRiservata.cshtml"; } @@ -14,24 +14,39 @@
- @using (Html.BeginForm()) + @using (Html.BeginForm("Index", "Rapportini")) {
-
@Html.TextBox("SearchString", null, new { placeholder = "Cerca rapportino", @class = "agy-form-field require" })
+
+ Impianto: + @Html.DropDownList("Impianto", ViewBag.Rapportini, null, new { @class = "agy-form-field require" }) +
+
+
 
+
 
 
-
}
+
+
@using (Html.BeginForm("ExportDataToExcel", "Rapportini")) { -
@Html.TextBox("dataIni", null, new { type = "date", @class = "agy-form-field require" })
-
@Html.TextBox("dataFin", null, new { type = "date", @class = "agy-form-field require" })
-
- + Data da: + @Html.TextBox("dataIni", null, new { type = "date", @class = "agy-form-field require" }) +
+ +
+ Data a: + @Html.TextBox("dataFin", null, new { type = "date", @class = "agy-form-field require" })
+ @Html.Hidden("Impianto", ViewBag.Rapportini, null) + +
+ +
} @@ -103,63 +118,3 @@ - - -@*

- Create New -

-
- - - - - - - - - - - - -@foreach (var item in Model) { - - - - - - - - - -} - -
- @Html.DisplayNameFor(model => model.seriale_rapportino) - - @Html.DisplayNameFor(model => model.azienda_impianto) - - @Html.DisplayNameFor(model => model.codice_impianto) - - @Html.DisplayNameFor(model => model.seriale_chiamata) - - @Html.DisplayNameFor(model => model.seriale_commessa) - - @Html.DisplayNameFor(model => model.data_rapportino) -
- @Html.DisplayFor(modelItem => item.seriale_rapportino) - - @Html.DisplayFor(modelItem => item.azienda_impianto) - - @Html.DisplayFor(modelItem => item.codice_impianto) - - @Html.DisplayFor(modelItem => item.seriale_chiamata) - - @Html.DisplayFor(modelItem => item.seriale_commessa) - - @Html.DisplayFor(modelItem => item.data_rapportino) - - @Html.ActionLink("Edit", "Edit", new { /* id=item.PrimaryKey */ }) | - @Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey */ }) | - @Html.ActionLink("Delete", "Delete", new { /* id=item.PrimaryKey */ }) -
- *@ \ No newline at end of file