Example #1
0
 public AccountDetail(Account account, double balance, double bookBalance, int noOfTransactions)
 {
     Account = account;
     Balance = balance;
     BookBalance = bookBalance;
     NoOfTransactions = noOfTransactions;
 }
Example #2
0
 public async Task<Account> CreateAccount(Book book, Account account)
 {
     if (book == null) throw new PiggyBankDataException("Book object is missing");
     if (account == null) throw new PiggyBankDataException("Account object is missing");
     account.Book = book;
     PiggyBankUtility.CheckMandatory(account);
     _dbContext.Accounts.Add(account);
     await _dbContext.SaveChangesAsync();
     return account;
 }
Example #3
0
        private async Task<AccountDetail> GetAccountDetail(Account account)
        {
            if (!account.IsValid) throw new PiggyBankDataNotFoundException("Account [" + account.Id + "] cannot be found");

            double amount = 0, bookAmount = 0;
            int noOfTransactions = 0;

            if (account.Closing != null)
            {
                amount += account.Closing.Amount ?? 0;
                bookAmount += account.Closing.BookAmount ?? 0;
            }

            var q = await GetTransactions(account)
                .Select(b => new
                    {
                        Amount = (b.DebitAccount.Id == account.Id ? 1 : -1) * (b.IsClosed ? 0 : 1) * account.DebitSign * b.Amount,
                        BookAmount = (b.DebitAccount.Id == account.Id ? 1 : -1) * (b.IsClosed ? 0 : 1) * account.DebitSign * b.BookAmount,
                        Group = 1
                    })
                .GroupBy(b => b.Group)
                .Select(g => new
                    {
                        Amount = g.Sum(x => x.Amount),
                        BookAmount = g.Sum(x => x.BookAmount),
                        NoOfTransactions = g.Count()
                    })
                .ToListAsync();
            /*
            var q = await (from b in 
                               (from b in GetTransactions(account)
                                select new
                                {
                                    Amount = (b.DebitAccount.Id == account.Id ? 1 : -1) * (b.IsClosed ? 0 : 1) * account.DebitSign * b.Amount,
                                    BookAmount = (b.DebitAccount.Id == account.Id ? 1 : -1) * (b.IsClosed ? 0 : 1) * account.DebitSign * b.BookAmount,
                                    Group = 1
                                })
                           group b by b.Group into g
                           select new
                           {
                               Amount = g.Sum(x => x.Amount),
                               BookAmount = g.Sum(x => x.BookAmount),
                               NoOfTransactions = g.Count()
                           }).ToListAsync();
            */
            if (q.Any())
            {
                var result = q.First();
                amount += result.Amount;
                bookAmount += result.BookAmount;
                noOfTransactions += result.NoOfTransactions;
            }

            return new AccountDetail(account, amount, bookAmount, noOfTransactions);
        }
Example #4
0
        public async Task<Account> UpdateAccount(Account account)
        {
            if (account == null) throw new PiggyBankDataException("Account object is missing");
            PiggyBankUtility.CheckMandatory(account);
            Account accountToUpdate = await FindAccount(account.Id);
            if (!accountToUpdate.IsValid) throw new PiggyBankDataNotFoundException("Account [" + account.Id + "] cannot be found");

            if ((await GetTransactions(account).AnyAsync()))
            {
                if (!account.IsValid) throw new PiggyBankDataException("Editing Account.IsValid is not supported for accounts with transactions");
                if (account.Type != accountToUpdate.Type) throw new PiggyBankDataException("Editing Account.Type is not supported for accounts with transactions");
                if (account.Currency != accountToUpdate.Currency) throw new PiggyBankDataException("Editing Account.Currency is not supported for accounts with transactions");
            }
            PiggyBankUtility.UpdateModel(accountToUpdate, account);
            _dbContext.SaveChanges();
            return accountToUpdate;
        }
Example #5
0
 private IQueryable<Transaction> GetTransactions(Account account)
 {
     int bookId = account.Book.Id;
     return _dbContext.Transactions
         .Where(b => b.IsValid &&
             b.Book.Id == bookId &&
             (b.DebitAccount.Id == account.Id || b.CreditAccount.Id == account.Id));
 }
Example #6
0
 private void UpdateAccountClosing(Transaction transaction, Account account, int debitSign, DateTime closingDate, DateTime timestamp)
 {
     if (account.Closing == null)
     {
         account.Closing = new AccountClosing();
         account.Closing.Account = account;
         account.Closing.Id = account.Id;
     }
     account.Closing.Amount += account.DebitSign * debitSign * transaction.Amount;
     account.Closing.BookAmount += account.DebitSign * debitSign * transaction.BookAmount;
     account.Closing.ClosingDate = closingDate;
     account.Closing.TimeStamp = timestamp;
 }