Ejemplo n.º 1
0
        /// <summary>
        /// This is the core automation routine that checks for appointments and processes each.
        /// </summary>
        private void RunAutomationCycle()
        {
            List <int> apptIDs = new List <int>();

            /***********************   get appointments to automate   ********************/
            SqlDataReader reader;
            string        CustomAutomationString = Configurator.getNodeValue("DatabaseInfo", "AUTOMATION_SQL");

            if (string.IsNullOrEmpty(CustomAutomationString) == false)
            {
                reader = BCDB2.Instance.ExecuteReader(CustomAutomationString);
            }
            else
            {
                reader = BCDB2.Instance.ExecuteReaderSPWithParams("sp_Automation", null);
            }
            try
            {
                while (reader.Read())
                {
                    if (reader.IsDBNull(0) == false)
                    {
                        apptIDs.Add(reader.GetInt32(0));
                    }
                }
            }
            catch (Exception e)
            {
                Logger.Instance.WriteToLog("Error getting appointment IDs: " + e.ToString());
            }
            finally
            {
                if (reader != null)
                {
                    reader.Close();
                }
            }

            backgroundWorker1.ReportProgress(0, apptIDs.Count.ToString() + " appointments are marked for automation - " + DateTime.Now.ToString());


            /***********************    automation cycle   ********************/
            foreach (int apptid in apptIDs)
            {
                if (backgroundWorker1.CancellationPending)
                {
                    break;
                }

                /***********************  Get Unitnum & set active patient  ********************/
                string unitnum         = "";
                ParameterCollection pc = new ParameterCollection("apptID", apptid);
                using (reader = BCDB2.Instance.ExecuteReaderSPWithParams("sp_3_GetUnitnum", pc))
                {
                    if (reader != null && reader.Read())
                    {
                        unitnum = reader.GetValue(0).ToString();
                    }
                }

                SessionManager.Instance.SetActivePatientNoCallback(unitnum, (int)apptid);

                /***********************  Risk Calculations   ********************/
                try
                {
                    backgroundWorker1.ReportProgress(1,
                                                     "Running risk calculations for appointment: " + apptid.ToString() +
                                                     ".");
                    SessionManager.Instance.GetActivePatient().RecalculateRisk();
                }
                catch (Exception e)
                {
                    backgroundWorker1.ReportProgress(1,
                                                     "Risk calculations FAILED for appointment: " + apptid.ToString() +
                                                     ". " + e.ToString());
                }

                /***********************  Update BigQueue   ********************/
                try
                {
                    backgroundWorker1.ReportProgress(1, "Updating BigQueue for appointment: " + apptid.ToString() + ".");
                    pc = new ParameterCollection("unitnum", unitnum);
                    BCDB2.Instance.RunSPWithParams("sp_3_populateBigQueue", pc);
                }
                catch (Exception e)
                {
                    backgroundWorker1.ReportProgress(1,
                                                     "Updating BigQueue FAILED for appointment: " + apptid.ToString() +
                                                     ". " + e.ToString());
                }

                /***********************  Process Queue Documents    ********************/
                backgroundWorker1.ReportProgress(1,
                                                 "Processing Queue Documents for appointment: " + apptid.ToString() + ".");
                pc = new ParameterCollection("apptID", apptid);
                BCDB2.Instance.RunSPWithParams("sp_processQueueDocuments", pc);

                /***********************  Run Automation Stored Procedures    ********************/
                backgroundWorker1.ReportProgress(1,
                                                 "Run Automation Stored Procedures  : " + apptid.ToString() + ".");

                RiskAppCore.ApptUtils.runAutomationStoredProcedures(apptid);

                /***********************  export HL7 files    ********************/
                backgroundWorker1.ReportProgress(1,
                                                 "Export HL7 File: " + apptid.ToString() + ".");
                RiskModels.RiskService.exportHL7File(apptid);

                /***********************  Process Batch Print Queue Documents    ********************/
                backgroundWorker1.ReportProgress(1,
                                                 "Processing batch print queue documents for appointment: " + apptid.ToString() + "."); // Make batch + printing a single progress report ???
                RiskAppCore.ApptUtils.saveAutomationDocumentsToPrintQueue(apptid);

                /***********************  Print Documents   ********************/
                string print_report = "";
                try
                {
                    backgroundWorker1.ReportProgress(1, "Printing documents for appointment: " + apptid.ToString() + ".");  // Make batch + printing a single progress report ???
                    ParameterCollection printDocArgs = new ParameterCollection();
                    printDocArgs.Add("apptid", apptid);
                    reader = BCDB2.Instance.ExecuteReaderSPWithParams("sp_AutomationHtmlDocsToPrint", printDocArgs);

                    while (reader.Read())
                    {
                        int    templateID   = -1;
                        string printer      = "";
                        string documentType = "";
                        string saveLocation = "";

                        if (reader.IsDBNull(0) == false)
                        {
                            templateID = reader.GetInt32(0);
                        }
                        if (reader.IsDBNull(1) == false)
                        {
                            printer = reader.GetString(1);
                        }
                        if (reader.IsDBNull(2) == false)
                        {
                            documentType = reader.GetString(2);
                        }
                        if (reader.IsDBNull(3) == false)
                        {
                            saveLocation = reader.GetString(3);
                        }

                        ///////////////////////////////////////////////////
                        // documentType defaults to HTML in V3 Automation //
                        ///////////////////////////////////////////////////
                        if (string.IsNullOrEmpty(documentType))
                        {
                            documentType = "HTML";
                        }
                        RiskApps3.Model.Clinic.Letters.PrintUtils printUtils = new RiskApps3.Model.Clinic.Letters.PrintUtils(printer);

                        if (printer.ToLower().StartsWith("save as"))
                        {
                            if (string.IsNullOrEmpty(saveLocation) == false)
                            {
                                if (documentType.ToUpper() == "HTML")
                                {
                                    if (string.Compare(printer, "save as pdf", true) == 0)
                                    {
                                        if (printUtils.savePdfDoc(apptid, unitnum, templateID, saveLocation) == true)
                                        {
                                            print_report += "Saved document template " + templateID.ToString() + " in " + saveLocation + "<br/>";
                                        }
                                    }
                                    else
                                    {
                                        if (printUtils.saveHtmlDoc(apptid, unitnum, templateID, saveLocation) == true)
                                        {
                                            print_report += "Saved document template " + templateID.ToString() + " in " + saveLocation + "<br/>";
                                        }
                                    }
                                }
                                else if (documentType.ToUpper() == "WORD")
                                {
                                    if (printUtils.saveWordDoc(apptid, unitnum, templateID, saveLocation) == true)
                                    {
                                        print_report += "Saved document template " + templateID.ToString() + " in " + saveLocation + "<br/>";
                                    }
                                }
                            }
                        }
                        else if (printer == "BATCH")
                        {
                            pc = new ParameterCollection();
                            pc.Add("apptid", apptid);
                            pc.Add("dateTime", DateTime.Now);
                            pc.Add("templateID", templateID);
                            pc.Add("unitnum", unitnum);
                            pc.Add("documentType", documentType);

                            string sqlStr =
                                "INSERT INTO tblPrintQueue (apptID, created, documentTemplateID, unitnum, automationPrint, automationSave, documentType) VALUES (@apptid, @dateTime, @templateID, @unitnum, 0, 0, @documentType)";
                            int rowsAffected = BCDB2.Instance.ExecuteNonQueryWithParams(sqlStr, pc);

                            print_report += "Sent document template " + templateID.ToString() + " to the batch print queue." + "<br/>";
                            backgroundWorker1.ReportProgress(2, "Appt " + apptid.ToString() + ":<br/>" + print_report);
                        }
                        else if (printer == "")
                        {
                        }
                        else if (string.IsNullOrEmpty(printer) == false)
                        {
                            if (documentType.ToUpper() == "WORD")
                            {
                                if (printUtils.printWordDoc(apptid, unitnum, templateID))
                                {
                                    print_report += "Sent document template " + templateID.ToString() + " to printer: " + printUtils.getPrinter() + "<br/>";
                                    pc            = new ParameterCollection();
                                    pc.Add("apptid", apptid);
                                    pc.Add("templateID", templateID);
                                    pc.Add("dateTime", DateTime.Now);

                                    string sqlStr = "INSERT INTO tblDocuments([apptID],[documentTemplateID],[created],[createdBy]) VALUES(@apptid, @templateID, @dateTime, 'AUTOMATION');";
                                    BCDB2.Instance.ExecuteNonQueryWithParams(sqlStr, pc);
                                }
                                else
                                {
                                    print_report += "Failed sending document template " + templateID.ToString() + " to printer: " + printUtils.getPrinter() + "<br/>";
                                }

                                backgroundWorker1.ReportProgress(2, "Appt " + apptid.ToString() + ":<br/>" + print_report);
                            }
                            else if (documentType.ToUpper() == "HTML")
                            {
                                HraHtmlDocument hdoc;

                                if ((hdoc = printUtils.printHtmlDoc(apptid, unitnum, templateID)) != null)
                                {
                                    print_report += "Sent document template " + templateID.ToString() + " to printer: " + hdoc.targetPrinter + "<br/>";

                                    pc = new ParameterCollection();
                                    pc.Add("apptid", hdoc.apptid);
                                    pc.Add("templateID", hdoc.template.documentTemplateID);
                                    pc.Add("dateTime", DateTime.Now);

                                    string sqlStr       = "INSERT INTO tblDocuments([apptID],[documentTemplateID],[created],[createdBy]) VALUES(@apptid, @templateID, @dateTime, 'AUTOMATION');";
                                    int    rowsAffected = BCDB2.Instance.ExecuteNonQueryWithParams(sqlStr, pc);

                                    backgroundWorker1.ReportProgress(2, hdoc.template.htmlText + Environment.NewLine + "Appt " + apptid.ToString() + ":<br/>" + print_report);
                                }
                                else
                                {
                                    print_report += "Failed to send document template " + templateID.ToString() + " to printer: " + printUtils.getPrinter() + "<br/>";
                                    backgroundWorker1.ReportProgress(2, "Appt " + apptid.ToString() + ":<br/>" + print_report);
                                }
                            }
                            else if (documentType != "HTML" && documentType != "WORD")
                            {
                                print_report += "Could not process document: templateID " + templateID.ToString() + ", printer: " + printer + ", UNKOWN DOCUMENT TYPE: \"" + documentType + "\"<br/>";
                                backgroundWorker1.ReportProgress(2, "Appt " + apptid.ToString() + ":<br/>" + print_report);
                            }
                        }
                    }
                }
                catch (Exception e)
                {
                    backgroundWorker1.ReportProgress(1,
                                                     "Printing FAILED for appointment: " + apptid.ToString() + ". " +
                                                     e.ToString());
                }
                finally
                {
                    if (reader != null)
                    {
                        reader.Close();
                    }
                }

                /***********************  Mark as printed to complete automation   ********************/
                try
                {
                    pc = new ParameterCollection("apptid", apptid);
                    String sqlStr       = "UPDATE tblRiskData SET printed = 1 WHERE apptid = @apptid";
                    int    rowsAffected = BCDB2.Instance.ExecuteNonQueryWithParams(sqlStr, pc);
                }
                catch (Exception e)
                {
                    backgroundWorker1.ReportProgress(1,
                                                     ("Automation - unable to update appt id=" + apptid.ToString() +
                                                      " printed=1.\n" + e.ToString()));
                }

                if (status != null)
                {
                    try
                    {
                        status.LogStatus(idString,
                                         "Automation",
                                         "Ok",
                                         ("Appt " + apptid + " " + print_report).Trim());
                    }
                    catch (System.Web.Services.Protocols.SoapException e)
                    {
                        //do nothing
                    }
                    catch (Exception ex)
                    {
                        Logger.Instance.WriteToLog(ex.ToString());
                    }
                }
            }
        }
Ejemplo n.º 2
0
        private void printSelectedBackgroundWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            if (printSelectedBackgroundWorker.CancellationPending)
            {
                e.Cancel = true;
                return;
            }
            float i     = 0;
            float count = selectedList.Count;

            foreach (PrintQueueEntry pqe in selectedList)
            {
                if (printSelectedBackgroundWorker.CancellationPending)
                {
                    e.Cancel = true;
                    return;
                }
                int percent = (int)((++i * 100.0f) / count);

                int    apptID             = -1;
                int    documentTemplateID = -1;
                int    ID      = -1;
                string unitnum = "";

                apptID             = pqe.apptID;
                documentTemplateID = pqe.documentTemplateID;
                ID      = pqe.ID;
                unitnum = pqe.unitnum;

                if (string.IsNullOrEmpty(unitnum) || apptID < 1 || ID < 1 || documentTemplateID < 1)
                {
                    string mrn = string.IsNullOrEmpty(unitnum) ? "" : unitnum;
                    Logger.Instance.WriteToLog("RiskApps3 PrintQueueForm: Cannot print document { unitnum=" + mrn + ", apptID=" + apptID.ToString() + ", documentTemplateID=" + documentTemplateID.ToString() + " }");
                    printSelectedBackgroundWorker.ReportProgress(percent,
                                                                 "RiskApps3 PrintQueueForm: Cannot print document { unitnum=" + mrn + ", apptID=" + apptID.ToString() + ", documentTemplateID=" + documentTemplateID.ToString() + " }");
                    continue;
                }
                bool printFailed = false;
                Model.Clinic.Letters.PrintUtils printUtils = new Model.Clinic.Letters.PrintUtils();

                ////////////////////////////////////////////////////////
                // documentType defaults to WORD in V3 Batch Printing //
                ////////////////////////////////////////////////////////
                if (string.IsNullOrEmpty(pqe.docType))
                {
                    pqe.docType = "WORD";
                }

                if (pqe.docType.ToUpper() == "HTML")
                {
                    if (printUtils.printHtmlDoc(apptID, unitnum, documentTemplateID) == null)
                    {
                        printFailed = true;
                        string mrn = string.IsNullOrEmpty(unitnum) ? "" : unitnum;
                        Logger.Instance.WriteToLog("RiskApps3 PrintQueueForm: Printing failed for { unitnum=" + mrn + ", apptID=" + apptID.ToString() + ", documentTemplateID=" + documentTemplateID.ToString() + " }");
                        printSelectedBackgroundWorker.ReportProgress(percent,
                                                                     "RiskApps3 PrintQueueForm: HTML printing failed for { unitnum=" + mrn + ", apptID=" + apptID.ToString() + ", documentTemplateID=" + documentTemplateID.ToString() + " }");
                    }
                }
                else if (pqe.docType.ToUpper() == "WORD")
                {
                    if (printUtils.printWordDoc(apptID, unitnum, documentTemplateID) == false)
                    {
                        printFailed = true;
                        string mrn = string.IsNullOrEmpty(unitnum) ? "" : unitnum;
                        Logger.Instance.WriteToLog("RiskApps3 PrintQueueForm: Printing failed for { unitnum=" + mrn + ", apptID=" + apptID.ToString() + ", documentTemplateID=" + documentTemplateID.ToString() + " }");
                        printSelectedBackgroundWorker.ReportProgress(percent,
                                                                     "RiskApps3 PrintQueueForm: Word printing failed for { unitnum=" + mrn + ", apptID=" + apptID.ToString() + ", documentTemplateID=" + documentTemplateID.ToString() + " }");
                    }
                }
                if (printFailed == false)
                {
                    ParameterCollection pc = new ParameterCollection();
                    pc.Add("apptID", apptID);
                    pc.Add("documentTemplateID", documentTemplateID);
                    pc.Add("dateTime", DateTime.Now);
                    string sqlStr = "INSERT INTO tblDocuments([apptID],[documentTemplateID],[created],[createdBy]) VALUES(@apptID, @documentTemplateID, @dateTime, 'BatchPrint');";

                    BCDB2.Instance.ExecuteNonQueryWithParams(sqlStr, pc);

                    pc.Clear();
                    pc.Add("ID", ID);
                    sqlStr = "UPDATE tblPrintQueue SET printed='" + DateTime.Now + "' WHERE ID = @ID;";
                    BCDB2.Instance.ExecuteNonQueryWithParams(sqlStr, pc);

                    printSelectedBackgroundWorker.ReportProgress(percent, "Printed document: template " + documentTemplateID.ToString() + "\r\nFor appt " + apptID.ToString() + " (MRN: " + unitnum + ")");
                }
                else
                {
                    printSelectedBackgroundWorker.ReportProgress(percent, "Failed to print document: template " + documentTemplateID.ToString() + "\r\nFor appt " + apptID.ToString() + " (MRN: " + unitnum + ")");
                }
                //Thread.Sleep(1000);
            }
        }
Ejemplo n.º 3
0
        private void printSelectedBackgroundWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            if (printSelectedBackgroundWorker.CancellationPending)
            {
                e.Cancel = true;
                return;
            }
            float i = 0;
            float count = selectedList.Count;
            foreach (PrintQueueEntry pqe in selectedList)
            {
                if (printSelectedBackgroundWorker.CancellationPending)
                {
                    e.Cancel = true;
                    return;
                }
                int percent = (int)((++i * 100.0f) / count);

                int apptID = -1;
                int documentTemplateID = -1;
                int ID = -1;
                string unitnum = "";

                apptID = pqe.apptID;
                documentTemplateID = pqe.documentTemplateID;
                ID = pqe.ID;
                unitnum = pqe.unitnum;

                if (string.IsNullOrEmpty(unitnum) || apptID < 1 || ID < 1 || documentTemplateID < 1)
                {
                    string mrn = string.IsNullOrEmpty(unitnum) ? "" : unitnum;
                    Logger.Instance.WriteToLog("RiskApps3 PrintQueueForm: Cannot print document { unitnum=" + mrn + ", apptID=" + apptID.ToString() + ", documentTemplateID=" + documentTemplateID.ToString() + " }");
                    printSelectedBackgroundWorker.ReportProgress(percent,
                            "RiskApps3 PrintQueueForm: Cannot print document { unitnum=" + mrn + ", apptID=" + apptID.ToString() + ", documentTemplateID=" + documentTemplateID.ToString() + " }");
                    continue;
                }
                bool printFailed = false;
                Model.Clinic.Letters.PrintUtils printUtils = new Model.Clinic.Letters.PrintUtils();

                ////////////////////////////////////////////////////////
                // documentType defaults to WORD in V3 Batch Printing //
                ////////////////////////////////////////////////////////
                if (string.IsNullOrEmpty(pqe.docType))
                {
                    pqe.docType = "WORD";
                }

                if (pqe.docType.ToUpper() == "HTML")
                {
                    if (printUtils.printHtmlDoc(apptID, unitnum, documentTemplateID) == null)
                    {
                        printFailed = true;
                        string mrn = string.IsNullOrEmpty(unitnum) ? "" : unitnum;
                        Logger.Instance.WriteToLog("RiskApps3 PrintQueueForm: Printing failed for { unitnum=" + mrn + ", apptID=" + apptID.ToString() + ", documentTemplateID=" + documentTemplateID.ToString() + " }");
                        printSelectedBackgroundWorker.ReportProgress(percent,
                            "RiskApps3 PrintQueueForm: HTML printing failed for { unitnum=" + mrn + ", apptID=" + apptID.ToString() + ", documentTemplateID=" + documentTemplateID.ToString() + " }");
                    }
                }
                else if (pqe.docType.ToUpper() == "WORD")
                {
                    if (printUtils.printWordDoc(apptID, unitnum, documentTemplateID) == false)
                    {
                        printFailed = true;
                        string mrn = string.IsNullOrEmpty(unitnum) ? "" : unitnum;
                        Logger.Instance.WriteToLog("RiskApps3 PrintQueueForm: Printing failed for { unitnum=" + mrn + ", apptID=" + apptID.ToString() + ", documentTemplateID=" + documentTemplateID.ToString() + " }");
                        printSelectedBackgroundWorker.ReportProgress(percent,
                            "RiskApps3 PrintQueueForm: Word printing failed for { unitnum=" + mrn + ", apptID=" + apptID.ToString() + ", documentTemplateID=" + documentTemplateID.ToString() + " }");
                    }
                }
                if (printFailed == false)
                {
                    ParameterCollection pc = new ParameterCollection();
                    pc.Add("apptID", apptID);
                    pc.Add("documentTemplateID", documentTemplateID);
                    pc.Add("dateTime", DateTime.Now);
                    string sqlStr = "INSERT INTO tblDocuments([apptID],[documentTemplateID],[created],[createdBy]) VALUES(@apptID, @documentTemplateID, @dateTime, 'BatchPrint');";

                    BCDB2.Instance.ExecuteNonQueryWithParams(sqlStr, pc);

                    pc.Clear();
                    pc.Add("ID", ID);
                    sqlStr = "UPDATE tblPrintQueue SET printed='" + DateTime.Now + "' WHERE ID = @ID;";
                    BCDB2.Instance.ExecuteNonQueryWithParams(sqlStr, pc);

                    printSelectedBackgroundWorker.ReportProgress(percent, "Printed document: template " + documentTemplateID.ToString() + "\r\nFor appt " + apptID.ToString() + " (MRN: " + unitnum + ")");
                }
                else
                {
                    printSelectedBackgroundWorker.ReportProgress(percent, "Failed to print document: template " + documentTemplateID.ToString() + "\r\nFor appt " + apptID.ToString() + " (MRN: " + unitnum + ")");
                }
                //Thread.Sleep(1000);
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// This is the core automation routine that checks for appointments and processes each.
        /// </summary>
        private void RunAutomationCycle()
        {
            List<int> apptIDs = new List<int>();

            /***********************   get appointments to automate   ********************/
            SqlDataReader reader;
            string CustomAutomationString = Configurator.getNodeValue("DatabaseInfo", "AUTOMATION_SQL");
            if (string.IsNullOrEmpty(CustomAutomationString) == false)
            {
                reader = BCDB2.Instance.ExecuteReader(CustomAutomationString);
            }
            else
            {
                reader = BCDB2.Instance.ExecuteReaderSPWithParams("sp_Automation", null);
            }
            try
            {
                while (reader.Read())
                {
                    if (reader.IsDBNull(0) == false)
                        apptIDs.Add(reader.GetInt32(0));
                }
            }
            catch (Exception e)
            {
                Logger.Instance.WriteToLog("Error getting appointment IDs: " + e.ToString());
            }
            finally
            {
                if (reader != null)
                    reader.Close();
            }

            backgroundWorker1.ReportProgress(0, apptIDs.Count.ToString() + " appointments are marked for automation - " + DateTime.Now.ToString());

            /***********************    automation cycle   ********************/
            foreach (int apptid in apptIDs)
            {
                if (backgroundWorker1.CancellationPending)
                    break;

                /***********************  Get Unitnum & set active patient  ********************/
                string unitnum = "";
                ParameterCollection pc = new ParameterCollection("apptID", apptid);
                using (reader = BCDB2.Instance.ExecuteReaderSPWithParams("sp_3_GetUnitnum", pc))
                {
                    if (reader != null && reader.Read())
                    {
                        unitnum = reader.GetValue(0).ToString();
                    }
                }

                SessionManager.Instance.SetActivePatientNoCallback(unitnum, (int)apptid);

                /***********************  Risk Calculations   ********************/
                try
                {
                    backgroundWorker1.ReportProgress(1,
                                                    "Running risk calculations for appointment: " + apptid.ToString() +
                                                    ".");
                    SessionManager.Instance.GetActivePatient().RecalculateRisk();
                }
                catch (Exception e)
                {
                    backgroundWorker1.ReportProgress(1,
                                                    "Risk calculations FAILED for appointment: " + apptid.ToString() +
                                                    ". " + e.ToString());
                }

                /***********************  Update BigQueue   ********************/
                try
                {
                    backgroundWorker1.ReportProgress(1, "Updating BigQueue for appointment: " + apptid.ToString() + ".");
                    pc = new ParameterCollection("unitnum", unitnum);
                    BCDB2.Instance.RunSPWithParams("sp_3_populateBigQueue", pc);
                }
                catch (Exception e)
                {
                    backgroundWorker1.ReportProgress(1,
                                                    "Updating BigQueue FAILED for appointment: " + apptid.ToString() +
                                                    ". " + e.ToString());
                }

                /***********************  Process Queue Documents    ********************/
                backgroundWorker1.ReportProgress(1,
                                                "Processing Queue Documents for appointment: " + apptid.ToString() + ".");
                pc = new ParameterCollection("apptID", apptid);
                BCDB2.Instance.RunSPWithParams("sp_processQueueDocuments", pc);

                /***********************  Run Automation Stored Procedures    ********************/
                backgroundWorker1.ReportProgress(1,
                                              "Run Automation Stored Procedures  : " + apptid.ToString() + ".");

                RiskAppCore.ApptUtils.runAutomationStoredProcedures(apptid);

                /***********************  export HL7 files    ********************/
                backgroundWorker1.ReportProgress(1,
                                              "Export HL7 File: " + apptid.ToString() + ".");
                RiskModels.RiskService.exportHL7File(apptid);

                /***********************  Process Batch Print Queue Documents    ********************/
                backgroundWorker1.ReportProgress(1,
                                              "Processing batch print queue documents for appointment: " + apptid.ToString() + "."); // Make batch + printing a single progress report ???
                RiskAppCore.ApptUtils.saveAutomationDocumentsToPrintQueue(apptid);

                /***********************  Print Documents   ********************/
                string print_report = "";
                try
                {
                    backgroundWorker1.ReportProgress(1, "Printing documents for appointment: " + apptid.ToString() + ".");  // Make batch + printing a single progress report ???
                    ParameterCollection printDocArgs = new ParameterCollection();
                    printDocArgs.Add("apptid", apptid);
                    reader = BCDB2.Instance.ExecuteReaderSPWithParams("sp_AutomationHtmlDocsToPrint", printDocArgs);

                    while (reader.Read())
                    {
                        int templateID = -1;
                        string printer = "";
                        string documentType = "";
                        string saveLocation = "";

                        if (reader.IsDBNull(0) == false)
                        {
                            templateID = reader.GetInt32(0);
                        }
                        if (reader.IsDBNull(1) == false)
                        {
                            printer = reader.GetString(1);
                        }
                        if (reader.IsDBNull(2) == false)
                        {
                            documentType = reader.GetString(2);
                        }
                        if (reader.IsDBNull(3) == false)
                        {
                            saveLocation = reader.GetString(3);
                        }

                        ///////////////////////////////////////////////////
                        // documentType defaults to HTML in V3 Automation //
                        ///////////////////////////////////////////////////
                        if (string.IsNullOrEmpty(documentType))
                        {
                            documentType = "HTML";
                        }
                        RiskApps3.Model.Clinic.Letters.PrintUtils printUtils = new RiskApps3.Model.Clinic.Letters.PrintUtils(printer);

                        if (printer.ToLower().StartsWith("save as"))
                        {
                            if (string.IsNullOrEmpty(saveLocation) == false)
                            {
                                if (documentType.ToUpper() == "HTML")
                                {
                                    if (string.Compare(printer, "save as pdf", true) == 0)
                                    {
                                        if (printUtils.savePdfDoc(apptid, unitnum, templateID, saveLocation) == true)
                                        {
                                            print_report += "Saved document template " + templateID.ToString() + " in " + saveLocation + "<br/>";
                                        }

                                    }
                                    else
                                    {
                                        if (printUtils.saveHtmlDoc(apptid, unitnum, templateID, saveLocation) == true)
                                        {
                                            print_report += "Saved document template " + templateID.ToString() + " in " + saveLocation + "<br/>";
                                        }
                                    }
                                }
                                else if (documentType.ToUpper() == "WORD")
                                {
                                    if (printUtils.saveWordDoc(apptid, unitnum, templateID, saveLocation) == true)
                                    {
                                        print_report += "Saved document template " + templateID.ToString() + " in " + saveLocation + "<br/>";
                                    }
                                }
                            }
                        }
                        else if (printer == "BATCH")
                        {
                            pc = new ParameterCollection();
                            pc.Add("apptid", apptid);
                            pc.Add("dateTime", DateTime.Now);
                            pc.Add("templateID", templateID);
                            pc.Add("unitnum", unitnum);
                            pc.Add("documentType", documentType);

                            string sqlStr =
                                "INSERT INTO tblPrintQueue (apptID, created, documentTemplateID, unitnum, automationPrint, automationSave, documentType) VALUES (@apptid, @dateTime, @templateID, @unitnum, 0, 0, @documentType)";
                            int rowsAffected = BCDB2.Instance.ExecuteNonQueryWithParams(sqlStr, pc);

                            print_report += "Sent document template " + templateID.ToString() + " to the batch print queue." + "<br/>";
                            backgroundWorker1.ReportProgress(2, "Appt " + apptid.ToString() + ":<br/>" + print_report);
                        }
                        else if (printer == "")
                        {
                        }
                        else if (string.IsNullOrEmpty(printer) == false)
                        {
                            if (documentType.ToUpper() == "WORD")
                            {
                                if (printUtils.printWordDoc(apptid, unitnum, templateID))
                                {
                                    print_report += "Sent document template " + templateID.ToString() + " to printer: " + printUtils.getPrinter() + "<br/>";
                                    pc = new ParameterCollection();
                                    pc.Add("apptid", apptid);
                                    pc.Add("templateID", templateID);
                                    pc.Add("dateTime", DateTime.Now);

                                    string sqlStr = "INSERT INTO tblDocuments([apptID],[documentTemplateID],[created],[createdBy]) VALUES(@apptid, @templateID, @dateTime, 'AUTOMATION');";
                                    BCDB2.Instance.ExecuteNonQueryWithParams(sqlStr, pc);
                                }
                                else
                                {
                                    print_report += "Failed sending document template " + templateID.ToString() + " to printer: " + printUtils.getPrinter() + "<br/>";
                                }

                                backgroundWorker1.ReportProgress(2, "Appt " + apptid.ToString() + ":<br/>" + print_report);
                            }
                            else if (documentType.ToUpper() == "HTML")
                            {
                                HraHtmlDocument hdoc;

                                if ((hdoc = printUtils.printHtmlDoc(apptid, unitnum, templateID)) != null)
                                {
                                    print_report += "Sent document template " + templateID.ToString() + " to printer: " + hdoc.targetPrinter + "<br/>";

                                    pc = new ParameterCollection();
                                    pc.Add("apptid", hdoc.apptid);
                                    pc.Add("templateID", hdoc.template.documentTemplateID);
                                    pc.Add("dateTime", DateTime.Now);

                                    string sqlStr = "INSERT INTO tblDocuments([apptID],[documentTemplateID],[created],[createdBy]) VALUES(@apptid, @templateID, @dateTime, 'AUTOMATION');";
                                    int rowsAffected = BCDB2.Instance.ExecuteNonQueryWithParams(sqlStr, pc);

                                    backgroundWorker1.ReportProgress(2, hdoc.template.htmlText + Environment.NewLine + "Appt " + apptid.ToString() + ":<br/>" + print_report);
                                }
                                else
                                {
                                    print_report += "Failed to send document template " + templateID.ToString() + " to printer: " + printUtils.getPrinter() + "<br/>";
                                    backgroundWorker1.ReportProgress(2, "Appt " + apptid.ToString() + ":<br/>" + print_report);
                                }
                            }
                            else if (documentType != "HTML" && documentType != "WORD")
                            {
                                print_report += "Could not process document: templateID " + templateID.ToString() + ", printer: " + printer + ", UNKOWN DOCUMENT TYPE: \"" + documentType + "\"<br/>";
                                backgroundWorker1.ReportProgress(2, "Appt " + apptid.ToString() + ":<br/>" + print_report);
                            }
                        }
                    }
                }
                catch (Exception e)
                {
                    backgroundWorker1.ReportProgress(1,
                                                    "Printing FAILED for appointment: " + apptid.ToString() + ". " +
                                                    e.ToString());
                }
                finally
                {
                    if (reader != null)
                        reader.Close();
                }

                /***********************  Mark as printed to complete automation   ********************/
                try
                {
                    pc = new ParameterCollection("apptid", apptid);
                    String sqlStr = "UPDATE tblRiskData SET printed = 1 WHERE apptid = @apptid";
                    int rowsAffected = BCDB2.Instance.ExecuteNonQueryWithParams(sqlStr, pc);
                }
                catch (Exception e)
                {
                    backgroundWorker1.ReportProgress(1,
                                                    ("Automation - unable to update appt id=" + apptid.ToString() +
                                                     " printed=1.\n" + e.ToString()));
                }

                if (status != null)
                {
                    try
                    {
                        status.LogStatus(idString,
                                         "Automation",
                                         "Ok",
                                         ("Appt " + apptid + " " + print_report).Trim());
                    }
                    catch (System.Web.Services.Protocols.SoapException e)
                    {
                        //do nothing
                    }
                    catch (Exception ex)
                    {
                        Logger.Instance.WriteToLog(ex.ToString());
                    }
                }
            }
        }