private void RemoveDuplicateMatches()
        {
            var groupedMatches = PriceEpisodeMatches.GroupBy(x => x.PriceEpisodeIdentifier);

            foreach (var groupedMatch in groupedMatches)
            {
                if (groupedMatch.Any(x => x.IsSuccess))
                {
                    PriceEpisodeMatches.RemoveAll(x => x.PriceEpisodeIdentifier == groupedMatch.Key &&
                                                  !x.IsSuccess);
                }
            }
        }
        public void Add(RawEarning earning, List <string> errors, TransactionTypesFlag paymentType, DasCommitment commitment)
        {
            var payable = false;

            if (errors.Any())
            {
                foreach (var error in errors)
                {
                    if (ValidationErrors.DoesNotAlreadyContainEarningForThisError(earning, error) &&
                        PriceEpisodePeriodMatches.DoesNotContainEarningForCommitmentAndPaymentType(earning, commitment, paymentType))
                    {
                        ValidationErrors.Add(new DatalockValidationError
                        {
                            LearnRefNumber         = earning.LearnRefNumber,
                            AimSeqNumber           = earning.AimSeqNumber,
                            PriceEpisodeIdentifier = earning.PriceEpisodeIdentifier,
                            RuleId = error,
                            Ukprn  = earning.Ukprn,
                        });
                    }

                    if (ValidationErrorsByPeriod.DoesNotAlreadyContainEarningForThisError(earning, error))
                    {
                        ValidationErrorsByPeriod.Add(new DatalockValidationErrorByPeriod()
                        {
                            LearnRefNumber         = earning.LearnRefNumber,
                            AimSeqNumber           = earning.AimSeqNumber,
                            PriceEpisodeIdentifier = earning.PriceEpisodeIdentifier,
                            RuleId = error,
                            Ukprn  = earning.Ukprn,
                            Period = earning.Period,
                        });
                    }
                }
            }
            else
            {
                payable = true;
            }

            if (commitment == null)
            {
                return;
            }

            PriceEpisodePeriodMatches.Add(new PriceEpisodePeriodMatchEntity
            {
                AimSeqNumber           = earning.AimSeqNumber,
                CommitmentId           = commitment.CommitmentId,
                LearnRefNumber         = earning.LearnRefNumber,
                PriceEpisodeIdentifier = earning.PriceEpisodeIdentifier,
                Period = earning.Period,
                TransactionTypesFlag = paymentType,
                Payable   = payable,
                Ukprn     = earning.Ukprn,
                VersionId = commitment.VersionId,
            });

            if (payable)
            {
                var validationErrorsToRemove = ValidationErrors.Where(x =>
                                                                      x.Ukprn == earning.Ukprn &&
                                                                      x.LearnRefNumber == earning.LearnRefNumber &&
                                                                      x.PriceEpisodeIdentifier == earning.PriceEpisodeIdentifier)
                                               .ToList();

                foreach (var validationError in validationErrorsToRemove)
                {
                    ValidationErrors.Remove(validationError);
                }
            }

            if (PriceEpisodeMatches.DoesNotAlreadyContainEarningForCommitment(earning, commitment, payable))
            {
                PriceEpisodeMatches.Add(new PriceEpisodeMatchEntity
                {
                    CommitmentId           = commitment.CommitmentId,
                    IsSuccess              = payable,
                    LearnRefNumber         = earning.LearnRefNumber,
                    PriceEpisodeIdentifier = earning.PriceEpisodeIdentifier,
                    Ukprn        = earning.Ukprn,
                    AimSeqNumber = earning.AimSeqNumber,
                });
            }
        }