using Humanizer; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.Rendering; using Newtonsoft.Json; using NuGet.Common; using System.Diagnostics; using System.Reflection; using System.Runtime.Intrinsics.Arm; using System.Security.Policy; using System.Text; using System.Text.Json.Nodes; using VirtualTask.Models; using X.PagedList; namespace VirtualTask.Controllers { public class AnagController : Controller { string apiUrl = string.Empty; string urlBase=string.Empty; string token=string.Empty; string tenant = string.Empty; string errMes = string.Empty; HttpClient client; string _serchiam = "SER_ANAGRA"; string admin = string.Empty; //seriale commessa string _sercomm = "SER_COMMES"; string time_sheet=string.Empty; private readonly IConfiguration _configuration; public AnagController(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("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+"anagraficheList"; urlBase = urlBase + "?token=" + token; Uri baseAddress = new Uri(urlBase); 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.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.ancodice) .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; return View(); } [HttpPost] public IActionResult Create(Anag model) { SessionHelper helper = new SessionHelper(this); admin = helper.GetStringValue("admin"); ViewBag.Admin = admin; AnagCommImp anagCommImp = new AnagCommImp(); if (ModelState.IsValid) { bool bcodfiscOK = false; bool bparivaOK = false; string sCodFis = !string.IsNullOrEmpty(model.ancodfis) ? model.ancodfis : string.Empty; string sParIva = !string.IsNullOrEmpty(model.anpariva) ? model.anpariva : string.Empty; if (string.IsNullOrEmpty(sParIva)) { bparivaOK = true; } else { bparivaOK = _checkVatItaFormale("IT", sParIva); } if (string.IsNullOrEmpty(sCodFis)) { bcodfiscOK = true; } else { bcodfiscOK = CodiceFiscale.VerificaCodiceFiscale(sCodFis); } if (!bcodfiscOK) { ModelState.AddModelError("ancodfis", "Codice Fiscale non corretto"); } if (!bparivaOK) { ModelState.AddModelError("anpariva", "Partita Iva non corretta"); } if (bcodfiscOK && bparivaOK) { //SessionHelper helper = new SessionHelper(this); token = helper.GetStringValue("tok"); tenant = helper.GetStringValue("tenant"); model.ancodazi = tenant; apiUrl = helper.GetStringValue("apiUrl"); admin = helper.GetStringValue("admin"); ViewBag.Admin = admin; //urlBase = apiUrl + "anagrafiche/add"; urlBase = apiUrl + "anagrafiche/addcommImp"; urlBase = urlBase + "?token=" + token; Uri baseAddress = new Uri(urlBase); client = new HttpClient(); client.BaseAddress = baseAddress; model.ancodice = getNewSeriale(); //cliente anagCommImp.cli = model; //impianto generico Impianto impianto = new Impianto(); var codcli = model.ancodice.Substring(model.ancodice.Length - 6, 6); anagCommImp.imp = impianto; anagCommImp.imp.imcodimp = "IMP_" + codcli; anagCommImp.imp.imdescri = "IMP_GEN_" + codcli; anagCommImp.imp.imultcli = model.ancodice; anagCommImp.imp.imindiri2 = model.anindiri; //commmessa generica CommesseVT_Table commessa = new CommesseVT_Table(); anagCommImp.comm = commessa; anagCommImp.comm.laserial = GetNewSerialeComm(); anagCommImp.comm.lacodcom = "COM_" + model.ancodice.Substring(model.ancodice.Length - 6, 6); anagCommImp.comm.ladeslav = "COMM_GEN_" + model.ancodice; anagCommImp.comm.lacodcli = model.ancodice; anagCommImp.comm.lacodimp = anagCommImp.imp.imcodimp; anagCommImp.comm.lacomgen = "S"; string data = JsonConvert.SerializeObject(anagCommImp); 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"); } } else { return View("Create", model); } } else { foreach (var Elemento in ModelState.Values) { foreach (var Errore in Elemento.Errors) { string ErroreRilevato = Errore.ErrorMessage; } } return RedirectToAction("Create"); } } #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 + "anagraficheList"; urlBase = urlBase + "?token=" + token; Uri baseAddress = new Uri(urlBase); client = new HttpClient(); client.BaseAddress = baseAddress; Anag anag = new Anag(); List modelList = new List(); HttpResponseMessage response = client.GetAsync(baseAddress).Result; if (response.IsSuccessStatusCode) { string data = response.Content.ReadAsStringAsync().Result; modelList = JsonConvert.DeserializeObject>(data); anag = modelList.Where(x => x.ancodice.Equals(id)).First(); } else { errMes = response.Content.ReadAsStringAsync().Result; helper.SetStringValue("errMsg", errMes); return RedirectToAction("Error"); } return View(anag); } #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 + "anagraficheList"; urlBase = urlBase + "?token=" + token; Uri baseAddress = new Uri(urlBase); client = new HttpClient(); client.BaseAddress = baseAddress; Anag anag = new Anag(); List modelList = new List(); HttpResponseMessage response = client.GetAsync(baseAddress).Result; if (response.IsSuccessStatusCode) { string data = response.Content.ReadAsStringAsync().Result; modelList = JsonConvert.DeserializeObject>(data); anag = modelList.Where(x => x.ancodice.Equals(id)).First(); } else { errMes = response.Content.ReadAsStringAsync().Result; helper.SetStringValue("errMsg", errMes); return RedirectToAction("Error"); } return View(anag); } [HttpPost] public IActionResult Edit(Anag model) { SessionHelper helper = new SessionHelper(this); admin = helper.GetStringValue("admin"); ViewBag.Admin = admin; bool bcodfiscOK = false; bool bparivaOK = false; string sCodFis = !string.IsNullOrEmpty(model.ancodfis) ? model.ancodfis : string.Empty; string sParIva = !string.IsNullOrEmpty(model.anpariva) ? model.anpariva : string.Empty; if (string.IsNullOrEmpty(sParIva)) { bparivaOK = true; } else { bparivaOK = _checkVatItaFormale("IT", sParIva); } if (string.IsNullOrEmpty(sCodFis)) { bcodfiscOK = true; } else { bcodfiscOK = CodiceFiscale.VerificaCodiceFiscale(sCodFis); } if (!bcodfiscOK) { ModelState.AddModelError("ancodfis", "Codice Fiscale non corretto"); } if (!bparivaOK) { ModelState.AddModelError("anpariva", "Partita Iva non corretta"); } if (bcodfiscOK && bparivaOK) { //SessionHelper helper = new SessionHelper(this); token = helper.GetStringValue("tok"); tenant = helper.GetStringValue("tenant"); model.ancodazi = tenant; apiUrl = helper.GetStringValue("apiUrl"); urlBase = apiUrl + "anagrafiche/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"); } } else { return View("Edit", 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 + "anagraficheList"; urlBase = urlBase + "?token=" + token; Uri baseAddress = new Uri(urlBase); client = new HttpClient(); client.BaseAddress = baseAddress; Anag anag = new Anag(); List modelList = new List(); HttpResponseMessage response = client.GetAsync(baseAddress).Result; if (response.IsSuccessStatusCode) { string data = response.Content.ReadAsStringAsync().Result; modelList = JsonConvert.DeserializeObject>(data); anag = modelList.Where(x => x.ancodice.Equals(id)).First(); } return View(anag); } //DA MODIFICARE PERCHE' NON E' UNA DELETE MA UN UPDATE DELLA DATA OBSOLESCENZA [HttpPost, ActionName("DeleteConfirmed")] public IActionResult DeleteConfirmed(Anag model) { SessionHelper helper = new SessionHelper(this); token = helper.GetStringValue("tok"); tenant = helper.GetStringValue("tenant"); model.ancodazi = tenant; model.andtobso=DateTime.Now; apiUrl = helper.GetStringValue("apiUrl"); admin = helper.GetStringValue("admin"); ViewBag.Admin = admin; urlBase = apiUrl + "anagrafiche/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 [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 }); } 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 progressivi = new List(); HttpResponseMessage response = client.GetAsync(baseAddress).Result; if (response.IsSuccessStatusCode) { string data = response.Content.ReadAsStringAsync().Result; progressivi = JsonConvert.DeserializeObject>(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; updateSeriale(upd ); } 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; //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(); } } static class CodiceFiscale { public static bool VerificaCodiceFiscale(string Codice) { // Dato un Codice Fiscale verifica il codice di controllo // Input: il Codice Fiscale da verificare, 16 caratteri // Output: true/false // 2010.12.05 Codice = Codice.Trim().ToUpper(); if (Codice.Length != 16) return false; // errore else { if (Codice.Substring(15, 1) == CalcolaCodiceControllo(Codice.Substring(0, 15))) return true; else return false; } } private static string CalcolaCodiceControllo(string Codice) { // Calcola il codice di controllo del Codice Fiscale // Input: i primi 15 caratteri del Codice Fiscale // Output: il codice di controllo // 2010.12.05 int Contatore = 0; Codice = Codice.ToUpper(); if (Codice.Length != 15) return "0"; // zero: errore else { for (int i = 0; i < Codice.Length; i++) { Contatore += ValoreDelCarattere(Codice.Substring(i, 1), i); } Contatore %= 26; // si considera il resto return Convert.ToChar(Contatore + 65).ToString(); } } private static int ValoreDelCarattere(string Carattere, int Posizione) { int Valore = 0; switch (Carattere) { case "A": case "0": if ((Posizione % 2) == 0) Valore = 1; else Valore = 0; break; case "B": case "1": if ((Posizione % 2) == 0) Valore = 0; else Valore = 1; break; case "C": case "2": if ((Posizione % 2) == 0) Valore = 5; else Valore = 2; break; case "D": case "3": if ((Posizione % 2) == 0) Valore = 7; else Valore = 3; break; case "E": case "4": if ((Posizione % 2) == 0) Valore = 9; else Valore = 4; break; case "F": case "5": if ((Posizione % 2) == 0) Valore = 13; else Valore = 5; break; case "G": case "6": if ((Posizione % 2) == 0) Valore = 15; else Valore = 6; break; case "H": case "7": if ((Posizione % 2) == 0) Valore = 17; else Valore = 7; break; case "I": case "8": if ((Posizione % 2) == 0) Valore = 19; else Valore = 8; break; case "J": case "9": if ((Posizione % 2) == 0) Valore = 21; else Valore = 9; break; case "K": if ((Posizione % 2) == 0) Valore = 2; else Valore = 10; break; case "L": if ((Posizione % 2) == 0) Valore = 4; else Valore = 11; break; case "M": if ((Posizione % 2) == 0) Valore = 18; else Valore = 12; break; case "N": if ((Posizione % 2) == 0) Valore = 20; else Valore = 13; break; case "O": if ((Posizione % 2) == 0) Valore = 11; else Valore = 14; break; case "P": if ((Posizione % 2) == 0) Valore = 3; else Valore = 15; break; case "Q": if ((Posizione % 2) == 0) Valore = 6; else Valore = 16; break; case "R": if ((Posizione % 2) == 0) Valore = 8; else Valore = 17; break; case "S": if ((Posizione % 2) == 0) Valore = 12; else Valore = 18; break; case "T": if ((Posizione % 2) == 0) Valore = 14; else Valore = 19; break; case "U": if ((Posizione % 2) == 0) Valore = 16; else Valore = 20; break; case "V": if ((Posizione % 2) == 0) Valore = 10; else Valore = 21; break; case "W": if ((Posizione % 2) == 0) Valore = 22; else Valore = 22; break; case "X": if ((Posizione % 2) == 0) Valore = 25; else Valore = 23; break; case "Y": if ((Posizione % 2) == 0) Valore = 24; else Valore = 24; break; case "Z": if ((Posizione % 2) == 0) Valore = 23; else Valore = 25; break; default: Valore = 0; break; } return Valore; } } private bool _checkVatItaFormale(string _myCountry, string _myVatNumber) { bool _isValid = false; Int16 _ris; try { if (_myCountry.CompareTo("IT") == 0) { if (_myVatNumber.Trim().Length == 11) { Int32 s = 0; int n = 1; // sommare ad s le cifre di posto dispari (dalla prima alla nona) while (n <= 9) { _ris = Convert.ToInt16(_myVatNumber.Substring(n - 1, 1)); s = s + _ris; n = n + 2; } // per ogni cifra di posto pari (dalla seconda alla decima), // moltiplicare la cifra per due e, se risulta piu' di 9, sottrarre 9; // quindi aggiungere il risultato a s; n = 2; while (n <= 10) { _ris = Convert.ToInt16(_myVatNumber.Substring(n - 1, 1)); _ris = Convert.ToInt16(_ris * 2); if (_ris > 9) { _ris = Convert.ToInt16(_ris - 9); } s = s + _ris; n = n + 2; } // si calcola il resto della divisione di s per 10 var _resto = s % 10; // calcolo ultima cifra della partita iva Int16 c = Convert.ToInt16((_resto == 0) ? 0 : 10 - _resto); _isValid = Convert.ToInt16(_myVatNumber.Substring(10, 1)) == c ? true : false; } } } catch { // noop _isValid = false; } return _isValid; } //per aggiornare seriale commessa tabella progressivi private string GetNewSerialeComm() { 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(); // } //} #endregion altri metodi } }