/// <summary>
        /// Validates the UFN number against the specified earner ID
        /// </summary>
        /// <param name="logonId">Login ID</param>
        /// <param name="collectionRequest">Collection request</param>
        /// <param name="criteria">Criteria for </param>
        /// <returns></returns>
        public UFNReturnValue UFNValidation(Guid logonId, Guid earnerId, DateTime UFNDate, string UFNNumber)
        {
            UFNReturnValue returnValue = new UFNReturnValue();

            try
            {
                // Get the logged on user from the current logons and add their
                // ApplicationSettings the list of concurrent sessions.
                Host.LoadLoggedOnUser(logonId);

                try
                {
                    Functions.RestrictRekoopIntegrationUser(UserInformation.Instance.DbUid);
                    switch (UserInformation.Instance.UserType)
                    {
                        case DataConstants.UserType.Staff:
                        case DataConstants.UserType.Client:
                        case DataConstants.UserType.ThirdParty:
                            // Can do everything
                            break;
                        default:
                            throw new Exception("Access denied");
                    }

                    SrvMatter srvMatter = new SrvMatter();
                    string errorMessage = string.Empty;
                    string warningMessage = string.Empty;

                    srvMatter.FeeEarnerMemberId = earnerId;
                    srvMatter.SetDefaultsOnFeeEarnerMemberId();

                    srvMatter.UFNValue = UFNDate.Date.Day.ToString().PadLeft(2, '0') + UFNDate.Date.Month.ToString().PadLeft(2, '0') +
                       UFNDate.Date.Year.ToString().Substring(2, 2) + "/" + UFNNumber;

                    srvMatter.UFNDate = UFNDate;
                    bool isValid = false;

                    if (UFNNumber == null)
                    {
                        isValid = srvMatter.setUniqueFileNumberValuesByDate(out errorMessage, out warningMessage);
                    }
                    else
                    {
                        srvMatter.UFNNumber = UFNNumber;
                        isValid = srvMatter.setUniqueFileNumberValuesByNumber(out errorMessage, out warningMessage);
                    }

                    if (!isValid)
                    {
                        returnValue.Success = false;
                        returnValue.Message = "The system has detected that the UFN reference entered is not valid in this instance. If the user is attempting to edit an existing UFN reference, the system will only permit the next available reference outside of the defined Fee Earner ranges.";
                    }
                    else
                    {
                        returnValue.Id = earnerId;
                        returnValue.Date = srvMatter.UFNDate;
                        returnValue.Number = srvMatter.UFNNumber;
                        returnValue.Value = srvMatter.UFNValue;
                    }
                }
                finally
                {
                    // Remove the logged on user's ApplicationSettings from the
                    // list of concurrent sessions
                    Host.UnloadLoggedOnUser();
                }
            }
            catch (System.Data.SqlClient.SqlException)
            {
                returnValue.Success = false;
                returnValue.Message = Functions.SQLErrorMessage;
            }
            catch (Exception ex)
            {
                returnValue.Success = false;
                returnValue.Message = ex.Message;
            }

            return returnValue;
        }
        /// <summary>
        /// Add a new matter
        /// </summary>
        /// <param name="logonId">Logon id obtained when logging on to the logon service</param>
        /// <param name="matter">Matter details</param>
        /// <returns></returns>
        public MatterReturnValue AddMatter(Guid logonId, Matter matter)
        {
            MatterReturnValue returnValue = new MatterReturnValue();

            try
            {
                // Get the logged on user from the current logons and add their
                // ApplicationSettings the list of concurrent sessions.
                Host.LoadLoggedOnUser(logonId);

                try
                {
                    Functions.RestrictRekoopIntegrationUser(UserInformation.Instance.DbUid);
                    switch (UserInformation.Instance.UserType)
                    {
                        case DataConstants.UserType.Staff:
                            // Can do everything
                            break;
                        case DataConstants.UserType.Client:
                        case DataConstants.UserType.ThirdParty:
                            throw new Exception("Access denied");
                        default:
                            throw new Exception("Access denied");
                    }

                    // Ensure we have permission
                    if (!UserSecuritySettings.GetUserSecuitySettings(182))
                        throw new Exception("You do not have sufficient permissions to carry out this request");

                    // Verify Annual Licence
                    if (!IRIS.Law.PmsBusiness.LicenseDetails.AnnualLicenseIsValid())
                    {
                        throw new Exception("Unable to add Matter. Your Annual Licence has expired or is invalid.");
                    }

                    SrvMatter srvMatter = new SrvMatter();

                    srvMatter.ClientId = matter.ClientId;
                    srvMatter.MatterDescription = matter.Description;
                    srvMatter.MatterPartnerMemberId = matter.PartnerMemberId;
                    srvMatter.FeeEarnerMemberId = matter.FeeEarnerMemberId;
                    srvMatter.WorkCategoryFranchised = matter.Franchised;
                    //Call SetDefaultsOnFeeEarnerMemberId in order to set the userId
                    srvMatter.SetDefaultsOnFeeEarnerMemberId();
                    srvMatter.UFNNumber = matter.UFN;
                    srvMatter.UFNDate = matter.UFNDate;
                    srvMatter.IsPublicFunding = matter.IsPublicFunding;
                    srvMatter.ClientBankId = matter.ClientBankId;
                    srvMatter.OfficeBankId = matter.OfficeBankId;
                    srvMatter.ChargeDescriptionId = matter.ChargeDescriptionId;
                    srvMatter.CurrentWorkTypeId = matter.WorkTypeId;
                    srvMatter.DepartmentId = matter.DepartmentId;
                    srvMatter.BranchId = matter.BranchReference;
                    srvMatter.CourtId = matter.CourtId;
                    srvMatter.MatterTypeId = matter.MatterTypeId;
                    srvMatter.ClientHOUCN = matter.HOUCN;
                    srvMatter.EarnerReference = matter.FeeEarnerReference;
                    srvMatter.ConOrgMemIdCollection = new NameValueCollection();
                    foreach (JointClientCandidateSearchItem client in matter.JointClientCandidates.Rows)
                    {
                        srvMatter.ConOrgMemIdCollection.Add(client.OrganisationId, client.MemberId);
                    }

                    string errorMessage;

                    returnValue.Success = srvMatter.Save(out errorMessage);
                    returnValue.Message = errorMessage;

                    matter.Id = srvMatter.ProjectId;
                    returnValue.Matter = matter;
                }
                finally
                {
                    // Remove the logged on user's ApplicationSettings from the
                    // list of concurrent sessions
                    Host.UnloadLoggedOnUser();
                }
            }
            catch (System.Data.SqlClient.SqlException)
            {
                returnValue.Success = false;
                returnValue.Message = Functions.SQLErrorMessage;
            }
            catch (Exception ex)
            {
                returnValue.Success = false;
                returnValue.Message = ex.Message;
            }

            return returnValue;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="logonId"></param>
        /// <param name="criteria"></param>
        /// <returns></returns>
        public WorkTypeSearchReturnValue GetValuesOnWorkTypeSelected(Guid logonId, WorkTypeSearchCriteria criteria)
        {
            WorkTypeSearchReturnValue returnValue = new WorkTypeSearchReturnValue();
            string errorMessage = string.Empty;
            string warningMessage = string.Empty;
            bool isValid = false;

            try
            {
                Host.LoadLoggedOnUser(logonId);
                try
                {
                    Functions.RestrictRekoopIntegrationUser(UserInformation.Instance.DbUid);
                    switch (UserInformation.Instance.UserType)
                    {
                        case DataConstants.UserType.Staff:
                        case DataConstants.UserType.Client:
                        case DataConstants.UserType.ThirdParty:
                            // Can do everything
                            break;
                        default:
                            throw new Exception("Access denied");
                    }

                    // Create the dataset
                    SrvMatter serviceMatter = new SrvMatter();
                    //Set the client id and call the  client validation method which will set the client ref
                    //Cli ref is required to get the ucn and houcn values which are set in SetDefaultsOnWorkType
                    serviceMatter.ClientId = criteria.ClientId;
                    serviceMatter.ValidateClientId(out errorMessage, out warningMessage);
                    serviceMatter.CurrentWorkTypeId = criteria.Id;
                    isValid = serviceMatter.ValidateCurrentWorkTypeId(out errorMessage, out warningMessage);

                    // TODO: Does not use the criteria: FilterString, OrganisationID, DepartmentId, DepartmentNo, IsPrivateClient, MatterTypeId

                    if (isValid)
                    {
                        serviceMatter.SetDefaultsOnWorkType();
                        returnValue.IsPublicFunded = serviceMatter.IsPublicFunding;
                        returnValue.DisbLimit = serviceMatter.WorkTypeDisbLimit;
                        returnValue.OverallLimit = serviceMatter.WorkTypeOverallLimit;
                        returnValue.Quote = serviceMatter.WorkTypeQuote;
                        returnValue.TimeLimit = serviceMatter.WorkTypeTimeLimit;
                        returnValue.WipLimit = serviceMatter.WorkTypeWipLimit;
                        returnValue.ChargeRateDescriptionId = serviceMatter.ChargeDescriptionId;
                        returnValue.Franchised = serviceMatter.WorkCategoryFranchised;
                        returnValue.WorkCategoryUFN = serviceMatter.WorkCategoryUFN;
                        returnValue.ClientHOUCN = serviceMatter.ClientHOUCN;
                        returnValue.ClientUCN = serviceMatter.ClientUCN;
                    }
                    else
                    {
                        returnValue.Success = false;
                        returnValue.Message = errorMessage;
                    }

                }
                finally
                {
                    Host.UnloadLoggedOnUser();
                }
            }
            catch (System.Data.SqlClient.SqlException)
            {
                returnValue.Success = false;
                returnValue.Message = Functions.SQLErrorMessage;
            }
            catch (Exception ex)
            {
                returnValue.Success = false;
                returnValue.Message = ex.Message;
            }
            return returnValue;
        }
        /// <summary>
        /// Get one matter
        /// </summary>
        /// <param name="logonId">Logon id obtained when logging on to the logon service</param>
        /// <param name="projectId">Matter project id</param>
        /// <returns></returns>
        public MatterReturnValue GetMatter(Guid logonId, Guid projectId)
        {
            MatterReturnValue returnValue = new MatterReturnValue();

            try
            {
                // Get the logged on user from the current logons and add their
                // ApplicationSettings the list of concurrent sessions.
                // ApplicationSettings.Instance can now be used to get the
                // ApplicationSettings for this session.
                Host.LoadLoggedOnUser(logonId);

                try
                {
                    Functions.RestrictRekoopIntegrationUser(UserInformation.Instance.DbUid);
                    switch (UserInformation.Instance.UserType)
                    {
                        case DataConstants.UserType.Staff:
                            // Can do everything
                            break;
                        case DataConstants.UserType.Client:
                        case DataConstants.UserType.ThirdParty:
                            if (!SrvMatterCommon.WebAllowedToAccessMatter(projectId))
                                throw new Exception("Access denied");
                            break;
                        default:
                            throw new Exception("Access denied");
                    }

                    SrvMatter srvMatter = new SrvMatter();
                    int campaignId = SrvMatterCommon.GetCampaignForProject(projectId);

                    srvMatter.Load(projectId);

                    Matter matter = new Matter();
                    matter.Id = srvMatter.ProjectId;

                    // Load Matter Details
                    matter.Description = srvMatter.MatterDescription;
                    matter.KeyDescription = srvMatter.MatterKeyDescription;
                    matter.FeeEarnerMemberId = srvMatter.FeeEarnerMemberId;
                    matter.PartnerMemberId = srvMatter.MatterPartnerMemberId;
                    matter.WorkTypeId = srvMatter.CurrentWorkTypeId;
                    matter.ClientBankId = srvMatter.ClientBankId;
                    matter.OfficeBankId = srvMatter.OfficeBankId;
                    matter.DepositBankId = srvMatter.MatterDepositBankId;
                    matter.BranchReference = srvMatter.BranchId;
                    matter.DepartmentId = srvMatter.DepartmentId;
                    matter.ChargeDescriptionId = srvMatter.ChargeDescriptionId;
                    matter.CourtId = srvMatter.CourtId;
                    matter.OpenDate = srvMatter.MatterOpenDate;
                    matter.NextReviewDate = srvMatter.MatterNextReviewDate;
                    matter.CostReviewDate = srvMatter.MatterCostReviewDate;
                    matter.LastSavedDate = srvMatter.MatterLastSaved;
                    matter.ClosedDate = srvMatter.MatterClosedDate;
                    matter.DestructDate = srvMatter.MatterDestructDate;
                    matter.FileNo = srvMatter.MatterFileNo;
                    matter.CompletedDate = srvMatter.MatterCompleted;
                    matter.SpanType1Ref = srvMatter.SpanType1;
                    matter.SpanType2Ref = srvMatter.SpanType2;

                    // Load Matter Additional Info
                    matter.Quote = srvMatter.MatterQuote;
                    matter.DisbsLimit = srvMatter.MatterDisbsLimit;
                    matter.TimeLimit = srvMatter.MatterTimeLimit;
                    matter.WIPLimit = srvMatter.MatterWIPLimit;
                    matter.OverallLimit = srvMatter.MatterOverallLimit;
                    matter.Status = srvMatter.MatterStatus;
                    matter.Indicators = srvMatter.MatterIndicators;
                    matter.BankReference = srvMatter.MatterBankReference;
                    matter.CashCollectionId = srvMatter.MatCashCollID;
                    matter.TotalLockup = srvMatter.MatTotalLockup;
                    matter.OurReference = srvMatter.OurReference;
                    matter.PreviousReference = srvMatter.PreviousReference;
                    matter.BusinessSourceId = srvMatter.SourceID;
                    matter.SourceCampaignId = campaignId;
                    matter.PersonDealingId = srvMatter.UserId;
                    matter.SalutationEnvelope = srvMatter.MatterSalutationEnvelope;
                    matter.SalutationLetter = srvMatter.MatterSalutationLetter;
                    matter.LetterHead = srvMatter.MatterLetterHead;

                    // Load Public Funding
                    matter.MatterLegalAided = srvMatter.MatterLegalAided;
                    matter.IsPublicFunding = srvMatter.IsPublicFunding;
                    matter.Franchised = srvMatter.MatterFranchised;
                    matter.isLondonRate = srvMatter.MatterLondonRate;
                    matter.UFNDate = srvMatter.UFNDate;
                    matter.UFN = srvMatter.UFNNumber;
                    matter.PFCertificateNo = srvMatter.MatterPFCertificateNo;
                    matter.PFCertificateNoLimits = srvMatter.MatterPFCertificateLimits;
                    matter.MatterTypeId = srvMatter.MatterTypeId;

                    // Load Client Details
                    Client client = new Client();
                    client.IsMember = srvMatter.IsMember;

                    //Set MemberID for Individual
                    if (client.IsMember)
                    {
                        client.MemberId = srvMatter.ClientId;
                        client.OrganisationId = DataConstants.DummyGuid;
                    }
                    //Set OrganisationId for Organisation
                    else
                    {
                        client.MemberId = DataConstants.DummyGuid;
                        client.OrganisationId = srvMatter.ClientId;
                    }

                    client.Reference = srvMatter.ClientReference;
                    client.FullName = srvMatter.ClientName;
                    client.IsArchived = srvMatter.IsClientArchived;

                    // Get Client Type ID
                    client.TypeId = this.GetClientType(srvMatter.ClientId, srvMatter.ProjectId, srvMatter.IsMember);

                    returnValue.Matter = matter;
                    returnValue.ClientDetails = client;
                }
                finally
                {
                    // Remove the logged on user's ApplicationSettings from the
                    // list of concurrent sessions
                    Host.UnloadLoggedOnUser();
                }
            }
            catch (System.Data.SqlClient.SqlException)
            {
                returnValue.Success = false;
                returnValue.Message = Functions.SQLErrorMessage;
            }
            catch (Exception ex)
            {
                returnValue.Success = false;
                returnValue.Message = ex.Message;
            }

            return returnValue;
        }
        /// <summary>
        /// Update an existing matter
        /// </summary>
        /// <param name="logonId">Logon id obtained when logging on to the logon service</param>
        /// <param name="matter">Matter details</param>
        /// <returns></returns>
        public ReturnValue UpdateMatter(Guid logonId, Matter matter)
        {
            ReturnValue returnValue = new ReturnValue();

            try
            {
                // Get the logged on user from the current logons and add their
                // ApplicationSettings the list of concurrent sessions.
                Host.LoadLoggedOnUser(logonId);

                try
                {
                    Functions.RestrictRekoopIntegrationUser(UserInformation.Instance.DbUid);
                    switch (UserInformation.Instance.UserType)
                    {
                        case DataConstants.UserType.Staff:
                            // Can do everything
                            break;
                        case DataConstants.UserType.Client:
                        case DataConstants.UserType.ThirdParty:
                            throw new Exception("Access denied");
                        default:
                            throw new Exception("Access denied");
                    }

                    SrvMatter srvMatter = new SrvMatter();
                    srvMatter.Load(matter.Id);

                    // Save Matter Details
                    srvMatter.ProjectId = matter.Id;
                    srvMatter.MatterDescription = matter.Description;
                    srvMatter.MatterKeyDescription = matter.KeyDescription;
                    srvMatter.FeeEarnerMemberId = matter.FeeEarnerMemberId;
                    srvMatter.MatterPartnerMemberId = matter.PartnerMemberId;
                    srvMatter.CurrentWorkTypeId = matter.WorkTypeId;
                    srvMatter.ClientBankId = matter.ClientBankId;
                    srvMatter.OfficeBankId = matter.OfficeBankId;
                    srvMatter.MatterDepositBankId = matter.DepositBankId;
                    srvMatter.BranchId = matter.BranchReference;
                    srvMatter.DepartmentId = matter.DepartmentId;
                    srvMatter.ChargeDescriptionId = matter.ChargeDescriptionId;
                    srvMatter.CourtId = matter.CourtId;
                    srvMatter.MatterOpenDate = matter.OpenDate;
                    srvMatter.MatterNextReviewDate = matter.NextReviewDate;
                    srvMatter.MatterCostReviewDate = matter.CostReviewDate;
                    srvMatter.MatterClosedDate = matter.ClosedDate;
                    srvMatter.MatterDestructDate = matter.DestructDate;
                    srvMatter.MatterFileNo = matter.FileNo;
                    srvMatter.MatterCompleted = matter.CompletedDate;

                    // Save Matter Additional Info
                    srvMatter.MatterQuote = matter.Quote;
                    srvMatter.MatterDisbsLimit = matter.DisbsLimit;
                    srvMatter.MatterTimeLimit = matter.TimeLimit;
                    srvMatter.MatterWIPLimit = matter.WIPLimit;
                    srvMatter.MatterOverallLimit = matter.OverallLimit;
                    srvMatter.MatterStatus = matter.Status;
                    srvMatter.MatterIndicators = matter.Indicators;
                    srvMatter.MatterBankReference = matter.BankReference;
                    srvMatter.MatCashCollID = matter.CashCollectionId;
                    srvMatter.MatTotalLockup = matter.TotalLockup;
                    srvMatter.OurReference = matter.OurReference;
                    srvMatter.PreviousReference = matter.PreviousReference;
                    srvMatter.SourceID = matter.BusinessSourceId;
                    // TODO: Save Campaign Id
                    // = campaignId = matter.SourceCampaignId;
                    srvMatter.UserId = matter.PersonDealingId;
                    srvMatter.MatterSalutationEnvelope = matter.SalutationEnvelope;
                    srvMatter.MatterSalutationLetter = matter.SalutationLetter;
                    srvMatter.MatterLetterHead = matter.LetterHead;

                    //Save Matter Public Funding
                    srvMatter.MatterLegalAided = matter.IsPublicFunding;
                    srvMatter.MatterFranchised = matter.Franchised;
                    srvMatter.MatterLondonRate = matter.isLondonRate;
                    srvMatter.UFNDate = matter.UFNDate;
                    srvMatter.UFNNumber = matter.UFN;
                    srvMatter.MatterPFCertificateNo = matter.PFCertificateNo;
                    srvMatter.MatterPFCertificateLimits = matter.PFCertificateNoLimits;
                    srvMatter.MatterTypeId = matter.MatterTypeId;

                    srvMatter.ClientId = matter.ClientId;

                    string errorMessage;

                    returnValue.Success = srvMatter.Save(out errorMessage);
                    returnValue.Message = errorMessage;
                }
                finally
                {
                    // Remove the logged on user's ApplicationSettings from the
                    // list of concurrent sessions
                    Host.UnloadLoggedOnUser();
                }
            }
            catch (System.Data.SqlClient.SqlException)
            {
                returnValue.Success = false;
                returnValue.Message = Functions.SQLErrorMessage;
            }
            catch (Exception ex)
            {
                returnValue.Success = false;
                returnValue.Message = ex.Message;
            }

            return returnValue;
        }
        /// <summary>
        /// Loads office cheque request details for printing
        /// </summary>
        /// <param name="logonId">Logon id obtained when logging on to the logon service</param>
        /// <param name="clientChequeRequestId">Office cheque request id toget cheque request details.</param>
        /// <returns>Returns office cheque request details by office cheque request id.</returns>
        public ChequeRequestReturnValue LoadOfficeChequeRequestDetailsForPrinting(Guid logonId, int officeChequeRequestId)
        {
            ChequeRequestReturnValue returnValue = new ChequeRequestReturnValue();

            try
            {
                // Get the logged on user from the current logons and add their
                // ApplicationSettings the list of concurrent sessions.
                Host.LoadLoggedOnUser(logonId);

                try
                {
                    Functions.RestrictRekoopIntegrationUser(UserInformation.Instance.DbUid);
                    switch (UserInformation.Instance.UserType)
                    {
                        case DataConstants.UserType.Staff:
                            // Can do everything
                            break;
                        case DataConstants.UserType.Client:
                        case DataConstants.UserType.ThirdParty:
                            throw new Exception("Access denied");
                        default:
                            throw new Exception("Access denied");
                    }

                    ChequeRequest officeChequeRequest = new ChequeRequest();

                    DsOfficeChequeRequests dsOfficeChequeRequestsDetails = SrvOfficeChequeRequestLookup.GetOfficeChequeRequestsByOfficeChequeRequestId(officeChequeRequestId);

                    // Gets printable properties from the dataset
                    foreach (DataRow drOfficeChequeRequest in dsOfficeChequeRequestsDetails.OfficeChequeRequests.Rows)
                    {
                        Guid projectId = (Guid)drOfficeChequeRequest["projectId"];

                        // Gets the matter description by project id
                        SrvMatter srvMatter = new SrvMatter();
                        srvMatter.Load(projectId);

                        Guid clientId = srvMatter.ClientId;
                        string matterDescription = srvMatter.MatterDescription;
                        string matterReference = srvMatter.MatterReference;
                        bool isMember = srvMatter.IsMember;

                        // Inserts "-" in between matter reference
                        matterReference = matterReference.Insert(6, "-");

                        string personName = srvMatter.ClientName;

                        // Gets the partner name by partner member id for project id
                        Guid partnerId = srvMatter.MatterPartnerMemberId;
                        string partnerName = SrvEarnerCommon.GetFeeEarnerNameByFeeEarnerId(partnerId);

                        // Gets the fee earner name by fee earner id
                        Guid feeEarnerId = srvMatter.FeeEarnerMemberId;
                        string feeEarnerName = SrvEarnerCommon.GetFeeEarnerNameByFeeEarnerId(feeEarnerId);

                        // This member id is from application settings for accounts.
                        Guid userMemberId = (Guid)drOfficeChequeRequest["memberId"];
                        DateTime officeChequeRequestDate = (DateTime)drOfficeChequeRequest["OfficeChequeRequestsDate"];

                        // Gets user name by member id
                        DsSystemUsers dsSystemUsers = SrvUserLookup.GetUser(userMemberId.ToString());
                        string userName = string.Empty;
                        if (dsSystemUsers.uvw_SystemUsers.Count > 0)
                        {
                            userName = dsSystemUsers.uvw_SystemUsers[0].name;
                        }
                        else
                        {
                            DsPersonDealing dsPersonDealing = SrvMatterLookup.GetPersonDealingLookup();
                            for (int index = 0; index < dsPersonDealing.uvw_PersonDealingLookup.Count; index++)
                            {
                                if (dsPersonDealing.uvw_PersonDealingLookup[index].MemberID == userMemberId)
                                {
                                    userName = dsPersonDealing.uvw_PersonDealingLookup[index].name;
                                }
                            }
                        }

                        int bankId = (int)drOfficeChequeRequest["bankId"];
                        string officeChequeRequestDescription = (string)drOfficeChequeRequest["OfficeChequeRequestsDesc"];
                        string officeChequeRequestPayee = (string)drOfficeChequeRequest["OfficeChequeRequestsPayee"];
                        int officeChequeRequestVATRateId = (int)drOfficeChequeRequest["VatRateId"];

                        // Gets VAT rate by VAT rate id
                        DsVatRates dsVATRates = SrvVATRateLookup.GetVatRates(officeChequeRequestVATRateId);
                        string officeChequeRequestVATRateReference = (string)dsVATRates.VatRates[0].VatRateRef;

                        // Gets the bank name by bank id
                        string officeChequeRequestBankName = this.GetBankByBankId(bankId);
                        decimal officeChequeRequestAmount = (decimal)drOfficeChequeRequest["OfficeChequeRequestsAmount"];
                        decimal officeChequeRequestVATAmount = (decimal)drOfficeChequeRequest["OfficeChequeRequestsVATAmount"];
                        bool isOfficeChequeAuthorised = (bool)drOfficeChequeRequest["OfficeChequeRequestsIsAuthorised"];
                        bool isOfficeChequeAnticipated = (bool)drOfficeChequeRequest["OfficeChequeRequestIsAnticipated"];

                        officeChequeRequest.UserName = userName;
                        officeChequeRequest.PersonName = personName;
                        officeChequeRequest.PartnerName = partnerName;
                        officeChequeRequest.FeeEarnerReference = feeEarnerName;
                        officeChequeRequest.BankName = officeChequeRequestBankName;
                        officeChequeRequest.ChequeRequestDate = officeChequeRequestDate;
                        officeChequeRequest.ChequeRequestPayee = officeChequeRequestPayee;
                        officeChequeRequest.VATAmount = officeChequeRequestVATAmount;
                        officeChequeRequest.VATRate = officeChequeRequestVATRateReference;
                        officeChequeRequest.ChequeRequestAmount = officeChequeRequestAmount;
                        officeChequeRequest.MatterDescription = matterDescription;
                        officeChequeRequest.MatterReference = matterReference;
                        officeChequeRequest.IsChequeRequestAuthorised = isOfficeChequeAuthorised;
                        officeChequeRequest.IsChequeRequestAnticipated = isOfficeChequeAnticipated;
                        officeChequeRequest.ChequeRequestDescription = officeChequeRequestDescription;

                        // Gets addressline1,addressline2,addressline3 for client.
                        if (isMember)
                        {
                            DsMemAddress dsMemAddress = SrvAddressLookup.GetMemberAddresses(clientId);
                            if (dsMemAddress.Address.Rows.Count > 0)
                            {
                                officeChequeRequest.AddressLine1 = dsMemAddress.Address[0].AddressLine1.ToString().Trim();
                                officeChequeRequest.AddressLine2 = dsMemAddress.Address[0].AddressLine2.ToString().Trim();
                                officeChequeRequest.AddressLine3 = dsMemAddress.Address[0].AddressLine3.ToString().Trim();
                                officeChequeRequest.AddressTown = dsMemAddress.Address[0].AddressTown.ToString().Trim();
                                officeChequeRequest.AddressCounty = dsMemAddress.Address[0].AddressCounty.ToString().Trim();
                                officeChequeRequest.AddressPostcode = dsMemAddress.Address[0].AddressPostCode.ToString().Trim();
                            }
                        }
                        else // Look for organisation address if the client is not member.
                        {
                            DsOrgAddress dsOrgAddress = SrvAddressLookup.GetOrganisationAddresses(clientId);
                            if (dsOrgAddress.Address.Rows.Count > 0)
                            {
                                officeChequeRequest.AddressLine1 = dsOrgAddress.Address[0].AddressLine1.ToString().Trim();
                                officeChequeRequest.AddressLine2 = dsOrgAddress.Address[0].AddressLine2.ToString().Trim();
                                officeChequeRequest.AddressLine3 = dsOrgAddress.Address[0].AddressLine3.ToString().Trim();
                                officeChequeRequest.AddressTown = dsOrgAddress.Address[0].AddressTown.ToString().Trim();
                                officeChequeRequest.AddressCounty = dsOrgAddress.Address[0].AddressCounty.ToString().Trim();
                                officeChequeRequest.AddressPostcode = dsOrgAddress.Address[0].AddressPostCode.ToString().Trim();
                            }

                        }
                    }

                    returnValue.ChequeRequest = officeChequeRequest;
                }
                finally
                {
                    // Remove the logged on user's ApplicationSettings from the
                    // list of concurrent sessions
                    Host.UnloadLoggedOnUser();
                }
            }
            catch (System.Data.SqlClient.SqlException)
            {
                returnValue.Success = false;
                returnValue.Message = Functions.SQLErrorMessage;
            }
            catch (Exception ex)
            {
                returnValue.Success = false;
                returnValue.Message = ex.Message;
            }

            return returnValue;
        }
        /// <summary>
        /// Loads cheque request details
        /// </summary>
        /// <param name="logonId">Logon id obtained when logging on to the logon service</param>
        /// <param name="clientChequeRequestId">Client cheque request id toget cheque request details.</param>
        /// <returns>Returns client cheque request details by client cheque request id.</returns>
        public ChequeRequestReturnValue LoadClientChequeRequestDetailsForPrinting(Guid logonId, int clientChequeRequestId)
        {
            ChequeRequestReturnValue returnValue = new ChequeRequestReturnValue();

            try
            {
                // Get the logged on user from the current logons and add their
                // ApplicationSettings the list of concurrent sessions.
                Host.LoadLoggedOnUser(logonId);

                try
                {
                    Functions.RestrictRekoopIntegrationUser(UserInformation.Instance.DbUid);
                    switch (UserInformation.Instance.UserType)
                    {
                        case DataConstants.UserType.Staff:
                            // Can do everything
                            break;
                        case DataConstants.UserType.Client:
                        case DataConstants.UserType.ThirdParty:
                            throw new Exception("Access denied");
                        default:
                            throw new Exception("Access denied");
                    }

                    ChequeRequest clientChequeRequest = new ChequeRequest();

                    DsClientChequeRequestsDetails dsClientChequeRequestsDetails = SrvClientChequeRequestLookup.GetClientChequeRequestDetails(clientChequeRequestId);

                    // Gets printable properties from the dataset
                    foreach (DataRow drClientChequeRequest in dsClientChequeRequestsDetails.uvw_ClientChequeRequestsDetails.Rows)
                    {
                        Guid projectId = (Guid)drClientChequeRequest["projectId"];

                        // Gets the matter details by project id
                        SrvMatter srvMatter = new SrvMatter();
                        srvMatter.Load(projectId);

                        Guid clientId = srvMatter.ClientId;
                        bool isMember = srvMatter.IsMember;
                        string matterDescription = srvMatter.MatterDescription;
                        string matterReference = srvMatter.MatterReference;

                        // Inserts "-" in between matter reference
                        matterReference = matterReference.Insert(6, "-");

                        string personName = srvMatter.ClientName;

                        // Gets the partner name by partner member id for project id
                        Guid partnerId = srvMatter.MatterPartnerMemberId;
                        string partnerName = SrvEarnerCommon.GetFeeEarnerNameByFeeEarnerId(partnerId);

                        // Client name should be related to project id
                        DateTime clientChequeRequestDate = (DateTime)drClientChequeRequest["ClientChequeRequestsDate"];
                        string feeEarnerReference = (string)drClientChequeRequest["feeRef"];
                        string userName = (string)drClientChequeRequest["userName"];
                        string clientChequeRequestDescription = (string)drClientChequeRequest["ClientChequeRequestsDesc"];
                        string clientChequeRequestPayee = (string)drClientChequeRequest["ClientChequeRequestsPayee"];
                        int bankId = (int)drClientChequeRequest["bankId"];
                        string clientChequeRequestBankName = this.GetBankByBankId(bankId);
                        decimal clientChequeRequestAmount = (decimal)drClientChequeRequest["ClientChequeRequestsAmount"];
                        bool isClientChequeAuthorised = (bool)drClientChequeRequest["ClientChequeRequestsIsAuthorised"];
                        string matBranchRef = (string)drClientChequeRequest["matBranchRef"];
                        string ClearanceTypeDesc = (string)drClientChequeRequest["ClearanceTypeDesc"];

                        clientChequeRequest.PersonName = personName;
                        clientChequeRequest.IsChequeRequestAuthorised = isClientChequeAuthorised;
                        clientChequeRequest.ChequeRequestDate = clientChequeRequestDate;
                        clientChequeRequest.ChequeRequestDescription = clientChequeRequestDescription;
                        clientChequeRequest.ChequeRequestPayee = clientChequeRequestPayee;
                        clientChequeRequest.BankName = clientChequeRequestBankName;
                        clientChequeRequest.ChequeRequestAmount = clientChequeRequestAmount;
                        clientChequeRequest.UserName = userName;
                        clientChequeRequest.FeeEarnerReference = feeEarnerReference;
                        clientChequeRequest.MatterReference = matterReference;
                        clientChequeRequest.MatterDescription = matterDescription;
                        clientChequeRequest.PartnerName = partnerName;
                        clientChequeRequest.ClearanceTypeDesc = ClearanceTypeDesc;
                        clientChequeRequest.MatBranchRef = matBranchRef;

                        // Gets address details for client
                        if (isMember)
                        {
                            DsMemAddress dsMemAddress = SrvAddressLookup.GetMemberAddresses(clientId);
                            if (dsMemAddress.Address.Rows.Count > 0)
                            {
                                clientChequeRequest.AddressLine1 = dsMemAddress.Address[0].AddressLine1.ToString().Trim();
                                clientChequeRequest.AddressLine2 = dsMemAddress.Address[0].AddressLine2.ToString().Trim();
                                clientChequeRequest.AddressLine3 = dsMemAddress.Address[0].AddressLine3.ToString().Trim();
                                clientChequeRequest.AddressTown = dsMemAddress.Address[0].AddressTown.ToString().Trim();
                                clientChequeRequest.AddressCounty = dsMemAddress.Address[0].AddressCounty.ToString().Trim();
                                clientChequeRequest.AddressPostcode = dsMemAddress.Address[0].AddressPostCode.ToString().Trim();
                            }
                        }
                        else // if a client is not member client look address detail fo organisation client.
                        {
                            DsOrgAddress dsOrgAddress = SrvAddressLookup.GetOrganisationAddresses(clientId);
                            if (dsOrgAddress.Address.Rows.Count > 0)
                            {
                                clientChequeRequest.AddressLine1 = dsOrgAddress.Address[0].AddressLine1.ToString().Trim();
                                clientChequeRequest.AddressLine2 = dsOrgAddress.Address[0].AddressLine2.ToString().Trim();
                                clientChequeRequest.AddressLine3 = dsOrgAddress.Address[0].AddressLine3.ToString().Trim();
                                clientChequeRequest.AddressTown = dsOrgAddress.Address[0].AddressTown.ToString().Trim();
                                clientChequeRequest.AddressCounty = dsOrgAddress.Address[0].AddressCounty.ToString().Trim();
                                clientChequeRequest.AddressPostcode = dsOrgAddress.Address[0].AddressPostCode.ToString().Trim();
                            }
                        }
                    }

                    returnValue.ChequeRequest = clientChequeRequest;
                }
                finally
                {
                    // Remove the logged on user's ApplicationSettings from the
                    // list of concurrent sessions
                    Host.UnloadLoggedOnUser();
                }
            }
            catch (System.Data.SqlClient.SqlException)
            {
                returnValue.Success = false;
                returnValue.Message = Functions.SQLErrorMessage;
            }
            catch (Exception ex)
            {
                returnValue.Success = false;
                returnValue.Message = ex.Message;
            }

            return returnValue;
        }
        /// <summary>
        /// Gets the client type id and the default branch.
        /// </summary>
        /// <param name="logonId">The logon id.</param>
        /// <param name="clientId">The client id.</param>
        /// <param name="isMember">if set to <c>true</c> [is member].</param>
        /// <returns></returns>
        public ClientReturnValue GetClientDefaults(Guid logonId, Guid clientId)
        {
            ClientReturnValue returnValue = new ClientReturnValue();

            try
            {
                // Get the logged on user from the current logons and add their
                // ApplicationSettings the list of concurrent sessions.
                Host.LoadLoggedOnUser(logonId);

                try
                {
                    Functions.RestrictRekoopIntegrationUser(UserInformation.Instance.DbUid);
                    switch (UserInformation.Instance.UserType)
                    {
                        case DataConstants.UserType.Staff:
                            // Can do everything
                            break;
                        case DataConstants.UserType.Client:
                        case DataConstants.UserType.ThirdParty:
                            throw new Exception("Access denied");
                        default:
                            throw new Exception("Access denied");
                    }

                    string errorMessage = string.Empty;
                    string warningMessage = string.Empty;
                    SrvMatter srvMatter = new SrvMatter();
                    srvMatter.ClientId = clientId;
                    bool success = srvMatter.ValidateClientId(out errorMessage, out warningMessage);
                    if (success)
                    {
                        returnValue.Client = new Client();
                        returnValue.Client.TypeId = srvMatter.ClientTypeId;
                        returnValue.Client.Branch = srvMatter.DefaultBranchId;
                    }
                    else
                    {
                        returnValue.Success = false;
                        returnValue.Message = errorMessage;
                    }
                }
                finally
                {
                    // Remove the logged on user's ApplicationSettings from the
                    // list of concurrent sessions
                    Host.UnloadLoggedOnUser();
                }
            }
            catch (System.Data.SqlClient.SqlException)
            {
                returnValue.Success = false;
                returnValue.Message = Functions.SQLErrorMessage;
            }
            catch (Exception ex)
            {
                returnValue.Success = false;
                returnValue.Message = ex.Message;
            }

            return returnValue;
        }