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 Add(AddCustomerViewModel model) { FillLookups(); string pictureName = ""; if (ModelState.IsValid)// && model.Location.IsValid()) { var newCustomerId = _customerRepository.GetAll().Last().Id + 1; if (!string.IsNullOrWhiteSpace(model.LogoFileName)) { bool isValidPicture; pictureName = GetLabelPath(model.CustomerType == OmsCustomerType.Custom, model.LogoFileName, newCustomerId, out isValidPicture); if (!isValidPicture) { ModelState.AddModelError(string.Empty, pictureName); return(View("Add", model)); } } var user = new User { FirstName = model.OrderContactFirstName, LastName = model.OrderContactLastName, Email = model.OrderContactEmail, Phone = model.OrderContactPhone, Password = model.PortalPassword, IsActive = true, TypeId = 2 }; _userRepository.Add(user); _userRepository.Save(); var customer = new Customer { BestBeforeDays = model.BestBeforeDays, LogoTypeId = model.UseLogoOnLabels, LogoFileName = pictureName, Name = model.CustomerName, CustomerTypeId = model.CustomerType, PONumber = model.CustomerPo, DistributedBy = model.LabelDistributedBy, BoxLabel = model.BoxLabel, TrayLabel = model.TrayLabel, BagLabel = model.BagLabel, User = user, IsArchived = model.IsArchived, UserId = user.Id }; _customerRepository.Add(customer); _customerRepository.Save(); var products = _productRepository.GetByCustomerType(model.CustomerType).Where(x => x.IsActive).ToList(); var customerProducts = new List <CustomerProductData>(); foreach (var product in products) { customerProducts.Add(new CustomerProductData { CustomerId = customer.Id, ProductId = product.Id, ProductCode = product.Code, ProductDescription = product.EnglishDescription, Gtin = product.Gtin, PricePerPound = product.PricePerPound }); } _customerProductDataRepository.AddRange(customerProducts); _customerProductDataRepository.Save(); if (model.Location.IsExists()) { var location = model.Location.ToCustomerLocation(); location.CustomerId = customer.Id; _customerLocationRepository.Add(location); _customerLocationRepository.Save(); } return(RedirectToAction("EditCustomer", new { customerId = customer.Id })); } foreach (var pair in model.Location.Validate()) { ModelState.AddModelError(pair.Key, pair.Value); } return(View(model)); }