public void Write(CashflowStatement cashflowStatement)
        {
            Console.WriteLine($"Cashflow Statement from {cashflowStatement.StartAt} to {cashflowStatement.EndAt}");
            Console.WriteLine();
            foreach (CashflowAccount account in cashflowStatement.Accounts.OrderBy(a => a.Name))
            {
                if (account.Inflow == 0 && account.Outflow == 0)
                {
                    continue;
                }

                Console.WriteLine($"{account.Name}");
                Console.WriteLine();
                Console.WriteLine("Period           Inflow     Outflow    Cashflow");
                Console.WriteLine("---------------- ---------- ---------- ----------");
                foreach (CashflowAccountPeriod period in account.Periods.OrderBy(i => i.Range.Start))
                {
                    Console.WriteLine("{0,-16} {1,10} {2,10} {3,10}",
                                      period.Range.Start.ToShortDateString(), period.Inflow, period.Outflow, period.Cashflow);
                }

                Console.WriteLine("---------------- ---------- ---------- ==========");
                Console.WriteLine("Total            {0,10} {1,10} {2,10}",
                                  account.Inflow, account.Outflow, account.Cashflow);
                Console.WriteLine("---------------- ---------- ---------- ==========");
                Console.WriteLine();
                Console.WriteLine();
            }

            // TODO: statement totals
        }
Пример #2
0
        public static void Configure(CommandLineApplication command)
        {
            command.Description = "Generate a cashflow statement from the database for a given period";

            CommandOption databaseConnectionOption = command.Option(
                "-d|--database",
                "The database connection to connect to",
                CommandOptionType.SingleValue);

            CommandOption passwordOption = command.Option(
                "-p|--password",
                "The password to connect with",
                CommandOptionType.SingleValue);

            CommandOption startAtOption = command.Option(
                "-s|--startAt",
                "The start of the cashflow period",
                CommandOptionType.SingleValue);

            CommandOption endAtOption = command.Option(
                "-e|--endAt",
                "The end of the cashflow period",
                CommandOptionType.SingleValue);

            command.OnExecute(() =>
            {
                string databaseConnectionName = databaseConnectionOption.Value();
                string password = passwordOption.HasValue() ? passwordOption.Value() : String.Empty;
                ServiceCollection serviceCollection = ServiceCollectionSetup.SetupCoreServices(
                    databaseConnectionName,
                    password
                    );

                // Financier.Core services
                serviceCollection.AddSingleton <IAccountRelationshipService, AccountRelationshipService>();
                serviceCollection.AddSingleton <IAccountService, AccountService>();
                serviceCollection.AddSingleton <ICurrencyService, CurrencyService>();
                serviceCollection.AddSingleton <ICashflowService, CashflowService>();
                serviceCollection.AddSingleton <IEnvironmentService, EnvironmentService>();
                serviceCollection.AddSingleton <ITransactionService, TransactionService>();

                // Application services
                serviceCollection.AddSingleton <ICashflowStatementWriterService, CashflowStatementConsoleWriterService>();

                IServiceProvider serviceProvider = serviceCollection.BuildServiceProvider();

                ICashflowService cashflowService = serviceProvider.GetRequiredService <ICashflowService>();
                DateTime startAt = DateTime.Parse(startAtOption.Value());
                DateTime endAt   = DateTime.Parse(endAtOption.Value());
                CashflowStatement cashflowStatement = cashflowService.Generate(CashflowPeriod.Fortnightly, startAt, endAt);

                ICashflowStatementWriterService cashflowStatementWriterService =
                    serviceProvider.GetRequiredService <ICashflowStatementWriterService>();
                cashflowStatementWriterService.Write(cashflowStatement);

                return(0);
            });
        }
Пример #3
0
        public void TestGenerateCashflowStatementMultiplePeriodsMultipleAccounts()
        {
            ILoggerFactory loggerFactory = new LoggerFactory();

            using (var sqliteMemoryWrapper = new SqliteMemoryWrapper())
            {
                var currencyFactory   = new CurrencyFactory();
                var usdCurrencyEntity = currencyFactory.Create(CurrencyPrefab.Usd, true);
                currencyFactory.Add(sqliteMemoryWrapper.DbContext, usdCurrencyEntity);

                var accountFactory = new AccountFactory();
                Entities.Account incomeAccountEntity =
                    accountFactory.Create(AccountPrefab.Income, usdCurrencyEntity);
                Entities.Account checkingAccountEntity =
                    accountFactory.Create(AccountPrefab.Checking, usdCurrencyEntity);
                Entities.Account creditCardAccountEntity =
                    accountFactory.Create(AccountPrefab.CreditCard, usdCurrencyEntity);
                Entities.Account rentExpenseAccountEntity =
                    accountFactory.Create(AccountPrefab.RentExpense, usdCurrencyEntity);
                Entities.Account rentPrepaymentAccountEntity =
                    accountFactory.Create(AccountPrefab.RentPrepayment, usdCurrencyEntity);
                Entities.Account groceriesExpenseAccountEntity =
                    accountFactory.Create(AccountPrefab.GroceriesExpense, usdCurrencyEntity);
                Entities.Account groceriesPrepaymentAccountEntity =
                    accountFactory.Create(AccountPrefab.GroceriesPrepayment, usdCurrencyEntity);

                accountFactory.Add(sqliteMemoryWrapper.DbContext, incomeAccountEntity);
                accountFactory.Add(sqliteMemoryWrapper.DbContext, checkingAccountEntity);
                accountFactory.Add(sqliteMemoryWrapper.DbContext, creditCardAccountEntity);
                accountFactory.Add(sqliteMemoryWrapper.DbContext, rentExpenseAccountEntity);
                accountFactory.Add(sqliteMemoryWrapper.DbContext, rentPrepaymentAccountEntity);
                accountFactory.Add(sqliteMemoryWrapper.DbContext, groceriesExpenseAccountEntity);
                accountFactory.Add(sqliteMemoryWrapper.DbContext, groceriesPrepaymentAccountEntity);

                var transactionEntities = new Entities.Transaction[]
                {
                    new Entities.Transaction
                    {
                        CreditAccount = incomeAccountEntity,
                        DebitAccount  = checkingAccountEntity,
                        Amount        = 100m,
                        At            = new DateTime(2018, 1, 1, 9, 0, 0)
                    },
                    new Entities.Transaction
                    {
                        CreditAccount = checkingAccountEntity,
                        DebitAccount  = rentPrepaymentAccountEntity,
                        Amount        = 50m,
                        At            = new DateTime(2018, 1, 2, 9, 0, 0)
                    },
                    new Entities.Transaction
                    {
                        CreditAccount = checkingAccountEntity,
                        DebitAccount  = groceriesPrepaymentAccountEntity,
                        Amount        = 20m,
                        At            = new DateTime(2018, 1, 3, 9, 0, 0)
                    },
                    new Entities.Transaction
                    {
                        CreditAccount = creditCardAccountEntity,
                        DebitAccount  = groceriesExpenseAccountEntity,
                        Amount        = 5m,
                        At            = new DateTime(2018, 1, 4, 9, 0, 0)
                    },
                    new Entities.Transaction
                    {
                        CreditAccount = creditCardAccountEntity,
                        DebitAccount  = groceriesExpenseAccountEntity,
                        Amount        = 8m,
                        At            = new DateTime(2018, 1, 5, 9, 0, 0)
                    },
                    new Entities.Transaction
                    {
                        CreditAccount = creditCardAccountEntity,
                        DebitAccount  = groceriesExpenseAccountEntity,
                        Amount        = 3m,
                        At            = new DateTime(2018, 1, 6, 9, 0, 0)
                    },
                    new Entities.Transaction
                    {
                        CreditAccount = groceriesPrepaymentAccountEntity,
                        DebitAccount  = creditCardAccountEntity,
                        Amount        = 5m,
                        At            = new DateTime(2018, 1, 7, 9, 0, 0)
                    },
                    new Entities.Transaction
                    {
                        CreditAccount = groceriesPrepaymentAccountEntity,
                        DebitAccount  = creditCardAccountEntity,
                        Amount        = 8m,
                        At            = new DateTime(2018, 1, 8, 9, 0, 0)
                    },
                    new Entities.Transaction
                    {
                        CreditAccount = groceriesPrepaymentAccountEntity,
                        DebitAccount  = creditCardAccountEntity,
                        Amount        = 3m,
                        At            = new DateTime(2018, 1, 9, 9, 0, 0)
                    },
                    new Entities.Transaction
                    {
                        CreditAccount = incomeAccountEntity,
                        DebitAccount  = checkingAccountEntity,
                        Amount        = 80m,
                        At            = new DateTime(2018, 1, 14, 9, 0, 0)
                    },
                    new Entities.Transaction
                    {
                        CreditAccount = checkingAccountEntity,
                        DebitAccount  = rentPrepaymentAccountEntity,
                        Amount        = 50m,
                        At            = new DateTime(2018, 1, 15, 9, 0, 0)
                    },
                    new Entities.Transaction
                    {
                        CreditAccount = checkingAccountEntity,
                        DebitAccount  = groceriesPrepaymentAccountEntity,
                        Amount        = 20m,
                        At            = new DateTime(2018, 1, 16, 9, 0, 0)
                    },
                    new Entities.Transaction
                    {
                        CreditAccount = creditCardAccountEntity,
                        DebitAccount  = groceriesExpenseAccountEntity,
                        Amount        = 3m,
                        At            = new DateTime(2018, 1, 17, 9, 0, 0)
                    },
                    new Entities.Transaction
                    {
                        CreditAccount = creditCardAccountEntity,
                        DebitAccount  = groceriesExpenseAccountEntity,
                        Amount        = 7m,
                        At            = new DateTime(2018, 1, 18, 9, 0, 0)
                    },
                    new Entities.Transaction
                    {
                        CreditAccount = creditCardAccountEntity,
                        DebitAccount  = groceriesExpenseAccountEntity,
                        Amount        = 12m,
                        At            = new DateTime(2018, 1, 19, 9, 0, 0)
                    },
                    new Entities.Transaction
                    {
                        CreditAccount = groceriesPrepaymentAccountEntity,
                        DebitAccount  = creditCardAccountEntity,
                        Amount        = 3m,
                        At            = new DateTime(2018, 1, 20, 9, 0, 0)
                    },
                    new Entities.Transaction
                    {
                        CreditAccount = groceriesPrepaymentAccountEntity,
                        DebitAccount  = creditCardAccountEntity,
                        Amount        = 7m,
                        At            = new DateTime(2018, 1, 21, 9, 0, 0)
                    },
                    new Entities.Transaction
                    {
                        CreditAccount = groceriesPrepaymentAccountEntity,
                        DebitAccount  = creditCardAccountEntity,
                        Amount        = 12m,
                        At            = new DateTime(2018, 1, 22, 9, 0, 0)
                    },
                    new Entities.Transaction
                    {
                        CreditAccount = rentPrepaymentAccountEntity,
                        DebitAccount  = rentExpenseAccountEntity,
                        Amount        = 100m,
                        At            = new DateTime(2018, 1, 27, 9, 0, 0)
                    },
                    new Entities.Transaction // this should be excluded from the report
                    {
                        CreditAccount = creditCardAccountEntity,
                        DebitAccount  = groceriesExpenseAccountEntity,
                        Amount        = 7m,
                        At            = new DateTime(2018, 1, 31, 9, 0, 0)
                    }
                };

                sqliteMemoryWrapper.DbContext.Transactions.AddRange(transactionEntities);
                sqliteMemoryWrapper.DbContext.SaveChanges();

                var accountRelationshipEntities = new Entities.AccountRelationship[]
                {
                    new Entities.AccountRelationship
                    {
                        SourceAccount      = checkingAccountEntity,
                        DestinationAccount = rentPrepaymentAccountEntity,
                        Type = AccountRelationshipType.PhysicalToLogical
                    },
                    new Entities.AccountRelationship
                    {
                        SourceAccount      = checkingAccountEntity,
                        DestinationAccount = groceriesPrepaymentAccountEntity,
                        Type = AccountRelationshipType.PhysicalToLogical
                    },
                    new Entities.AccountRelationship
                    {
                        SourceAccount      = rentPrepaymentAccountEntity,
                        DestinationAccount = rentExpenseAccountEntity,
                        Type = AccountRelationshipType.PrepaymentToExpense
                    },
                    new Entities.AccountRelationship
                    {
                        SourceAccount      = groceriesPrepaymentAccountEntity,
                        DestinationAccount = groceriesExpenseAccountEntity,
                        Type = AccountRelationshipType.PrepaymentToExpense
                    }
                };

                sqliteMemoryWrapper.DbContext.AccountRelationships.AddRange(accountRelationshipEntities);
                sqliteMemoryWrapper.DbContext.SaveChanges();

                var accountRelationshipService = new AccountRelationshipService(
                    loggerFactory,
                    sqliteMemoryWrapper.DbContext
                    );
                var accountService = new AccountService(
                    loggerFactory,
                    sqliteMemoryWrapper.DbContext
                    );
                var currencyService = new CurrencyService(
                    loggerFactory,
                    sqliteMemoryWrapper.DbContext
                    );
                var transactionService = new TransactionService(
                    loggerFactory,
                    sqliteMemoryWrapper.DbContext
                    );
                var cashflowService = new CashflowService(
                    loggerFactory,
                    accountRelationshipService,
                    accountService,
                    currencyService,
                    transactionService
                    );

                CashflowStatement cashflowStatement =
                    cashflowService.Generate(CashflowPeriod.Fortnightly, new DateTime(2018, 1, 1), new DateTime(2018, 1, 28));
                List <CashflowAccount> cashflowAccounts =
                    cashflowStatement
                    .Accounts
                    .OrderBy(i => i.Name)
                    .ToList();

                Assert.AreEqual(2, cashflowAccounts.Count);
                Assert.AreEqual("Groceries", cashflowAccounts[0].Name);
                Assert.AreEqual(2, cashflowAccounts[0].Periods.Count());
                Assert.AreEqual(20m, cashflowAccounts[0].Periods.ElementAt(0).Inflow);
                Assert.AreEqual(16m, cashflowAccounts[0].Periods.ElementAt(0).Outflow);
                Assert.AreEqual(4m, cashflowAccounts[0].Periods.ElementAt(0).Cashflow);
                Assert.AreEqual(20m, cashflowAccounts[0].Periods.ElementAt(1).Inflow);
                Assert.AreEqual(22m, cashflowAccounts[0].Periods.ElementAt(1).Outflow);
                Assert.AreEqual(-2m, cashflowAccounts[0].Periods.ElementAt(1).Cashflow);
                Assert.AreEqual(40m, cashflowAccounts[0].Inflow);
                Assert.AreEqual(38m, cashflowAccounts[0].Outflow);
                Assert.AreEqual(2m, cashflowAccounts[0].Cashflow);
                Assert.AreEqual("Rent", cashflowAccounts[1].Name);
                Assert.AreEqual(2, cashflowAccounts[1].Periods.Count());
                Assert.AreEqual(50m, cashflowAccounts[1].Periods.ElementAt(0).Inflow);
                Assert.AreEqual(0m, cashflowAccounts[1].Periods.ElementAt(0).Outflow);
                Assert.AreEqual(50m, cashflowAccounts[1].Periods.ElementAt(0).Cashflow);
                Assert.AreEqual(50m, cashflowAccounts[1].Periods.ElementAt(1).Inflow);
                Assert.AreEqual(100m, cashflowAccounts[1].Periods.ElementAt(1).Outflow);
                Assert.AreEqual(-50m, cashflowAccounts[1].Periods.ElementAt(1).Cashflow);
                Assert.AreEqual(100m, cashflowAccounts[1].Inflow);
                Assert.AreEqual(100m, cashflowAccounts[1].Outflow);
                Assert.AreEqual(0m, cashflowAccounts[1].Cashflow);
            }
        }