Esempio n. 1
0
        public ActionResult GenerateSkus(ProductSkuGeneratorViewModel model)
        {
            var options = db.Options.Where(o => model.OptionIds.Any(id => o.Id == id) &&
                                           o.Category.Type != OptionCategoryType.Text).ToArray();
            var categoryIds = options.Select(o => o.OptionCategoryId).Distinct().ToArray();

            var optionsPerm = OptionIdsPermutation(categoryIds, options);
            var skus        = model.Skus;

            foreach (var optList in optionsPerm)
            {
                var sku = new ProductSkuEditViewModel();
                sku.Sku       = (model.Prefix.AsNullIfEmpty() ?? "SKU-") + MiscHelpers.RandomText(8);
                sku.OptionIds = JsonConvert.SerializeObject(optList);

                if (skus.Any(s => s.OptionIds == sku.OptionIds))
                {
                    continue;
                }

                sku.Options = JsonConvert.SerializeObject(optList.Select(
                                                              id => new
                {
                    category = db.Options.First(o => o.Id == id).Category.Name,
                    option   = db.Options.First(o => o.Id == id).Name
                }));
                skus.Add(sku);
            }

            return(JsonSuccess(skus));
        }
Esempio n. 2
0
        public ActionResult GenerateSkus(ProductSkuGeneratorViewModel model)
        {
            var options = db.Options.Where(o => model.OptionIds.Any(id => o.Id == id)
                                                && o.Category.Type != OptionCategoryType.Text).ToArray();
            var categoryIds = options.Select(o => o.OptionCategoryId).Distinct().ToArray();

            var optionsPerm = OptionIdsPermutation(categoryIds, options);
            var skus = model.Skus;
            foreach (var optList in optionsPerm)
            {
                var sku = new ProductSkuEditViewModel();
                sku.Sku = (model.Prefix.AsNullIfEmpty() ?? "SKU-") + MiscHelpers.RandomText(8);
                sku.OptionIds = JsonConvert.SerializeObject(optList);

                if (skus.Any(s => s.OptionIds == sku.OptionIds))
                    continue;

                sku.Options = JsonConvert.SerializeObject(optList.Select(
                    id => new
                          {
                              category = db.Options.First(o => o.Id == id).Category.Name,
                              option = db.Options.First(o => o.Id == id).Name
                          }));
                skus.Add(sku);
            }

            return JsonSuccess(skus);
        }