Exemple #1
0
        /// <summary>
        /// <see cref="IRentals.FindPayeeWithTransactions(int?, DateTime?, DateTime?, bool)"/>
        /// </summary>
        public Payee FindPayeeWithTransactions(int?id, DateTime?from = null, DateTime?to = null, bool ascending = true)
        {
            using (var context = new RentalsContext())
            {
                var payee = context.Payees
                            .Include(p => p.DefaultCategory)
                            .SingleOrDefault(a => a.Id == id);

                if (payee != null)
                {
                    context.Payees.Attach(payee);

                    if (from == null)
                    {
                        from = DateTime.MinValue;
                    }

                    if (to == null)
                    {
                        to = DateTime.MaxValue;
                    }

                    if (ascending)
                    {
                        context.Entry(payee)
                        .Collection(c => c.Transactions)
                        .Query()
                        .Include(t => t.Account)
                        .Include(t => t.Category)
                        .Where(t => t.Date >= from && t.Date <= to)
                        .OrderBy(t => t.Date)
                        .ThenBy(t => t.Id)
                        .Load();
                    }
                    else
                    {
                        context.Entry(payee)
                        .Collection(c => c.Transactions)
                        .Query()
                        .Include(t => t.Account)
                        .Include(t => t.Category)
                        .Where(t => t.Date >= from && t.Date <= to)
                        .OrderByDescending(t => t.Date)
                        .ThenByDescending(t => t.Id)
                        .Load();
                    }
                }

                return(payee);
            }
        }
Exemple #2
0
 /// <summary>
 /// <see cref="IRentals.SaveUpdatedTransaction(Transaction)"/>
 /// </summary>
 public void SaveUpdatedTransaction(Transaction transaction)
 {
     using (var context = new RentalsContext())
     {
         context.Entry(transaction).State = EntityState.Modified;
         context.SaveChanges();
     }
 }
Exemple #3
0
 /// <summary>
 /// <see cref="IRentals.SaveUpdatedPayee(Payee)"/>
 /// </summary>
 public void SaveUpdatedPayee(Payee payee)
 {
     using (var context = new RentalsContext())
     {
         context.Entry(payee).State = EntityState.Modified;
         context.SaveChanges();
     }
 }
Exemple #4
0
 /// <summary>
 /// <see cref="IRentals.SaveUpdatedCategory(Category)"/>
 /// </summary>
 public void SaveUpdatedCategory(Category category)
 {
     using (var context = new RentalsContext())
     {
         context.Entry(category).State = EntityState.Modified;
         context.SaveChanges();
     }
 }
Exemple #5
0
 /// <summary>
 /// <see cref="IRentals.SaveUpdatedAccount(Account)"/>
 /// </summary>
 public void SaveUpdatedAccount(Account account)
 {
     using (var context = new RentalsContext())
     {
         context.Entry(account).State = EntityState.Modified;
         context.SaveChanges();
     }
 }
Exemple #6
0
        /// <summary>
        /// <see cref="IRentals.FindAccountWithTransactions(int?, DateTime?, DateTime?, bool)"/>
        /// </summary>
        public Account FindAccountWithTransactions(int?id, DateTime?from = null, DateTime?to = null, bool ascending = true)
        {
            using (var context = new RentalsContext())
            {
                var account = context.Accounts
                              .SingleOrDefault(a => a.Id == id);

                if (account != null)
                {
                    context.Accounts.Attach(account);
                    var transactions = context.Entry(account)
                                       .Collection(a => a.Transactions)
                                       .Query()
                                       .Include(t => t.Payee)
                                       .Include(t => t.Category)
                                       .OrderBy(t => t.Date)
                                       .ThenBy(t => t.Id);

                    CalculateTransactionBalances(transactions, account.OpeningBalance);

                    if (from == null)
                    {
                        from = DateTime.MinValue;
                    }

                    if (to == null)
                    {
                        to = DateTime.MaxValue;
                    }

                    if (ascending)
                    {
                        account.Transactions = transactions
                                               .Where(t => t.Date >= from && t.Date <= to)
                                               .ToList();
                    }
                    else
                    {
                        account.Transactions = transactions
                                               .Where(t => t.Date >= from && t.Date <= to)
                                               .OrderByDescending(t => t.Date)
                                               .ThenByDescending(t => t.Id)
                                               .ToList();
                    }

                    account.Balance = GetAccountBalance(id);
                }

                return(account);
            }
        }