private async Task SetLearningStoppedChangeOfCircumstance(LearningStoppedStatus learningStoppedStatus, ICollectionCalendarService collectionCalendarService)
        {
            if (learningStoppedStatus.LearningStopped && Model.Status != IncentiveStatus.Stopped)
            {
                Model.Status = IncentiveStatus.Stopped;
                StartBreakInLearning(learningStoppedStatus.DateStopped.Value);
                await RemoveEarningsAfterStopDate(learningStoppedStatus.DateStopped.Value, collectionCalendarService);

                AddEvent(new LearningStopped(
                             Model.Id,
                             learningStoppedStatus.DateStopped.Value));
            }
            else if (Model.Status == IncentiveStatus.Stopped && !learningStoppedStatus.LearningStopped)
            {
                Model.Status = IncentiveStatus.Active;
                StopBreakInLearning(learningStoppedStatus);

                if (learningStoppedStatus.DateResumed.HasValue)
                {
                    AddEvent(new LearningResumed(
                                 Model.Id,
                                 learningStoppedStatus.DateResumed.Value));
                }
            }
        }
        public async Task Then_the_learning_stopped_event_is_raised_when_learning_is_stopped_and_the_incentive_is_already_in_a_stopped_state()
        {
            //Arrange
            _incentiveModel.Status = IncentiveStatus.Stopped;

            var command = new LearnerChangeOfCircumstanceCommand(_incentive.Id);

            var stoppedStatus = new LearningStoppedStatus(true, DateTime.Now.AddDays(-2));

            var learningData = new LearningData(true);

            learningData.SetIsStopped(stoppedStatus);
            var submissionData = _fixture.Create <SubmissionData>();

            submissionData.SetSubmissionDate(DateTime.Today);
            submissionData.SetLearningData(learningData);

            _learner = new LearnerFactory().GetExisting(
                _fixture.Build <LearnerModel>()
                .With(x => x.SubmissionData, submissionData)
                .With(x => x.ApprenticeshipIncentiveId, _incentive.Id)
                .With(x => x.ApprenticeshipIncentiveId, _incentive.Id)
                .Create());

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

            // Act
            await _sut.Handle(command);

            // Assert
            _incentive.FlushEvents().Count(e => e is LearningStopped).Should().Be(0);
        }
        private void StopBreakInLearning(LearningStoppedStatus status)
        {
            var stopDate = status.DateResumed.Value.AddDays(-1);

            if (Model.BreakInLearnings.Any(b => b.EndDate == stopDate.Date) || Model.BreakInLearnings.Count == 0)
            {
                return;
            }

            var activeBreak = Model.BreakInLearnings.Single(b => !b.EndDate.HasValue);

            if (stopDate.Date <= activeBreak.StartDate) // EI-1195
            {
                Model.BreakInLearnings.Remove(activeBreak);
                status.Undo();
                AddEvent(new BreakInLearningDeleted(Model.Id));
            }
            else
            {
                Model.BreakInLearnings.Single(b => !b.EndDate.HasValue).SetEndDate(stopDate);
            }
        }
        public static LearningStoppedStatus IsStopped(this LearnerSubmissionDto learnerData, Domain.ApprenticeshipIncentives.ApprenticeshipIncentive incentive)
        {
            var learningStoppedStatus = new LearningStoppedStatus(false);

            if (incentive == null)
            {
                return(learningStoppedStatus);
            }

            var matchedRecords =
                (from tr in learnerData.Training
                 where tr.Reference == PROGRAM_REFERENCE
                 from pe in tr.PriceEpisodes
                 from p in pe.Periods
                 where p.ApprenticeshipId == incentive.Apprenticeship.Id
                 select new
            {
                p.ApprenticeshipId,
                pe.StartDate,
                pe.EndDate,
            }).ToArray();

            if (matchedRecords.Any())
            {
                var latestPriceEpisode = matchedRecords.OrderByDescending(m => m.StartDate).First();

                if (latestPriceEpisode.EndDate.HasValue && latestPriceEpisode.EndDate.Value.Date < DateTime.Today.Date)
                {
                    learningStoppedStatus = new LearningStoppedStatus(true, latestPriceEpisode.EndDate.Value.AddDays(1));
                }
                else
                {
                    learningStoppedStatus = new LearningStoppedStatus(false, latestPriceEpisode.StartDate);
                }
            }

            return(learningStoppedStatus);
        }