Example #1
0
        ///<summary>Called by eClipboard check-in once an appointment has been moved to the waiting room and the patient is ready to fill out forms.
        ///Returns number of new sheets created and inserted into Sheet table.</summary>
        public static int CreateSheetsForCheckIn(Appointment appt)
        {
            if (RemotingClient.RemotingRole == RemotingRole.ClientWeb)
            {
                return(Meth.GetInt(MethodBase.GetCurrentMethod(), appt));
            }
            if (!MobileAppDevices.IsClinicSignedUpForEClipboard(PrefC.HasClinicsEnabled?appt.ClinicNum:0))              //this clinic isn't signed up for this feature
            {
                return(0);
            }
            if (!ClinicPrefs.GetBool(PrefName.EClipboardCreateMissingFormsOnCheckIn, appt.ClinicNum))             //This feature is turned off
            {
                return(0);
            }
            bool useDefault = ClinicPrefs.GetBool(PrefName.EClipboardUseDefaults, appt.ClinicNum);
            List <EClipboardSheetDef> listSheetsToCreate = EClipboardSheetDefs.GetForClinic(useDefault ? 0 : appt.ClinicNum);

            if (listSheetsToCreate.Count == 0)            //There aren't any sheets to create here
            {
                return(0);
            }
            List <Sheet> listAlreadyCompleted  = Sheets.GetForPatient(appt.PatNum);
            List <Sheet> listAlreadyInTerminal = Sheets.GetForTerminal(appt.PatNum);

            //if we already have sheets queued for the patient don't add duplicates
            if (listAlreadyInTerminal.Count > 0)
            {
                listAlreadyCompleted.RemoveAll(x => listAlreadyInTerminal.Select(y => y.SheetNum).Contains(x.SheetNum));
                listSheetsToCreate.RemoveAll(x => listAlreadyInTerminal.Select(y => y.SheetDefNum).Contains(x.SheetDefNum));
            }
            byte         showInTerminal = GetBiggestShowInTerminal(appt.PatNum);
            List <Sheet> listNewSheets  = new List <Sheet>();

            foreach (EClipboardSheetDef sheetInsert in listSheetsToCreate.OrderBy(x => x.ItemOrder))
            {
                //First check if we've already completed this form against our resubmission interval rules
                DateTime lastCompleted = listAlreadyCompleted
                                         .Where(x => x.SheetDefNum == sheetInsert.SheetDefNum)
                                         .OrderBy(x => x.DateTimeSheet)
                                         .LastOrDefault()?.DateTimeSheet ?? DateTime.MinValue;
                if (lastCompleted > DateTime.MinValue)
                {
                    if (sheetInsert.ResubmitInterval.Days == 0)
                    {
                        continue;                         //If this interval is set to 0 and they've already completed this form once, we never want to create it automatically again
                    }
                    int elapsed = (DateTime.Today - lastCompleted.Date).Days;
                    if (elapsed < sheetInsert.ResubmitInterval.Days)
                    {
                        continue;                         //The interval hasn't elapsed yet so we don't want to create this sheet
                    }
                }
                SheetDef def      = SheetDefs.GetSheetDef(sheetInsert.SheetDefNum);
                Sheet    newSheet = CreateSheetFromSheetDef(def, appt.PatNum);
                SheetParameter.SetParameter(newSheet, "PatNum", appt.PatNum);
                SheetFiller.FillFields(newSheet);
                //Counting starts at 1 in this case and we don't want to ovewrite the previous number so increment first
                newSheet.ShowInTerminal = ++showInTerminal;
                listNewSheets.Add(newSheet);
            }
            SaveNewSheetList(listNewSheets);
            return(listNewSheets.Count);
        }