public ActionResult RemoveOne(int id)
        {
            if (!_orchardServices.Authorizer.Authorize(CommercePermissions.ManageProducts, null, T("Not authorized to manage products")))
            {
                return(new HttpUnauthorizedResult());
            }

            var product = _contentManager.Get <ProductPart>(id);

            _productInventoryService.UpdateInventory(product, -1);
            Dictionary <string, int> newInventory;
            IBundleService           bundleService;

            if (_wca.GetContext().TryResolve(out bundleService))
            {
                var affectedBundles = _contentManager.Query <BundlePart, BundlePartRecord>()
                                      .Where(b => b.Products.Any(p => p.ContentItemRecord.Id == product.Id))
                                      .WithQueryHints(new QueryHints().ExpandParts <ProductPart>())
                                      .List();
                newInventory = affectedBundles.ToDictionary(
                    b => b.As <ProductPart>().Sku,
                    b => bundleService.GetProductQuantitiesFor(b).Min(p => _productInventoryService.GetInventory(p.Product) / p.Quantity));
            }
            else
            {
                newInventory = new Dictionary <string, int>(1);
            }
            newInventory.Add(product.Sku, _productInventoryService.GetInventory(product));
            return(new JsonResult {
                Data = newInventory
            });
        }
Ejemplo n.º 2
0
        public ActionResult RemoveOne(int id)
        {
            var bundle   = _contentManager.Get <BundlePart>(id);
            var products = _bundleService.GetProductQuantitiesFor(bundle).ToList();

            foreach (var productPartQuantity in products)
            {
                //These calls will also update the inventory for the bundle
                _productInventoryService.UpdateInventory(productPartQuantity.Product, -productPartQuantity.Quantity);
            }
            var newInventory = products.ToDictionary(p => p.Product.Sku, p => _productInventoryService.GetInventory(p.Product));

            newInventory.Add(bundle.As <ProductPart>().Sku, products.Min(p => _productInventoryService.GetInventory(p.Product) / p.Quantity));
            return(new JsonResult {
                Data = newInventory
            });
        }