public EditProjectFundingSourceBudgetViewModel(ProjectFirmaModels.Models.Project project,
                                                       List <ProjectFirmaModels.Models.ProjectFundingSourceBudget> projectFundingSourceBudgets)
        {
            var fundingTypeID = project.FundingTypeID ?? 0;

            NoFundingSourceIdentifiedYet = project.ProjectNoFundingSourceIdentifieds.FirstOrDefault()?.NoFundingSourceIdentifiedYet;
            ViewModelForAngular          = new ViewModelForAngularEditor(fundingTypeID, projectFundingSourceBudgets, NoFundingSourceIdentifiedYet);
        }
        public ExpectedFundingViewModel(ProjectFirmaModels.Models.Project project)
        {
            NoFundingSourceIdentifiedYet = project.ProjectNoFundingSourceIdentifieds.FirstOrDefault()?.NoFundingSourceIdentifiedYet;
            var projectFundingSourceBudgets = project.ProjectFundingSourceBudgets.ToList();

            ViewModelForAngular = new ViewModelForAngularEditor(project.FundingTypeID ?? 0, projectFundingSourceBudgets, NoFundingSourceIdentifiedYet);
            Comments            = project.BudgetComment;
        }
 public ExpectedFundingViewModel(ProjectUpdateBatch projectUpdateBatch, List <ProjectFundingSourceBudgetUpdate> projectFundingSourceBudgetUpdates,
                                 string comments)
 {
     NoFundingSourceIdentifiedYet = projectUpdateBatch.ProjectNoFundingSourceIdentifiedUpdates.FirstOrDefault()?.NoFundingSourceIdentifiedYet;
     Comments = comments;
     ExpectedFundingUpdateNote = projectUpdateBatch.ExpectedFundingUpdateNote;
     ViewModelForAngular       = new ViewModelForAngularEditor(projectUpdateBatch.ProjectUpdate.FundingTypeID ?? 0, projectFundingSourceBudgetUpdates, NoFundingSourceIdentifiedYet);
 }
        public IEnumerable <ValidationResult> Validate(ValidationContext validationContext)
        {
            var validationResults = new List <ValidationResult>();

            if (ViewModelForAngular.FundingTypeID == 0)
            {
                validationResults.Add(new ValidationResult($"You must answer the question \"Does the {FieldDefinitionEnum.Project.ToType().GetFieldDefinitionLabel()} budget vary by year or is it the same?\""));
            }

            // ViewModelForAngular will be null if no ProjectFundingSourceBudgets are entered, recreate it so model will be valid when returning with validation error
            ViewModelForAngular = ViewModelForAngular ?? new ViewModelForAngularEditor(0, new List <ProjectFirmaModels.Models.ProjectFundingSourceBudget>(), 0);

            if (ViewModelForAngular.FundingTypeID != 0 && ViewModelForAngular.NoFundingSourceIdentifiedYet == null)
            {
                validationResults.Add(new ValidationResult($"{FieldDefinitionEnum.NoFundingSourceIdentified.ToType().GetFieldDefinitionLabel()} cannot be blank. If the amount is unknown, you can enter zero."));
            }

            if (ViewModelForAngular.ProjectFundingSourceBudgets == null)
            {
                // set to empty list so model will be valid when returning with validation error
                ViewModelForAngular.ProjectFundingSourceBudgets = new List <ProjectFundingSourceBudgetSimple>();
                return(validationResults);
            }

            if (ViewModelForAngular.ProjectFundingSourceBudgets.GroupBy(x => x.FundingSourceID).Any(x => x.Count() > 1))
            {
                validationResults.Add(new ValidationResult($"Each {FieldDefinitionEnum.FundingSource.ToType().GetFieldDefinitionLabel()} can only be used once."));
            }

            foreach (var projectFundingSourceBudget in ViewModelForAngular.ProjectFundingSourceBudgets)
            {
                if (projectFundingSourceBudget.AnyValueIsNull())
                {
                    var fundingSource =
                        HttpRequestStorage.DatabaseEntities.FundingSources.Single(x =>
                                                                                  x.FundingSourceID == projectFundingSourceBudget.FundingSourceID);
                    validationResults.Add(new ValidationResult(
                                              $"{FieldDefinitionEnum.SecuredFunding.ToType().GetFieldDefinitionLabel()} and {FieldDefinitionEnum.TargetedFunding.ToType().GetFieldDefinitionLabel()} must both have values for {FieldDefinitionEnum.FundingSource.ToType().GetFieldDefinitionLabel()}: {fundingSource.GetDisplayName()}. If the amount of Secured or {FieldDefinitionEnum.TargetedFunding.ToType().GetFieldDefinitionLabel()} is unknown, you can enter zero."));
                }
            }
            return(validationResults);
        }