589 lines
21 KiB
C#
589 lines
21 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;
|
|
|
|
|
|
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 searchString, int? page = 1)
|
|
{
|
|
SessionHelper helper = new SessionHelper(this);
|
|
token = helper.GetStringValue("tok");
|
|
if (string.IsNullOrEmpty(token))
|
|
return RedirectToAction("Index", "Login");
|
|
|
|
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;
|
|
|
|
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);
|
|
|
|
if (!string.IsNullOrEmpty(searchString))
|
|
{
|
|
modelList = modelList.Where(s => s.chcodimp.ToUpper().Contains(searchString.ToUpper())).ToList();
|
|
|
|
ViewData["CurrentFilter"] = searchString;
|
|
}
|
|
else
|
|
ViewData["CurrentFilter"] = null;
|
|
|
|
if (page != null && page < 1)
|
|
{
|
|
page = 1;
|
|
}
|
|
|
|
var pageSize = 10;
|
|
|
|
var shortLinks = modelList
|
|
.OrderByDescending(s => s.chcodimp)
|
|
.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("Index","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;
|
|
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;
|
|
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("Index", "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;
|
|
|
|
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(model);
|
|
}
|
|
|
|
#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);
|
|
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);
|
|
|
|
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 = "";
|
|
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
|
|
}
|
|
}
|