private object ReadCellsForPatient(Patient patient, IRow row, int cellCount)
        {
            var pft = new PatientPulmonaryFunctionTest();
            var hmt = new PatientHaematology();

            for (int cellCursor = 0; cellCursor < cellCount; cellCursor++)
            {
                if (row.GetCell(cellCursor, MissingCellPolicy.CREATE_NULL_AS_BLANK) != null)
                {
                    ReadCell(patient, pft, hmt, row, cellCursor);
                }
            }
            Imported.Add(patient);
            return(patient);
        }
        public List <PatientPulmonaryFunctionTest> ResolvePFTs(AspergillosisContext context, Patient patient)
        {
            var pfts = new List <PatientPulmonaryFunctionTest>();

            foreach (var test in Tests)
            {
                try
                {
                    var patientPulmonaryFunctionTest = new PatientPulmonaryFunctionTest();
                    var dbTest = context.PulmonaryFunctionTests.Where(pft => pft.ShortName.Equals(test)).FirstOrDefault();
                    if (dbTest == null)
                    {
                        continue;
                    }
                    patientPulmonaryFunctionTest.PulmonaryFunctionTest = dbTest;
                    patientPulmonaryFunctionTest.Patient = patient;
                    var    property  = GetType().GetProperty(test + "Value");
                    string testValue = property.GetValue(this, null)?.ToString();
                    if (testValue == null)
                    {
                        continue;
                    }
                    var decimalTest = Convert.ToDecimal(testValue);
                    patientPulmonaryFunctionTest.DateTaken   = (DateTime)Convert.ToDateTime(DateTaken);
                    patientPulmonaryFunctionTest.ResultValue = decimalTest;
                    var calculator = GetCalculatorForPatient(test, patient);
                    if (calculator != null)
                    {
                        var predictedValue = calculator.Calculate(AgeAsInteger(), HeightAsDouble());
                        var predicted      = calculator.Percentage(AgeAsInteger(), HeightAsDouble(), Convert.ToDouble(decimalTest));
                        patientPulmonaryFunctionTest.PredictedValue = Convert.ToDecimal(predicted);
                        patientPulmonaryFunctionTest.NormalValue    = predictedValue;
                        patientPulmonaryFunctionTest.Age            = AgeAsInteger();
                        patientPulmonaryFunctionTest.Height         = Convert.ToInt32(HeightAsDouble() * 100);
                    }
                    patientPulmonaryFunctionTest.CreatedDate = DateTime.Now;
                    pfts.Add(patientPulmonaryFunctionTest);
                } catch (OverflowException ex)
                {
                    continue;
                }
            }
            return(pfts);
        }
        private void ReadCell(Patient patient, PatientPulmonaryFunctionTest pft,
                              PatientHaematology hmt, IRow row, int cellCursor)
        {
            string header          = _headers.ElementAt(cellCursor);
            string newObjectFields = (string)_dictonary[header];

            if (string.IsNullOrEmpty(newObjectFields))
            {
                return;
            }
            string propertyValue = row.GetCell(cellCursor, MissingCellPolicy.CREATE_NULL_AS_BLANK).ToString();

            if (!string.IsNullOrEmpty(propertyValue) && newObjectFields != null)
            {
                var    klassAndField = newObjectFields.Split(".");
                string propertyName  = klassAndField[1];
                switch (klassAndField[0])
                {
                case "PatientPulmonaryFunctionTest":
                    var test = _context.PulmonaryFunctionTests.Where(p => p.ShortName.Equals(header))
                               .FirstOrDefault();
                    pft = new PatientPulmonaryFunctionTest();
                    string property = "ResultValue";
                    var    value    = row.GetCell(cellCursor).NumericCellValue;
                    pft.GetType().GetProperty(property).SetValue(pft, (decimal)value);
                    pft.PulmonaryFunctionTestId = test.ID;
                    pft.PatientId = patient.ID;

                    property = "PredictedValue";
                    var predHeaderIndex = _headers.FindIndex(h => h.Contains(header + "Predicted"));
                    var predHeaderValue = row.GetCell(predHeaderIndex)?.NumericCellValue;
                    if (predHeaderValue != null)
                    {
                        pft.GetType().GetProperty(property).SetValue(pft, (decimal)predHeaderValue);
                    }


                    var dateHeaderIndex = _headers.FindIndex(h => h.Contains("DateOfTest"));
                    var dateCellValue   = row.GetCell(dateHeaderIndex).DateCellValue;
                    pft.GetType().GetProperty("DateTaken").SetValue(pft, dateCellValue);
                    if (patient.PatientPulmonaryFunctionTests == null)
                    {
                        patient.PatientPulmonaryFunctionTests = new List <PatientPulmonaryFunctionTest>();
                    }
                    patient.PatientPulmonaryFunctionTests.Add(pft);
                    break;

                case "PatientMeasurement":
                    var meas      = new PatientMeasurement();
                    var dateIndex = _headers.FindIndex(h => h.Contains("DateOfTest"));
                    var dateValue = row.GetCell(dateIndex).DateCellValue;
                    meas.DateTaken = dateValue;

                    var heightIdx   = _headers.FindIndex(h => h.Contains("Height"));
                    var heightValue = row.GetCell(heightIdx)?.NumericCellValue;

                    var weightIdx   = _headers.FindIndex(h => h.Contains("Weight"));
                    var weightValue = row.GetCell(weightIdx)?.NumericCellValue;

                    if (heightValue != null)
                    {
                        meas.Height = Convert.ToDecimal(heightValue);
                    }
                    if (weightValue != null)
                    {
                        meas.Weight = Convert.ToDecimal(weightValue);
                    }

                    if (patient.PatientMeasurements == null)
                    {
                        patient.PatientMeasurements = new List <PatientMeasurement>();
                    }
                    patient.PatientMeasurements.Add(meas);
                    break;

                case "PatientHaematology":
                    if (header.Equals("HaematologyDate"))
                    {
                        propertyName = "DateTaken";
                        var hemValue = row.GetCell(cellCursor).DateCellValue;
                        hmt.GetType().GetProperty(propertyName).SetValue(hmt, hemValue);
                        hmt.PatientId = patient.ID;
                        if (patient.PatientHaematologies == null)
                        {
                            patient.PatientHaematologies = new List <PatientHaematology>();
                        }
                        patient.PatientHaematologies.Add(hmt);

                        var hbInd = _headers.FindIndex(h => h.Contains("Hb"));

                        var hbValue = row.GetCell(hbInd)?.NumericCellValue;
                        if (hbValue != null)
                        {
                            hmt.Hb = hbValue.Value;
                        }

                        var wbcInd = _headers.FindIndex(h => h.Contains("WBC"));
                        if (wbcInd != -1)
                        {
                            var wbcValue = row.GetCell(wbcInd)?.NumericCellValue;
                            if (wbcValue != null)
                            {
                                hmt.WBC = wbcValue.Value;
                            }
                        }


                        var albInd  = _headers.FindIndex(h => h.Contains("Albumin"));
                        var albVaue = row.GetCell(albInd)?.NumericCellValue;


                        if (albVaue != null)
                        {
                            hmt.Albumin = albVaue.Value;
                        }
                    }
                    break;
                }
            }
        }
 public SpirometryCalculator(PatientPulmonaryFunctionTest patientPulmonaryFunctionTest)
 {
     _patientPulmonaryFunctionTest = patientPulmonaryFunctionTest;
     SetEquationList();
 }