///<summary>Returns true if the import was successful. Imports either a webform or a sheet that was transferred using the CEMT tool. ///Tries to find a matching patient using LName,FName,and DOB. If no matching patient is found automatically, it will allow the user to either ///create a new patient, select an existing patient,delete, or skip the sheet. Call using a try/catch.</summary> private static bool DidImportSheet(WebForms_Sheet webFormsSheet, Sheet sheet, List <WebForms_Sheet> listWebSheets, List <Sheet> listSheets, string cultureName, ref List <long> listSheetIdsForDeletion) { bool isWebForms = webFormsSheet != null && listWebSheets != null; long patNum = 0; string lName = ""; string fName = ""; List <string> listPhoneNumbers = new List <string>(); string email = ""; DateTime bDate = DateTime.MinValue; if (isWebForms) { WebForms_Sheets.ParseWebFormSheet(webFormsSheet, cultureName, out lName, out fName, out bDate, out listPhoneNumbers, out email); } else { Sheets.ParseTransferSheet(sheet, out lName, out fName, out bDate, out listPhoneNumbers, out email); } List <long> listMatchingPats = Patients.GetPatNumsByNameBirthdayEmailAndPhone(lName, fName, bDate, email, listPhoneNumbers); Patient pat = null; if (listMatchingPats.IsNullOrEmpty() || listMatchingPats.Count > 1) //0 or > 1 { List <long> listWebSheetNumsForPat = new List <long>(); if (isWebForms) { List <long> listSheetsToDelete = listSheetIdsForDeletion; listWebSheetNumsForPat = WebForms_Sheets.FindSheetsForPat(webFormsSheet, listWebSheets, cultureName) //Only include web sheets that have not already been imported .Where(x => !listSheetsToDelete.Contains(x)).ToList(); } else //Cemt Import { listWebSheetNumsForPat = Sheets.FindSheetsForPat(sheet, listSheets); } FormPatientPickWebForm FormPpw = new FormPatientPickWebForm(webFormsSheet, listWebSheetNumsForPat.Count, sheet); FormPpw.LnameEntered = lName; FormPpw.FnameEntered = fName; FormPpw.BdateEntered = bDate; FormPpw.HasMoreThanOneMatch = (listMatchingPats.Count > 1); FormPpw.ShowDialog(); if (FormPpw.DialogResult == DialogResult.Cancel) { if (isWebForms) { //user wants to stop importing altogether //we will pick up where we left off here next time WebForms_Sheets.DeleteSheetData(listSheetIdsForDeletion.Distinct().ToList()); } return(false); // only false when user wants to stop importing } else if (FormPpw.DialogResult == DialogResult.Ignore) { if (FormPpw.IsDiscardAll) { //user wants to delete all webforms for this patient. Mark them for deletion. listSheetIdsForDeletion.AddRange(listWebSheetNumsForPat); } return(true); //continue on to the next one } patNum = FormPpw.SelectedPatNum; //might be zero to indicate new patient } else //Exactly one match was found so make a log entry what the match was. { patNum = listMatchingPats[0]; pat = Patients.GetPat(patNum); //Security log for OD automatically importing a sheet into a patient. string logText; if (isWebForms) { logText = Lan.g("FormWebForms", "Web form import from:"); } else { logText = Lan.g("FormWebForms", "CEMT patient transfer import from:"); } logText += " " + lName + ", " + fName + " " + bDate.ToShortDateString() + "\r\n" + Lan.g("FormWebForms", "Auto imported into:") + " " + pat.LName + ", " + pat.FName + " " + pat.Birthdate.ToShortDateString(); SecurityLogs.MakeLogEntry(Permissions.SheetEdit, patNum, logText); } if (patNum == 0) { pat = CreatePatient(lName, fName, bDate, webFormsSheet, sheet, cultureName); patNum = pat.PatNum; //Security log for user creating a new patient. string logText; if (isWebForms) { logText = Lan.g("FormWebForms", "Web form import from:"); } else { logText = Lan.g("FormWebForms", "CEMT patient transfer import from:"); } logText += " " + lName + ", " + fName + " " + bDate.ToShortDateString() + "\r\n" + Lan.g("FormWebForms", "User created new pat:") + " " + pat.LName + ", " + pat.FName + " " + pat.Birthdate.ToShortDateString(); SecurityLogs.MakeLogEntry(Permissions.SheetEdit, patNum, logText); } else if (pat == null) { pat = Patients.GetPat(patNum); } //We should probably make a security log entry for a manually selected patient. if (isWebForms) { Sheet newSheet = SheetUtil.CreateSheetFromWebSheet(patNum, webFormsSheet); Sheets.SaveNewSheet(newSheet); if (DataExistsInDb(newSheet)) { listSheetIdsForDeletion.Add(webFormsSheet.SheetID); } } else //CEMT Patient Transfer //Sheet is ready to get updated with the patient. { sheet.PatNum = patNum; sheet.DateTimeSheet = MiscData.GetNowDateTime(); if (PrefC.HasClinicsEnabled) { sheet.ClinicNum = pat.ClinicNum; } sheet.IsWebForm = true; //This is so the sheet shows up in gridmain in this form. Sheets.Update(sheet); } return(true); }
public static string RetrieveAndSaveData(string cultureName) { string strMsg = ""; #region CEMT Patient Transfers //Now get the patients that were transferred from the CEMT tool. These sheets will have a PatNum=0; List <Sheet> listSheetsFromCemtTool = Sheets.GetTransferSheets(); List <long> listCemtSheetsToDelete = new List <long>(); foreach (Sheet cemtSheet in listSheetsFromCemtTool) { try { //continue if the SheetNum is either marked as skipped or is going to be deleted. if (cemtSheet.SheetNum.In(listCemtSheetsToDelete)) { continue; } if (!DidImportSheet(null, cemtSheet, null, listSheetsFromCemtTool, System.Globalization.CultureInfo.CurrentCulture.Name, ref listCemtSheetsToDelete)) { //user wants to cancel import. return(strMsg); } } catch (Exception e) { strMsg += e.Message + "\r\n"; } } foreach (long sheetNum in listCemtSheetsToDelete) { Sheets.Delete(sheetNum); //Does not delete the sheet but sets the Sheet.IsDeleted=true. } #endregion #region WebForms try { SheetsSynchProxy.TimeoutOverride = 300000; //5 minutes. Default is 100000 (1.66667 minutes). if (WebUtils.GetDentalOfficeID() == 0) { strMsg = Lan.g("FormWebForms", "Either the registration key provided by the dental office is incorrect or the Host Server Address cannot be found."); return(strMsg); } List <WebForms_Sheet> listWebFormSheets; List <long> listSheetsSeen = new List <long>(); int iterations = 0; do { if (WebForms_Sheets.TryGetSheets(out listWebFormSheets)) { //if we are not in HQ, filter out non-HQ sheets that don't match our current clinic listWebFormSheets = listWebFormSheets.FindAll(x => x.ClinicNum == 0 || Clinics.ClinicNum == 0 || x.ClinicNum == Clinics.ClinicNum); } iterations++; List <long> listSheetIdsForDeletion = new List <long>(); listWebFormSheets.RemoveAll(x => listSheetsSeen.Contains(x.SheetID)); //Remove all sheets that we've already seen. if (listWebFormSheets.Count == 0) { if (iterations == 1 && listSheetsFromCemtTool.Count == 0) { strMsg = Lan.g("FormWebForms", "No Patient forms retrieved from server"); } else { strMsg = Lan.g("FormWebForms", "All Patient forms retrieved from server"); } return(strMsg); } listSheetsSeen.AddRange(listWebFormSheets.Select(x => x.SheetID)); //loop through all incoming sheets foreach (WebForms_Sheet webFormsSheet in listWebFormSheets) { try { //this try catch is put so that a defective downloaded sheet does not stop other sheets from being downloaded. if (listSheetIdsForDeletion.Contains(webFormsSheet.SheetID)) { continue; //Marked for deletion already. Next. } if (!DidImportSheet(webFormsSheet, null, listWebFormSheets, null, cultureName, ref listSheetIdsForDeletion)) { //user wants to cancel import. return(strMsg); } } catch (Exception e) { strMsg += e.Message + "\r\n"; } } // end of for loop if (!WebForms_Sheets.DeleteSheetData(listSheetIdsForDeletion.Distinct().ToList())) { break; //If any error happens, stop retrieving forms. } } while(listWebFormSheets.Count > 0); } catch (Exception e) { strMsg += e.Message; return(strMsg); } #endregion return(strMsg); }