/// <summary>
        //// Gets the branch view.
        /// </summary>
        // / <param name = "branchView" > The branch view.</param>
        //  / <param name = "message" > The message.</param>
        // / <returns></returns>
        public IBranchListView GetBranchView(IBranchListView branchView, string message)
        {
            var jurisdiction = this.generalRepository.GetJurisdiction();

            jurisdiction = jurisdiction.Where(x => x.IsActive == true).ToList();
            return(this.agentOfDeductionFactory.CreateBranchView(branchView, message, jurisdiction));
        }
        /// <summary>
        /// Saves the branch user.
        /// </summary>
        /// <param name="branchUserView">The branch user view.</param>
        /// <returns></returns>
        /// <exception cref="System.ArgumentNullException">branchUserView</exception>
        public string SaveBranchUser(IBranchListView branchUserView)
        {
            var result = string.Empty;

            if (branchUserView == null)
            {
                throw new ArgumentNullException(nameof(branchUserView));
            }

            var view = new UserBranch
            {
                BranchId           = branchUserView.BranchId,
                IsActive           = true,
                UserRegistrationId = branchUserView.UserId
            };

            try
            {
                using (
                    var dbContext = (PitalyticsEntities)this.dbContextFactory.GetDbContext())
                {
                    dbContext.UserBranches.Add(view);
                    dbContext.SaveChanges();
                }
            }
            catch (Exception e)
            {
                result = string.Format("SaveUserBranch - {0} , {1}", e.Message,
                                       e.InnerException != null ? e.InnerException.Message : "");
            }

            return(result);
        }
        /// <summary>
        /// Saves the branch information.
        /// </summary>
        /// <param name="branchView">The branch view.</param>
        /// <returns></returns>
        /// <exception cref="System.ArgumentException">AdminSaveTiming</exception>
        public string SaveBranchInfo(IBranchListView branchView, out int branchId)
        {
            var result = string.Empty;

            if (branchView == null)
            {
                throw new ArgumentException("AdminSaveTiming");
            }

            var newRecord = new Branch
            {
                Description = branchView.BranchName,
                IsActive    = true,
                BranchName  = branchView.BranchName
            };

            try
            {
                using (
                    var dbContext = (PitalyticsEntities)this.dbContextFactory.GetDbContext())
                {
                    dbContext.Branches.Add(newRecord);

                    dbContext.SaveChanges();
                }
            }
            catch (Exception e)
            {
                result = string.Format("SaveBranch - {0} , {1}", e.Message,
                                       e.InnerException != null ? e.InnerException.Message : "");
            }
            branchId = newRecord.BranchId;
            return(result);
        }
        /// <summary>
        /// Edits the branch.
        /// </summary>
        /// <param name="branchView">The branch view.</param>
        /// <returns></returns>
        /// <exception cref="System.ArgumentNullException">
        /// branchView
        /// or
        /// branchDetails
        /// </exception>
        public string EditBranch(IBranchListView branchView)
        {
            var result = string.Empty;

            if (branchView == null)
            {
                throw new ArgumentNullException(nameof(branchView));
            }
            try
            {
                using (
                    var dbContext = (PitalyticsEntities)this.dbContextFactory.GetDbContext())
                {
                    var branchDetails = dbContext.Branches.SingleOrDefault(x => x.BranchId == branchView.BranchId);
                    if (branchDetails == null)
                    {
                        throw new ArgumentNullException(nameof(branchDetails));
                    }

                    branchDetails.BranchName = branchView.BranchName;
                    dbContext.SaveChanges();
                }
            }
            catch (Exception e)
            {
                result = string.Format("SaveBranch - {0} , {1}", e.Message,
                                       e.InnerException != null ? e.InnerException.Message : "");
            }

            return(result);
        }
        /// <summary>
        /// Gets the updated branch user ListView.
        /// </summary>
        /// <param name="branchUserListView">The branch user ListView.</param>
        /// <param name="processingMessage">The processing message.</param>
        /// <returns></returns>
        public IBranchListView GetUpdatedBranchUserListView(IBranchListView branchUserListView, string processingMessage)
        {
            var agentId = (int)session.GetSessionValue(SessionKey.AgentOfDeductionId);;
            var userId  = (int)session.GetSessionValue(SessionKey.UserId);

            var users = this.accountRepository.GetUserRegistrationsById(userId);

            var branchType = this.agentOfDeductionRepository.GetAgentOfdeductionBranch(agentId);     //list of branch


            return(this.agentOfDeductionFactory.GetUpdatedBranchUserListView(users, branchType, branchUserListView, processingMessage));
        }
        /// <summary>
        /// Creates the branch view.
        /// </summary>
        /// <param name="branchView">The branch view.</param>
        /// <param name="processingMessage">The processing message.</param>
        /// <returns></returns>
        /// <exception cref="System.ArgumentException">branchView</exception>
        public IBranchListView CreateBranchView(IBranchListView branchView, string processingMessage, IList <IJurisdiction> jurisdictions)
        {
            if (branchView == null)
            {
                throw new ArgumentException("branchView");
            }
            var jurisdictionDDL = GetJurisdictionDropdown.GetJurisdicions(jurisdictions, branchView.JurisdictionId);

            branchView.ProcessingMessage = processingMessage;
            branchView.JurisdictionNames = jurisdictionDDL;
            return(branchView);
        }
        /// <summary>
        /// Creates the branch view.
        /// </summary>
        /// <param name="branchView">The branch view.</param>
        /// <returns></returns>
        /// <exception cref="System.ArgumentNullException">branchView</exception>
        public IBranchListView CreateBranchView(IBranchListView branchView)
        {
            if (branchView == null)
            {
                throw new ArgumentNullException(nameof(branchView));
            }
            var view = new BranchListView
            {
                ProcessingMessage = "",
                Description       = branchView.Description,
                BranchId          = branchView.BranchId
            };

            return(view);
        }
        /// <summary>
        /// Processes the branch information.
        /// </summary>
        /// <param name="branchView">The branch view.</param>
        /// <returns></returns>
        public string ProcessBranchInfo(IBranchListView branchView)
        {
            var processingMessages = string.Empty;

            var dataValue     = this.agentOfDeductionRepository.GetBranchDescriptionByValue(branchView.BranchName);
            var isRecordExist = (dataValue == null) ? false : true;

            if (isRecordExist)
            {
                processingMessages = Messages.BranchExist;
                return(processingMessages);
            }
            var branchId   = 0;
            var branchInfo = this.agentOfDeductionRepository.SaveBranchInfo(branchView, out branchId);

            if (string.IsNullOrEmpty(branchInfo))
            {
                var            userId             = (int)session.GetSessionValue(SessionKey.UserId);;
                BranchListView branchUserListView = new BranchListView
                {
                    BranchId = branchId,
                    UserId   = userId
                };
                var agentId = (int)session.GetSessionValue(SessionKey.AgentOfDeductionId);;

                AgentOfDeductionBranchView agentOfDeductionView = new AgentOfDeductionBranchView()
                {
                    AgentOfDeductionId = agentId,
                    BranchId           = branchId
                };
                BranchJurisdictionView branchJurisdictionView = new BranchJurisdictionView()
                {
                    BranchId       = branchId,
                    JurisdictionId = branchView.JurisdictionId
                };
                branchInfo = this.agentOfDeductionRepository.SaveBranchUser(branchUserListView);
                branchInfo = this.agentOfDeductionRepository.SaveAgentOfDeductionBranch(agentOfDeductionView);
                branchInfo = this.agentOfDeductionRepository.SaveJurisdictionBranch(branchJurisdictionView);
            }



            return(branchInfo);
        }
        /// <summary>
        /// Updates the branch information.
        /// </summary>
        /// <param name="branchView">The branch view.</param>
        /// <returns></returns>
        /// <exception cref="System.ArgumentNullException">branchView</exception>
        public string UpdateBranchInfo(IBranchListView branchView)
        {
            if (branchView == null)
            {
                throw new ArgumentNullException(nameof(branchView));
            }


            var processingMessages = string.Empty;

            var dataValue     = this.agentOfDeductionRepository.GetBranchDescriptionByValue(branchView.BranchName);
            var isRecordExist = (dataValue == null) ? false : true;

            if (isRecordExist)
            {
                processingMessages = Messages.BranchExist;
                return(processingMessages);
            }


            var editBranch = this.agentOfDeductionRepository.EditBranch(branchView);

            return(editBranch);
        }
 /// <summary>
 /// Saves the branch user information.
 /// </summary>
 /// <param name="branchUserView">The branch user view.</param>
 /// <returns></returns>
 public string SaveBranchUserInfo(IBranchListView branchUserView)
 {
     return(this.agentOfDeductionRepository.SaveBranchUser(branchUserView));
 }
        /// <summary>
        /// Gets the updated branch user ListView.
        /// </summary>
        /// <param name="userRegistrations">The user registrations.</param>
        /// <param name="branches">The branches.</param>
        /// <param name="branchUserListView">The branch user ListView.</param>
        /// <param name="processingMessage">The processing message.</param>
        /// <returns></returns>
        public IBranchListView GetUpdatedBranchUserListView(IList <IUserRegistration> userRegistrations, IList <IBranch> agentOfDeductionBranches, IBranchListView branchUserListView, string processingMessage)
        {
            var userType = GetUserDropdown.UserListItems(userRegistrations, branchUserListView.UserId);

            var branchType = GetBranchDropdownList.BranchListItems(agentOfDeductionBranches, branchUserListView.BranchId);


            var view = new BranchListView
            {
                BranchNames       = branchType,
                UserNames         = userType,
                ProcessingMessage = processingMessage
            };

            return(view);
        }