/// <summary> /// Validates this instance. Special validation case: item/quantity combination should be unique. /// </summary> /// <returns></returns> public bool Validate() { bool result; InnerItem.Validate(); if (InnerItem.List <= 0) { InnerItem.SetError("List", "List Price must be greater than 0".Localize(), true); } else { InnerItem.ClearError("List"); } // validate duplicates in preloaded prices var preloadedPrices = (ObservableCollection <Price>)_preloadedItems; result = !preloadedPrices.Any(x => x.PriceId != InnerItem.PriceId && x.ItemId == InnerItem.ItemId && x.MinQuantity == InnerItem.MinQuantity); if (result) { using (var repository = _repositoryFactory.GetRepositoryInstance()) { // query for price duplicate in repository var priceFromRepository = repository.Prices.Where(x => x.PricelistId == InnerItem.PricelistId && x.PriceId != InnerItem.PriceId && x.ItemId == InnerItem.ItemId && x.MinQuantity == InnerItem.MinQuantity).FirstOrDefault(); if (priceFromRepository != null) { // check if duplicate price in repository was updated locally var localPriceExistsAndItsValid = preloadedPrices.Any(x => x.PriceId == priceFromRepository.PriceId); if (!localPriceExistsAndItsValid) { // duplicate price exists in repository but not locally result = false; } } } } if (result) { InnerItem.ClearError("MinQuantity"); } else { InnerItem.SetError("MinQuantity", "This pricelist already contains this item with the same quantity".Localize(), true); } result = InnerItem.Errors.Count == 0; return(result); }