Пример #1
0
        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 UpdateCouponCategories()
        {
            int[] currentCategoryIds = GetCategoryIds();
            if (EqualIdCollections(currentCategoryIds, _pendingCategoryIds))
            {
                return;
            }
            if (currentCategoryIds != null)
            {
                List <CouponCategory> couponCategories = new List <CouponCategory>(CouponCategory.GetAll(ActiveCoupon.Id));

                // Remove any in current, that doesn't exist in pending
                foreach (CouponCategory existing in
                         from id in currentCategoryIds
                         where _pendingCategoryIds == null || !_pendingCategoryIds.Any(testId => testId == id)
                         select CouponCategory.FindByCategoryId(couponCategories, id))
                {
                    CouponCategory.Delete(existing.Id);
                }
            }

            // Add any missing pending to the current
            if (_pendingCategoryIds == null)
            {
                return;
            }
            foreach (int id in _pendingCategoryIds
                     .Where(id => !(currentCategoryIds != null && currentCategoryIds.Any(testId => testId == id))))
            {
                // Add a new CouponCategory for this Coupon
                CouponCategory.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[] GetCategoryIds()
        {
            if (ActiveCoupon == null)
            {
                return(null);
            }
            IEnumerable <CouponCategory> categories = CouponCategory.GetAll(ActiveCoupon.Id);
            List <int> selected = categories.Select(category => category.CategoryId).ToList();

            if (selected.Count == 0)
            {
                return(null);
            }
            selected.Sort();
            return(selected.ToArray());
        }
Пример #5
0
        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);
                }
            }
        }
Пример #6
0
 private int[] GetCategoryIds(Coupon value)
 {
     return(CouponCategory.GetAll(value.Id).Select(item => item.CategoryId).ToArray());
 }