Пример #1
0
        public EditAccountView(MainController owner, Account account)
        {
            if (owner == null)
            {
                throw new ArgumentNullException("owner");
            }

            if (account == null)
            {
                throw new ArgumentNullException("account");
            }

            this.owner = owner;
            this.originalAccount = account;

            InitializeComponent();

            this.securityComboBox.Items.Add(new SecurityOption(null));
            this.securityComboBox.Items.AddRange(this.owner.Book.Securities.Select(s => new SecurityOption(s)).ToArray());

            this.nameTextBox.Text = this.originalAccount.Name;
            this.balanceAccountRadio.Checked = this.originalAccount.AccountType == AccountType.Balance;
            this.groupingAccountRadio.Checked = this.originalAccount.AccountType == AccountType.Grouping;
            this.securityComboBox.SelectedItem = this.securityComboBox.Items.Cast<SecurityOption>().Where(so => so.Security == this.originalAccount.Security).Single();
            this.fractionTextBox.Text = this.originalAccount.Security == null ? string.Empty : this.originalAccount.Security.FormatValue(this.originalAccount.Security.FractionTraded / this.originalAccount.SmallestFraction.Value);
        }
Пример #2
0
        public Account(Guid accountId, AccountType accountType, Security security, Account parentAccount, string name, int? smallestFraction)
        {
            if (accountId == Guid.Empty)
            {
                throw new ArgumentOutOfRangeException("accountId");
            }

            if (!Enum.GetValues(typeof(AccountType)).Cast<AccountType>().Contains(accountType))
            {
                throw new ArgumentOutOfRangeException("accountType");
            }

            if (security == null && smallestFraction.HasValue)
            {
                throw new ArgumentNullException("security");
            }

            if (string.IsNullOrEmpty(name))
            {
                throw new ArgumentNullException("name");
            }

            if (security != null && !smallestFraction.HasValue)
            {
                throw new ArgumentNullException("smallestFraction");
            }

            if (smallestFraction <= 0)
            {
                throw new ArgumentOutOfRangeException("smallestFraction");
            }

            if (security != null)
            {
                if (security.FractionTraded % smallestFraction != 0)
                {
                    throw new InvalidOperationException("An account's smallest fraction must represent a whole number multiple of the units used by its security");
                }
            }

            var parent = parentAccount;
            while (parent != null)
            {
                if (parent.AccountId == accountId)
                {
                    throw new InvalidOperationException("An account may not share an its Account Id with any of its ancestors.");
                }

                parent = parent.ParentAccount;
            }

            this.accountId = accountId;
            this.accountType = accountType;
            this.security = security;
            this.parentAccount = parentAccount;
            this.name = name;
            this.smallestFraction = smallestFraction;
        }
Пример #3
0
        private static void AddBookAccounts(TreeNode node, Book book, Account parent)
        {
            if (node.Checked)
            {
                var account = new Account(Guid.NewGuid(), (AccountType)node.Tag, null, parent, node.Text, null);
                book.AddAccount(account);

                foreach (TreeNode child in node.Nodes)
                {
                    AddBookAccounts(child, book, account);
                }
            }
        }
Пример #4
0
        /// <summary>
        /// Initializes a new instance of the <see cref="SharpBooks.AccountData" /> class, copying data from a <see cref="SharpBooks.Account" />.
        /// </summary>
        /// <param name="account">The <see cref="SharpBooks.Account" /> from which to copy.</param>
        public AccountData(Account account)
        {
            if (account == null)
            {
                throw new ArgumentNullException("account");
            }

            this.AccountId = account.AccountId;
            this.AccountType = account.AccountType;
            this.ParentAccountId = account.ParentAccount == null ? (Guid?)null : account.ParentAccount.AccountId;
            this.SecurityId = account.Security == null ? (Guid?)null : account.Security.SecurityId;
            this.Name = account.Name;
            this.SmallestFraction = account.SmallestFraction;
        }
Пример #5
0
        public void AddAccount(AccountData account)
        {
            lock (this)
            {
                var security = this.destinationBook.Securities.Where(s => s.SecurityId == account.SecurityId).Single();

                Account parent = null;
                if (account.ParentAccountId.HasValue)
                {
                    parent = this.destinationBook.Accounts.Where(a => a.AccountId == account.ParentAccountId).Single();
                }

                var newAccount = new Account(
                    account.AccountId,
                    account.AccountType,
                    security,
                    parent,
                    account.Name,
                    account.SmallestFraction);

                this.destinationBook.AddAccount(
                    newAccount);
            }
        }
Пример #6
0
        /// <summary>
        /// Removes an account from the <see cref="Book"/>.
        /// </summary>
        /// <param name="account">The account to remove.</param>
        public void RemoveAccount(Account account)
        {
            lock (this.lockMutex)
            {
                if (account == null)
                {
                    throw new ArgumentNullException("account");
                }

                if (!this.accounts.Contains(account))
                {
                    throw new InvalidOperationException("Could not remove the account from the book, because the account is not a member of the book.");
                }

                var childAccounts = from a in this.accounts
                                    where a.ParentAccount == account
                                    select a;

                if (childAccounts.Any())
                {
                    throw new InvalidOperationException("Could not remove the account from the book, because the account currently has children.");
                }

                var involvedTransactions = from t in this.transactions.Keys
                                           where (from s in t.Splits
                                                  where s.Account == account
                                                  select s).Any()
                                           select t;

                if (involvedTransactions.Any())
                {
                    throw new InvalidOperationException("Could not remove the account from the book, because the account currently has splits.");
                }

                this.accounts.Remove(account);
                if (account.ParentAccount == null)
                {
                    this.rootAccounts.Remove(account);
                }

                this.UpdateSaveTracks(st => st.RemoveAccount(account.AccountId));
                account.Book = null;

                this.balances.Remove(account);
                this.totalBalances.Remove(account);
            }

            this.AccountRemoved.SafeInvoke(this, new AccountRemovedEventArgs(account));
        }
Пример #7
0
        public CompositeBalance GetAccountTotalBalance(Account account)
        {
            lock (this.lockMutex)
            {
                if (account == null)
                {
                    throw new ArgumentNullException("account");
                }

                CompositeBalance balance;
                if (this.totalBalances.TryGetValue(account, out balance))
                {
                    return balance;
                }

                if (!this.balances.TryGetValue(account, out balance))
                {
                    throw new InvalidOperationException("The account specified is not a member of the book.");
                }

                var subAccountBalances = from a in this.accounts
                                         where a.ParentAccount == account
                                         select this.GetAccountTotalBalance(a);

                balance = new CompositeBalance(balance, subAccountBalances);
                this.totalBalances[account] = balance;
                return balance;
            }
        }
Пример #8
0
        public ICollection<Split> GetAccountSplits(Account account)
        {
            lock (this.lockMutex)
            {
                if (account == null)
                {
                    throw new ArgumentNullException("account");
                }

                if (!this.accounts.Contains(account))
                {
                    throw new InvalidOperationException("The account specified is not a member of the book.");
                }

                var splits = new List<Split>();

                foreach (var t in this.transactions.Keys)
                {
                    splits.AddRange(t.Splits.Where(s => s.Account == account));
                }

                return splits.AsReadOnly();
            }
        }
Пример #9
0
        public CompositeBalance GetAccountBalance(Account account)
        {
            lock (this.lockMutex)
            {
                if (account == null)
                {
                    throw new ArgumentNullException("account");
                }

                CompositeBalance balance;
                if (!this.balances.TryGetValue(account, out balance))
                {
                    throw new InvalidOperationException("The account specified is not a member of the book.");
                }

                return balance;
            }
        }
Пример #10
0
        /// <summary>
        /// Adds an account to the <see cref="Book"/>.
        /// </summary>
        /// <param name="account">The account to add.</param>
        public void AddAccount(Account account)
        {
            lock (this.lockMutex)
            {
                if (account == null)
                {
                    throw new ArgumentNullException("account");
                }

                if (this.accounts.Contains(account))
                {
                    throw new InvalidOperationException("Could not add the account to the book, because the account already belongs to the book.");
                }

                if (account.Security != null && !this.securities.Contains(account.Security))
                {
                    throw new InvalidOperationException("Could not add the account to the book, because the account's security has not been added.");
                }

                if (account.ParentAccount != null && !this.accounts.Contains(account.ParentAccount))
                {
                    throw new InvalidOperationException("Could not add the account to the book, because the account's parent has not been added.");
                }

                var duplicateIds = from a in this.accounts
                                   where a.AccountId == account.AccountId
                                   select a;

                if (duplicateIds.Any())
                {
                    throw new InvalidOperationException("Could not add the account to the book, because another account has already been added with the same Account Id.");
                }

                var duplicateNames = from a in this.accounts
                                     where a.Name == account.Name
                                     where a.ParentAccount == account.ParentAccount
                                     select a;

                if (duplicateNames.Any())
                {
                    throw new InvalidOperationException("Could not add the account to the book, because another account has already been added with the same Name and Parent.");
                }

                this.accounts.Add(account);
                if (account.ParentAccount == null)
                {
                    this.rootAccounts.Add(account);
                }

                this.UpdateSaveTracks(st => st.AddAccount(new AccountData(account)));
                account.Book = this;

                this.balances.Add(account, new CompositeBalance());
            }

            this.AccountAdded.SafeInvoke(this, new AccountAddedEventArgs(account));
        }
Пример #11
0
 public ICollection<Split> GetAccountSplits(Account account)
 {
     return this.book.GetAccountSplits(account);
 }
Пример #12
0
        private void okButton_Click(object sender, EventArgs e)
        {
            var security = ((SecurityOption)this.securityComboBox.SelectedItem).Security;

            this.newAccount = new Account(
                this.originalAccount.AccountId,
                this.balanceAccountRadio.Checked ? AccountType.Balance : AccountType.Grouping,
                security,
                this.originalAccount.ParentAccount,
                this.nameTextBox.Text,
                security != null ? (int)(security.FractionTraded / security.ParseValue(this.fractionTextBox.Text)) : (int?)null);
        }
Пример #13
0
        public void SetAccount(Account account, TransactionLock transactionLock)
        {
            this.Transaction.EnterCriticalSection();

            try
            {
                this.Transaction.ValidateLock(transactionLock);

                this.Account = account;
            }
            finally
            {
                this.Transaction.ExitCriticalSection();
            }
        }