/// <summary>
        /// This method returns false if referral link already exists
        /// </summary>
        /// <param name="referralLink">referralLink</param>
        /// <param name="organizationID">organizationID</param>
        /// <returns></returns>
        public bool CheckDuplicateReferralLink(string referralLink, int organizationID)
        {
            try
            {
                using (var unitOfWork = new EFUnitOfWork())
                {
                    var ibRepo =
                        new IntroducingBrokerRepository(new EFRepository <IntroducingBroker>(), unitOfWork);

                    ObjectSet <IntroducingBroker> ibObjSet =
                        ((CurrentDeskClientsEntities)ibRepo.Repository.UnitOfWork.Context).IntroducingBrokers;

                    var linkExists = ibObjSet.Where(link => link.CustomizedLink == referralLink && link.FK_OrganizationID == organizationID).ToList();
                    if (linkExists.Count() > 0)
                    {
                        return(false);
                    }
                    else
                    {
                        return(true);
                    }
                }
            }
            catch (Exception ex)
            {
                CommonErrorLogger.CommonErrorLog(ex, System.Reflection.MethodBase.GetCurrentMethod().Name);
                throw;
            }
        }
        /// <summary>
        /// This method saves referral link in IB table
        /// </summary>
        /// <param name="userID">userID</param>
        /// <param name="referralLink">referralLink</param>
        /// <returns></returns>
        public bool SaveReferralLink(int userID, string referralLink)
        {
            try
            {
                using (var unitOfWork = new EFUnitOfWork())
                {
                    var ibRepo =
                        new IntroducingBrokerRepository(new EFRepository <IntroducingBroker>(), unitOfWork);

                    ObjectSet <IntroducingBroker> ibObjSet =
                        ((CurrentDeskClientsEntities)ibRepo.Repository.UnitOfWork.Context).IntroducingBrokers;

                    var ibRow = ibObjSet.Include("User").Where(usr => usr.User.PK_UserID == userID).FirstOrDefault();
                    if (ibRow != null)
                    {
                        ibRow.CustomizedLink = referralLink;
                        ibRepo.Save();
                        return(true);
                    }
                    return(false);
                }
            }
            catch (Exception ex)
            {
                CommonErrorLogger.CommonErrorLog(ex, System.Reflection.MethodBase.GetCurrentMethod().Name);
                throw;
            }
        }
        /// <summary>
        /// This Function Will Update Joint Account Contact Information Of Introducing Broker
        /// </summary>
        /// <param name="userID">userID</param>
        /// <param name="jointAccountInformation">jointAccountInformation</param>
        /// <returns>result</returns>
        public bool UpdateJointContactInforamation(int userID, JointAccountInformation jointAccountInformation)
        {
            try
            {
                using (var unitOfWork = new EFUnitOfWork())
                {
                    var introducingBrokerRepo =
                        new IntroducingBrokerRepository(new EFRepository <IntroducingBroker>(), unitOfWork);

                    ObjectSet <IntroducingBroker> introducingBrokerObjSet =
                        ((CurrentDeskClientsEntities)introducingBrokerRepo.Repository.UnitOfWork.Context).IntroducingBrokers;

                    var updateRow = introducingBrokerObjSet.Where(x => x.FK_UserID == userID).
                                    Include("JointAccountInformations").Include("User").FirstOrDefault();

                    //Update particular column values
                    updateRow.JointAccountInformations.FirstOrDefault().TelephoneNumber = jointAccountInformation.TelephoneNumber;
                    updateRow.JointAccountInformations.FirstOrDefault().MobileNumber    = jointAccountInformation.MobileNumber;
                    updateRow.User.UserEmailID = jointAccountInformation.EmailAddress;
                    //if (updateRow.AssetManagers.FirstOrDefault() != null)
                    //{
                    //    updateRow.AssetManagers.FirstOrDefault().UserEmail = jointAccountInformation.EmailAddress;
                    //}

                    //Save changes
                    introducingBrokerRepo.Save();
                }
                return(true);
            }
            catch (Exception ex)
            {
                CommonErrorLogger.CommonErrorLog(ex, System.Reflection.MethodBase.GetCurrentMethod().Name);
                return(true);
            }
        }
        /// <summary>
        /// This Function Will Update The Joint-Account Personal Information of Introducing Broker
        /// </summary>
        /// <param name="userID">userID</param>
        /// <param name="phoneID">PhoneID</param>
        /// <returns>result</returns>
        public bool UpdateJointPersonalInformation(int userID, string phoneID)
        {
            try
            {
                using (var unitOfWork = new EFUnitOfWork())
                {
                    var introducingBrokerRepo =
                        new IntroducingBrokerRepository(new EFRepository <IntroducingBroker>(), unitOfWork);

                    ObjectSet <IntroducingBroker> introducingBrokerObjSet =
                        ((CurrentDeskClientsEntities)introducingBrokerRepo.Repository.UnitOfWork.Context).IntroducingBrokers;

                    var updateRow = introducingBrokerObjSet.Where(x => x.FK_UserID == userID).
                                    Include("JointAccountInformations").FirstOrDefault();

                    //Update particular column value
                    updateRow.JointAccountInformations.FirstOrDefault().PhoneID = phoneID;

                    //Save changes
                    introducingBrokerRepo.Save();
                }
                return(true);
            }
            catch (Exception ex)
            {
                CommonErrorLogger.CommonErrorLog(ex, System.Reflection.MethodBase.GetCurrentMethod().Name);
                return(false);
            }
        }
        /// <summary>
        /// This Function Will get all the Platform logins of all the clients
        /// of respective introducing broker.
        /// </summary>
        /// <returns></returns>
        public List <int?> GetPlatformLoginsIntroducingBroker(int userID)
        {
            try
            {
                using (var unitOfWork = new EFUnitOfWork())
                {
                    var introducingBrokerRepo =
                        new IntroducingBrokerRepository(new EFRepository <IntroducingBroker>(), unitOfWork);

                    //Get IntroducingBrokedID
                    var introducingBrokerID = GetBrokerIDFromBrokerUserID(userID);

                    //ObjectSet<IntroducingBroker> introducingBrokerObjSet =
                    //  ((CurrentDeskClientsEntities)introducingBrokerRepo.Repository.UnitOfWork.Context).IntroducingBrokers;

                    //var introducingBrokerList = introducingBrokerObjSet.Include("Client_Account").
                    //                            Where(x => x.PK_IntroducingBrokerID == introducingBrokerID).ToList();

                    var context           = ((CurrentDeskClientsEntities)introducingBrokerRepo.Repository.UnitOfWork.Context);
                    var platformLoginList = (from ib in context.IntroducingBrokers
                                             join cl in context.Clients on ib.PK_IntroducingBrokerID equals cl.FK_IntroducingBrokerID
                                             join ca in context.Client_Account on cl.PK_ClientID equals ca.FK_ClientID
                                             where (ib.PK_IntroducingBrokerID == introducingBrokerID && ca.PlatformLogin != null)
                                             select ca.PlatformLogin).ToList();

                    return(platformLoginList);
                }
            }
            catch (Exception ex)
            {
                CommonErrorLogger.CommonErrorLog(ex, System.Reflection.MethodBase.GetCurrentMethod().Name);
                throw;
            }
        }
        /// <summary>
        /// This method returns partner name based on userIDs
        /// </summary>
        /// <param name="userID">userID</param>
        /// <returns></returns>
        public string GetPartnerName(int userID)
        {
            try
            {
                using (var unitOfWork = new EFUnitOfWork())
                {
                    var introducingBrokerRepo =
                        new IntroducingBrokerRepository(new EFRepository <IntroducingBroker>(), unitOfWork);


                    ObjectSet <IntroducingBroker> introducingBrokerObjSet =
                        ((CurrentDeskClientsEntities)introducingBrokerRepo.Repository.UnitOfWork.Context).IntroducingBrokers;

                    //Get The Selected tunning and assign its Properties.
                    var selectedIB =
                        introducingBrokerObjSet.Where(ib => ib.FK_UserID == userID).FirstOrDefault();

                    //Check for nullability
                    if (selectedIB != null)
                    {
                        //Get account type details
                        var accTypeBO          = new AccountTypeBO();
                        var accountTypeDetails = accTypeBO.GetAccountTypeAndFormTypeValue((int)selectedIB.FK_AccountTypeID);

                        if (accountTypeDetails.FK_AccountTypeValue == Constants.K_ACCT_INDIVIDUAL)
                        {
                            var individualAccountBO = new IndividualAccountInformationBO();
                            return(individualAccountBO.GetPartnerIndividualName(selectedIB.PK_IntroducingBrokerID));
                        }
                        else if (accountTypeDetails.FK_AccountTypeValue == Constants.K_ACCT_JOINT)
                        {
                            var jointAccountBO = new JointAccountInformationBO();
                            return(jointAccountBO.GetPartnerIndividualName(selectedIB.PK_IntroducingBrokerID));
                        }
                        else if (accountTypeDetails.FK_AccountTypeValue == Constants.K_ACCT_CORPORATE)
                        {
                            var corporateAccountBO = new CorporateAccountInformationBO();
                            return(corporateAccountBO.GetPartnerIndividualName(selectedIB.PK_IntroducingBrokerID));
                        }
                        else if (accountTypeDetails.FK_AccountTypeValue == Constants.K_ACCT_TRUST)
                        {
                            var trustAccountBO = new TrustAccountInformationBO();
                            return(trustAccountBO.GetPartnerIndividualName(selectedIB.PK_IntroducingBrokerID));
                        }
                    }

                    return(String.Empty);
                }
            }
            catch (Exception ex)
            {
                CommonErrorLogger.CommonErrorLog(ex, System.Reflection.MethodBase.GetCurrentMethod().Name);
                return(String.Empty);
            }
        }
        /// <summary>
        /// This method returns Introducing Broker details
        /// </summary>
        /// <param name="userID">userID</param>
        /// <param name="accountType">accountType</param>
        /// <param name="accountCode">accountCode</param>
        /// <param name="userDisplayName">userDisplayName</param>
        /// <param name="organizationID">organizationID</param>
        /// <returns></returns>
        public bool GetClientAccountInformation(int userID, ref int accountType, ref int accountCode, ref string userDisplayName, ref int organizationID)
        {
            try
            {
                using (var unitOfWork = new EFUnitOfWork())
                {
                    var ibRepo =
                        new IntroducingBrokerRepository(new EFRepository <IntroducingBroker>(), unitOfWork);

                    ObjectSet <IntroducingBroker> ibObjSet =
                        ((CurrentDeskClientsEntities)ibRepo.Repository.UnitOfWork.Context).IntroducingBrokers;

                    var selectedClient = ibObjSet.Where(usr => usr.FK_UserID == userID).FirstOrDefault();

                    if (selectedClient != null)
                    {
                        accountType    = (int)selectedClient.FK_AccountTypeID;
                        accountCode    = (int)selectedClient.FK_AccountID;
                        organizationID = selectedClient.FK_OrganizationID;

                        if (accountType == Constants.K_PARTNER_INDIVIDUAL)
                        {
                            var individualAccountBO = new IndividualAccountInformationBO();
                            userDisplayName = individualAccountBO.GetLiveIndividualName(selectedClient.PK_IntroducingBrokerID, LoginAccountType.PartnerAccount);
                        }
                        else if (accountType == Constants.K_PARTNER_JOINT)
                        {
                            var jointAccountBO = new JointAccountInformationBO();
                            userDisplayName = jointAccountBO.GetLiveIndividualName(selectedClient.PK_IntroducingBrokerID, LoginAccountType.PartnerAccount);
                        }
                        else if (accountType == Constants.K_PARTNER_CORPORATE)
                        {
                            var corporateAccountBO = new CorporateAccountInformationBO();
                            userDisplayName = corporateAccountBO.GetLiveIndividualName(selectedClient.PK_IntroducingBrokerID, LoginAccountType.PartnerAccount);
                        }
                        else if (accountType == Constants.K_PARTNER_TRUST)
                        {
                            var trustAccountBO = new TrustAccountInformationBO();
                            userDisplayName = trustAccountBO.GetLiveIndividualName(selectedClient.PK_IntroducingBrokerID, LoginAccountType.PartnerAccount);
                        }
                        return(true);
                    }
                    return(false);
                }
            }
            catch (Exception ex)
            {
                CommonErrorLogger.CommonErrorLog(ex, System.Reflection.MethodBase.GetCurrentMethod().Name);
                throw;
            }
        }
        /// <summary>
        /// This method gets cfd trade volume for a particular day
        /// </summary>
        /// <param name="introducingBrokerID">introducingBrokerID</param>
        /// <param name="fromDate">fromDate</param>
        /// <param name="toDate">toDate</param>
        /// <returns></returns>
        public double GetCFDTradesVolumeByDay(int introducingBrokerID, long fromDate, long toDate)
        {
            try
            {
                using (var unitOfWork = new EFUnitOfWork())
                {
                    var ibRepo =
                        new IntroducingBrokerRepository(new EFRepository <IntroducingBroker>(), unitOfWork);

                    return(((CurrentDeskClientsEntities)ibRepo.Repository.UnitOfWork.Context).GetCFDTradesVolumeByDay(introducingBrokerID, fromDate, toDate).FirstOrDefault() ?? 0);
                }
            }
            catch (Exception ex)
            {
                CommonErrorLogger.CommonErrorLog(ex, System.Reflection.MethodBase.GetCurrentMethod().Name);
                throw;
            }
        }
        /// <summary>
        /// This function will insert introducing broker details
        /// </summary>
        /// <param name="newIB">newIB</param>
        public void AddIntroducingBrokerDetails(IntroducingBroker newIB)
        {
            try
            {
                using (var unitOfWork = new EFUnitOfWork())
                {
                    var IBDetailsRepo =
                        new IntroducingBrokerRepository(new EFRepository <IntroducingBroker>(), unitOfWork);

                    IBDetailsRepo.Add(newIB);
                    IBDetailsRepo.Save();
                }
            }
            catch (Exception ex)
            {
                CommonErrorLogger.CommonErrorLog(ex, System.Reflection.MethodBase.GetCurrentMethod().Name);
                throw;
            }
        }
        /// <summary>
        /// This method returns broker pkID from brokerUserID
        /// </summary>
        /// <param name="brokerUserID">brokerUserID</param>
        /// <returns></returns>
        public int GetBrokerIDFromBrokerUserID(int brokerUserID)
        {
            try
            {
                using (var unitOfWork = new EFUnitOfWork())
                {
                    var ibRepo =
                        new IntroducingBrokerRepository(new EFRepository <IntroducingBroker>(), unitOfWork);

                    ObjectSet <IntroducingBroker> ibObjSet =
                        ((CurrentDeskClientsEntities)ibRepo.Repository.UnitOfWork.Context).IntroducingBrokers;

                    return((int)ibObjSet.Where(ib => ib.FK_UserID == brokerUserID).FirstOrDefault().PK_IntroducingBrokerID);
                }
            }
            catch (Exception ex)
            {
                CommonErrorLogger.CommonErrorLog(ex, System.Reflection.MethodBase.GetCurrentMethod().Name);
                throw;
            }
        }
        /// <summary>
        /// This method brokerID based on referral keyword
        /// </summary>
        /// <param name="referralKeyword">referralKeyword</param>
        /// <param name="organizationID">organizationID</param>
        /// <returns></returns>
        public int GetBrokerIDFromReferralKeyword(string referralKeyword, int organizationID)
        {
            try
            {
                using (var unitOfWork = new EFUnitOfWork())
                {
                    var ibRepo =
                        new IntroducingBrokerRepository(new EFRepository <IntroducingBroker>(), unitOfWork);

                    ObjectSet <IntroducingBroker> ibObjSet =
                        ((CurrentDeskClientsEntities)ibRepo.Repository.UnitOfWork.Context).IntroducingBrokers;

                    return(ibObjSet.Where(ib => ib.CustomizedLink == referralKeyword && ib.FK_OrganizationID == organizationID).FirstOrDefault().PK_IntroducingBrokerID);
                }
            }
            catch (Exception ex)
            {
                CommonErrorLogger.CommonErrorLog(ex, System.Reflection.MethodBase.GetCurrentMethod().Name);
                throw;
            }
        }
        /// <summary>
        /// This method updates Trustee Individual Auth Officer Contact Info based on FK_UserID from Introducing Broker table
        /// </summary>
        /// <param name="userID">userID</param>
        /// <param name="telCountryCode">telCountryCode</param>
        /// <param name="telNumber">telNumber</param>
        /// <param name="mobileCountryCode">mobileCountryCode</param>
        /// <param name="mobileNumber">mobileNumber</param>
        /// <param name="secEmailID">secEmailID</param>
        /// <returns>bool</returns>
        public bool UpdateTrusteeIndividualAuthOfficerContactInfo(int userID, string telCountryCode, string telNumber, string mobileCountryCode, string mobileNumber, string secEmailID)
        {
            try
            {
                using (var unitOfWork = new EFUnitOfWork())
                {
                    var introducingBrokerRepo =
                        new IntroducingBrokerRepository(new EFRepository <IntroducingBroker>(), unitOfWork);

                    ObjectSet <IntroducingBroker> introducingBrokerObjSet =
                        ((CurrentDeskClientsEntities)introducingBrokerRepo.Repository.UnitOfWork.Context).IntroducingBrokers;

                    var updateRow = introducingBrokerObjSet.Where(x => x.FK_UserID == userID).
                                    Include("TrustAccountInformations").Include("AssetManagers").FirstOrDefault();

                    if (updateRow != null)
                    {
                        //Update particular column values
                        updateRow.TrustAccountInformations.FirstOrDefault().TrusteeIndividualTelephoneNumber = telCountryCode + "-" + telNumber;
                        updateRow.TrustAccountInformations.FirstOrDefault().TrusteeIndividualMobileNumber    = mobileCountryCode + "-" + mobileNumber;
                        updateRow.UserEmail = secEmailID;
                        if (updateRow.AssetManagers.FirstOrDefault() != null)
                        {
                            updateRow.AssetManagers.FirstOrDefault().UserEmail = secEmailID;
                        }

                        //Save changes
                        introducingBrokerRepo.Save();
                        return(true);
                    }
                }

                return(false);
            }
            catch (Exception ex)
            {
                CommonErrorLogger.CommonErrorLog(ex, System.Reflection.MethodBase.GetCurrentMethod().Name);
                return(true);
            }
        }
        /// <summary>
        /// This method returns individual account details of IB
        /// </summary>
        /// <param name="userID">userID</param>
        /// <returns></returns>
        public IntroducingBroker GetIndividualAccountDetails(int userID)
        {
            try
            {
                using (var unitOfWork = new EFUnitOfWork())
                {
                    var introducingBrokerRepo =
                        new IntroducingBrokerRepository(new EFRepository <IntroducingBroker>(), unitOfWork);

                    ObjectSet <IntroducingBroker> introducingBrokerObjSet =
                        ((CurrentDeskClientsEntities)introducingBrokerRepo.Repository.UnitOfWork.Context).IntroducingBrokers;

                    return(introducingBrokerObjSet.Where(x => x.FK_UserID == userID)
                           .Include("IndividualAccountInformations").Include("BankAccountInformations").Include("User").Include("Client_Account").FirstOrDefault());
                }
            }
            catch (Exception ex)
            {
                CommonErrorLogger.CommonErrorLog(ex, System.Reflection.MethodBase.GetCurrentMethod().Name);
                throw;
            }
        }
        /// <summary>
        /// This function will Provide IB information
        /// depending Upon FK_UserID
        /// </summary>
        /// <param name="userID"></param>
        /// <returns></returns>
        public IntroducingBroker GetClientInformation(int userID)
        {
            var currentDeskSecurity = new CurrentDeskSecurity();

            try
            {
                using (var unitOfWork = new EFUnitOfWork())
                {
                    var introducingBrokerRepo =
                        new IntroducingBrokerRepository(new EFRepository <IntroducingBroker>(), unitOfWork);

                    ObjectSet <IntroducingBroker> introducingBrokerObjSet =
                        ((CurrentDeskClientsEntities)introducingBrokerRepo.Repository.UnitOfWork.Context).IntroducingBrokers;

                    //Get The Selected tunning and assign its Properties.
                    return(introducingBrokerObjSet.Where(ib => ib.FK_UserID == userID).FirstOrDefault());
                }
            }
            catch (Exception ex)
            {
                CommonErrorLogger.CommonErrorLog(ex, System.Reflection.MethodBase.GetCurrentMethod().Name);
                throw;
            }
        }
        /// <summary>
        /// This method returns list of partner names and userID
        /// </summary>
        /// <param name="userID">users</param>
        /// <returns></returns>
        public List <BrokerClients> GetPartnerNames(List <User> users, int accountNumberPosition)
        {
            try
            {
                //Get all userids
                var listOfUsersID = users.Select(x => x.PK_UserID).ToList();

                using (var unitOfWork = new EFUnitOfWork())
                {
                    var ibRepo =
                        new IntroducingBrokerRepository(new EFRepository <IntroducingBroker>(), unitOfWork);

                    var context =
                        ((CurrentDeskClientsEntities)ibRepo.Repository.UnitOfWork.Context);

                    //Get the selected client and assign its Properties.
                    var selectedClient =
                        context.IntroducingBrokers.Where(brk => listOfUsersID.Contains((int)brk.FK_UserID))
                        .Include("IndividualAccountInformations")
                        .Include("JointAccountInformations")
                        .Include("CorporateAccountInformations")
                        .Include("TrustAccountInformations")
                        .Include("Client_Account")
                        .ToList();


                    List <BrokerClients> lstBrokerClients = new List <BrokerClients>();

                    //Check for nullability
                    if (selectedClient != null)
                    {
                        foreach (var item in selectedClient)
                        {
                            var    brokerClient = new BrokerClients();
                            string brokerName   = string.Empty;

                            //Get account type details
                            var accTypeBO          = new AccountTypeBO();
                            var accountTypeDetails = accTypeBO.GetAccountTypeAndFormTypeValue((int)item.FK_AccountTypeID);

                            if (accountTypeDetails.FK_AccountTypeValue == Constants.K_ACCT_INDIVIDUAL)
                            {
                                var liveInfo = item.IndividualAccountInformations.FirstOrDefault();
                                brokerName = (liveInfo != null ? liveInfo.FirstName + " " + liveInfo.LastName : null) + " - " + (item.Client_Account.FirstOrDefault() != null ? item.Client_Account.FirstOrDefault().LandingAccount.Split('-')[accountNumberPosition] : "");
                            }
                            else if (accountTypeDetails.FK_AccountTypeValue == Constants.K_ACCT_JOINT)
                            {
                                var jointInfo = item.JointAccountInformations.FirstOrDefault();
                                brokerName = (jointInfo != null ? jointInfo.PrimaryAccountHolderFirstName + " " + jointInfo.PrimaryAccountHolderLastName : null) + " - " + (item.Client_Account.FirstOrDefault() != null ? item.Client_Account.FirstOrDefault().LandingAccount.Split('-')[accountNumberPosition] : "");
                            }
                            else if (accountTypeDetails.FK_AccountTypeValue == Constants.K_ACCT_CORPORATE)
                            {
                                var corInfo = item.CorporateAccountInformations.FirstOrDefault();
                                brokerName = (corInfo != null ? corInfo.CompanyName : null) + " - " + (item.Client_Account.FirstOrDefault() != null ? item.Client_Account.FirstOrDefault().LandingAccount.Split('-')[accountNumberPosition] : "");
                            }
                            else if (accountTypeDetails.FK_AccountTypeValue == Constants.K_ACCT_TRUST)
                            {
                                var trustInfo = item.TrustAccountInformations.FirstOrDefault();
                                brokerName = (trustInfo != null ? trustInfo.TrustName : null) + " - " + (item.Client_Account.FirstOrDefault() != null ? item.Client_Account.FirstOrDefault().LandingAccount.Split('-')[accountNumberPosition] : "");
                            }

                            brokerClient.UserID      = (int)item.FK_UserID;
                            brokerClient.DisplayName = brokerName;

                            //Add to list
                            lstBrokerClients.Add(brokerClient);
                        }
                    }

                    return(lstBrokerClients);
                }
            }
            catch (Exception ex)
            {
                CommonErrorLogger.CommonErrorLog(ex, System.Reflection.MethodBase.GetCurrentMethod().Name);
                throw;
            }
        }