Exemplo n.º 1
0
            public async Task <Unit> Handle(CreateRecurringPaymentsCommand request, CancellationToken cancellationToken)
            {
                List <RecurringPayment> recurringPayments = await contextAdapter.Context
                                                            .RecurringPayments
                                                            .Include(x => x.ChargedAccount)
                                                            .Include(x => x.TargetAccount)
                                                            .Include(x => x.Category)
                                                            .Include(x => x.RelatedPayments)
                                                            .AsQueryable()
                                                            .IsNotExpired()
                                                            .ToListAsync();

                var recPaymentsToCreate = recurringPayments
                                          .Where(x => x.RelatedPayments.Any())
                                          .Where(x => RecurringPaymentHelper.CheckIfRepeatable(x.RelatedPayments
                                                                                               .OrderByDescending(d => d.Date)
                                                                                               .First()))
                                          .Select(x => new Payment(RecurringPaymentHelper.GetPaymentDateFromRecurring(x),
                                                                   x.Amount,
                                                                   x.Type,
                                                                   x.ChargedAccount,
                                                                   x.TargetAccount,
                                                                   x.Category,
                                                                   x.Note ?? "",
                                                                   x))
                                          .ToList();

                recPaymentsToCreate.ForEach(x => x.RecurringPayment?.SetLastRecurrenceCreatedDate());

                contextAdapter.Context.Payments.AddRange(recPaymentsToCreate);
                await contextAdapter.Context.SaveChangesAsync();

                return(Unit.Value);
            }
Exemplo n.º 2
0
        public void CheckIfRepeatable_ValidatedRecurrenceMonthly_False()
        {
            var account = new AccountEntity {
                Id = 2
            };

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

            RecurringPaymentHelper.CheckIfRepeatable(new Payment
            {
                Data =
                {
                    Date             = DateTime.Today.GetFirstDayOfMonth(),
                    IsCleared        = true,
                    RecurringPayment = recurringPayment
                }
            })
            .ShouldBeFalse();
        }
Exemplo n.º 3
0
        public void CheckIfRepeatable_UnclearedPayment_ReturnFalse(
            PaymentRecurrence recurrence, int amountOfDaysUntilRepeat)
        {
            var account = new AccountEntity {
                Id = 2
            };

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

            RecurringPaymentHelper.CheckIfRepeatable(new Payment
            {
                Data =
                {
                    Date             = DateTime.Today.AddDays(amountOfDaysUntilRepeat),
                    RecurringPayment = recurringPayment
                }
            })
            .ShouldBeFalse();
        }
Exemplo n.º 4
0
        [InlineData(PaymentRecurrence.Quarterly, 355, true)]  // with year change
        public void CheckIfRepeatable_ValidatedRecurrence(PaymentRecurrence recurrence, int amountOfDaysPassed,
                                                          bool expectedResult)
        {
            var account = new AccountEntity {
                Id = 2
            };

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

            RecurringPaymentHelper.CheckIfRepeatable(
                new Payment
            {
                Data =
                {
                    Date             = DateTime.Today.AddDays(-amountOfDaysPassed),
                    IsCleared        = true,
                    RecurringPayment = recurringPayment
                }
            })
            .ShouldBe(expectedResult);
        }
        public void CheckIfRepeatable_ValidatedRecurrenceMonthly_False()
        {
            var account = new Account("foo");

            var payment = new Payment(DateTime.Today.GetFirstDayOfMonth(), 105, PaymentType.Expense, account);

            payment.AddRecurringPayment(PaymentRecurrence.Monthly, DateTime.Today);

            RecurringPaymentHelper.CheckIfRepeatable(payment)
            .ShouldBeFalse();
        }
        public void CheckIfRepeatable_UnclearedPayment_ReturnFalse(PaymentRecurrence recurrence, int amountOfDaysUntilRepeat)
        {
            var account = new Account("foo");

            var payment = new Payment(DateTime.Today.AddDays(amountOfDaysUntilRepeat), 105, PaymentType.Expense, account);

            payment.AddRecurringPayment(recurrence, DateTime.Today);

            RecurringPaymentHelper.CheckIfRepeatable(payment)
            .ShouldBeFalse();
        }
        [InlineData(PaymentRecurrence.Quarterly, 355, true)]  // with year change
        public void CheckIfRepeatable_ValidatedRecurrence(PaymentRecurrence recurrence, int amountOfDaysPassed, bool expectedResult)
        {
            var account = new Account("foo");

            var payment = new Payment(DateTime.Today.AddDays(-amountOfDaysPassed), 105, PaymentType.Expense, account);

            payment.AddRecurringPayment(recurrence, DateTime.Today);

            RecurringPaymentHelper.CheckIfRepeatable(payment)
            .ShouldEqual(expectedResult);
        }
Exemplo n.º 8
0
        public void CheckIfRepeatable_ValidatedRecurrence_Bimonthly()
        {
            var account = new Account("foo");

            var payment = new Payment(DateTime.Today.AddMonths(-2), 105, PaymentType.Expense, account);

            payment.AddRecurringPayment(PaymentRecurrence.Bimonthly, DateTime.Today);

            RecurringPaymentHelper.CheckIfRepeatable(payment)
            .ShouldEqual(true);
        }
Exemplo n.º 9
0
        /// <summary>
        ///     Checks if one of the recurring payment has to be repeated
        /// </summary>
        public void CheckRecurringPayments()
        {
            var paymentList = paymentRepository.LoadRecurringList();

            foreach (var payment in paymentList.Where(x => x.ChargedAccount != null))
            {
                var relatedPayment = GetLastOccurence(payment);

                if (RecurringPaymentHelper.CheckIfRepeatable(payment.RecurringPayment, relatedPayment))
                {
                    var newPayment = RecurringPaymentHelper.GetPaymentFromRecurring(payment.RecurringPayment);

                    paymentRepository.Save(newPayment);
                    accountRepository.AddPaymentAmount(newPayment);
                }
            }
        }
Exemplo n.º 10
0
        public void CheckIfRepeatable_UnclearedPayment_ReturnFalse()
        {
            var account = new Account {
                Id = 2
            };

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

            RecurringPaymentHelper.CheckIfRepeatable(recurringPayment,
                                                     new Payment {
                Date = DateTime.Today.AddDays(11)
            }).ShouldBeFalse();
        }
Exemplo n.º 11
0
        /// <summary>
        ///     Checks if one of the recurring payment has to be repeated
        /// </summary>
        public void CheckRecurringPayments()
        {
            var paymentList = paymentManager.LoadRecurringPaymentList();

            foreach (var payment in paymentList.Where(x => x.ChargedAccount != null))
            {
                var relatedPayment = GetLastOccurence(payment);

                if (RecurringPaymentHelper.CheckIfRepeatable(payment.RecurringPayment, relatedPayment))
                {
                    var newPayment = RecurringPaymentHelper.GetPaymentFromRecurring(payment.RecurringPayment);

                    var paymentSucceded = paymentRepository.Save(newPayment);
                    var accountSucceded = paymentManager.AddPaymentAmount(newPayment);
                    if (paymentSucceded && accountSucceded)
                    {
                        settingsManager.LastDatabaseUpdate = DateTime.Now;
                    }
                }
            }
        }
Exemplo n.º 12
0
        public void CheckIfRepeatable_Yearly_ValidatedRecurrence()
        {
            var account = new Account {
                Id = 2
            };

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

            RecurringPaymentHelper.CheckIfRepeatable(recurringPayment,
                                                     new Payment {
                Date = DateTime.Today.AddDays(-380), IsCleared = true
            })
            .ShouldBeTrue();
        }
Exemplo n.º 13
0
        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();
        }