/// <summary>
        /// CreatedBy:Asanka Senarathna
        /// CreatedDate:6/27/2016
        /// Get Active Loans for Inactive
        /// </summary>
        /// <param name="company_Id"></param>
        /// <param name="branchId"></param>
        /// <param name="roleId"></param>
        /// <returns></returns>
        public LoanSelection GetActiveLoanforInactive( int companyId, int branchId, int roleId)
        {
            LoanSelection detailList = new LoanSelection();
            DataHandler dataHandler = new DataHandler();
            List<object[]> paramertList = new List<object[]>();

            paramertList.Add(new object[] { "@companyId", companyId });
            paramertList.Add(new object[] { "@branchId", branchId });
            paramertList.Add(new object[] { "@roleId", roleId });
            try
            {
                DataSet dataSet = dataHandler.GetDataSet("spGetActiveLoansforInactive", paramertList);
                if (dataSet != null && dataSet.Tables.Count != 0)
                {
                    List<Branch> RegBranches = new List<Branch>();
                    List<NonRegBranch> NonRegBranchList = new List<NonRegBranch>();
                    List<LoanSetupStep1> LoanList = new List<LoanSetupStep1>();

                    foreach (DataRow dataRow in dataSet.Tables[0].Rows)
                    {
                        Branch branch = new Branch();
                        NonRegBranch nonRegBranch = new NonRegBranch();
                        LoanSetupStep1 loan = new LoanSetupStep1();

                        branch.BranchId = int.Parse(dataRow["branch_id"].ToString());
                        branch.BranchName = dataRow["regBranchName"].ToString();

                        nonRegBranch.NonRegBranchId = int.Parse(dataRow["non_reg_branch_id"].ToString());
                        nonRegBranch.BranchId = branch.BranchId;
                        nonRegBranch.CompanyNameBranchName = dataRow["nonRegBranchName"].ToString();

                        loan.loanId = int.Parse(dataRow["loan_id"].ToString());
                        loan.loanNumber = dataRow["loan_number"].ToString();
                        loan.loanCode = dataRow["loan_code"].ToString();
                        loan.loanAmount = decimal.Parse(dataRow["loan_amount"].ToString());
                        loan.CreatedDate = DateTime.Parse(dataRow["created_date"].ToString());
                        loan.startDate = DateTime.Parse(dataRow["start_date"].ToString());
                        loan.maturityDate = DateTime.Parse(dataRow["maturity_date"].ToString());
                        loan.CurrentLoanStatus = bool.Parse(dataRow["loan_status"].ToString());

                        loan.nonRegisteredBranchId = nonRegBranch.NonRegBranchId;
                        bool checkBranch = false;
                        bool checkNonRegBranch = false;
                        bool checkLoan = false;
                        foreach (var br in RegBranches)
                        {
                            if (br.BranchId == branch.BranchId)
                            {
                                checkBranch = true;
                            }
                        }
                        if (checkBranch == false)
                        {
                            RegBranches.Add(branch);
                        }
                        foreach (var nrbr in NonRegBranchList)
                        {
                            if (nrbr.NonRegBranchId == nonRegBranch.NonRegBranchId)
                            {
                                checkNonRegBranch = true;
                            }
                        }
                        if (checkNonRegBranch == false)
                        {
                            NonRegBranchList.Add(nonRegBranch);
                        }

                        foreach (var l in LoanList)
                        {
                            if (l.loanId == loan.loanId)
                            {
                                checkLoan = true;
                            }
                        }
                        if (checkLoan == false)
                        {
                            LoanList.Add(loan);
                        }

                    }
                    detailList.RegBranches = RegBranches;
                    detailList.NonRegBranchList = NonRegBranchList;
                    detailList.LoanList = LoanList;

                    return detailList;
                }
                else
                {
                    return null;
                }
            }

            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// CreatedBy:Irfan
        /// CreatedDate:2016/02/11
        /// Get Non Reg Branch by non reg branch Id
        /// 
        /// UpdatedBy : nadeeka
        /// UpdatedDate: 2016/03/04
        /// removed existing connection open method and set parameter to object list and pass stored procedure name
        /// call DataHandler class method and getting dataset object,
        /// return partner branch object using dataset object    
        /// 
        /// </summary>
        /// <param name="branch Id"></param>
        /// <returns></returns>
        public NonRegBranch getNonRegBranchByNonRegBranchId(int nonRegBranchId)
        {
            DataHandler dataHandler = new DataHandler();
            List<object[]> paramertList = new List<object[]>();
            paramertList.Add(new object[] { "@non_reg_branch_id", nonRegBranchId });
            try
            {
            DataSet dataSet = dataHandler.GetDataSet("spGetNonRegBranchByNonRegBranchId", paramertList);
                if (dataSet != null && dataSet.Tables.Count != 0 && dataSet.Tables[0].Rows.Count != 0)
                {
                    DataRow dataRow = dataSet.Tables[0].Rows[0];
                    NonRegBranch branch = new NonRegBranch();
                    branch.BranchId = int.Parse(dataRow["branch_id"].ToString());
                    branch.NonRegBranchId = int.Parse(dataRow["non_reg_branch_id"].ToString());
                    branch.BranchCode = dataRow["branch_code"].ToString();
                    branch.BranchName = dataRow["branch_name"].ToString();
                    branch.BranchAddress1 = dataRow["branch_address_1"].ToString();
                    branch.BranchAddress2 = dataRow["branch_address_2"].ToString();
                    branch.BranchCity = dataRow["city"].ToString();
                    branch.CompanyNameBranchName = dataRow["company_name"].ToString();
                    branch.NonRegCompanyId = int.Parse(dataRow["company_id"].ToString());
                    branch.BranchEmail = dataRow["email"].ToString();
                    return branch;
                }
                else
                {
                    return null;
                }
        }

            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// CreatedBy : Kasun
        /// CreatedDate: 2016/03/30
        /// 
        /// get permissioned loans with branch , non reg branch and loan details
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public LoanSelection GetPermisssionGivenLoanwithBranchDeatils(int userId,int companyId,int? branchId,int roleId)
        {
            LoanSelection detailList = new LoanSelection();
            DataHandler dataHandler = new DataHandler();
            List<object[]> paramertList = new List<object[]>();

            paramertList.Add(new object[] { "@userId", userId });
            paramertList.Add(new object[] { "@companyId", companyId });
            paramertList.Add(new object[] { "@branchId", branchId });
            paramertList.Add(new object[] { "@roleId", roleId });
            try
            {
                DataSet dataSet = dataHandler.GetDataSet("spRetrivePermissionGivenLoans", paramertList);
                if (dataSet != null && dataSet.Tables.Count != 0)
                {
                    List<Branch> RegBranches = new List<Branch>();
                    List<NonRegBranch> NonRegBranchList = new List<NonRegBranch>();
                    List<LoanSetupStep1> LoanList =  new List<LoanSetupStep1>();

                    foreach (DataRow dataRow in dataSet.Tables[0].Rows)
                    {
                        Branch branch = new Branch();
                        NonRegBranch nonRegBranch = new NonRegBranch();
                        LoanSetupStep1 loan = new LoanSetupStep1();

                        branch.BranchId = int.Parse(dataRow["branch_id"].ToString());
                        branch.BranchName = dataRow["regBranchName"].ToString();

                        nonRegBranch.NonRegBranchId = int.Parse(dataRow["non_reg_branch_id"].ToString());
                        nonRegBranch.BranchId = branch.BranchId;
                        nonRegBranch.CompanyNameBranchName = dataRow["nonRegBranchName"].ToString();

                        loan.loanId = int.Parse(dataRow["loan_id"].ToString());
                        loan.loanNumber = dataRow["loan_number"].ToString();
                        loan.loanCode = dataRow["loan_code"].ToString();
                        loan.rightId = dataRow["right_id"].ToString();
                        loan.loanAmount = decimal.Parse(dataRow["loan_amount"].ToString());
                        loan.startDate = DateTime.Parse(dataRow["start_date"].ToString());
                        loan.maturityDate = DateTime.Parse(dataRow["maturity_date"].ToString()) ;
                        loan.CreatedDate = DateTime.Parse(dataRow["created_date"].ToString()) ;

                        if (dataRow["is_title_tracked"].ToString() != null && dataRow["is_title_tracked"].ToString() != "") {
                            loan.titleTracked = bool.Parse(dataRow["is_title_tracked"].ToString());
                        }
                        if (!string.IsNullOrEmpty(dataRow["has_lot_inspection_fee"].ToString()))
                        {
                            if (bool.Parse(dataRow["has_lot_inspection_fee"].ToString()))
                            {
                                loan.LotInspectionFee = 1;
                            }
                            else
                            {
                                loan.LotInspectionFee = 0;
                            }
                        }

                        else
                        {
                            loan.LotInspectionFee = 0;
                        }
                        if (!string.IsNullOrEmpty(dataRow["has_monthly_loan_fee"].ToString()))
                        {
                            if (bool.Parse(dataRow["has_monthly_loan_fee"].ToString()))
                            {
                                loan.MonthlyLoanFee = 1;
                            }
                            else
                            {
                                loan.MonthlyLoanFee = 0;
                            }
                        }

                        else
                        {
                            loan.MonthlyLoanFee = 0;
                        }
                        if (!string.IsNullOrEmpty(dataRow["has_advance_fee"].ToString()))
                        {
                            if (bool.Parse(dataRow["has_advance_fee"].ToString()))
                            {
                                loan.AdvanceFee = 1;
                            }
                            else
                            {
                                loan.AdvanceFee = 0;
                            }
                            if (!string.IsNullOrEmpty(dataRow["payment_due_method"].ToString()))
                            {
                                if (dataRow["payment_due_method"].ToString().Contains("Vehicle Payoff"))
                                {
                                    loan.AdvanceFeePayAtPayoff = true;
                                }
                                else
                                {
                                    loan.AdvanceFeePayAtPayoff = false;
                                }

                            }
                        }

                        else
                        {
                            loan.AdvanceFee = 0;
                        }
                        loan.nonRegisteredBranchId = nonRegBranch.NonRegBranchId;
                        bool checkBranch = false;
                        bool checkNonRegBranch = false;
                        bool checkLoan = false;
                        foreach (var br in RegBranches) {
                            if (br.BranchId == branch.BranchId) {
                                checkBranch = true;
                            }
                        }
                        if (checkBranch == false) {
                            RegBranches.Add(branch);
                        }
                        foreach (var nrbr in NonRegBranchList)
                        {
                            if (nrbr.NonRegBranchId == nonRegBranch.NonRegBranchId)
                            {
                                checkNonRegBranch = true;
                            }
                        }
                        if (checkNonRegBranch == false)
                        {
                            NonRegBranchList.Add(nonRegBranch);
                        }

                        foreach (var l in LoanList)
                        {
                            if (l.loanId == loan.loanId)
                            {
                                checkLoan = true;
                            }
                        }
                        if (checkLoan == false)
                        {
                            LoanList.Add(loan);
                        }
                        
                    }
                    detailList.RegBranches = RegBranches;
                    detailList.NonRegBranchList = NonRegBranchList;
                    detailList.LoanList = LoanList;

                    return detailList;
                }
                else
                {
                    return null;
                }
            }

            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// CreatedBy: MAM. IRFAN
        /// CreatedDate: 2016/02/05
        /// 
        /// Getting all non reistered company branches by Registered company id
        /// 
        /// </summary>
        /// <returns> a list contain all branches</returns>
        /// 
        public List<NonRegBranch> getNonRegBranchesNonCompId(int nonRegCompanyId)
        {
            List<NonRegBranch> branchesLists = new List<NonRegBranch>();
            DataHandler dataHandler = new DataHandler();
            List<object[]> paramertList = new List<object[]>();
            paramertList.Add(new object[] { "@non_reg_companyId", nonRegCompanyId });
            try
            {
            DataSet dataSet = dataHandler.GetDataSet("spGetNonRegBranchesByNonRegCompanyId", paramertList);
            if (dataSet != null && dataSet.Tables.Count != 0)
            {
                foreach (DataRow dataRow in dataSet.Tables[0].Rows)
                {
                    NonRegBranch branch = new NonRegBranch();
                    branch.NonRegBranchId = int.Parse(dataRow["non_reg_branch_id"].ToString());

                    branch.BranchName = dataRow["branch_name"].ToString();
                    branch.BranchCode = dataRow["branch_code"].ToString();
                    branch.BranchId = int.Parse(dataRow["branch_id"].ToString());
                    branch.BranchAddress1 = dataRow["branch_address_1"].ToString();
                    branch.BranchAddress2 = dataRow["branch_address_2"].ToString();
                    branch.StateId = Convert.ToInt32(dataRow["state_id"].ToString());
                    branch.BranchCity = dataRow["city"].ToString();
                    branch.BranchZip = dataRow["zip"].ToString();
                    branch.CompanyNameBranchName = dataRow["company_name"].ToString() + " - " + dataRow["branch_name"].ToString();
                    string[] zipWithExtention = branch.BranchZip.Split('-');

                    if (zipWithExtention[0] != null) branch.ZipPre = zipWithExtention[0];
                    if (zipWithExtention.Count() >= 2 && zipWithExtention[1] != null) branch.Extention = zipWithExtention[1];

                    branch.BranchEmail = dataRow["email"].ToString();
                    branch.BranchPhoneNum1 = dataRow["phone_num_1"].ToString();
                    branch.BranchPhoneNum2 = dataRow["phone_num_2"].ToString();
                    branch.BranchPhoneNum3 = dataRow["phone_num_3"].ToString();
                    branch.BranchFax = dataRow["fax"].ToString();
                    branch.BranchCompany = Convert.ToInt32(dataRow["company_id"]);
                    branchesLists.Add(branch);
                }

                return branchesLists;
            }
            else
            {
                return null;
            }
        }

            catch (Exception ex)
            {
                throw ex;
            }
        }