public Greenway.PrimeSuite.DataContracts.Document.DocumentImportChartResponse SendPDF(Greenway.PrimeSuite.DataContracts.Document.DocumentImportChartRequest DocumentImportRequest, String sIP)
		{
			PrimeSuiteServiceClient oClient = null;
			try {

                oClient = GreenwayDocuments.HelperFiles.HelperClass.GetServiceClient(sIP);
				Greenway.PrimeSuite.DataContracts.Document.DocumentImportChartResponse oResponse = new Greenway.PrimeSuite.DataContracts.Document.DocumentImportChartResponse();
				oResponse = oClient.DocumentImportChart(DocumentImportRequest);

				return oResponse;

			} catch (Exception ex) {

				throw ex;
			} finally {
                GreenwayDocuments.HelperFiles.HelperClass.CloseServiceClient(oClient);
			}
		}
        // POST: api/ProcessErrorDocument
        public IHttpActionResult Post([FromBody]ScannedDocument barCodeText)
        {
            //string s1 = value.ArchiveID;
            DocumentTypeData documentTypeData = new DocumentTypeData();
            DocumentsData documentsData = new DocumentsData();
            string[] patientId = barCodeText.PatientID.Split(' ');
            string[] tabId = barCodeText.TabID.Split('~');
            barCodeText.ID = documentsData.GetDocumentIDFromPath(System.Configuration.ConfigurationManager.AppSettings["pathToWatchedFolder"] + barCodeText.FullPath);
            barCodeText.ArchiveID = documentsData.GetArchiveIDFromPath(System.Configuration.ConfigurationManager.AppSettings["pathToWatchedFolder"] + barCodeText.FullPath);

            try
            {
                System.IO.FileStream inFile;
                byte[] binaryData;

                try
                {
                    inFile = new System.IO.FileStream(System.Configuration.ConfigurationManager.AppSettings["pathToErrorFolder"] + barCodeText.FullPath,
                                              System.IO.FileMode.Open,
                                              System.IO.FileAccess.Read);
                    binaryData = new Byte[inFile.Length];
                    long bytesRead = inFile.Read(binaryData, 0,
                                         (int)inFile.Length);
                    inFile.Close();
                }
                catch (Exception er)
                {
                    string[] fileNameArray = barCodeText.FullPath.Split('\\');
                    string fileName = fileNameArray[fileNameArray.Length - 1];
                    return BadRequest(er.ToString());
                }

                string base64String;
                try
                {
                    base64String =
                      System.Convert.ToBase64String(binaryData,
                                             0,
                                             binaryData.Length);
                }
                catch (System.ArgumentNullException)
                {
                    string[] fileNameArray = barCodeText.FullPath.Split('\\');
                    string fileName = fileNameArray[fileNameArray.Length - 1];
                    return BadRequest();
                }

                ContractBuilder oBuilder = new ContractBuilder();
                Greenway.PrimeSuite.DataContracts.Document.DocumentImportChartRequest oRequest = new Greenway.PrimeSuite.DataContracts.Document.DocumentImportChartRequest();
                Greenway.PrimeSuite.DataContracts.Document.DocumentImportChartResponse oResponse = new Greenway.PrimeSuite.DataContracts.Document.DocumentImportChartResponse();

                oRequest = oBuilder.BuildDocumentImportChartRequest(base64String);
                oRequest.PrimeSuitePatientId = int.Parse(patientId[0]);
                oRequest.DocumentTypeID = int.Parse(tabId[0]);
                oRequest.DocumentName = documentTypeData.GetDocumentNameBasedOnDocumentType(tabId[0]);
                oRequest.DocumentSignerID = int.Parse(System.Configuration.ConfigurationManager.AppSettings["providerSignerID"]);
                oRequest.Credentials.PrimeSuiteCredential.PrimeSuiteUserName = System.Configuration.ConfigurationManager.AppSettings["greenwayUserID"];
                oRequest.Credentials.PrimeSuiteCredential.PrimeSuiteUserPassword = System.Configuration.ConfigurationManager.AppSettings["greenwayUserPassword"];
                EmployeeDesktop.API.Document ocall = new EmployeeDesktop.API.Document();
                oResponse = ocall.SendPDF(oRequest, System.Configuration.ConfigurationManager.AppSettings["greenwayServer"]);
                documentTypeData.UpdateScannedDocuments(barCodeText.ID, oResponse.DocumentID.ToString());
                documentTypeData.UpdateDocumentArchive(barCodeText.ArchiveID, "T");
                File.Delete(System.Configuration.ConfigurationManager.AppSettings["pathToErrorFolder"] + barCodeText.FullPath);
                return Ok();
            }
            catch (Exception er)
            {
                return BadRequest(er.ToString());
            }
        }
        private static void ProcessBarcodedDocuments(ImportDocument newDocumentToImport)
        {
            PostOfficev2.API.DataAccess.DocumentRepository documentData = new PostOfficev2.API.DataAccess.DocumentRepository();
            CurrentDocuments currentDocument = documentData.GetCurrentDocumentPathPDF(Utility.GetUserName());
            newDocumentToImport.FullPath = currentDocument.PathToCurrentPDFDocument;

            int pdfPageCount = 0;
            Document pdfDocument = new Document(newDocumentToImport.FullPath);
            pdfPageCount = pdfDocument.Pages.Count;

            try
            {
                System.IO.FileStream inFile;
                byte[] binaryData;

                try
                {
                    inFile = new System.IO.FileStream(newDocumentToImport.FullPath,
                                              System.IO.FileMode.Open,
                                              System.IO.FileAccess.Read);
                    binaryData = new Byte[inFile.Length];
                    long bytesRead = inFile.Read(binaryData, 0,
                                         (int)inFile.Length);
                    inFile.Close();
                }
                catch (Exception er)
                {
                    Logging.LogErrors(ConfigurationValues.ErrorLogPath, er.ToString());
                    string[] fileNameArray = newDocumentToImport.FullPath.Split('\\');
                    string fileName = fileNameArray[fileNameArray.Length - 1];
                    File.Copy(newDocumentToImport.FullPath, pathToErrorFolder + fileName, true);
                    return;
                }

                string base64String;
                try
                {
                    base64String =
                      System.Convert.ToBase64String(binaryData,
                                             0,
                                             binaryData.Length);
                }
                catch (System.ArgumentNullException)
                {
                    //LogErrors("Binary data array is null.");
                    string[] fileNameArray = newDocumentToImport.FullPath.Split('\\');
                    string fileName = fileNameArray[fileNameArray.Length - 1];
                    File.Copy(newDocumentToImport.FullPath, pathToErrorFolder + fileName, true);
                    //AddErrorDocument(pathToErrorFolder + fileName, barCodeText.User);
                    return;
                }

                ContractBuilder oBuilder = new ContractBuilder();
                Greenway.PrimeSuite.DataContracts.Document.DocumentImportChartRequest oRequest = new Greenway.PrimeSuite.DataContracts.Document.DocumentImportChartRequest();
                Greenway.PrimeSuite.DataContracts.Document.DocumentImportChartResponse oResponse = new Greenway.PrimeSuite.DataContracts.Document.DocumentImportChartResponse();

                oRequest.Header = new Greenway.PrimeSuite.DataContracts.API.Header();

                oRequest = oBuilder.BuildDocumentImportChartRequest(base64String);

                oRequest.PrimeSuitePatientId = newDocumentToImport.PatientID;
                oRequest.DocumentTypeID = newDocumentToImport.DocTypeId;

                oRequest.DocumentName = "Lab Result -";
                oRequest.DocumentDescription = string.Empty;
                oRequest.Credentials.PrimeSuiteCredential.PrimeSuiteUserName = greenwayUserID;
                oRequest.Credentials.PrimeSuiteCredential.PrimeSuiteUserPassword = greenwayUserPassword;

                GreenwayDocuments.DocImporter.Document ocall = new GreenwayDocuments.DocImporter.Document();

                oResponse = ocall.SendPDF(oRequest, greenwayServer);

                ArchiveDocument archiveDocument = new ArchiveDocument();

                string[] listOfDocumentsToArchive = currentDocument.CurrentDocumentList.Split('~');

                for (int i = 0; i < listOfDocumentsToArchive.Length - 1; i++)
                {
                    string[] documentName = listOfDocumentsToArchive[i].Split('\\');
                    archiveDocument.ArchiveTheDocument(documentName[documentName.Length - 1], listOfDocumentsToArchive[i], Utility.GetUserName());
                    File.Delete(listOfDocumentsToArchive[i]);
                }

                return;
            }
            catch (Exception er)
            {
                Logging.LogErrors(ConfigurationValues.ErrorLogPath, er.ToString());
                string[] fileNameArray = newDocumentToImport.FullPath.Split('\\');
                string fileName = fileNameArray[fileNameArray.Length - 1];
                File.Copy(newDocumentToImport.FullPath, pathToErrorFolder + fileName, true);
                return;
            }
        }
        private static void ProcessBarcodedDocuments(ScannedDocument barCodeText)
        {
            int pdfPageCount = 0;
            int pdfPageCountCheck = 0;
            Document pdfDocument = new Document(barCodeText.FullPath);
            pdfPageCount = pdfDocument.Pages.Count;
            pdfPageCountCheck = pdfPageCountCheck + barCodeText.PageCount;

            if (pdfPageCount != pdfPageCountCheck)
            {
                string[] fileNameArray = barCodeText.FullPath.Split('\\');
                string fileName = fileNameArray[fileNameArray.Length - 1];
                UpdateDocumentArchive(barCodeText.ArchiveID, "F");
                File.Copy(barCodeText.FullPath, pathToErrorFolder + fileName, true);
                AddErrorDocument(pathToErrorFolder + fileName, barCodeText.User);
                File.Delete(barCodeText.FullPath);
                return;
            }

            try
            {
                System.IO.FileStream inFile;
                byte[] binaryData;

                try
                {
                    inFile = new System.IO.FileStream(barCodeText.FullPath,
                                              System.IO.FileMode.Open,
                                              System.IO.FileAccess.Read);
                    binaryData = new Byte[inFile.Length];
                    long bytesRead = inFile.Read(binaryData, 0,
                                         (int)inFile.Length);
                    inFile.Close();
                }
                catch (Exception er)
                {
                    LogErrors(er.ToString());
                    string[] fileNameArray = barCodeText.FullPath.Split('\\');
                    string fileName = fileNameArray[fileNameArray.Length - 1];
                    File.Copy(barCodeText.FullPath, pathToErrorFolder + fileName, true);
                    AddErrorDocument(pathToErrorFolder + fileName, barCodeText.User);
                    return;
                }

                string base64String;
                try
                {
                    base64String =
                      System.Convert.ToBase64String(binaryData,
                                             0,
                                             binaryData.Length);
                }
                catch (System.ArgumentNullException)
                {
                    LogErrors("Binary data array is null.");
                    string[] fileNameArray = barCodeText.FullPath.Split('\\');
                    string fileName = fileNameArray[fileNameArray.Length - 1];
                    File.Copy(barCodeText.FullPath, pathToErrorFolder + fileName, true);
                    AddErrorDocument(pathToErrorFolder + fileName, barCodeText.User);
                    return;
                }

                ContractBuilder oBuilder = new ContractBuilder();
                Greenway.PrimeSuite.DataContracts.Document.DocumentImportChartRequest oRequest = new Greenway.PrimeSuite.DataContracts.Document.DocumentImportChartRequest();
                Greenway.PrimeSuite.DataContracts.Document.DocumentImportChartResponse oResponse = new Greenway.PrimeSuite.DataContracts.Document.DocumentImportChartResponse();

                oRequest = oBuilder.BuildDocumentImportChartRequest(base64String);
                oRequest.PrimeSuitePatientId = int.Parse(barCodeText.PatientID);
                oRequest.DocumentTypeID = int.Parse(barCodeText.TabID);
                oRequest.DocumentName = GetDocumentNameBasedOnDocumentType(barCodeText.TabID);
                oRequest.DocumentSignerID = int.Parse(System.Configuration.ConfigurationManager.AppSettings["providerSignerID"]);
                oRequest.Credentials.PrimeSuiteCredential.PrimeSuiteUserName = greenwayUserID;
                oRequest.Credentials.PrimeSuiteCredential.PrimeSuiteUserPassword = greenwayUserPassword;

                GreenwayDocuments.DocImporter.Document ocall = new GreenwayDocuments.DocImporter.Document();

                int countOfRecordsFromGreenway = GetDocumentCountGreenway(oRequest.PrimeSuitePatientId.ToString(),
                    oRequest.DocumentSignerID.ToString(),
                    oRequest.DocumentTypeID.ToString(), "Imported - " + oRequest.DocumentName);

                int countOfRecordsFromScannedDocuments = GetDocumentCountScannedDocuments(oRequest.PrimeSuitePatientId.ToString(),
                    oRequest.DocumentSignerID.ToString(),
                    oRequest.DocumentTypeID.ToString(), "Imported - " + oRequest.DocumentName);

                oResponse = ocall.SendPDF(oRequest, greenwayServer);
                Log("The document id is: " + oResponse.DocumentID.ToString());
                UpdateScannedDocuments(barCodeText.ID, oResponse.DocumentID.ToString());
                UpdateDocumentArchive(barCodeText.ArchiveID, "T");
                File.Delete(barCodeText.FullPath);

                CheckToSeeIfProblemWithRecords();
            }
            catch (Exception er)
            {
                LogErrors("Error is : " + er.ToString() + "PatienID: " + barCodeText.PatientID + " <br />"
                        + "Document Name: " + GetDocumentNameBasedOnDocumentType(barCodeText.TabID) + " <br />"
                        + "Document Type ID:    " + barCodeText.TabID + " <br />"
                        + "Page Count:    " + barCodeText.PageCount.ToString() + " <br />");
                string[] fileNameArray = barCodeText.FullPath.Split('\\');
                string fileName = fileNameArray[fileNameArray.Length - 1];
                UpdateDocumentArchive(barCodeText.ArchiveID, "F");
                File.Copy(barCodeText.FullPath, pathToErrorFolder + fileName, true);
                AddErrorDocument(pathToErrorFolder + fileName, barCodeText.User);
                return;
            }
        }