public CategoryRelationLinkWrapper(CategoryRelationLink link, double percentage, double maxPercentage, ObservableCollection <CategoryRelationLinkWrapper> wrappers, ObservableCollection <CategoryRelationLink> links) { _percentage = percentage; Link = link; _links = links; _wrappers = wrappers; _selectedCategory = link.Category; if (_selectedCategory == null) { CanBeDeleted = false; } else { CanBeDeleted = true; } DeleteCommand = new RelayCommand((obj) => { _links.Remove(link); wrappers.Remove(this); CalculateNewPercentages(true); CalculateCategories(true); }); }
private void ResetCategoriesLinkPercentages(Relation entity) { double totalPercentage = entity.CategoryLinks.Sum(b => b.Percentage); if (totalPercentage < 100) { CategoryRelationLink categoryTransactionLink = new CategoryRelationLink(); categoryTransactionLink.Percentage = 100 - totalPercentage; categoryTransactionLink.Relation = entity; categoryTransactionLink.RelationID = entity.ID; categoryTransactionLink.UserId = ServiceResolver.GetService <IUserProvider>().GetUserId(); entity.CategoryLinks.Add(categoryTransactionLink); } else if (totalPercentage > 100) { double hasToBeLess = totalPercentage - 100; var link = entity.CategoryLinks.Last(); while (link != null && entity.CategoryLinks.Sum(b => b.Percentage) > 100) { if (hasToBeLess > link.Percentage) { hasToBeLess -= link.Percentage; link.Percentage = 0; } else { link.Percentage -= hasToBeLess; } link = entity.CategoryLinks.Skip(entity.CategoryLinks.IndexOf(link) - 1).FirstOrDefault(); } } }
private void InitializeLinkWrapperIfNonExists() { if (LinkWrappers.Count == 0) { Relation relation = EditingItem.Result; CategoryRelationLink link = new CategoryRelationLink(); link.RelationID = relation.ID; link.Relation = relation; link.Category = null; link.CategoryID = null; LinkWrappers.Add(new CategoryRelationLinkWrapper(link: link, percentage: 100, maxPercentage: 100, wrappers: LinkWrappers, links: EditingItem.Result.CategoryLinks)); } }
private void AddCategoryToRelation(object obj) { Relation relation = EditingItem.Result as Relation; CategoryRelationLink link = new CategoryRelationLink(); link.RelationID = relation.ID; link.Relation = relation; link.CategoryID = SelectedCategory.ID; link.Category = SelectedCategory; relation.CategoryLinks.Add(link); LinkWrappers.Add(new CategoryRelationLinkWrapper(link: link, percentage: 0, maxPercentage: 100 - EditingItem.Result.CategoryLinks.Where(c => c != link).Sum(c => c.Percentage), wrappers: LinkWrappers, links: relation.CategoryLinks)); }
private void CalculateCategories(bool dontIgnoreSelf = false) { if (_wrappers.Where(b => b.SelectedCategory == null).Count() == 0) { Relation relation = Link.Relation; CategoryRelationLink link = new CategoryRelationLink(); link.RelationID = Link.RelationID; link.Relation = relation; link.Category = null; link.CategoryID = null; link.UserId = ServiceResolver.GetService <IUserProvider>().GetUserId(); _links.Add(link); _wrappers.Add(new CategoryRelationLinkWrapper(link: link, percentage: 0, maxPercentage: 100 - _links.Where(c => c != link || dontIgnoreSelf).Sum(c => c.Percentage), wrappers: _wrappers, links: _links)); } }