private void ValidateChildrenRelationships() { if (ChildProjects.Count() > 0) { if (Entity.BeginDate.HasValue && ChildProjects.Count(p => p.BeginDate.HasValue) > 0) { DateTime minBeginDate = ChildProjects.Where(p => p.BeginDate.HasValue).Min(p => p.BeginDate.Value); if (Entity.BeginDate.Value > minBeginDate) { Recipient.SetError(nameof(Entity.BeginDate), $"Дата открытия проекта не может быть позже, чем самая ранняя дата открытия дочерних проектов ({minBeginDate.ToShortDateString()})"); } } if (Entity.EndDate.HasValue && ChildProjects.Count(p => p.EndDate.HasValue) > 0) { DateTime maxEndDate = ChildProjects.Where(p => p.EndDate.HasValue).Max(p => p.EndDate.Value); if (Entity.EndDate.Value < maxEndDate) { Recipient.SetError(nameof(Entity.EndDate), $"Дата закрытия проекта не может быть раньше, чем самая поздня дата закрытия дочерних проектов ({maxEndDate.ToShortDateString()})"); } } ValidateChildrenContractAmount(); ValidateChildrenSubcontractorsAmountBudget(); ValidateChildrenOrganisationAmountBudget(); ValidateChildrenEmployeePayrollBudget(); ValidateChildrenOtherCostsBudget(); } }
private void ValidateChildrenBudgetPropertyAmount(string propertyName, string errorMessageTemplate) { LoadRelationships(); var hasBudgetValue = GetBudgetPropertyHasValueFunc(propertyName); if (hasBudgetValue(Entity)) { var conditionExpression = GetBudgetPropertyHasValueExpression(propertyName); var valueExpression = GetBudgetPropertyValueExpression(propertyName); var filtered = ChildProjects.Where(conditionExpression).ToList(); decimal total = filtered.AsQueryable().Sum(valueExpression); var valueFunc = valueExpression.Compile(); if (total > valueFunc(Entity)) { Recipient.SetError(propertyName, string.Format(errorMessageTemplate, total)); } } }