432 lines
16 KiB
C#
432 lines
16 KiB
C#
using ClosedXML.Excel;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.AspNetCore.Mvc.Rendering;
|
|
using Newtonsoft.Json;
|
|
using System.Diagnostics;
|
|
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;
|
|
string time_sheet = 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 dataIni, DateTime dataFin, 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;
|
|
time_sheet = helper.GetStringValue("time_sheet");
|
|
ViewBag.TimeSheet = time_sheet;
|
|
//urlBase = apiUrl + "timbratureList";
|
|
urlBase = apiUrl + "timbratureListVW";
|
|
urlBase = urlBase + "?token=" + token;
|
|
Uri baseAddress = new Uri(urlBase);
|
|
client = new HttpClient();
|
|
client.BaseAddress = baseAddress;
|
|
|
|
List<TimbraturaVW> modelList = new List<TimbraturaVW>();
|
|
|
|
HttpResponseMessage response = client.GetAsync(baseAddress).Result;
|
|
|
|
if (response.IsSuccessStatusCode)
|
|
{
|
|
string data = response.Content.ReadAsStringAsync().Result;
|
|
modelList = JsonConvert.DeserializeObject<List<TimbraturaVW>>(data);
|
|
|
|
ViewBag.Timbrature = GetCommesse();
|
|
|
|
if (dataIni.Date != DateTime.MinValue)
|
|
{
|
|
modelList = modelList.Where(x => x.data_timbratura.GetValueOrDefault().Date >= dataIni.Date).ToList();
|
|
ViewData["dataIni"] = dataIni;
|
|
|
|
}
|
|
else
|
|
ViewData["dataIni"] = null;
|
|
|
|
if (dataFin.Date != DateTime.MinValue)
|
|
{
|
|
modelList = modelList.Where(x => x.data_timbratura.GetValueOrDefault().Date <= dataFin.Date).ToList();
|
|
ViewData["dataFin"] = dataFin;
|
|
}
|
|
else
|
|
ViewData["dataFin"] = null;
|
|
|
|
if (!string.IsNullOrEmpty(commessa))
|
|
{
|
|
modelList = modelList.Where(s => s.commessa.Contains(commessa)).ToList();
|
|
|
|
ViewData["commessa"] = commessa;
|
|
}
|
|
else
|
|
ViewData["commessa"] = null;
|
|
|
|
if (!string.IsNullOrEmpty(tecnico))
|
|
{
|
|
modelList = modelList.Where(s => s.tecnico.ToLower().Contains(tecnico.ToLower())).ToList();
|
|
ViewData["tecnico"] = tecnico;
|
|
}
|
|
else
|
|
ViewData["tecnico"] = null;
|
|
|
|
if (page != null && page < 1)
|
|
{
|
|
page = 1;
|
|
}
|
|
|
|
var pageSize = 10;
|
|
|
|
var shortLinks = modelList
|
|
.OrderByDescending(s => s.id)
|
|
.ToPagedList(page ?? 1, pageSize);
|
|
|
|
ViewBag.Tecnici = GetTecnici();
|
|
|
|
|
|
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 + "timbratureListVW";
|
|
urlBase = urlBase + "?token=" + token;
|
|
Uri baseAddress = new Uri(urlBase);
|
|
client = new HttpClient();
|
|
client.BaseAddress = baseAddress;
|
|
|
|
TimbraturaVW rapp = new TimbraturaVW();
|
|
|
|
List<TimbraturaVW> modelList = new List<TimbraturaVW>();
|
|
|
|
HttpResponseMessage response = client.GetAsync(baseAddress).Result;
|
|
|
|
if (response.IsSuccessStatusCode)
|
|
{
|
|
string data = response.Content.ReadAsStringAsync().Result;
|
|
modelList = JsonConvert.DeserializeObject<List<TimbraturaVW>>(data);
|
|
rapp = modelList.Where(x => x.id.Equals(id)).First();
|
|
//string descTecnico = GetDescTecnico(rapp.tecnico);
|
|
//rapp.tecnico = descTecnico;
|
|
|
|
}
|
|
else
|
|
{
|
|
errMes = response.Content.ReadAsStringAsync().Result;
|
|
helper.SetStringValue("errMsg", errMes);
|
|
return RedirectToAction("Error");
|
|
}
|
|
|
|
return View(rapp);
|
|
}
|
|
|
|
#endregion DETAILS
|
|
|
|
#region metodi interni
|
|
|
|
private List<SelectListItem> GetTecnici()
|
|
{
|
|
SessionHelper helper = new SessionHelper(this);
|
|
token = helper.GetStringValue("tok");
|
|
apiUrl = helper.GetStringValue("apiUrl");
|
|
urlBase = apiUrl + "tecniciList";
|
|
urlBase = urlBase + "?token=" + token;
|
|
Uri baseAddress = new Uri(urlBase);
|
|
client = new HttpClient();
|
|
client.BaseAddress = baseAddress;
|
|
List<SelectListItem> selectItems = new List<SelectListItem>();
|
|
List<Tecnici> modelList = new List<Tecnici>();
|
|
HttpResponseMessage response = client.GetAsync(baseAddress).Result;
|
|
if (response.IsSuccessStatusCode)
|
|
{
|
|
string data = response.Content.ReadAsStringAsync().Result;
|
|
modelList = JsonConvert.DeserializeObject<List<Tecnici>>(data);
|
|
modelList = modelList.Where(x => x.tcdatobs == null).ToList();
|
|
//per gestire primo elemento tendina (deve essere vuoto)
|
|
SelectListItem listItemFirst = new SelectListItem();
|
|
|
|
listItemFirst.Value = string.Empty;
|
|
listItemFirst.Text = " - Tecnico";
|
|
selectItems.Add(listItemFirst);
|
|
|
|
foreach (var role in modelList)
|
|
{
|
|
SelectListItem listItem = new SelectListItem();
|
|
|
|
string s = role.tccodice + " - " + role.tcdescri;
|
|
listItem.Value = role.tccodice;
|
|
listItem.Text = s/*role.tcdescri*/;
|
|
|
|
selectItems.Add(listItem);
|
|
}
|
|
}
|
|
|
|
return selectItems;
|
|
}
|
|
|
|
private List<SelectListItem> GetCommesse()
|
|
{
|
|
SessionHelper helper = new SessionHelper(this);
|
|
token = helper.GetStringValue("tok");
|
|
apiUrl = helper.GetStringValue("apiUrl");
|
|
urlBase = apiUrl + "commesseList";
|
|
urlBase = urlBase + "?token=" + token;
|
|
Uri baseAddress = new Uri(urlBase);
|
|
client = new HttpClient();
|
|
client.BaseAddress = baseAddress;
|
|
List<SelectListItem> selectItems = new List<SelectListItem>();
|
|
List<CommesseVT> modelList = new List<CommesseVT>();
|
|
HttpResponseMessage response = client.GetAsync(baseAddress).Result;
|
|
|
|
if (response.IsSuccessStatusCode)
|
|
{
|
|
string data = response.Content.ReadAsStringAsync().Result;
|
|
modelList = JsonConvert.DeserializeObject<List<CommesseVT>>(data);
|
|
|
|
//per gestire primo elemento tendina (deve essere vuoto)
|
|
SelectListItem listItemFirt = new SelectListItem();
|
|
|
|
listItemFirt.Value = string.Empty;
|
|
listItemFirt.Text = " - Commessa";
|
|
selectItems.Add(listItemFirt);
|
|
|
|
var appoggio = string.Empty;
|
|
|
|
foreach (var role in modelList)
|
|
{
|
|
if (!appoggio.Equals(role.lacodcom))
|
|
{
|
|
SelectListItem listItem = new SelectListItem();
|
|
string s = role.lacodcom;
|
|
listItem.Value = role.lacodcom;
|
|
listItem.Text = s;
|
|
selectItems.Add(listItem);
|
|
}
|
|
|
|
appoggio = role.lacodcom;
|
|
}
|
|
}
|
|
|
|
return selectItems;
|
|
}
|
|
|
|
public IActionResult ExportExcel(DateTime dataIni, DateTime dataFin, string commessa, string? tecnico)
|
|
{
|
|
SessionHelper helper = new SessionHelper(this);
|
|
|
|
//16/09/2024 messo questo controllo perchè se faccio esporta senza aver fatto cerca va in errore quando crea file excel.
|
|
if (tecnico == null || tecnico.Equals("System.Collections.Generic.List`1[Microsoft.AspNetCore.Mvc.Rendering.SelectListItem]"))
|
|
{
|
|
tecnico = string.Empty;
|
|
}
|
|
|
|
if (commessa.Equals("System.Collections.Generic.List`1[Microsoft.AspNetCore.Mvc.Rendering.SelectListItem]"))
|
|
{
|
|
commessa = string.Empty;
|
|
}
|
|
|
|
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 (dataIni.Date != DateTime.MinValue)
|
|
{
|
|
modelList = modelList.Where(x => x.data_timbratura.GetValueOrDefault().Date >= dataIni.Date).ToList();
|
|
}
|
|
|
|
if (dataFin.Date != DateTime.MinValue)
|
|
{
|
|
modelList = modelList.Where(x => x.data_timbratura.GetValueOrDefault().Date <= dataFin.Date).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");
|
|
}
|
|
}
|
|
|
|
private string GetDescTecnico(string codTec)
|
|
{
|
|
SessionHelper helper = new SessionHelper(this);
|
|
token = helper.GetStringValue("tok");
|
|
apiUrl = helper.GetStringValue("apiUrl");
|
|
urlBase = apiUrl + "tecniciList";
|
|
urlBase = urlBase + "?token=" + token;
|
|
Uri baseAddress = new Uri(urlBase);
|
|
client = new HttpClient();
|
|
client.BaseAddress = baseAddress;
|
|
|
|
string descrizioneTecnico=string.Empty;
|
|
List<Tecnici> modelList = new List<Tecnici>();
|
|
|
|
HttpResponseMessage response = client.GetAsync(baseAddress).Result;
|
|
if (response.IsSuccessStatusCode)
|
|
{
|
|
string data = response.Content.ReadAsStringAsync().Result;
|
|
modelList = JsonConvert.DeserializeObject<List<Tecnici>>(data);
|
|
modelList = modelList.Where(x => x.tcdatobs == null && x.tccodice.Equals(codTec)).ToList();
|
|
|
|
foreach (var role in modelList)
|
|
{
|
|
SelectListItem listItem = new SelectListItem();
|
|
|
|
descrizioneTecnico = role.tccodice.Trim() + " - " + role.tcdescri.Trim();
|
|
}
|
|
}
|
|
|
|
return descrizioneTecnico;
|
|
}
|
|
|
|
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
|
|
public IActionResult Error()
|
|
{
|
|
SessionHelper helper = new SessionHelper(this);
|
|
string e = helper.GetStringValue("errMsg");
|
|
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier, ErrMsg = e });
|
|
}
|
|
|
|
#endregion metodi interni
|
|
}
|
|
}
|