// POST: api/SendFax
        public IHttpActionResult Post([FromBody] Fax faxValues)
        {
            System.IO.Directory.CreateDirectory(ConfigurationValues.TemporaryFaxPath
                        + Utility.GetUserName());

            Logging.LogErrors(ConfigurationValues.ErrorLogPath, "Start Creating Fax");

            OperationResult createFaxRecordOperationResult = new Models.OperationResult();
            OperationResult sendFaxToMultiTechOperationResult = new Models.OperationResult();

            FaxRepository faxData = new FaxRepository();
            DocumentRepository documentData = new DocumentRepository();

            string[] faxesToSendList = faxValues.FaxTo.Split('~');

            SendFax sendFax = new SendFax();

            try
            {
                for (int i = 0; i < faxesToSendList.Length - 1; i++)
                {

                    Logging.LogErrors(ConfigurationValues.ErrorLogPath, "Getting fax information");

                    FaxingInformation faxingInformatonInitial = new FaxingInformation();

                    faxingInformatonInitial.DocumentList = faxValues.DocumentList;
                    faxingInformatonInitial.FaxTo = faxesToSendList[i];
                    faxingInformatonInitial.Notes = faxValues.Notes;
                    faxingInformatonInitial.From = faxValues.From;
                    faxingInformatonInitial.Cover = faxValues.Cover;
                    faxingInformatonInitial.CoverSheetOnly = faxValues.CoverOnly;

                    Logging.LogErrors(ConfigurationValues.ErrorLogPath, "Getting fax information");                    
                    //Create Fax
                    FaxingInformation faxingInformatonComplete = sendFax.CreateFax(faxingInformatonInitial);

                    System.IO.File.Copy(ConfigurationValues.TemporaryFaxPath
                        + Utility.GetUserName() + "\\" + faxingInformatonComplete.FaxName,
                        ConfigurationValues.PernamentFaxPath
                        + faxingInformatonComplete.FaxName, true);

                    createFaxRecordOperationResult = faxData.SendFax(faxingInformatonComplete, ConfigurationValues.PernamentFaxPath
                        + faxingInformatonComplete.FaxName, faxValues.From, faxValues.Notes);

                    if (createFaxRecordOperationResult.Success)
                    {
                        sendFaxToMultiTechOperationResult = sendFaxMultitech.SendTheFax(faxingInformatonComplete.Name, faxingInformatonComplete.FaxPath,
                            faxValues.From, faxingInformatonComplete.FaxNumber, createFaxRecordOperationResult.MessageList[0], ConfigurationValues.ApplicationPath);
                        if (sendFaxToMultiTechOperationResult.Success)
                        {
                            if (faxValues.SendToEmr == "F")
                            {
                                ArchiveDocument = new ArchiveDocument();

                                CurrentDocuments faxTodocument = documentData.GetCurrentDocumentPathPdf(Utility.GetUserName());
                                string[] filesToDelete = faxTodocument.CurrentDocumentList.Split('~');
                                for (int j = 0; j < filesToDelete.Length - 1; j++)
                                {
                                    try
                                    {
                                        string[] fileParts = filesToDelete[j].Split('\\');

                                        ArchiveDocument.ArchiveTheDocument(fileParts[fileParts.Length - 1], filesToDelete[j], Utility.GetUserName());

                                        File.Delete(filesToDelete[j]);
                                    }
                                    catch { }

                                    documentData.DeleteDocumentInFolder(filesToDelete[j]);
                                }


                                Logging.LogErrors(ConfigurationValues.ErrorLogPath, "Begin Delete file");

                                File.Delete(faxingInformatonComplete.FaxPath);

                                File.Delete(ConfigurationValues.TemporaryFaxPath
                                    + Utility.GetUserName()
                                    + "\\cover.pdf");
                                Logging.LogErrors(ConfigurationValues.ErrorLogPath, "end delete file");

                            }
                        }
                        else
                        {
                            faxData.UpdateFaxRecord(sendFaxToMultiTechOperationResult.MessageList[0]);
                            return BadRequest(sendFaxToMultiTechOperationResult.MessageList[0]);
                        }
                    }
                    else
                    {
                        return BadRequest(createFaxRecordOperationResult.MessageList[0]);
                    }
                }
            }
            catch (Exception er)
            {
                return BadRequest(er.ToString());
            }

            CurrentDocuments documentsToDelete = documentData.GetCurrentDocumentPathPdf(Utility.GetUserName());

            try
            {
                File.Delete(documentData.GetFullPathToDocument(Utility.GetUserName()));
            }
            catch { }

            string[] faxesToDelete = documentsToDelete.CurrentDocumentList.Split('~');

            try
            {
                for (int i = 0; i < faxesToDelete.Length - 1; i++)
                {
                    System.IO.File.Delete(ConfigurationValues.OutboundFaxDirectory + "\\"
                        + Utility.GetUserName() + "\\"
                        + faxesToDelete[i]);
                }
            }
            catch { }
            return Ok();
        }
        public OperationResult SendFax(FaxingInformation miscFaxInformation, string permanentFaxPath, string from, string notes)
        {

            OperationResult operationResult = new Models.OperationResult();

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

            using (IDbConnection db = new SqlConnection(ConfigurationValues.PostOfficeDatabaseConnection))
            {
                try
                {
                    string query = "INSERT INTO [FaxesSendServer]("
                        + "[AccountID]"
                        + ",[UserID]"
                        + ",[FaxName]"
                        + ",[FaxPath]"
                        + ",[FaxNumber]"
                        + ",[RecipientName]"
                        + ",[Notes]"
                        + ",[PageCount]"
                        + ",[FaxSent]"
                        + ",[InUse]"
                        + ",[ToTif]"
                        + ",[CallWait]"
                        + ",[TimesCalled]"
                        + ",[Status]"
                        + ",[ShowFax]"
                        + ",[CreateTime]"
                        + ",[CompletionTime]"
                        + ",[DateStamp])"
                        + " VALUES("
                        + "@AccountID"
                        + ",@UserID"
                        + ",@FaxName"
                        + ",@FaxPath"
                        + ",@FaxNumber"
                        + ",@RecipientName"
                        + ",@Notes"
                        + ",@PageCount"
                        + ",@FaxSent"
                        + ",@InUse"
                        + ",@ToTif"
                        + ",@CallWait"
                        + ",@TimesCalled"
                        + ",@Status"
                        + ",@ShowFax"
                        + ",@CreateTime"
                        + ",@CompletionTime"
                        + ",@DateStamp)";

                    db.Execute(query, new
                    {
                        @AccountID = 1001,
                        @UserID = Utility.GetUserName(),
                        @FaxName = miscFaxInformation.Name,
                        @FaxPath = permanentFaxPath,
                        @FaxNumber = miscFaxInformation.FaxNumber,
                        @RecipientName = from,
                        @Notes = notes,
                        @PageCount = miscFaxInformation.PageCount,
                        @FaxSent = "Y",
                        @InUse = "N",
                        @ToTif = "Y",
                        @CallWait = 0,
                        @TimesCalled = 0,
                        @Status = "New Fax Entry",
                        @ShowFax = "Y",
                        @CreateTime = now,
                        @CompletionTime = string.Empty,
                        @DateStamp = now
                    });

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

                    return operationResult;
                }
                catch (Exception er)
                {
                    operationResult.Success = false;
                    operationResult.AddMessage(er.ToString());
                    return operationResult;
                }
            }
        }
        public FaxingInformation CreateFax(FaxingInformation faxingInformation)
        {
            string temporaryPath = string.Empty;

            faxingInformation.FaxName
                = Guid.NewGuid().ToString() + ".pdf";

            FaxRepository faxData = new FaxRepository();
            string[] firstFaxTo = faxingInformation.FaxTo.Split('~');
            List<FaxToInformation> data = new List<FaxToInformation>();

            if (faxingInformation.FaxTo.Substring(0, 1) == "G")
            {
                data = faxData.GetFaxToInformationGreenway(firstFaxTo[0]);
            }
            else
            {
                data = faxData.GetFaxToInformation(firstFaxTo[0]);
            }

            try
            {
                faxingInformation.FaxTo = data[0].Name;

            }
            catch
            {

            }
            try
            {
                DocumentRepository documentData = new DocumentRepository();
                CurrentDocuments faxTodocument = documentData.GetCurrentDocumentPathPdf(Utility.GetUserName());
                Logging.LogErrors(ConfigurationValues.ErrorLogPath, faxTodocument.PathToCurrentPdfDocument);                    
                MemoryStream newPdfStream = Utility.CreateMemoryStreamFromPdf(faxTodocument.PathToCurrentPdfDocument);

                temporaryPath = ConfigurationValues.TemporaryFaxPath + Utility.GetUserName() + "\\"
                    + faxingInformation.FaxName;

                Logging.LogErrors(ConfigurationValues.ErrorLogPath, temporaryPath);                    
                FileStream newPdfStreamFile = new FileStream(temporaryPath, FileMode.Create);

                newPdfStream.WriteTo(newPdfStreamFile);
                Logging.LogErrors(ConfigurationValues.ErrorLogPath, "writing to file");                    
                newPdfStream.Close();
                Logging.LogErrors(ConfigurationValues.ErrorLogPath, "Closing stream");                    
                newPdfStreamFile.Close();

                MemoryStream coverSheet
                    = CreateCoverSheetV2(faxingInformation.FaxTo, data[0].FaxNumber, Utility.GetPDFPageCount(newPdfStreamFile.Name),
                    faxingInformation.Notes, faxingInformation.From, int.Parse(faxingInformation.Cover));

                faxingInformation.PageCount = Utility.GetPDFPageCount(newPdfStreamFile.Name);
                faxingInformation.FaxNumber = UpdatedFaxNumber(data[0].FaxNumber);
                faxingInformation.Name = faxingInformation.FaxTo;

                FileStream presStream = new FileStream(ConfigurationValues.TemporaryFaxPath + Utility.GetUserName()
                    + "\\cover.pdf", FileMode.Create);
                coverSheet.WriteTo(presStream);
                coverSheet.Close();
                presStream.Close();
                if (faxingInformation.CoverSheetOnly == "T")
                {
                    File.Copy(ConfigurationValues.TemporaryFaxPath + Utility.GetUserName()
                            + "\\cover.pdf", newPdfStreamFile.Name, true);
                    File.Delete(ConfigurationValues.TemporaryFaxPath + Utility.GetUserName()
                            + "\\cover.pdf");
                }
                else
                {
                    Utility.MergeTwoPDFDocuments(ConfigurationValues.TemporaryFaxPath + Utility.GetUserName()
                       + "\\cover.pdf", newPdfStreamFile.Name);
                }

                faxingInformation.FaxPath = newPdfStreamFile.Name;

                return faxingInformation;
            }
            catch (Exception er)
            {
                Logging.LogErrors(ConfigurationValues.ErrorLogPath, er.ToString());
                return faxingInformation;
            }
        }