Exemplo n.º 1
0
        private static void DumpDatabases()
        {
            using (var financeDb = new FinanceDb())
            {
                foreach (Account account in financeDb.Accounts)
                {
                    WriteLine("Account {0} ({1}) - Balance ${2}",
                              account.Number,
                              account.Name,
                              account.Balance);
                    WriteLine(
                        "------------------------------------------------------------------------------------------");
                    foreach (Operation operation in account.Operations)
                    {
                        WriteLine("{0} - {1} - ${2}", operation.Time, operation.Description, operation.Amount);
                    }
                    WriteLine();
                }
            }

            using (var orderDb = new OrderDb())
            {
                WriteLine("Orders");
                WriteLine("----------------------------------------------------------------------------");
                foreach (Order order in orderDb.Orders)
                {
                    WriteLine("{0} - {1}", order.Description, order.TotalAmount);
                }
            }
        }
        protected override IEnumerable <CategoryAmount> calculate(FinanceDb db, IQueryable <Transaction> transactions)
        {
            var selectedMonthData = calculateSelectedMonthData(db);
            var averageData       = base.calculate(db, transactions);

            var categories = averageData
                             .Select(x => x.Category)
                             .Intersect(selectedMonthData.Select(x => x.Category))
                             .ToList();

            var data = categories.Select(category => new CategoryComparison(
                                             category,
                                             averageData.Single(x => x.Category == category).Amount,
                                             selectedMonthData.Single(x => x.Category == category).Amount)
                                         ).ToList();

            HigherSpendingData = data
                                 .Where(x => x.Difference > 0)
                                 .OrderByDescending(x => x.Difference)
                                 .ToList();
            LowerSpendingData = data
                                .Where(x => x.Difference < 0)
                                .OrderBy(x => x.Difference)
                                .ToList();

            return(averageData);
        }
Exemplo n.º 3
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddCors(options =>
            {
                options.AddPolicy("AllowAll", p =>
                {
                    p.AllowAnyOrigin()
                    .AllowAnyHeader()
                    .AllowAnyMethod()
                    .AllowCredentials();
                });
            });

            // Initialize all classes instances for the BL and DAL
            FinanceDb finance     = new FinanceDb();
            MailDb    mails       = new MailDb();
            var       mailHandler = new MailHandler("*****@*****.**", "Michael", "Miki2995*", mails.UserMailRecipients);
            var       rContainer  = new RouteContainer(mailHandler, "Finance Route was used and this is the result");
            var       routes      = new Dictionary <string, IRoute>()
            {
                { "finance", new FinanceRoute(rContainer, finance.MoneyList) }
            };

            services.AddSingleton <IRequestRouter>(new SimpleRequestRouter(routes));

            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
        }
Exemplo n.º 4
0
 public static IEnumerable <object> Build(FinanceDb db)
 {
     return(db.TopLevelCategories
            .ToList()
            .Select(c => new CategoryNode(c))
            .ToList());
 }
Exemplo n.º 5
0
 public void Process(IEnumerable <Transaction> transactions, FinanceDb db)
 {
     foreach (var trx in transactions)
     {
         Process(trx);
         db.Transactions.Add(trx);
     }
     db.SaveChanges();
 }
Exemplo n.º 6
0
 public StatementParser(Stream stream, FinanceDb db)
 {
     this.reader     = new StreamReader(stream);
     this.types      = new Generator <TransactionType, string>(db.TransactionTypes, n => new TransactionType(n), (type, name) => type.Name.Equals(name, StringComparison.CurrentCultureIgnoreCase));
     this.cards      = new Generator <Card, string>(db.Cards, n => new Card(n.ToUpper()), (card, number) => card.Number == number.ToUpper());
     this.categories = new Generator <Category, string>(db.Categories, n => new Category(n, null), (category, name) => category.Name.Equals(name, StringComparison.CurrentCultureIgnoreCase));
     errors          = new List <string>();
     transactions    = parse().ToList();
 }
        private Dictionary <Category, decimal> getAverageSpendingByCategory(FinanceDb db, DateTime startDate, DateTime endDate)
        {
            var report = new AverageMonthlySpendingByCategory();

            report.DateFrom = startDate;
            report.DateTo   = endDate;
            report.Run(db);
            return(report.Data.ToDictionary(ca => ca.Category, ca => ca.Amount));
        }
Exemplo n.º 8
0
        private IQueryable <Transaction> retrieve(FinanceDb db)
        {
            var search = new Search
            {
                DateFrom = Month.ToDateTime(MonthAndYear.PositionInMonth.Start),
                DateTo   = Month.ToDateTime(MonthAndYear.PositionInMonth.End)
            };

            return(db.Transactions.Where(search.IsMatch()));
        }
Exemplo n.º 9
0
        public UploadTransactionsResult(FinanceDb db)
            : this()
        {
            var mostRecentTransaction = db.Transactions.OrderByDescending(x => x.Date).FirstOrDefault();

            if (mostRecentTransaction != null)
            {
                MostRecentTransactionDate = mostRecentTransaction.Date;
            }
        }
Exemplo n.º 10
0
 protected IEnumerable <Category> getCategoriesOfInterest(FinanceDb db)
 {
     if (TopLevelCategory == null)
     {
         return(db.TopLevelCategories);
     }
     else
     {
         return(TopLevelCategory.DirectChildren);
     }
 }
Exemplo n.º 11
0
        private IQueryable <Transaction> retrieve(FinanceDb db)
        {
            var search = new Search()
            {
                Category = Category,
                DateFrom = DateFrom,
                DateTo   = DateTo
            };

            return(db.Transactions.Where(search.IsMatch()));
        }
        protected override IEnumerable <CategoryMonthAmount> calculate(FinanceDb db, IQueryable <Transaction> transactions)
        {
            IEnumerable <CategoryMonthAmount> data;

            data = transactions
                   .GroupBy(x => new { x.Category, x.Date.Year, x.Date.Month })
                   .ToList()
                   .Select(group => new CategoryMonthAmount(group.Key.Category, new MonthAndYear(group.Key.Year, group.Key.Month), group.Sum(x => x.Amount)))
                   .ToList();
            data = new PerMonthCategoryRoller(Category).RollUpEachMonthByCategoriesOfInterest(db, data);
            return(data.ToList());
        }
Exemplo n.º 13
0
        public void Initialize(FinanceDb db)
        {
            Search.Initialize(db);
            var transactions = db.Transactions
                               .Include(x => x.Category)
                               .Include(x => x.Card.Person);

            AllTransactions = Sort.Sort(Search.Filter(transactions));
            Total           = AllTransactions.Count();
            Transactions    = AllTransactions.Take(NextLimit).AsEnumerable();
            CurrentLimit    = NextLimit;
            NextLimit       = DefaultLimit;
        }
        protected override IEnumerable <CategoryAmount> calculate(FinanceDb db, IQueryable <Transaction> transactions)
        {
            var monthAndCategoryTotals = transactions
                                         .GroupBy(x => new { x.Category, x.Date.Year, x.Date.Month })
                                         .ToList()
                                         .Select(group => new CategoryMonthAmount(group.Key.Category, new MonthAndYear(group.Key.Year, group.Key.Month), group.Sum(x => x.Amount)))
                                         .ToList();
            var data = new AverageOverMonthsCategoryRoller(Category, StartMonth, EndMonth).RollUpByCategoriesOfInterest(db, monthAndCategoryTotals);

            data = filter(data);
            return(data
                   .OrderBy(x => x.Category.Name)
                   .ToList());
        }
Exemplo n.º 15
0
        public override void Run(FinanceDb db)
        {
            Budget = db.GetOrCreateDefaultBudget();
            var transactions = retrieve(db).GroupBy(x => x.Category)
                               .ToList();

            Data = Budget.Items.Select(item => new BudgetReportItem(
                                           item,
                                           GetTransactionsForCategories(transactions, item.Categories)
                                           )).ToList();

            MiscellaneousTransactions = GetTransactionsForCategories(transactions, Budget.SpareCategories(db));
            MiscellaneousSpending     = GetTotalForCategories(MiscellaneousTransactions);
        }
        protected override IEnumerable <CategoryAmount> calculate(FinanceDb db, IQueryable <Transaction> transactions)
        {
            var totals = transactions
                         .GroupBy(trx => trx.Category)
                         .ToList()
                         .Select(group => new CategoryAmount(group.Key, group.Sum(g => g.Amount)))
                         .ToList();
            var data = new TotalAmountCategoryRoller(Category).RollUpByCategoriesOfInterest(db, totals);

            data = filter(data);
            return(data
                   .OrderByDescending(x => x.Amount)
                   .ToList());
        }
Exemplo n.º 17
0
 public IEnumerable <CategoryAmount> RollUpByCategoriesOfInterest(FinanceDb db, IEnumerable <TSource> totals)
 {
     initialize(totals);
     foreach (var parent in getCategoriesOfInterest(db).ToList())
     {
         var children       = new HashSet <Category>(parent.Children);
         var categoryTotals = totals.Where(t => children.Contains(t.Category));
         yield return(new CategoryAmount(parent, rollUpCategory(categoryTotals)));
     }
     if (TopLevelCategory != null)
     {
         var categoryTotals = totals.Where(t => t.Category == TopLevelCategory);
         yield return(new CategoryAmount(TopLevelCategory, rollUpCategory(categoryTotals)));
     }
 }
        private IEnumerable <CategoryAmount> calculateSelectedMonthData(FinanceDb db)
        {
            var search = new Search()
            {
                DateFrom = SelectedMonth.ToDateTime(MonthAndYear.PositionInMonth.Start),
                DateTo   = SelectedMonth.ToDateTime(MonthAndYear.PositionInMonth.End),
            };
            var selectedMonthTotals = db.Transactions
                                      .Where(search.IsMatch())
                                      .GroupBy(x => x.Category)
                                      .ToList()
                                      .Select(group => new CategoryMonthAmount(group.Key, SelectedMonth, group.Sum(x => x.Amount)));
            var selectedMonthData = new AverageOverMonthsCategoryRoller(Category, SelectedMonth, SelectedMonth)
                                    .RollUpByCategoriesOfInterest(db, selectedMonthTotals)
                                    .ToList();

            return(filter(selectedMonthData));
        }
Exemplo n.º 19
0
        private static void AddOperation(string accountNumber, decimal amount, string description)
        {
            using (var financeDb = new FinanceDb())
            {
                var account = financeDb.Accounts.Single(a => a.Number == accountNumber);
                account.Balance += amount;

                if (account.Balance < account.MinimalBalance)
                {
                    throw new InvalidOperationException("Account balance insufficient.");
                }

                financeDb.Operations.Add(new Operation
                {
                    Account     = account,
                    Amount      = amount,
                    Description = description,
                    Time        = DateTime.Now
                });
                financeDb.SaveChanges();
            }
        }
Exemplo n.º 20
0
        private static void CreateDatabases()
        {
            string dataDirectory = Path.Combine(Path.GetDirectoryName(Environment.GetCommandLineArgs()[0]));

            AppDomain.CurrentDomain.SetData("DataDirectory", dataDirectory);

            using (var financeDb = new FinanceDb())
            {
                if (!financeDb.Database.CreateIfNotExists())
                {
                    financeDb.Operations.RemoveRange(financeDb.Operations);
                    financeDb.Accounts.RemoveRange(financeDb.Accounts);
                }

                financeDb.Accounts.Add(new Account
                {
                    Name   = "IT Department",
                    Number = ItDepartment
                });

                financeDb.Accounts.Add(new Account
                {
                    Name    = "HR Department",
                    Number  = HrDepartment,
                    Balance = 500000
                });
                financeDb.SaveChanges();
            }

            using (var orderDb = new OrderDb())
            {
                if (!orderDb.Database.CreateIfNotExists())
                {
                    orderDb.Orders.RemoveRange(orderDb.Orders);
                    orderDb.SaveChanges();
                }
            }
        }
Exemplo n.º 21
0
 protected abstract IEnumerable <TDataItem> calculate(FinanceDb db, IQueryable <Transaction> transactions);
Exemplo n.º 22
0
 public FinancesControllerBase()
 {
     Db = new FinanceDb();
 }
Exemplo n.º 23
0
 public void Initialize(FinanceDb db)
 {
     Categories = db.Categories;
     People     = db.People;
 }
Exemplo n.º 24
0
 public AutoCategoriser(FinanceDb db)
 {
     rules = db.ActiveRules.ToDictionary(r => r, r => new List <Transaction>());
 }
Exemplo n.º 25
0
        public override void Run(FinanceDb db)
        {
            var transactions = retrieve(db);

            data = calculate(db, transactions);
        }
 public IEnumerable <CategoryMonthAmount> RollUpEachMonthByCategoriesOfInterest(FinanceDb db, IEnumerable <CategoryMonthAmount> totals)
 {
     foreach (var month in totals.Select(x => x.Month).Distinct())
     {
         var subset = totals.Where(x => x.Month == month);
         foreach (CategoryAmount categoryAmount in RollUpByCategoriesOfInterest(db, subset))
         {
             yield return(new CategoryMonthAmount(categoryAmount.Category, month, categoryAmount.Amount));
         }
     }
 }
Exemplo n.º 27
0
 public RulesViewModel(FinanceDb db)
 {
     Rules = db.ActiveRules
             .OrderBy(x => x.Category.Id)
             .ToList();
 }
Exemplo n.º 28
0
 public abstract void Run(FinanceDb db);
Exemplo n.º 29
0
 public BudgetViewModel(Budget budget, FinanceDb db)
 {
     Budget          = budget;
     SpareCategories = budget.SpareCategories(db);
 }