private bool HasRestrictions(Coupon coupon, TicketItem ticketItem) { Item item = Item.Get(ticketItem.ItemId); // Coupon Category Restrictions List <CouponCategory> cats = new List <CouponCategory>(CouponCategory.GetAll(coupon.Id)); if ((cats.Count > 0) && coupon.IsExclusive && CouponCategory.FindByCategoryId(cats, item.CategoryId) == null) { return(true); } if ((cats.Count > 0) && !coupon.IsExclusive && CouponCategory.FindByCategoryId(cats, item.CategoryId) != null) { return(true); } // Coupon Item Restrictions List <CouponItem> items = new List <CouponItem>(CouponItem.GetAll(coupon.Id)); if ((items.Count > 0) && coupon.IsExclusive && CouponItem.FindByItemId(items, item.Id) == null) { return(true); } if ((items.Count > 0) && !coupon.IsExclusive && CouponItem.FindByItemId(items, item.Id) != null) { return(true); } return(false); }
private void UpdateCouponItems() { int[] currentItemIds = GetItemIds(); if (EqualIdCollections(currentItemIds, _pendingItemIds)) { return; } if (currentItemIds != null) { List <CouponItem> couponItems = new List <CouponItem>(CouponItem.GetAll(ActiveCoupon.Id)); // Remove any in current, that doesn't exist in pending foreach (CouponItem existing in from id in currentItemIds where _pendingItemIds == null || !_pendingItemIds.Any(testId => testId == id) select CouponItem.FindByItemId(couponItems, id)) { CouponItem.Delete(existing.Id); } } // Add any missing pending to the current if (_pendingItemIds != null) { foreach (int id in _pendingItemIds .Where(id => !(currentItemIds != null && currentItemIds.Any(testId => testId == id)))) { // Add a new CouponCategory for this Coupon CouponItem.Add(ActiveCoupon.Id, id); } } }
private void UpdateCoupon() { if (editorControl.UpdateCoupon()) { SetEditMode(false); if (listBox1.SelectedIndex >= 0) { var item = (FormattedListBoxItem)listBox1.SelectedItem; item.Set(editorControl.ActiveCoupon, editorControl.ActiveCoupon.Description); } else { var listItem = new FormattedListBoxItem( editorControl.ActiveCoupon, editorControl.ActiveCoupon.Description, true); listBox1.Items.Add(listItem); listBox1.SelectedItem = listItem; listBox1.ScrollIntoView(listItem); buttonDelete.IsEnabled = true; } } if (editorControl.ActiveCoupon != null) { var couponCategories = new List <CouponCategory>(CouponCategory.GetAll(editorControl.ActiveCoupon.Id)); foreach (int id in editorControl.couponCategorySelectionControl.SelectedCategoryIds .Where(id => CouponCategory.FindByCategoryId(couponCategories, id) == null)) { CouponCategory.Add(editorControl.ActiveCoupon.Id, id); } foreach (CouponCategory category in couponCategories .Where(category => !editorControl.couponCategorySelectionControl.SelectedCategoryIds .Contains(category.CategoryId))) { CouponCategory.Delete(editorControl.ActiveCoupon.Id, category.CategoryId); } var couponItems = new List <CouponItem>(CouponItem.GetAll(editorControl.ActiveCoupon.Id)); foreach (int id in editorControl.couponItemSelectionControl.SelectedItemIds .Where(id => CouponItem.FindByItemId(couponItems, id) == null)) { CouponItem.Add(editorControl.ActiveCoupon.Id, id); } foreach (CouponItem item in couponItems .Where(item => !editorControl.couponCategorySelectionControl.SelectedCategoryIds .Contains(item.ItemId))) { CouponItem.Delete(editorControl.ActiveCoupon.Id, item.ItemId); } } }
private int[] GetItemIds() { if (ActiveCoupon == null) { return(null); } List <int> selected = CouponItem.GetAll(ActiveCoupon.Id) .Select(item => item.ItemId).ToList(); if (selected.Count == 0) { return(null); } selected.Sort(); return(selected.ToArray()); }
private void InitializeAvailableCoupons() { List <Coupon> allCoupons = new List <Coupon>(Coupon.GetAll()); List <CouponCategory> allCategories = new List <CouponCategory>(CouponCategory.GetAll()); List <CouponItem> allItems = new List <CouponItem>(CouponItem.GetAll()); listBoxAvailable.Items.Clear(); foreach (Coupon coupon in allCoupons) { if (!coupon.IsActive) { continue; } List <CouponCategory> couponCategories = new List <CouponCategory>(CouponCategory.FindAllByCouponId(allCategories, coupon.Id)); List <CouponItem> couponItems = new List <CouponItem>(CouponItem.FindAllByCouponId(allItems, coupon.Id)); // Check for applied coupons and exclude them if ((from FormattedListBoxItem item in listBoxApplied.Items select(Coupon) item.ReferenceObject) .Any(appliedCoupon => appliedCoupon.Id == coupon.Id)) { continue; } // Add applicable coupons bool add = false; if (coupon.ApplyToAll) { add = true; } else if ((SelectedTicketItem == null) && (couponItems.Count == 0) && (couponCategories.Count == 0)) { add = true; } else if (SelectedTicketItem != null) { Category category = Category.Get(SelectedItem.CategoryId); if (coupon.IsExclusive) { HasRestrictions(coupon, SelectedTicketItem); if (couponItems.Any(couponItem => SelectedTicketItem.ItemId == couponItem.ItemId)) { add = true; } if (couponCategories.Any(couponCategory => category.Id == couponCategory.CategoryId)) { add = true; } } else { bool found = couponItems.Any(couponItem => SelectedTicketItem.ItemId == couponItem.ItemId) || couponCategories.Any(couponCategory => category.Id == couponCategory.CategoryId); if (!found) { add = true; } } } if (add) { AddAvailable(coupon); } } }
private int[] GetItemIds(Coupon value) { return(CouponItem.GetAll(value.Id).Select(item => item.ItemId).ToArray()); }