Example #1
0
        private void FillGrid()
        {
            if (textDateStart.errorProvider1.GetError(textDateStart) != "" ||
                textDateEnd.errorProvider1.GetError(textDateEnd) != "")
            {
                return;
            }
            if (SelectedPatNum > 0)
            {
                Patient pat = Patients.GetLim(SelectedPatNum);
                textPatient.Text = pat.GetNameLF();
            }
            else
            {
                textPatient.Text = "";
            }
            MsgList = HL7Msgs.GetHL7Msgs(PIn.Date(textDateStart.Text), PIn.Date(textDateEnd.Text), SelectedPatNum, comboHL7Status.SelectedIndex);
            gridMain.BeginUpdate();
            gridMain.Columns.Clear();
            ODGridColumn col = new ODGridColumn(Lan.g(this, "DateTime"), 180);

            gridMain.Columns.Add(col);
            col = new ODGridColumn(Lan.g(this, "Patient"), 170);
            gridMain.Columns.Add(col);
            col = new ODGridColumn(Lan.g(this, "AptNum"), 60);
            gridMain.Columns.Add(col);
            col = new ODGridColumn(Lan.g(this, "Status"), 75);
            gridMain.Columns.Add(col);
            col = new ODGridColumn(Lan.g(this, "Note"), 400);
            gridMain.Columns.Add(col);
            gridMain.Rows.Clear();
            if (MsgList != null)
            {
                for (int i = 0; i < MsgList.Count; i++)
                {
                    ODGridRow row = new ODGridRow();
                    row.Cells.Add(MsgList[i].DateTStamp.ToString());
                    if (MsgList[i].PatNum > 0)
                    {
                        row.Cells.Add(Patients.GetLim(MsgList[i].PatNum).GetNameFL());
                    }
                    else
                    {
                        row.Cells.Add("");
                    }
                    if (MsgList[i].AptNum > 0)
                    {
                        row.Cells.Add(MsgList[i].AptNum.ToString());
                    }
                    else
                    {
                        row.Cells.Add("");
                    }
                    row.Cells.Add(Enum.GetName(typeof(HL7MessageStatus), MsgList[i].HL7Status));
                    row.Cells.Add(MsgList[i].Note);
                    gridMain.Rows.Add(row);
                }
            }
            gridMain.EndUpdate();
        }
Example #2
0
        //OD accepts commandline arguments from eCW.  That's handled in FormOpenDental.

        //public static void SendHL7(Appointment apt,Patient pat) {
        //  OpenDentBusiness.HL7.DFT dft=new OpenDentBusiness.HL7.DFT(apt,pat);
        //  HL7Msg msg=new HL7Msg();
        //  msg.AptNum=apt.AptNum;
        //  msg.HL7Status=HL7MessageStatus.OutPending;//it will be marked outSent by the HL7 service.
        //  msg.MsgText=dft.GenerateMessage();
        //  HL7Msgs.Insert(msg);
        //}

        public static void SendHL7(long aptNum, long provNum, Patient pat, string pdfDataBase64, string pdfDescription, bool justPDF, List <Procedure> listProcs)
        {
            OpenDentBusiness.HL7.EcwDFT dft = new OpenDentBusiness.HL7.EcwDFT();
            dft.InitializeEcw(aptNum, provNum, pat, pdfDataBase64, pdfDescription, justPDF, listProcs);
            HL7Msg msg = new HL7Msg();

            if (justPDF)
            {
                msg.AptNum = 0;              //Prevents the appt complete button from changing to the "Revise" button prematurely.
            }
            else
            {
                msg.AptNum = aptNum;
            }
            msg.HL7Status = HL7MessageStatus.OutPending;          //it will be marked outSent by the HL7 service.
            msg.MsgText   = dft.GenerateMessage();
            msg.PatNum    = pat.PatNum;
            HL7ProcAttach hl7ProcAttach = new HL7ProcAttach();

            hl7ProcAttach.HL7MsgNum = HL7Msgs.Insert(msg);
            if (listProcs != null)
            {
                foreach (Procedure proc in listProcs)
                {
                    hl7ProcAttach.ProcNum = proc.ProcNum;
                    HL7ProcAttaches.Insert(hl7ProcAttach);
                }
            }
        }
Example #3
0
        private void gridMain_CellDoubleClick(object sender, UI.ODGridClickEventArgs e)
        {
            FormHL7MsgEdit FormS = new FormHL7MsgEdit();

            FormS.MsgCur = HL7Msgs.GetOne(MsgList[e.Row].HL7MsgNum);
            FormS.ShowDialog();
            FillGrid();
        }
Example #4
0
 private void EcwOldTimerCallbackSendFunction(Object stateInfo)
 {
     //does not happen for standalone
     if (_ecwOldIsSending)             //if there is a thread that is still sending, return
     {
         return;
     }
     try {
         _ecwOldIsSending = true;
         if (IsVerboseLogging)
         {
             EventLog.WriteEntry("GetOnePending Start");
         }
         List <HL7Msg> list = HL7Msgs.GetOnePending();
         if (IsVerboseLogging)
         {
             EventLog.WriteEntry("GetOnePending Finished");
         }
         string filename;
         for (int i = 0; i < list.Count; i++)           //Right now, there will only be 0 or 1 item in the list.
         {
             if (list[i].AptNum == 0)
             {
                 filename = ODFileUtils.CreateRandomFile(ecwOldHl7FolderIn, ".txt");
             }
             else
             {
                 filename = Path.Combine(ecwOldHl7FolderIn, list[i].AptNum.ToString() + ".txt");
             }
             //EventLog.WriteEntry("Attempting to create file: "+filename);
             File.WriteAllText(filename, list[i].MsgText);
             list[i].HL7Status = HL7MessageStatus.OutSent;
             HL7Msgs.Update(list[i]);                    //set the status to sent.
         }
         if (_ecwOldDateTimeOldMsgsDeleted.Date < DateTime.Now.Date)
         {
             if (IsVerboseLogging)
             {
                 EventLog.WriteEntry("DeleteOldMsgText Starting");
             }
             _ecwOldDateTimeOldMsgsDeleted = DateTime.Now;
             HL7Msgs.DeleteOldMsgText();                    //this function deletes if DateTStamp is less than CURDATE-INTERVAL 4 MONTH.  That means it will delete message text only once a day, not time based.
             if (IsVerboseLogging)
             {
                 EventLog.WriteEntry("DeleteOldMsgText Finished");
             }
         }
     }
     catch (Exception ex) {
         EventLog.WriteEntry("OpenDentHL7 error when sending HL7 message: " + ex.Message, EventLogEntryType.Warning);             //Warning because the service will spawn a new thread in 1.8 seconds
     }
     finally {
         _ecwOldIsSending = false;
     }
 }
Example #5
0
        /// <summary>Helper method to send given appt to the unscheduled list.
        /// Creates SecurityLogs and considers HL7.</summary>
        public static void SetApptUnschedHelper(Appointment appt, Patient pat = null, bool doFireApptEvent = true)
        {
            DateTime datePrevious = appt.DateTStamp;

            Appointments.SetAptStatus(appt, ApptStatus.UnschedList); //Appointments S-Class handles Signalods
            #region SecurityLogs
            if (appt.AptStatus != ApptStatus.Complete)               //seperate log entry for editing completed appts.
            {
                SecurityLogs.MakeLogEntry(Permissions.AppointmentMove, appt.PatNum,
                                          appt.ProcDescript + ", " + appt.AptDateTime.ToString() + ", Sent to Unscheduled List",
                                          appt.AptNum, datePrevious);
            }
            else
            {
                SecurityLogs.MakeLogEntry(Permissions.AppointmentCompleteEdit, appt.PatNum,
                                          appt.ProcDescript + ", " + appt.AptDateTime.ToString() + ", Sent to Unscheduled List",
                                          appt.AptNum, datePrevious);
            }
            #endregion
            #region HL7
            //If there is an existing HL7 def enabled, send a SIU message if there is an outbound SIU message defined
            if (HL7Defs.IsExistingHL7Enabled())
            {
                if (pat == null)
                {
                    pat = Patients.GetPat(appt.PatNum);
                }
                //S15 - Appt Cancellation event
                MessageHL7 messageHL7 = MessageConstructor.GenerateSIU(pat, Patients.GetPat(pat.Guarantor), EventTypeHL7.S15, appt);
                //Will be null if there is no outbound SIU message defined, so do nothing
                if (messageHL7 != null)
                {
                    HL7Msg hl7Msg = new HL7Msg();
                    hl7Msg.AptNum    = appt.AptNum;
                    hl7Msg.HL7Status = HL7MessageStatus.OutPending;                  //it will be marked outSent by the HL7 service.
                    hl7Msg.MsgText   = messageHL7.ToString();
                    hl7Msg.PatNum    = pat.PatNum;
                    HL7Msgs.Insert(hl7Msg);
#if DEBUG
                    MessageBox.Show("Appointments", messageHL7.ToString());
#endif
                }
            }
            #endregion
            if (doFireApptEvent)
            {
                AppointmentEvent.Fire(ODEventType.AppointmentEdited, appt);
            }
            Recalls.SynchScheduledApptFull(appt.PatNum);
        }
Example #6
0
        private void TimerCallbackSendFiles(Object stateInfo)
        {
            List <HL7Msg> list = HL7Msgs.GetOnePending();
            string        filename;

            for (int i = 0; i < list.Count; i++)       //Right now, there will only be 0 or 1 item in the list.
            {
                filename = ODFileUtils.CreateRandomFile(hl7FolderOut, ".txt");
                File.WriteAllText(filename, list[i].MsgText);
                list[i].HL7Status = HL7MessageStatus.OutSent;
                HL7Msgs.Update(list[i]);                //set the status to sent.
                HL7Msgs.DeleteOldMsgText();             //This is inside the loop so that it happens less frequently.  To clean up incoming messages, we may move this someday.
            }
        }
Example #7
0
File: ECW.cs Project: nampn/ODental
        //OD accepts commandline arguments from eCW.  That's handled in FormOpenDental.

        //public static void SendHL7(Appointment apt,Patient pat) {
        //  OpenDentBusiness.HL7.DFT dft=new OpenDentBusiness.HL7.DFT(apt,pat);
        //  HL7Msg msg=new HL7Msg();
        //  msg.AptNum=apt.AptNum;
        //  msg.HL7Status=HL7MessageStatus.OutPending;//it will be marked outSent by the HL7 service.
        //  msg.MsgText=dft.GenerateMessage();
        //  HL7Msgs.Insert(msg);
        //}

        public static void SendHL7(long aptNum, long provNum, Patient pat, string pdfDataBase64, string pdfDescription, bool justPDF)
        {
            OpenDentBusiness.HL7.DFT dft = new OpenDentBusiness.HL7.DFT();
            dft.InitializeEcw(aptNum, provNum, pat, pdfDataBase64, pdfDescription, justPDF);
            HL7Msg msg = new HL7Msg();

            if (justPDF)
            {
                msg.AptNum = 0;              //Prevents the appt complete button from changing to the "Revise" button prematurely.
            }
            else
            {
                msg.AptNum = aptNum;
            }
            msg.HL7Status = HL7MessageStatus.OutPending;          //it will be marked outSent by the HL7 service.
            msg.MsgText   = dft.GenerateMessage();
            HL7Msgs.Insert(msg);
        }
Example #8
0
        private void TimerCallbackSendFunction(Object stateInfo)
        {
            //does not happen for standalone
            List <HL7Msg> list = HL7Msgs.GetOnePending();
            string        filename;

            for (int i = 0; i < list.Count; i++)
            {
                if (list[i].AptNum == 0)
                {
                    filename = ODFileUtils.CreateRandomFile(hl7FolderIn, ".txt");
                }
                else
                {
                    filename = Path.Combine(hl7FolderIn, list[i].AptNum.ToString() + ".txt");
                }
                //EventLog.WriteEntry("Attempting to create file: "+filename);
                File.WriteAllText(filename, list[i].MsgText);
                list[i].HL7Status = HL7MessageStatus.OutSent;
                HL7Msgs.Update(list[i]);                //set the status to sent.
            }
        }
Example #9
0
        private void EcwOldTimerCallbackSendFunction(Object stateInfo)
        {
            //does not happen for standalone
            List <HL7Msg> list = HL7Msgs.GetOnePending();
            string        filename;

            for (int i = 0; i < list.Count; i++)       //Right now, there will only be 0 or 1 item in the list.
            {
                if (list[i].AptNum == 0)
                {
                    filename = ODFileUtils.CreateRandomFile(ecwOldHl7FolderIn, ".txt");
                }
                else
                {
                    filename = Path.Combine(ecwOldHl7FolderIn, list[i].AptNum.ToString() + ".txt");
                }
                //EventLog.WriteEntry("Attempting to create file: "+filename);
                File.WriteAllText(filename, list[i].MsgText);
                list[i].HL7Status = HL7MessageStatus.OutSent;
                HL7Msgs.Update(list[i]);                //set the status to sent.
                HL7Msgs.DeleteOldMsgText();             //This is inside the loop so that it happens less frequently.  To clean up incoming messages, we may move this someday.
            }
        }
Example #10
0
        ///<summary>Sets given appt.AptStatus to broken.
        ///Provide procCode that should be charted, can be null but will not chart a broken procedure.
        ///Also considers various broken procedure based prefs.
        ///Makes its own securitylog entries.</summary>
        public static void BreakApptHelper(Appointment appt, Patient pat, ProcedureCode procCode)
        {
            //suppressHistory is true due to below logic creating a log with a specific HistAppointmentAction instead of the generic changed.
            DateTime datePrevious    = appt.DateTStamp;
            bool     suppressHistory = false;

            if (procCode != null)
            {
                suppressHistory = (procCode.ProcCode.In("D9986", "D9987"));
            }
            Appointments.SetAptStatus(appt, ApptStatus.Broken, suppressHistory); //Appointments S-Class handles Signalods
            if (appt.AptStatus != ApptStatus.Complete)                           //seperate log entry for completed appointments.
            {
                SecurityLogs.MakeLogEntry(Permissions.AppointmentEdit, pat.PatNum,
                                          appt.ProcDescript + ", " + appt.AptDateTime.ToString()
                                          + ", Broken from the Appts module.", appt.AptNum, datePrevious);
            }
            else
            {
                SecurityLogs.MakeLogEntry(Permissions.AppointmentCompleteEdit, pat.PatNum,
                                          appt.ProcDescript + ", " + appt.AptDateTime.ToString()
                                          + ", Broken from the Appts module.", appt.AptNum, datePrevious);
            }
            #region HL7
            //If there is an existing HL7 def enabled, send a SIU message if there is an outbound SIU message defined
            if (HL7Defs.IsExistingHL7Enabled())
            {
                //S15 - Appt Cancellation event
                MessageHL7 messageHL7 = MessageConstructor.GenerateSIU(pat, Patients.GetPat(pat.Guarantor), EventTypeHL7.S15, appt);
                //Will be null if there is no outbound SIU message defined, so do nothing
                if (messageHL7 != null)
                {
                    HL7Msg hl7Msg = new HL7Msg();
                    hl7Msg.AptNum    = appt.AptNum;
                    hl7Msg.HL7Status = HL7MessageStatus.OutPending;                  //it will be marked outSent by the HL7 service.
                    hl7Msg.MsgText   = messageHL7.ToString();
                    hl7Msg.PatNum    = pat.PatNum;
                    HL7Msgs.Insert(hl7Msg);
#if DEBUG
                    MessageBox.Show("Appointments", messageHL7.ToString());
#endif
                }
            }
            #endregion
            #region Charting the proc
            if (procCode != null)
            {
                switch (procCode.ProcCode)
                {
                case "D9986":                        //Missed
                    HistAppointments.CreateHistoryEntry(appt.AptNum, HistAppointmentAction.Missed);
                    break;

                case "D9987":                        //Cancelled
                    HistAppointments.CreateHistoryEntry(appt.AptNum, HistAppointmentAction.Cancelled);
                    break;
                }
                Procedure procedureCur = new Procedure();
                procedureCur.PatNum       = pat.PatNum;
                procedureCur.ProvNum      = (procCode.ProvNumDefault > 0 ? procCode.ProvNumDefault : appt.ProvNum);
                procedureCur.CodeNum      = procCode.CodeNum;
                procedureCur.ProcDate     = DateTime.Today;
                procedureCur.DateEntryC   = DateTime.Now;
                procedureCur.ProcStatus   = ProcStat.C;
                procedureCur.ClinicNum    = appt.ClinicNum;
                procedureCur.UserNum      = Security.CurUser.UserNum;
                procedureCur.Note         = Lans.g("AppointmentEdit", "Appt BROKEN for") + " " + appt.ProcDescript + "  " + appt.AptDateTime.ToString();
                procedureCur.PlaceService = (PlaceOfService)PrefC.GetInt(PrefName.DefaultProcedurePlaceService);              //Default proc place of service for the Practice is used.
                List <InsSub>  listInsSubs    = InsSubs.RefreshForFam(Patients.GetFamily(pat.PatNum));
                List <InsPlan> listInsPlans   = InsPlans.RefreshForSubList(listInsSubs);
                List <PatPlan> listPatPlans   = PatPlans.Refresh(pat.PatNum);
                InsPlan        insPlanPrimary = null;
                InsSub         insSubPrimary  = null;
                if (listPatPlans.Count > 0)
                {
                    insSubPrimary  = InsSubs.GetSub(listPatPlans[0].InsSubNum, listInsSubs);
                    insPlanPrimary = InsPlans.GetPlan(insSubPrimary.PlanNum, listInsPlans);
                }
                double procFee;
                long   feeSch;
                if (insPlanPrimary == null || procCode.NoBillIns)
                {
                    feeSch = FeeScheds.GetFeeSched(0, pat.FeeSched, procedureCur.ProvNum);
                }
                else                  //Only take into account the patient's insurance fee schedule if the D9986 procedure is not marked as NoBillIns
                {
                    feeSch = FeeScheds.GetFeeSched(insPlanPrimary.FeeSched, pat.FeeSched, procedureCur.ProvNum);
                }
                procFee = Fees.GetAmount0(procedureCur.CodeNum, feeSch, procedureCur.ClinicNum, procedureCur.ProvNum);
                if (insPlanPrimary != null && insPlanPrimary.PlanType == "p" && !insPlanPrimary.IsMedical)         //PPO
                {
                    double provFee = Fees.GetAmount0(procedureCur.CodeNum, Providers.GetProv(procedureCur.ProvNum).FeeSched, procedureCur.ClinicNum,
                                                     procedureCur.ProvNum);
                    procedureCur.ProcFee = Math.Max(provFee, procFee);
                }
                else
                {
                    procedureCur.ProcFee = procFee;
                }
                if (!PrefC.GetBool(PrefName.EasyHidePublicHealth))
                {
                    procedureCur.SiteNum = pat.SiteNum;
                }
                Procedures.Insert(procedureCur);
                //Now make a claimproc if the patient has insurance.  We do this now for consistency because a claimproc could get created in the future.
                List <Benefit>   listBenefits          = Benefits.Refresh(listPatPlans, listInsSubs);
                List <ClaimProc> listClaimProcsForProc = ClaimProcs.RefreshForProc(procedureCur.ProcNum);
                Procedures.ComputeEstimates(procedureCur, pat.PatNum, listClaimProcsForProc, false, listInsPlans, listPatPlans, listBenefits, pat.Age, listInsSubs);
                FormProcBroken FormPB = new FormProcBroken(procedureCur);
                FormPB.IsNew = true;
                FormPB.ShowDialog();
            }
            #endregion
            #region BrokenApptAdjustment
            if (PrefC.GetBool(PrefName.BrokenApptAdjustment))
            {
                Adjustment AdjustmentCur = new Adjustment();
                AdjustmentCur.DateEntry = DateTime.Today;
                AdjustmentCur.AdjDate   = DateTime.Today;
                AdjustmentCur.ProcDate  = DateTime.Today;
                AdjustmentCur.ProvNum   = appt.ProvNum;
                AdjustmentCur.PatNum    = pat.PatNum;
                AdjustmentCur.AdjType   = PrefC.GetLong(PrefName.BrokenAppointmentAdjustmentType);
                AdjustmentCur.ClinicNum = appt.ClinicNum;
                FormAdjust FormA = new FormAdjust(pat, AdjustmentCur);
                FormA.IsNew = true;
                FormA.ShowDialog();
            }
            #endregion
            #region BrokenApptCommLog
            if (PrefC.GetBool(PrefName.BrokenApptCommLog))
            {
                Commlog CommlogCur = new Commlog();
                CommlogCur.PatNum       = pat.PatNum;
                CommlogCur.CommDateTime = DateTime.Now;
                CommlogCur.CommType     = Commlogs.GetTypeAuto(CommItemTypeAuto.APPT);
                CommlogCur.Note         = Lan.g("Appointment", "Appt BROKEN for") + " " + appt.ProcDescript + "  " + appt.AptDateTime.ToString();
                CommlogCur.Mode_        = CommItemMode.None;
                CommlogCur.UserNum      = Security.CurUser.UserNum;
                FormCommItem FormCI = new FormCommItem();
                FormCI.ShowDialog(new CommItemModel()
                {
                    CommlogCur = CommlogCur
                }, new CommItemController(FormCI)
                {
                    IsNew = true
                });
            }
            #endregion
            AutomationL.Trigger(AutomationTrigger.BreakAppointment, null, pat.PatNum);
            Recalls.SynchScheduledApptFull(appt.PatNum);
        }
Example #11
0
        ///<summary>Sets given appt.AptStatus to broken.
        ///Provide procCode that should be charted, can be null but will not chart a broken procedure.
        ///Also considers various broken procedure based prefs.
        ///Makes its own securitylog entries.</summary>
        public static void BreakApptHelper(Appointment appt, Patient pat, ProcedureCode procCode)
        {
            //suppressHistory is true due to below logic creating a log with a specific HistAppointmentAction instead of the generic changed.
            DateTime datePrevious    = appt.DateTStamp;
            bool     suppressHistory = false;

            if (procCode != null)
            {
                suppressHistory = (procCode.ProcCode.In("D9986", "D9987"));
            }
            Appointments.SetAptStatus(appt, ApptStatus.Broken, suppressHistory); //Appointments S-Class handles Signalods
            if (appt.AptStatus != ApptStatus.Complete)                           //seperate log entry for completed appointments.
            {
                SecurityLogs.MakeLogEntry(Permissions.AppointmentEdit, pat.PatNum,
                                          appt.ProcDescript + ", " + appt.AptDateTime.ToString()
                                          + ", Broken from the Appts module.", appt.AptNum, datePrevious);
            }
            else
            {
                SecurityLogs.MakeLogEntry(Permissions.AppointmentCompleteEdit, pat.PatNum,
                                          appt.ProcDescript + ", " + appt.AptDateTime.ToString()
                                          + ", Broken from the Appts module.", appt.AptNum, datePrevious);
            }
            #region HL7
            //If there is an existing HL7 def enabled, send a SIU message if there is an outbound SIU message defined
            if (HL7Defs.IsExistingHL7Enabled())
            {
                //S15 - Appt Cancellation event
                MessageHL7 messageHL7 = MessageConstructor.GenerateSIU(pat, Patients.GetPat(pat.Guarantor), EventTypeHL7.S15, appt);
                //Will be null if there is no outbound SIU message defined, so do nothing
                if (messageHL7 != null)
                {
                    HL7Msg hl7Msg = new HL7Msg();
                    hl7Msg.AptNum    = appt.AptNum;
                    hl7Msg.HL7Status = HL7MessageStatus.OutPending;                  //it will be marked outSent by the HL7 service.
                    hl7Msg.MsgText   = messageHL7.ToString();
                    hl7Msg.PatNum    = pat.PatNum;
                    HL7Msgs.Insert(hl7Msg);
#if DEBUG
                    MessageBox.Show("Appointments", messageHL7.ToString());
#endif
                }
            }
            #endregion
            List <Procedure> listProcedures = new List <Procedure>();
            //splits should only exist on procs if they are using tp pre-payments
            List <PaySplit> listSplitsForApptProcs = new List <PaySplit>();
            bool            isNonRefundable        = false;
            double          brokenProcAmount       = 0;
            Procedure       brokenProcedure        = new Procedure();
            bool            wasBrokenProcDeleted   = false;
            if (PrefC.GetYN(PrefName.PrePayAllowedForTpProcs))
            {
                listProcedures = Procedures.GetProcsForSingle(appt.AptNum, false);
                if (listProcedures.Count > 0)
                {
                    listSplitsForApptProcs = PaySplits.GetPaySplitsFromProcs(listProcedures.Select(x => x.ProcNum).ToList());
                }
            }
            #region Charting the proc
            if (procCode != null)
            {
                switch (procCode.ProcCode)
                {
                case "D9986":                        //Missed
                    HistAppointments.CreateHistoryEntry(appt.AptNum, HistAppointmentAction.Missed);
                    break;

                case "D9987":                        //Cancelled
                    HistAppointments.CreateHistoryEntry(appt.AptNum, HistAppointmentAction.Cancelled);
                    break;
                }
                brokenProcedure.PatNum       = pat.PatNum;
                brokenProcedure.ProvNum      = (procCode.ProvNumDefault > 0 ? procCode.ProvNumDefault : appt.ProvNum);
                brokenProcedure.CodeNum      = procCode.CodeNum;
                brokenProcedure.ProcDate     = DateTime.Today;
                brokenProcedure.DateEntryC   = DateTime.Now;
                brokenProcedure.ProcStatus   = ProcStat.C;
                brokenProcedure.ClinicNum    = appt.ClinicNum;
                brokenProcedure.UserNum      = Security.CurUser.UserNum;
                brokenProcedure.Note         = Lans.g("AppointmentEdit", "Appt BROKEN for") + " " + appt.ProcDescript + "  " + appt.AptDateTime.ToString();
                brokenProcedure.PlaceService = (PlaceOfService)PrefC.GetInt(PrefName.DefaultProcedurePlaceService);              //Default proc place of service for the Practice is used.
                List <InsSub>  listInsSubs    = InsSubs.RefreshForFam(Patients.GetFamily(pat.PatNum));
                List <InsPlan> listInsPlans   = InsPlans.RefreshForSubList(listInsSubs);
                List <PatPlan> listPatPlans   = PatPlans.Refresh(pat.PatNum);
                InsPlan        insPlanPrimary = null;
                InsSub         insSubPrimary  = null;
                if (listPatPlans.Count > 0)
                {
                    insSubPrimary  = InsSubs.GetSub(listPatPlans[0].InsSubNum, listInsSubs);
                    insPlanPrimary = InsPlans.GetPlan(insSubPrimary.PlanNum, listInsPlans);
                }
                double procFee;
                long   feeSch;
                if (insPlanPrimary == null || procCode.NoBillIns)
                {
                    feeSch = FeeScheds.GetFeeSched(0, pat.FeeSched, brokenProcedure.ProvNum);
                }
                else                  //Only take into account the patient's insurance fee schedule if the D9986 procedure is not marked as NoBillIns
                {
                    feeSch = FeeScheds.GetFeeSched(insPlanPrimary.FeeSched, pat.FeeSched, brokenProcedure.ProvNum);
                }
                procFee = Fees.GetAmount0(brokenProcedure.CodeNum, feeSch, brokenProcedure.ClinicNum, brokenProcedure.ProvNum);
                if (insPlanPrimary != null && insPlanPrimary.PlanType == "p" && !insPlanPrimary.IsMedical)         //PPO
                {
                    double provFee = Fees.GetAmount0(brokenProcedure.CodeNum, Providers.GetProv(brokenProcedure.ProvNum).FeeSched, brokenProcedure.ClinicNum,
                                                     brokenProcedure.ProvNum);
                    brokenProcedure.ProcFee = Math.Max(provFee, procFee);
                }
                else if (listSplitsForApptProcs.Count > 0 && PrefC.GetBool(PrefName.TpPrePayIsNonRefundable) && procCode.ProcCode == "D9986")
                {
                    //if there are pre-payments, non-refundable pre-payments is turned on, and the broken appointment is a missed code then auto-fill
                    //the window with the sum of the procs for the appointment. Transfer money below after broken procedure is confirmed by the user.
                    brokenProcedure.ProcFee = listSplitsForApptProcs.Sum(x => x.SplitAmt);
                    isNonRefundable         = true;
                }
                else
                {
                    brokenProcedure.ProcFee = procFee;
                }
                if (!PrefC.GetBool(PrefName.EasyHidePublicHealth))
                {
                    brokenProcedure.SiteNum = pat.SiteNum;
                }
                Procedures.Insert(brokenProcedure);
                //Now make a claimproc if the patient has insurance.  We do this now for consistency because a claimproc could get created in the future.
                List <Benefit>   listBenefits          = Benefits.Refresh(listPatPlans, listInsSubs);
                List <ClaimProc> listClaimProcsForProc = ClaimProcs.RefreshForProc(brokenProcedure.ProcNum);
                Procedures.ComputeEstimates(brokenProcedure, pat.PatNum, listClaimProcsForProc, false, listInsPlans, listPatPlans, listBenefits, pat.Age, listInsSubs);
                FormProcBroken FormPB = new FormProcBroken(brokenProcedure, isNonRefundable);
                FormPB.IsNew = true;
                FormPB.ShowDialog();
                brokenProcAmount     = FormPB.AmountTotal;
                wasBrokenProcDeleted = FormPB.IsProcDeleted;
            }
            #endregion
            #region BrokenApptAdjustment
            if (PrefC.GetBool(PrefName.BrokenApptAdjustment))
            {
                Adjustment AdjustmentCur = new Adjustment();
                AdjustmentCur.DateEntry = DateTime.Today;
                AdjustmentCur.AdjDate   = DateTime.Today;
                AdjustmentCur.ProcDate  = DateTime.Today;
                AdjustmentCur.ProvNum   = appt.ProvNum;
                AdjustmentCur.PatNum    = pat.PatNum;
                AdjustmentCur.AdjType   = PrefC.GetLong(PrefName.BrokenAppointmentAdjustmentType);
                AdjustmentCur.ClinicNum = appt.ClinicNum;
                FormAdjust FormA = new FormAdjust(pat, AdjustmentCur);
                FormA.IsNew = true;
                FormA.ShowDialog();
            }
            #endregion
            #region BrokenApptCommLog
            if (PrefC.GetBool(PrefName.BrokenApptCommLog))
            {
                Commlog commlogCur = new Commlog();
                commlogCur.PatNum       = pat.PatNum;
                commlogCur.CommDateTime = DateTime.Now;
                commlogCur.CommType     = Commlogs.GetTypeAuto(CommItemTypeAuto.APPT);
                commlogCur.Note         = Lan.g("Appointment", "Appt BROKEN for") + " " + appt.ProcDescript + "  " + appt.AptDateTime.ToString();
                commlogCur.Mode_        = CommItemMode.None;
                commlogCur.UserNum      = Security.CurUser.UserNum;
                commlogCur.IsNew        = true;
                FormCommItem FormCI = new FormCommItem(commlogCur);
                FormCI.ShowDialog();
            }
            #endregion
            #region Transfer money from TP Procedures if necessary
            //Note this MUST come after FormProcBroken since clicking cancel in that window will delete the procedure.
            if (isNonRefundable && !wasBrokenProcDeleted && listSplitsForApptProcs.Count > 0)
            {
                //transfer what the user specified in the broken appointment window.
                //transfer up to the amount specified by the user
                foreach (Procedure proc in listProcedures)
                {
                    if (brokenProcAmount == 0)
                    {
                        break;
                    }
                    List <PaySplit> listSplitsForAppointmentProcedure = listSplitsForApptProcs.FindAll(x => x.ProcNum == proc.ProcNum);
                    foreach (PaySplit split in listSplitsForAppointmentProcedure)
                    {
                        if (brokenProcAmount == 0)
                        {
                            break;
                        }
                        double amt = Math.Min(brokenProcAmount, split.SplitAmt);
                        Payments.CreateTransferForTpProcs(proc, new List <PaySplit> {
                            split
                        }, brokenProcedure, amt);
                        double amtPaidOnApt = listSplitsForApptProcs.Sum(x => x.SplitAmt);
                        if (amtPaidOnApt > amt)
                        {
                            //If the original prepayment amount is greater than the amt being specified for the appointment break, transfer
                            //the difference to an Unallocated Unearned Paysplit on the account.
                            double remainingAmt = amtPaidOnApt - amt;
                            //We have to create a new transfer payment here to correlate to the split.
                            Payment txfrPayment = new Payment();
                            txfrPayment.PayAmt    = 0;
                            txfrPayment.PayDate   = DateTime.Today;
                            txfrPayment.ClinicNum = split.ClinicNum;
                            txfrPayment.PayNote   = "Automatic transfer from treatment planned procedure prepayment.";
                            txfrPayment.PatNum    = split.PatNum;                       //ultimately where the payment ends up.
                            txfrPayment.PayType   = 0;
                            Payments.Insert(txfrPayment);
                            PaymentEdit.IncomeTransferData transferData = PaymentEdit.IncomeTransferData.CreateTransfer(split, txfrPayment.PayNum, true, remainingAmt);
                            PaySplit offset         = transferData.ListSplitsCur.FirstOrDefault(x => x.FSplitNum != 0);
                            long     offsetSplitNum = PaySplits.Insert(offset);                      //Get the FSplitNum from the offset
                            PaySplit allocation     = transferData.ListSplitsCur.FirstOrDefault(x => x.FSplitNum == 0);
                            allocation.FSplitNum = offsetSplitNum;
                            PaySplits.Insert(allocation);                            //Insert so the split is now up to date
                            SecurityLogs.MakeLogEntry(Permissions.PaymentCreate, txfrPayment.PatNum, "Automatic transfer of funds for treatment plan procedure pre-payments.");
                        }
                        brokenProcAmount -= amt;
                    }
                }
            }
            //if broken appointment procedure was deleted (user cancelled out of the window) just keep money on the original procedure.
            #endregion
            AppointmentEvent.Fire(ODEventType.AppointmentEdited, appt);
            AutomationL.Trigger(AutomationTrigger.BreakAppointment, null, pat.PatNum);
            Recalls.SynchScheduledApptFull(appt.PatNum);
        }
Example #12
0
        ///<summary>Returns AptNum of the incoming appointment.</summary>
        public static long ProcessSCH(Patient pat, HL7DefSegment segDef, SegmentHL7 seg)
        {
            string   aptNote   = "";
            double   aptLength = 0;
            long     aptNum    = 0;
            DateTime aptStart  = DateTime.MinValue;
            DateTime aptStop   = DateTime.MinValue;

            for (int i = 0; i < segDef.hl7DefFields.Count; i++)
            {
                int ordinalPos = segDef.hl7DefFields[i].OrdinalPos;
                switch (segDef.hl7DefFields[i].FieldName)
                {
                case "apt.AptNum":
                    aptNum = PIn.Long(seg.GetFieldComponent(ordinalPos));
                    continue;

                case "apt.lengthStartEnd":
                    aptLength = FieldParser.SecondsToMinutes(seg.GetFieldComponent(ordinalPos, 2));
                    aptStart  = FieldParser.DateTimeParse(seg.GetFieldComponent(ordinalPos, 3));
                    aptStop   = FieldParser.DateTimeParse(seg.GetFieldComponent(ordinalPos, 4));
                    continue;

                case "apt.Note":
                    aptNote = seg.GetFieldComponent(ordinalPos);
                    continue;

                default:
                    continue;
                }
            }
            Appointment apt      = Appointments.GetOneApt(aptNum);
            Appointment aptOld   = null;
            bool        isNewApt = apt == null;

            if (isNewApt)
            {
                apt           = new Appointment();
                apt.AptNum    = aptNum;
                apt.PatNum    = pat.PatNum;
                apt.AptStatus = ApptStatus.Scheduled;
            }
            else
            {
                aptOld = apt.Clone();
            }
            if (apt.PatNum != pat.PatNum)           //we can't process this message because wrong patnum.
            {
                throw new Exception("Appointment does not match patient: " + pat.FName + " " + pat.LName + ", apt.PatNum: " + apt.PatNum.ToString() + ", pat.PatNum: " + pat.PatNum.ToString());
            }
            apt.Note = aptNote;
            string pattern;

            //If aptStop is MinValue we know that stop time was not sent or was not in the correct format so try to use the duration field.
            if (aptStop == DateTime.MinValue && aptLength != 0)         //Stop time is optional.  If not included we will use the duration field to calculate pattern.
            {
                pattern = FieldParser.ProcessPattern(aptStart, aptStart.AddMinutes(aptLength));
            }
            else              //We received a good stop time or stop time is MinValue but we don't have a good aptLength so ProcessPattern will return the apt length or the default 5 minutes
            {
                pattern = FieldParser.ProcessPattern(aptStart, aptStop);
            }
            apt.AptDateTime = aptStart;
            apt.Pattern     = pattern;
            apt.ProvNum     = pat.PriProv;      //Set apt.ProvNum to the patient's primary provider.  This may change after processing the AIG or PV1 segments, but set here in case those segs are missing.
            if (pat.FName == "" || pat.LName == "")
            {
                throw new Exception("Appointment not processed due to missing patient first or last name. PatNum:" + pat.PatNum.ToString());
            }
            if (isNewApt)
            {
                if (IsVerboseLogging)
                {
                    EventLog.WriteEntry("OpenDentHL7", "Inserted appointment for: " + pat.FName + " " + pat.LName, EventLogEntryType.Information);
                }
                Appointments.InsertIncludeAptNum(apt, true);
            }
            else
            {
                if (IsVerboseLogging)
                {
                    EventLog.WriteEntry("OpenDentHL7", "Updated appointment for: " + pat.FName + " " + pat.LName, EventLogEntryType.Information);
                }
                Appointments.Update(apt, aptOld);
            }
            HL7MsgCur.AptNum = apt.AptNum;
            HL7Msgs.Update(HL7MsgCur);
            return(aptNum);
        }
Example #13
0
        ///<summary>If relationship is self, this method does nothing.  A new pat will later change guarantor to be same as patnum. </summary>
        public static void ProcessGT1(Patient pat, HL7DefSegment segDef, SegmentHL7 seg)
        {
            //Find the position of the guarNum, guarChartNum, guarName, and guarBirthdate in this HL7 segment based on the definition of a GT1
            int guarPatNumOrdinal    = -1;
            int guarChartNumOrdinal  = -1;
            int guarNameOrdinal      = -1;
            int guarBirthdateOrdinal = -1;

            for (int i = 0; i < segDef.hl7DefFields.Count; i++)
            {
                if (segDef.hl7DefFields[i].FieldName == "guar.PatNum")
                {
                    guarPatNumOrdinal = segDef.hl7DefFields[i].OrdinalPos;
                }
                if (segDef.hl7DefFields[i].FieldName == "guar.ChartNumber")
                {
                    guarChartNumOrdinal = segDef.hl7DefFields[i].OrdinalPos;
                }
                if (segDef.hl7DefFields[i].FieldName == "guar.nameLFM")
                {
                    guarNameOrdinal = segDef.hl7DefFields[i].OrdinalPos;
                }
                if (segDef.hl7DefFields[i].FieldName == "guar.birthdateTime")
                {
                    guarBirthdateOrdinal = segDef.hl7DefFields[i].OrdinalPos;
                }
            }
            //If neither guar.PatNum nor guar.ChartNumber are included in this GT1 definition log a message in the event log and return
            if (guarPatNumOrdinal == -1 && guarChartNumOrdinal == -1)
            {
                HL7MsgCur.Note = "Guarantor not processed.  guar.PatNum or guar.ChartNumber must be included in the GT1 definition.  PatNum of patient:" + pat.PatNum.ToString();
                HL7Msgs.Update(HL7MsgCur);
                EventLog.WriteEntry("OpenDentHL7", "Guarantor not processed.  guar.PatNum or guar.ChartNumber must be included in the GT1 definition.  PatNum of patient:" + pat.PatNum.ToString()
                                    , EventLogEntryType.Information);
                return;
            }
            //If guar.nameLFM is not included in this GT1 definition log a message in the event log and return
            if (guarNameOrdinal == -1)
            {
                HL7MsgCur.Note = "Guarantor not processed due to guar.nameLFM not included in the GT1 definition.  Patnum of patient:" + pat.PatNum.ToString();
                HL7Msgs.Update(HL7MsgCur);
                EventLog.WriteEntry("OpenDentHL7", "Guarantor not processed due to guar.nameLFM not included in the GT1 definition.  Patnum of patient:" + pat.PatNum.ToString()
                                    , EventLogEntryType.Information);
                return;
            }
            //If the first or last name are not included in this GT1 segment, log a message in the event log and return
            if (seg.GetFieldComponent(guarNameOrdinal, 0) == "" || seg.GetFieldComponent(guarNameOrdinal, 1) == "")
            {
                HL7MsgCur.Note = "Guarantor not processed due to missing first or last name.  PatNum of patient:" + pat.PatNum.ToString();
                HL7Msgs.Update(HL7MsgCur);
                EventLog.WriteEntry("OpenDentHL7", "Guarantor not processed due to missing first or last name.  PatNum of patient:" + pat.PatNum.ToString()
                                    , EventLogEntryType.Information);
                return;
            }
            //Only process GT1 if either guar.PatNum or guar.ChartNumber is included and both guar.LName and guar.FName are included
            long   guarPatNum   = 0;
            string guarChartNum = "";

            if (guarPatNumOrdinal != -1)
            {
                guarPatNum = PIn.Long(seg.GetFieldFullText(guarPatNumOrdinal));
            }
            if (guarChartNumOrdinal != -1)
            {
                guarChartNum = seg.GetFieldComponent(guarChartNumOrdinal);
            }
            if (guarPatNum == 0 && guarChartNum == "")         //because we have an example where they sent us this (position 2 (guarPatNumOrder or guarChartNumOrder for eCW) is empty): GT1|1||^^||^^^^||||||||
            {
                HL7MsgCur.Note = "Guarantor not processed due to missing both guar.PatNum and guar.ChartNumber.  One of those numbers must be included.  PatNum of patient:" + pat.PatNum.ToString();
                HL7Msgs.Update(HL7MsgCur);
                EventLog.WriteEntry("OpenDentHL7", "Guarantor not processed due to missing both guar.PatNum and guar.ChartNumber.  One of those numbers must be included.  PatNum of patient:" + pat.PatNum.ToString()
                                    , EventLogEntryType.Information);
                return;
            }
            if (guarPatNum == pat.PatNum || guarChartNum == pat.ChartNumber)         //if relationship is self
            {
                return;
            }
            //Guar must be someone else
            Patient guar    = null;
            Patient guarOld = null;

            //Find guarantor by guar.PatNum if defined and in this segment
            if (guarPatNum != 0)
            {
                guar = Patients.GetPat(guarPatNum);
            }
            else              //guarPatNum was 0 so try to get guar by guar.ChartNumber or name and birthdate
                              //try to find guarantor using chartNumber
            {
                guar = Patients.GetPatByChartNumber(guarChartNum);
                if (guar == null)
                {
                    //try to find the guarantor by using name and birthdate
                    string   guarLName     = seg.GetFieldComponent(guarNameOrdinal, 0);
                    string   guarFName     = seg.GetFieldComponent(guarNameOrdinal, 1);
                    DateTime guarBirthdate = FieldParser.DateTimeParse(seg.GetFieldFullText(guarBirthdateOrdinal));
                    long     guarNumByName = Patients.GetPatNumByNameAndBirthday(guarLName, guarFName, guarBirthdate);
                    if (guarNumByName == 0)                   //guarantor does not exist in OD
                    //so guar will still be null, triggering creation of new guarantor further down.
                    {
                    }
                    else
                    {
                        guar             = Patients.GetPat(guarNumByName);
                        guarOld          = guar.Copy();
                        guar.ChartNumber = guarChartNum;                      //from now on, we will be able to find guar by chartNumber
                        Patients.Update(guar, guarOld);
                    }
                }
            }
            //At this point we have a guarantor located in OD or guar=null so guar is new patient
            bool isNewGuar = guar == null;

            if (isNewGuar)             //then we need to add guarantor to db
            {
                guar = new Patient();
                if (guarPatNum != 0)
                {
                    guar.PatNum = guarPatNum;
                }
                else
                {
                    guar.ChartNumber = guarChartNum;
                }
                guar.PriProv     = PrefC.GetLong(PrefName.PracticeDefaultProv);
                guar.BillingType = PrefC.GetLong(PrefName.PracticeDefaultBillType);
            }
            else
            {
                guarOld = guar.Copy();
            }
            //Now that we have our guarantor, process the GT1 segment
            for (int i = 0; i < segDef.hl7DefFields.Count; i++)
            {
                int itemOrder = segDef.hl7DefFields[i].OrdinalPos;
                switch (segDef.hl7DefFields[i].FieldName)
                {
                case "guar.addressCityStateZip":
                    guar.Address  = seg.GetFieldComponent(itemOrder, 0);
                    guar.Address2 = seg.GetFieldComponent(itemOrder, 1);
                    guar.City     = seg.GetFieldComponent(itemOrder, 2);
                    guar.State    = seg.GetFieldComponent(itemOrder, 3);
                    guar.Zip      = seg.GetFieldComponent(itemOrder, 4);
                    continue;

                case "guar.birthdateTime":
                    guar.Birthdate = FieldParser.DateTimeParse(seg.GetFieldComponent(itemOrder));
                    continue;

                case "guar.ChartNumber":
                    guar.ChartNumber = seg.GetFieldComponent(itemOrder);
                    continue;

                case "guar.Gender":
                    guar.Gender = FieldParser.GenderParse(seg.GetFieldComponent(itemOrder));
                    continue;

                case "guar.HmPhone":
                    guar.HmPhone = FieldParser.PhoneParse(seg.GetFieldComponent(itemOrder));
                    continue;

                case "guar.nameLFM":
                    guar.LName   = seg.GetFieldComponent(itemOrder, 0);
                    guar.FName   = seg.GetFieldComponent(itemOrder, 1);
                    guar.MiddleI = seg.GetFieldComponent(itemOrder, 2);
                    continue;

                //case "guar.PatNum": Maybe do nothing??
                case "guar.SSN":
                    guar.SSN = seg.GetFieldComponent(itemOrder);
                    continue;

                case "guar.WkPhone":
                    guar.WkPhone = FieldParser.PhoneParse(seg.GetFieldComponent(itemOrder));
                    continue;

                default:
                    continue;
                }
            }
            if (isNewGuar)
            {
                guarOld = guar.Copy();
                if (guar.PatNum == 0)
                {
                    guar.PatNum = Patients.Insert(guar, false);
                }
                else
                {
                    guar.PatNum = Patients.Insert(guar, true);
                }
                guar.Guarantor = guar.PatNum;
                Patients.Update(guar, guarOld);
            }
            else
            {
                Patients.Update(guar, guarOld);
            }
            pat.Guarantor = guar.PatNum;
            return;
        }
Example #14
0
        /// <summary>
        private static Patient CreatePatient(String LastName, String FirstName, DateTime birthDate, WebForms_Sheet webFormSheet, Sheet sheet, string cultureName)
        {
            bool    isWebForm = webFormSheet != null;
            Patient newPat    = new Patient();

            newPat.LName     = LastName;
            newPat.FName     = FirstName;
            newPat.Birthdate = birthDate;
            if (isWebForm)
            {
                newPat.ClinicNum = webFormSheet.ClinicNum;
            }
            else
            {
                newPat.ClinicNum = sheet.ClinicNum;
            }
            if (!PrefC.HasClinicsEnabled)
            {
                //Set the patients primary provider to the practice default provider.
                newPat.PriProv = Providers.GetDefaultProvider().ProvNum;
            }
            else              //Using clinics.
            //Set the patients primary provider to the clinic default provider.
            {
                newPat.PriProv = Providers.GetDefaultProvider(Clinics.ClinicNum).ProvNum;
            }
            Type t = newPat.GetType();

            FieldInfo[] fi = t.GetFields();
            foreach (FieldInfo field in fi)
            {
                // find match for fields in Patients in the SheetFields
                if (isWebForm)
                {
                    List <WebForms_SheetField> listWebFormsSheetFields = webFormSheet.SheetFields.FindAll(x => x.FieldName.ToLower() == field.Name.ToLower());
                    if (listWebFormsSheetFields.Count() > 0)
                    {
                        // this loop is used to fill a field that may generate mutiple values for a single field in the patient.
                        //for example the field gender has 2 eqivalent sheet fields in the SheetFields
                        foreach (WebForms_SheetField webFormsSheetField in listWebFormsSheetFields)
                        {
                            FillPatientFields(newPat, field, webFormsSheetField.FieldValue, webFormsSheetField.RadioButtonValue, cultureName, isWebForm, false);
                        }
                    }
                }
                else
                {
                    List <SheetField> listSheetFields = sheet.SheetFields.FindAll(x => x.FieldName.ToLower() == field.Name.ToLower());
                    if (listSheetFields.Count() > 0)
                    {
                        // this loop is used to fill a field that may generate mutiple values for a single field in the patient.
                        //for example the field gender has 2 eqivalent sheet fields in the SheetFields
                        foreach (SheetField sheetField in listSheetFields)
                        {
                            FillPatientFields(newPat, field, sheetField.FieldValue, sheetField.RadioButtonValue, "", isWebForm, sheet.IsCemtTransfer);
                        }
                    }
                }
            }
            try {
                Patients.Insert(newPat, false);
                SecurityLogs.MakeLogEntry(Permissions.PatientCreate, newPat.PatNum, isWebForm ? "Created from Web Forms." : "Created from CEMT transfer.");
                //set Guarantor field the same as PatNum
                Patient patOld = newPat.Copy();
                newPat.Guarantor = newPat.PatNum;
                Patients.Update(newPat, patOld);
                //If there is an existing HL7 def enabled, send an ADT message if there is an outbound ADT message defined
                if (HL7Defs.IsExistingHL7Enabled())
                {
                    MessageHL7 messageHL7 = MessageConstructor.GenerateADT(newPat, newPat, EventTypeHL7.A04);                //patient is guarantor
                    //Will be null if there is no outbound ADT message defined, so do nothing
                    if (messageHL7 != null)
                    {
                        HL7Msg hl7Msg = new HL7Msg();
                        hl7Msg.AptNum    = 0;
                        hl7Msg.HL7Status = HL7MessageStatus.OutPending;                      //it will be marked outSent by the HL7 service.
                        hl7Msg.MsgText   = messageHL7.ToString();
                        hl7Msg.PatNum    = newPat.PatNum;
                        HL7Msgs.Insert(hl7Msg);
#if DEBUG
                        MessageBox.Show("FormWebForms", messageHL7.ToString());
#endif
                    }
                }
            }
            catch (Exception e) {
                MessageBox.Show(e.Message);
            }
            return(newPat);
        }
Example #15
0
 private void butOK_Click(object sender, EventArgs e)
 {
     MsgCur.Note = textNote.Text;
     HL7Msgs.Update(MsgCur);
     DialogResult = DialogResult.OK;
 }
Example #16
0
        /// <summary>
        /// </summary>
        private Patient CreatePatient(String LastName, String FirstName, DateTime birthDate, WebSheets.SheetAndSheetField sAnds)
        {
            Patient newPat = new Patient();

            newPat.LName     = LastName;
            newPat.FName     = FirstName;
            newPat.Birthdate = birthDate;
            newPat.ClinicNum = sAnds.web_sheet.ClinicNum;
            if (PrefC.GetBool(PrefName.EasyNoClinics))
            {
                //Set the patients primary provider to the practice default provider.
                newPat.PriProv = Providers.GetDefaultProvider().ProvNum;
            }
            else              //Using clinics.
                              //Set the patients primary provider to the clinic default provider.
            {
                newPat.PriProv = Providers.GetDefaultProvider(Clinics.ClinicNum).ProvNum;
            }
            Type t = newPat.GetType();

            FieldInfo[] fi = t.GetFields();
            foreach (FieldInfo field in fi)
            {
                // find match for fields in Patients in the web_sheetfieldlist
                var WebSheetFieldList = sAnds.web_sheetfieldlist.Where(sf => sf.FieldName.ToLower() == field.Name.ToLower());
                if (WebSheetFieldList.Count() > 0)
                {
                    // this loop is used to fill a field that may generate mutiple values for a single field in the patient.
                    //for example the field gender has 2 eqivalent sheet fields in the web_sheetfieldlist
                    for (int i = 0; i < WebSheetFieldList.Count(); i++)
                    {
                        WebSheets.webforms_sheetfield sf = WebSheetFieldList.ElementAt(i);
                        String SheetWebFieldValue        = sf.FieldValue;
                        String RadioButtonValue          = sf.RadioButtonValue;
                        FillPatientFields(newPat, field, SheetWebFieldValue, RadioButtonValue);
                    }
                }
            }
            try{
                Patients.Insert(newPat, false);
                SecurityLogs.MakeLogEntry(Permissions.PatientCreate, newPat.PatNum, "Created from Web Forms.");
                //set Guarantor field the same as PatNum
                Patient patOld = newPat.Copy();
                newPat.Guarantor = newPat.PatNum;
                Patients.Update(newPat, patOld);
                //If there is an existing HL7 def enabled, send an ADT message if there is an outbound ADT message defined
                if (HL7Defs.IsExistingHL7Enabled())
                {
                    MessageHL7 messageHL7 = MessageConstructor.GenerateADT(newPat, newPat, EventTypeHL7.A04);                //patient is guarantor
                    //Will be null if there is no outbound ADT message defined, so do nothing
                    if (messageHL7 != null)
                    {
                        HL7Msg hl7Msg = new HL7Msg();
                        hl7Msg.AptNum    = 0;
                        hl7Msg.HL7Status = HL7MessageStatus.OutPending;                      //it will be marked outSent by the HL7 service.
                        hl7Msg.MsgText   = messageHL7.ToString();
                        hl7Msg.PatNum    = newPat.PatNum;
                        HL7Msgs.Insert(hl7Msg);
#if DEBUG
                        MessageBox.Show(this, messageHL7.ToString());
#endif
                    }
                }
            }
            catch (Exception e) {
                gridMain.EndUpdate();
                MessageBox.Show(e.Message);
            }
            return(newPat);
        }
Example #17
0
        private void TimerCallbackSendTCP(Object stateInfo)
        {
            List <HL7Msg> list = HL7Msgs.GetOnePending();

            for (int i = 0; i < list.Count; i++)                                       //Right now, there will only be 0 or 1 item in the list.
            {
                string     sendMsgControlId = HL7Msgs.GetControlId(list[i]);           //could be empty string
                Socket     socket           = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                string[]   strIpPort        = HL7DefEnabled.OutgoingIpPort.Split(':'); //this was already validated in the HL7DefEdit window.
                IPAddress  ipaddress        = IPAddress.Parse(strIpPort[0]);           //already validated
                int        port             = int.Parse(strIpPort[1]);                 //already validated
                IPEndPoint endpoint         = new IPEndPoint(ipaddress, port);
                try {
                    socket.Connect(endpoint);
                    if (!IsSendTCPConnected)                     //Previous run failed to connect. This time connected so make log entry that connection was successful
                    {
                        EventLog.WriteEntry("OpenDentHL7", "The HL7 send TCP/IP socket connection failed to connect previously and was successful this time.", EventLogEntryType.Information);
                        IsSendTCPConnected = true;
                    }
                }
                catch (SocketException ex) {
                    if (IsSendTCPConnected)                     //Previous run connected fine, make log entry and set bool to false
                    {
                        EventLog.WriteEntry("OpenDentHL7", "The HL7 send TCP/IP socket connection failed to connect.\r\nException: " + ex.Message, EventLogEntryType.Warning);
                        IsSendTCPConnected = false;
                    }
                    return;
                }
                string data = MLLP_START_CHAR + list[i].MsgText + MLLP_END_CHAR + MLLP_ENDMSG_CHAR;
                try {
                    byte[] byteData = Encoding.ASCII.GetBytes(data);
                    socket.Send(byteData);                                //this is a blocking call
                    //For MLLP V2, do a blocking Receive here, along with a timeout.
                    byte[] ackBuffer = new byte[256];                     //plenty big enough to receive the entire ack/nack response
                    socket.ReceiveTimeout = 5000;                         //5 second timeout. Database is polled every 6 seconds for a new message to send, 5 second wait for acknowledgment
                    int    byteCountReceived = socket.Receive(ackBuffer); //blocking Receive
                    char[] chars             = new char[byteCountReceived];
                    Encoding.UTF8.GetDecoder().GetChars(ackBuffer, 0, byteCountReceived, chars, 0);
                    StringBuilder strbAckMsg = new StringBuilder();
                    strbAckMsg.Append(chars);
                    if (strbAckMsg.Length > 0 && strbAckMsg[0] != MLLP_START_CHAR)
                    {
                        list[i].Note = list[i].Note + "Malformed acknowledgment.\r\n";
                        HL7Msgs.Update(list[i]);
                        throw new Exception("Malformed acknowledgment.");
                    }
                    else if (strbAckMsg.Length >= 3 &&
                             strbAckMsg[strbAckMsg.Length - 1] == MLLP_ENDMSG_CHAR &&               //last char is the endmsg char.
                             strbAckMsg[strbAckMsg.Length - 2] == MLLP_END_CHAR)                  //the second-to-the-last char is the end char.
                    {
                        //we have a complete message
                        strbAckMsg.Remove(0, 1);                       //strip off the start char
                        strbAckMsg.Remove(strbAckMsg.Length - 2, 2);   //strip off the end chars
                    }
                    else if (strbAckMsg.Length >= 2 &&              //so that the next line won't crash
                             strbAckMsg[strbAckMsg.Length - 1] == MLLP_END_CHAR)                  //the last char is the end char.
                    {
                        //we will treat this as a complete message, because the endmsg char is optional.
                        strbAckMsg.Remove(0, 1);                       //strip off the start char
                        strbAckMsg.Remove(strbFullMsg.Length - 1, 1);  //strip off the end char
                    }
                    else
                    {
                        list[i].Note = list[i].Note + "Malformed acknowledgment.\r\n";
                        HL7Msgs.Update(list[i]);
                        throw new Exception("Malformed acknowledgment.");
                    }
                    MessageHL7 ackMsg = new MessageHL7(strbAckMsg.ToString());
                    try {
                        MessageParser.ProcessAck(ackMsg, IsVerboseLogging);
                    }
                    catch (Exception ex) {
                        list[i].Note = list[i].Note + ackMsg.ToString() + "\r\nError processing acknowledgment.\r\n";
                        HL7Msgs.Update(list[i]);
                        throw new Exception("Error processing acknowledgment.\r\n" + ex.Message);
                    }
                    if (ackMsg.AckCode == "" || ackMsg.ControlId == "")
                    {
                        list[i].Note = list[i].Note + ackMsg.ToString() + "\r\nInvalid ACK message.  Attempt to resend.\r\n";
                        HL7Msgs.Update(list[i]);
                        throw new Exception("Invalid ACK message received.");
                    }
                    if (ackMsg.ControlId == sendMsgControlId && ackMsg.AckCode == "AA")                 //acknowledged received (Application acknowledgment: Accept)
                    {
                        list[i].Note = list[i].Note + ackMsg.ToString() + "\r\nMessage ACK (acknowledgment) received.\r\n";
                    }
                    else if (ackMsg.ControlId == sendMsgControlId && ackMsg.AckCode != "AA") //ACK received for this message, but ack code was not acknowledgment accepted
                    {
                        if (list[i].Note.Contains("NACK4"))                                  //this is the 5th negative acknowledgment, don't try again
                        {
                            list[i].Note      = list[i].Note + "Ack code: " + ackMsg.AckCode + "\r\nThis is NACK5, the message status has been changed to OutFailed. We will not attempt to send again.\r\n";
                            list[i].HL7Status = HL7MessageStatus.OutFailed;
                        }
                        else if (list[i].Note.Contains("NACK"))                         //failed sending at least once already
                        {
                            list[i].Note = list[i].Note + "Ack code: " + ackMsg.AckCode + "\r\nNACK" + list[i].Note.Split(new string[] { "NACK" }, StringSplitOptions.None).Length + "\r\n";
                        }
                        else
                        {
                            list[i].Note = list[i].Note + "Ack code: " + ackMsg.AckCode + "\r\nMessage NACK (negative acknowlegment) received. We will try to send again.\r\n";
                        }
                        HL7Msgs.Update(list[i]); //Add NACK note to hl7msg table entry
                        return;                  //socket closed in finally
                    }
                    else                         //ack received for control ID that does not match the control ID of message just sent
                    {
                        list[i].Note = list[i].Note + "Sent message control ID: " + sendMsgControlId + "\r\nAck message control ID: " + ackMsg.ControlId
                                       + "\r\nAck received for message other than message just sent.  We will try to send again.\r\n";
                        HL7Msgs.Update(list[i]);
                        return;
                    }
                }
                catch (SocketException ex) {
                    EventLog.WriteEntry("OpenDentHL7", "The HL7 send TCP/IP socket encountered an error when sending message or receiving acknowledgment.\r\nSocket Exception: " + ex.Message, EventLogEntryType.Warning);
                    return;                    //Try again in 3 seconds. socket closed in finally
                }
                catch (Exception ex) {
                    EventLog.WriteEntry("OpenDentHL7", "The HL7 send TCP/IP encountered an error when sending message or receiving acknowledgment.\r\nException: " + ex.Message, EventLogEntryType.Warning);
                    return;                    //socket closed in finally
                }
                finally {
                    if (socket != null)
                    {
                        socket.Shutdown(SocketShutdown.Both);
                        socket.Close();
                    }
                }
                list[i].HL7Status = HL7MessageStatus.OutSent;
                HL7Msgs.Update(list[i]);                //set the status to sent and save ack message in Note field.
                HL7Msgs.DeleteOldMsgText();             //This is inside the loop so that it happens less frequently.  To clean up incoming messages, we may move this someday.
            }
        }
Example #18
0
        ///<summary>Runs in a separate thread</summary>
        private void OnDataReceived(IAsyncResult asyncResult)
        {
            int byteCountReceived = socketIncomingWorker.EndReceive(asyncResult);          //blocks until data is recieved.

            char[]  chars   = new char[byteCountReceived];
            Decoder decoder = Encoding.UTF8.GetDecoder();

            decoder.GetChars(dataBufferIncoming, 0, byteCountReceived, chars, 0); //doesn't necessarily get all bytes from the buffer because buffer could be half full.
            strbFullMsg.Append(chars);                                            //strbFullMsg might already have partial data
            //I think we are guaranteed to have received at least one char.
            bool isFullMsg   = false;
            bool isMalformed = false;

            if (strbFullMsg.Length == 1 && strbFullMsg[0] == MLLP_ENDMSG_CHAR) //the only char in the message is the end char
            {
                strbFullMsg.Clear();                                           //this must be the very end of a previously processed message.  Discard.
                isFullMsg = false;
            }
            //else if(strbFullMsg[0]!=MLLP_START_CHAR) {
            else if (strbFullMsg.Length > 0 && strbFullMsg[0] != MLLP_START_CHAR)
            {
                //Malformed message.
                isFullMsg   = true;            //we're going to do this so that the error gets saved in the database further down.
                isMalformed = true;
            }
            else if (strbFullMsg.Length >= 3 &&      //so that the next two lines won't crash
                     strbFullMsg[strbFullMsg.Length - 1] == MLLP_ENDMSG_CHAR &&       //last char is the endmsg char.
                     strbFullMsg[strbFullMsg.Length - 2] == MLLP_END_CHAR)          //the second-to-the-last char is the end char.
            {
                //we have a complete message
                strbFullMsg.Remove(0, 1);                      //strip off the start char
                strbFullMsg.Remove(strbFullMsg.Length - 2, 2); //strip off the end chars
                isFullMsg = true;
            }
            else if (strbFullMsg.Length >= 2 &&      //so that the next line won't crash
                     strbFullMsg[strbFullMsg.Length - 1] == MLLP_END_CHAR)          //the last char is the end char.
            {
                //we will treat this as a complete message, because the endmsg char is optional.
                //if the endmsg char gets sent in a subsequent block, the code above will discard it.
                strbFullMsg.Remove(0, 1);                      //strip off the start char
                strbFullMsg.Remove(strbFullMsg.Length - 1, 1); //strip off the end char
                isFullMsg = true;
            }
            else
            {
                isFullMsg = false;              //this is an incomplete message.  Continue to receive more blocks.
            }
            //end of big if statement-------------------------------------------------
            if (!isFullMsg)
            {
                dataBufferIncoming = new byte[8]; //clear the buffer
                socketIncomingWorker.BeginReceive(dataBufferIncoming, 0, dataBufferIncoming.Length, SocketFlags.None, new AsyncCallback(OnDataReceived), null);
                return;                           //get another block
            }
            //Prepare to save message to database if malformed and not processed
            HL7Msg hl7Msg = new HL7Msg();

            hl7Msg.MsgText = strbFullMsg.ToString();
            strbFullMsg.Clear();            //ready for the next message
            bool   isProcessed      = true;
            string messageControlId = "";
            string ackEvent         = "";

            if (isMalformed)
            {
                hl7Msg.HL7Status = HL7MessageStatus.InFailed;
                hl7Msg.Note      = "This message is malformed so it was not processed.";
                HL7Msgs.Insert(hl7Msg);
                isProcessed = false;
            }
            else
            {
                MessageHL7 messageHl7Object = new MessageHL7(hl7Msg.MsgText);              //this creates an entire heirarchy of objects.
                try {
                    MessageParser.Process(messageHl7Object, IsVerboseLogging);             //also saves the message to the db
                    messageControlId = messageHl7Object.ControlId;
                    ackEvent         = messageHl7Object.AckEvent;
                }
                catch (Exception ex) {
                    EventLog.WriteEntry("OpenDentHL7", "Error in OnDataRecieved when processing message:\r\n" + ex.Message + "\r\n" + ex.StackTrace, EventLogEntryType.Error);
                    isProcessed = false;
                }
            }
            MessageHL7 hl7Ack = MessageConstructor.GenerateACK(messageControlId, isProcessed, ackEvent);

            if (hl7Ack == null)
            {
                EventLog.WriteEntry("OpenDentHL7", "No ACK defined for the enabled HL7 definition or no HL7 definition enabled.", EventLogEntryType.Information);
                return;
            }
            byte[] ackByteOutgoing = Encoding.ASCII.GetBytes(MLLP_START_CHAR + hl7Ack.ToString() + MLLP_END_CHAR + MLLP_ENDMSG_CHAR);
            if (IsVerboseLogging)
            {
                EventLog.WriteEntry("OpenDentHL7", "Beginning to send ACK.\r\n" + MLLP_START_CHAR + hl7Ack.ToString() + MLLP_END_CHAR + MLLP_ENDMSG_CHAR, EventLogEntryType.Information);
            }
            socketIncomingWorker.Send(ackByteOutgoing);            //this is a locking call
            //eCW uses the same worker socket to send the next message. Without this call to BeginReceive, they would attempt to send again
            //and the send would fail since we were no longer listening in this thread. eCW would timeout after 30 seconds of waiting for their
            //acknowledgement, then they would close their end and create a new socket for the next message. With this call, we can accept message
            //after message without waiting for a new connection.
            dataBufferIncoming = new byte[8];          //clear the buffer
            socketIncomingWorker.BeginReceive(dataBufferIncoming, 0, dataBufferIncoming.Length, SocketFlags.None, new AsyncCallback(OnDataReceived), null);
        }
Example #19
0
        ///<summary>Creates a single recall appointment. If it's from a double click, then it will end up on that spot in the Appts module.  If not, it will end up on the pinboard with StringDateJumpTo as due date to jump to.  ListAptNumsSelected will contain the AptNum of the new appointment.</summary>
        public void MakeRecallAppointment()
        {
            List <InsSub>  listInsSubs  = InsSubs.RefreshForFam(_famCur);
            List <InsPlan> listInsPlans = InsPlans.RefreshForSubList(listInsSubs);
            Appointment    apt          = null;
            DateTime       dateTimeApt  = DateTime.MinValue;

            if (this.IsInitialDoubleClick)
            {
                dateTimeApt = DateTimeClicked;
            }
            try{
                apt = AppointmentL.CreateRecallApt(_patCur, listInsPlans, -1, listInsSubs, dateTimeApt);
            }
            catch (Exception ex) {
                MessageBox.Show(ex.Message);
                return;
            }
            DateTime datePrevious = apt.DateTStamp;

            ListAptNumsSelected.Add(apt.AptNum);
            if (IsInitialDoubleClick)
            {
                Appointment oldApt = apt.Copy();
                if (_patCur.AskToArriveEarly > 0)
                {
                    apt.DateTimeAskedToArrive = apt.AptDateTime.AddMinutes(-_patCur.AskToArriveEarly);
                    MessageBox.Show(Lan.g(this, "Ask patient to arrive") + " " + _patCur.AskToArriveEarly
                                    + " " + Lan.g(this, "minutes early at") + " " + apt.DateTimeAskedToArrive.ToShortTimeString() + ".");
                }
                apt.AptStatus = ApptStatus.Scheduled;
                apt.ClinicNum = _patCur.ClinicNum;
                apt.Op        = OpNumClicked;
                apt           = Appointments.AssignFieldsForOperatory(apt);
                //Use apt.ClinicNum because it was just set based on Op.ClinicNum in AssignFieldsForOperatory().
                if (!AppointmentL.IsSpecialtyMismatchAllowed(_patCur.PatNum, apt.ClinicNum))
                {
                    return;
                }
                Appointments.Update(apt, oldApt);
                _otherResult = OtherResult.CreateNew;
                SecurityLogs.MakeLogEntry(Permissions.AppointmentCreate, apt.PatNum, apt.AptDateTime.ToString(), apt.AptNum, datePrevious);
                //If there is an existing HL7 def enabled, send a SIU message if there is an outbound SIU message defined
                if (HL7Defs.IsExistingHL7Enabled())
                {
                    //S12 - New Appt Booking event
                    MessageHL7 messageHL7 = MessageConstructor.GenerateSIU(_patCur, _famCur.GetPatient(_patCur.Guarantor), EventTypeHL7.S12, apt);
                    //Will be null if there is no outbound SIU message defined, so do nothing
                    if (messageHL7 != null)
                    {
                        HL7Msg hl7Msg = new HL7Msg();
                        hl7Msg.AptNum    = apt.AptNum;
                        hl7Msg.HL7Status = HL7MessageStatus.OutPending;                      //it will be marked outSent by the HL7 service.
                        hl7Msg.MsgText   = messageHL7.ToString();
                        hl7Msg.PatNum    = _patCur.PatNum;
                        HL7Msgs.Insert(hl7Msg);
#if DEBUG
                        MessageBox.Show(this, messageHL7.ToString());
#endif
                    }
                }
                DialogResult = DialogResult.OK;
                return;
            }
            //not initialClick
            _otherResult = OtherResult.PinboardAndSearch;
            Recall recall = Recalls.GetRecallProphyOrPerio(_patCur.PatNum);          //shouldn't return null.
            if (recall.DateDue < DateTime.Today)
            {
                StringDateJumpTo = DateTime.Today.ToShortDateString();              //they are overdue
            }
            else
            {
                StringDateJumpTo = recall.DateDue.ToShortDateString();
            }
            //no securitylog entry needed here.  That will happen when it's dragged off pinboard.
            DialogResult = DialogResult.OK;
        }
Example #20
0
        //Open \\SERVERFILES\storage\OPEN DENTAL\Programmers Documents\Standards (X12, ADA, etc)\HL7\Version2.6\V26_CH02_Control_M4_JAN2007.doc
        //At the top of page 33, there are rules for the recipient.
        //Basically, they state that parsing should not fail just because there are extra unexpected items.
        //And parsing should also not fail if expected items are missing.

        public static void Process(MessageHL7 msg, bool isVerboseLogging)
        {
            HL7MsgCur           = new HL7Msg();
            HL7MsgCur.HL7Status = HL7MessageStatus.InFailed;          //it will be marked InProcessed once data is inserted.
            HL7MsgCur.MsgText   = msg.ToString();
            HL7MsgCur.PatNum    = 0;
            HL7MsgCur.AptNum    = 0;
            List <HL7Msg> hl7Existing = HL7Msgs.GetOneExisting(HL7MsgCur);

            if (hl7Existing.Count > 0)           //This message is already in the db
            {
                HL7MsgCur.HL7MsgNum = hl7Existing[0].HL7MsgNum;
                HL7Msgs.UpdateDateTStamp(HL7MsgCur);
                msg.ControlId = HL7Msgs.GetControlId(HL7MsgCur);
                return;
            }
            else
            {
                //Insert as InFailed until processing is complete.  Update once complete, PatNum will have correct value, AptNum will have correct value if SIU message or 0 if ADT, and status changed to InProcessed
                HL7Msgs.Insert(HL7MsgCur);
            }
            IsVerboseLogging = isVerboseLogging;
            IsNewPat         = false;
            HL7Def def = HL7Defs.GetOneDeepEnabled();

            if (def == null)
            {
                HL7MsgCur.Note = "Could not process HL7 message.  No HL7 definition is enabled.";
                HL7Msgs.Update(HL7MsgCur);
                throw new Exception("Could not process HL7 message.  No HL7 definition is enabled.");
            }
            HL7DefMessage hl7defmsg = null;

            for (int i = 0; i < def.hl7DefMessages.Count; i++)
            {
                if (def.hl7DefMessages[i].MessageType == msg.MsgType)               //&& def.hl7DefMessages[i].EventType==msg.EventType) { //Ignoring event type for now, we will treat all ADT's and SIU's the same
                {
                    hl7defmsg = def.hl7DefMessages[i];
                }
            }
            if (hl7defmsg == null)           //No message definition matches this message's MessageType and EventType
            {
                HL7MsgCur.Note = "Could not process HL7 message.  No definition for this type of message in the enabled HL7Def.";
                HL7Msgs.Update(HL7MsgCur);
                throw new Exception("Could not process HL7 message.  No definition for this type of message in the enabled HL7Def.");
            }
            string   chartNum      = null;
            long     patNum        = 0;
            DateTime birthdate     = DateTime.MinValue;
            string   patLName      = null;
            string   patFName      = null;
            bool     isExistingPID = false;      //Needed to add note to hl7msg table if there isn't a PID segment in the message.

            //Get patient in question, incoming messages must have a PID segment so use that to find the pat in question
            for (int s = 0; s < hl7defmsg.hl7DefSegments.Count; s++)
            {
                if (hl7defmsg.hl7DefSegments[s].SegmentName != SegmentNameHL7.PID)
                {
                    continue;
                }
                int pidOrder = hl7defmsg.hl7DefSegments[s].ItemOrder;
                //we found the PID segment in the def, make sure it exists in the msg
                if (msg.Segments.Count <= pidOrder ||           //If the number of segments in the message is less than the item order of the PID segment
                    msg.Segments[pidOrder].GetField(0).ToString() != "PID"                      //Or if the segment we expect to be the PID segment is not the PID segment
                    )
                {
                    break;
                }
                isExistingPID = true;
                for (int f = 0; f < hl7defmsg.hl7DefSegments[s].hl7DefFields.Count; f++)           //Go through fields of PID segment and get patnum, chartnum, patient name, and/or birthdate to locate patient
                {
                    if (hl7defmsg.hl7DefSegments[s].hl7DefFields[f].FieldName == "pat.ChartNumber")
                    {
                        int chartNumOrdinal = hl7defmsg.hl7DefSegments[s].hl7DefFields[f].OrdinalPos;
                        chartNum = msg.Segments[pidOrder].Fields[chartNumOrdinal].ToString();
                    }
                    else if (hl7defmsg.hl7DefSegments[s].hl7DefFields[f].FieldName == "pat.PatNum")
                    {
                        int patNumOrdinal = hl7defmsg.hl7DefSegments[s].hl7DefFields[f].OrdinalPos;
                        patNum = PIn.Long(msg.Segments[pidOrder].Fields[patNumOrdinal].ToString());
                    }
                    else if (hl7defmsg.hl7DefSegments[s].hl7DefFields[f].FieldName == "pat.birthdateTime")
                    {
                        int patBdayOrdinal = hl7defmsg.hl7DefSegments[s].hl7DefFields[f].OrdinalPos;
                        birthdate = FieldParser.DateTimeParse(msg.Segments[pidOrder].Fields[patBdayOrdinal].ToString());
                    }
                    else if (hl7defmsg.hl7DefSegments[s].hl7DefFields[f].FieldName == "pat.nameLFM")
                    {
                        int patNameOrdinal = hl7defmsg.hl7DefSegments[s].hl7DefFields[f].OrdinalPos;
                        patLName = msg.Segments[pidOrder].GetFieldComponent(patNameOrdinal, 0);
                        patFName = msg.Segments[pidOrder].GetFieldComponent(patNameOrdinal, 1);
                    }
                }
            }
            if (!isExistingPID)
            {
                HL7MsgCur.Note = "Could not process the HL7 message due to missing PID segment.";
                HL7Msgs.Update(HL7MsgCur);
                throw new Exception("Could not process HL7 message.  Could not process the HL7 message due to missing PID segment.");
            }
            //We now have patnum, chartnum, patname, and/or birthdate so locate pat
            Patient pat    = null;
            Patient patOld = null;

            if (patNum != 0)
            {
                pat = Patients.GetPat(patNum);
            }
            if (def.InternalType != "eCWStandalone" && pat == null)
            {
                IsNewPat = true;
            }
            //In eCWstandalone integration, if we couldn't locate patient by patNum or patNum was 0 then pat will still be null so try to locate by chartNum if chartNum is not null
            if (def.InternalType == "eCWStandalone" && chartNum != null)
            {
                pat = Patients.GetPatByChartNumber(chartNum);
            }
            //In eCWstandalone integration, if pat is still null we need to try to locate patient by name and birthdate
            if (def.InternalType == "eCWStandalone" && pat == null)
            {
                long patNumByName = Patients.GetPatNumByNameAndBirthday(patLName, patFName, birthdate);
                //If patNumByName is 0 we couldn't locate by patNum, chartNum or name and birthdate so this message must be for a new patient
                if (patNumByName == 0)
                {
                    IsNewPat = true;
                }
                else
                {
                    pat             = Patients.GetPat(patNumByName);
                    patOld          = pat.Copy();
                    pat.ChartNumber = chartNum;                  //from now on, we will be able to find pat by chartNumber
                    Patients.Update(pat, patOld);
                }
            }
            if (IsNewPat)
            {
                pat = new Patient();
                if (chartNum != null)
                {
                    pat.ChartNumber = chartNum;
                }
                if (patNum != 0)
                {
                    pat.PatNum    = patNum;
                    pat.Guarantor = patNum;
                }
                pat.PriProv     = PrefC.GetLong(PrefName.PracticeDefaultProv);
                pat.BillingType = PrefC.GetLong(PrefName.PracticeDefaultBillType);
            }
            else
            {
                patOld = pat.Copy();
            }
            //Update hl7msg table with correct PatNum for this message
            HL7MsgCur.PatNum = pat.PatNum;
            HL7Msgs.Update(HL7MsgCur);
            //If this is a message that contains an SCH segment, loop through to find the AptNum.  Pass it to the other segments that will need it.
            long aptNum = 0;

            for (int s = 0; s < hl7defmsg.hl7DefSegments.Count; s++)
            {
                if (hl7defmsg.hl7DefSegments[s].SegmentName != SegmentNameHL7.SCH)
                {
                    continue;
                }
                //we found the SCH segment
                int schOrder = hl7defmsg.hl7DefSegments[s].ItemOrder;
                for (int f = 0; f < hl7defmsg.hl7DefSegments[s].hl7DefFields.Count; f++)           //Go through fields of SCH segment and get AptNum
                {
                    if (hl7defmsg.hl7DefSegments[s].hl7DefFields[f].FieldName == "apt.AptNum")
                    {
                        int aptNumOrdinal = hl7defmsg.hl7DefSegments[s].hl7DefFields[f].OrdinalPos;
                        aptNum = PIn.Long(msg.Segments[schOrder].Fields[aptNumOrdinal].ToString());
                    }
                }
            }
            //We now have a patient object , either loaded from the db or new, and aptNum so process this message for this patient
            //We need to insert the pat to get a patnum so we can compare to guar patnum to see if relationship to guar is self
            if (IsNewPat)
            {
                if (isVerboseLogging)
                {
                    EventLog.WriteEntry("OpenDentHL7", "Inserted patient: " + pat.FName + " " + pat.LName, EventLogEntryType.Information);
                }
                if (pat.PatNum == 0)
                {
                    pat.PatNum = Patients.Insert(pat, false);
                }
                else
                {
                    pat.PatNum = Patients.Insert(pat, true);
                }
                patOld = pat.Copy();
            }
            for (int i = 0; i < hl7defmsg.hl7DefSegments.Count; i++)
            {
                try {
                    SegmentHL7 seg = msg.GetSegment(hl7defmsg.hl7DefSegments[i].SegmentName, !hl7defmsg.hl7DefSegments[i].IsOptional);
                    if (seg != null)                   //null if segment was not found but is optional
                    {
                        ProcessSeg(pat, aptNum, hl7defmsg.hl7DefSegments[i], seg, msg);
                    }
                }
                catch (ApplicationException ex) {               //Required segment was missing, or other error.
                    HL7MsgCur.Note = "Could not process this HL7 message.  " + ex;
                    HL7Msgs.Update(HL7MsgCur);
                    throw new Exception("Could not process HL7 message.  " + ex);
                }
            }
            //We have processed the message so now update or insert the patient
            if (pat.FName == "" || pat.LName == "")
            {
                EventLog.WriteEntry("OpenDentHL7", "Patient demographics not processed due to missing first or last name. PatNum:" + pat.PatNum.ToString()
                                    , EventLogEntryType.Information);
                HL7MsgCur.Note = "Patient demographics not processed due to missing first or last name. PatNum:" + pat.PatNum.ToString();
                HL7Msgs.Update(HL7MsgCur);
                return;
            }
            if (IsNewPat)
            {
                if (pat.Guarantor == 0)
                {
                    pat.Guarantor = pat.PatNum;
                    Patients.Update(pat, patOld);
                }
                else
                {
                    Patients.Update(pat, patOld);
                }
            }
            else
            {
                if (isVerboseLogging)
                {
                    EventLog.WriteEntry("OpenDentHL7", "Updated patient: " + pat.FName + " " + pat.LName, EventLogEntryType.Information);
                }
                Patients.Update(pat, patOld);
            }
            HL7MsgCur.HL7Status = HL7MessageStatus.InProcessed;
            HL7Msgs.Update(HL7MsgCur);
        }