266 lines
9.1 KiB
C#
266 lines
9.1 KiB
C#
using ClosedXML.Excel;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Newtonsoft.Json;
|
|
using VirtualTask.Models;
|
|
using X.PagedList;
|
|
|
|
namespace VirtualTask.Controllers
|
|
{
|
|
public class TimbratureController : Controller
|
|
{
|
|
string apiUrl = string.Empty;
|
|
string urlBase = string.Empty;
|
|
string token = string.Empty;
|
|
string tenant = string.Empty;
|
|
string errMes = string.Empty;
|
|
string admin = string.Empty;
|
|
|
|
HttpClient client;
|
|
|
|
private readonly IConfiguration _configuration;
|
|
|
|
public TimbratureController(IConfiguration configuration)
|
|
{
|
|
client = new HttpClient();
|
|
_configuration = configuration;
|
|
var key = _configuration["ApplicationInsights:rootUrlApi"];
|
|
apiUrl = key;
|
|
}
|
|
|
|
#region INDEX
|
|
|
|
public IActionResult Index(DateTime dataTimb, string commessa, string tecnico, int? page = 1)
|
|
{
|
|
SessionHelper helper = new SessionHelper(this);
|
|
token = helper.GetStringValue("tok");
|
|
|
|
if (string.IsNullOrEmpty(token))
|
|
{
|
|
return RedirectToAction("Login2", "Login");
|
|
}
|
|
|
|
apiUrl = helper.GetStringValue("apiUrl");
|
|
admin = helper.GetStringValue("admin");
|
|
ViewBag.Admin = admin;
|
|
urlBase = apiUrl + "timbratureList";
|
|
urlBase = urlBase + "?token=" + token;
|
|
Uri baseAddress = new Uri(urlBase);
|
|
client = new HttpClient();
|
|
client.BaseAddress = baseAddress;
|
|
|
|
List<Timbratura> modelList = new List<Timbratura>();
|
|
|
|
HttpResponseMessage response = client.GetAsync(baseAddress).Result;
|
|
|
|
if (response.IsSuccessStatusCode)
|
|
{
|
|
string data = response.Content.ReadAsStringAsync().Result;
|
|
modelList = JsonConvert.DeserializeObject<List<Timbratura>>(data);
|
|
|
|
if (dataTimb.Date != DateTime.MinValue)
|
|
{
|
|
modelList = modelList.Where(x => x.data_timbratura.GetValueOrDefault().Date == dataTimb).ToList();
|
|
}
|
|
|
|
if (!string.IsNullOrEmpty(commessa))
|
|
{
|
|
modelList = modelList.Where(s => s.commessa.ToLower().Contains(commessa.ToLower())).ToList();
|
|
}
|
|
|
|
if (!string.IsNullOrEmpty(tecnico))
|
|
{
|
|
modelList = modelList.Where(s => s.tecnico.ToLower().Contains(tecnico.ToLower())).ToList();
|
|
|
|
}
|
|
//if (id != 0)
|
|
//{
|
|
// modelList = modelList.Where(s => s.id == id).ToList();
|
|
// ViewData["CurrentFilter"] = id;
|
|
//}
|
|
//else
|
|
//{
|
|
// ViewData["CurrentFilter"] = null;
|
|
//}
|
|
if (page != null && page < 1)
|
|
{
|
|
page = 1;
|
|
}
|
|
|
|
var pageSize = 10;
|
|
|
|
var shortLinks = modelList
|
|
.OrderByDescending(s => s.id)
|
|
.ToPagedList(page ?? 1, pageSize);
|
|
|
|
return View(shortLinks);
|
|
}
|
|
else
|
|
{
|
|
errMes = response.Content.ReadAsStringAsync().Result;
|
|
helper.SetStringValue("errMsg", errMes);
|
|
return RedirectToAction("Error");
|
|
}
|
|
}
|
|
|
|
#endregion INDEX
|
|
|
|
#region DETAILS
|
|
|
|
public IActionResult Details(int id)
|
|
{
|
|
SessionHelper helper = new SessionHelper(this);
|
|
|
|
|
|
token = helper.GetStringValue("tok");
|
|
|
|
apiUrl = helper.GetStringValue("apiUrl");
|
|
admin = helper.GetStringValue("admin");
|
|
ViewBag.Admin = admin;
|
|
urlBase = apiUrl + "timbratureList";
|
|
urlBase = urlBase + "?token=" + token;
|
|
Uri baseAddress = new Uri(urlBase);
|
|
client = new HttpClient();
|
|
client.BaseAddress = baseAddress;
|
|
|
|
Timbratura rapp = new Timbratura();
|
|
|
|
List<Timbratura> modelList = new List<Timbratura>();
|
|
|
|
HttpResponseMessage response = client.GetAsync(baseAddress).Result;
|
|
|
|
if (response.IsSuccessStatusCode)
|
|
{
|
|
string data = response.Content.ReadAsStringAsync().Result;
|
|
modelList = JsonConvert.DeserializeObject<List<Timbratura>>(data);
|
|
rapp = modelList.Where(x => x.id.Equals(id)).First();
|
|
}
|
|
else
|
|
{
|
|
errMes = response.Content.ReadAsStringAsync().Result;
|
|
helper.SetStringValue("errMsg", errMes);
|
|
return RedirectToAction("Error");
|
|
}
|
|
|
|
return View(rapp);
|
|
}
|
|
|
|
#endregion DETAILS
|
|
|
|
#region metodi interni
|
|
|
|
public IActionResult ExportExcel(DateTime dataTimb, string commessa, string tecnico)
|
|
{
|
|
SessionHelper helper = new SessionHelper(this);
|
|
token = helper.GetStringValue("tok");
|
|
if (string.IsNullOrEmpty(token))
|
|
{
|
|
return RedirectToAction("Login2", "Login");
|
|
}
|
|
|
|
apiUrl = helper.GetStringValue("apiUrl");
|
|
admin = helper.GetStringValue("admin");
|
|
ViewBag.Admin = admin;
|
|
urlBase = apiUrl + "timbratureList";
|
|
urlBase = urlBase + "?token=" + token;
|
|
Uri baseAddress = new Uri(urlBase);
|
|
client = new HttpClient();
|
|
client.BaseAddress = baseAddress;
|
|
|
|
List<Timbratura> modelList = new List<Timbratura>();
|
|
|
|
HttpResponseMessage response = client.GetAsync(baseAddress).Result;
|
|
|
|
if (response.IsSuccessStatusCode)
|
|
{
|
|
string data = response.Content.ReadAsStringAsync().Result;
|
|
|
|
modelList = JsonConvert.DeserializeObject<List<Timbratura>>(data);
|
|
|
|
if (dataTimb.Date != DateTime.MinValue)
|
|
{
|
|
modelList = modelList.Where(x => x.data_timbratura.GetValueOrDefault().Date == dataTimb).ToList();
|
|
}
|
|
|
|
if (!string.IsNullOrEmpty(commessa))
|
|
{
|
|
modelList = modelList.Where(s => s.commessa.Contains(commessa)).ToList();
|
|
}
|
|
|
|
if (!string.IsNullOrEmpty(tecnico))
|
|
{
|
|
modelList = modelList.Where(s => s.tecnico.Contains(tecnico)).ToList();
|
|
|
|
}
|
|
|
|
var workbook = new XLWorkbook();
|
|
workbook.AddWorksheet("sheetName");
|
|
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();
|
|
|
|
col++;
|
|
}
|
|
|
|
var row = 0;
|
|
|
|
foreach (var item in modelList)
|
|
{
|
|
var colonna = 1;
|
|
//righe per ogni item nel modelList
|
|
foreach (var prop in item.GetType().GetProperties())
|
|
{
|
|
//controllo se item null assegno stringa vuota
|
|
//perchè se null da errore quando scrive l'excel
|
|
if (prop.GetValue(item) != null)
|
|
{
|
|
ws.Cell(row + 2, colonna).Value = prop.GetValue(item).ToString();
|
|
}
|
|
else
|
|
{
|
|
ws.Cell(row + 2, colonna).Value = string.Empty;
|
|
}
|
|
colonna++;
|
|
}
|
|
|
|
row++;
|
|
}
|
|
|
|
//Salvo il file Excel
|
|
var anno = DateTime.Now.Year.ToString();
|
|
var mese = DateTime.Now.Month.ToString();
|
|
var giorno = DateTime.Now.Day.ToString();
|
|
var ore = DateTime.Now.Hour.ToString();
|
|
var minuti = DateTime.Now.Minute.ToString();
|
|
var secondi = DateTime.Now.Second.ToString();
|
|
|
|
var dateFile = anno + mese + giorno + ore + minuti + secondi;
|
|
|
|
//workbook.SaveAs($"C:\\Users\\utente\\Desktop\\ExcelVT\\buoni_{dateFile}.xlsx");
|
|
string contentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
|
|
//string fileName = "authors.xlsx";
|
|
string fileName = $"Timbrature_{dateFile}.xlsx";
|
|
using (var stream = new MemoryStream())
|
|
{
|
|
workbook.SaveAs(stream);
|
|
var content = stream.ToArray();
|
|
return File(content, contentType, fileName);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
errMes = response.Content.ReadAsStringAsync().Result;
|
|
helper.SetStringValue("errMsg", errMes);
|
|
return RedirectToAction("Error");
|
|
}
|
|
}
|
|
|
|
#endregion metodi interni
|
|
}
|
|
}
|