/// <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>
        /// 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;
        }