Ejemplo n.º 1
0
        public IEnumerable <AccountBalance> CalculateSimulatedAccountBalances(DateTime beginDate, DateTime endDate, Account acct)
        {
            var result                = new List <AccountBalance>();
            var postedDetails         = GetTransactionDetails(endDate, acct).ToList();
            var postedTransactionIds  = postedDetails.Select(t => t.TransactionId).Distinct().ToList();
            var postedTransactions    = Transactions.Where(t => postedTransactionIds.Contains(t.Id)).ToList();
            var simulatedTransactions = ScheduledTransactions.Where(t => t.FromAccountId == acct.Id || t.ToAccountId == acct.Id).SelectMany(t => t.Simulate(beginDate, endDate)).ToList();

            // remove simulated transactions that have hit
            for (int i = simulatedTransactions.Count - 1; i >= 0; i--)
            {
                var simulatedTransaction = simulatedTransactions[i];

                if (simulatedTransaction.ScheduledTransactionId.HasValue)
                {
                    if (
                        postedTransactions.Any(
                            t =>
                            t.Date == simulatedTransaction.Date &&
                            t.ScheduledTransactionId == simulatedTransaction.ScheduledTransactionId))
                    {
                        simulatedTransactions.RemoveAt(i);
                    }
                }
            }

            var simulatedDetails             = simulatedTransactions.SelectMany(t => t.Details.Where(td => td.AccountId == acct.Id)).ToList();
            var priorPostedTransactionIds    = Transactions.Where(t => t.Date < beginDate && postedTransactionIds.Contains(t.Id)).Select(t => t.Id);
            var priorPostedDetails           = postedDetails.Where(t => priorPostedTransactionIds.Contains(t.TransactionId));
            var priorSimulatedTransactionIds = simulatedTransactions.Where(t => t.Date < beginDate).Select(t => t.Id);
            var priorSimulatedDetails        = simulatedDetails.Where(t => priorSimulatedTransactionIds.Contains(t.TransactionId));
            var balance = priorPostedDetails.Sum(t => t.CreditAmount - t.DebitAmount) +
                          priorSimulatedDetails.Sum(t => t.CreditAmount - t.DebitAmount);


            postedTransactions.RemoveAll(t => priorPostedTransactionIds.Contains(t.Id));
            postedDetails.RemoveAll(t => priorPostedDetails.Contains(t));
            simulatedTransactions.RemoveAll(t => priorSimulatedTransactionIds.Contains(t.Id));
            simulatedDetails.RemoveAll(t => priorSimulatedDetails.Contains(t));

            while (beginDate <= endDate)
            {
                var currentPostedTransactionIds    = postedTransactions.Where(t => t.Date == beginDate).Select(t => t.Id);
                var currentSimulatedTransactionIds = simulatedTransactions.Where(t => t.Date == beginDate).Select(t => t.Id);
                var currentDetails          = postedDetails.Where(t => currentPostedTransactionIds.Contains(t.TransactionId));
                var currentSimulatedDetails = simulatedDetails.Where(t => currentSimulatedTransactionIds.Contains(t.TransactionId));

                balance += (currentDetails.Sum(t => t.CreditAmount - t.DebitAmount) +
                            currentSimulatedDetails.Sum(t => t.CreditAmount - t.DebitAmount));

                result.Add(new AccountBalance()
                {
                    Date = beginDate, Balance = balance, Transactions = simulatedTransactions.Where(t => t.Date == beginDate).Union(postedTransactions.Where(t => t.Date == beginDate)).ToList()
                });

                beginDate = beginDate.AddDays(1);
            }

            return(result);
        }
Ejemplo n.º 2
0
        public void Remove(ModelBase obj)
        {
            if (obj is Account)
            {
                Accounts.Remove(obj as Account);

                var transactionIds = Transactions.SelectMany(t => t.Details).Where(t => t.AccountId == ((Account)obj).Id).Select(t => t.TransactionId);

                foreach (var transId in transactionIds)
                {
                    Remove(Transactions.FirstOrDefault(t => t.Id == transId));
                }

                foreach (var schedTrans in ScheduledTransactions.Where(t => t.FromAccountId == ((Account)obj).Id || t.ToAccountId == ((Account)obj).Id).ToList())
                {
                    Remove(schedTrans);
                }
            }
            else if (obj is Transaction)
            {
                Transactions.Remove(obj as Transaction);

                foreach (var detail in TransactionDetails.Where(t => t.TransactionId == ((Transaction)obj).Id).ToList())
                {
                    Remove(detail);
                }
            }
            else if (obj is TransactionDetail)
            {
                TransactionDetails.Remove(obj as TransactionDetail);
            }
            else if (obj is Receipt)
            {
                Receipts.Remove(obj as Receipt);
            }
            else if (obj is ScheduledTransaction)
            {
                ScheduledTransactions.Remove(obj as ScheduledTransaction);

                foreach (var detail in ScheduledTransactionDetails.Where(t => t.ScheduledTransactionId == ((ScheduledTransaction)obj).Id).ToList())
                {
                    Remove(detail);
                }
            }
            else if (obj is ScheduledTransactionDetail)
            {
                ScheduledTransactionDetails.Remove(obj as ScheduledTransactionDetail);
            }
            else if (obj is Category)
            {
                Categories.Remove(obj as Category);
            }
            else if (obj is Product)
            {
                Products.Remove(obj as Product);
            }
        }
Ejemplo n.º 3
0
        public AccountBalance CalculateSimulatedAccountBalance(DateTime upToDate, Account acct)
        {
            var postedDetails        = GetTransactionDetails(upToDate, acct);
            var postedTransactionIds = postedDetails.Select(t => t.TransactionId).Distinct().ToList();
            var postedTransactions   = Transactions.Where(t => postedTransactionIds.Contains(t.Id)).ToList();

            var scheduledTransactions = ScheduledTransactions.Where(t => t.FromAccountId == acct.Id || t.ToAccountId == acct.Id);
            var simulatedTransactions = scheduledTransactions.SelectMany(t => t.Simulate(upToDate, upToDate)).ToList();

            // remove simulated transactions that have hit
            for (int i = simulatedTransactions.Count - 1; i >= 0; i--)
            {
                var simulatedTransaction = simulatedTransactions[i];

                if (simulatedTransaction.ScheduledTransactionId.HasValue)
                {
                    if (
                        postedTransactions.Any(
                            t =>
                            t.Date == simulatedTransaction.Date &&
                            t.ScheduledTransactionId == simulatedTransaction.ScheduledTransactionId))
                    {
                        simulatedTransactions.RemoveAt(i);
                    }
                }
            }

            var simulatedDetails = simulatedTransactions.SelectMany(t => t.Details.Where(td => td.AccountId == acct.Id));

            var balance = postedDetails.Sum(t => t.CreditAmount - t.DebitAmount) +
                          simulatedDetails.Sum(t => t.CreditAmount - t.DebitAmount);

            return(new AccountBalance()
            {
                Balance = balance, Date = upToDate, Transactions = postedTransactions.Where(t => t.Date == upToDate).Union(simulatedTransactions.Where(t => t.Date == upToDate)).ToList()
            });
        }