private void RunAutomatedFaxesForConnecticutOrtho()
        {
            faxRepository = new FaxRepository();
            createLetters = new AutomatedFaxBL.Letters.CreateLetters();

            documentsList = faxRepository.GetListOfDocumentssToSendViaFax();
            faxRepository.InsertSignedDocumentsFromGreenway(documentsList);
            faxTelephoneNumberList = new List<FaxTelephoneNumber>();
            faxTelephoneNumber = new FaxTelephoneNumber();
            sendFaxesReporting = new SendFaxesReporting();
            string faxPathWithCoverLetterAndNote = string.Empty;
            
            for (int i = 0; i < documentsList.Count; i++)
            {
                if (documentsList[i].Proccess == "Y")
                {
                    if (faxRepository.CheckToSeeIfProviderThatSignedDocumentIsCareProvider(documentsList[i].CareProviderID,
                        documentsList[i].SignerID))
                    {
                        if (GetDocumentFromGreenway(documentsList[i].DocumentID))
                        {
                            sendFaxesReporting = new SendFaxesReporting();

                            faxRepository.InsertAllSignedNotes(documentsList[i]);

                            sendFaxesReporting = new SendFaxesReporting();
                            sendFaxesReporting.DocumentID = documentsList[i].DocumentID;
                            sendFaxesReporting.CareProviderID = documentsList[i].CareProviderID;
                            sendFaxesReporting.CareProviderName = documentsList[i].CareProviderName;
                            sendFaxesReporting.PatientID = documentsList[i].PatientID;
                            sendFaxesReporting.PatientName = documentsList[i].PatientName;
                            sendFaxesReporting.PrimaryCareProviderID = documentsList[i].PrimaryCareProviderID;
                            sendFaxesReporting.PrimaryCareProvider = documentsList[i].PrimaryCareProvider;
                            sendFaxesReporting.ReferringCareProviderID = documentsList[i].ReferringCareProviderID;
                            sendFaxesReporting.ReferringCareProvider = documentsList[i].ReferringCareProvider;

                            faxTelephoneNumber = faxRepository.GetFaxNumbers(documentsList[i].PrimaryCareProviderID.ToString());

                            if (documentsList[i].PrimaryCareProviderID > 0)
                            {
                                if (faxTelephoneNumber.FaxNumber.Length > 0 && faxRepository.CheckToseeIfProviderWorksForPractice(faxTelephoneNumber.ProviderID) == false)
                                {
                                    faxPathWithCoverLetterAndNote = string.Empty;

                                    if (documentsList[i].DocTypeName == "History and Physical")
                                    {
                                        faxPathWithCoverLetterAndNote = createLetters.CreateLetterToPutInFrontOfNote(faxTelephoneNumber, documentsList[i]);
                                    }
                                    else if (documentsList[i].DocTypeName == "Progress Note")
                                    {
                                        string newDocumentName = Guid.NewGuid().ToString() + ".pdf";
                                        faxPathWithCoverLetterAndNote = ConfigurationValues.FinalFaxPath + newDocumentName;
                                        File.Copy(ConfigurationValues.CreatePdfPath, ConfigurationValues.FinalFaxPath + newDocumentName);
                                    }

                                    if (Utility.GetPDFPageCount(faxPathWithCoverLetterAndNote) < 15)
                                    {
                                        sendFaxesReporting.PrimaryCareFaxNumber = faxTelephoneNumber.FaxNumber;
                                        sendFaxesReporting.PrimaryCareFaxSent = "Y";
                                        sendFaxesReporting.PrimaryCareReason = "Approved Provider and has fax number";

                                        OperationResult operationResult = CreateFaxRecord(documentsList[i].CareProviderName,
                                            faxRepository.UpdatedFaxNumber(faxTelephoneNumber.FaxNumber),
                                            faxTelephoneNumber.Name, faxPathWithCoverLetterAndNote);

                                        sendFaxesReporting.PrimaryCareSendID = int.Parse(operationResult.MessageList[0]);

                                        if (operationResult.Success)
                                        {
                                            AutomatedFaxBL.Multitech.SendFaxMultitech sendFax = new AutomatedFaxBL.Multitech.SendFaxMultitech();

                                            faxTelephoneNumber.FaxNumber = faxTelephoneNumber.FaxNumber.Replace("(", "")
                                                .Replace(")", "")
                                                .Replace("-", "")
                                                .Replace(" ", "");
                                            sendFax.SendTheFax(documentsList[i].PatientName, faxPathWithCoverLetterAndNote, faxTelephoneNumber.Name, faxRepository.UpdatedFaxNumber(faxTelephoneNumber.FaxNumber), operationResult.MessageList[0], "");
                                        }
                                    }
                                    else
                                    {
                                        sendFaxesReporting.PrimaryCareFaxSent = "N";
                                        sendFaxesReporting.PrimaryCareFaxNumber = faxTelephoneNumber.FaxNumber;
                                        sendFaxesReporting.PrimaryCareReason = "Number of pages greater than 14";
                                    }
                                }
                                else
                                {
                                    whyFaxWasNotSent = string.Empty;
                                    if (faxTelephoneNumber.FaxNumber.Length < 1 && faxRepository.CheckToseeIfProviderWorksForPractice(faxTelephoneNumber.ProviderID))
                                    {
                                        whyFaxWasNotSent = "No Fax Number for Provider and Primary Care Provider is in the Practice";
                                    }
                                    else if (faxTelephoneNumber.FaxNumber.Length < 1)
                                    {
                                        whyFaxWasNotSent = "No Fax Number for Provider";
                                    }
                                    else if (faxRepository.CheckToseeIfProviderWorksForPractice(faxTelephoneNumber.ProviderID))
                                    {
                                        whyFaxWasNotSent = "Primary Care Provider is in the Practice";
                                    }

                                    sendFaxesReporting.PrimaryCareFaxSent = "N";
                                    sendFaxesReporting.PrimaryCareFaxNumber = faxTelephoneNumber.FaxNumber;
                                    sendFaxesReporting.PrimaryCareReason = whyFaxWasNotSent;
                                }
                            }
                            else
                            {
                                sendFaxesReporting.PrimaryCareFaxSent = "N";
                                sendFaxesReporting.PrimaryCareFaxNumber = faxTelephoneNumber.FaxNumber;
                                sendFaxesReporting.PrimaryCareReason = "No Primary Care Provider";
                            }

                            faxTelephoneNumber = faxRepository.GetFaxNumbers(documentsList[i].ReferringCareProviderID.ToString());

                            if (documentsList[i].ReferringCareProviderID == documentsList[i].PrimaryCareProviderID)
                            {
                                sendFaxesReporting.ReferringCareFaxSent = "N";
                                sendFaxesReporting.ReferringCareFaxNumber = faxTelephoneNumber.FaxNumber;
                                sendFaxesReporting.ReferringCareReason = "Primary Care Provider and Referring Care Provider are the same";
                            }
                            else if (documentsList[i].ReferringCareProviderID > 0)
                            {
                                if (faxTelephoneNumber.FaxNumber.Length > 0 && faxRepository.CheckToseeIfProviderWorksForPractice(faxTelephoneNumber.ProviderID) == false)
                                {
                                    faxPathWithCoverLetterAndNote = string.Empty;

                                    if (documentsList[i].DocTypeName == "History and Physical")
                                    {
                                        faxPathWithCoverLetterAndNote = createLetters.CreateLetterToPutInFrontOfNote(faxTelephoneNumber, documentsList[i]);
                                    }
                                    else if (documentsList[i].DocTypeName == "Progress Note")
                                    {
                                        string newDocumentName = Guid.NewGuid().ToString() + ".pdf";
                                        faxPathWithCoverLetterAndNote = ConfigurationValues.FinalFaxPath + newDocumentName;
                                        File.Copy(ConfigurationValues.CreatePdfPath, ConfigurationValues.FinalFaxPath + newDocumentName);
                                    }
                                    if (Utility.GetPDFPageCount(faxPathWithCoverLetterAndNote) < 15)
                                    {
                                        sendFaxesReporting.ReferringCareFaxSent = "Y";
                                        sendFaxesReporting.ReferringCareFaxNumber = faxTelephoneNumber.FaxNumber;
                                        sendFaxesReporting.ReferringCareReason = "Approved Provider and has fax number";

                                        OperationResult operationResult = CreateFaxRecord(documentsList[i].CareProviderName,
                                            faxRepository.UpdatedFaxNumber(faxTelephoneNumber.FaxNumber),
                                            faxTelephoneNumber.Name, faxPathWithCoverLetterAndNote);

                                        sendFaxesReporting.ReferringCareSendID = int.Parse(operationResult.MessageList[0]);

                                        if (operationResult.Success)
                                        {
                                            AutomatedFaxBL.Multitech.SendFaxMultitech sendFax = new AutomatedFaxBL.Multitech.SendFaxMultitech();

                                            faxTelephoneNumber.FaxNumber = faxTelephoneNumber.FaxNumber.Replace("(", "")
                                                .Replace(")", "")
                                                .Replace("-", "")
                                                .Replace(" ", "");

                                            sendFax.SendTheFax(documentsList[i].PatientName, faxPathWithCoverLetterAndNote, faxTelephoneNumber.Name, faxRepository.UpdatedFaxNumber(faxTelephoneNumber.FaxNumber), operationResult.MessageList[0], "");
                                        }
                                    }
                                    else
                                    {
                                        sendFaxesReporting.ReferringCareFaxSent = "N";
                                        sendFaxesReporting.ReferringCareFaxNumber = faxTelephoneNumber.FaxNumber;
                                        sendFaxesReporting.ReferringCareReason = "Number of pages greater than 14";
                                    }
                                }
                                else
                                {
                                    whyFaxWasNotSent = string.Empty;
                                    if (faxTelephoneNumber.FaxNumber.Length < 1 && faxRepository.CheckToseeIfProviderWorksForPractice(faxTelephoneNumber.ProviderID))
                                    {
                                        whyFaxWasNotSent = "No Fax Number for Provider and Referring Care Provider is in the Practice";
                                    }
                                    else if (faxTelephoneNumber.FaxNumber.Length < 1)
                                    {
                                        whyFaxWasNotSent = "No Fax Number for Provider";
                                    }
                                    else if (faxRepository.CheckToseeIfProviderWorksForPractice(faxTelephoneNumber.ProviderID))
                                    {
                                        whyFaxWasNotSent = "Referring Care Provider is in the Practice";
                                    }

                                    sendFaxesReporting.ReferringCareFaxSent = "N";
                                    sendFaxesReporting.ReferringCareFaxNumber = faxTelephoneNumber.FaxNumber;
                                    sendFaxesReporting.ReferringCareReason = whyFaxWasNotSent;
                                }
                            }
                            else
                            {
                                sendFaxesReporting.ReferringCareFaxSent = "N";
                                sendFaxesReporting.ReferringCareFaxNumber = faxTelephoneNumber.FaxNumber;
                                sendFaxesReporting.ReferringCareReason = "No Referring Care Provider";
                            }
                            SendFaxesReporting sendFaxesReporting2 = sendFaxesReporting;
                            faxRepository.AddReportingRecord(sendFaxesReporting);
                        }
                    }
                    else
                    {
                        //Care Provider who signed note is not Care Provider of record for visit
                        faxRepository.InsertAllSignedNotes(documentsList[i]);

                        sendFaxesReporting = new SendFaxesReporting();
                        sendFaxesReporting.DocumentID = documentsList[i].DocumentID;
                        sendFaxesReporting.CareProviderID = documentsList[i].CareProviderID;
                        sendFaxesReporting.CareProviderName = documentsList[i].CareProviderName;
                        sendFaxesReporting.PatientID = documentsList[i].PatientID;
                        sendFaxesReporting.PatientName = documentsList[i].PatientName;
                        sendFaxesReporting.PrimaryCareProviderID = documentsList[i].PrimaryCareProviderID;
                        sendFaxesReporting.PrimaryCareProvider = documentsList[i].PrimaryCareProvider;
                        sendFaxesReporting.ReferringCareProviderID = documentsList[i].ReferringCareProviderID;
                        sendFaxesReporting.ReferringCareProvider = documentsList[i].ReferringCareProvider;
                        sendFaxesReporting.PrimaryCareFaxSent = "N";
                        sendFaxesReporting.PrimaryCareFaxNumber = string.Empty;
                        sendFaxesReporting.PrimaryCareReason = "Care Provider who signed note is not Care Provider of record for visit";
                        sendFaxesReporting.ReferringCareFaxSent = "N";
                        sendFaxesReporting.ReferringCareFaxNumber = string.Empty;
                        sendFaxesReporting.ReferringCareReason = "Care Provider who signed note is not Care Provider of record for visit";
                        faxRepository.AddReportingRecord(sendFaxesReporting);
                        //Do nothing for this record
                    }
                }
                else
                {
                    //Document not set to Y to process
                    //Either the document had already been sent 
                    //or it has been signed more than one time on the same day by the same provider
                    faxRepository.InsertAllSignedNotes(documentsList[i]);

                    sendFaxesReporting = new SendFaxesReporting();
                    sendFaxesReporting.DocumentID = documentsList[i].DocumentID;
                    sendFaxesReporting.CareProviderID = documentsList[i].CareProviderID;
                    sendFaxesReporting.CareProviderName = documentsList[i].CareProviderName;
                    sendFaxesReporting.PatientID = documentsList[i].PatientID;
                    sendFaxesReporting.PatientName = documentsList[i].PatientName;
                    sendFaxesReporting.PrimaryCareProviderID = documentsList[i].PrimaryCareProviderID;
                    sendFaxesReporting.PrimaryCareProvider = documentsList[i].PrimaryCareProvider;
                    sendFaxesReporting.ReferringCareProviderID = documentsList[i].ReferringCareProviderID;
                    sendFaxesReporting.ReferringCareProvider = documentsList[i].ReferringCareProvider;
                    sendFaxesReporting.PrimaryCareFaxSent = "N";
                    sendFaxesReporting.PrimaryCareFaxNumber = string.Empty;
                    sendFaxesReporting.PrimaryCareReason = "Either document has already been sent or duplicate signature on the same day";
                    sendFaxesReporting.ReferringCareFaxSent = "N";
                    sendFaxesReporting.ReferringCareFaxNumber = string.Empty;
                    sendFaxesReporting.ReferringCareReason = "Either document has already been sent or duplicate signature on the same day";
                    faxRepository.AddReportingRecord(sendFaxesReporting);
                }
                System.Threading.Thread.Sleep(20000);
            }

            System.Threading.Thread.Sleep(30000);

            for (int i = 0; i <= 3; i++)
            {
                try
                {
                    Console.WriteLine(DateTime.Now.ToShortTimeString());
                    faxRepository.UpdateCompletionTimes();
                    faxRepository.UpdateStatusOfSentFaxes();
                    System.Threading.Thread.Sleep(300000);
                    Console.WriteLine(DateTime.Now.ToShortTimeString());
                }
                catch (Exception er)
                {
                    Logging.LogErrors(ConfigurationValues.ErrorLogPath, er.ToString());
                }
            }
        }
        public OperationResult AddReportingRecord(SendFaxesReporting sentNotesFax)
        {
            if (string.IsNullOrEmpty(sentNotesFax.CareProviderName))
            {
                sentNotesFax.CareProviderName = string.Empty;
            }

            if (string.IsNullOrEmpty(sentNotesFax.PatientName))
            {
                sentNotesFax.PatientName = string.Empty;
            }

            if (string.IsNullOrEmpty(sentNotesFax.PrimaryCareFaxNumber))
            {
                sentNotesFax.PrimaryCareFaxNumber = string.Empty;
            }

            if (string.IsNullOrEmpty(sentNotesFax.PrimaryCareFaxSent))
            {
                sentNotesFax.PrimaryCareFaxSent = string.Empty;
            }

            if (string.IsNullOrEmpty(sentNotesFax.PrimaryCareProvider))
            {
                sentNotesFax.PrimaryCareProvider = string.Empty;
            }

            if (string.IsNullOrEmpty(sentNotesFax.PrimaryCareReason))
            {
                sentNotesFax.PrimaryCareReason = string.Empty;
            }

            if (string.IsNullOrEmpty(sentNotesFax.PrimaryCareResult))
            {
                sentNotesFax.PrimaryCareResult = string.Empty;
            }

            if (string.IsNullOrEmpty(sentNotesFax.ReferringCareFaxNumber))
            {
                sentNotesFax.ReferringCareFaxNumber = string.Empty;
            }

            if (string.IsNullOrEmpty(sentNotesFax.ReferringCareFaxSent))
            {
                sentNotesFax.ReferringCareFaxSent = string.Empty;
            }

            if (string.IsNullOrEmpty(sentNotesFax.ReferringCareProvider))
            {
                sentNotesFax.ReferringCareProvider = string.Empty;
            }

            if (string.IsNullOrEmpty(sentNotesFax.ReferringCareReason))
            {
                sentNotesFax.ReferringCareReason = string.Empty;
            }

            if (string.IsNullOrEmpty(sentNotesFax.ReferringCareResult))
            {
                sentNotesFax.ReferringCareResult = string.Empty;
            }

            OperationResult operationResult = new Models.OperationResult();

            string now = DateTime.Now.ToShortDateString()
                + " " + DateTime.Now.ToShortTimeString();

            using (IDbConnection db = new SqlConnection(ConfigurationValues.WaldenFaxConnection))
            {
                try
                {

                    const string query = "INSERT INTO [AutomatedFaxesSent]"
                        + "("
                        + "[DocumentID]"
                        + " ,[PatientID]"
                        + " ,[PatientName]"
                        + " ,[CareProviderID]"
                        + " ,[CareProviderName]"
                        + " ,[PrimaryCareProviderID]"
                        + " ,[PrimaryCareProvider]"
                        + " ,[PrimaryCareFaxNumber]"
                        + " ,[PrimaryCareFaxSent]"
                        + " ,[PrimaryCareReason]"
                        + " ,[PrimaryCareResult]"
                        + " ,[PrimaryCareSendID]"
                        + " ,[PrimaryCareCompletionTime]"
                        + " ,[ReferringCareProviderID]"
                        + " ,[ReferringCareProvider]"
                        + " ,[ReferringCareFaxNumber]"
                        + " ,[ReferringCareFaxSent]"
                        + " ,[ReferringCareReason]"
                        + " ,[ReferringCareResult]"
                        + " ,[ReferringCareSendID]"
                        + " ,[ReferringCareCompletionTime]"
                        + " ,[DateTimeCreated]"
                        + " ,[DateTimeSent]"
                        + ")"
                        + "VALUES"
                        + "("
                        + " @DocumentID,@PatientID,@PatientName,@CareProviderID,@CareProviderName,@PrimaryCareProviderID,"
                        + " @PrimaryCareProvider,@PrimaryCareFaxNumber,@PrimaryCareFaxSent,@PrimaryCareReason,@PrimaryCareResult,@PrimaryCareSendID,@PrimaryCareCompletionTime,"
                        + " @ReferringCareProviderID,@ReferringCareProvider,@ReferringCareFaxNumber,@ReferringCareFaxSent,@ReferringCareReason,@ReferringCareResult,"
                        + " @ReferringCareSendID,@ReferringCareCompletionTime,@DateTimeCreated,@DateTimeSent"
                        + " )";

                    int rowsAffectd = db.Execute(query, new
                    {

                        @DocumentID = sentNotesFax.DocumentID,
                        @PatientID = sentNotesFax.PatientID,
                        @PatientName = sentNotesFax.PatientName,
                        @CareProviderID = sentNotesFax.CareProviderID,
                        @CareProviderName = sentNotesFax.CareProviderName,
                        @PrimaryCareProviderID = sentNotesFax.PrimaryCareProviderID,
                        @PrimaryCareProvider = sentNotesFax.PrimaryCareProvider,
                        @PrimaryCareFaxNumber = sentNotesFax.PrimaryCareFaxNumber,
                        @PrimaryCareFaxSent = sentNotesFax.PrimaryCareFaxSent,
                        @PrimaryCareReason = sentNotesFax.PrimaryCareReason,
                        @PrimaryCareResult = sentNotesFax.PrimaryCareResult,
                        @PrimaryCareSendID = sentNotesFax.PrimaryCareSendID,
                        @PrimaryCareCompletionTime = string.Empty,
                        @ReferringCareProviderID = sentNotesFax.ReferringCareProviderID,
                        @ReferringCareProvider = sentNotesFax.ReferringCareProvider,
                        @ReferringCareFaxNumber = sentNotesFax.ReferringCareFaxNumber,
                        @ReferringCareFaxSent = sentNotesFax.ReferringCareFaxSent,
                        @ReferringCareReason = sentNotesFax.ReferringCareReason,
                        @ReferringCareResult = sentNotesFax.ReferringCareResult,
                        @ReferringCareSendID = sentNotesFax.ReferringCareSendID,
                        @ReferringCareCompletionTime = string.Empty,
                        @DateTimeCreated = now,
                        @DateTimeSent = now
                    });

                    sendFaxId = GetSendFaxID();
                    operationResult.Success = true;
                    operationResult.AddMessage(sendFaxId.ToString());

                    return operationResult;
                }
                catch (Exception er)
                {
                    Utility.LogErrors(ConfigurationValues.ErrorLogPath, er.ToString());
                    operationResult.Success = false;
                    operationResult.AddMessage(er.ToString());
                    return operationResult;
                }
            }
        }
        private void RunAutomatedFaxesForConnecticutOrtho()
        {

            faxRepository = new FaxRepository();
            createLetters = new AutomatedFaxBL.Letters.CreateLetters();

            //        //ResendFaxesThatHaveFailed();
            PeformActionOnFaxesThatHaveFailedThreeTimes();
            documentsList = faxRepository.GetListOfDocumentssToSendViaFax(faxRepository.GetListOfCarProviderIDs(),
                faxRepository.GetLocationsList());
            faxTelephoneNumberList = new List<FaxTelephoneNumber>();
            faxTelephoneNumber = new FaxTelephoneNumber();
            sendFaxesReporting = new SendFaxesReporting();
            string faxPathWithCoverLetterAndNote = string.Empty;

            for (int i = 0; i < documentsList.Count; i++)
            {
                if (documentsList[i].Proccess == "Y")
                {
                    if (faxRepository.CheckToSeeIfProviderThatSignedDocumentIsCareProvider(documentsList[i].CareProviderID,
                        documentsList[i].SignerID))
                    {
                        if (GetDocumentFromGreenway(documentsList[i].DocumentID))
                        {
                            sendFaxesReporting = new SendFaxesReporting();

                            faxRepository.InsertAllSignedNotes(documentsList[i]);

                            sendFaxesReporting = new SendFaxesReporting();
                            sendFaxesReporting.DocumentID = documentsList[i].DocumentID;
                            sendFaxesReporting.CareProviderID = documentsList[i].CareProviderID;
                            sendFaxesReporting.CareProviderName = documentsList[i].CareProviderName;
                            sendFaxesReporting.PatientID = documentsList[i].PatientID;
                            sendFaxesReporting.PatientName = documentsList[i].PatientName;
                            sendFaxesReporting.PrimaryCareProviderID = documentsList[i].PrimaryCareProviderID;
                            sendFaxesReporting.PrimaryCareProvider = documentsList[i].PrimaryCareProvider;
                            sendFaxesReporting.ReferringCareProviderID = documentsList[i].ReferringCareProviderID;
                            sendFaxesReporting.ReferringCareProvider = documentsList[i].ReferringCareProvider;

                            faxTelephoneNumber = faxRepository.GetFaxNumbers(documentsList[i].PrimaryCareProviderID.ToString());

                            if (documentsList[i].PrimaryCareProviderID > 0)
                            {
                                if (faxTelephoneNumber.FaxNumber.Length > 0 && faxRepository.CheckToseeIfProviderWorksForPractice(faxTelephoneNumber.ProviderID) == false)
                                {
                                    faxPathWithCoverLetterAndNote = string.Empty;

                                    if (documentsList[i].DocTypeName == "History and Physical")
                                    {
                                        faxPathWithCoverLetterAndNote = createLetters.CreateLetterToPutInFrontOfNote(faxTelephoneNumber, documentsList[i]);
                                    }
                                    else if (documentsList[i].DocTypeName == "Progress Note")
                                    {
                                        string newDocumentName = Guid.NewGuid().ToString() + ".pdf";
                                        faxPathWithCoverLetterAndNote = ConfigurationValues.FinalFaxPath + newDocumentName;
                                        File.Copy(ConfigurationValues.CreatePdfPath, ConfigurationValues.FinalFaxPath + newDocumentName);
                                        // ConfigurationValues.FinalFaxPath + newDocumentName
                                    }

                                    if (Utility.GetPageCount(faxPathWithCoverLetterAndNote) < 15)
                                    {
                                        sendFaxesReporting.PrimaryCareFaxNumber = faxTelephoneNumber.FaxNumber;
                                        sendFaxesReporting.PrimaryCareFaxSent = "Y";
                                        sendFaxesReporting.PrimaryCareReason = "Approved Provider and has fax number";

                                        OperationResult operationResult = CreateFaxRecord(documentsList[i].CareProviderName,
                                            faxRepository.UpdatedFaxNumber(faxTelephoneNumber.FaxNumber),
                                            faxTelephoneNumber.Name, faxPathWithCoverLetterAndNote);

                                        sendFaxesReporting.PrimaryCareSendID = int.Parse(operationResult.MessageList[0]);

                                        if (operationResult.Success)
                                        {
                                            AutomatedFaxBL.Multitech.SendFaxMultitech sendFax = new AutomatedFaxBL.Multitech.SendFaxMultitech();

                                            faxTelephoneNumber.FaxNumber = faxTelephoneNumber.FaxNumber.Replace("(", "")
                                                .Replace(")", "")
                                                .Replace("-", "")
                                                .Replace(" ", "");
                                            sendFax.SendTheFax(documentsList[i].PatientName, faxPathWithCoverLetterAndNote, faxTelephoneNumber.Name, faxRepository.UpdatedFaxNumber(faxTelephoneNumber.FaxNumber), operationResult.MessageList[0], "");
                                        }
                                    }
                                    else
                                    {
                                        sendFaxesReporting.PrimaryCareFaxSent = "N";
                                        sendFaxesReporting.PrimaryCareFaxNumber = faxTelephoneNumber.FaxNumber;
                                        sendFaxesReporting.PrimaryCareReason = "Number of pages greater than 14";
                                    }
                                }// end 
                                else
                                {
                                    whyFaxWasNotSent = string.Empty;
                                    if (faxTelephoneNumber.FaxNumber.Length < 1 && faxRepository.CheckToseeIfProviderWorksForPractice(faxTelephoneNumber.ProviderID))
                                    {
                                        whyFaxWasNotSent = "No Fax Number for Provider and Primary Care Provider is in the Practice";
                                    }
                                    else if (faxTelephoneNumber.FaxNumber.Length < 1)
                                    {
                                        whyFaxWasNotSent = "No Fax Number for Provider";
                                    }
                                    else if (faxRepository.CheckToseeIfProviderWorksForPractice(faxTelephoneNumber.ProviderID))
                                    {
                                        whyFaxWasNotSent = "Primary Care Provider is in the Practice";
                                    }

                                    sendFaxesReporting.PrimaryCareFaxSent = "N";
                                    sendFaxesReporting.PrimaryCareFaxNumber = faxTelephoneNumber.FaxNumber;
                                    sendFaxesReporting.PrimaryCareReason = whyFaxWasNotSent;
                                }
                            }
                            else
                            {
                                sendFaxesReporting.PrimaryCareFaxSent = "N";
                                sendFaxesReporting.PrimaryCareFaxNumber = faxTelephoneNumber.FaxNumber;
                                sendFaxesReporting.PrimaryCareReason = "No Primary Care Provider";
                            }

                            faxTelephoneNumber = faxRepository.GetFaxNumbers(documentsList[i].ReferringCareProviderID.ToString());

                            if (documentsList[i].ReferringCareProviderID == documentsList[i].PrimaryCareProviderID)
                            {
                                sendFaxesReporting.ReferringCareFaxSent = "N";
                                sendFaxesReporting.ReferringCareFaxNumber = faxTelephoneNumber.FaxNumber;
                                sendFaxesReporting.ReferringCareReason = "No Delivery Requested";
                            }
                            else if (documentsList[i].ReferringCareProviderID > 0)
                            {
                                if (faxTelephoneNumber.FaxNumber.Length > 0 && faxRepository.CheckToseeIfProviderWorksForPractice(faxTelephoneNumber.ProviderID) == false)
                                {

                                    faxPathWithCoverLetterAndNote = string.Empty;

                                    if (documentsList[i].DocTypeName == "History and Physical")
                                    {
                                        faxPathWithCoverLetterAndNote = createLetters.CreateLetterToPutInFrontOfNote(faxTelephoneNumber, documentsList[i]);
                                    }
                                    else if (documentsList[i].DocTypeName == "Progress Note")
                                    {
                                        string newDocumentName = Guid.NewGuid().ToString() + ".pdf";
                                        faxPathWithCoverLetterAndNote = ConfigurationValues.FinalFaxPath + newDocumentName;
                                        File.Copy(ConfigurationValues.CreatePdfPath, ConfigurationValues.FinalFaxPath + newDocumentName);
                                    }
                                    //faxPathWithCoverLetterAndNote = createLetters.CreateLetterToPutInFrontOfNote(faxTelephoneNumber, documentsList[i]);
                                    if (Utility.GetPageCount(faxPathWithCoverLetterAndNote) < 15)
                                    {


                                        //createLetters.CreateLetterToPutInFrontOfNote(faxTelephoneNumber, documentsList[i]);

                                        sendFaxesReporting.ReferringCareFaxSent = "Y";
                                        sendFaxesReporting.ReferringCareFaxNumber = faxTelephoneNumber.FaxNumber;
                                        sendFaxesReporting.ReferringCareReason = "Approved Provider and has fax number";

                                        OperationResult operationResult = CreateFaxRecord(documentsList[i].CareProviderName,
                                            faxRepository.UpdatedFaxNumber(faxTelephoneNumber.FaxNumber),
                                            faxTelephoneNumber.Name, faxPathWithCoverLetterAndNote);

                                        sendFaxesReporting.ReferringCareSendID = int.Parse(operationResult.MessageList[0]);

                                        if (operationResult.Success)
                                        {
                                            AutomatedFaxBL.Multitech.SendFaxMultitech sendFax = new AutomatedFaxBL.Multitech.SendFaxMultitech();

                                            faxTelephoneNumber.FaxNumber = faxTelephoneNumber.FaxNumber.Replace("(", "")
                                                .Replace(")", "")
                                                .Replace("-", "")
                                                .Replace(" ", "");

                                            sendFax.SendTheFax(documentsList[i].PatientName, faxPathWithCoverLetterAndNote, faxTelephoneNumber.Name, faxRepository.UpdatedFaxNumber(faxTelephoneNumber.FaxNumber), operationResult.MessageList[0], "");
                                        }
                                    }
                                    else
                                    {
                                        sendFaxesReporting.ReferringCareFaxSent = "N";
                                        sendFaxesReporting.ReferringCareFaxNumber = faxTelephoneNumber.FaxNumber;
                                        sendFaxesReporting.ReferringCareReason = "Number of pages greater than 14";
                                    }
                                }// end 
                                else
                                {
                                    whyFaxWasNotSent = string.Empty;
                                    if (faxTelephoneNumber.FaxNumber.Length < 1 && faxRepository.CheckToseeIfProviderWorksForPractice(faxTelephoneNumber.ProviderID))
                                    {
                                        whyFaxWasNotSent = "No Fax Number for Provider and Referring Care Provider is in the Practice";
                                    }
                                    else if (faxTelephoneNumber.FaxNumber.Length < 1)
                                    {
                                        whyFaxWasNotSent = "No Fax Number for Provider";
                                    }
                                    else if (faxRepository.CheckToseeIfProviderWorksForPractice(faxTelephoneNumber.ProviderID))
                                    {
                                        whyFaxWasNotSent = "Referring Care Provider is in the Practice";
                                    }

                                    sendFaxesReporting.ReferringCareFaxSent = "N";
                                    sendFaxesReporting.ReferringCareFaxNumber = faxTelephoneNumber.FaxNumber;
                                    sendFaxesReporting.ReferringCareReason = whyFaxWasNotSent;
                                }
                            }
                            else
                            {
                                sendFaxesReporting.ReferringCareFaxSent = "N";
                                sendFaxesReporting.ReferringCareFaxNumber = faxTelephoneNumber.FaxNumber;
                                sendFaxesReporting.ReferringCareReason = "No Referring Care Provider";
                            }
                            SendFaxesReporting sendFaxesReporting2 = sendFaxesReporting;
                            faxRepository.AddReportingRecord(sendFaxesReporting);
                        }
                    }
                    else
                    {
                        //Care Provider who signed note is not Care Provider of record for visit
                    }
                }
                else
                {
                    //Document not have a Yes to process
                }
                System.Threading.Thread.Sleep(30000);
            }

            System.Threading.Thread.Sleep(30000);
            //}
            //    faxRepository.UpdateCompletionTimes();
            //    faxRepository.UpdateStatusOfSentFaxes();

        }