VirtualTask/Controllers/ChiamateController.cs
2024-09-24 11:51:21 +02:00

697 lines
25 KiB
C#

using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Newtonsoft.Json;
using System.Data;
using System.Diagnostics;
using System.Text;
using VirtualTask.Models;
using X.PagedList;
namespace VirtualTask.Controllers
{
public class ChiamateController : Controller
{
string apiUrl = string.Empty;
string urlBase = string.Empty;
string token = string.Empty;
string tenant = string.Empty;
string errMes = string.Empty;
string _serchiam = "SER_CHIAMA";
string _numchiam = "NUM_CHIAMA";
string admin = string.Empty;
string time_sheet=string.Empty;
HttpClient client;
private readonly IConfiguration _configuration;
public ChiamateController(IConfiguration configuration)
{
client = new HttpClient();
_configuration = configuration;
var key = _configuration["ApplicationInsights:rootUrlApi"];
apiUrl = key;
}
#region INDEX
public IActionResult Index(string impianto, string tecnico, DateTime dataIni, DateTime dataFin, string stato, string indirizzo, 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 + "chiamateListMngr";
urlBase = urlBase + "?token=" + token;
Uri baseAddress = new Uri(urlBase);
client = new HttpClient();
client.BaseAddress = baseAddress;
List<Chiamate> modelList = new List<Chiamate>();
HttpResponseMessage response = client.GetAsync(baseAddress).Result;
if (response.IsSuccessStatusCode)
{
string data = response.Content.ReadAsStringAsync().Result;
modelList = JsonConvert.DeserializeObject<List<Chiamate>>(data);
ViewBag.Impianti = getImpianti();
ViewBag.Tecnici = getTecnici();
ViewBag.StatiChiamata = getStatiChiamata();
//if (!string.IsNullOrEmpty(searchString))
//{
// modelList = modelList.Where(s => s.chcodimp.ToUpper().Contains(searchString.ToUpper())).ToList();
// ViewData["CurrentFilter"] = searchString;
//}
//else
// ViewData["CurrentFilter"] = null;
if (!string.IsNullOrEmpty(impianto))
{
modelList = modelList.Where(s => s.chcodimp.Contains(impianto)).ToList();
ViewData["CurrentFilter"] = impianto;
ViewBag.Impianto = impianto;
}
else
ViewData["CurrentFilter"] = null;
if (!string.IsNullOrEmpty(tecnico))
{
modelList = modelList.Where(s => s.chtchiam !=null && s.chtchiam.Contains(tecnico)).ToList();
ViewData["CurrentFilter"] = tecnico;
ViewBag.Impianto = tecnico;
}
else
ViewData["CurrentFilter"] = null;
if (dataIni.Date != DateTime.MinValue)
{
modelList = modelList.Where(x => x.chdata.GetValueOrDefault().Date >= dataIni.Date).ToList();
}
if (dataFin.Date != DateTime.MinValue)
{
modelList = modelList.Where(x => x.chdata.GetValueOrDefault().Date <= dataFin.Date).ToList();
}
if (!string.IsNullOrEmpty(stato))
{
modelList = modelList.Where(x => x.chstato.Contains(stato)).ToList();
ViewData["CurrentFilter"] = stato;
ViewBag.Impianto = stato;
}
else
ViewData["CurrentFilter"] = null;
if (!string.IsNullOrEmpty(indirizzo))
{
modelList = modelList.Where(s => s.indirizzoBreve.ToUpper().Contains(indirizzo.ToUpper())).ToList();
ViewData["CurrentFilter"] = indirizzo;
}
else
ViewData["CurrentFilter"] = null;
if (page != null && page < 1)
{
page = 1;
}
var pageSize = 10;
var shortLinks = modelList
.OrderByDescending(s => s.chnumero)
.ToPagedList(page ?? 1, pageSize);
return View(shortLinks);
}
else
{
errMes = response.Content.ReadAsStringAsync().Result;
helper.SetStringValue("errMsg", errMes);
return RedirectToAction("Error");
}
}
#endregion INDEX
#region CREATE
public IActionResult Create()
{
SessionHelper helper = new SessionHelper(this);
admin = helper.GetStringValue("admin");
ViewBag.Admin = admin;
ViewBag.Impianti = getImpianti();
ViewBag.StatiChiamata = getStatiChiamata();
//ViewBag.TipiChiamata = getTipiChiamata();
ViewBag.CodiciSegnalazione = getCodiciSegnalazione();
ViewBag.Tecnici = getTecnici();
return View();
}
[HttpPost]
public IActionResult Create(Chiamate model)
{
SessionHelper helper = new SessionHelper(this);
token = helper.GetStringValue("tok");
tenant = helper.GetStringValue("tenant");
if (string.IsNullOrEmpty(token))
{
return RedirectToAction("Login2","Login");
}
#region campi da impostare
model.chcodazi = tenant;
model.chaziimp = tenant;
model.chserial = getNewSeriale();
model.chnumero=getNewNumeroChiamata();
//DateTime adesso = DateTime.Now;
//model.chdata = adesso;
//model.chdtass = adesso;
//model.chdtapp = adesso;
if (model.chdata == null)
{
model.chdata = DateTime.Now;
}
model.chdtapp = model.chdata;
model.chdtass = model.chdata;
model.chtipo = "A";//X=creato da app, A creato da adhoc. DEVO METTERE A perche altrimenti l'app lo tratta come una chiamata da commessa
model.chmodrac = "EMAIL";
//int year=adesso.Year;
//int ora = adesso.Hour;
//int min=adesso.Minute;
//model.chora = ora;
//model.choraapi = ora;
//model.chorass = ora;
//model.chmin = min;
//model.chminapi = min;
//model.chminass = min;
int year = model.chdata.Value.Year;
int hour = model.chdata.Value.Hour;
int minute = model.chdata.Value.Minute;
model.chora = hour;
model.chmin = minute;
model.choraapi = hour;
model.chminapi = minute;
model.chorass = hour;
model.chminass = minute;
model.chcodese=Convert.ToString(year);
#endregion
apiUrl = helper.GetStringValue("apiUrl");
admin = helper.GetStringValue("admin");
ViewBag.Admin = admin;
urlBase = apiUrl + "chiamata/add";
urlBase = urlBase + "?token=" + token;
client = new HttpClient();
Uri baseAddress = new Uri(urlBase);
client.BaseAddress = baseAddress;
string data = JsonConvert.SerializeObject(model);
StringContent content = new StringContent(data, Encoding.UTF8, "application/json");
HttpResponseMessage response = client.PostAsync(baseAddress, content).Result;
if (response.IsSuccessStatusCode)
{
return RedirectToAction("Index");
}
else
{
errMes = response.Content.ReadAsStringAsync().Result;
helper.SetStringValue("errMsg", errMes);
return RedirectToAction("Error");
}
//return View();
}
#endregion CREATE
#region DETAIL
public IActionResult Details(string id)
{
SessionHelper helper = new SessionHelper(this);
token = helper.GetStringValue("tok");
apiUrl = helper.GetStringValue("apiUrl");
admin = helper.GetStringValue("admin");
ViewBag.Admin = admin;
urlBase = apiUrl + "chiamateListMngr";
urlBase = urlBase + "?token=" + token;
Uri baseAddress = new Uri(urlBase);
client = new HttpClient();
client.BaseAddress = baseAddress;
Chiamate chiamata = new Chiamate();
List<Chiamate> modelList = new List<Chiamate>();
HttpResponseMessage response = client.GetAsync(baseAddress).Result;
if (response.IsSuccessStatusCode)
{
string data = response.Content.ReadAsStringAsync().Result;
modelList = JsonConvert.DeserializeObject<List<Chiamate>>(data);
chiamata = modelList.Where(x => x.chserial.Equals(id)).First();
}
else
{
errMes = response.Content.ReadAsStringAsync().Result;
helper.SetStringValue("errMsg", errMes);
return RedirectToAction("Error");
}
return View(chiamata);
}
#endregion DETAIL
#region EDIT
public IActionResult Edit(string id)
{
SessionHelper helper = new SessionHelper(this);
token = helper.GetStringValue("tok");
apiUrl = helper.GetStringValue("apiUrl");
admin = helper.GetStringValue("admin");
ViewBag.Admin = admin;
urlBase = apiUrl + "chiamateListMngr";
urlBase = urlBase + "?token=" + token;
Uri baseAddress = new Uri(urlBase);
client = new HttpClient();
client.BaseAddress = baseAddress;
Chiamate chiamata = new Chiamate();
List<Chiamate> modelList = new List<Chiamate>();
HttpResponseMessage response = client.GetAsync(baseAddress).Result;
if (response.IsSuccessStatusCode)
{
string data = response.Content.ReadAsStringAsync().Result;
modelList = JsonConvert.DeserializeObject<List<Chiamate>>(data);
chiamata = modelList.Where(t => t.chserial.Equals(id)).First();
}
else
{
errMes = response.Content.ReadAsStringAsync().Result;
helper.SetStringValue("errMsg", errMes);
return RedirectToAction("Error");
}
ViewBag.Impianti = getImpianti();
ViewBag.StatiChiamata = getStatiChiamata();
//ViewBag.TipiChiamata = getTipiChiamata();
ViewBag.CodiciSegnalazione = getCodiciSegnalazione();
ViewBag.Tecnici = getTecnici();
return View(chiamata);
}
[HttpPost]
public IActionResult Edit(Chiamate model)
{
SessionHelper helper = new SessionHelper(this);
token = helper.GetStringValue("tok");
tenant = helper.GetStringValue("tenant");
if (string.IsNullOrEmpty(token))
{
return RedirectToAction("Login2", "Login");
}
model.chcodazi = tenant;
apiUrl = helper.GetStringValue("apiUrl");
urlBase = apiUrl + "chiamata/mod";
urlBase = urlBase + "?token=" + token;
Uri baseAddress = new Uri(urlBase);
client = new HttpClient();
client.BaseAddress = baseAddress;
model.chdtapp = model.chdata;
model.chdtass = model.chdata;
int year = model.chdata.Value.Year;
int hour = model.chdata.Value.Hour;
int minute = model.chdata.Value.Minute;
model.chora = hour;
model.chmin = minute;
model.choraapi = hour;
model.chminapi = minute;
model.chorass = hour;
model.chminass = minute;
string data = JsonConvert.SerializeObject(model);
StringContent content = new StringContent(data, Encoding.UTF8, "application/json");
HttpResponseMessage response = client.PostAsync(baseAddress, content).Result;
if (response.IsSuccessStatusCode)
{
return RedirectToAction("Index");
}
else
{
errMes = response.Content.ReadAsStringAsync().Result;
helper.SetStringValue("errMsg", errMes);
return RedirectToAction("Error");
}
}
#endregion EDIT
#region DELETE
[HttpGet]
public IActionResult Delete(string id)
{
SessionHelper helper = new SessionHelper(this);
token = helper.GetStringValue("tok");
apiUrl = helper.GetStringValue("apiUrl");
admin = helper.GetStringValue("admin");
ViewBag.Admin = admin;
urlBase = apiUrl + "chiamateListMngr";
urlBase = urlBase + "?token=" + token;
Uri baseAddress = new Uri(urlBase);
client = new HttpClient();
client.BaseAddress = baseAddress;
Chiamate chiamata = new Chiamate();
List<Chiamate> modelList = new List<Chiamate>();
HttpResponseMessage response = client.GetAsync(baseAddress).Result;
if (response.IsSuccessStatusCode)
{
string data = response.Content.ReadAsStringAsync().Result;
modelList = JsonConvert.DeserializeObject<List<Chiamate>>(data);
chiamata = modelList.Where(x => x.chserial.Equals(id)).First();
}
else
{
errMes = response.Content.ReadAsStringAsync().Result;
helper.SetStringValue("errMsg", errMes);
return RedirectToAction("Error");
}
return View(chiamata);
}
[HttpPost, ActionName("DeleteConfirmed")]
public IActionResult DeleteConfirmed(string id)
{
SessionHelper helper = new SessionHelper(this);
token = helper.GetStringValue("tok");
apiUrl = helper.GetStringValue("apiUrl");
admin = helper.GetStringValue("admin");
ViewBag.Admin = admin;
urlBase = apiUrl + "chiamata/del?" + "chserial=" + id + "&";
urlBase = urlBase + "token=" + token;
Uri baseAddress = new Uri(urlBase);
client = new HttpClient();
client.BaseAddress = baseAddress;
string data = JsonConvert.SerializeObject(id);
StringContent content = new StringContent(data, Encoding.UTF8, "application/json");
HttpResponseMessage response = client.PostAsync(baseAddress, content).Result;
if (response.IsSuccessStatusCode)
{
return RedirectToAction("Index");
}
else
{
errMes = response.Content.ReadAsStringAsync().Result;
helper.SetStringValue("errMsg", errMes);
return RedirectToAction("Error");
}
//return View();
}
#endregion DELETE
#region metodi interni
private List<SelectListItem> getImpianti()
{
SessionHelper helper = new SessionHelper(this);
token = helper.GetStringValue("tok");
apiUrl = helper.GetStringValue("apiUrl");
urlBase = apiUrl + "impiantiListMngr";
urlBase = urlBase + "?token=" + token;
Uri baseAddress = new Uri(urlBase);
client = new HttpClient();
client.BaseAddress = baseAddress;
List<SelectListItem> selectItems = new List<SelectListItem>();
List<Impianto> modelList = new List<Impianto>();
HttpResponseMessage response = client.GetAsync(baseAddress).Result;
if (response.IsSuccessStatusCode)
{
string data = response.Content.ReadAsStringAsync().Result;
modelList = JsonConvert.DeserializeObject<List<Impianto>>(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;
listItem.Text = s;
selectItems.Add(listItem);
}
}
return selectItems;
}
private List<SelectListItem> getStatiChiamata()
{
List<SelectListItem> selectItems = new List<SelectListItem>();
SelectListItem listItem = new SelectListItem();
listItem.Value = "C";
listItem.Text = "Assegnata";
selectItems.Add(listItem);
listItem = new SelectListItem();
listItem.Value = "B";
listItem.Text = "Da Assegnare";
selectItems.Add(listItem);
listItem = new SelectListItem();
listItem.Value = "Z";
listItem.Text = "Chiusa";
selectItems.Add(listItem);
listItem = new SelectListItem();
listItem.Value = "S";
listItem.Text = "Sospesa";
selectItems.Add(listItem);
return selectItems;
}
private List<SelectListItem> getTipiChiamata()
{
List<SelectListItem> selectItems = new List<SelectListItem>();
SelectListItem listItem = new SelectListItem();
listItem.Value = "A";
listItem.Text = "Tipo A";
selectItems.Add(listItem);
listItem = new SelectListItem();
listItem.Value = "B";
listItem.Text = "Tipo B";
selectItems.Add(listItem);
return selectItems;
}
private List<SelectListItem> getCodiciSegnalazione()
{
List<SelectListItem> selectItems = new List<SelectListItem>();
SelectListItem listItem = new SelectListItem();
listItem.Value = "Intervento";
listItem.Text = "Intervento";
selectItems.Add(listItem);
listItem = new SelectListItem();
listItem.Value = "Collaudo";
listItem.Text = "Collaudo";
selectItems.Add(listItem);
listItem = new SelectListItem();
listItem.Value = "InterventoGar";
listItem.Text = "Intervento in garanzia";
selectItems.Add(listItem);
listItem = new SelectListItem();
listItem.Value = "InterventoMan";
listItem.Text = "Intervento in manutenzione";
selectItems.Add(listItem);
listItem = new SelectListItem();
listItem.Value = "InterventoCli";
listItem.Text = "Intervento a carico del cliente";
selectItems.Add(listItem);
return selectItems;
}
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);
//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;
selectItems.Add(listItem);
}
}
return selectItems;
}
private string getNewSeriale()
{
int p = -1;
string seriale = string.Empty;
SessionHelper helper = new SessionHelper(this);
token = helper.GetStringValue("tok");
apiUrl = helper.GetStringValue("apiUrl");
urlBase = apiUrl + "progressiviList";
urlBase = urlBase + "?token=" + token;
Uri baseAddress = new Uri(urlBase);
client = new HttpClient();
client.BaseAddress = baseAddress;
List<Progressivo> progressivi = new List<Progressivo>();
HttpResponseMessage response = client.GetAsync(baseAddress).Result;
if (response.IsSuccessStatusCode)
{
string data = response.Content.ReadAsStringAsync().Result;
progressivi = JsonConvert.DeserializeObject<List<Progressivo>>(data);
var last = progressivi.Where(t => t.tipo_prog.Equals(_serchiam)).First();
p = last.val_prog;
p++;
seriale = Convert.ToString(p);
seriale = seriale.PadLeft(10, '0');
Progressivo upd = new Progressivo();
upd.val_prog = p;
upd.tipo_prog = _serchiam;
upd.azienda = tenant;
updateTabellaProgressivi(upd);
}
return seriale;
}
private async void updateTabellaProgressivi(Progressivo p)
{
SessionHelper helper = new SessionHelper(this);
token = helper.GetStringValue("tok");
apiUrl = helper.GetStringValue("apiUrl");
tenant = helper.GetStringValue("tenant");
urlBase = apiUrl + "progressivo/mod";
urlBase = urlBase + "?token=" + token;
//Uri baseAddress = new Uri(urlBase);
client = new HttpClient();
var stringPayload = JsonConvert.SerializeObject(p);
var httpContent = new StringContent(stringPayload, Encoding.UTF8, "application/json");
var httpResponse = await client.PostAsync(urlBase, httpContent);
if (httpResponse.Content != null)
{
var responseContent = await httpResponse.Content.ReadAsStringAsync();
}
}
private decimal getNewNumeroChiamata()
{
int p = -1;
decimal numero =0;
SessionHelper helper = new SessionHelper(this);
token = helper.GetStringValue("tok");
apiUrl = helper.GetStringValue("apiUrl");
urlBase = apiUrl + "progressiviList";
urlBase = urlBase + "?token=" + token;
Uri baseAddress = new Uri(urlBase);
client = new HttpClient();
client.BaseAddress = baseAddress;
List<Progressivo> progressivi = new List<Progressivo>();
HttpResponseMessage response = client.GetAsync(baseAddress).Result;
if (response.IsSuccessStatusCode)
{
string data = response.Content.ReadAsStringAsync().Result;
progressivi = JsonConvert.DeserializeObject<List<Progressivo>>(data);
var last = progressivi.Where(t => t.tipo_prog.Equals(_numchiam)).First();
p = last.val_prog;
p++;
numero = Convert.ToDecimal(p);
Progressivo upd = new Progressivo();
upd.val_prog = p;
upd.tipo_prog = _numchiam;
upd.azienda = tenant;
updateTabellaProgressivi(upd);
}
return numero;
}
[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
}
}