/// <summary>
        /// Adds the draft bill.
        /// </summary>
        /// <param name="logonId">The logon id.</param>
        /// <param name="bill">The bill.</param>
        /// <returns></returns>
        public DraftBillReturnValue AddDraftBill(Guid logonId, DraftBill bill)
        {
            DraftBillReturnValue returnValue = new DraftBillReturnValue();

            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(145))
                        throw new Exception("You do not have sufficient permissions to carry out this request");

                    string errorMessage = string.Empty;
                    string warningMessage = string.Empty;

                    SrvDraftBill srvDraftBill = new SrvDraftBill();
                    srvDraftBill.ProjectId = bill.ProjectId;
                    bool success = srvDraftBill.ValidateProjectId(out errorMessage, out warningMessage);
                    if (success)
                    {
                        srvDraftBill.SetDefaultDisbursementLedger();
                    }
                    else
                    {
                        throw new Exception(errorMessage);
                    }
                    srvDraftBill.Date = bill.DraftBillDate.Date;
                    srvDraftBill.ProfitCosts = bill.ProfitCosts;
                    srvDraftBill.VATAmount = bill.VATAmount;
                    srvDraftBill.VATRateId = bill.VATRateId;
                    srvDraftBill.BilledTimeUpto = bill.BilledTimeUpto;
                    srvDraftBill.PostingDescriptionDetails = bill.PostingDescription;
                    srvDraftBill.UnbPaidNonVatNotes = bill.UnbilledPaidNonVATableNotes;
                    srvDraftBill.UnbPaidVatNotes = bill.UnbilledPaidVATableNotes;
                    srvDraftBill.AntiNonVatNotes = bill.AntiNonVATableNotes;
                    srvDraftBill.AntiVatNotes = bill.AntiVATableNotes;
                    this.SetDraftBillTransValues(bill.UnBilledPaidNonVatableList, srvDraftBill.UnBilledPaidNonVatableDisbsList);
                    this.SetDraftBillTransValues(bill.UnBilledPaidVatableList, srvDraftBill.UnBilledPaidVatableDisbsList);
                    this.SetDraftBillAnticipatedTransValues(bill.AnticipatedDisbursementNonVatableList, srvDraftBill.AntiDisbsNonVatableList);
                    this.SetDraftBillAnticipatedTransValues(bill.AnticipatedDisbursementVatableList, srvDraftBill.AntiDisbsVatableList);
                    this.SetTimeValues(bill.TimeTransactions, srvDraftBill.TimeTransaction);

                    var primaryClientAssociation = new BrAssociations().GetAssociationForMatterByProjectIdAndRoleId(srvDraftBill.ProjectId, 1);

                    if (primaryClientAssociation.ProjectAssociations.Count > 0)
                    {
                        var billPayer = new DraftBillPayer()
                        {
                            MemberId = primaryClientAssociation.ProjectAssociations[0].MemberID,
                            OrgId = primaryClientAssociation.ProjectAssociations[0].OrgID,
                            Amount = srvDraftBill.GetBillTotal(),
                            BillPayerStatusId = 1,
                            CashCollActDue = DataConstants.BlankDate,
                            Narrative = string.Empty,
                            AddressId = primaryClientAssociation.ProjectAssociations[0].MemberID == DataConstants.DummyGuid
                                ? SrvAddressLookup.GetOrganisationAddressForBilling(primaryClientAssociation.ProjectAssociations[0].OrgID)
                                : SrvAddressLookup.GetMemberAddressForBilling(primaryClientAssociation.ProjectAssociations[0].MemberID)
                        };

                        srvDraftBill.BillPayers.Add(billPayer);
                    }

                    returnValue.Success = srvDraftBill.Save(out errorMessage);
                    bill.DraftBillId = srvDraftBill.Id;
                    returnValue.DraftBill = bill;
                    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>
        /// Returns draft bill details by draft bill id
        /// </summary>
        /// <param name="logonId">Logon id obtained when logging on to the logon service</param>
        /// <param name="draftBillId">Draft bill id to populate</param>
        /// <returns></returns>
        public DraftBillReturnValue GetDraftBill(Guid logonId, int draftBillId)
        {
            DraftBillReturnValue returnValue = new DraftBillReturnValue();

            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");
                    }

                    SrvDraftBill srvDraftBill = new SrvDraftBill();

                    srvDraftBill.Id = draftBillId;
                    srvDraftBill.Load(draftBillId);

                    DraftBill draftBill = new DraftBill();
                    draftBill.DraftBillId = srvDraftBill.Id;
                    draftBill.DraftBillDate = srvDraftBill.Date;
                    draftBill.ProfitCosts = srvDraftBill.ProfitCosts;
                    draftBill.VATAmount = srvDraftBill.VATAmount;
                    draftBill.VATRateId = srvDraftBill.VATRateId;
                    draftBill.BilledTimeUpto = srvDraftBill.BilledTimeUpto;
                    draftBill.IsProcessed = srvDraftBill.IsProcessed;
                    draftBill.IsSubmitted = srvDraftBill.IsSubmitted;
                    draftBill.ProjectId = srvDraftBill.ProjectId;
                    draftBill.PostingDescription = srvDraftBill.PostingDescriptionDetails;
                    draftBill.UnbilledPaidNonVATableNotes = srvDraftBill.UnbPaidNonVatNotes;
                    draftBill.UnbilledPaidVATableNotes = srvDraftBill.UnbPaidVatNotes;
                    draftBill.AntiNonVATableNotes = srvDraftBill.AntiNonVatNotes;
                    draftBill.AntiVATableNotes = srvDraftBill.AntiVatNotes;

                    returnValue.DraftBill = draftBill;
                }
                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;
        }