public ActionResult AddPatientLabTest(int id) { ViewBag.MenuItem = CurrentMenuItem; var returnUrl = (TempData["returnUrl"] ?? Request.UrlReferrer ?? (object)string.Empty).ToString(); TempData["returnUrl"] = returnUrl; ViewBag.ReturnUrl = returnUrl; var patient = unitOfWork.Repository <Patient>().Get(id); if (patient == null) { ViewBag.Entity = "Patient"; return(View("NotFound")); } var model = new PatientLabTestAddModel { PatientId = id, PatientFullName = patient.FullName, TestDate = DateTime.Today }; var labTests = new List <SelectListItem> { { new SelectListItem { Value = "", Text = "" } } }; labTests.AddRange(unitOfWork.Repository <LabTest>() .Queryable() .Select(c => new SelectListItem { Value = c.Id.ToString(), Text = c.Description }) .OrderBy(c => c.Text) .ToArray()); ViewBag.LabTests = labTests; var labTestUnits = new List <SelectListItem> { { new SelectListItem { Value = "", Text = "" } } }; labTestUnits.AddRange(unitOfWork.Repository <LabTestUnit>() .Queryable() .OrderBy(c => c.Description) .Select(c => new SelectListItem { Value = c.Id.ToString(), Text = c.Description }) .ToArray()); ViewBag.LabTestUnits = labTestUnits; var labResults = new List <SelectListItem> { { new SelectListItem { Value = "", Text = "" } } }; labResults.AddRange(unitOfWork.Repository <LabResult>() .Queryable() .OrderBy(c => c.Description) .Select(c => new SelectListItem { Value = c.Description, Text = c.Description }) .ToArray()); ViewBag.TestResults = labResults; var customAttributes = unitOfWork.Repository <CustomAttributeConfiguration>() .Queryable() .Where(ca => ca.ExtendableTypeName == "PatientLabTest") .ToList(); model.CustomAttributes = customAttributes.Select(c => new CustomAttributeAddModel { Name = c.AttributeKey, Detail = c.AttributeDetail == null ? "" : "(" + c.AttributeDetail + ")", Type = c.CustomAttributeType.ToString(), IsRequired = c.IsRequired, StringMaxLength = c.StringMaxLength, NumericMinValue = c.NumericMinValue, NumericMaxValue = c.NumericMaxValue, PastDateOnly = c.PastDateOnly, FutureDateOnly = c.FutureDateOnly }) .ToArray(); var selectiondataRepository = unitOfWork.Repository <SelectionDataItem>(); foreach (var selectCustomAttribute in customAttributes.Where(c => c.CustomAttributeType == VPS.CustomAttributes.CustomAttributeType.Selection)) { ViewData[selectCustomAttribute.AttributeKey] = selectiondataRepository .Queryable() .Where(sd => sd.AttributeKey == selectCustomAttribute.AttributeKey) .Select(s => new SelectListItem { Value = s.SelectionKey, Text = s.Value }) .ToArray(); } return(View(model)); }
public ActionResult AddPatientLabTest(PatientLabTestAddModel model) { ViewBag.MenuItem = CurrentMenuItem; var returnUrl = (TempData["returnUrl"] ?? string.Empty).ToString(); ViewBag.ReturnUrl = returnUrl; var patient = unitOfWork.Repository <Patient>().Get(model.PatientId); if (patient == null) { ViewBag.Entity = "Patient"; return(View("NotFound")); } if (model.TestDate < patient.DateOfBirth) { ModelState.AddModelError("TestDate", "Test Date should be after Date Of Birth"); } if (ModelState.IsValid) { var labTest = unitOfWork.Repository <LabTest>().Get(model.LabTestId); if (labTest == null) { ViewBag.Entity = "Lab Test"; return(View("NotFound")); } try { var labTestUnit = unitOfWork.Repository <LabTestUnit>().Queryable().SingleOrDefault(lu => lu.Id == model.LabTestUnitId); var patientLabTest = new PatientLabTest { Patient = patient, LabTest = labTest, TestDate = model.TestDate, TestResult = model.TestResult, LabValue = model.LabValue, TestUnit = labTestUnit, ReferenceLower = model.ReferenceLower, ReferenceUpper = model.ReferenceUpper }; if (model.CustomAttributes != null) { var patientLabTestExtended = (IExtendable)patientLabTest; var customAttributes = unitOfWork.Repository <CustomAttributeConfiguration>().Queryable().Where(ca => ca.ExtendableTypeName == typeof(PatientLabTest).Name).ToList(); for (int i = 0; i < model.CustomAttributes.Length; i++) { var attributeConfig = GetFrameworkCustomAttributeConfig(customAttributes, model.CustomAttributes[i].Name); // If there is not custom attribute configured with this name, ignore. if (attributeConfig == null) { continue; } try { if (attributeConfig.IsRequired && string.IsNullOrWhiteSpace(model.CustomAttributes[i].Value)) { ModelState.AddModelError(string.Format("CustomAttributes[{0}].Value", i), string.Format("{0} is required.", model.CustomAttributes[i].Name)); continue; } switch (model.CustomAttributes[i].Type) { case "Numeric": decimal number = 0M; if (decimal.TryParse(model.CustomAttributes[i].Value, out number)) { patientLabTestExtended.ValidateAndSetAttributeValue(attributeConfig, number, User.Identity.Name); } break; case "Selection": Int32 selection = 0; if (Int32.TryParse(model.CustomAttributes[i].Value, out selection)) { patientLabTestExtended.ValidateAndSetAttributeValue(attributeConfig, selection, User.Identity.Name); } break; case "DateTime": DateTime parsedDate = DateTime.MinValue; if (DateTime.TryParse(model.CustomAttributes[i].Value, out parsedDate)) { patientLabTestExtended.ValidateAndSetAttributeValue(attributeConfig, parsedDate, User.Identity.Name); } break; case "String": default: patientLabTestExtended.ValidateAndSetAttributeValue(attributeConfig, model.CustomAttributes[i].Value ?? string.Empty, User.Identity.Name); break; } } catch (CustomAttributeValidationException ve) { ModelState.AddModelError(string.Format("CustomAttributes[{0}].Value", i), ve.Message); continue; } } } if (ModelState.IsValid) { unitOfWork.Repository <PatientLabTest>().Save(patientLabTest); unitOfWork.Complete(); HttpCookie cookie = new HttpCookie("PopUpMessage"); cookie.Value = "Test and procedure added successfully"; Response.Cookies.Add(cookie); return(Redirect("/Patient/PatientView.aspx?pid=" + patientLabTest.Patient.Id.ToString())); } } catch (Exception ex) { ModelState.AddModelError("", string.Format("Unable to add the Patient Lab Test: {0}", ex.Message)); } } // Prepare custom attributes var cattributes = unitOfWork.Repository <CustomAttributeConfiguration>() .Queryable() .Where(ca => ca.ExtendableTypeName == typeof(PatientLabTest).Name) .ToList(); model.CustomAttributes = cattributes.Select(c => new CustomAttributeAddModel { Name = c.AttributeKey, Detail = c.AttributeDetail == null ? "" : "(" + c.AttributeDetail + ")", Type = c.CustomAttributeType.ToString(), IsRequired = c.IsRequired, StringMaxLength = c.StringMaxLength, NumericMinValue = c.NumericMinValue, NumericMaxValue = c.NumericMaxValue, PastDateOnly = c.PastDateOnly, FutureDateOnly = c.FutureDateOnly }) .ToArray(); var selectiondataRepository = unitOfWork.Repository <SelectionDataItem>(); if (model.CustomAttributes != null) { foreach (var selectCustomAttribute in model.CustomAttributes.Where(c => c.Type == VPS.CustomAttributes.CustomAttributeType.Selection.ToString())) { ViewData[selectCustomAttribute.Name] = selectiondataRepository .Queryable() .Where(sd => sd.AttributeKey == selectCustomAttribute.Name) .Select(s => new SelectListItem { Value = s.SelectionKey, Text = s.Value }) .ToArray(); } } var labTests = new List <SelectListItem> { { new SelectListItem { Value = "", Text = "" } } }; labTests.AddRange(unitOfWork.Repository <LabTest>() .Queryable() .Select(c => new SelectListItem { Value = c.Id.ToString(), Text = c.Description }) .OrderBy(c => c.Text) .ToArray()); ViewBag.LabTests = labTests; var labTestUnits = new List <SelectListItem> { { new SelectListItem { Value = "", Text = "" } } }; labTestUnits.AddRange(unitOfWork.Repository <LabTestUnit>() .Queryable() .OrderBy(c => c.Description) .Select(c => new SelectListItem { Value = c.Id.ToString(), Text = c.Description }) .ToArray()); ViewBag.LabTestUnits = labTestUnits; var labResults = new List <SelectListItem> { { new SelectListItem { Value = "", Text = "" } } }; labResults.AddRange(unitOfWork.Repository <LabResult>() .Queryable() .OrderBy(c => c.Description) .Select(c => new SelectListItem { Value = c.Description, Text = c.Description }) .ToArray()); ViewBag.TestResults = labResults; TempData["returnUrl"] = returnUrl; return(View(model)); }