Exemple #1
0
        public void Edit(int productId, ProductCodeItem model)
        {
            var product = _productRepository.Get(productId);

            if (product == null)
            {
                return;
            }

            var customerData = product.CustomerProductData.FirstOrDefault(x => x.CustomerId == model.Id);

            if (customerData != null)
            {
                customerData.ProductCode        = model.ProductCode;
                customerData.ProductDescription = model.ProductDescription;
                customerData.Gtin          = model.Gtin;
                customerData.PricePerPound = model.PricePerPound;
            }
            else
            {
                _customerProductDataRepository.Add(new CustomerProductData
                {
                    CustomerId         = model.Id,
                    ProductId          = productId,
                    ProductCode        = model.ProductCode,
                    ProductDescription = model.ProductDescription,
                    Gtin          = model.Gtin,
                    PricePerPound = model.PricePerPound
                });
            }

            _customerProductDataRepository.Save();
        }
        public ActionResult Products_Update([DataSourceRequest] DataSourceRequest request,
                                            [Bind(Prefix = "models")] IEnumerable <CustomerProductItem> products)
        {
            foreach (var product in products)
            {
                var customerId = SessionService.Get().CustomerViewModel.Id;

                var cpd =
                    _customerProductDataRepository.GetByProduct(product.Id)
                    .SingleOrDefault(p => p.CustomerId == customerId);

                if (product.IsSelected)
                {
                    var target = new CustomerProductData
                    {
                        CustomerId         = customerId,
                        ProductId          = product.Id,
                        ProductCode        = product.ProductCode,
                        ProductDescription = product.ProductDescription,
                        Gtin          = product.Gtin,
                        PricePerPound = product.PricePerPound,
                        BoxQuantity   = product.BoxQuantity,
                        PieceQuantity = product.PieceQuantity,
                        BagSizeId     = product.BagSize != null ? product.BagSize.Id : (int?)null,
                        BoxSizeId     = product.BoxSize != null ? product.BoxSize.Id : (int?)null
                    };
                    if (cpd != null)
                    {
                        _customerProductDataRepository.Remove(cpd);
                    }
                    _customerProductDataRepository.Add(target);
                }
                else
                {
                    _customerProductDataRepository.Remove(cpd);
                }
                _customerProductDataRepository.Save();
            }
            return(Json(products.ToDataSourceResult(request)));
        }