diff --git a/Controllers/AnagController.cs b/Controllers/AnagController.cs index 31ac04c..1ac02bb 100644 --- a/Controllers/AnagController.cs +++ b/Controllers/AnagController.cs @@ -97,32 +97,72 @@ namespace VirtualTask.Controllers [HttpPost] public IActionResult Create(Anag model) { - SessionHelper helper = new SessionHelper(this); - token = helper.GetStringValue("tok"); - tenant = helper.GetStringValue("tenant"); - model.ancodazi=tenant; - apiUrl = helper.GetStringValue("apiUrl"); - urlBase = apiUrl + "anagrafiche/add"; - urlBase = urlBase + "?token=" + token; - Uri baseAddress = new Uri(urlBase); - client = new HttpClient(); - client.BaseAddress = baseAddress; - model.ancodice = 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) + if (ModelState.IsValid) { - return RedirectToAction("Index"); + 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; + bcodfiscOK = CodiceFiscale.VerificaCodiceFiscale(sCodFis); + bparivaOK = _checkVatItaFormale("IT", sParIva); + + if (!bcodfiscOK) + { + ModelState.AddModelError("andescri", "Codice Fiscale non corretto"); + } + if (!bparivaOK) + { + ModelState.AddModelError("anpariva", "Partita Iva non corretta"); + } + + if (bcodfiscOK) + { + SessionHelper helper = new SessionHelper(this); + + token = helper.GetStringValue("tok"); + tenant = helper.GetStringValue("tenant"); + model.ancodazi = tenant; + apiUrl = helper.GetStringValue("apiUrl"); + urlBase = apiUrl + "anagrafiche/add"; + urlBase = urlBase + "?token=" + token; + Uri baseAddress = new Uri(urlBase); + client = new HttpClient(); + client.BaseAddress = baseAddress; + model.ancodice = 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"); + } + } + else + { + return View("Create", model); + } } else { - errMes = response.Content.ReadAsStringAsync().Result; - helper.SetStringValue("errMsg", errMes); - return RedirectToAction("Error"); + foreach (var Elemento in ModelState.Values) + { + foreach (var Errore in Elemento.Errors) + { + string ErroreRilevato = Errore.ErrorMessage; + } + + } + return RedirectToAction("Create"); } + } #endregion CREATE @@ -368,5 +408,289 @@ namespace VirtualTask.Controllers 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.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.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; + } + } } diff --git a/Models/Anag.cs b/Models/Anag.cs index cc74670..5e0323e 100644 --- a/Models/Anag.cs +++ b/Models/Anag.cs @@ -45,7 +45,7 @@ namespace VirtualTask.Models /// pec public string? an_empec { get; set; } - [Display(Name = "RAGIONE SOCIALE")] + [Display(Name = "RAGIONE SOCIALE"), Required(ErrorMessage = "Ragione sociale obbligatoria")] /// descrizione public string? andescri { get; set; } diff --git a/Views/Anag/Create.cshtml b/Views/Anag/Create.cshtml index 75fee19..98836fe 100644 --- a/Views/Anag/Create.cshtml +++ b/Views/Anag/Create.cshtml @@ -12,6 +12,8 @@
+ @Html.ValidationSummary(false, "", new { @class = "text-danger" }) + @Html.HiddenFor(x => x.andtobso) @Html.HiddenFor(x => x.annumlis) @@ -20,43 +22,42 @@
- +@* *@
-
- +@* *@
- +@* *@
- +@* *@
- +@* *@
- +@* *@
- +@* *@
- +@* *@