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