public void SetDaysInLearning(CollectionPeriod collectionPeriod)
        {
            var censusDate = collectionPeriod.CensusDate;

            int days = 0;

            foreach (var learningPeriod in Model.LearningPeriods)
            {
                if (!learningPeriod.EndDate.HasValue)
                {
                    days += censusDate.Subtract(learningPeriod.StartDate.Date).Days + 1;
                }
                else
                {
                    if (learningPeriod.EndDate.Value.Date < censusDate)
                    {
                        days += learningPeriod.EndDate.Value.Date.Subtract(learningPeriod.StartDate.Date).Days + 1;
                    }
                    else
                    {
                        days += censusDate.Subtract(learningPeriod.StartDate.Date).Days + 1;
                    }
                }
            }

            var daysInLearning = new DaysInLearning(collectionPeriod.PeriodNumber, collectionPeriod.AcademicYear, days);
            var existing       = Model.DaysInLearnings.SingleOrDefault(d => d.CollectionPeriodNumber == collectionPeriod.PeriodNumber && d.CollectionYear == collectionPeriod.AcademicYear);

            if (existing != null)
            {
                Model.DaysInLearnings.Remove(existing);
            }

            Model.DaysInLearnings.Add(daysInLearning);
        }
        public async Task Then_existing_days_in_learning_are_updated_in_the_data_store()
        {
            // Arrange
            var existingLearner = _fixture.Create <ApprenticeshipIncentives.Models.Learner>();
            await _dbContext.AddAsync(existingLearner);

            await _dbContext.SaveChangesAsync();

            var testLearner =
                _fixture.Build <LearnerModel>()
                .With(l => l.Id, existingLearner.Id)
                .Create();

            var d        = existingLearner.DaysInLearnings.First();
            var expected = new DaysInLearning(new Domain.ValueObjects.CollectionPeriod(d.CollectionPeriodNumber, d.CollectionPeriodYear), _fixture.Create <int>());

            testLearner.DaysInLearnings.Add(expected);

            // Act
            await _sut.Update(testLearner);

            await _dbContext.SaveChangesAsync();

            // Assert
            var storedLearner = _dbContext.Learners.Single();

            storedLearner.DaysInLearnings.Should().HaveCount(4);
            storedLearner.DaysInLearnings.Any(x => x.CollectionPeriodNumber == expected.CollectionPeriod.PeriodNumber &&
                                              x.CollectionPeriodYear == expected.CollectionPeriod.AcademicYear &&
                                              x.NumberOfDaysInLearning == expected.NumberOfDays)
            .Should().BeTrue();
        }
        public void Arrange()
        {
            _fixture = new Fixture();

            _sutModel = _fixture
                        .Build <LearnerModel>()
                        .Create();

            _collectionPeriod = _fixture.Create <CollectionPeriod>();

            _daysInLearning = new DaysInLearning(_collectionPeriod.PeriodNumber, _collectionPeriod.AcademicYear, _fixture.Create <int>());

            _sutModel.DaysInLearnings = new List <DaysInLearning>()
            {
                new DaysInLearning((byte)(_collectionPeriod.PeriodNumber - 1), (short)(_collectionPeriod.AcademicYear - 1), _fixture.Create <int>()),
                _daysInLearning,
                new DaysInLearning((byte)(_collectionPeriod.PeriodNumber + 1), (short)(_collectionPeriod.AcademicYear + 1), _fixture.Create <int>()),
            };

            _sut = Sut(_sutModel);
        }
        public void Arrange()
        {
            _fixture = new Fixture();

            _mockIncentiveDomainRespository = new Mock <IApprenticeshipIncentiveDomainRepository>();
            _mockCollectionCalendarService  = new Mock <ICollectionCalendarService>();
            _mockAccountDomainRepository    = new Mock <IAccountDomainRepository>();
            _mockLearnerDomainRepository    = new Mock <ILearnerDomainRepository>();

            _startDate       = DateTime.Today;
            _payment1DueDate = _startDate.AddDays(10);

            _collectionCalendarPeriods = new List <Domain.ValueObjects.CollectionCalendarPeriod>()
            {
                new Domain.ValueObjects.CollectionCalendarPeriod(
                    new Domain.ValueObjects.CollectionPeriod(1, (short)DateTime.Now.Year),
                    (byte)DateTime.Now.Month,
                    (short)DateTime.Now.Year,
                    DateTime.Now.AddDays(-1),
                    DateTime.Now,
                    true,
                    false)
            };

            _mockCollectionCalendarService
            .Setup(m => m.Get())
            .ReturnsAsync(new Domain.ValueObjects.CollectionCalendar(_collectionCalendarPeriods));

            _vrfVendorId = Guid.NewGuid().ToString();
            var legalEntity = _fixture.Build <LegalEntityModel>()
                              .With(l => l.VrfVendorId, _vrfVendorId)
                              .With(l => l.SignedAgreementVersion, 5)
                              .Create();

            var accountModel = _fixture.Build <AccountModel>()
                               .With(a => a.LegalEntityModels, new List <LegalEntityModel>()
            {
                legalEntity
            })
                               .Create();

            var domainAccount = Domain.Accounts.Account.Create(accountModel);

            _account = new Account(accountModel.Id, legalEntity.AccountLegalEntityId);

            var pendingPayment1 = _fixture.Build <PendingPaymentModel>()
                                  .With(m => m.Account, _account)
                                  .With(m => m.DueDate, _payment1DueDate)
                                  .With(m => m.PendingPaymentValidationResultModels, new List <PendingPaymentValidationResultModel>()).Create();

            var pendingPayment2 = _fixture.Build <PendingPaymentModel>()
                                  .With(m => m.Account, _account)
                                  .With(m => m.DueDate, _payment1DueDate.AddDays(2))
                                  .With(m => m.PendingPaymentValidationResultModels, new List <PendingPaymentValidationResultModel>()).Create();

            var pendingPayments = new List <PendingPaymentModel>()
            {
                pendingPayment1, pendingPayment2
            };

            var model = _fixture.Build <ApprenticeshipIncentiveModel>()
                        .With(m => m.Account, _account)
                        .With(m => m.StartDate, _startDate)
                        .With(m => m.PendingPaymentModels, pendingPayments)
                        .With(m => m.PausePayments, false)
                        .With(m => m.MinimumAgreementVersion, new AgreementVersion(4))
                        .Create();

            var incentive = new ApprenticeshipIncentiveFactory().GetExisting(model.Id, model);

            _fixture.Register(() => incentive);

            _mockIncentiveDomainRespository
            .Setup(m => m.Find(incentive.Id))
            .ReturnsAsync(incentive);

            _mockAccountDomainRepository
            .Setup(m => m.Find(incentive.Account.Id))
            .ReturnsAsync(domainAccount);

            var submissionData = new SubmissionData();

            submissionData.SetSubmissionDate(DateTime.UtcNow);
            submissionData.SetLearningData(new LearningData(true));
            submissionData.LearningData.SetIsInLearning(true);

            _daysInLearning = new DaysInLearning(new Domain.ValueObjects.CollectionPeriod(1, (short)DateTime.Now.Year), 90);

            _learnerModel = _fixture.Build <LearnerModel>()
                            .With(m => m.ApprenticeshipId, incentive.Apprenticeship.Id)
                            .With(m => m.ApprenticeshipIncentiveId, incentive.Id)
                            .With(m => m.UniqueLearnerNumber, incentive.Apprenticeship.UniqueLearnerNumber)
                            .With(m => m.SubmissionData, submissionData)
                            .With(m => m.DaysInLearnings, new List <DaysInLearning>()
            {
                _daysInLearning
            })
                            .Create();

            _learner = new LearnerFactory().GetExisting(_learnerModel);

            _mockLearnerDomainRepository
            .Setup(m => m.GetOrCreate(incentive))
            .ReturnsAsync(_learner);

            _sut = new ValidatePendingPaymentCommandHandler(
                _mockIncentiveDomainRespository.Object,
                _mockAccountDomainRepository.Object,
                _mockCollectionCalendarService.Object,
                _mockLearnerDomainRepository.Object);
        }