private Task <SoilTestsViewModel> GetSoilTestAsync() { SoilTestsViewModel svm = new SoilTestsViewModel(); svm.missingTests = false; FarmDetails fd = _ud.FarmDetails(); svm.testingMethod = fd.testingMethod; svm.tests = new List <DisplaySoilTest>(); List <Field> flds = _ud.GetFields(); foreach (var m in flds) { DisplaySoilTest dc = new DisplaySoilTest(); dc.fldName = m.fieldName; if (m.soilTest != null) { dc.sampleDate = m.soilTest.sampleDate.ToString("MMM-yyyy"); dc.dispNO3H = m.soilTest.valNO3H.ToString("G29"); dc.dispP = m.soilTest.ValP.ToString("G29"); dc.dispK = m.soilTest.valK.ToString("G29"); dc.dispPH = m.soilTest.valPH.ToString("G29"); dc.dispPRating = _sd.GetPhosphorusSoilTestRating(_soilTestConverter.GetConvertedSTP(_ud.FarmDetails()?.testingMethod, m.soilTest)); dc.dispKRating = _sd.GetPotassiumSoilTestRating(_soilTestConverter.GetConvertedSTK(_ud.FarmDetails()?.testingMethod, m.soilTest)); } else { svm.missingTests = true; } svm.tests.Add(dc); } return(Task.FromResult(svm)); }
public IActionResult SoilTestDetails(SoilTestDetailsViewModel tvm) { decimal nmbr; if (ModelState.IsValid) { if (!Decimal.TryParse(tvm.dispNO3H, out nmbr)) { ModelState.AddModelError("dispNO3H", "Numbers only."); } else { if (nmbr < 0) { ModelState.AddModelError("dispNO3H", "Invalid."); } } if (!Decimal.TryParse(tvm.dispP, out nmbr)) { ModelState.AddModelError("dispP", "Numbers only."); } else { if (nmbr < 0) { ModelState.AddModelError("dispP", "Invalid."); } } if (!Decimal.TryParse(tvm.dispK, out nmbr)) { ModelState.AddModelError("dispK", "Numbers only."); } else { if (nmbr < 0) { ModelState.AddModelError("dispK", "Invalid."); } } if (!Decimal.TryParse(tvm.dispPH, out nmbr)) { ModelState.AddModelError("dispPH", "Numbers only."); } else { if (nmbr < 0 || nmbr > 14) { ModelState.AddModelError("dispPH", "Invalid."); } } if (!ModelState.IsValid) { return(View(tvm)); } Field fld = _ud.GetFieldDetails(tvm.fieldName); if (fld.soilTest == null) { fld.soilTest = new SoilTest(); } fld.soilTest.sampleDate = Convert.ToDateTime(tvm.sampleDate); fld.soilTest.ValP = Convert.ToDecimal(tvm.dispP); fld.soilTest.valK = Convert.ToDecimal(tvm.dispK); fld.soilTest.valNO3H = Convert.ToDecimal(tvm.dispNO3H); fld.soilTest.valPH = Convert.ToDecimal(tvm.dispPH); fld.soilTest.ConvertedKelownaK = _soilTestConversions.GetConvertedSTK(_ud.FarmDetails()?.testingMethod, fld.soilTest); fld.soilTest.ConvertedKelownaP = _soilTestConversions.GetConvertedSTP(_ud.FarmDetails()?.testingMethod, fld.soilTest); _ud.UpdateFieldSoilTest(fld); //update the Nutrient calculations with the new/changed soil test data Utility.ChemicalBalanceMessage cbm = new Utility.ChemicalBalanceMessage(_ud, _sd); cbm.RecalcCropsSoilTestMessagesByField(tvm.fieldName); string target = "#test"; string url = Url.Action("RefreshTestList", "Soil"); return(Json(new { success = true, url = url, target = target })); } return(View(tvm)); }