389 lines
14 KiB
C#
389 lines
14 KiB
C#
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.AspNetCore.Mvc.Rendering;
|
|
using Newtonsoft.Json;
|
|
using SoftwayWeb.Models;
|
|
using System.Reflection;
|
|
using System.Text;
|
|
|
|
|
|
namespace SoftwayWeb.Controllers
|
|
{
|
|
public class AddDestinazioneController : 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 AddDestinazioneController(IConfiguration configuration)
|
|
{
|
|
_configuration = configuration;
|
|
client = new HttpClient();
|
|
var key = _configuration["ApplicationInsights:rootUrlApi"];
|
|
apiUrl = key;
|
|
}
|
|
|
|
|
|
public IActionResult AddDestinazione(string serialeGiro, string? codAutista, string? nomeAutista, string? codMezzo, string? desMezzo, DateTime dataGiro)
|
|
{
|
|
SessionHelper helper = new SessionHelper(this);
|
|
helper.ClearFormatedKey("errMsg");
|
|
token = helper.GetStringValue("tok");
|
|
|
|
if (string.IsNullOrEmpty(token))
|
|
{
|
|
return RedirectToAction("Login", "Login");
|
|
}
|
|
Modgir model = new Modgir();
|
|
|
|
ViewBag.serialeGiro = serialeGiro;
|
|
model.Pisergir = serialeGiro;
|
|
ViewBag.Commit = getCommittenti();
|
|
ViewBag.Mezzi=getMezzi();
|
|
string autista = string.Empty;
|
|
autista = codAutista + " - " + nomeAutista;
|
|
ViewBag.CodAutista=autista;
|
|
|
|
string mezzo = string.Empty;
|
|
mezzo = codMezzo + " - " + desMezzo;
|
|
ViewBag.Mezzi = mezzo;
|
|
|
|
|
|
model.Piautist = codAutista;
|
|
ViewBag.Autista = nomeAutista;
|
|
ViewBag.CodMezzo = codMezzo;
|
|
model.Pitarga = codMezzo;
|
|
ViewBag.Automezzo = desMezzo;
|
|
ViewBag.dataGiro = dataGiro.ToString("dd/MM/yyyy");
|
|
ViewBag.dateTimeGiro = dataGiro;
|
|
model.Pidata = dataGiro;
|
|
|
|
return View(model);
|
|
}
|
|
|
|
[HttpPost]
|
|
public IActionResult NewDestinazioneStep1(Modgir modgir, string nomeAutista, string automezzo, DateTime dataGiro)
|
|
{
|
|
SessionHelper helper = new SessionHelper(this);
|
|
helper.ClearFormatedKey("errMsg");
|
|
token = helper.GetStringValue("tok");
|
|
|
|
if (string.IsNullOrEmpty(token))
|
|
{
|
|
return RedirectToAction("Login", "Login");
|
|
}
|
|
ViewBag.Automezzo = automezzo;
|
|
ViewBag.Autista = nomeAutista;
|
|
ViewBag.datetimeGiro = modgir.Pidata;
|
|
|
|
apiUrl = helper.GetStringValue("apiUrl");
|
|
|
|
urlBase = apiUrl + "Giri/brogliaccio";
|
|
Uri baseAddress = new Uri(urlBase);
|
|
client = new HttpClient();
|
|
client.BaseAddress = baseAddress;
|
|
|
|
|
|
List<Sbr_ord> modelList = new List<Sbr_ord>();
|
|
HttpResponseMessage response = client.GetAsync(baseAddress).Result;
|
|
ViewBag.controllo = string.Empty;
|
|
StringBuilder stringBuilder = new StringBuilder();
|
|
if (response.IsSuccessStatusCode)
|
|
{
|
|
string data = response.Content.ReadAsStringAsync().Result;
|
|
modelList = JsonConvert.DeserializeObject<List<Sbr_ord>>(data);
|
|
var filtro = modelList.Where(t => t.Brdatcar == modgir.Pidata && t.Brcodcom!=null && t.Brcodcom.Equals(modgir.Picommit) && t.Brcodsed != null && t.Brcodsed.Equals(modgir.Pidesdiv));
|
|
if(filtro!=null && filtro.Count()>0)
|
|
{
|
|
stringBuilder.AppendLine( "Sede di consegna già prevista.");
|
|
foreach(Sbr_ord s in filtro)
|
|
{
|
|
stringBuilder.AppendLine("Autista: " +s.Catnome);
|
|
stringBuilder.AppendLine("Mezzo: " + s.Caudesc);
|
|
}
|
|
stringBuilder.AppendLine("Impossibile aggiungere la consegna.");
|
|
ViewBag.controllo = stringBuilder.ToString();
|
|
|
|
}
|
|
}
|
|
else
|
|
{
|
|
errMes = helper.getErrMsgFromResponse(response);
|
|
helper.SetStringValue("errMsg", errMes);
|
|
return RedirectToAction("Error");
|
|
}
|
|
|
|
|
|
|
|
|
|
return View(modgir);
|
|
}
|
|
|
|
[HttpPost]
|
|
public IActionResult NewDestinazione(Modgir modgir /*,string? codAutista, string? codMezzo, DateTime dataGiro, string serGiro*/)
|
|
{
|
|
SessionHelper helper = new SessionHelper(this);
|
|
helper.ClearFormatedKey("errMsg");
|
|
token = helper.GetStringValue("tok");
|
|
|
|
if (string.IsNullOrEmpty(token))
|
|
{
|
|
return RedirectToAction("Login", "Login");
|
|
}
|
|
|
|
//qui metodo post per scrivere su pimodgir
|
|
Modgir mg = new Modgir();
|
|
|
|
mg.Piserial = string.Empty;
|
|
mg.Pidata = modgir.Pidata;
|
|
mg.Picommit = modgir.Picommit;
|
|
mg.Pidesdiv = modgir.Pidesdiv;
|
|
mg.Pitarga = modgir.Pitarga;
|
|
mg.Piautist = modgir.Piautist.TrimEnd();
|
|
mg.Pitiprec = "A";
|
|
//mg.Pirigele = string.Empty;
|
|
mg.Pisergir = modgir.Pisergir;
|
|
|
|
mg.Picasse = modgir.Picasse;
|
|
mg.Pitrasf = modgir.Pitrasf;
|
|
mg.Picolli = modgir.Picolli;
|
|
mg.Pinumcons = modgir.Pinumcons;
|
|
mg.Piuova = modgir.Piuova;
|
|
mg.Picist = modgir.Picist;
|
|
mg.Piseq = modgir.Piseq;
|
|
mg.Pinote = modgir.Pinote;
|
|
//per vederla una delle voci DEVE essere diversa da 0.
|
|
//se nessuna lo è imposto num cons a UNO
|
|
if(mg.Picasse==0 && mg.Pitrasf==0 && mg.Picolli == 0 && mg.Pinumcons == 0 && mg.Piuova == 0 && mg.Picist == 0 && mg.Piseq == 0 && string.IsNullOrEmpty(mg.Pinote))
|
|
{
|
|
mg.Pinumcons = 1;
|
|
}
|
|
|
|
|
|
|
|
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 response = client.PostAsync(baseAddress, content).Result;
|
|
|
|
if (response.IsSuccessStatusCode)
|
|
{
|
|
return RedirectToAction("Index","Giri");
|
|
}
|
|
else
|
|
{
|
|
errMes = helper.getErrMsgFromResponse(response);
|
|
helper.SetStringValue("errMsg", errMes);
|
|
return RedirectToAction("Error");
|
|
}
|
|
}
|
|
|
|
public IActionResult Index()
|
|
{
|
|
return View();
|
|
}
|
|
|
|
#region METODI INTERNI
|
|
|
|
private List<SelectListItem> getCommittenti()
|
|
{
|
|
SessionHelper helper = new SessionHelper(this);
|
|
|
|
token = helper.GetStringValue("tok");
|
|
apiUrl = helper.GetStringValue("apiUrl");
|
|
urlBase = apiUrl + "Clienti/getCommittenti";
|
|
|
|
Uri baseAddress = new Uri(urlBase);
|
|
client = new HttpClient();
|
|
client.BaseAddress = baseAddress;
|
|
List<SelectListItem> selectItems = new List<SelectListItem>();
|
|
List<Clienti> listClienti = new List<Clienti>();
|
|
HttpResponseMessage response = client.GetAsync(baseAddress).Result;
|
|
|
|
if (response.IsSuccessStatusCode)
|
|
{
|
|
string data = response.Content.ReadAsStringAsync().Result;
|
|
listClienti = JsonConvert.DeserializeObject<List<Clienti>>(data);
|
|
|
|
//per gestire primo elemento tendina (deve essere vuoto)
|
|
SelectListItem listItemFirst = new SelectListItem();
|
|
|
|
listItemFirst.Value = string.Empty;
|
|
listItemFirst.Text = " - Seleziona Committente";
|
|
selectItems.Add(listItemFirst);
|
|
|
|
foreach (var item in listClienti)
|
|
{
|
|
SelectListItem listItem = new SelectListItem();
|
|
|
|
string s = item.Andescri + " - " + item.Ancodice;
|
|
//string s = item.Andescri;
|
|
listItem.Value = item.Ancodice;
|
|
listItem.Text = s;
|
|
selectItems.Add(listItem);
|
|
}
|
|
}
|
|
|
|
return selectItems;
|
|
}
|
|
|
|
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 = " - Seleziona 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 = " - Seleziona 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;
|
|
|
|
}
|
|
#endregion METODI INTERNI
|
|
|
|
#region CASCADING
|
|
|
|
public IActionResult Cascading()
|
|
{
|
|
Cascading model = new Cascading();
|
|
model.Committenti = getCommittenti();
|
|
return View(model);
|
|
}
|
|
|
|
[HttpPost]
|
|
public IActionResult Cascading(string Picommit)
|
|
{
|
|
SessionHelper helper = new SessionHelper(this);
|
|
Cascading model = new Cascading();
|
|
model.Committenti = getCommittenti();
|
|
List<SelectListItem> selectItems = new List<SelectListItem>();
|
|
if (!string.IsNullOrEmpty(Picommit))
|
|
{
|
|
token = helper.GetStringValue("tok");
|
|
apiUrl = helper.GetStringValue("apiUrl");
|
|
urlBase = apiUrl + "Clienti/getSediByCommittente"+ "?codCom=" + Picommit;
|
|
|
|
Uri baseAddress = new Uri(urlBase);
|
|
client = new HttpClient();
|
|
client.BaseAddress = baseAddress;
|
|
|
|
List<SedeConsegna> sediCons = new List<SedeConsegna>();
|
|
|
|
//List<SedeConsegna> listSedi = new List<SedeConsegna>();
|
|
HttpResponseMessage response = client.GetAsync(baseAddress).Result;
|
|
|
|
if (response.IsSuccessStatusCode)
|
|
{
|
|
string data = response.Content.ReadAsStringAsync().Result;
|
|
sediCons = JsonConvert.DeserializeObject<List<SedeConsegna>>(data);
|
|
|
|
SelectListItem listItemFirst = new SelectListItem();
|
|
|
|
listItemFirst.Value = string.Empty;
|
|
listItemFirst.Text = " - Seleziona sede";
|
|
selectItems.Add(listItemFirst);
|
|
|
|
foreach (var sede in sediCons)
|
|
{
|
|
|
|
SelectListItem listItem = new SelectListItem();
|
|
string s = sede.Pcdescri + " - " + sede.Pccodsed;
|
|
listItem.Value = sede.Pccodsed;
|
|
listItem.Text = s;
|
|
selectItems.Add(listItem);
|
|
model.SediCons.Add(listItem);
|
|
}
|
|
|
|
}
|
|
}
|
|
return Json(new SelectList(selectItems, "Value", "Text"));
|
|
|
|
|
|
}
|
|
|
|
#endregion CASCADING
|
|
}
|
|
}
|