SoftwayWeb/Controllers/DestinazioniController.cs
2025-04-02 15:48:42 +02:00

698 lines
27 KiB
C#

using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.AspNetCore.Mvc.Rendering;
using Newtonsoft.Json;
using SoftwayWeb.Models;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using X.PagedList;
namespace SoftwayWeb.Controllers
{
public class DestinazioniController : Controller
{
string apiUrl = string.Empty;
string urlBase = string.Empty;
string token = string.Empty;
string errMes = string.Empty;
string messAcc = string.Empty;
string messNew = string.Empty;
private readonly IConfiguration _configuration;
private readonly IConfiguration _mess1;
private readonly IConfiguration _mess2;
HttpClient client;
SessionHelper helper;
public DestinazioniController(IConfiguration configuration, IConfiguration mess1, IConfiguration mess2)
{
_configuration = configuration;
_mess1 = mess1;
_mess2 = mess2;
client = new HttpClient();
var key = _configuration["ApplicationInsights:rootUrlApi"];
var messA = _mess1["ApplicationInsights:messDestAccodo"];
var messN = _mess2["ApplicationInsights:messDestNew"];
apiUrl = key;
messAcc = messA;
messNew = messN;
}
public IActionResult Index(string id, string? codAutista, string? nomeAutista, DateTime dataGiro, string? codMezzo, string? desMezzo, int? page = 1)
{
helper = new SessionHelper(this);
helper.ClearFormatedKey("errMsg");
token = helper.GetStringValue("tok");
if (string.IsNullOrEmpty(token))
{
return RedirectToAction("Login", "Login");
}
string autista = string.Empty;
string dataGi = string.Empty;
string mezzo = string.Empty;
string dataGiro2 = string.Empty;
ViewBag.CodAutista = codAutista;
ViewBag.Autista = nomeAutista;
ViewBag.CodMezzo = codMezzo;
ViewBag.Automezzo = desMezzo;
ViewBag.dataGiro = dataGiro.ToString("dd/MM/yyyy");
if (!string.IsNullOrEmpty(codAutista))
{
helper.SetStringValue("codAutista", codAutista.TrimEnd());
helper.SetStringValue("nomeAutista", nomeAutista.TrimEnd());
}
else
{
string codAut = helper.GetStringValue("codAutista");
codAutista = codAut.TrimEnd();
ViewBag.CodAutista = codAutista;
string nomeAut = helper.GetStringValue("nomeAutista");
ViewBag.Autista = nomeAut;
}
if (!string.IsNullOrEmpty(codMezzo))
{
helper.SetStringValue("codMezzo", codMezzo.TrimEnd());
helper.SetStringValue("desMezzo", desMezzo.TrimEnd());
}
else
{
string mez = helper.GetStringValue("codMezzo").TrimEnd();
codMezzo = mez;
ViewBag.CodMezzo = codMezzo;
string nomeMezzo = helper.GetStringValue("desMezzo");
ViewBag.Automezzo = nomeMezzo;
}
if (dataGiro.Date != DateTime.MinValue)
{
helper.SetStringValue("dataGiro", dataGiro.ToString("yyyy-MM-dd'T'HH:mm:ss.fff'Z'"));
dataGi = dataGiro.ToString("yyyy-MM-dd'T'HH:mm:ss.fff'Z'");
helper.SetStringValue("dataGiro2", dataGiro.ToString("dd/MM/yyyy"));
}
else
{
dataGi = helper.GetStringValue("dataGiro");
string dataG = helper.GetStringValue("dataGiro2");
ViewBag.dataGiro = dataG;
}
urlBase = apiUrl + "Giri/listaDestinazioniByAutistaDataMezzoNonValidate";
urlBase = urlBase + "?autista=" + codAutista.Trim() + "&dataGiro=" + dataGi + "&mezzo=" + codMezzo.Trim();
Uri baseAddress = new Uri(urlBase);
client = new HttpClient();
client.BaseAddress = baseAddress;
List<Destinazioni_Out> modelList = new List<Destinazioni_Out>();
HttpResponseMessage response = client.GetAsync(baseAddress).Result;
if (response.IsSuccessStatusCode)
{
string dato = response.Content.ReadAsStringAsync().Result;
modelList = JsonConvert.DeserializeObject<List<Destinazioni_Out>>(dato);
modelList = modelList.Where(x => x.serialeGiro == id).OrderBy(x=> x.Seq).ThenBy(x => x.Committente).ToList();
if (page != null && page < 1)
{
page = 1;
}
var pageSize = 10;
var shortList = modelList.ToPagedList(page ?? 1, pageSize);
return View(shortList);
}
else
{
errMes = response.Content.ReadAsStringAsync().Result;
helper.SetStringValue("errMsg", errMes);
return RedirectToAction("Error");
}
}
public IActionResult IndexValidate(string id, string? codAutista, string? nomeAutista, DateTime dataGiro, string? codMezzo, string? desMezzo, int? page = 1)
{
helper = new SessionHelper(this);
helper.ClearFormatedKey("errMsg");
token = helper.GetStringValue("tok");
if (string.IsNullOrEmpty(token))
{
return RedirectToAction("Login", "Login");
}
string autista = string.Empty;
string dataGi = string.Empty;
string mezzo = string.Empty;
string dataGiro2 = string.Empty;
ViewBag.CodAutista = codAutista;
ViewBag.Autista = nomeAutista;
ViewBag.CodMezzo = codMezzo;
ViewBag.Automezzo = desMezzo;
ViewBag.dataGiro = dataGiro.ToString("dd/MM/yyyy");
if (!string.IsNullOrEmpty(codAutista))
{
helper.SetStringValue("codAutista", codAutista.TrimEnd());
helper.SetStringValue("nomeAutista", nomeAutista.TrimEnd());
}
else
{
string codAut = helper.GetStringValue("codAutista");
codAutista = codAut.TrimEnd();
ViewBag.CodAutista = codAutista;
string nomeAut = helper.GetStringValue("nomeAutista");
ViewBag.Autista = nomeAut;
}
if (!string.IsNullOrEmpty(codMezzo))
{
helper.SetStringValue("codMezzo", codMezzo.TrimEnd());
helper.SetStringValue("desMezzo", desMezzo.TrimEnd());
}
else
{
string mez = helper.GetStringValue("codMezzo").TrimEnd();
codMezzo = mez;
ViewBag.CodMezzo = codMezzo;
string nomeMezzo = helper.GetStringValue("desMezzo");
ViewBag.Automezzo = nomeMezzo;
}
if (dataGiro.Date != DateTime.MinValue)
{
helper.SetStringValue("dataGiro", dataGiro.ToString("yyyy-MM-dd'T'HH:mm:ss.fff'Z'"));
dataGi = dataGiro.ToString("yyyy-MM-dd'T'HH:mm:ss.fff'Z'");
helper.SetStringValue("dataGiro2", dataGiro.ToString("dd/MM/yyyy"));
}
else
{
dataGi = helper.GetStringValue("dataGiro");
string dataG = helper.GetStringValue("dataGiro2");
ViewBag.dataGiro = dataG;
}
urlBase = apiUrl + "Giri/listaDestinazioniByAutistaDataMezzoWeb";
urlBase = urlBase + "?autista=" + codAutista.Trim() + "&dataGiro=" + dataGi.Trim() + "&mezzo=" + codMezzo.Trim();
Uri baseAddress = new Uri(urlBase);
client = new HttpClient();
client.BaseAddress = baseAddress;
List<Destinazioni_Out> modelList = new List<Destinazioni_Out>();
HttpResponseMessage response = client.GetAsync(baseAddress).Result;
if (response.IsSuccessStatusCode)
{
string dato = response.Content.ReadAsStringAsync().Result;
modelList = JsonConvert.DeserializeObject<List<Destinazioni_Out>>(dato);
modelList = modelList.Where(x => x.serialeGiro == id).OrderBy(x => x.Seq).ThenBy(x => x.Committente).ToList();
if (page != null && page < 1)
{
page = 1;
}
var pageSize = 10;
var shortList = modelList.ToPagedList(page ?? 1, pageSize);
return View(shortList);
}
else
{
errMes = response.Content.ReadAsStringAsync().Result;
helper.SetStringValue("errMsg", errMes);
return RedirectToAction("Error");
}
}
public IActionResult EliminaDestinazione(string serial)
{
SessionHelper helper = new SessionHelper(this);
helper.ClearFormatedKey("errMsg");
token = helper.GetStringValue("tok");
if (string.IsNullOrEmpty(token))
{
return RedirectToAction("Login", "Login");
}
apiUrl = helper.GetStringValue("apiUrl");
urlBase = apiUrl + "Giri/listaDestinazioneBySerial";
urlBase = urlBase + "?seriale=" + serial;
Uri baseAddress = new Uri(urlBase);
client = new HttpClient();
client.BaseAddress = baseAddress;
Destinazioni_Out model = new Destinazioni_Out();
HttpResponseMessage response = client.GetAsync(baseAddress).Result;
if (response.IsSuccessStatusCode)
{
Modgir mg = new Modgir();
string dato = response.Content.ReadAsStringAsync().Result;
model = JsonConvert.DeserializeObject<Destinazioni_Out>(dato);
if (!string.IsNullOrEmpty(model.Brserial))
{
//qui metodo post per scrivere su pimodgir
mg.Piserial = model.Brserial;
mg.Pidata = model.DataCarico;
mg.Picommit = model.CodCommittente;
mg.Pidesdiv = model.CodSede;
mg.Pitarga = model.CodAutomezzo;
mg.Piautist = model.CodAutista;
mg.Pitiprec = "D";
mg.Pisergir = model.serialeGiro;
urlBase = apiUrl + "ModificaGiro/addModgir";
baseAddress = new Uri(urlBase);
client = new HttpClient();
client.BaseAddress = baseAddress;
string data = JsonConvert.SerializeObject(mg);
StringContent content = new StringContent(data, Encoding.UTF8, "application/json");
HttpResponseMessage response2 = client.PostAsync(baseAddress, content).Result;
if (response2.IsSuccessStatusCode)
{
}
else
{
errMes = response.Content.ReadAsStringAsync().Result;
helper.SetStringValue("errMsg", errMes);
return RedirectToAction("Error");
}
}
else
{
string referer = Request.Headers["Referer"].ToString();
StringBuilder sb = new StringBuilder();
sb.AppendLine("Impossibile eliminare la destinazione. Giro non inizializzato (inserire i bancali)");
errMes = sb.ToString();
helper.SetStringValue("errMsg", errMes);
helper.SetStringValue("field1", serial);
helper.SetStringValue("backPage", referer);
sb = null;
return RedirectToAction("Alert");
}
}
else
{
errMes = response.Content.ReadAsStringAsync().Result;
helper.SetStringValue("errMsg", errMes);
return RedirectToAction("Error");
}
return RedirectToAction("IndexValidate", "Destinazioni", new
{
//string id, string? codAutista, string? nomeAutista, DateTime dataGiro, string? codMezzo, string? desMezzo
id = model.serialeGiro,
codAutista = model.CodAutista,
nomeAutista=model.Autista,
dataGiro = model.DataCarico,
codMezzo=model.CodAutomezzo,
desMezzo = model.DescAutomezzo
});
}
public IActionResult ModificaDestinazione(string serial)
{
SessionHelper helper = new SessionHelper(this);
helper.ClearFormatedKey("errMsg");
var requestUrl = $"{Request.Scheme}://{Request.Host.Value}/";
apiUrl = helper.GetStringValue("apiUrl");
urlBase = apiUrl + "Giri/listaDestinazioneBySerial";
urlBase = urlBase + "?seriale=" + serial;
Uri baseAddress = new Uri(urlBase);
client = new HttpClient();
client.BaseAddress = baseAddress;
Destinazioni_Out model = new Destinazioni_Out();
HttpResponseMessage response = client.GetAsync(baseAddress).Result;
if (response.IsSuccessStatusCode)
{
string dato = response.Content.ReadAsStringAsync().Result;
model = JsonConvert.DeserializeObject<Destinazioni_Out>(dato);
if(!string.IsNullOrEmpty( model.Brserial))
{
List<SelectListItem> autisti = new List<SelectListItem>();
autisti = getAutisti();
autisti.Find(c => c.Value.Trim().Equals(model.CodAutista)).Selected = true;
ViewBag.Autisti = autisti;
List<SelectListItem> mezzi = new List<SelectListItem>();
mezzi = getMezzi();
mezzi.Find(c => c.Value.Trim().Equals(model.CodAutomezzo)).Selected = true;
ViewBag.CodMezzo = mezzi;
model.CodAutistaOld = model.CodAutista;
model.CodAutomezzoOld = model.CodAutomezzo;
model.AutistaOld = model.Autista;
model.DescAutomezzoOld = model.DescAutomezzo;
return View(model);
}
else
{
string referer = Request.Headers["Referer"].ToString();
StringBuilder sb = new StringBuilder();
sb.AppendLine("Impossibile modificare la destinazione. Giro non inizializzato (inserire i bancali)");
errMes = sb.ToString();
helper.SetStringValue("errMsg", errMes);
helper.SetStringValue("field1", serial);
helper.SetStringValue("field2", "");
helper.SetStringValue("field3", "");
helper.SetStringValue("backPage", referer);
sb = null;
return RedirectToAction("Alert");
}
}
else
{
errMes = response.Content.ReadAsStringAsync().Result;
errMes = errMes + "seriale: " + serial;
helper.SetStringValue("errMsg", errMes);
return RedirectToAction("Error");
}
}
[HttpPost]
public IActionResult ModificaDestinazioneStep1(Destinazioni_Out model)
{
SessionHelper helper = new SessionHelper(this);
helper.ClearFormatedKey("errMsg");
token = helper.GetStringValue("tok");
if (string.IsNullOrEmpty(token))
{
return RedirectToAction("Login", "Login");
}
//prima controllo se è un giro nuovo o devo accodare le destinazioni ad un altro utente
string dataGi = string.Empty;
dataGi = model.DataCarico.Value.ToString("yyyy-MM-dd'T'HH:mm:ss.fff'Z'");
//urlBase = apiUrl + "Giri/listaDestinazioniByAutistaDataMezzoWeb";
urlBase = apiUrl + "Giri/listaGiriTestataByAutistaDataMezzo";
urlBase = urlBase + "?autista=" + model.CodAutista + "&dataGiro=" + dataGi + "&mezzo=" + model.CodAutomezzo;
Uri baseAddress = new Uri(urlBase);
client = new HttpClient();
client.BaseAddress = baseAddress;
List<Destinazioni_Out> modelList = new List<Destinazioni_Out>();
List<Pigircon> modelList2 = new List<Pigircon>();
HttpResponseMessage response = client.GetAsync(baseAddress).Result;
if (response.IsSuccessStatusCode)
{
string dato = response.Content.ReadAsStringAsync().Result;
modelList2 = JsonConvert.DeserializeObject<List<Pigircon>>(dato);
//var presenti = modelList2.OrderByDescending(t => t.);
string mex = string.Empty;
if (modelList2 != null && modelList2.Count() > 0)
{
//giro già esistente.. accodo
mex = messAcc;
}
else
{
// giro non esistente.. ne creo uno nuovo
mex = messNew;
}
ViewBag.Messaggio = mex;
//devo trovare la descrizione del nuovo autista e quella del nuovo automezzo
string nomeAutista=string.Empty;
List<SelectListItem> list = new List<SelectListItem>();
list = getAutisti();
foreach(SelectListItem i in list)
{
if(i.Value.Equals(model.CodAutista))
{
nomeAutista = i.Text;
}
}
model.Autista=nomeAutista;
string nomeMezzo = string.Empty;
list = getMezzi();
foreach (SelectListItem i in list)
{
if (i.Value.Equals(model.CodAutomezzo))
{
nomeMezzo = i.Text;
}
}
model.DescAutomezzo = nomeMezzo;
}
else
{
errMes = helper.getErrMsgFromResponse(response);
helper.SetStringValue("errMsg", errMes);
return RedirectToAction("Error");
}
return View(model);
}
[HttpPost]
public IActionResult ModificaDestinazionePost(Destinazioni_Out model)
{
SessionHelper helper = new SessionHelper(this);
helper.ClearFormatedKey("errMsg");
token = helper.GetStringValue("tok");
if (string.IsNullOrEmpty(token))
{
return RedirectToAction("Login", "Login");
}
//prima controllo se è un giro nuovo o devo accodare le destinazioni ad un altro utente
string dataGi = string.Empty;
dataGi = model.DataCarico !=null ? model.DataCarico.Value.ToString("yyyy-MM-dd'T'HH:mm:ss.fff'Z'"):string.Empty;
urlBase = apiUrl + "Giri/listaDestinazioniByAutistaDataMezzoWeb";
urlBase = urlBase + "?autista=" + model.CodAutista + "&dataGiro=" + dataGi + "&mezzo=" + model.CodAutomezzo;
Uri baseAddress = new Uri(urlBase);
client = new HttpClient();
client.BaseAddress = baseAddress;
List<Destinazioni_Out> modelList = new List<Destinazioni_Out>();
HttpResponseMessage response = client.GetAsync(baseAddress).Result;
Modgir mg = new Modgir();
mg.Piserial = model.Brserial;
mg.Pidata = model.DataCarico;
mg.Picommit = model.CodCommittente;
mg.Pidesdiv = model.CodSede;
mg.Pitarga = model.CodAutomezzo;
mg.Piautist = model.CodAutista;
if (response.IsSuccessStatusCode)
{
string dato = response.Content.ReadAsStringAsync().Result;
modelList = JsonConvert.DeserializeObject<List<Destinazioni_Out>>(dato);
var presenti = modelList.OrderByDescending(t => t.Prog);
if (presenti != null && presenti.Count()>0)
{
//giro già esistente.. accodo
mg.Pitiprec = "M";
mg.Pisergirold = model.serialeGiro;
foreach(Destinazioni_Out d in presenti)
{
mg.Pisergir = d.serialeGiro;
}
}
else
{
// giro non esistente.. ne creo uno nuovo
mg.Pitiprec = "N";
mg.Pisergirold = model.serialeGiro;
}
}
else
{
errMes = helper.getErrMsgFromResponse(response);
helper.SetStringValue("errMsg", errMes);
return RedirectToAction("Error");
}
apiUrl = helper.GetStringValue("apiUrl");
urlBase = apiUrl + "ModificaGiro/addModgir";
baseAddress = new Uri(urlBase);
client = new HttpClient();
client.BaseAddress = baseAddress;
string data = JsonConvert.SerializeObject(mg);
StringContent content = new StringContent(data, Encoding.UTF8, "application/json");
HttpResponseMessage response2 = client.PostAsync(baseAddress, content).Result;
if (response2.IsSuccessStatusCode)
{
return RedirectToAction("IndexValidate", "Destinazioni", new
{
id = model.serialeGiro,
codAutista = model.CodAutistaOld,
nomeAutista = model.AutistaOld,
dataGiro = model.DataCarico,
codMezzo = model.CodAutomezzoOld,
desMezzo = model.DescAutomezzoOld
});
}
else
{
errMes = helper.getErrMsgFromResponse(response2);
helper.SetStringValue("errMsg", errMes);
return RedirectToAction("Error");
}
}
private List<SelectListItem> getAutisti()
{
SessionHelper helper = new SessionHelper(this);
apiUrl = helper.GetStringValue("apiUrl");
urlBase = apiUrl + "Giri/listaAutisti";
Uri baseAddress = new Uri(urlBase);
client = new HttpClient();
client.BaseAddress = baseAddress;
List<SelectListItem> selectItems = new List<SelectListItem>();
List<Personale> modelList = new List<Personale>();
HttpResponseMessage response = client.GetAsync(baseAddress).Result;
if (response.IsSuccessStatusCode)
{
string data = response.Content.ReadAsStringAsync().Result;
modelList = JsonConvert.DeserializeObject<List<Personale>>(data);
//per gestire primo elemento tendina (deve essere vuoto)
SelectListItem listItemFirt = new SelectListItem();
foreach (var role in modelList)
{
SelectListItem listItem = new SelectListItem();
string s = role.Catcodice + " - " + role.Catnome;
listItem.Value = role.Catcodice;
listItem.Text = s;
selectItems.Add(listItem);
}
}
return selectItems;
}
private List<SelectListItem> getMezzi()
{
SessionHelper helper = new SessionHelper(this);
apiUrl = helper.GetStringValue("apiUrl");
urlBase = apiUrl + "Giri/listaAutomezzi";
Uri baseAddress = new Uri(urlBase);
client = new HttpClient();
client.BaseAddress = baseAddress;
List<SelectListItem> selectItems = new List<SelectListItem>();
List<Automezzi> listMezzi = new List<Automezzi>();
HttpResponseMessage response = client.GetAsync(baseAddress).Result;
if (response.IsSuccessStatusCode)
{
string data = response.Content.ReadAsStringAsync().Result;
listMezzi = JsonConvert.DeserializeObject<List<Automezzi>>(data);
listMezzi = listMezzi.OrderBy(x => x.CauTarga).ToList();
//per gestire primo elemento tendina (deve essere vuoto)
SelectListItem listItemFirt = new SelectListItem();
listItemFirt.Value = string.Empty;
listItemFirt.Text = " - Automezzo";
selectItems.Add(listItemFirt);
foreach (var mezzo in listMezzi)
{
SelectListItem listItem = new SelectListItem();
List<SelectListItem> listItemOrderby = new List<SelectListItem>();
string s = mezzo.CauTarga + " - " + mezzo.CauDesc;
listItem.Value = mezzo.CauTarga;
listItem.Text = s;
selectItems.Add(listItem);
}
}
return selectItems;
}
#region controllo javascript NON usato
[HttpPost] // can be HttpGet
public ActionResult Test(string codmezzo, string codaut)
{
bool isValid = yourcheckmethod(codmezzo, codaut); //.. check
//bool isValid = true;
var obj = new
{
valid = isValid
};
return Json(obj);
}
private bool yourcheckmethod(string codmezzo, string codaut)
{
return false;
}
#endregion
[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 });
}
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
public IActionResult Alert()
{
SessionHelper helper = new SessionHelper(this);
string e = helper.GetStringValue("errMsg");
string referer= helper.GetStringValue("backPage");
string _field1= helper.GetStringValue("field1");
string _field2 = helper.GetStringValue("field2");
string _field3 = helper.GetStringValue("field3");
return View(new ErrorViewModel {
RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier,
ErrMsg = e,
BackPage= referer,
Field1=_field1,
Field1_lbl = "Serial",
Field2 =_field2,
Field2_lbl = "Back Page",
Field3 = null,
Field3_lbl = null,
});
}
}
}