public void GetRecurringFromPayment_Endless(PaymentRecurrence recurrence, PaymentType type) { var startDate = new DateTime(2015, 03, 12); var enddate = Convert.ToDateTime("7.8.2016"); var payment = new PaymentViewModel { ChargedAccount = new AccountViewModel {Id = 3}, TargetAccount = new AccountViewModel {Id = 8}, Category = new CategoryViewModel {Id = 16}, Date = startDate, Amount = 2135, IsCleared = false, Type = type, IsRecurring = true }; var recurring = RecurringPaymentHelper.GetRecurringFromPayment(payment, true, recurrence, enddate); recurring.ChargedAccount.Id.ShouldBe(3); recurring.TargetAccount.Id.ShouldBe(8); recurring.StartDate.ShouldBe(startDate); recurring.EndDate.ShouldBe(enddate); recurring.IsEndless.ShouldBe(true); recurring.Amount.ShouldBe(payment.Amount); recurring.Category.Id.ShouldBe(payment.Category.Id); recurring.Type.ShouldBe(type); recurring.Recurrence.ShouldBe(recurrence); recurring.Note.ShouldBe(payment.Note); }
private double HandleTransferAmount(PaymentViewModel payment, double balance) { if (accountId == payment.ChargedAccountId) { balance -= payment.Amount; } else { balance += payment.Amount; } return balance; }
public void TargetAccount_SetId() { var paymentVm = new PaymentViewModel(); paymentVm.TargetAccount.ShouldBeNull(); paymentVm.TargetAccountId.ShouldBe(0); var accountViewModel = new Fixture().Create<AccountViewModel>(); paymentVm.TargetAccount = accountViewModel; paymentVm.TargetAccount.ShouldBe(accountViewModel); paymentVm.TargetAccount.Id.ShouldBe(accountViewModel.Id); paymentVm.TargetAccountId.ShouldBe(accountViewModel.Id); }
public void Category_SetId() { var paymentVm = new PaymentViewModel(); paymentVm.Category.ShouldBeNull(); paymentVm.CategoryId.ShouldBeNull(); var category = new Fixture().Create<CategoryViewModel>(); paymentVm.Category = category; paymentVm.Category.ShouldBe(category); paymentVm.Category.Id.ShouldBe(category.Id); paymentVm.CategoryId.ShouldBe(category.Id); }
public void Enum_CorrectlyParsed(PaymentType type, int enumInt) { var cb = new ContainerBuilder(); cb.RegisterModule<DataAccessModule>(); cb.Build(); var paymentViewModel = new PaymentViewModel() { Id = 9, Type = type }; var mappedPayment = Mapper.Map<Payment>(paymentViewModel); mappedPayment.Type.ShouldBe(enumInt); }
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> /// Creates an recurring PaymentViewModel based on the Financial PaymentViewModel. /// </summary> /// <param name="payment">The financial PaymentViewModel the reuccuring shall be based on.</param> /// <param name="isEndless">If the recurrence is infinite or not.</param> /// <param name="recurrence">How often the PaymentViewModel shall be repeated.</param> /// <param name="enddate">Enddate for the recurring PaymentViewModel if it's not endless.</param> /// <returns>The new created recurring PaymentViewModel</returns> public static RecurringPaymentViewModel GetRecurringFromPayment(PaymentViewModel payment, bool isEndless, PaymentRecurrence recurrence, DateTime enddate = new DateTime()) => new RecurringPaymentViewModel { Id = payment.RecurringPaymentId, ChargedAccount = payment.ChargedAccount, ChargedAccountId = payment.ChargedAccount.Id, TargetAccount = payment.TargetAccount, TargetAccountId = payment.TargetAccount?.Id ?? 0, StartDate = payment.Date, EndDate = enddate, IsEndless = isEndless, Amount = payment.Amount, CategoryId = payment.CategoryId, Category = payment.Category, Type = payment.Type, Recurrence = recurrence, Note = payment.Note };
/// <summary> /// Checks if the recurring PaymentViewModel is up for a repetition based on the passed PaymentViewModel /// </summary> /// <param name="recurringPayment">Recurring PaymentViewModel to check.</param> /// <param name="relatedPayment">PaymentViewModel to compare.</param> /// <returns>True or False if the PaymentViewModel have to be repeated.</returns> public static bool CheckIfRepeatable(RecurringPaymentViewModel recurringPayment, PaymentViewModel relatedPayment) { if (!relatedPayment.IsCleared) { return false; } switch (recurringPayment.Recurrence) { case PaymentRecurrence.Daily: return DateTime.Today.Date != relatedPayment.Date.Date; case PaymentRecurrence.DailyWithoutWeekend: return (DateTime.Today.Date != relatedPayment.Date.Date) && (DateTime.Today.DayOfWeek != DayOfWeek.Saturday) && (DateTime.Today.DayOfWeek != DayOfWeek.Sunday); case PaymentRecurrence.Weekly: var daysWeekly = DateTime.Now - relatedPayment.Date; return daysWeekly.Days >= 7; case PaymentRecurrence.Biweekly: var daysBiweekly = DateTime.Now - relatedPayment.Date; return daysBiweekly.Days >= 14; case PaymentRecurrence.Monthly: return DateTime.Now.Month != relatedPayment.Date.Month; case PaymentRecurrence.Bimonthly: return relatedPayment.Date.Month <= DateTime.Now.AddMonths(-2).Month; case PaymentRecurrence.Yearly: return ((DateTime.Now.Year != relatedPayment.Date.Year) && (DateTime.Now.Month >= relatedPayment.Date.Month)) || (DateTime.Now.Year - relatedPayment.Date.Year > 1); default: return false; } }
public bool SavePayment(PaymentViewModel payment) { if (!payment.IsRecurring && (payment.RecurringPaymentId != 0)) { recurringPaymentRepository.Delete(payment.RecurringPayment); payment.RecurringPaymentId = 0; payment.RecurringPayment = null; } bool handledRecuringPayment; handledRecuringPayment = (payment.RecurringPayment == null) || recurringPaymentRepository.Save(payment.RecurringPayment); if (payment.RecurringPayment != null) { payment.RecurringPaymentId = payment.RecurringPayment.Id; } var saveWasSuccessful = handledRecuringPayment && paymentRepository.Save(payment); return saveWasSuccessful; }
/// <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_WithNoChildrenSet() { var paymentRepository = new PaymentRepository(new DatabaseManager(new WindowsSqliteConnectionFactory(), new MvxWpfFileStore(FILE_ROOT))); var testPayment = new PaymentViewModel { Amount = 40, ChargedAccount = new AccountViewModel() }; paymentRepository.Save(testPayment); paymentRepository.ReloadCache(); var selected = paymentRepository.FindById(testPayment.Id); selected.ShouldNotBeNull(); selected.ShouldBeInstanceOf<PaymentViewModel>(); }
public void SavePayment_RecPayment_IdInPaymentSaved() { var payment = new PaymentViewModel { Id = 2, RecurringPaymentId = 3, RecurringPayment = new RecurringPaymentViewModel {Id = 3, Amount = 300}, IsRecurring = true }; var paymentRepositorySetup = new Mock<IPaymentRepository>(); paymentRepositorySetup.Setup(x => x.GetList(null)) .Returns(new List<PaymentViewModel> {payment}); new PaymentManager(paymentRepositorySetup.Object, new Mock<IAccountRepository>().Object, new Mock<IRecurringPaymentRepository>().Object, new Mock<IDialogService>().Object).SavePayment(payment); payment.IsRecurring.ShouldBeTrue(); payment.RecurringPaymentId.ShouldBe(3); }
private void HandlePaymentAmount(PaymentViewModel payment, Func<double, double> amountFunc, Func<PaymentViewModel, AccountViewModel> getAccountFunc) { var account = getAccountFunc(payment); if (account == null) { return; } account.CurrentBalance += amountFunc(payment.Amount); accountRepository.Save(account); }
private async void OpenContextMenu(PaymentViewModel payment) { var result = await modifyDialogService.ShowEditSelectionDialog(); switch (result) { case ModifyOperation.Edit: EditPaymentCommand.Execute(payment); break; case ModifyOperation.Delete: DeletePaymentCommand.Execute(payment); break; } }
/// <summary> /// Removes the PaymentViewModel Amount from the charged AccountViewModel of this PaymentViewModel /// </summary> /// <param name="payment">PaymentViewModel to remove the AccountViewModel from.</param> public bool RemovePaymentAmount(PaymentViewModel payment) { var succeded = RemovePaymentAmount(payment, payment.ChargedAccount); return succeded; }
/// <summary> /// Adds the PaymentViewModel amount from the selected AccountViewModel /// </summary> /// <param name="payment">PaymentViewModel to add the AccountViewModel from.</param> public bool AddPaymentAmount(PaymentViewModel payment) { if (!payment.IsCleared) { return false; } PrehandleAddIfTransfer(payment); Func<double, double> amountFunc = x => payment.Type == PaymentType.Income ? x : -x; if ((payment.ChargedAccount == null) && (payment.ChargedAccountId != 0)) { payment.ChargedAccount = accountRepository.GetList(x => x.Id == payment.ChargedAccountId).FirstOrDefault(); } HandlePaymentAmount(payment, amountFunc, GetChargedAccountFunc(payment.ChargedAccount)); return true; }
public void Save_UpdateTimeStamp() { var selectedPayment = new PaymentViewModel { ChargedAccountId = 3, ChargedAccount = new AccountViewModel {Id = 3, Name = "3"} }; var localDateSetting = DateTime.MinValue; var settingsManagerMock = new Mock<ISettingsManager>(); settingsManagerMock.SetupSet(x => x.LastDatabaseUpdate = It.IsAny<DateTime>()) .Callback((DateTime x) => localDateSetting = x); var paymentRepoSetup = new Mock<IPaymentRepository>(); paymentRepoSetup.Setup(x => x.GetList(null)).Returns(new List<PaymentViewModel>()); paymentRepoSetup.Setup(x => x.FindById(It.IsAny<int>())).Returns(selectedPayment); paymentRepoSetup.Setup(x => x.Save(selectedPayment)).Returns(true); var accountRepoMock = new Mock<IAccountRepository>(); accountRepoMock.Setup(x => x.GetList(null)) .Returns(new List<AccountViewModel> { new AccountViewModel { Id = 3, Name = "3" } }); var dialogService = new Mock<IDialogService>().Object; var paymentManagerSetup = new Mock<IPaymentManager>(); paymentManagerSetup.Setup(x => x.SavePayment(It.IsAny<PaymentViewModel>())).Returns(true); paymentManagerSetup.Setup(x => x.AddPaymentAmount(It.IsAny<PaymentViewModel>())).Returns(true); var viewmodel = new ModifyPaymentViewModel(paymentRepoSetup.Object, accountRepoMock.Object, dialogService, paymentManagerSetup.Object, settingsManagerMock.Object, new Mock<IMvxMessenger>().Object, new Mock<IBackupManager>().Object) { SelectedPayment = selectedPayment }; viewmodel.SaveCommand.Execute(); localDateSetting.ShouldBeGreaterThan(DateTime.Now.AddSeconds(-1)); localDateSetting.ShouldBeLessThan(DateTime.Now.AddSeconds(1)); }
public void SaveCommand_Recurrence_RecurrenceSetCorrectly(PaymentRecurrence recurrence) { //setup var testPayment = new PaymentViewModel(); var paymentRepoSetup = new Mock<IPaymentRepository>(); var accountRepoMock = new Mock<IAccountRepository>(); var settingsManagerMock = new Mock<ISettingsManager>(); settingsManagerMock.SetupAllProperties(); var paymentManagerMock = new Mock<IPaymentManager>(); paymentManagerMock.Setup(x => x.SavePayment(It.IsAny<PaymentViewModel>())).Callback((PaymentViewModel payment) => testPayment = payment); var viewmodel = new ModifyPaymentViewModel(paymentRepoSetup.Object, accountRepoMock.Object, new Mock<IDialogService>().Object, paymentManagerMock.Object, settingsManagerMock.Object, new Mock<IMvxMessenger>().Object, new Mock<IBackupManager>().Object); viewmodel.Init(PaymentType.Income); viewmodel.SelectedPayment.ChargedAccount = new AccountViewModel(); viewmodel.SelectedPayment.IsRecurring = true; viewmodel.Recurrence = recurrence; // execute viewmodel.SaveCommand.Execute(); //Assert testPayment.RecurringPayment.ShouldNotBeNull(); testPayment.RecurringPayment.Recurrence.ShouldBe(recurrence); }
private void SetDefaultPayment(PaymentType paymentType) { SelectedPayment = new PaymentViewModel { Type = paymentType, Date = DateTime.Now, // Assign empty CategoryViewModel to reset the GUI Category = new CategoryViewModel(), ChargedAccount = ChargedAccounts.FirstOrDefault() }; }
/// <summary> /// Init the view for a new PaymentViewModel. Is executed after the constructor call. /// </summary> /// <param name="type">Type of the PaymentViewModel. Is ignored when paymentId is passed.</param> /// <param name="paymentId">The id of the PaymentViewModel to edit.</param> public void Init(PaymentType type, int paymentId = 0) { TargetAccounts = new ObservableCollection<AccountViewModel>(accountRepository.GetList()); ChargedAccounts = new ObservableCollection<AccountViewModel>(TargetAccounts); if (paymentId == 0) { IsEdit = false; IsEndless = true; amount = 0; PrepareDefault(type); } else { IsEdit = true; PaymentId = paymentId; selectedPayment = paymentRepository.FindById(PaymentId); PrepareEdit(); } AccountViewModelBeforeEdit = SelectedPayment.ChargedAccount; }
private void PrehandleAddIfTransfer(PaymentViewModel payment) { if (payment.Type == PaymentType.Transfer) { Func<double, double> amountFunc = x => x; HandlePaymentAmount(payment, amountFunc, GetTargetAccountFunc()); } }
/// <summary> /// Deletes a payment and if asks the user if the recurring payment shall be deleted as well. /// If the users says yes, delete recurring payment. /// </summary> /// <param name="payment">Payment to delete.</param> /// <returns>Returns if the operation was successful</returns> public async Task<bool> DeletePayment(PaymentViewModel payment) { paymentRepository.Delete(payment); if (!payment.IsRecurring || !await dialogService.ShowConfirmMessage(Strings.DeleteRecurringPaymentTitle, Strings.DeleteRecurringPaymentMessage)) return true; // Delete all recurring payments if the user wants so. return DeleteRecurringPaymentForPayment(payment); }
public void CheckRecurringPayments_NewEntryForDailyAndWeeklyRecurring() { //Setup var paymentRepoSetup = new Mock<IPaymentRepository>(); var resultList = new List<PaymentViewModel>(); var payment1 = new PaymentViewModel { Id = 1, Amount = 99, ChargedAccountId = 2, ChargedAccount = new AccountViewModel {Id = 2}, Date = DateTime.Now.AddDays(-1), RecurringPaymentId = 3, RecurringPayment = new RecurringPaymentViewModel { Id = 3, Recurrence = (int) PaymentRecurrence.Daily, ChargedAccountId = 2, ChargedAccount = new AccountViewModel {Id = 2}, Amount = 95 }, IsCleared = true, IsRecurring = true }; var payment2 = new PaymentViewModel { Id = 2, Amount = 105, Date = DateTime.Now.AddDays(-7), ChargedAccountId = 2, ChargedAccount = new AccountViewModel {Id = 2}, RecurringPaymentId = 4, RecurringPayment = new RecurringPaymentViewModel { Id = 4, Recurrence = PaymentRecurrence.Weekly, ChargedAccountId = 2, ChargedAccount = new AccountViewModel {Id = 2}, Amount = 105 }, IsCleared = true, IsRecurring = true }; var testList = new List<PaymentViewModel> { payment1, payment2 }; paymentRepoSetup.Setup(x => x.Save(It.IsAny<PaymentViewModel>())) .Callback((PaymentViewModel payment) => resultList.Add(payment)); paymentRepoSetup.SetupSequence(x => x.GetList(It.IsAny<Expression<Func<PaymentViewModel, bool>>>())) .Returns(new List<PaymentViewModel> {payment1}) .Returns(new List<PaymentViewModel> {payment2}); var paymentManagerSetup = new Mock<IPaymentManager>(); paymentManagerSetup.Setup(x => x.LoadRecurringPaymentList(null)) .Returns(testList); var settingsManagerMock = new Mock<ISettingsManager>(); //Execution new RecurringPaymentManager(paymentManagerSetup.Object, paymentRepoSetup.Object, settingsManagerMock.Object).CheckRecurringPayments(); //Assertion resultList.Count.ShouldBe(2); resultList[0].Amount.ShouldBe(95); resultList[0].ChargedAccountId.ShouldBe(2); resultList[0].RecurringPaymentId.ShouldBe(3); resultList[0].RecurringPayment.ShouldNotBeNull(); resultList[0].RecurringPayment.Recurrence.ShouldBe(PaymentRecurrence.Daily); resultList[1].Amount.ShouldBe(105); resultList[1].ChargedAccountId.ShouldBe(2); resultList[1].RecurringPaymentId.ShouldBe(4); resultList[1].RecurringPayment.ShouldNotBeNull(); resultList[1].RecurringPayment.Recurrence.ShouldBe(PaymentRecurrence.Weekly); }
private void EditPayment(PaymentViewModel payment) { ShowViewModel<ModifyPaymentViewModel>(new {paymentId = payment.Id}); }
public void SavePayment_RecPayment_RecPaymentSaved() { var recPaymentToSave = new RecurringPaymentViewModel {Id = 3, Amount = 300}; var payment = new PaymentViewModel { Id = 2, RecurringPaymentId = 3, RecurringPayment = recPaymentToSave, IsRecurring = true }; var recPaymentSaved = new RecurringPaymentViewModel(); var paymentRepositorySetup = new Mock<IPaymentRepository>(); paymentRepositorySetup.Setup(x => x.GetList(null)) .Returns(new List<PaymentViewModel> {payment}); var recPaymentRepositorySetup = new Mock<IRecurringPaymentRepository>(); recPaymentRepositorySetup.Setup(x => x.Save(It.IsAny<RecurringPaymentViewModel>())) .Callback((RecurringPaymentViewModel recPay) => recPaymentSaved = recPay); new PaymentManager(paymentRepositorySetup.Object, new Mock<IAccountRepository>().Object, recPaymentRepositorySetup.Object, new Mock<IDialogService>().Object).SavePayment(payment); payment.IsRecurring.ShouldBeTrue(); payment.RecurringPayment.ShouldBe(recPaymentSaved); }
private async void DeletePayment(PaymentViewModel payment) { if (!await dialogService .ShowConfirmMessage(Strings.DeleteTitle, Strings.DeletePaymentConfirmationMessage)) return; var deletePaymentSucceded = await paymentManager.DeletePayment(payment); var deleteAccountSucceded = paymentManager.RemovePaymentAmount(payment); if (deletePaymentSucceded && deleteAccountSucceded) { settingsManager.LastDatabaseUpdate = DateTime.Now; #pragma warning disable 4014 backupManager.EnqueueBackupTask(); #pragma warning restore 4014 } LoadCommand.Execute(); }
private bool DeleteRecurringPaymentForPayment(PaymentViewModel payment) { var succeed = true; if (recurringPaymentRepository.GetList().Any(x => x.Id == payment.RecurringPaymentId)) { var recpayment = recurringPaymentRepository .FindById(payment.RecurringPaymentId); if (!recurringPaymentRepository.Delete(recpayment)) { succeed = false; } // edit other payments if (succeed) { foreach (var paymentViewModel in paymentRepository.GetList(x => x.IsRecurring && x.RecurringPaymentId == recpayment.Id)) { paymentViewModel.IsRecurring = false; paymentViewModel.RecurringPayment = null; var saveSuccess = paymentRepository.Save(paymentViewModel); if (!saveSuccess) { succeed = false; } } } } return succeed; }