public string Process(DtoDrugDispensed drugDispensed) { try { LookupLogic facilityLookup = new LookupLogic(); string receivingFacilityMflCode = drugDispensed.MESSAGE_HEADER.RECEIVING_FACILITY; string sendingFacilityMflCode = drugDispensed.MESSAGE_HEADER.SENDING_FACILITY; //check if facility exists LookupFacility recieverfacility = facilityLookup.GetFacility(receivingFacilityMflCode); LookupFacility senderfacility = facilityLookup.GetFacility(sendingFacilityMflCode); if (recieverfacility == null) { return(Msg = $"The facility {receivingFacilityMflCode} does not exist"); } if (senderfacility == null) { return(Msg = $"The facility {sendingFacilityMflCode} does not exist"); } if (recieverfacility.FacilityID != senderfacility.FacilityID) { return(Msg = "The sending facility is not the same as the receiving facility!"); } //check if it is the right facility LookupFacility thisFacility = facilityLookup.GetFacility(); if (thisFacility == null) { return(Msg = "Facility not found"); } if (recieverfacility.FacilityID != thisFacility.FacilityID) { return(Msg = $"This message belongs to {receivingFacilityMflCode}, not this facility {thisFacility.MFLCode}!"); } var patientLookup = new PatientLookupManager(); //check patient var identifier = drugDispensed.PATIENT_IDENTIFICATION.INTERNAL_PATIENT_ID.FirstOrDefault(n => n.IDENTIFIER_TYPE == "CCC_NUMBER"); if (identifier == null) { return(Msg = "Message does not contain a CCC number!"); } var patient = patientLookup.GetPatientByCccNumber(identifier.ID); if (patient == null) { return(Msg = "Patient could not be found!"); } //check pharmacy order exists int orderId = Convert.ToInt32(drugDispensed.COMMON_ORDER_DETAILS.PLACER_ORDER_NUMBER.NUMBER); var pharmacyOrder = _pharmacyOrderManager.GetPharmacyOrder(orderId); if (pharmacyOrder == null) { return(Msg = "Pharmacy Order could not be found!"); } var orderedDrugs = _pharmacyDispenseManager.GetByPharmacyOrderId(pharmacyOrder.ptn_pharmacy_pk); if (orderedDrugs != null) { var newDispensedDrugs = orderedDrugs; foreach (var drug in newDispensedDrugs) { //todo refactor to use drug codes and possibly drug ids to be included in the message var drugFind = _drugManager.GetDrug(drug.Drug_Pk); string drugname = drugFind.DrugName; PHARMACY_DISPENSE messageDispensed = null; messageDispensed = drugDispensed.PHARMACY_DISPENSE.FirstOrDefault(x => drugname.Contains(x.ACTUAL_DRUGS)); if (messageDispensed != null) { drug.DispensedQuantity = messageDispensed.QUANTITY_DISPENSED; //todo get frequencyId //drug.FrequencyID = messageDispensed.FREQUENCY; drug.PatientInstructions = messageDispensed.DISPENSING_NOTES; //todo get strength ids //drug.StrengthID = messageDispensed.STRENGTH; drug.Duration = messageDispensed.DURATION; drug.UpdateDate = DateTime.Now; drug.UserID = InteropUser.UserId; //drug.SingleDose = Convert.ToDecimal(Regex.Replace(messageDispensed.DOSAGE, @"^[A-Za-z]+", "")); } try { _pharmacyDispenseManager.UpdatePatientPharmacyDispense(drug); } catch (Exception e) { Console.WriteLine(e); return(Msg = "error " + e.Message); } } } else { PatientPharmacyDispense newlyDispensedDrugs = new PatientPharmacyDispense(); foreach (var drug in drugDispensed.PHARMACY_DISPENSE) { string drugNameQuery = "%" + drug.ACTUAL_DRUGS + "%"; var drugFind = _drugManager.GetDrugsByName(drugNameQuery).FirstOrDefault(); newlyDispensedDrugs.Drug_Pk = drugFind.Drug_pk; newlyDispensedDrugs.DispensedQuantity = drug.QUANTITY_DISPENSED; newlyDispensedDrugs.Duration = drug.DURATION; //todo get frequencyId //newlyDispensedDrugs.FrequencyID = drug.FREQUENCY; //newlyDispensedDrugs.SingleDose = Convert.ToDecimal(Regex.Replace(drug.DOSAGE, @"^[A-Za-z]+", "")); //todo get strength ids //newlyDispensedDrugs.StrengthID = drug.STRENGTH; newlyDispensedDrugs.PatientInstructions = drug.DISPENSING_NOTES; newlyDispensedDrugs.Prophylaxis = 0; } foreach (var order in drugDispensed.PHARMACY_ENCODED_ORDER) { newlyDispensedDrugs.OrderedQuantity = Convert.ToInt32(order.QUANTITY_PRESCRIBED); newlyDispensedDrugs.PatientInstructions = order.PRESCRIPTION_NOTES; } try { _pharmacyDispenseManager.AddPatientPharmacyDispense(newlyDispensedDrugs); } catch (Exception e) { Console.WriteLine(e); return(Msg = "error " + e.Message); } } var updatedPharmacyOrder = pharmacyOrder; var orderingPhysician = drugDispensed.COMMON_ORDER_DETAILS.ORDERING_PHYSICIAN; updatedPharmacyOrder.OrderedByName = orderingPhysician.PREFIX + " " + orderingPhysician.FIRST_NAME + " " + orderingPhysician.LAST_NAME; //todo harmonise users updatedPharmacyOrder.DispensedBy = InteropUser.UserId; updatedPharmacyOrder.DispensedByDate = drugDispensed.MESSAGE_HEADER.MESSAGE_DATETIME; updatedPharmacyOrder.OrderStatus = 2; string str = updatedPharmacyOrder.PharmacyNotes; if (str != null) { str += " Dispensed from IL"; } updatedPharmacyOrder.PharmacyNotes = str; try { _pharmacyOrderManager.UpdatePharmacyOrder(updatedPharmacyOrder); } catch (Exception e) { Console.WriteLine(e); return(Msg = "error " + e.Message); } Msg = "Success"; } catch (Exception e) { Console.WriteLine(e); Msg = "error " + e.Message; } return(Msg); }
//int _userId = Convert.ToInt32(HttpContext.Current.Session["AppUserId"]); //int _facilityId = Convert.ToInt32(HttpContext.Current.Session["AppLocationId"]); public string Save(ViralLoadResultsDto viralLoadResults) { LabOrderEntity labOrder = null; List <LabDetailsEntity> labDetails = null; var results = viralLoadResults.ViralLoadResult; if (results != null) { try { var patientLookup = new PatientLookupManager(); var labOrderManager = new PatientLabOrderManager(); var patientCcc = viralLoadResults.PatientIdentification.INTERNAL_PATIENT_ID.FirstOrDefault(n => n.IdentifierType == "CCC_NUMBER").IdentifierValue; var patient = patientLookup.GetPatientByCccNumber(patientCcc); string receivingFacilityMFLCode = viralLoadResults.MesssageHeader.ReceivingFacility; LookupLogic flm = new LookupLogic(); LookupFacility thisFacility = flm.GetFacility(receivingFacilityMFLCode); int interopUserId = InteropUser.UserId; if (thisFacility == null) { Msg = $"The facility {receivingFacilityMFLCode} does not exist"; throw new Exception(Msg); } if (patient == null) { Msg = $"Patient {patientCcc} does not exist "; throw new Exception(Msg); } if (results.Count(r => string.IsNullOrWhiteSpace(r.VlResult.Trim())) > 0) { Msg = $"Viral load message has no results indicated "; throw new Exception(Msg); } int invalidResult = 0; foreach (var result in results) { if (result.VlResult.Contains("LDL")) { } else if (Regex.Split(result.VlResult, @"[^0-9\.]+").Length > 0) { } else { invalidResult++; } } if (invalidResult > 0) { Msg = $"Viral load message has invalid results indicated "; throw new Exception(Msg); } if (patient != null && thisFacility != null) { //todo brian check labOrder = labOrderManager.GetPatientLabOrdersByDate((int)patient.ptn_pk, results.FirstOrDefault().DateSampleCollected).DefaultIfEmpty(null).FirstOrDefault(); DateTime sampleCollectionDate = results.FirstOrDefault().DateSampleCollected; if (labOrder == null) { var patientMasterVisitManager = new PatientMasterVisitManager(); //var visitType = flm.GetItemIdByGroupAndItemName("VisitType", "Enrollment")[0] // .ItemId; int patientMasterVisitId = patientMasterVisitManager.AddPatientMasterVisit(patient.Id, interopUserId, 316); var listOfTestsOrdered = new List <ListLabOrder>(); var order = new ListLabOrder() { FacilityId = Convert.ToInt32(viralLoadResults.MesssageHeader.ReceivingFacility), LabName = "Viral Load",// results.FirstOrDefault().LabTestedIn, LabNameId = 3, LabNotes = results.FirstOrDefault().Regimen + " " + results.FirstOrDefault().SampleType, LabOrderDate = sampleCollectionDate, LabOrderId = 0, OrderReason = "", Results = results.FirstOrDefault().VlResult, VisitId = patientMasterVisitId, ResultDate = viralLoadResults.MesssageHeader.MessageDatetime }; listOfTestsOrdered.Add(order); var jss = new JavaScriptSerializer(); string patientLabOrder = jss.Serialize(listOfTestsOrdered); //include userid and facility ID int orderId = labOrderManager.savePatientLabOrder(patient.Id, (int)patient.ptn_pk, interopUserId, thisFacility.FacilityID, 203, patientMasterVisitId, sampleCollectionDate.ToString(), "IL lab order", patientLabOrder, "Complete"); labOrder = labOrderManager.GetLabOrdersById(orderId); labDetails = labOrderManager.GetLabTestsOrderedById(labOrder.Id); } else { labDetails = labOrderManager.GetLabTestsOrderedById(labOrder.Id); } if (labOrder != null) { bool isUndetectable = false; string resultText = ""; decimal?resultValue = null; foreach (var result in results) { if (result.VlResult.Contains("LDL")) { isUndetectable = true; resultText = result.VlResult; } else { var resultString = result.VlResult.Replace("copies/ml", ""); string[] numbers = Regex.Split(resultString, @"[^0-9\.]+"); //bool isSuccess = decimal.TryParse(resultString, out decimalValue); //if (isSuccess) resultValue = decimalValue; for (int i = 0; i < numbers.Length; i++) { if (Regex.IsMatch(numbers[i], @"^\d+$")) { resultValue = Convert.ToDecimal(numbers[i]); break; } } } if (labOrder != null) { var labResults = new LabResultsEntity() { //todo remove hard coding LabOrderId = labOrder.Id, LabOrderTestId = labDetails.FirstOrDefault().Id, ParameterId = 3, LabTestId = 3, ResultText = resultText, ResultValue = resultValue, ResultUnit = "copies/ml", ResultUnitId = 129, Undetectable = isUndetectable, StatusDate = result.DateSampleTested, HasResult = true }; labOrderManager.AddPatientLabResults(labResults); labOrder.OrderStatus = "Complete"; labOrderManager.savePatientLabOrder(labOrder); } } Msg = "Success"; } } else { Msg = "Patient does not exist"; return(Msg); } } catch (Exception e) { Msg = "error " + e.Message; throw e; } } else { Msg = "Message does not contain results"; throw new Exception(Msg); } return(Msg); }