private void btnProducts_Click(object sender, EventArgs e)
        {
            ProductForm productForm = new ProductForm();

            productForm.Show();
            Hide();
        }
        private void AddOrDeleteCategories(ProductForm model, Product product)
        {
            foreach (var categoryId in model.Product.CategoryIds)
            {
                if (product.Categories.Any(x => x.CategoryId == categoryId))
                {
                    continue;
                }

                var productCategory = new ProductCategory
                {
                    CategoryId = categoryId
                };
                product.AddCategory(productCategory);
            }

            var deletedProductCategories =
                product.Categories.Where(productCategory => !model.Product.CategoryIds.Contains(productCategory.CategoryId))
                .ToList();

            foreach (var deletedProductCategory in deletedProductCategories)
            {
                deletedProductCategory.Product = null;
                product.Categories.Remove(deletedProductCategory);
                _productCategoryRepository.Remove(deletedProductCategory);
            }
        }
        private static void MapProductVariationVmToProduct(ProductForm model, Product product)
        {
            foreach (var variationVm in model.Product.Variations)
            {
                var productLink = new ProductLink
                {
                    LinkType      = ProductLinkType.Super,
                    Product       = product,
                    LinkedProduct = product.Clone()
                };

                productLink.LinkedProduct.Name                  = variationVm.Name;
                productLink.LinkedProduct.SeoTitle              = StringHelper.ToUrlFriendly(variationVm.Name);
                productLink.LinkedProduct.Price                 = variationVm.Price;
                productLink.LinkedProduct.OldPrice              = variationVm.OldPrice;
                productLink.LinkedProduct.NormalizedName        = variationVm.NormalizedName;
                productLink.LinkedProduct.HasOptions            = false;
                productLink.LinkedProduct.IsVisibleIndividually = false;

                foreach (var combinationVm in variationVm.OptionCombinations)
                {
                    productLink.LinkedProduct.AddOptionCombination(new ProductOptionCombination
                    {
                        OptionId  = combinationVm.OptionId,
                        Value     = combinationVm.Value,
                        SortIndex = combinationVm.SortIndex
                    });
                }

                productLink.LinkedProduct.ThumbnailImage = product.ThumbnailImage;

                product.AddProductLinks(productLink);
            }
        }
        public IActionResult Create(ProductForm model)
        {
            if (!ModelState.IsValid)
            {
                return(new BadRequestObjectResult(ModelState));
            }

            var product = new Product
            {
                Name             = model.Product.Name,
                SeoTitle         = StringHelper.ToUrlFriendly(model.Product.Name),
                ShortDescription = model.Product.ShortDescription,
                Description      = model.Product.Description,
                Specification    = model.Product.Specification,
                Price            = model.Product.Price,
                OldPrice         = model.Product.OldPrice,
                IsPublished      = model.Product.IsPublished,
                BrandId          = model.Product.BrandId
            };

            foreach (var option in model.Product.Options)
            {
                foreach (var value in option.Values)
                {
                    product.AddOptionValue(new ProductOptionValue
                    {
                        Value    = value,
                        OptionId = option.Id
                    });
                }
            }

            MapProductVariationVmToProduct(model, product);

            foreach (var attribute in model.Product.Attributes)
            {
                var attributeValue = new ProductAttributeValue
                {
                    AttributeId = attribute.Id,
                    Value       = attribute.Value
                };

                product.AddAttributeValue(attributeValue);
            }

            foreach (var categoryId in model.Product.CategoryIds)
            {
                var productCategory = new ProductCategory
                {
                    CategoryId = categoryId
                };
                product.AddCategory(productCategory);
            }

            SaveProductImages(model, product);

            productService.Create(product);

            return(Ok());
        }
Beispiel #5
0
        private static void MapProductLinkVmToProduct(ProductForm model, Product product)
        {
            foreach (var relatedProductVm in model.Product.RelatedProducts)
            {
                var productLink = new ProductLink
                {
                    LinkType        = ProductLinkType.Related,
                    Product         = product,
                    LinkedProductId = relatedProductVm.Id
                };

                product.AddProductLinks(productLink);
            }

            foreach (var crossSellProductVm in model.Product.CrossSellProducts)
            {
                var productLink = new ProductLink
                {
                    LinkType        = ProductLinkType.CrossSell,
                    Product         = product,
                    LinkedProductId = crossSellProductVm.Id
                };

                product.AddProductLinks(productLink);
            }
        }
        public IActionResult Edit(long id, ProductForm model)
        {
            if (!ModelState.IsValid)
            {
                return(new BadRequestObjectResult(ModelState));
            }

            var product = productRepository.Get(id);

            product.Name             = model.Product.Name;
            product.ShortDescription = model.Product.ShortDescription;
            product.Description      = model.Product.Description;
            product.Specification    = model.Product.Specification;
            product.Price            = model.Product.Price;
            product.OldPrice         = model.Product.OldPrice;
            product.BrandId          = model.Product.BrandId;

            SaveProductImages(model, product);

            foreach (var productMediaId in model.Product.DeletedMediaIds)
            {
                var productMedia = product.Medias.First(x => x.Id == productMediaId);
                mediaService.DeleteMedia(productMedia.Media);
                product.RemoveMedia(productMedia);
            }

            AddOrDeleteCategories(model, product);

            productService.Update(product);

            return(Ok());
        }
        public IActionResult Create(ProductForm model)
        {
            if (!ModelState.IsValid)
            {
                return(new BadRequestObjectResult(ModelState));
            }

            var product = new Product
            {
                Name             = model.Product.Name,
                SeoTitle         = StringHelper.ToUrlFriendly(model.Product.Name),
                ShortDescription = model.Product.ShortDescription,
                Description      = model.Product.Description,
                Specification    = model.Product.Specification,
                Price            = model.Product.Price,
                OldPrice         = model.Product.OldPrice,
                IsPublished      = model.Product.IsPublished,
                BrandId          = model.Product.BrandId
            };

            foreach (var categoryId in model.Product.CategoryIds)
            {
                var productCategory = new ProductCategory
                {
                    CategoryId = categoryId
                };
                product.AddCategory(productCategory);
            }

            SaveProductImages(model, product);

            productService.Create(product);

            return(Ok());
        }
        // Due to some issue with EF Core, we have to use _productLinkRepository in this case.
        private void AddOrDeleteProductRelation(ProductForm model, Product product)
        {
            foreach (var productRelationVm in model.Product.RelatedProducts)
            {
                var productLink = product.ProductLinks.Where(x => x.LinkType == ProductLinkType.Relation).FirstOrDefault(x => x.LinkedProductId == productRelationVm.Id);
                if (productLink == null)
                {
                    productLink = new ProductLink
                    {
                        LinkType        = ProductLinkType.Relation,
                        Product         = product,
                        LinkedProductId = productRelationVm.Id,
                    };

                    _productLinkRepository.Add(productLink);
                }
            }

            foreach (var productLink in product.ProductLinks.Where(x => x.LinkType == ProductLinkType.Relation))
            {
                if (model.Product.RelatedProducts.All(x => x.Id != productLink.LinkedProductId))
                {
                    _productLinkRepository.Remove(productLink);
                }
            }
        }
Beispiel #9
0
 private void MapUploadedFile(ProductForm model)
 {
     // Currently model binder cannot map the collection of file productImages[0], productImages[1]
     foreach (var file in Request.Form.Files)
     {
         if (file.Name.Contains("productImages"))
         {
             model.ProductImages.Add(file);
         }
         else if (file.Name.Contains("productDocuments"))
         {
             model.ProductDocuments.Add(file);
         }
         else if (file.Name.Contains("product[variations]"))
         {
             var key          = file.Name.Replace("product", "");
             var keyParts     = key.Split(new char[] { '[', ']' }, StringSplitOptions.RemoveEmptyEntries);
             var variantIndex = int.Parse(keyParts[1]);
             if (key.Contains("newImages"))
             {
                 model.Product.Variations[variantIndex].NewImages.Add(file);
             }
             else
             {
                 model.Product.Variations[variantIndex].ThumbnailImage = file;
             }
         }
     }
 }
Beispiel #10
0
 protected void LoadRelatedFormData(ProductForm model)
 {
     model.Stores           = _storeService.Query.Where(s => s.Enabled).OrderBy(s => s.Name).Select(s => new SelectListItem(s.Name, s.Id.ToString())).ToList();
     model.Vendors          = _vendorService.Query.Where(v => v.Enabled).OrderBy(v => v.Name).Select(v => new SelectListItem(v.Name, v.Id.ToString())).ToList();
     model.Countries        = _countryService.Query.Where(c => c.Enabled).OrderBy(c => c.Name).Select(c => new SelectListItem(c.Name, c.Id.ToString())).ToList();
     model.GoogleCategories = _categoryService.Query.Where(g => g.Enabled).OrderBy(g => g.Name).Select(g => new SelectListItem(g.Name, g.Id.ToString())).ToList();
 }
        private void AddOrDeleteProductOption(ProductForm model, Product product)
        {
            var optionIndex = 0;

            foreach (var optionVm in model.Product.Options)
            {
                var optionValue = product.OptionValues.FirstOrDefault(x => x.OptionId == optionVm.Id);
                if (optionValue == null)
                {
                    product.AddOptionValue(new ProductOptionValue
                    {
                        OptionId  = optionVm.Id,
                        Value     = JsonConvert.SerializeObject(optionVm.Values),
                        SortIndex = optionIndex
                    });
                }
                else
                {
                    optionValue.Value     = JsonConvert.SerializeObject(optionVm.Values);
                    optionValue.SortIndex = optionIndex;
                }

                optionIndex++;
            }

            var deletedProductOptionValues = product.OptionValues.Where(x => model.Product.Options.All(vm => vm.Id != x.OptionId)).ToList();

            foreach (var productOptionValue in deletedProductOptionValues)
            {
                product.OptionValues.Remove(productOptionValue);
                _productOptionValueRepository.Remove(productOptionValue);
            }
        }
        private void AddOrDeleteProductAttribute(ProductForm model, Product product)
        {
            foreach (var productAttributeVm in model.Product.Attributes)
            {
                var productAttrValue =
                    product.AttributeValues.FirstOrDefault(x => x.AttributeId == productAttributeVm.Id);
                if (productAttrValue == null)
                {
                    productAttrValue = new ProductAttributeValue
                    {
                        AttributeId = productAttributeVm.Id,
                        Value       = productAttributeVm.Value
                    };
                    product.AddAttributeValue(productAttrValue);
                }
                else
                {
                    productAttrValue.Value = productAttributeVm.Value;
                }
            }

            var deletedAttrValues =
                product.AttributeValues.Where(attrValue => model.Product.Attributes.All(x => x.Id != attrValue.AttributeId))
                .ToList();

            foreach (var deletedAttrValue in deletedAttrValues)
            {
                deletedAttrValue.Product = null;
                product.AttributeValues.Remove(deletedAttrValue);
                _productAttributeValueRepository.Remove(deletedAttrValue);
            }
        }
        public ActionResult SaveRecord(ProductForm model)
        {
            try
            {
                SalesLayanEntities14 db = new SalesLayanEntities14();
                Product prod            = new Product();

                prod.prod_name            = model.Prod_name;
                prod.prod_model           = model.Prod_model;
                prod.prod_quantity        = model.Prod_quantity;
                prod.prod_description     = model.Prod_description;
                prod.prod_unit_cost_price = model.Prod_unit_cost_price;
                prod.cat_id = model.Cat_id;


                db.Products.Add(prod);
                db.SaveChanges();
                int latestProdId = prod.prod_id;
                TempData["status"] = "Success";
            }

            catch (Exception ex)
            {
                throw ex;
            }
            return(RedirectToAction("Index"));
        }
Beispiel #14
0
        private void addToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ProductForm form = new ProductForm();

            form.ShowDialog();
            refreshDataGrid();
        }
        private async Task SaveProductMedias(ProductForm model, Product product)
        {
            if (model.ThumbnailImage != null)
            {
                var fileName = await SaveFile(model.ThumbnailImage);

                if (product.ThumbnailImage != null)
                {
                    product.ThumbnailImage.FileName = fileName;
                }
                else
                {
                    product.ThumbnailImage = new Media {
                        FileName = fileName
                    };
                }
            }

            // Currently model binder cannot map the collection of file productImages[0], productImages[1]
            foreach (var file in Request.Form.Files)
            {
                if (file.ContentDisposition.Contains("productImages"))
                {
                    model.ProductImages.Add(file);
                }
                else if (file.ContentDisposition.Contains("productDocuments"))
                {
                    model.ProductDocuments.Add(file);
                }
            }

            foreach (var file in model.ProductImages)
            {
                var fileName = await SaveFile(file);

                var productMedia = new ProductMedia
                {
                    Product = product,
                    Media   = new Media {
                        FileName = fileName, MediaType = MediaType.Image
                    }
                };
                product.AddMedia(productMedia);
            }

            foreach (var file in model.ProductDocuments)
            {
                var fileName = await SaveFile(file);

                var productMedia = new ProductMedia
                {
                    Product = product,
                    Media   = new Media {
                        FileName = fileName, MediaType = MediaType.File, Caption = file.FileName
                    }
                };
                product.AddMedia(productMedia);
            }
        }
Beispiel #16
0
        public ActionResult _ProductForm(ProductForm model, HttpPostedFileBase file)
        {
            if (ModelState.IsValid)
            {
                var body    = "Name: {0} <br /> Email: {1} <br /> Phone: {2} <br /> Company Name: {3} <br /> Website/Facebook: {4} <br /> Product: {5} <br /> Product Color: {6} <br /> Logo Color: {7}";
                var message = new SendGridMessage();
                //message.AddTo("*****@*****.**");
                /* message.AddTo("*****@*****.**");*/                         // replace with valid value
                message.AddTo("*****@*****.**");
                message.From    = new MailAddress("*****@*****.**"); // replace with valid value
                message.Subject = "Product Selection Form";
                message.Html    = string.Format(body, model.Name, model.Email, model.ProductPhone, model.CompanyName, model.WebsiteFacebook, model.Product, model.Color, model.LogoColor);

                try
                {
                    if (file.ContentLength > 0)
                    {
                        var fileName = Path.GetFileName(file.FileName);
                        var path     = Path.Combine(Server.MapPath("~/App_Data/Images"), fileName);
                        file.SaveAs(path);

                        message.AddAttachment(path);

                        //Azure credentials

                        var username = ConfigurationManager.AppSettings["sendGridUser"];
                        var pswd     = ConfigurationManager.AppSettings["sendGridPassword"];

                        // variable to store azure credentials
                        var credentials = new NetworkCredential(username, pswd);
                        // Create an Web transport for sending email.
                        var transportWeb = new Web(credentials);

                        // Send the email, which returns an awaitable task.
                        transportWeb.DeliverAsync(message);

                        ModelState.Clear(); //clears form when page reload

                        return(RedirectToAction("ProductFormConfirmation", "Home", new { model.Name, model.Email }));
                    }
                }

                catch
                {
                    var username = ConfigurationManager.AppSettings["sendGridUser"];
                    var pswd     = ConfigurationManager.AppSettings["sendGridPassword"];

                    var credentials  = new NetworkCredential(username, pswd);
                    var transportWeb = new Web(credentials);

                    transportWeb.DeliverAsync(message);

                    ModelState.Clear(); //clears form when page reload

                    return(RedirectToAction("ProductFormConfirmation", "Home", new { model.Name, model.Email }));
                }
            }
            return(View(model));
        }
Beispiel #17
0
        private void product_Click(object sender, EventArgs e)
        {
            ProductForm productForm = new ProductForm();

            LoadFormToPanel(productForm, productPanel);

            //productForm.ShowDialog();
        }
        private void btnPrint_Click(object sender, EventArgs e)
        {
            DataSet1.ProductDataTable dt = new DataSet1.ProductDataTable();
            dt = (DataSet1.ProductDataTable)SetProductInfo(prod);
            ProductForm rf = new ProductForm(dt);

            rf.Show();
        }
Beispiel #19
0
        public ActionResult DeleteConfirmed(Guid id)
        {
            ProductForm productForm = UnitOfWork.ProductFormRepository.GetById(id);

            UnitOfWork.ProductFormRepository.Delete(productForm);
            UnitOfWork.Save();
            return(RedirectToAction("Index"));
        }
Beispiel #20
0
 private void buttonGoProducts_Click(object sender, EventArgs e)
 {
     if (Application.OpenForms["ProductOperations"] == null)
     {
         Form ProductOperations = new ProductForm();
         ProductOperations.Show();
     }
 }
Beispiel #21
0
 private void editToolStripMenuItem_Click(object sender, EventArgs e)
 {
     if (dataGridProducts.SelectedRows.Count > 0)
     {
         ProductForm form = new ProductForm(list[dataGridProducts.SelectedRows[0].Index]);
         form.ShowDialog();
         refreshDataGrid();
     }
 }
Beispiel #22
0
        private void LinkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            var form = new ProductForm(_currentKit.Product);

            if (form.ShowDialog() == DialogResult.OK)
            {
                UpdateByProduct(_currentKit.Product);
            }
        }
Beispiel #23
0
        public string ConvertProducts()
        {
            DatabaseContext db       = new DatabaseContext();
            var             products = db.TempProducts;

            foreach (var product in products)
            {
                var productGroup = UnitOfWork.ProductGroupRepository.Get(c => c.Code == product.Product_Type_Code).FirstOrDefault();

                if (productGroup != null)
                {
                    ProductCreator productCreator = UnitOfWork.ProductCreatorRepository
                                                    .Get(c => c.Title == product.Maker).FirstOrDefault();

                    Guid?creatorId = null;
                    if (productCreator != null)
                    {
                        creatorId = productCreator.Id;
                    }

                    ProductForm productForm = UnitOfWork.ProductFormRepository.Get(c => c.Title == product.Type)
                                              .FirstOrDefault();

                    Guid?productFormId = null;
                    if (productForm != null)
                    {
                        productFormId = productForm.Id;
                    }

                    Product oProduct = new Product()
                    {
                        Id               = Guid.NewGuid(),
                        Title            = product.Product_Name,
                        ProductGroupId   = productGroup.Id,
                        Length           = product.Length,
                        Weight           = product.App_Weight,
                        IsPureWeight     = product.Net_weight,
                        Grade            = product.Grade,
                        Width            = product.Height,
                        Thickness        = product.Width,
                        ProductCreatorId = creatorId,
                        ProductFormId    = productFormId,
                        Other            = product.Other,
                        IsDeleted        = false,
                        IsActive         = true,
                        CreationDate     = DateTime.Now
                    };
                    UnitOfWork.ProductRepository.Insert(oProduct);
                    //db.Products.Add(oProduct);
                    //db.SaveChanges();
                    UnitOfWork.Save();
                }
            }
            UnitOfWork.Save();

            return("");
        }
Beispiel #24
0
        public ActionResult <Product> AddEdit([FromForm] ProductForm p, [FromForm] IFormFile img)
        {
            int     id = p.Id;
            Product returnValue;

            if (id == -1)
            {
                Product newProduct = new Product();
                newProduct.Id          = products.Count > 0 ? products[^ 1].Id + 1 : 1;
Beispiel #25
0
        public IActionResult Add()
        {
            var model = new ProductForm()
            {
                Categories = _repo.GetAllCategoriesId()
            };

            return(View(model));
        }
Beispiel #26
0
        public IActionResult NewProduct()
        {
            ProductForm PForm = new ProductForm()
            {
                AllUsers = _context.Users.ToList()
            };

            return(View(PForm));
        }
Beispiel #27
0
        //private void listView1_Click(object sender, EventArgs e)
        //{

        //}

        //private void listView1_Resize(object sender, EventArgs e)
        //{
        //    //listView1.Columns[1].Width = listView1.Width - 305;
        //}

        private void lbtAddnew_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            ProductForm frm = new ProductForm(new MasterProduct()
            {
                ProductType = DataRepository.GetProductTypeById(_proTypeId)
            }, MdiParentForm);

            frm.ShowDialog();
        }
Beispiel #28
0
        public ProductWindow(ProductViewModel product)
        {
            InitializeComponent();
            product.UpdateInitial();
            var ctx = new ProductForm(product);

            ctx.Window  = this;
            DataContext = ctx;
        }
Beispiel #29
0
        //create new register in products
        public async Task <Product> Add(ProductForm productForm)
        {
            using (var transaction = _context.Database.BeginTransaction())
            {
                try
                {
                    UploadImage._environment = _environment;
                    if (productForm.Imagem == null)
                    {
                        var newProd = new Product()
                        {
                            Name = productForm.Name, Value = productForm.Value
                        };
                        _context.Products.Add(newProd);
                        _context.SaveChanges();
                        await transaction.CommitAsync();

                        return(newProd);
                    }
                    else
                    {
                        var newProd = new Product()
                        {
                            Name = productForm.Name, Value = productForm.Value
                        };

                        _context.Products.Add(newProd);

                        _context.SaveChanges();

                        //salvando imagem no root
                        var imagePath = UploadImage.upload(productForm.Imagem, newProd.Id.ToString());
                        //validando se imagem foi salva com sucesso
                        if (imagePath != "Failed")
                        {
                            newProd.Image_path = imagePath;
                            _context.Update(newProd);
                            _context.SaveChanges();
                            await transaction.CommitAsync();

                            return(newProd);
                        }
                        else
                        {
                            transaction.Rollback();
                            return(null);
                        }
                    }
                }
                catch (System.Exception)
                {
                    transaction.Rollback();
                    return(null);
                }
            }
        }
        private void AddOrDeleteProductVariation(ProductForm model, Product product)
        {
            foreach (var productVariationVm in model.Product.Variations)
            {
                var productLink = product.ProductLinks.Where(x => x.LinkType == ProductLinkType.Super).FirstOrDefault(x => x.LinkedProduct.Name == productVariationVm.Name);
                if (productLink == null)
                {
                    productLink = new ProductLink
                    {
                        LinkType      = ProductLinkType.Super,
                        Product       = product,
                        LinkedProduct = product.Clone()
                    };

                    productLink.LinkedProduct.Name                  = productVariationVm.Name;
                    productLink.LinkedProduct.SeoTitle              = StringHelper.ToUrlFriendly(productVariationVm.Name);
                    productLink.LinkedProduct.Sku                   = productVariationVm.Sku;
                    productLink.LinkedProduct.Gtin                  = productVariationVm.Gtin;
                    productLink.LinkedProduct.Price                 = productVariationVm.Price;
                    productLink.LinkedProduct.OldPrice              = productVariationVm.OldPrice;
                    productLink.LinkedProduct.NormalizedName        = productVariationVm.NormalizedName;
                    productLink.LinkedProduct.HasOptions            = false;
                    productLink.LinkedProduct.IsVisibleIndividually = false;
                    productLink.LinkedProduct.ThumbnailImage        = product.ThumbnailImage;

                    foreach (var combinationVm in productVariationVm.OptionCombinations)
                    {
                        productLink.LinkedProduct.AddOptionCombination(new ProductOptionCombination
                        {
                            OptionId  = combinationVm.OptionId,
                            Value     = combinationVm.Value,
                            SortIndex = combinationVm.SortIndex
                        });
                    }

                    product.AddProductLinks(productLink);
                }
                else
                {
                    productLink.LinkedProduct.Sku       = productVariationVm.Sku;
                    productLink.LinkedProduct.Gtin      = productVariationVm.Gtin;
                    productLink.LinkedProduct.Price     = productVariationVm.Price;
                    productLink.LinkedProduct.OldPrice  = productVariationVm.OldPrice;
                    productLink.LinkedProduct.IsDeleted = false;
                }
            }

            foreach (var productLink in product.ProductLinks.Where(x => x.LinkType == ProductLinkType.Super))
            {
                if (model.Product.Variations.All(x => x.Name != productLink.LinkedProduct.Name))
                {
                    _productLinkRepository.Remove(productLink);
                    productLink.LinkedProduct.IsDeleted = true;
                }
            }
        }
        public RedirectToRouteResult Save(ProductForm form)
        {
            var product = _productRepository.GetById(form.Id);

            product.Name = form.Name;
            product.Model = form.Model;
            product.Sku = form.Sku;
            product.Price = form.Price;

            _productRepository.Save(product);

            return RedirectToAction("Index");
        }
Beispiel #32
0
 private void addnewProduct()
 {
         ProductForm pf = new ProductForm();
         pf.Title = "New Product for " + supplier.CompanyName;
         if (pf.ShowDialog().Value)
         {
             Product newProd = new Product();
             newProd.ProductName = pf.productName.Text;
             newProd.QuantityPerUnit = pf.quantityPerUnit.Text;
             newProd.UnitPrice = Decimal.Parse(pf.unitPrice.Text);
             this.supplier.Products.Add(newProd);
             this.productsInfo.Add(newProd);
             saveChanges.IsEnabled = true;
         }
 }
        public ActionResult Edit(ProductForm form)
        {
            if(! ModelState.IsValid) {
                return View("Edit", form);
            }

            var product = _productRepository.GetById(form.Id);

            product.Name = form.Name;
            product.Model = form.Model;
            product.Sku = form.Sku;
            product.Price = form.Price;

            _productRepository.Save(product);

            return RedirectToAction("Index");
        }
        private void AddOrDeleteProductAttribute(ProductForm model, Product product)
        {
            foreach (var productAttributeVm in model.Product.Attributes)
            {
                var productAttrValue =
                    product.AttributeValues.FirstOrDefault(x => x.AttributeId == productAttributeVm.Id);
                if (productAttrValue == null)
                {
                    productAttrValue = new ProductAttributeValue
                    {
                        AttributeId = productAttributeVm.Id,
                        Value = productAttributeVm.Value
                    };
                    product.AddAttributeValue(productAttrValue);
                }
                else
                {
                    productAttrValue.Value = productAttributeVm.Value;
                }
            }

            var deletedAttrValues =
               product.AttributeValues.Where(attrValue => model.Product.Attributes.All(x => x.Id != attrValue.AttributeId))
                   .ToList();

            foreach (var deletedAttrValue in deletedAttrValues)
            {
                deletedAttrValue.Product = null;
                product.AttributeValues.Remove(deletedAttrValue);
                productAttributeValueRepository.Remove(deletedAttrValue);
            }
        }
        private static void MapProductVariationVmToProduct(ProductForm model, Product product)
        {
            foreach (var variationVm in model.Product.Variations)
            {
                var productLink = new ProductLink
                {
                    LinkType = ProductLinkType.Super,
                    Product = product,
                    LinkedProduct = product.Clone()
                };

                productLink.LinkedProduct.Name = variationVm.Name;
                productLink.LinkedProduct.SeoTitle = StringHelper.ToUrlFriendly(variationVm.Name);
                productLink.LinkedProduct.Price = variationVm.Price;
                productLink.LinkedProduct.NormalizedName = variationVm.NormalizedName;
                productLink.LinkedProduct.HasOptions = false;
                productLink.LinkedProduct.IsVisibleIndividually = false;

                foreach (var combinationVm in variationVm.OptionCombinations)
                {
                    productLink.LinkedProduct.AddOptionCombination(new ProductOptionCombination
                    {
                        OptionId = combinationVm.OptionId,
                        Value = combinationVm.Value
                    });
                }

                productLink.LinkedProduct.ThumbnailImage = product.ThumbnailImage;

                product.AddProductLinks(productLink);
            }
        }
        private void AddOrDeleteProductOption(ProductForm model, Product product)
        {
            foreach (var optionVm in model.Product.Options)
            {
                foreach (var value in optionVm.Values)
                {
                    if (!product.OptionValues.Any(x => x.OptionId == optionVm.Id && x.Value == value))
                    {
                        product.AddOptionValue(new ProductOptionValue
                        {
                            Value = value,
                            OptionId = optionVm.Id
                        });
                    }
                }
            }

            var deletedProductOptionValues = new List<ProductOptionValue>();
            foreach (var productOptionValue in product.OptionValues)
            {
                var isExist = false;
                foreach (var optionVm in model.Product.Options)
                {
                    foreach (var value in optionVm.Values)
                    {
                        if (productOptionValue.OptionId == optionVm.Id && productOptionValue.Value == value)
                        {
                            isExist = true;
                            break;
                        }
                    }

                    if (isExist)
                    {
                        break;
                    }
                }

                if (!isExist)
                {
                    deletedProductOptionValues.Add(productOptionValue);
                }
            }

            foreach (var productOptionValue in deletedProductOptionValues)
            {
                product.OptionValues.Remove(productOptionValue);
                productOptionValueRepository.Remove(productOptionValue);
            }
        }
Beispiel #37
0
 private void listControl1_DoubleClick(object sender, EventArgs e)
 {
     ProductInfo pi = SelectedProduct;
     if (pi == null) return;
     ProductForm form = new ProductForm(DataService.Instance.IsAdmin);
     form.InitialData(pi);
     form.ShowDialog(this);
 }
        private void AddOrDeleteProductVariation(ProductForm model, Product product)
        {
            foreach (var productVariationVm in model.Product.Variations)
            {
                var productLink = product.ProductLinks.Where(x => x.LinkType == ProductLinkType.Super).FirstOrDefault(x => x.LinkedProduct.Name == productVariationVm.Name);
                if (productLink == null)
                {
                    productLink = productLink = new ProductLink
                    {
                        LinkType = ProductLinkType.Super,
                        Product = product,
                        LinkedProduct = product.Clone()
                    };

                    productLink.LinkedProduct.Name = productVariationVm.Name;
                    productLink.LinkedProduct.SeoTitle = StringHelper.ToUrlFriendly(productVariationVm.Name);
                    productLink.LinkedProduct.Price = productVariationVm.Price;
                    productLink.LinkedProduct.NormalizedName = productVariationVm.NormalizedName;
                    productLink.LinkedProduct.HasOptions = false;
                    productLink.LinkedProduct.IsVisibleIndividually = false;

                    foreach (var combinationVm in productVariationVm.OptionCombinations)
                    {
                        productLink.LinkedProduct.AddOptionCombination(new ProductOptionCombination
                        {
                            OptionId = combinationVm.OptionId,
                            Value = combinationVm.Value
                        });
                    }

                    product.AddProductLinks(productLink);
                }
                else
                {
                    productLink.LinkedProduct.Price = productVariationVm.Price;
                    productLink.LinkedProduct.IsDeleted = false;
                }
            }

            foreach (var productLink in product.ProductLinks.Where(x => x.LinkType == ProductLinkType.Super))
            {
                if (model.Product.Variations.All(x => x.Name != productLink.LinkedProduct.Name))
                {
                    productLinkRepository.Remove(productLink);
                    productLink.LinkedProduct.IsDeleted = true;
                }
            }
        }
        private void SaveProductImages(ProductForm model, Product product)
        {
            if (model.ThumbnailImage != null)
            {
                var fileName = SaveFile(model.ThumbnailImage);
                if (product.ThumbnailImage != null)
                {
                    product.ThumbnailImage.FileName = fileName;
                }
                else
                {
                    product.ThumbnailImage = new Media { FileName = fileName };
                }
            }

            // Currently model binder cannot map the collection of file productImages[0], productImages[1]
            foreach (var file in Request.Form.Files)
            {
                if (file.ContentDisposition.Contains("productImages"))
                {
                    model.ProductImages.Add(file);
                }
            }

            foreach (var file in model.ProductImages)
            {
                var fileName = SaveFile(file);
                var productMedia = new ProductMedia
                {
                    Product = product,
                    Media = new Media { FileName = fileName }
                };
                product.AddMedia(productMedia);
            }
        }
        public IActionResult Create(ProductForm model)
        {
            if (!ModelState.IsValid)
            {
                return new BadRequestObjectResult(ModelState);
            }

            var product = new Product
            {
                Name = model.Product.Name,
                SeoTitle = StringHelper.ToUrlFriendly(model.Product.Name),
                ShortDescription = model.Product.ShortDescription,
                Description = model.Product.Description,
                Specification = model.Product.Specification,
                Price = model.Product.Price,
                OldPrice = model.Product.OldPrice,
                IsPublished = model.Product.IsPublished,
                BrandId = model.Product.BrandId,
                HasOptions = model.Product.Variations.Any() ? true : false,
                IsVisibleIndividually = true
            };

            foreach (var option in model.Product.Options)
            {
                foreach (var value in option.Values)
                {
                    product.AddOptionValue(new ProductOptionValue
                    {
                        Value = value,
                        OptionId = option.Id
                    });
                }
            }

            foreach (var attribute in model.Product.Attributes)
            {
                var attributeValue = new ProductAttributeValue
                {
                    AttributeId = attribute.Id,
                    Value = attribute.Value
                };

                product.AddAttributeValue(attributeValue);
            }

            foreach (var categoryId in model.Product.CategoryIds)
            {
                var productCategory = new ProductCategory
                {
                    CategoryId = categoryId
                };
                product.AddCategory(productCategory);
            }

            SaveProductImages(model, product);

            MapProductVariationVmToProduct(model, product);

            productService.Create(product);

            return Ok();
        }
Beispiel #41
0
        private void editProduct(Product product)
        {
            ProductForm pf = new ProductForm();
            pf.Title = "Edit Product Details";
            pf.productName.Text = product.ProductName;
            pf.quantityPerUnit.Text = product.QuantityPerUnit;
            pf.unitPrice.Text = product.UnitPrice.ToString();

            if (pf.ShowDialog().Value)
            {
                product.ProductName = pf.productName.Text;
                product.QuantityPerUnit = pf.quantityPerUnit.Text;
                product.UnitPrice = Decimal.Parse(pf.unitPrice.Text);
                saveChanges.IsEnabled = true;
            }
        }
        private void AddOrDeleteCategories(ProductForm model, Product product)
        {
            foreach (var categoryId in model.Product.CategoryIds)
            {
                if (product.Categories.Any(x => x.CategoryId == categoryId))
                {
                    continue;
                }

                var productCategory = new ProductCategory
                {
                    CategoryId = categoryId
                };
                product.AddCategory(productCategory);
            }

            var deletedProductCategories =
                product.Categories.Where(productCategory => !model.Product.CategoryIds.Contains(productCategory.CategoryId))
                    .ToList();

            foreach (var deletedProductCategory in deletedProductCategories)
            {
                deletedProductCategory.Product = null;
                product.Categories.Remove(deletedProductCategory);
                productCategoryRepository.Remove(deletedProductCategory);
            }
        }
 private void ProductToolStripMenuItem_Click(object sender, EventArgs e)
 {
     ProductForm productForm = new ProductForm();
     productForm.MdiParent = this;
     productForm.Show();
 }
        public IActionResult Edit(long id, ProductForm model)
        {
            if (!ModelState.IsValid)
            {
                return new BadRequestObjectResult(ModelState);
            }

            var product = productRepository.Query()
                .Include(x => x.ThumbnailImage)
                .Include(x => x.Medias).ThenInclude(m => m.Media)
                .Include(x => x.ProductLinks).ThenInclude(x => x.LinkedProduct)
                .Include(x => x.OptionValues).ThenInclude(o => o.Option)
                .Include(x => x.AttributeValues).ThenInclude(a => a.Attribute).ThenInclude(g => g.Group)
                .Include(x => x.Categories)
                .FirstOrDefault(x => x.Id == id);
            product.Name = model.Product.Name;
            product.SeoTitle = StringHelper.ToUrlFriendly(product.Name);
            product.ShortDescription = model.Product.ShortDescription;
            product.Description = model.Product.Description;
            product.Specification = model.Product.Specification;
            product.Price = model.Product.Price;
            product.OldPrice = model.Product.OldPrice;
            product.BrandId = model.Product.BrandId;

            SaveProductImages(model, product);

            foreach (var productMediaId in model.Product.DeletedMediaIds)
            {
                var productMedia = product.Medias.First(x => x.Id == productMediaId);
                mediaService.DeleteMedia(productMedia.Media);
                product.RemoveMedia(productMedia);
            }

            AddOrDeleteProductOption(model, product);

            AddOrDeleteProductAttribute(model, product);

            AddOrDeleteCategories(model, product);

            AddOrDeleteProductVariation(model, product);

            productService.Update(product);

            return Ok();
        }