using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.Rendering; using Newtonsoft.Json; using System.Data; using System.Diagnostics; using System.Linq.Expressions; 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, string cliente, 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 modelList = new List(); HttpResponseMessage response = client.GetAsync(baseAddress).Result; if (response.IsSuccessStatusCode) { string data = response.Content.ReadAsStringAsync().Result; modelList = JsonConvert.DeserializeObject>(data); if (!string.IsNullOrEmpty(cliente)) { modelList = modelList.Where(s => s.andescri != null && s.andescri.ToUpper().Contains(cliente.ToUpper())).ToList(); ViewData["ClienteFilter"] = cliente; ViewBag.Clienti = cliente; } else ViewData["ClienteFilter"] = null; if (!string.IsNullOrEmpty(impianto)) { modelList = modelList.Where(s => s.chcodimp!=null && 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["CurrentFilterTec"] = tecnico; ViewBag.Impianto = tecnico; } else ViewData["CurrentFilterTec"] = null; if (!string.IsNullOrEmpty(indirizzo)) { modelList = modelList.Where(s => s.indirizzoBreve.ToUpper().Contains(indirizzo.ToUpper())).ToList(); ViewData["CurrentFilterIndiri"] = indirizzo; } else ViewData["CurrentFilterIndiri"] = null; if (dataIni.Date != DateTime.MinValue) { modelList = modelList.Where(x => x.chdata.GetValueOrDefault().Date >= dataIni.Date).ToList(); ViewData["CurrentFilterDataDa"] = dataIni.Date; ViewBag.Da = dataIni.Date.ToString("yyyy-MM-dd"); } else ViewData["CurrentFilterDataDa"] = DateTime.MinValue; if (dataFin.Date != DateTime.MinValue) { modelList = modelList.Where(x => x.chdata.GetValueOrDefault().Date <= dataFin.Date).ToList(); ViewData["CurrentFilterDataA"] = dataFin.Date; ViewBag.A = dataFin.Date.ToString("yyyy-MM-dd"); } else ViewData["CurrentFilterDataA"] = DateTime.MinValue; if (!string.IsNullOrEmpty(stato)) { switch (stato) { case "Z": // Chiusa: devo andare a vedere se ser_buono_chiu è valorizzato modelList = modelList.Where(x => !string.IsNullOrEmpty(x.ser_buono_chiu)).ToList(); break; case "R": // rifiutata: devo anadre a vedere se rif è valorizzato e uguale a S modelList = modelList.Where(x => !string.IsNullOrEmpty(x.rif) && x.rif.Equals("S")).ToList(); break; case "B": // da assegnare: devo anadre a vedere che il tecnico NON SIA valorizzato modelList = modelList.Where(x => string.IsNullOrEmpty(x.chtchiam)).ToList(); break; case "C": // assegnato: devo anadre a vedere CHE il tecnico SIA valorizzato modelList = modelList.Where(x => !string.IsNullOrEmpty(x.chtchiam)).ToList(); break; default: // S=sospese // in questi casi vado ad cercare nel campo stato della chiamata modelList = modelList.Where(x => !string.IsNullOrEmpty(x.chstato) && x.chstato.Contains(stato)).ToList(); break; } ViewData["CurrentFilterStato"] = stato; } else ViewData["CurrentFilterStato"] = null; if (page != null && page < 1) { page = 1; } var pageSize = 10; var shortLinks = modelList .OrderByDescending(s => s.chnumero) .ToPagedList(page ?? 1, pageSize); ViewBag.Clienti = getClienti(cliente); ViewBag.Impianti = getImpianti(impianto); ViewBag.Tecnici = getTecnici(tecnico); ViewBag.StatiChiamata = getStatiChiamata(stato); return View(shortLinks); } else { errMes = response.Content.ReadAsStringAsync().Result; helper.SetStringValue("errMsg", errMes); return RedirectToAction("Error"); } } #endregion INDEX #region CREATE public IActionResult Create(string id) { SessionHelper helper = new SessionHelper(this); admin = helper.GetStringValue("admin"); ViewBag.Admin = admin; ViewBag.Impianti = getImpianti(null); ViewBag.StatiChiamata = getStatiChiamata(null); //ViewBag.TipiChiamata = getTipiChiamata(); ViewBag.CodiciSegnalazione = getCodiciSegnalazione(null); ViewBag.Tecnici = getTecnici(null); // Pre-seleziona impianto var model = new Chiamate(); if (!string.IsNullOrEmpty(id)) { model.chcodimp = id; } return View(model); } [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 if (model.chtchiam != null) { model.chstato = "C"; } 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; //MF 25/10/2024 Anno, ore e minuti prendono data appuntamento e non più data apertura chiamata //int year = model.chdata.Value.Year; //int hour = model.chdata.Value.Hour; //int minute = model.chdata.Value.Minute; int year = model.chdtapp.Value.Year; int hour = model.chdtapp.Value.Hour; int minute = model.chdtapp.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 modelList = new List(); HttpResponseMessage response = client.GetAsync(baseAddress).Result; if (response.IsSuccessStatusCode) { string data = response.Content.ReadAsStringAsync().Result; modelList = JsonConvert.DeserializeObject>(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 modelList = new List(); HttpResponseMessage response = client.GetAsync(baseAddress).Result; if (response.IsSuccessStatusCode) { string data = response.Content.ReadAsStringAsync().Result; modelList = JsonConvert.DeserializeObject>(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(null); ViewBag.StatiChiamata = getStatiChiamata(null); ViewBag.CodiciSegnalazione = getCodiciSegnalazione(null); ViewBag.Tecnici = getTecnici(null); 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; model.chdtass = model.chdtapp; //int year = model.chdata.Value.Year; //int hour = model.chdata.Value.Hour; //int minute = model.chdata.Value.Minute; int year = model.chdtapp.Value.Year; int hour = model.chdtapp.Value.Hour; int minute = model.chdtapp.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 modelList = new List(); HttpResponseMessage response = client.GetAsync(baseAddress).Result; if (response.IsSuccessStatusCode) { string data = response.Content.ReadAsStringAsync().Result; modelList = JsonConvert.DeserializeObject>(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 byte GetImgFromRapp_New() { var img = new Byte(); return img; } private List getClienti(string cliente) { SessionHelper helper = new SessionHelper(this); token = helper.GetStringValue("tok"); urlBase = apiUrl + "anagraficheList"; 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); modelList = modelList.Where(x => x.andtobso == null).ToList(); //per gestire primo elemento tendina (deve essere vuoto) SelectListItem listItemFirst = new SelectListItem(); listItemFirst.Value = string.Empty; listItemFirst.Text = " - Cliente"; selectItems.Add(listItemFirst); foreach (var item in modelList) { SelectListItem listItem = new SelectListItem(); string s = item.ancodice + " - " + item.andescri; listItem.Value = item.ancodice; listItem.Text = s; selectItems.Add(listItem); } } return selectItems; } private List getImpianti(string impianto) { SessionHelper helper = new SessionHelper(this); //if (!string.IsNullOrEmpty(impianto)) //{ // helper.SetStringValue("imp", impianto); //} //else //{ // helper.SetStringValue("imp", ""); //} 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 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); modelList = modelList.Where(x => x.imfinatt == null).ToList(); //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; if(role.imcodimp!=null && role.imcodimp.Equals(impianto)) { listItem.Selected = true; } selectItems.Add(listItem); } } return selectItems; } private List getStatiChiamata(string stato) { List selectItems = new List(); SelectListItem listItemFirt = new SelectListItem(); listItemFirt.Value = string.Empty; listItemFirt.Text = " - Stato"; selectItems.Add(listItemFirt); SelectListItem listItem = new SelectListItem(); listItem.Value = "C"; listItem.Text = "Assegnata"; if(!string.IsNullOrEmpty(stato) &&stato.Equals("C")) listItem.Selected = true; selectItems.Add(listItem); listItem = new SelectListItem(); listItem.Value = "B"; listItem.Text = "Da Assegnare"; if (!string.IsNullOrEmpty(stato) && stato.Equals("B")) listItem.Selected = true; selectItems.Add(listItem); listItem = new SelectListItem(); listItem.Value = "Z"; listItem.Text = "Chiusa"; if (!string.IsNullOrEmpty(stato) && stato.Equals("Z")) listItem.Selected = true; selectItems.Add(listItem); listItem = new SelectListItem(); listItem.Value = "S"; listItem.Text = "Sospesa"; if (!string.IsNullOrEmpty(stato) && stato.Equals("S")) listItem.Selected = true; selectItems.Add(listItem); listItem = new SelectListItem(); listItem.Value = "R"; listItem.Text = "Rifiutata"; if (!string.IsNullOrEmpty(stato) && stato.Equals("R")) listItem.Selected = true; selectItems.Add(listItem); return selectItems; } private List getTipiChiamata() { List selectItems = new List(); 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 getCodiciSegnalazione(string codSegn) { SessionHelper helper = new SessionHelper(this); token = helper.GetStringValue("tok"); apiUrl = helper.GetStringValue("apiUrl"); urlBase = apiUrl + "CodiciSegnalazioniVTList"; 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); modelList = modelList.Where(x => x.DataObso == null).ToList(); //per gestire primo elemento tendina (deve essere vuoto) SelectListItem listItemFirst = new SelectListItem(); listItemFirst.Value = string.Empty; listItemFirst.Text = " - Cod. Segnalazione"; selectItems.Add(listItemFirst); foreach (var role in modelList) { SelectListItem listItem = new SelectListItem(); string s = role.cscodice + " - " + role.csdescr; listItem.Value = role.cscodice; listItem.Text = s; if (role.cscodice != null && role.cscodice.Equals(codSegn)) { listItem.Selected = true; } selectItems.Add(listItem); } } return selectItems; } private List getTecnici(string tecnico) { 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 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); 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; if (role.tccodice != null && role.tccodice.Equals(tecnico)) { listItem.Selected = true; } 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 progressivi = new List(); HttpResponseMessage response = client.GetAsync(baseAddress).Result; if (response.IsSuccessStatusCode) { string data = response.Content.ReadAsStringAsync().Result; progressivi = JsonConvert.DeserializeObject>(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 progressivi = new List(); HttpResponseMessage response = client.GetAsync(baseAddress).Result; if (response.IsSuccessStatusCode) { string data = response.Content.ReadAsStringAsync().Result; progressivi = JsonConvert.DeserializeObject>(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 } }