//Delete document by id and file path
        public static bool Delete(int Id, string FilePath)
        {
            bool IsDeleted = false;
            try
            {

                DocumentDTO DocumentDTO = new DocumentDTO();
                DocumentDTO = GetById(Id);
                GlobalSettings.LoggedInClientId = DocumentDTO.ClientId;
                GlobalSettings.LoggedInUserId = DocumentDTO.UserId;
                int PartnerId = ClientService.GetById(DocumentDTO.ClientId).PartnerId;
                GlobalSettings.LoggedInPartnerId = PartnerId;

                IsDeleted = CommonService.RemoveDocument(FilePath); //+ DocumentDTO.Path
                if (IsDeleted != false)
                {
                    UnitOfWork uow = new UnitOfWork();
                    uow.DocumentRepo.Delete(Id);
                    uow.SaveChanges();
                }
                return IsDeleted;
            }
            catch
            {
                throw;
            }
        }
        //Create document
        public static int Create(DocumentDTO DocumentDTO)
        {
            try
            {
                GlobalSettings.LoggedInClientId = DocumentDTO.ClientId;
                GlobalSettings.LoggedInUserId = DocumentDTO.UserId;
                int PartnerId = ClientService.GetById(DocumentDTO.ClientId).PartnerId;
                GlobalSettings.LoggedInPartnerId = PartnerId;

                var Document = new Document();
                using (var uow = new UnitOfWork())
                {
                    Document = Transform.DocumentToDomain(DocumentDTO);
                    uow.DocumentRepo.Insert(Document);
                    uow.SaveChanges();
                    return (Document.Id);

                }

            }
            //catch (LoggedInUserException)
            //{
            //    throw new System.TimeoutException();
            //}
            catch (Exception)
            {
                throw;
            }
        }
        //Save contact document to the folder
        public static string SaveDocumentToFolder(HttpPostedFile file, string documentPath, int ClientId, int UserId, string FileName)
        {
            try
            {
                var b = new byte[file.ContentLength];
                string result = "";
                documentPath = documentPath + ClientId; //"\\" + ModuleName
                MemoryStream ms = new MemoryStream(b);
                // MemoryStream ms = new MemoryStream(file.ContentLength);
                bool IsExists = System.IO.Directory.Exists(documentPath);
                if (IsExists == false)
                {
                    System.IO.Directory.CreateDirectory(documentPath);
                }

                var path = System.IO.Path.Combine(documentPath, FileName); //file.FileName

                if (File.Exists(path))
                {
                    result = "File already Exists";
                    return result;
                }
                else
                {
                    file.SaveAs(documentPath + "/" + FileName); //file.FileName
                    ms.Close();

                    DocumentDTO DocumentDTO = new DocumentDTO();
                    DocumentDTO.ClientId = ClientId;
                    DocumentDTO.CreatedOn = System.DateTime.Now;
                    DocumentDTO.FileName = FileName;// file.FileName;
                    DocumentDTO.Path = ClientId + "/" + FileName;// file.FileName;
                    DocumentDTO.UserId = UserId;

                    GlobalSettings.LoggedInClientId = ClientId;
                    int PartnerId = ClientService.GetById(ClientId).PartnerId;
                    GlobalSettings.LoggedInPartnerId = PartnerId;
                    GlobalSettings.LoggedInUserId = UserId;

                    DocumentService.Create(DocumentDTO);
                    result = "File uploaded successfully";
                    return result;

                }
            }
            catch (Exception ex)
            {
                return ex.ToString();
            }
        }
        //Get document list by client id
        public static List<DocumentDTO> GetDocumentListByClientId(int ClientId)
        {
            List<DocumentDTO> DocumentDTOList = new List<DocumentDTO>();

            try
            {

                using (var uow = new UnitOfWork())
                {
                    IEnumerable<Document> Document = uow.DocumentRepo.GetAll().Where(e => e.ClientId == ClientId);
                    if (Document != null)
                    {
                        foreach (var item in Document)
                        {
                            DocumentDTO DocumentDTO = new DocumentDTO();
                            DocumentDTO = Transform.DocumentToDTO(item);
                            UserDTO UserDTO = new UserDTO();
                            UserDTO = UserService.GetById(DocumentDTO.UserId);
                            DocumentDTO.User = UserDTO.Name;

                            ClientDTO ClientDTO = new ClientDTO();
                            ClientDTO = ClientService.GetById(DocumentDTO.ClientId);
                            DocumentDTO.Client = ClientDTO.Company;

                            DocumentDTOList.Add(DocumentDTO);
                        }
                    }
                }

                return DocumentDTOList;
            }
            //catch (LoggedInUserException)
            //{
            //    throw new System.TimeoutException();
            //}
            catch (Exception)
            {

                throw;
            }
        }
 public static Document DocumentToDomain(DocumentDTO DocumentDTO)
 {
     if (DocumentDTO == null) return null;
      Mapper.CreateMap<DocumentDTO, Document>();
      Document Document = Mapper.Map<Document>(DocumentDTO);
      return Document;
 }