public List <ApprenticeshipContractTypeEarningsEvent> Build(ProcessLearnerCommand learnerSubmission)
        {
            var intermediateResults = InitialLearnerTransform(learnerSubmission, true);
            var results             = new List <ApprenticeshipContractTypeEarningsEvent>();

            foreach (var intermediateLearningAim in intermediateResults)
            {
                var episodesByContractType = intermediateLearningAim.PriceEpisodes.GroupBy(x => x.PriceEpisodeValues.PriceEpisodeContractType);

                foreach (var priceEpisodes in episodesByContractType)
                {
                    var learnerWithSortedPriceEpisodes = intermediateLearningAim.CopyReplacingPriceEpisodes(priceEpisodes);

                    var earningEvent = factory.Create(priceEpisodes.Key);
                    if (!earningEvent.IsPayable)
                    {
                        continue;
                    }

                    mapper.Map(learnerWithSortedPriceEpisodes, earningEvent);

                    results.Add(earningEvent);
                }
            }

            return(results);
        }
        public List <ApprenticeshipContractTypeEarningsEvent> Build(ProcessLearnerCommand learnerSubmission)
        {
            var intermediateResults = InitialLearnerTransform(learnerSubmission, true);
            var results             = new List <ApprenticeshipContractTypeEarningsEvent>();

            foreach (var intermediateLearningAim in intermediateResults)
            {
                var episodesByContractType = intermediateLearningAim.PriceEpisodes.GroupBy(x => x.PriceEpisodeValues.PriceEpisodeContractType);
                var redundancyDates        = intermediateLearningAim.PriceEpisodes
                                             .Where(pe => pe.PriceEpisodeValues.PriceEpisodeRedStatusCode == 1 && pe.PriceEpisodeValues.PriceEpisodeRedStartDate.HasValue)
                                             .OrderBy(pe => pe.PriceEpisodeValues.PriceEpisodeRedStartDate)
                                             .Select(pe => new { pe.PriceEpisodeValues.PriceEpisodeRedStartDate, pe.PriceEpisodeIdentifier }).FirstOrDefault();


                foreach (var priceEpisodes in episodesByContractType)
                {
                    var learnerWithSortedPriceEpisodes =
                        intermediateLearningAim.CopyReplacingPriceEpisodes(priceEpisodes);

                    var earningEvent = factory.Create(priceEpisodes.Key);
                    if (!earningEvent.IsPayable)
                    {
                        continue;
                    }

                    mapper.Map(learnerWithSortedPriceEpisodes, earningEvent);

                    if (redundancyDates != null && earningEvent.PriceEpisodes.Any(pe => pe.Identifier == redundancyDates.PriceEpisodeIdentifier))
                    {
                        results.AddRange(redundancyEarningService.SplitContractEarningByRedundancyDate(earningEvent, redundancyDates.PriceEpisodeRedStartDate.Value));
                    }
                    else
                    {
                        results.Add(earningEvent);
                    }
                }
            }

            return(results);
        }