public void GetRecurringFromPayment(int recurrence, string date, bool isEndless, int type)
        {
            var startDate = new DateTime(2015, 03, 12);
            var enddate = Convert.ToDateTime(date);

            var payment = new Payment
            {
                ChargedAccount = new Account {Id = 3},
                TargetAccount = new Account {Id = 8},
                Category = new Category {Id = 16},
                Date = startDate,
                Amount = 2135,
                IsCleared = false,
                Type = type,
                IsRecurring = true
            };

            var recurring = RecurringPaymentHelper.GetRecurringFromPayment(payment, isEndless,
                recurrence, enddate);

            recurring.ChargedAccount.Id.ShouldBe(3);
            recurring.TargetAccount.Id.ShouldBe(8);
            recurring.StartDate.ShouldBe(startDate);
            recurring.EndDate.ShouldBe(enddate);
            recurring.IsEndless.ShouldBe(isEndless);
            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);
        }
        public void Save_DifferentPaymentTypes_CorrectlySaved()
        {
            var accountRepositorySetup = new Mock<IAccountRepository>();
            accountRepositorySetup.SetupGet(x => x.Data).Returns(new ObservableCollection<Account>());

            var categoryDataAccessSetup = new Mock<IRepository<Category>>();
            categoryDataAccessSetup.SetupGet(x => x.Data).Returns(new ObservableCollection<Category>());

            var paymentDataAccessMock = new PaymentDataAccessMock();
            var repository = new PaymentRepository(paymentDataAccessMock,
                new RecurringPaymentDataAccessMock(),
                accountRepositorySetup.Object,
                categoryDataAccessSetup.Object);

            var account = new Account
            {
                Id = 2,
                Name = "TestAccount"
            };

            var payment = new Payment
            {
                ChargedAccount = account,
                TargetAccount = null,
                Amount = 20,
                Type = (int) PaymentType.Income
            };

            repository.Save(payment);

            Assert.AreSame(payment, paymentDataAccessMock.PaymentTestList[0]);
            Assert.AreEqual((int) PaymentType.Income, paymentDataAccessMock.PaymentTestList[0].Type);
        }
        private void Edit(Payment payment)
        {
            paymentRepository.Selected = payment;

            ShowViewModel<ModifyPaymentViewModel>(
                new { isEdit = true, typeString = payment.Type.ToString() });
        }
        /// <summary>
        ///     Checks if the recurring payment is up for a repetition based on the passed Payment
        /// </summary>
        /// <param name="recurringPayment">Recurring payment to check.</param>
        /// <param name="relatedPayment">Payment to compare.</param>
        /// <returns>True or False if the payment have to be repeated.</returns>
        public static bool CheckIfRepeatable(RecurringPayment recurringPayment, Payment relatedPayment)
        {
            if (!relatedPayment.IsCleared) return false;

            switch (recurringPayment.Recurrence)
            {
                case (int) PaymentRecurrence.Daily:
                    return DateTime.Today.Date != relatedPayment.Date.Date;

                case (int) PaymentRecurrence.DailyWithoutWeekend:
                    return DateTime.Today.Date != relatedPayment.Date.Date
                           && DateTime.Today.DayOfWeek != DayOfWeek.Saturday
                           && DateTime.Today.DayOfWeek != DayOfWeek.Sunday;

                case (int) PaymentRecurrence.Weekly:
                    var daysWeekly = DateTime.Now - relatedPayment.Date;
                    return daysWeekly.Days >= 7;

                case (int) PaymentRecurrence.Biweekly:
                    var daysBiweekly = DateTime.Now - relatedPayment.Date;
                    return daysBiweekly.Days >= 14;

                case (int) PaymentRecurrence.Monthly:
                    return DateTime.Now.Month != relatedPayment.Date.Month;

                case (int) 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;
            }
        }
        private Payment GetLastOccurence(Payment payment)
        {
            var transcationList = paymentRepository.Data
                .Where(x => x.RecurringPaymentId == payment.RecurringPaymentId)
                .OrderBy(x => x.Date)
                .ToList();

            return transcationList.LastOrDefault();
        }
        private async void Delete(Payment payment)
        {
            if (!await
                dialogService.ShowConfirmMessage(Strings.DeleteTitle, Strings.DeletePaymentConfirmationMessage))
                return;

            paymentRepository.Delete(payment);
            LoadedCommand.Execute();
        }
 private double HandleTransferAmount(Payment payment, double balance)
 {
     if (AccountRepository.Selected == payment.ChargedAccount)
     {
         balance -= payment.Amount;
     } else
     {
         balance += payment.Amount;
     }
     return balance;
 }
예제 #8
0
        public async Task<bool> CheckForRecurringPayment(Payment payment)
        {
            if (!payment.IsRecurring)
            {
                return false;
            }

            return
                await
                    dialogService.ShowConfirmMessage(Strings.ChangeSubsequentPaymentTitle,
                        Strings.ChangeSubsequentPaymentMessage,
                        Strings.RecurringLabel, Strings.JustThisLabel);
        }
        public void SaveToDatabase_NewPayment_CorrectId()
        {
            var amount = 789;

            var payment = new Payment
            {
                Amount = amount
            };

            new PaymentDataAccess(connectionCreator).SaveItem(payment);

            Assert.IsTrue(payment.Id >= 1);
            Assert.AreEqual(amount, payment.Amount);
        }
        public void SaveToDatabase_ExistingPayment_CorrectId()
        {
            var payment = new Payment();

            var dataAccess = new PaymentDataAccess(connectionCreator);
            dataAccess.SaveItem(payment);

            Assert.AreEqual(0, payment.Amount);

            var id = payment.Id;

            var amount = 789;
            payment.Amount = amount;

            Assert.AreEqual(id, payment.Id);
            Assert.AreEqual(amount, payment.Amount);
        }
        public void DeleteAssociatedPaymentsFromDatabase_Account_DeleteRightPayments()
        {
            var resultList = new List<int>();

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

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

            var paymentRepositorySetup = new Mock<IPaymentRepository>();
            paymentRepositorySetup.SetupAllProperties();
            paymentRepositorySetup.Setup(x => x.Delete(It.IsAny<Payment>()))
                .Callback((Payment trans) => resultList.Add(trans.Id));
            paymentRepositorySetup.Setup(x => x.GetRelatedPayments(It.IsAny<Account>()))
                .Returns(new List<Payment>
                {
                    payment
                });

            var repo = paymentRepositorySetup.Object;
            repo.Data = new ObservableCollection<Payment>();

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

            resultList.Count.ShouldBe(1);
            resultList.First().ShouldBe(payment.Id);
        }
        public void SaveWithouthAccount_NoAccount_InvalidDataException()
        {
            var accountRepositorySetup = new Mock<IAccountRepository>();
            accountRepositorySetup.SetupGet(x => x.Data).Returns(new ObservableCollection<Account>());

            var categoryDataAccessSetup = new Mock<IRepository<Category>>();
            categoryDataAccessSetup.SetupGet(x => x.Data).Returns(new ObservableCollection<Category>());

            var repository = new PaymentRepository(new PaymentDataAccessMock(),
                new RecurringPaymentDataAccessMock(),
                accountRepositorySetup.Object,
                categoryDataAccessSetup.Object);

            var payment = new Payment
            {
                Amount = 20
            };

            repository.Save(payment);
        }
예제 #13
0
        /// <summary>
        ///     Save a new payment or update an existin one.
        /// </summary>
        /// <param name="payment">item to save</param>
        public void Save(Payment payment)
        {
            if (payment.ChargedAccountId == 0)
            {
                throw new AccountMissingException("charged accout is missing");
            }

            payment.IsCleared = payment.ClearPaymentNow;

            //delete recurring payment if isRecurring is no longer set.
            if (!payment.IsRecurring && payment.RecurringPaymentId != 0)
            {
                recurringDataAccess.DeleteItem(payment.RecurringPayment);
                payment.RecurringPaymentId = 0;
            }

            if (payment.Id == 0)
            {
                data.Add(payment);
            }
            dataAccess.SaveItem(payment);
        }
 /// <summary>
 ///     Creates an recurring Payment based on the Financial payment.
 /// </summary>
 /// <param name="payment">The financial payment the reuccuring shall be based on.</param>
 /// <param name="isEndless">If the recurrence is infinite or not.</param>
 /// <param name="recurrence">How often the payment shall be repeated.</param>
 /// <param name="enddate">Enddate for the recurring payment if it's not endless.</param>
 /// <returns>The new created recurring payment</returns>
 public static RecurringPayment GetRecurringFromPayment(Payment payment,
     bool isEndless,
     int recurrence,
     DateTime enddate = new DateTime())
 {
     return new RecurringPayment
     {
         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
     };
 }
예제 #15
0
        /// <summary>
        ///     Deletes the passed payment and removes the item from cache
        /// </summary>
        /// <param name="paymentToDelete">Payment to delete.</param>
        public void Delete(Payment paymentToDelete)
        {
            var payments = Data.Where(x => x.Id == paymentToDelete.Id).ToList();

            foreach (var payment in payments)
            {
                data.Remove(payment);
                dataAccess.DeleteItem(payment);

                // If this accountToDelete was the last finacial accountToDelete for the linked recurring accountToDelete
                // delete the db entry for the recurring accountToDelete.
                DeleteRecurringPaymentIfLastAssociated(payment);
            }
        }
예제 #16
0
        private void DeleteRecurringPaymentIfLastAssociated(Payment item)
        {
            if (Data.All(x => x.RecurringPaymentId != item.RecurringPaymentId))
            {
                var recurringList = recurringDataAccess.LoadList(x => x.Id == item.RecurringPaymentId).ToList();

                foreach (var recTrans in recurringList)
                {
                    recurringDataAccess.DeleteItem(recTrans);
                }
            }
        }
예제 #17
0
        /// <summary>
        ///     Deletes the passed recurring payment
        /// </summary>
        /// <param name="paymentToDelete">Recurring payment to delete.</param>
        public void DeleteRecurring(Payment paymentToDelete)
        {
            var payments = Data.Where(x => x.Id == paymentToDelete.Id).ToList();

            recurringDataAccess.DeleteItem(paymentToDelete.RecurringPayment);

            foreach (var payment in payments)
            {
                payment.RecurringPayment = null;
                payment.IsRecurring = false;
                Save(payment);
            }
        }
예제 #18
0
        /// <summary>
        ///     Adds the payment amount from the selected account
        /// </summary>
        /// <param name="payment">Payment to add the account from.</param>
        public void AddPaymentAmount(Payment payment)
        {
            if (!payment.IsCleared) return;

            PrehandleAddIfTransfer(payment);

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

            HandlePaymentAmount(payment, amountFunc, GetChargedAccountFunc(payment.ChargedAccount));
        }
예제 #19
0
 private void PrehandleAddIfTransfer(Payment payment)
 {
     if (payment.Type == (int)PaymentType.Transfer)
     {
         Func<double, double> amountFunc = x => x;
         HandlePaymentAmount(payment, amountFunc, GetTargetAccountFunc());
     }
 }
        public void RemoveRecurringForPayments_RecTrans_PaymentPropertiesProperlyChanged()
        {
            var payment = new Payment
            {
                Id = 2,
                RecurringPaymentId = 3,
                RecurringPayment = new RecurringPayment {Id = 3},
                IsRecurring = true
            };

            var paymentRepositorySetup = new Mock<IPaymentRepository>();
            paymentRepositorySetup.SetupAllProperties();

            var paymentRepository = paymentRepositorySetup.Object;
            paymentRepository.Data = new ObservableCollection<Payment>(new List<Payment> {payment});

            new PaymentManager(paymentRepository,
                new Mock<IAccountRepository>().Object,
                new Mock<IDialogService>().Object).RemoveRecurringForPayments(payment.RecurringPayment);

            payment.IsRecurring.ShouldBeFalse();
            payment.RecurringPaymentId.ShouldBe(0);
        }
예제 #21
0
        /// <summary>
        ///     Removes the payment Amount from the selected account
        /// </summary>
        /// <param name="payment">Payment to remove.</param>
        /// <param name="account">Account to remove the amount from.</param>
        public void RemovePaymentAmount(Payment payment, Account account)
        {
            if (!payment.IsCleared) return;

            PrehandleRemoveIfTransfer(payment);

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

            HandlePaymentAmount(payment, amountFunc, GetChargedAccountFunc(account));
        }
예제 #22
0
 /// <summary>
 ///     Removes the payment Amount from the charged account of this payment
 /// </summary>
 /// <param name="payment">Payment to remove the account from.</param>
 public void RemovePaymentAmount(Payment payment)
 {
     RemovePaymentAmount(payment, payment.ChargedAccount);
 }
 private void SetDefaultPayment(PaymentType paymentType)
 {
     SelectedPayment = new Payment
     {
         Type = (int) paymentType,
         Date = DateTime.Now,
         // Assign empty category to reset the GUI
         Category = new Category()
     };
 }
        public void DeletePayment_Transfer_Deleted(bool isCleared)
        {
            var deletedId = 0;

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

            var payment = new Payment
            {
                Id = 10,
                ChargedAccountId = account1.Id,
                ChargedAccount = account1,
                TargetAccountId = account2.Id,
                TargetAccount = account2,
                Amount = 50,
                Type = (int) PaymentType.Transfer,
                IsCleared = isCleared
            };


            var paymentDataAccessMockSetup = new Mock<IDataAccess<Payment>>();
            paymentDataAccessMockSetup.Setup(x => x.DeleteItem(It.IsAny<Payment>()))
                .Callback((Payment trans) => deletedId = trans.Id);
            paymentDataAccessMockSetup.Setup(x => x.SaveItem(It.IsAny<Payment>()));
            paymentDataAccessMockSetup.Setup(x => x.LoadList(null)).Returns(new List<Payment> {payment});

            var recPaymentDataAccessMockSetup = new Mock<IDataAccess<RecurringPayment>>();
            recPaymentDataAccessMockSetup.Setup(x => x.DeleteItem(It.IsAny<RecurringPayment>()));
            recPaymentDataAccessMockSetup.Setup(x => x.LoadList(It.IsAny<Expression<Func<RecurringPayment, bool>>>()))
                .Returns(new List<RecurringPayment>());

            var accountRepositorySetup = new Mock<IAccountRepository>();
            accountRepositorySetup.SetupGet(x => x.Data).Returns(new ObservableCollection<Account> {account1, account2});

            var categoryDataAccessSetup = new Mock<IRepository<Category>>();
            categoryDataAccessSetup.SetupGet(x => x.Data).Returns(new ObservableCollection<Category>());

            new PaymentRepository(paymentDataAccessMockSetup.Object,
                recPaymentDataAccessMockSetup.Object,
                accountRepositorySetup.Object,
                categoryDataAccessSetup.Object).Delete(
                    payment);

            Assert.AreEqual(10, deletedId);
            Assert.AreEqual(500, account1.CurrentBalance);
            Assert.AreEqual(900, account2.CurrentBalance);
        }
        public void AddItemToDataList_SaveAccount_IsAddedToData()
        {
            var accountRepositorySetup = new Mock<IAccountRepository>();
            accountRepositorySetup.SetupGet(x => x.Data).Returns(new ObservableCollection<Account>());

            var categoryDataAccessSetup = new Mock<IRepository<Category>>();
            categoryDataAccessSetup.SetupGet(x => x.Data).Returns(new ObservableCollection<Category>());

            var repository = new PaymentRepository(new PaymentDataAccessMock(),
                new RecurringPaymentDataAccessMock(),
                accountRepositorySetup.Object,
                categoryDataAccessSetup.Object);

            var account = new Account
            {
                Id = 2,
                Name = "TestAccount"
            };

            var payment = new Payment
            {
                ChargedAccount = account,
                Amount = 20,
                Type = (int) PaymentType.Transfer
            };

            repository.Save(payment);
            Assert.IsTrue(repository.Data.Contains(payment));
        }
        public void Save_TransferPayment_CorrectlySaved()
        {
            var accountRepositorySetup = new Mock<IAccountRepository>();
            accountRepositorySetup.SetupGet(x => x.Data).Returns(new ObservableCollection<Account>());

            var categoryDataAccessSetup = new Mock<IRepository<Category>>();
            categoryDataAccessSetup.SetupGet(x => x.Data).Returns(new ObservableCollection<Category>());

            var paymentDataAccessMock = new PaymentDataAccessMock();
            var repository = new PaymentRepository(new PaymentDataAccessMock(),
                new RecurringPaymentDataAccessMock(),
                accountRepositorySetup.Object,
                categoryDataAccessSetup.Object);

            var account = new Account
            {
                Id = 2,
                Name = "TestAccount"
            };

            var targetAccount = new Account
            {
                Id = 3,
                Name = "targetAccount"
            };

            var payment = new Payment
            {
                ChargedAccount = account,
                ChargedAccountId = 2,
                TargetAccount = targetAccount,
                TargetAccountId = 3,
                Amount = 20,
                Type = (int) PaymentType.Transfer
            };

            repository.Save(payment);

            Assert.AreSame(payment, repository.Data[0]);
            Assert.AreEqual((int) PaymentType.Transfer, repository.Data[0].Type);
        }
        public void PaymentRepository_Delete()
        {
            var accountRepositorySetup = new Mock<IAccountRepository>();
            accountRepositorySetup.SetupGet(x => x.Data).Returns(new ObservableCollection<Account>());

            var categoryDataAccessSetup = new Mock<IRepository<Category>>();
            categoryDataAccessSetup.SetupGet(x => x.Data).Returns(new ObservableCollection<Category>());

            var paymentDataAccessMock = new PaymentDataAccessMock();
            var repository = new PaymentRepository(new PaymentDataAccessMock(),
                new RecurringPaymentDataAccessMock(),
                accountRepositorySetup.Object,
                categoryDataAccessSetup.Object);

            var account = new Account
            {
                Id = 2,
                Name = "TestAccount"
            };

            var payment = new Payment
            {
                ChargedAccount = account,
                ChargedAccountId = 2,
                Amount = 20
            };

            repository.Save(payment);
            Assert.AreSame(payment, repository.Data[0]);

            repository.Delete(payment);

            Assert.IsFalse(repository.Data.Any());
        }
예제 #28
0
        private void HandlePaymentAmount(Payment payment,
            Func<double, double> amountFunc,
            Func<Payment, Account> getAccountFunc)
        {
            var account = getAccountFunc(payment);
            if (account == null)
            {
                return;
            }

            account.CurrentBalance += amountFunc(payment.Amount);
            Save(account);
        }
        public void SaveToDatabase_CreateAndUpdatePayment_CorrectlyUpdated()
        {
            var firstAmount = 5555555;
            var secondAmount = 222222222;

            var payment = new Payment
            {
                Amount = firstAmount
            };

            var dataAccess = new PaymentDataAccess(connectionCreator);
            dataAccess.SaveItem(payment);

            Assert.AreEqual(firstAmount, dataAccess.LoadList().FirstOrDefault(x => x.Id == payment.Id).Amount);

            payment.Amount = secondAmount;
            dataAccess.SaveItem(payment);

            var categories = dataAccess.LoadList();
            Assert.IsFalse(categories.Any(x => Math.Abs(x.Amount - firstAmount) < 0.1));
            Assert.AreEqual(secondAmount, categories.First(x => x.Id == payment.Id).Amount);
        }
        public void DeleteFromDatabase_PaymentToDelete_CorrectlyDelete()
        {
            var payment = new Payment
            {
                Note = "paymentToDelete",
            };

            var dataAccess = new PaymentDataAccess(connectionCreator);
            dataAccess.SaveItem(payment);

            Assert.IsTrue(dataAccess.LoadList(x => x.Id == payment.Id).Any());

            dataAccess.DeleteItem(payment);
            Assert.IsFalse(dataAccess.LoadList(x => x.Id == payment.Id).Any());
        }