public void EndofMonthManager_AccountIsPositive()
        {
            AccountViewModel account1 = new AccountViewModel
            {
                Id = 1,
                CurrentBalance = -100,
            };

            var paymentRepoSetup = new Mock<IPaymentRepository>();
            paymentRepoSetup.Setup(x => x.GetList(It.IsAny<Expression<Func<PaymentViewModel, bool>>>())).Returns(new List<PaymentViewModel>
            {
                new PaymentViewModel {Id = 10, TargetAccountId=1, Amount=100,Date= DateTime.Now, Type = PaymentType.Income},
                new PaymentViewModel {Id = 15, TargetAccountId=1, Amount=100, Date= DateTime.Now, Type = PaymentType.Income}
            });

            var accounts = new List<AccountViewModel>
            {
                new AccountViewModel {Id=2, CurrentBalance=100},
                account1
            };

            EndOfMonthManager testManager = new EndOfMonthManager(paymentRepoSetup.Object);
            testManager.CheckEndOfMonthBalanceForAccounts(accounts);

            account1.IsOverdrawn.ShouldBeFalse();
        }
        public void SaveCommand_Does_Not_Allow_Duplicate_Names()
        {
            var accountList = new List<AccountViewModel>();

            var settingsManagerMock = new Mock<ISettingsManager>();

            var accountRepositorySetup = new Mock<IAccountRepository>();
            accountRepositorySetup.Setup(c => c.GetList(It.IsAny<Expression<Func<AccountViewModel, bool>>>()))
                .Returns(accountList);
            accountRepositorySetup.Setup(c => c.Save(It.IsAny<AccountViewModel>()))
                .Callback((AccountViewModel acc) => { accountList.Add(acc); });

            var account = new AccountViewModel
            {
                Id = 1,
                Name = "Test AccountViewModel"
            };
            var newAccount = new AccountViewModel
            {
                Name = "Test AccountViewModel"
            };
            accountList.Add(account);
            
            var viewmodel = new ModifyAccountViewModel(accountRepositorySetup.Object, 
                new Mock<IDialogService>().Object, 
                settingsManagerMock.Object,
                new Mock<IBackupManager>().Object)
            {
                IsEdit = false,
                SelectedAccount = newAccount
            };

            viewmodel.SaveCommand.Execute();
            Assert.AreEqual(1, accountList.Count());
        }
        public double GetEndOfMonthBalanceForAccount(AccountViewModel accountViewModel)
        {
            accountId = accountViewModel.Id;
            var balance = accountViewModel.CurrentBalance;
            var unclearedPayments = LoadUnclearedPayments();

            foreach (var payment in unclearedPayments)
            {
                switch (payment.Type)
                {
                    case PaymentType.Expense:
                        balance -= payment.Amount;
                        break;

                    case PaymentType.Income:
                        balance += payment.Amount;
                        break;

                    case PaymentType.Transfer:
                        balance = HandleTransferAmount(payment, balance);
                        break;
                }
            }
            return balance;
        }
 /// <summary>
 ///     Deletes the passed AccountViewModel and removes it from cache
 /// </summary>
 public bool Delete(AccountViewModel accountToDelete)
 {
     using (var db = dbManager.GetConnection())
     {
         var itemToDelete = db.Table<Account>().Single(x => x.Id == accountToDelete.Id);
         return db.Delete(itemToDelete) == 1;
     }
 }
        public void DeleteAssociatedPaymentsFromDatabase_Account_DeleteRightPayments()
        {
            var resultList = new List<int>();

            var account1 = new AccountViewModel
            {
                Id = 3,
                Name = "just an AccountViewModel",
                CurrentBalance = 500
            };
            var account2 = new AccountViewModel
            {
                Id = 4,
                Name = "just an AccountViewModel",
                CurrentBalance = 900
            };

            var payment = new PaymentViewModel
            {
                Id = 1,
                ChargedAccount = account1,
                ChargedAccountId = account1.Id
            };


            var paymentRepositorySetup = new Mock<IPaymentRepository>();
            paymentRepositorySetup.SetupAllProperties();
            paymentRepositorySetup.Setup(x => x.Delete(It.IsAny<PaymentViewModel>()))
                .Callback((PaymentViewModel trans) => resultList.Add(trans.Id));
            paymentRepositorySetup.Setup(x => x.GetList(It.IsAny<Expression<Func<PaymentViewModel, bool>>>()))
                .Returns(new List<PaymentViewModel>
                {
                    payment
                });

            var repo = paymentRepositorySetup.Object;

            new PaymentManager(paymentRepositorySetup.Object,
                new Mock<IAccountRepository>().Object,
                new Mock<IRecurringPaymentRepository>().Object,
                new Mock<IDialogService>().Object)
                .DeleteAssociatedPaymentsFromDatabase(account1);

            Assert.AreEqual(1, resultList.Count);
            Assert.AreEqual(payment.Id, resultList.First());
        }
        /// <summary>
        ///     Save a new AccountViewModel or update an existing one.
        /// </summary>
        public bool Save(AccountViewModel accountVmToSave)
        {
            using (var db = dbManager.GetConnection())
            {
                if (string.IsNullOrWhiteSpace(accountVmToSave.Name))
                {
                    accountVmToSave.Name = Strings.NoNamePlaceholderLabel;
                }

                var account = Mapper.Map<Account>(accountVmToSave);

                if (account.Id == 0)
                {
                    var rows = db.Insert(account);
                    accountVmToSave.Id = db.Table<Account>().OrderByDescending(x => x.Id).First().Id;
                    return rows == 1;
                }
                return db.Update(account) == 1;
            }
        }
        /// <summary>
        ///     Deletes all the payments and the recurring payments associated with teh account.
        /// </summary>
        /// <param name="accountViewModel">The associated Account.</param>
        public void DeleteAssociatedPaymentsFromDatabase(AccountViewModel accountViewModel)
        {
            var paymentToDelete = paymentRepository
                .GetList(x => (x.ChargedAccountId == accountViewModel.Id) || (x.TargetAccountId == accountViewModel.Id))
                .OrderByDescending(x => x.Date)
                .ToList();

            foreach (var payment in paymentToDelete)
            {
                if (payment.IsRecurring)
                {
                    foreach (var recTrans in recurringPaymentRepository.GetList(x => x.Id == payment.RecurringPaymentId)
                    )
                    {
                        recurringPaymentRepository.Delete(recTrans);
                    }
                }

                paymentRepository.Delete(payment);
            }
        }
        public void Converter_TransferSameAccount_Plus()
        {
            ClearAll();
            Setup();
            var account = new AccountViewModel
            {
                Id = 4,
                CurrentBalance = 400
            };

            var mock = new Mock<IAccountRepository>();

            Mvx.RegisterSingleton(mock.Object);

            new PaymentAmountConverter()
                .Convert(new PaymentViewModel
                {
                    Amount = 80,
                    Type = PaymentType.Transfer,
                    ChargedAccount = new AccountViewModel(),
                    CurrentAccountId = account.Id
                }, null, new AccountViewModel(), null)
                .ShouldBe("+ " + 80.ToString("C"));
        }
        /// <summary>
        ///     Removes the PaymentViewModel Amount from the selected AccountViewModel
        /// </summary>
        /// <param name="payment">PaymentViewModel to remove.</param>
        /// <param name="accountViewModel">AccountViewModel to remove the amount from.</param>
        public bool RemovePaymentAmount(PaymentViewModel payment, AccountViewModel accountViewModel)
        {
            if (!payment.IsCleared)
            {
                return false;
            }

            PrehandleRemoveIfTransfer(payment);

            Func<double, double> amountFunc = x =>
                payment.Type == PaymentType.Income
                    ? -x
                    : x;

            HandlePaymentAmount(payment, amountFunc, GetChargedAccountFunc(accountViewModel));
            return true;
        }
        public void Save_UpdateTimeStamp()
        {
            var account = new AccountViewModel {Id = 0, Name = "AccountViewModel"};

            var accountRepositorySetup = new Mock<IAccountRepository>();
            accountRepositorySetup.Setup(x => x.Save(account)).Returns(true);
            accountRepositorySetup.Setup(x => x.GetList(null)).Returns(() => new List<AccountViewModel>());

            var localDateSetting = DateTime.MinValue;
            var settingsManagerMock = new Mock<ISettingsManager>();
            settingsManagerMock.SetupSet(x => x.LastDatabaseUpdate = It.IsAny<DateTime>())
                .Callback((DateTime x) => localDateSetting = x);

            var viewmodel = new ModifyAccountViewModel(accountRepositorySetup.Object,
                new Mock<IDialogService>().Object, 
                settingsManagerMock.Object,
                new Mock<IBackupManager>().Object)
            {
                IsEdit = false,
                SelectedAccount = account
            };

            viewmodel.SaveCommand.Execute();

            localDateSetting.ShouldBeGreaterThan(DateTime.Now.AddSeconds(-1));
            localDateSetting.ShouldBeLessThan(DateTime.Now.AddSeconds(1));
        }
        private void GoToPaymentOverView(AccountViewModel accountViewModel)
        {
            if (accountViewModel == null)
            {
                return;
            }

            ShowViewModel<PaymentListViewModel>(new {id = accountViewModel.Id});
        }
        private async void Delete(AccountViewModel accountToDelete)
        {
            if (accountToDelete == null)
            {
                return;
            }

            if (await dialogService.ShowConfirmMessage(Strings.DeleteTitle, Strings.DeleteAccountConfirmationMessage))
            {
                paymentManager.DeleteAssociatedPaymentsFromDatabase(accountToDelete);

                if (accountRepository.Delete(accountToDelete))
                {
                    if (AllAccounts.Contains(accountToDelete))
                    {
                        AllAccounts.Remove(accountToDelete);
                    }
                    settingsManager.LastDatabaseUpdate = DateTime.Now;
                }
                else
                {
                    await dialogService
                        .ShowConfirmMessage(Strings.ErrorTitleDelete, Strings.ErrorMessageDelete);
                }
            }
            BalanceViewModel.UpdateBalanceCommand.Execute();
        }
        public void SelectedItemChangedCommand_UpdatesCorrectely()
        {
            var settingsManagerMock = new Mock<ISettingsManager>();
            settingsManagerMock.SetupAllProperties();

            var accountRepoMock = new Mock<IAccountRepository>();
            accountRepoMock.Setup(x => x.GetList(null)).Returns(new List<AccountViewModel>());

            var paymentManager = new PaymentManager(new Mock<IPaymentRepository>().Object,
                accountRepoMock.Object,
                new Mock<IRecurringPaymentRepository>().Object,
                new Mock<IDialogService>().Object);

            var viewmodel = new ModifyPaymentViewModel(new Mock<IPaymentRepository>().Object,
                accountRepoMock.Object,
                new Mock<IDialogService>().Object,
                paymentManager,
                settingsManagerMock.Object,
                new Mock<IMvxMessenger>().Object,
                new Mock<IBackupManager>().Object);

            viewmodel.Init(PaymentType.Income);

            AccountViewModel test1 = new AccountViewModel();//target AccountViewModel
            AccountViewModel test2 = new AccountViewModel();//charge AccountViewModel
            viewmodel.TargetAccounts.Add(test1);
            viewmodel.ChargedAccounts.Add(test1);
            viewmodel.TargetAccounts.Add(test2);
            viewmodel.ChargedAccounts.Add(test2);

            viewmodel.SelectedPayment.TargetAccount = test1;
            viewmodel.SelectedPayment.ChargedAccount = test2;

            viewmodel.SelectedItemChangedCommand.Execute();

            viewmodel.ChargedAccounts.Contains(viewmodel.SelectedPayment.ChargedAccount).ShouldBeTrue();
            viewmodel.TargetAccounts.Contains(viewmodel.SelectedPayment.TargetAccount).ShouldBeTrue();
            viewmodel.ChargedAccounts.Contains(viewmodel.SelectedPayment.TargetAccount).ShouldBeFalse();
            viewmodel.TargetAccounts.Contains(viewmodel.SelectedPayment.ChargedAccount).ShouldBeFalse();
        }
 private void EditAccount(AccountViewModel accountViewModel)
 {
     ShowViewModel<ModifyAccountViewModel>(new { accountId = accountViewModel.Id});
 }
        public void GetPaymentFromRecurring_MonthlyPayment_CorrectMappedPayment()
        {
            var account = new AccountViewModel { Id = 2 };
            var dayOfMonth = 26;

            var recurringPayment = new RecurringPaymentViewModel
            {
                Id = 4,
                Recurrence = PaymentRecurrence.Monthly,
                StartDate = new DateTime(2015, 08, dayOfMonth),
                ChargedAccountId = 2,
                ChargedAccount = account,
                Amount = 105
            };

            var result = RecurringPaymentHelper.GetPaymentFromRecurring(recurringPayment);

            result.ChargedAccount.ShouldBe(account);
            result.ChargedAccountId.ShouldBe(account.Id);
            result.Amount.ShouldBe(105);
            result.Date.ShouldBe(new DateTime(DateTime.Today.Year, DateTime.Today.Month, dayOfMonth));
        }
Esempio n. 16
0
 private Func<PaymentViewModel, AccountViewModel> GetChargedAccountFunc(AccountViewModel accountViewModel)
 {
     Func<PaymentViewModel, AccountViewModel> accountFunc =
         trans => accountRepository.GetList(x => x.Id == accountViewModel.Id).FirstOrDefault();
     return accountFunc;
 }
        public void GetPaymentFromRecurring_CorrectMappedPayment(PaymentRecurrence recurrence)
        {
            var account = new AccountViewModel {Id = 2};

            var recurringPayment = new RecurringPaymentViewModel
            {
                Id = 4,
                Recurrence = recurrence,
                StartDate = new DateTime(2015, 08, DateTime.Today.Day),
                ChargedAccountId = 2,
                ChargedAccount = account,
                Amount = 105
            };

            var result = RecurringPaymentHelper.GetPaymentFromRecurring(recurringPayment);

            result.ChargedAccount.ShouldBe(account);
            result.ChargedAccountId.ShouldBe(account.Id);
            result.Amount.ShouldBe(105);
            result.Date.ShouldBe(DateTime.Today);
        }
        public void CheckIfRepeatable_UnclearedPayment_ReturnFalse(PaymentRecurrence recurrence, int amountOfDaysUntilRepeat)
        {
            var account = new AccountViewModel {Id = 2};

            var recurringPayment = new RecurringPaymentViewModel
            {
                Id = 4,
                Recurrence = PaymentRecurrence.Weekly,
                StartDate = new DateTime(2015, 08, 25),
                ChargedAccountId = 2,
                ChargedAccount = account,
                Amount = 105
            };

            RecurringPaymentHelper.CheckIfRepeatable(recurringPayment,
                new PaymentViewModel {Date = DateTime.Today.AddDays(amountOfDaysUntilRepeat)}).ShouldBeFalse();
        }
        public void CheckIfRepeatable_ValidatedRecurrence(PaymentRecurrence recurrence, int amountOfDaysPassed)
        {
            var account = new AccountViewModel {Id = 2};

            var recurringPayment = new RecurringPaymentViewModel
            {
                Id = 4,
                Recurrence = recurrence,
                StartDate = new DateTime(2015, 08, 25),
                ChargedAccountId = 2,
                ChargedAccount = account,
                Amount = 105
            };

            RecurringPaymentHelper.CheckIfRepeatable(recurringPayment,
                new PaymentViewModel {Date = DateTime.Today.AddDays(-amountOfDaysPassed), IsCleared = true})
                .ShouldBeTrue();
        }