public ActionResult Create(FormCollection formCollection) { if (Session["OrganisationID"] == null) { TempData["accessError"] = "Vaše session vypršela, nebo nejste přihlášen."; return RedirectToAction("LogOn", "Organisation"); } bool correct = true; CultureInfo culture = CultureInfo.CreateSpecificCulture("cs-CZ"); foreach (var key in formCollection.AllKeys) { if (key.Contains(".")) { if (String.IsNullOrWhiteSpace(formCollection[key])) { this.TempData[key + "error"] = "Zadanné pole nesmí být prázdné"; correct = false; continue; } else { this.TempData[key] = formCollection[key]; this.TempData[key + "error"] = ""; continue; } } if (String.IsNullOrWhiteSpace(formCollection[key])) { this.ViewData[key + "error"] = "Zadanné pole nesmí být prázdné"; correct = false; continue; } if (key.Contains("S")) { char[] delimiter = { '-' }; string[] identifiers = key.Split(delimiter); int subsectorID = Convert.ToInt32(identifiers[1]); int kpiID = Convert.ToInt32(identifiers[2]); SpecialisedIdentifier kpi = db.SpecialisedIdentifiersESG.Where(i => i.SpecialisedSubsectorID == subsectorID && i.ID == kpiID).SingleOrDefault(); if (kpi.UnitID == 4) { try { Convert.ToInt64(formCollection[key], culture); } catch (FormatException ex) { this.ViewData[key + "error"] = "Zadaná hodnota musí mít celočíselný formát."; correct = false; continue; } } if (kpi.UnitID == 3 || kpi.UnitID == 2) { try { Convert.ToDouble(formCollection[key], culture); } catch (FormatException ex) { this.ViewData[key + "error"] = "Zadaná hodnota musí být číslo."; correct = false; continue; } } if (kpi.UnitID == 1) { double i = 0; try { i = Convert.ToDouble(formCollection[key], culture); } catch (FormatException ex) { this.ViewData[key + "error"] = "Zadaná hodnota musí být číslo v rozsahu 0-100."; correct = false; continue; } if (i < 0 || i > 100) { this.ViewData[key + "error"] = "Zadaná hodnota musí být číslo v rozsahu 0-100."; correct = false; continue; } } this.ViewData[key] = formCollection[key]; } if (!key.Contains("S") && !key.Contains(".")) { char[] delimiter = { '-' }; string[] identifiers = key.Split(delimiter); int kpiID = Convert.ToInt32(identifiers[1]); KeyPerformanceIdentifier kpi = db.KeyPerformanceIdentifiersESG.Where(i => i.ID == kpiID).SingleOrDefault(); if (kpi.UnitID == 4) { try { Convert.ToInt64(formCollection[key], culture); } catch (FormatException ex) { this.ViewData[key + "error"] = "Zadaná hodnota musí mít celočíselný formát."; correct = false; continue; } } if (kpi.UnitID == 3 || kpi.UnitID == 2) { try { Convert.ToDouble(formCollection[key], culture); } catch (FormatException ex) { this.ViewData[key + "error"] = "Zadaná hodnota musí být číslo."; correct = false; continue; } } if (kpi.UnitID == 1) { double i = 0; try { formCollection[key] = formCollection[key].Replace(".", ","); i = Convert.ToDouble(formCollection[key], culture); } catch (FormatException ex) { this.ViewData[key + "error"] = "Zadaná hodnota musí být číslo v rozsahu 0-100."; correct = false; continue; } if (i < 0 || i > 100) { this.ViewData[key + "error"] = "Zadaná hodnota musí být číslo v rozsahu 0-100."; correct = false; continue; } } this.ViewData[key] = formCollection[key]; } } if (!correct) { ChosedIdentifiers chosedIdentifiers = new ChosedIdentifiers(); foreach (var key in formCollection.AllKeys) { if (key.StartsWith("S")) { char[] delimiter = { '-' }; string[] identifiers = key.Split(delimiter); int subsectorID = Convert.ToInt32(identifiers[1]); int kpiID = Convert.ToInt32(identifiers[2]); SpecialisedSubsector subsector = db.SpecialisedSubsectorsESG.Where(s => s.ID == subsectorID).SingleOrDefault(); chosedIdentifiers.AddSpecialisedSubsector(subsector); SpecialisedIdentifier identifier = db.SpecialisedIdentifiersESG.Where(i => i.ID == kpiID).SingleOrDefault(); chosedIdentifiers.AddSpecialisedIdentifierToSpecialisedSubsector(subsector, identifier); } else if (!key.Contains("S") && !key.Contains(".")) { char[] delimiter = { '-' }; string[] identifiers = key.Split(delimiter); int subsectorID = Convert.ToInt32(identifiers[0]); int kpiID = Convert.ToInt32(identifiers[1]); Subsector subsector = db.SubsectorsESG.Where(s => s.ID == subsectorID).SingleOrDefault(); chosedIdentifiers.AddSubsector(subsector); KeyPerformanceIdentifier kpi = db.KeyPerformanceIdentifiersESG.Where(i => i.ID == kpiID).SingleOrDefault(); chosedIdentifiers.AddIdentifierToSubsector(subsector, kpi); } } return View("Fill", chosedIdentifiers); } this.TempData.Clear(); int organisationID = (int)Session["OrganisationID"]; int representativeID = (int)Session["RepresentativeID"]; var representative = db.RepresentativesESG.Where(o => o.ID == representativeID).SingleOrDefault(); var organisation = db.CompaniesESG.Where(o => o.ID == organisationID).SingleOrDefault(); Report report = new Report(); report.NumOfViews = 0; report.CompanyID = organisationID; report.Created = DateTime.Now; report.RepresentativeID = representativeID; db.ReportsESG.Add(report); db.SaveChanges(); foreach (var key in formCollection.AllKeys) { if (key.StartsWith("S")) { char[] thirdDelimiter = { '-' }; string[] ids = key.Split(thirdDelimiter); int specialSubSectorID = Convert.ToInt32(ids[1]); int specialIdentifierID = Convert.ToInt32(ids[2]); ReportedSpecialisedIdentifier rsi = new ReportedSpecialisedIdentifier(); rsi.ReportID = report.ID; rsi.SpecialisedIdentifierID = specialIdentifierID; rsi.ReportedValue = formCollection[key]; db.ReportedSpecialisedIdentifiersESG.Add(rsi); db.SaveChanges(); continue; } if (key.Contains(".")) { char[] firstDelimiter = { '.' }; string[] ids = key.Split(firstDelimiter); int firstLabelID = Convert.ToInt32(ids[0]); int secondLabelID = Convert.ToInt32(ids[1]); OrganisationProfileField organisationProfileField = db.OrganisationProfileFieldsESG .Where(opf => opf.FirstLabelID == firstLabelID && opf.SecondLabelID == secondLabelID) .SingleOrDefault(); ReportedNonIdentifier reportedNonIdentifier = new ReportedNonIdentifier(); reportedNonIdentifier.ReportID = report.ID; reportedNonIdentifier.ReportedValue = formCollection[key]; reportedNonIdentifier.OrganisationProfileField = organisationProfileField; db.ReportedNonIdentifiersESG.Add(reportedNonIdentifier); } else { char[] SecondDelimiter = { '-' }; string[] ids = key.Split(SecondDelimiter); int subSectorID = Convert.ToInt32(ids[0]); int kpiID = Convert.ToInt32(ids[1]); ReportedIdentifier reportedIdentifier = new ReportedIdentifier(); reportedIdentifier.KeyPerformanceIdentifierID = kpiID; reportedIdentifier.ReportID = report.ID; reportedIdentifier.SubsectorID = subSectorID; reportedIdentifier.ReportedValue = formCollection[key]; db.ReportedIdentifiersESG.Add(reportedIdentifier); } db.SaveChanges(); } TempData.Clear(); return RedirectToAction("Index", "Home"); }
/// <summary> /// Displays form fiels for chosed identifiers. /// </summary> /// <param name="formCollection">object that encapsulates chosed identifiers for reporting</param> public ActionResult Fill(FormCollection formCollection) { if (Session["OrganisationID"] == null) { TempData["accessError"] = "Vaše session vypršela, nebo nejste přihlášen."; return RedirectToAction("LogOn", "Organisation"); } bool standardSubsectors = true; bool specialisedSubsectorsCorrect = true; this.TempData.Clear(); foreach (var key in formCollection.AllKeys) { if (!key.StartsWith("S")) { int subsectorID = Convert.ToInt32(key); var subsector = db.SubsectorsESG.Where(s => s.ID == subsectorID).SingleOrDefault(); char[] delimiter2 = { ',' }; string[] identifiers2 = formCollection[key].Split(delimiter2); IList<int> intIdentifiers1 = new List<int>(); foreach (var item in identifiers2) { intIdentifiers1.Add(Convert.ToInt32(item)); TempData[key + "-" + item] = "checked"; } int countScope1 = subsector.KeyPerformanceIdentifiers.Where(kpi => kpi.Scope == 1).Count(); int countScope1Check = subsector.KeyPerformanceIdentifiers .Where(kpi => kpi.Scope == 1) .Where(kpi => intIdentifiers1.Contains(kpi.ID)) .Count(); if (countScope1 != countScope1Check) { standardSubsectors = false; } } if (key.StartsWith("S")) { char[] delimiter = { '-' }; string[] parts = key.Split(delimiter); int subsectorID = Convert.ToInt32(parts[1]); var subsector = db.SpecialisedSubsectorsESG.Where(s => s.ID == subsectorID).SingleOrDefault(); char[] delimiter2 = { ',' }; string[] identifiers2 = formCollection[key].Split(delimiter2); IList<int> intIdentifiers = new List<int>(); foreach (var item in identifiers2) { intIdentifiers.Add(Convert.ToInt32(item)); TempData["S-" + subsectorID + "-" + item] = "checked"; } int countScope1 = subsector.SpecialisedIdentifiers.Where(kpi => kpi.Scope == 1 && kpi.Activated).Count(); int countScope1Check = subsector.SpecialisedIdentifiers .Where(si => si.Scope == 1 && si.Activated) .Where(si => intIdentifiers.Contains(si.ID)) .Count(); if (countScope1 != countScope1Check) { specialisedSubsectorsCorrect = false; } } } if (formCollection.AllKeys.Where(s => !s.StartsWith("S")).Count() == 0) { TempData["errorFound"] = "errorFound"; TempData["notChosedStandardSubsector"] = "Musi být zvolen alespoň 1 standardní podsektor a všechny jeho identifikátory rozsahu 1."; return RedirectToAction("Choose"); } if (!standardSubsectors || !specialisedSubsectorsCorrect) { TempData["errorFound"] = "errorFound"; TempData["notChosedStandardSubsector"] = "Musi být zvolen alespoň 1 standardní podsektor a všechny jeho identifikátory rozsahu 1."; return RedirectToAction("Choose"); } ChosedIdentifiers chosedIdentifiers = null; chosedIdentifiers = new ChosedIdentifiers(); foreach (var key in formCollection.AllKeys) { if (key.StartsWith("S")) { char[] delimiter1 = { '-' }; string[] identifiers1 = key.Split(delimiter1); int specialisedSubsectorID = Convert.ToInt32(identifiers1[1]); SpecialisedSubsector specialisedSubsector = db.SpecialisedSubsectorsESG.Where(ss => ss.ID == specialisedSubsectorID).SingleOrDefault(); chosedIdentifiers.AddSpecialisedSubsector(specialisedSubsector); char[] delimiter2 = { ',' }; string[] identifiers2 = formCollection[key].Split(delimiter2); IList<int> intIdentifiers1 = new List<int>(); foreach (var item in identifiers2) { intIdentifiers1.Add(Convert.ToInt32(item)); } var specialisedSubsectorIdentifiers = db.SpecialisedIdentifiersESG.Where(si => intIdentifiers1.Contains(si.ID)); foreach (SpecialisedIdentifier item in specialisedSubsectorIdentifiers) { chosedIdentifiers.SpecialisedSubsectorsIdentifiers[specialisedSubsector].Add(item); } continue; } int subsectorID = Convert.ToInt32(key); Subsector subsector = db.SubsectorsESG.Where(s => s.ID == subsectorID).SingleOrDefault(); chosedIdentifiers.AddSubsector(subsector); char[] delimiter = { ',' }; string[] identifiers = formCollection[key].Split(delimiter); IList<int> intIdentifiers = new List<int>(); foreach (var item in identifiers) { intIdentifiers.Add(Convert.ToInt32(item)); } var subsectorIdentifiers = db.KeyPerformanceIdentifiersESG.Where(kpi => intIdentifiers.Contains(kpi.ID)); foreach (KeyPerformanceIdentifier item in subsectorIdentifiers) { chosedIdentifiers.SubsectorsIdentifiers[subsector].Add(item); } } return View(chosedIdentifiers); }