///<summary>Cascading delete that deletes all MedLab, MedLabResult, MedLabSpecimen, and MedLabFacAttach. ///Also deletes any embedded PDFs that are linked to by the MedLabResults. ///The MedLabs and all associated results, specimens, and FacAttaches referenced by the MedLabNums in listExcludeMedLabNums will not be deleted. ///Used for deleting old entries and keeping new ones. The list may be empty and then all will be deleted.</summary> public static int DeleteLabsAndResults(MedLab medLab, List <long> listExcludeMedLabNums = null) { if (RemotingClient.RemotingRole == RemotingRole.ClientWeb) { return(Meth.GetInt(MethodBase.GetCurrentMethod(), medLab, listExcludeMedLabNums)); } List <MedLab> listLabsOld = MedLabs.GetForPatAndSpecimen(medLab.PatNum, medLab.SpecimenID, medLab.SpecimenIDFiller); //patNum could be 0 if (listExcludeMedLabNums != null) { listLabsOld = listLabsOld.FindAll(x => !listExcludeMedLabNums.Contains(x.MedLabNum)); } if (listLabsOld.Count < 1) { return(0); } int failedCount = 0; List <long> listLabNumsOld = listLabsOld.Select(x => x.MedLabNum).ToList(); List <MedLabResult> listResultsOld = listLabsOld.SelectMany(x => x.ListMedLabResults).ToList(); //sends one query to the db per MedLab MedLabFacAttaches.DeleteAllForLabsOrResults(listLabNumsOld, listResultsOld.Select(x => x.MedLabResultNum).ToList()); MedLabSpecimens.DeleteAllForLabs(listLabNumsOld); //MedLabSpecimens have a FK to MedLabNum MedLabResults.DeleteAllForMedLabs(listLabNumsOld); //MedLabResults have a FK to MedLabNum MedLabs.DeleteAll(listLabNumsOld); foreach (Document doc in Documents.GetByNums(listResultsOld.Select(x => x.DocNum).ToList())) { Patient docPat = Patients.GetPat(doc.PatNum); if (docPat == null) { Documents.Delete(doc); continue; } try { ImageStore.DeleteDocuments(new List <Document> { doc }, ImageStore.GetPatientFolder(docPat, ImageStore.GetPreferredAtoZpath())); } catch (Exception ex) { ex.DoNothing(); //To avoid a warning message. The ex is needed to ensure all exceptions are caught. failedCount++; } } return(failedCount); }
///<summary>Returns a list of MedLabFacilityNums, the order in the list will be the facility ID on the report. Basically a local re-numbering. ///Each message has a facility or facilities with footnote IDs, e.g. 01, 02, etc. The results each link to the facility that performed the test. ///But if there are multiple messages for a test order, e.g. when there is a final result for a subset of the original test results, ///the additional message may have a facility with footnote ID of 01 that is different than the original message facility with ID 01. ///So each ID could link to multiple facilities. We will re-number the facilities so that each will have a unique number for this report.</summary> public static List <MedLabFacility> GetFacilityList(List <MedLab> listMedLabs, out List <MedLabResult> listResults) { //No need to check RemotingRole; no call to db. listResults = listMedLabs.SelectMany(x => x.ListMedLabResults).ToList(); for (int i = listResults.Count - 1; i > -1; i--) //loop through backward and only keep the most final/most recent result { if (i == 0) { break; } if (listResults[i].ObsID == listResults[i - 1].ObsID && listResults[i].ObsIDSub == listResults[i - 1].ObsIDSub) { listResults.RemoveAt(i); } } listResults.OrderBy(x => x.MedLabNum).ThenBy(x => x.MedLabResultNum); //listResults will now only contain the most recent or most final/corrected results, sorted by the order inserted in the db List <MedLabFacAttach> listFacAttaches = MedLabFacAttaches.GetAllForResults(listResults.Select(x => x.MedLabResultNum).Distinct().ToList()); Dictionary <long, long> dictResultNumFacNum = listFacAttaches.ToDictionary(x => x.MedLabResultNum, x => x.MedLabFacilityNum); List <MedLabFacility> listFacilities = new List <MedLabFacility>(); for (int i = 0; i < listResults.Count; i++) { if (!dictResultNumFacNum.ContainsKey(listResults[i].MedLabResultNum)) { continue; } long facilityNumCur = dictResultNumFacNum[listResults[i].MedLabResultNum]; if (!listFacilities.Exists(x => x.MedLabFacilityNum == facilityNumCur)) { listFacilities.Add(MedLabFacilities.GetOne(facilityNumCur)); } listResults[i].FacilityID = (listFacilities.Select(x => x.MedLabFacilityNum).ToList().IndexOf(facilityNumCur) + 1).ToString().PadLeft(2, '0'); } return(listFacilities); }