// POST: api/Copier
        public IHttpActionResult Post([FromBody]Copier copier)
        {
            OperationResult operationResult = new Models.OperationResult();
            CopierData copierData = new CopierData();


            if (copier.ID == 0)
            {
                operationResult = copierData.AddCopier(copier);
                if (operationResult.Success)
                {
                    return Ok();
                }
                else
                {
                    return BadRequest(operationResult.ErrorMessage);
                }
            }
            else
            {
                operationResult = copierData.UpdateCopier(copier);
                if (operationResult.Success)
                {
                    return Ok();
                }
                else
                {
                    return BadRequest(operationResult.ErrorMessage);
                }
            }
        }
        // POST: api/EditFaxToEntry
        public IHttpActionResult Post([FromBody]SendFaxInformation faxEntry)
        {
            OperationResult operationResult = new Models.OperationResult();
            SendToData sendToData = new SendToData();
            faxEntry.FaxNumber = faxEntry.FaxNumber.Replace("-", "")
                .Replace(" ", "")
                .Replace("(", "")
                .Replace(")", "");

            if (faxEntry.Prefix == "Prefix")
            {
                faxEntry.Prefix = string.Empty;
            }

            if (faxEntry.Suffix == "Suffix")
            {
                faxEntry.Suffix = string.Empty;
            }

            faxEntry.Name = faxEntry.Prefix + " "
            + faxEntry.FirstName + " "
            + faxEntry.LastName + " "
            + faxEntry.Suffix;

            operationResult.Success = sendToData.UpdateFaxEntry(faxEntry);

            if (operationResult.Success)
            {
                return Ok();
            }
            else
            {
                return BadRequest(); 
            }
        }
        // DELETE: api/EditFaxToEntry/5
        public IHttpActionResult Delete(int id)
        {
            OperationResult operationResult = new Models.OperationResult();
            SendToRepository sendToData = new SendToRepository();
            operationResult = sendToData.DeleteFaxEntry(id);

            if (operationResult.Success)
            {
                return Ok();
            }
            else
            {
                return BadRequest();
            }
        }
예제 #4
0
        /// <summary>
        /// 把返回码映射为json。
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        public static string SerializeDBReturnCode(DBReturnCode code)
        {
            Models.OperationResult result = new Models.OperationResult();
            result.ErrorCode = (int)code;
            switch (code)
            {
            case DBReturnCode.BAD_REQUEST: result.ErrorMessage = "参数异常"; break;

            case DBReturnCode.DATA_CONFLICT: result.ErrorMessage = "数据库冲突"; break;

            case DBReturnCode.NOT_EXIST: result.ErrorMessage = "数据不存在"; break;

            case DBReturnCode.OP_ERROR: result.ErrorMessage = "操作失败"; break;

            case DBReturnCode.SUCCESS: result.ErrorMessage = "操作成功"; break;
            }
            return(JsonConvert.SerializeObject(result));
        }
예제 #5
0
 /// <summary>
 /// 登陆操作
 /// </summary>
 /// <returns></returns>
 public ActionResult login()
 {
     Models.OperationResult result = new Models.OperationResult()
     {
         ErrorCode = 001, ErrorMessage = "参数错误"
     };
     try
     {
         string username = Request.Params.Get("username");
         string password = Request.Params.Get("password");
         using (Maintain mt = new Maintain())
         {
             result = DAL.UserService.Login(mt, username, password);
             if (result.ErrorCode == 0)
             {
                 Models.UserInfo user = DAL.UserService.GetUser(mt, username);
                 //不可把password明文传回客户端
                 String temp = user.Password;
                 user.Password = "";
                 var j = JsonConvert.SerializeObject(user);
                 user.Password = temp;
                 return(Json(j, JsonRequestBehavior.AllowGet));
             }
             else
             {
                 //登录失败返回错误信息
                 return(Json(JsonConvert.SerializeObject(result), JsonRequestBehavior.AllowGet));
             }
         }
     }
     catch (Exception e)
     {
         //异常时返回错误信息
         Console.WriteLine("error:" + e.Message);
         result.ErrorMessage = e.Message;
         var j = JsonConvert.SerializeObject(result);
         return(Json(j, JsonRequestBehavior.AllowGet));
     }
 }
        public OperationResult AddFolder(Folder folder)
        {
            OperationResult operationResult = new Models.OperationResult();

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

                    string query = "INSERT INTO [HoldFolders]"
                        + " ("
                        + " [ActiveDirectoryUser]"
                        + " ,[DocumentType]"
                        + " ,[FolderName]"
                        + " )"
                        + "VALUES"
                        + " ("
                        + " @ActiveDirectoryUser,@DocumentType,@FolderName"
                        + " )";

                    db.Execute(query, new
                    {
                        @ActiveDirectoryUser = Utility.GetUserName(),
                        @DocumentType = folder.DocumentType,
                        @FolderName = folder.FolderName
                    });

                    operationResult.Success = true;
                    return operationResult;
                }
                catch (Exception er)
                {
                    operationResult.Success = false;
                    operationResult.AddMessage(er.ToString());
                    return operationResult;
                }
            }
        }
        public void InsertFormsConsoleRecord(AppointmentInfo appointmentInfo)
        {
            OperationResult operationResult = new Models.OperationResult();

            if (CheckToSeeIfRecordIsPresent(appointmentInfo.AppointmentId) == false)
            {
                if (appointmentInfo.DateOfBirth == "1/1/0001 12:00:00 AM")
                {
                    appointmentInfo.DateOfBirth = string.Empty;
                }

                using (IDbConnection db = new SqlConnection(ConfigurationValues.FormsConsoleConnection))
                {
                    try
                    {
                        const string query = "INSERT INTO [FormsConsoleData]"
                            + " ("
                            + " [PersonApptDate]"
                            + " ,[PersonApptTime]"
                            + " ,[PersonResource]"
                            + " ,[PersonAppointmentId]"
                            + " ,[PersonResourceID]"
                            + " ,[PersonLocationName]"
                            + " ,[PersonPatientID]"
                            + " ,[PersonTypeAbbreviation]"
                            + " ,[PersonTypeAlternateName]"
                            + " ,[PersonStatus]"
                            + " ,[PersonChiefComplaint]"
                            + " ,[PersonType]"
                            + " ,[PersonFirstName]"
                            + " ,[PersonMiddleName1]"
                            + " ,[PersonLastName]"
                            + " ,[PersonGender]"
                            + " ,[PersonDateOfBirth]"
                            + " ,[PersonCellPhone1]"
                            + " ,[PersonPrimaryPhone]"
                            + " ,[PersonPrimaryWorkPhone]"
                            + " ,[PersonAddressLine1]"
                            + " ,[PersonAddressLine2]"
                            + " ,[PersonAddressCity]"
                            + " ,[PersonAddressState]"
                            + " ,[PersonAddressPostalCode]"
                            + " ,[PersonAddressCounty]"
                            + " ,[AltPatientID]"
                            + " ,[Occupation]"
                            + " ,[wcAddressLine1]"
                            + " ,[wcAddressLine2]"
                            + " ,[wcCity]"
                            + " ,[wcCounty]"
                            + " ,[wcEmployerID]"
                            + " ,[wcFax]"
                            + " ,[wcName]"
                            + " ,[wcPhone]"
                            + " ,[wcPostalCode]"
                            + " ,[wcState]"
                            + " ,[LastModifiedDate]"
                            + " ,[CreateDate]"
                            + " )"
                            + " VALUES"
                            + " ("
                            + " @PersonApptDate,@PersonApptTime,@PersonResource,@PersonAppointmentId,@PersonResourceID,@PersonLocationName"
                            + " ,@PersonPatientID,@PersonTypeAbbreviation,@PersonTypeAlternateName,@PersonStatus,@PersonChiefComplaint"
                            + " ,@PersonType,@PersonFirstName,@PersonMiddleName1,@PersonLastName,@PersonGender,@PersonDateOfBirth"
                            + " ,@PersonCellPhone1,@PersonPrimaryPhone,@PersonPrimaryWorkPhone,@PersonAddressLine1"
                            + " ,@PersonAddressLine2,@PersonAddressCity,@PersonAddressState,@PersonAddressPostalCode,@PersonAddressCounty,@AltPatientID,@Occupation"
                            + " ,@wcAddressLine1,@wcAddressLine2,@wcCity,@wcCounty,@wcEmployerID,@wcFax,@wcName,@wcPhone,@wcPostalCode,@wcState,@LastModifiedDate,@CreateDate"
                            + ")";

                        int rowsAffectd = db.Execute(query, new
                        {
                            @PersonApptDate = appointmentInfo.AppointmentDate.ToShortDateString(),
                            @PersonApptTime = appointmentInfo.AppointmentTime.ToShortTimeString(),
                            @PersonResource = appointmentInfo.Resource,
                            @PersonAppointmentId = appointmentInfo.AppointmentId,
                            @PersonResourceID = appointmentInfo.ResourceId,
                            @PersonLocationName = appointmentInfo.LocationName,
                            @PersonPatientID = appointmentInfo.PatientID,
                            @PersonTypeAbbreviation = appointmentInfo.TypeAbbreviation,
                            @PersonTypeAlternateName = appointmentInfo.TypeAlternateName,
                            @PersonStatus = appointmentInfo.Status,
                            @PersonChiefComplaint = appointmentInfo.ChiefComplaint,
                            @PersonType = appointmentInfo.Type,
                            @PersonFirstName = appointmentInfo.FirstName,
                            @PersonMiddleName1 = appointmentInfo.MiddleName1,
                            @PersonLastName = appointmentInfo.LastName,
                            @PersonDateOfBirth = appointmentInfo.DateOfBirth,
                            @PersonGender = appointmentInfo.Gender,
                            @PersonCellPhone1 = appointmentInfo.CellPhone1,
                            @PersonPrimaryPhone = appointmentInfo.PrimaryPhone,
                            @PersonPrimaryWorkPhone = appointmentInfo.PrimaryWorkPhone,
                            @PersonAddressLine1 = appointmentInfo.AddressLine1,
                            @PersonAddressLine2 = appointmentInfo.AddressLine2,
                            @PersonAddressCity = appointmentInfo.City,
                            @PersonAddressState = appointmentInfo.State,
                            @PersonAddressPostalCode = appointmentInfo.PostalCode,
                            @PersonAddressCounty = appointmentInfo.County,
                            @AltPatientID = appointmentInfo.AltPatientID,
                            @Occupation = appointmentInfo.Occupation,
                            @wcAddressLine1 = appointmentInfo.wcAddressLine1,
                            @wcAddressLine2 = appointmentInfo.wcAddressLine2,
                            @wcCity = appointmentInfo.wcCity,
                            @wcCounty = appointmentInfo.wcCounty,
                            @wcEmployerID = appointmentInfo.wcEmployerID,
                            @wcFax = appointmentInfo.wcFax,
                            @wcName = appointmentInfo.wcName,
                            @wcPhone = appointmentInfo.wcPhone,
                            @wcPostalCode = appointmentInfo.wcPostalCode,
                            @wcState = appointmentInfo.wcState,
                            @LastModifiedDate = DateTime.Now,
                            @CreateDate = appointmentInfo.DateCreated
                        });

                        //operationResult.Success = true;

                    }
                    catch (Exception er)
                    {
                        Utility.LogErrors(ConfigurationValues.ErrorLogPath, er.ToString());
                    }
                }
            }
        }
        public void InsertWorkmancompRecord(WorkmanComp workmanComp)
        {
            OperationResult operationResult = new Models.OperationResult();

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

                    const string query = "INSERT INTO [WorkmanComp]"
                        + " ("
                        + " [EmployerID]"
                        + " ,[AddressLine1]"
                        + " ,[AddressLine2]"
                        + " ,[City]"
                        + " ,[State]"
                        + " ,[PostalCode]"
                        + " ,[County]"
                        + " ,[Phone]"
                        + " ,[Fax]"
                        + " ,[Name]"
                        + ")"
                        + " VALUES"
                        + " ("
                        + " @EmployerID,@AddressLine1,@AddressLine2,@City,@State,@PostalCode,@County,@Phone,@Fax,@Name"
                        + ")";

                    int rowsAffectd = db.Execute(query, new
                    {
                        @EmployerID = workmanComp.EmployerID,
                        @AddressLine1 = workmanComp.AddressLine1,
                        @AddressLine2 = workmanComp.AddressLine2,
                        @City = workmanComp.City,
                        @State = workmanComp.State,
                        @PostalCode = workmanComp.PostalCode,
                        @County = workmanComp.County,
                        @Phone = workmanComp.Phone,
                        @Fax = workmanComp.Fax,
                        @Name = workmanComp.Name
                    });

                    //operationResult.Success = true;

                }
                catch (Exception er)
                {
                    Utility.LogErrors(ConfigurationValues.ErrorLogPath, er.ToString());
                }
            }
        }
        public void InsertGlobalPostOp(GlobalPostOp  globalPostOp)
        {
            OperationResult operationResult = new Models.OperationResult();

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

                    const string query = "INSERT INTO [GlobalPostOp]"
                        + " ("
                        + " [PatientId]"
                        + " ,[DateOfService]"
                        + " ,[ProcedureCode]"
                        + " ,[Charge]"
                        + " ,[Description]"
                        + " ,[RenderingProvider]"
                        + " ,[ChargeID]"
                        + ")"
                        + " VALUES"
                        + " ("
                        + " @PatientId,@DateOfService,@ProcedureCode,@Charge,@Description,@RenderingProvider,@ChargeID"
                        + ") ";


                    int rowsAffectd = db.Execute(query, new
                    {
                        @PatientId = globalPostOp.PatientId,
                        @DateOfService = globalPostOp.ServiceDate,
                        @ProcedureCode = globalPostOp.ProcedureCode,
                        @Charge = globalPostOp.Charge,
                        @Description = globalPostOp.ProcedureDescription,
                        @RenderingProvider = globalPostOp.RenderingProvider,
                        @ChargeID = globalPostOp.ChargeId

                    });

                    //operationResult.Success = true;

                }
                catch (Exception er)
                {
                    Utility.LogErrors(ConfigurationValues.ErrorLogPath, er.ToString());
                }
            }
        }
        public void UpdateLastModifiedDate(DateTime lastModifiedDate)
        {
            OperationResult operationResult = new Models.OperationResult();

            using (IDbConnection db = new SqlConnection(ConfigurationValues.FormsConsoleConnection))
            {
                try
                {
                    const string query = "update [Update]"
                        + " set LastModified = @LastModified";
                    int rowsAffectd = db.Execute(query, new
                    {
                        @LastModified = lastModifiedDate
                    });
                }
                catch (Exception er)
                {
                    Utility.LogErrors(ConfigurationValues.ErrorLogPath, er.ToString());
                }
            }
        }
        // POST: api/SendFax
        public IHttpActionResult Post([FromBody] Fax faxValues)
        {

            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('~');
            string permanentFaxPath = string.Empty;

            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)
                        {
                            archiveDocument = new ArchiveDocument();

                            CurrentDocuments faxTodocument = documentData.GetCurrentDocumentPathPDF(Utility.GetUserName());
                            string[] filesToDelete = faxTodocument.CurrentDocumentList.Split('~');
                            for (int j = 0; j < filesToDelete.Length - 1; j++)
                            {
                                //try
                                //{
                                //    File.Delete(ConfigurationValues.OutboundFaxDirectory + "\\" + Utility.GetUserName() + "\\" + filesToDelete[j]);
                                //}
                                //catch { }
                                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]);
                            }

                            System.IO.File.Delete(ConfigurationValues.TemporaryFaxPath
                                + Utility.GetUserName()
                                + "\\" + faxingInformatonComplete.FaxName);
                        }
                        else
                        {
                            faxData.UpdateFaxRecord(createFaxRecordOperationResult.MessageList[0]);
                        }
                    }
                    else
                    {
                        return BadRequest(createFaxRecordOperationResult.ErrorMessage);
                    }
                }
            }
            catch (Exception er)
            {
                return InternalServerError(er);
            }

            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 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;
                }
            }
        }
        public OperationResult InsertAllSignedNotes(SignedDocument document)
        {
            OperationResult operationResult = new Models.OperationResult();

            if (string.IsNullOrEmpty(document.DocumentID.ToString()))
            {
                document.DocumentID = 0;
            }

            if (string.IsNullOrEmpty(document.PatientID.ToString()))
            {
                document.PatientID = 0;
            }

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

            if (string.IsNullOrEmpty(document.CareProviderID.ToString()))
            {
                document.CareProviderID = 0;
            }

            if (string.IsNullOrEmpty(document.CareProviderName))
            {
                document.CareProviderName = string.Empty;
            }

            if (string.IsNullOrEmpty(document.PrimaryCareProviderID.ToString()))
            {
                document.PrimaryCareProviderID = 0;
            }

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

            if (string.IsNullOrEmpty(document.ReferringCareProviderID.ToString()))
            {
                document.ReferringCareProviderID = 0;
            }

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

            if (string.IsNullOrEmpty(document.DocTypeName))
            {
                document.DocTypeName = string.Empty;
            }

            if (string.IsNullOrEmpty(document.SignerID.ToString()))
            {
                document.SignerID = 0;
            }

            using (IDbConnection db = new SqlConnection(ConfigurationValues.WaldenFaxConnection))
            {
                try
                {
                    const string query = "INSERT INTO [AllSignedNotes]"
                        + " ("
                        + " [DocumentID]"
                        + " ,[VisitID]"
                        + " ,[DateSigned]"
                        + " ,[PatientID]"
                        + " ,[PatientName]"
                        + " ,[CareProviderID]"
                        + " ,[CareProviderName]"
                        + " ,[PrimaryCareProviderID]"
                        + " ,[PrimaryCareProvider]"
                        + " ,[ReferringCareProviderID]"
                        + " ,[ReferringCareProvider]"
                        + " ,[DocTypeName]"
                        + " ,[SignerID]"
                        + ")"
                        + " VALUES"
                        + " ("
                        + " @DocumentID,@VisitID ,@DateSigned , @PatientID, @PatientName, @CareProviderID, @CareProviderName,"
                        + " @PrimaryCareProviderID, @PrimaryCareProvider, @ReferringCareProviderID, @ReferringCareProvider,"
                        + " @DocTypeName, @SignerID"
                        + " )";


                    int rowsAffectd = db.Execute(query, new
                    {
                        @DocumentID = document.DocumentID,
                        @VisitID = document.VisitID,
                        @DateSigned = document.DateSigned,
                        @PatientID = document.PatientID,
                        @PatientName = document.PatientName,
                        @CareProviderID = document.CareProviderID,
                        @CareProviderName = document.CareProviderName,
                        @PrimaryCareProviderID = document.PrimaryCareProviderID,
                        @PrimaryCareProvider = document.PrimaryCareProvider,
                        @ReferringCareProviderID = document.ReferringCareProviderID,
                        @ReferringCareProvider = document.ReferringCareProvider,
                        @DocTypeName = document.DocTypeName,
                        @SignerID = document.SignerID
                    });

                    operationResult.Success = true;
                    operationResult.AddMessage("Success");
                    return operationResult;
                }
                catch (Exception er)
                {
                    Utility.LogErrors(ConfigurationValues.ErrorLogPath, er.ToString());
                    operationResult.Success = false;
                    operationResult.AddMessage(er.ToString());
                    return operationResult;
                }
            }
        }
        //public OperationResult SendFax(MiscFaxInformation miscFaxInformation, string permanentFaxPath, string from, string notes)
        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.WaldenFaxConnection))
            {
                try
                {
                    const 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)";

                    int rowsAffectd = 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;
                }
            }
        }
        // POST: api/SendFax
        public IHttpActionResult Post([FromBody] Fax faxValues)
        {
            OperationResult createFaxRecordOperationResult = new Models.OperationResult();
            OperationResult sendFaxToMultiTechOperationResult = new Models.OperationResult();
            
            FaxData faxData = new FaxData();

            string documentList = faxValues.DocumentList;
            string faxTo = faxValues.FaxTo;
            string notes = faxValues.Notes;
            string cover = faxValues.Cover;
            string from = faxValues.From;

            string faxPath = string.Empty;
            string[] faxes = faxTo.Split('~');
            string permanentFaxPath = string.Empty;

            Misc misc = new Misc();

            try
            {
                for (int i = 0; i < faxes.Length - 1; i++)
                {
                    FaxingInformation faxingInformatonInitial = new FaxingInformation();

                    faxingInformatonInitial.DocumentList = documentList;
                    faxingInformatonInitial.FaxTo = faxes[i];
                    faxingInformatonInitial.Notes = notes;
                    faxingInformatonInitial.From = from;
                    faxingInformatonInitial.Cover = cover;

                    FaxingInformation faxingInformatonComplete = misc.CreateFax(faxingInformatonInitial);

                    permanentFaxPath = Walden.CompleteFax.Library.Database.PernamentFaxPath
                            + Utility.GetUserName() + "\\" + faxingInformatonComplete.NameFromDateTimeString;

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

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

                    createFaxRecordOperationResult = faxData.SendFax(faxingInformatonComplete, ConfigurationValues.PernamentFaxPath
                        + faxingInformatonComplete.NameFromDateTimeString, from, notes);

                    if (createFaxRecordOperationResult.Success)
                    {
                        sendFaxToMultiTechOperationResult = sendFaxMultitech.SendTheFax(faxingInformatonComplete.Name, faxingInformatonComplete.FaxPath,
                            from, faxingInformatonComplete.FaxNumber, createFaxRecordOperationResult.MessageList[0], ConfigurationValues.ApplicationPath);
                        if (sendFaxToMultiTechOperationResult.Success)
                        {
                            System.IO.File.Delete(ConfigurationValues.TemporaryFaxPath
                                + Utility.GetUserName()
                                + "\\" + faxingInformatonComplete.NameFromDateTimeString);
                        }
                        else
                        {
                            faxData.UpdateFaxRecord(createFaxRecordOperationResult.MessageList[0]);
                        }
                    }
                    else
                    {
                        return BadRequest(createFaxRecordOperationResult.ErrorMessage);
                    }
                }
            }
            catch (Exception er)
            {
                return InternalServerError(er);
            }

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

            for (int i = 0; i < faxesToDelete.Length - 1; i++)
            {
                System.IO.File.Delete(ConfigurationValues.TemporaryFaxPath
                    + Utility.GetUserName() + "\\"
                    + faxesToDelete[i]);
            }

            return Ok();
        }
        // DELETE: api/Copier/5
        public IHttpActionResult Delete([FromBody] Copier copier)
        {
            OperationResult operationResult = new Models.OperationResult();
            CopierData copierData = new CopierData();

            operationResult = copierData.DeleteCopier(copier);
            if (operationResult.Success)
            {
                return Ok();
            }
            else
            {
                return BadRequest(operationResult.ErrorMessage);
            }
        }
        public void UpdateStatusPrimaryCareFax(int primaryCareID, string primaryCareStatus)
        {

            OperationResult operationResult = new Models.OperationResult();

            using (IDbConnection db = new SqlConnection(ConfigurationValues.WaldenFaxConnection))
            {
                try
                {
                    const string query = "Update AutomatedFaxesSent"
                        + " set PrimaryCareResult = @PrimaryCareResult"
                        + " where PrimaryCareSendID = @PrimaryCareSendID";

                    int rowsAffectd = db.Execute(query, new
                    {
                        @PrimaryCareSendID = primaryCareID,
                        @PrimaryCareResult = primaryCareStatus
                    });

                    //operationResult.Success = true;

                }
                catch (Exception er)
                {
                    Utility.LogErrors(ConfigurationValues.ErrorLogPath, er.ToString());
                }
            }

        }
        public void InsertProviderDetail(ProviderDetail providerDetail)
        {
            OperationResult operationResult = new Models.OperationResult();

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

                    const string query = "INSERT INTO [ProviderDetail]"
                        + " ("
                        + " [LocationAddressID]"
                        + " ,[AddressID]"
                        + " ,[City]"
                        + " ,[State]"
                        + " ,[PostalCode]"
                        + " ,[ActiveLocation]"
                        + " ,[LocationName]"
                        + " ,[LocationID]"
                        + " ,[AddressLine1]"
                        + " ,[AddressLine2]"
                        + " ,[PhoneNum1]"
                        + " ,[PhoneNum2]"
                        + " ,[Country]"
                        + " )"
                        + " VALUES"
                        + " ("
                        + " @LocationAddressID,@AddressID,@City,@State,@PostalCode,@ActiveLocation,@LocationName,"
                        + " @LocationID,@AddressLine1,@AddressLine2,@PhoneNum1,@PhoneNum2,@Country"
                        + ")";
                    int rowsAffectd = db.Execute(query, new
                    {
                        @LocationAddressID = providerDetail.LocationAddressID,
                        @AddressID = providerDetail.AddressID,
                        @City = providerDetail.City,
                        @State = providerDetail.State,
                        @PostalCode = providerDetail.PostalCode,
                        @ActiveLocation = providerDetail.ActiveLocation,
                        @LocationName = providerDetail.LocationName,
                        @LocationID = providerDetail.LocationID,
                        @AddressLine1 = providerDetail.AddressLine1,
                        @AddressLine2 = providerDetail.AddressLine2,
                        @PhoneNum1 = providerDetail.PhoneNum1,
                        @PhoneNum2 = providerDetail.PhoneNum2,
                        @Country = providerDetail.Country
                    });

                    //operationResult.Success = true;

                }
                catch (Exception er)
                {
                    Utility.LogErrors(ConfigurationValues.ErrorLogPath, er.ToString());
                }
            }
        }
        public OperationResult SendFax(SendFax sendFax)
        {
            OperationResult operationResult = new Models.OperationResult();

            using (IDbConnection db = new SqlConnection(ConfigurationValues.WaldenFaxConnection))
            {
                try
                {
                    const 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)";

                    int rowsAffectd = db.Execute(query, new
                    {
                        @AccountID = sendFax.AccountID,
                        @UserID = sendFax.UserID,
                        @FaxName = sendFax.FaxName,
                        @FaxPath = sendFax.FaxPath,
                        @FaxNumber = sendFax.FaxNumber,
                        @RecipientName = sendFax.RecipientName,
                        @Notes = sendFax.Notes,
                        @PageCount = sendFax.PageCount,
                        @FaxSent = sendFax.FaxSent,
                        @InUse = sendFax.InUse,
                        @ToTif = sendFax.ToTif,
                        @CallWait = sendFax.CallWait,
                        @TimesCalled = sendFax.TimesCalled,
                        @Status = sendFax.Status,
                        @ShowFax = sendFax.ShowFax,
                        @CreateTime = sendFax.CreateTime,
                        @CompletionTime = sendFax.CompletionTime,
                        @DateStamp = sendFax.DateStamp
                    });

                    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;
                }
            }
        }
        public void DeleteProviderDetail()
        {
            OperationResult operationResult = new Models.OperationResult();

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

                    const string query = "delete from [ProviderDetail]"
                    ;
                    int rowsAffectd = db.Execute(query);

                }
                catch (Exception er)
                {
                    Utility.LogErrors(ConfigurationValues.ErrorLogPath, er.ToString());
                }
            }
        }
        public void UpdateMaxAppointmentID(string appointmentId)
        {
            OperationResult operationResult = new Models.OperationResult();

            using (IDbConnection db = new SqlConnection(ConfigurationValues.FormsConsoleConnection))
            {
                try
                {
                    const string query = "update [Update]"
                        + " set AppointmentID = @AppointmentID";
                    int rowsAffectd = db.Execute(query, new
                    {
                        @AppointmentID = appointmentId
                    });

                }
                catch (Exception er)
                {
                    Utility.LogErrors(ConfigurationValues.ErrorLogPath, er.ToString());
                }
            }
        }
        public void InsertSignedDocumentsFromGreenway(List<SignedDocument> signedDocumentList)
        {
            OperationResult operationResult = new Models.OperationResult();

            for (int i = 0; i < signedDocumentList.Count; i++)
                using (IDbConnection db = new SqlConnection(ConfigurationValues.WaldenFaxConnection))
                {
                    try
                    {
                        const string query = "INSERT INTO [SignedDocumentsFromGreenway]"
                            + " ("
                            + " [SequenceNumber]"
                            + " ,[Location]"
                            + " ,[VisitID]"
                            + " ,[DocumentID]"
                            + " ,[DateSigned]"
                            + " ,[PatientID]"
                            + " ,[PatientName]"
                            + " ,[CareProviderID]"
                            + " ,[CareProviderName]"
                            + " ,[PrimaryCareProviderID]"
                            + " ,[PrimaryCareProvider]"
                            + " ,[ReferringCareProviderID]"
                            + " ,[ReferringCareProvider]"
                            + " ,[DocTypeName]"
                            + " ,[SignerID]"
                            + " ,[CreatedByID]"
                            + " )"
                            + " VALUES"
                            + " ("
                            + " @SequenceNumber,@Location,@VisitID,@DocumentID,@DateSigned,@PatientID,@PatientName"
                            + " ,@CareProviderID,@CareProviderName,@PrimaryCareProviderID,@PrimaryCareProvider"
                            + " ,@ReferringCareProviderID,@ReferringCareProvider,@DocTypeName,@SignerID,@CreatedByID"
                            + ")";

                        int rowsAffectd = db.Execute(query,new 
                        {
                            @SequenceNumber = signedDocumentList[i].SequenceNumber,
                            @Location = signedDocumentList[i].Location,
                            @VisitID = signedDocumentList[i].VisitID,
                            @DocumentID = signedDocumentList[i].DocumentID,
                            @DateSigned = signedDocumentList[i].DateSigned,
                            @PatientID = signedDocumentList[i].PatientID,
                            @PatientName = signedDocumentList[i].PatientName,
                            @CareProviderID = signedDocumentList[i].CareProviderID,
                            @CareProviderName = signedDocumentList[i].CareProviderName,
                            @PrimaryCareProviderID = signedDocumentList[i].PrimaryCareProviderID,
                            @PrimaryCareProvider = signedDocumentList[i].PrimaryCareProvider,
                            @ReferringCareProviderID = signedDocumentList[i].ReferringCareProviderID,
                            @ReferringCareProvider = signedDocumentList[i].ReferringCareProvider,
                            @DocTypeName = signedDocumentList[i].DocTypeName,
                            @SignerID = signedDocumentList[i].SignerID,
                            CreatedByID = signedDocumentList[i].CreatedById
                        });
                    }
                    catch (Exception er)
                    {
                        Logging.LogErrors(ConfigurationValues.ErrorLogPath, er.ToString());
                    }
            }
        }