Esempio n. 1
0
        public IEnumerable <ValidationResult> Validate(ValidationContext validationContext)
        {
            if (Sales != null && Sales.Any())
            {
                var percentOfAllAreas       = Sales.Where(t => t.AreaPercent.HasValue).Sum(t => t.AreaPercent);
                var percentOfAllDepartments = Sales.Where(t => t.DepartmentPercent.HasValue).Sum(t => t.DepartmentPercent);

                if (Sales.Where(t => t.AreaId.HasValue && t.AreaId > 0).GroupBy(t => t.AreaId).Any(t => t.Count() > 1))
                {
                    yield return(new ValidationResult("You can not add the same areas."));
                }

                if (Sales.Where(t => t.DepartmentId.HasValue && t.DepartmentId > 0).GroupBy(t => t.DepartmentId).Any(t => t.Count() > 1))
                {
                    yield return(new ValidationResult("You can not add the same departments."));
                }

                if (percentOfAllAreas > 0 && percentOfAllAreas != 100)
                {
                    yield return(new ValidationResult("A sum of percents for all areas must be equal to 100."));
                }

                if (percentOfAllDepartments > 0 && percentOfAllDepartments != 100)
                {
                    yield return(new ValidationResult("A sum of percents for all departments must be equal to 100."));
                }

                if (Sales.Any(t => t.AreaId.HasValue && !t.AreaPercent.HasValue))
                {
                    yield return(new ValidationResult("You can not save area without percents."));
                }

                if (Sales.Any(t => t.DepartmentId.HasValue && !t.DepartmentPercent.HasValue))
                {
                    yield return(new ValidationResult("You can not save department without percents."));
                }

                if (Sales.Any(t => !t.AreaId.HasValue && t.AreaPercent.HasValue))
                {
                    yield return(new ValidationResult("You can not save percentage without areas."));
                }

                if (Sales.Any(t => !t.DepartmentId.HasValue && t.DepartmentPercent.HasValue))
                {
                    yield return(new ValidationResult("You can not save percentage without departments."));
                }
            }
        }