SoftwayWeb/Controllers/GiriController.cs

525 lines
19 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.Data;
using System.Diagnostics;
using System.Runtime.Intrinsics.Arm;
using System.Text;
using X.PagedList;
namespace SoftwayWeb.Controllers
{
public class GiriController : Controller
{
string apiUrl = string.Empty;
string urlBase = string.Empty;
string token = string.Empty;
string errMes = string.Empty;
private readonly IConfiguration _configuration;
HttpClient client;
SessionHelper helper;
public GiriController(IConfiguration configuration)
{
_configuration = configuration;
client = new HttpClient();
var key = _configuration["ApplicationInsights:rootUrlApi"];
apiUrl = key;
}
public IActionResult Index(string? codAutista, string? codMezzo, DateTime data, bool aperto = true, int? page = 1)
{
helper = new SessionHelper(this);
token = helper.GetStringValue("tok");
if (string.IsNullOrEmpty(token))
{
return RedirectToAction("Login", "Login");
}
urlBase = apiUrl + "Giri/listaGiri?aperto="+ aperto;
//string url = apiUrl + "Giri/listaGiri";
//urlBase = url + "?token=" + token;
Uri baseAddress = new Uri(urlBase);
client = new HttpClient();
client.BaseAddress = baseAddress;
List<GiriConsegnaView> modelList = new List<GiriConsegnaView>();
HttpResponseMessage response = client.GetAsync(baseAddress).Result;
if (response.IsSuccessStatusCode)
{
string dato = response.Content.ReadAsStringAsync().Result;
modelList = JsonConvert.DeserializeObject<List<GiriConsegnaView>>(dato);
ViewBag.CodAutista = getAutisti();
if (!string.IsNullOrEmpty(codAutista))
{
modelList = modelList.Where(x => x.CodAutista.Contains(codAutista)).ToList();
ViewData["CurrentFilter"] = codAutista;
ViewBag.Autista = codAutista;
}
else
{
ViewData["CurrentFilter"] = null;
}
ViewBag.CodMezzo = getMezzi();
if (!string.IsNullOrEmpty(codMezzo))
{
modelList = modelList.Where(x => x.CodMezzo.Contains(codMezzo)).ToList();
ViewData["CurrentFilter"] = codMezzo;
}
else
{
ViewData["CurrentFilter"] = null;
}
if (data.Date != DateTime.MinValue)
{
modelList = modelList.Where(x => x.DataGiro.GetValueOrDefault().Date == data.Date).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 Create(bool sel)
{
SessionHelper helper = new SessionHelper(this);
token = helper.GetStringValue("tok");
if (string.IsNullOrEmpty(token))
{
return RedirectToAction("Login", "Login");
}
List<GiriConsegnaDaCreare> modelList = new List<GiriConsegnaDaCreare>();
apiUrl = helper.GetStringValue("apiUrl");
urlBase = apiUrl + "Giri/listaGiriDaCreare";
Uri baseAddress = new Uri(urlBase);
client = new HttpClient();
client.BaseAddress = baseAddress;
HttpResponseMessage response = client.GetAsync(baseAddress).Result;
if (response.IsSuccessStatusCode)
{
string data = response.Content.ReadAsStringAsync().Result;
modelList = JsonConvert.DeserializeObject<List<GiriConsegnaDaCreare>>(data);
}
foreach(GiriConsegnaDaCreare giro in modelList)
{
giro.IsSelected= sel;
}
return View(modelList);
}
[HttpPost]
public IActionResult PostIndex(IList<GiriConsegnaDaCreare> lst)
{
SessionHelper helper = new SessionHelper(this);
token = helper.GetStringValue("tok");
if (string.IsNullOrEmpty(token))
{
return RedirectToAction("Login", "Login");
}
foreach (GiriConsegnaDaCreare g in lst)
{
if(g.IsSelected==true)
{
//ViewBag.Autisti = getAutisti();
apiUrl = helper.GetStringValue("apiUrl");
urlBase = apiUrl + "Giri/addGiro2";
GiriConsegnaView gcv=new GiriConsegnaView();
gcv.CodAutista = g.CodAutista;
gcv.DataGiro=g.DataGiro;
gcv.CodMezzo = g.CodMezzo;
gcv.DataChiusura = DateTime.MinValue;
Uri baseAddress = new Uri(urlBase);
client = new HttpClient();
client.BaseAddress = baseAddress;
string data = JsonConvert.SerializeObject(gcv);
StringContent content = new StringContent(data, Encoding.UTF8, "application/json");
HttpResponseMessage response = client.PostAsync(baseAddress, content).Result;
if (response.IsSuccessStatusCode)
{
}
else
{
errMes = response.Content.ReadAsStringAsync().Result;
helper.SetStringValue("errMsg", errMes);
return RedirectToAction("Error");
}
}
}
return RedirectToAction("Index", "Giri");
}
public IActionResult Bancali(string id, bool aperto = true)
{
SessionHelper helper = new SessionHelper(this);
token = helper.GetStringValue("tok");
if (string.IsNullOrEmpty(token))
{
return RedirectToAction("Login", "Login");
}
apiUrl = helper.GetStringValue("apiUrl");
urlBase = apiUrl + "Giri/listaGiri?aperto=" + aperto;
Uri baseAddress = new Uri(urlBase);
client = new HttpClient();
client.BaseAddress = baseAddress;
HttpResponseMessage response = client.GetAsync(baseAddress).Result;
List<GiriConsegnaView> modelList = new List<GiriConsegnaView>();
GiriConsegnaView model = new GiriConsegnaView();
if (response.IsSuccessStatusCode)
{
string data = response.Content.ReadAsStringAsync().Result;
modelList = JsonConvert.DeserializeObject<List<GiriConsegnaView>>(data);
model = modelList.Where(x => x.SerialeGiro.Equals(id)).First();
}
else
{
errMes = response.Content.ReadAsStringAsync().Result;
helper.SetStringValue("errMsg", errMes);
return RedirectToAction("Error");
}
return View(model);
}
[HttpPost]
public IActionResult Bancali(GiriConsegnaView model)
{
SessionHelper helper = new SessionHelper(this);
token = helper.GetStringValue("tok");
if (string.IsNullOrEmpty(token))
{
return RedirectToAction("Login", "Login");
}
apiUrl = helper.GetStringValue("apiUrl");
urlBase = apiUrl + "Giri/modGiro";
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", "Giri");
}
else
{
errMes = response.Content.ReadAsStringAsync().Result;
helper.SetStringValue("errMsg", errMes);
return RedirectToAction("Error");
}
}
[HttpGet]
public IActionResult ModificaTutteDestinazioni(string id, string? codAutista, string? nomeAutista, string? codMezzo, string? nomeMezzo, DateTime dataGiroCons, int? page = 1)
{
helper = new SessionHelper(this);
token = helper.GetStringValue("tok");
if (string.IsNullOrEmpty(token))
{
return RedirectToAction("Login", "Login");
}
ViewBag.CodAutista = codAutista;
ViewBag.Autista = nomeAutista;
ViewBag.CodMezzo = codMezzo;
ViewBag.Mezzo = nomeMezzo;
ViewBag.DataGiroCons = dataGiroCons;
string dataGiroString = dataGiroCons.ToString("yyyy-MM-dd'T'HH:mm:ss.fff'Z'");
urlBase = apiUrl + "Giri/listaDestinazioniByAutistaDataMezzo";
urlBase = urlBase + "?autista=" + codAutista + "&dataGiro=" + dataGiroString + "&mezzo=" + codMezzo;
Uri baseAddress = new Uri(urlBase);
client = new HttpClient();
client.BaseAddress = baseAddress;
List<Destinazioni_Out> modelList = new List<Destinazioni_Out>();
List<SelectListItem> autisti = new List<SelectListItem>();
autisti = getAutisti();
ViewBag.Autisti = autisti;
List<SelectListItem> mezzi = new List<SelectListItem>();
mezzi = getMezzi();
ViewBag.Mezzi = mezzi;
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.Equals(id.TrimEnd())).ToList();
//if (page != null && page < 1)
//{
// page = 1;
//}
//var pageSize = 10;
//var shortList = modelList.ToPagedList(/*page ?? 1, pageSize*/);
return View(modelList);
}
else
{
errMes = response.Content.ReadAsStringAsync().Result;
helper.SetStringValue("errMsg", errMes);
return RedirectToAction("Error");
}
//return View(modelList);
}
[HttpPost]
public IActionResult ModificaTutteDestinazioniPost(List<Destinazioni_Out> modelList, string? autisti, /*string? nomeAutista,*/ string? mezzi)
{
SessionHelper helper = new SessionHelper(this);
token = helper.GetStringValue("tok");
if (string.IsNullOrEmpty(token))
{
return RedirectToAction("Login", "Login");
}
foreach (var destinazione in modelList)
{
Modgir mg = new Modgir();
mg.Piserial = destinazione.Brserial;
mg.Pidata = destinazione.DataCarico;
mg.Picommit = destinazione.CodCommittente;
mg.Pidesdiv = destinazione.CodSede;
mg.Pitarga = mezzi.TrimEnd();
mg.Piautist = autisti.TrimEnd();
mg.Pitiprec = "M";
mg.Pisergir = destinazione.Pisergir;
apiUrl = helper.GetStringValue("apiUrl");
urlBase = apiUrl + "ModificaGiro/addModgir";
Uri 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)
//{
// //Destinazioni_Out destMod = new Destinazioni_Out();
// //destMod.serialeGiro = destinazione.Brserial;
// //destMod.CodAutista = destinazione.CodAutista;
// //destMod.CodAutomezzo = destinazione.CodAutomezzo;
// //new
// //{
// // id = destinazione.serialeGiro,
// // codAutista = destinazione.CodAutista,
// // dataGiro = destinazione.DataCarico,
// // codMezzo = destinazione.CodAutomezzo
// //},;
//}
}
return RedirectToAction("Index", "Giri");
}
//[HttpPost]
//public IActionResult Create(GiriConsegnaView model)
//{
// SessionHelper helper = new SessionHelper(this);
// ViewBag.Autisti = getAutisti();
// apiUrl = helper.GetStringValue("apiUrl");
// urlBase = apiUrl + "Giri/addGiro2";
// 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", "Giri");
// }
// else
// {
// errMes = response.Content.ReadAsStringAsync().Result;
// helper.SetStringValue("errMsg", errMes);
// return RedirectToAction("Error");
// }
//}
[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 });
}
#region METODI PRIVATI
private List<SelectListItem> getAutisti()
{
SessionHelper helper = new SessionHelper(this);
token = helper.GetStringValue("tok");
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();
listItemFirt.Value = string.Empty;
listItemFirt.Text = " - Autista";
selectItems.Add(listItemFirt);
foreach (var role in modelList)
{
SelectListItem listItem = new SelectListItem();
//string s = role.Catcodice + " - " + role.Catnome;
string s = role.Catnome + " - " + role.Catcodice; // 25/07/2024 invertito nome autista con codice autista
listItem.Value = role.Catcodice;
listItem.Text = s;
selectItems.Add(listItem);
}
}
return selectItems;
}
private List<SelectListItem> getMezzi()
{
SessionHelper helper = new SessionHelper(this);
token = helper.GetStringValue("tok");
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;
}
private List<GiriConsegnaDaCreare> getNuoviGiri()
{
SessionHelper helper = new SessionHelper(this);
List<GiriConsegnaDaCreare> modelList = new List<GiriConsegnaDaCreare> ();
apiUrl = helper.GetStringValue("apiUrl");
urlBase = apiUrl + "Giri/listaGiriDaCreare";
Uri baseAddress = new Uri(urlBase);
client = new HttpClient();
client.BaseAddress = baseAddress;
HttpResponseMessage response = client.GetAsync(baseAddress).Result;
if (response.IsSuccessStatusCode)
{
string data = response.Content.ReadAsStringAsync().Result;
modelList = JsonConvert.DeserializeObject<List<GiriConsegnaDaCreare>>(data);
}
return modelList;
}
#endregion METODI PRIVATI
}
}