using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.Rendering; using Newtonsoft.Json; using System.Diagnostics; using System.Reflection; using System.Text; using VirtualTask.Models; using X.PagedList; namespace VirtualTask.Controllers { public class CommesseVTController : Controller { string apiUrl = string.Empty; string urlBase = string.Empty; string token = string.Empty; string tenant = string.Empty; string errMes = string.Empty; string admin = string.Empty; string _sercomm = "SER_COMMES"; string time_sheet=string.Empty; HttpClient client; private readonly IConfiguration _configuration; public CommesseVTController(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"); 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 + "commesseList"; 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(searchString)) { modelList = modelList.Where(s => s.andescri!=null && s.andescri.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.lacodcom) .ToPagedList(page ?? 1, pageSize); return View(shortLinks); } else { errMes = response.Content.ReadAsStringAsync().Result; helper.SetStringValue("errMsg", errMes); return RedirectToAction("Error"); } //return View(); } #endregion INDEX #region CREATE public IActionResult Create() { SessionHelper helper = new SessionHelper(this); admin = helper.GetStringValue("admin"); ViewBag.Admin = admin; //ViewBag.Impianti = getImpianti(); ViewBag.Anag = getClienti(); return View(); } [HttpPost] public IActionResult Create (CommesseVT_Table model) { SessionHelper helper = new SessionHelper(this); token = helper.GetStringValue("tok"); tenant = helper.GetStringValue("tenant"); admin = helper.GetStringValue("admin"); ViewBag.Admin = admin; if (string.IsNullOrEmpty(token)) { return RedirectToAction("Login2", "Login"); } model.lacodazi = tenant; apiUrl = helper.GetStringValue("apiUrl"); urlBase = apiUrl + "commesseVT/add"; urlBase = urlBase + "?token=" + token; Uri baseAddress = new Uri(urlBase); client = new HttpClient(); client.BaseAddress = baseAddress; model.laserial = GetNewSeriale(); 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 DETAILS public IActionResult Details(string id, string azienda) { SessionHelper helper = new SessionHelper(this); token = helper.GetStringValue("tok"); admin = helper.GetStringValue("admin"); ViewBag.Admin = admin; apiUrl = helper.GetStringValue("apiUrl"); urlBase = apiUrl + "commesseList"; urlBase = urlBase + "?token=" + token; Uri baseAddress = new Uri(urlBase); client = new HttpClient(); client.BaseAddress = baseAddress; CommesseVT_Table commessa = new CommesseVT_Table(); List modelList = new List(); HttpResponseMessage response = client.GetAsync(baseAddress).Result; if (response.IsSuccessStatusCode) { string data = response.Content.ReadAsStringAsync().Result; modelList = JsonConvert.DeserializeObject>(data); commessa = modelList.Where(x => x.laserial.Equals(id) && x.lacodazi.Equals(azienda)).First(); } else { errMes = response.Content.ReadAsStringAsync().Result; helper.SetStringValue("Error", errMes); return RedirectToAction("Error"); } ViewBag.Impiabti = getImpianti(); return View(commessa); } #endregion DETAILS #region EDIT public IActionResult Edit(string id, string azienda) { SessionHelper helper = new SessionHelper(this); token = helper.GetStringValue("tok"); admin = helper.GetStringValue("admin"); ViewBag.Admin = admin; apiUrl = helper.GetStringValue("apiUrl"); urlBase = apiUrl + "commesseList"; urlBase = urlBase + "?token=" + token; Uri baseAddress = new Uri(urlBase); client = new HttpClient(); client.BaseAddress = baseAddress; CommesseVT_Table commessa = new CommesseVT_Table(); List modelList = new List(); HttpResponseMessage response = client.GetAsync(baseAddress).Result; if (response.IsSuccessStatusCode) { string data = response.Content.ReadAsStringAsync().Result; modelList = JsonConvert.DeserializeObject>(data); commessa = modelList.Where(x => x.laserial.Equals(id) && x.lacodazi.Equals(azienda)).First(); } else { errMes = response.Content.ReadAsStringAsync().Result; helper.SetStringValue("errMsg", errMes); return RedirectToAction("Error"); } ViewBag.Impianti = getImpiantiCliente(commessa.lacodcli); ViewBag.Anag = getClienti(); return View(commessa); } [HttpPost] public IActionResult Edit(CommesseVT_Table model) { SessionHelper helper = new SessionHelper(this); token = helper.GetStringValue("tok"); tenant = helper.GetStringValue("tenant"); admin = helper.GetStringValue("admin"); ViewBag.Admin = admin; if (string.IsNullOrEmpty(token)) { return RedirectToAction("Index", "Login"); } //model.lacodazi = tenant; apiUrl = helper.GetStringValue("apiUrl"); urlBase = apiUrl + "commesseVT/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(urlBase, 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, string azienda) { SessionHelper helper = new SessionHelper(this); token = helper.GetStringValue("tok"); apiUrl = helper.GetStringValue("apiUrl"); admin = helper.GetStringValue("admin"); ViewBag.Admin = admin; urlBase = apiUrl + "commesseList"; urlBase = urlBase + "?token=" + token; Uri baseaAddress = new Uri(urlBase); client = new HttpClient(); client.BaseAddress = baseaAddress; CommesseVT_Table commesse = new CommesseVT_Table(); List modelList = new List(); HttpResponseMessage response = client.GetAsync(baseaAddress).Result; if (response.IsSuccessStatusCode) { string data = response.Content.ReadAsStringAsync().Result; modelList = JsonConvert.DeserializeObject>(data); commesse = modelList.Where(x => x.laserial.Equals(id)&& x.lacodazi.Equals(azienda)).First(); return View(commesse); } else { errMes = response.Content.ReadAsStringAsync().Result; helper.SetStringValue("errMsg", errMes); return RedirectToAction("Error"); } } [HttpPost, ActionName("DeleteConfirmed")] public IActionResult DeleteConfirmed(CommesseVT_Table model) { SessionHelper helper = new SessionHelper(this); token = helper.GetStringValue("tok"); tenant = helper.GetStringValue("tenant"); //model.lacodazi = tenant; model.ladatchi = DateTime.Now; apiUrl = helper.GetStringValue("apiUrl"); urlBase = apiUrl + "commesseVT/del"; 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"); } } #endregion DELETE #region METODI INTERNI private List getImpiantiCliente(string codCli) { SessionHelper helper = new SessionHelper(this); token = helper.GetStringValue("tok"); //apiUrl = helper.GetStringValue("") 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 && x.imultcli !=null && x.imultcli.Equals(codCli)).ToList(); //per gestire primo elemento tendina (deve essere vuoto) SelectListItem listItemFirst = new SelectListItem(); listItemFirst.Value = string.Empty; listItemFirst.Text = " - Impianto"; selectItems.Add(listItemFirst); foreach (var item in modelList) { SelectListItem listItem = new SelectListItem(); string s = item.imcodimp + " - " + item.imdescri; listItem.Value = item.imcodimp; listItem.Text = s; selectItems.Add(listItem); } } return selectItems; } private List getImpianti() { SessionHelper helper = new SessionHelper(this); token = helper.GetStringValue("tok"); //apiUrl = helper.GetStringValue("") 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 listItemFirst = new SelectListItem(); listItemFirst.Value = string.Empty; listItemFirst.Text = " - Impianto"; selectItems.Add(listItemFirst); foreach (var item in modelList) { SelectListItem listItem = new SelectListItem(); string s = item.imcodimp + " - " + item.imdescri; listItem.Value = item.imcodimp; listItem.Text = s; selectItems.Add(listItem); } } return selectItems; } private List getImpiantiALL() { SessionHelper helper = new SessionHelper(this); token = helper.GetStringValue("tok"); //apiUrl = helper.GetStringValue("") 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); } return modelList; } private List getClienti() { 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); //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 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 = new List(); HttpResponseMessage response = client.GetAsync(baseAddress).Result; if (response.IsSuccessStatusCode) { string data = response.Content.ReadAsStringAsync().Result; progressivo = JsonConvert.DeserializeObject>(data); var last = progressivo.Where(x => x.tipo_prog.Equals(_sercomm)).First(); p = last.val_prog; p++; seriale = Convert.ToString(p); seriale = seriale.PadLeft(10, '0'); Progressivo update = new Progressivo(); update.val_prog = p; update.tipo_prog = _sercomm; update.azienda = tenant; UpdateSeriale(update); } return seriale; } private async void UpdateSeriale(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; 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(); } } [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 #region CASCADING public IActionResult Cascading() { Cascading model = new Cascading(); model.Clienti = getClienti(); return View(model); } [HttpPost] public ActionResult Cascading(string lacodcli) { Cascading model = new Cascading(); model.Clienti = getClienti(); if (!string.IsNullOrEmpty(lacodcli)) { List impianti = new List(); impianti = getImpiantiALL(); var impiaCli = impianti.Where(x => x.imultcli.Equals(lacodcli)).ToList(); foreach (Impianto i in impiaCli) { SelectListItem listItem = new SelectListItem(); string s = i.imcodimp + " - " + i.imdescri; listItem.Value = i.imcodimp; listItem.Text = s; model.Impianti.Add(listItem); //model.Impianti.Add(new SelectListItem { Text = i.imcodimp, Value = i.imcodimp }); } } //string data = JsonConvert.SerializeObject(model.Impianti); //return Json(data); return Json(new SelectList(model.Impianti, "Value", "Text")); } //[HttpPost] //public ActionResult GetDistrict(string ancodice) //{ // int statId; // List districtNames = new List(); // if (!string.IsNullOrEmpty(stateId)) // { // statId = Convert.ToInt32(stateId); // List districts = schoolEntity.DistrictMasters.Where(x => x.StateId == statId).ToList(); // districts.ForEach(x => // { // districtNames.Add(new SelectListItem { Text = x.DistrictName, Value = x.DistrictId.ToString() }); // }); // } // return Json(districtNames, JsonRequestBehavior.AllowGet); //} #endregion } }