public bool CreateVendorProduct(VendorProductDto model) { try { // get its vendor's info and if it's always on stock, use it alwaysQuantity var vendor = _context.vendors.FirstOrDefault(x => x.Id == model.VendorId); if (vendor.IsAlwaysInStock) { model.Quantity = vendor.AlwaysQuantity ?? 0; } var product = Mapper.Map <vendorproduct>(model); product.Created = DateTime.UtcNow; product.CreatedBy = model.ModifiedBy; _context.vendorproducts.Add(product); _context.SaveChanges(); // generated id model.EisSupplierSKU = product.EisSupplierSKU; return(true); } catch (DbEntityValidationException ex) { var errorMsg = EisHelper.ParseDbEntityValidationException(ex); _logger.LogError(LogEntryType.VendorProductService, errorMsg, ex.StackTrace); return(false); } catch (Exception ex) { _logger.LogError(LogEntryType.VendorProductService, EisHelper.GetExceptionMessage(ex), ex.StackTrace); return(false); } }
public bool UpdateVendorProduct(string eisSupplierSKU, VendorProductDto model) { try { var existingProduct = _context.vendorproducts .FirstOrDefault(x => x.EisSupplierSKU == eisSupplierSKU); if (existingProduct == null) { return(false); } // get its vendor's info and if it's always on stock, use it alwaysQuantity var vendor = existingProduct.vendor; if (vendor.IsAlwaysInStock) { model.Quantity = vendor.AlwaysQuantity ?? 0; } // reflect the changes from model Mapper.Map(model, existingProduct); existingProduct.ModifiedBy = model.ModifiedBy; existingProduct.Modified = DateTime.UtcNow; _context.SaveChanges(); return(true); } catch (DbEntityValidationException ex) { var errorMsg = EisHelper.ParseDbEntityValidationException(ex); _logger.LogError(LogEntryType.VendorProductService, errorMsg, ex.StackTrace); return(false); } catch (Exception ex) { _logger.LogError(LogEntryType.VendorProductService, EisHelper.GetExceptionMessage(ex), ex.StackTrace); return(false); } }
public ActionResult Save(string id, VendorProductDto model) { // init the viewbags for dropdown populateViewBags(); if (!ModelState.IsValid) { var errors = ModelState.Values.SelectMany(e => e.Errors.Select(x => x.ErrorMessage)); ModelState.AddModelError("", string.Join("<br/>", errors)); // return to the original page which the request came from var originpage = string.IsNullOrEmpty(id) ? "create" : "edit"; return(View(originpage, model)); } // set the IsAutoLinkToEisSKU if the IsCreateEisSKUAndLink set to TRUE if (model.IsCreateEisSKUAndLink) { model.IsAutoLinkToEisSKU = true; } // remove the trailing the trailing spaces model.SupplierSKU = model.SupplierSKU.Trim(); model.Name = model.Name.Trim(); model.ModifiedBy = User.Identity.Name; if (string.IsNullOrEmpty(id)) { // get the vendor start SKU code var startSKUCode = _vendorService.GetVendorStartSku(model.VendorId); var eisSupplierSKU = string.Format("{0}{1}", startSKUCode.Trim(), model.SupplierSKU.Trim()).ToUpper(); // let's determine first if this vendor product already exists if (_vendorProductService.IsEisSupplierSKUExists(eisSupplierSKU)) { ModelState.AddModelError("", string.Format("This vendor product with EisSupplierSKU: \'{0}\' is already exist!", eisSupplierSKU)); return(View("create", model)); } // set the EisSupplierSKU and save the product model.EisSupplierSKU = eisSupplierSKU; _vendorProductService.CreateVendorProduct(model); } else { // update the vendor product _vendorProductService.UpdateVendorProduct(id, model); } // check first if we want to auto-link and create new EIS product if it doesn't exist if (model.IsCreateEisSKUAndLink && !string.IsNullOrEmpty(model.UPC)) { var vendorProduct = new VendorProduct(); CopyObject.CopyFields(model, vendorProduct); _vendorProductService.AddLinkAndCreateEisProductIfNoMatchWithUPC(vendorProduct); } else { // check if there's a need to auto-link this vendor product with EIS product if (model.IsAutoLinkToEisSKU && !model.IsCreateEisSKUAndLink && !string.IsNullOrEmpty(model.UPC)) { _vendorProductService.UpdateEisProductLinks(model.EisSupplierSKU, model.UPC, model.MinPack); } else if (!model.IsAutoLinkToEisSKU) { // delete the existing EIS product links if there's any _vendorProductService.DeleteOldVendorProductLinks(model.EisSupplierSKU, new List <string>()); } } // if we got this far, everything is OK TempData["Message"] = "Changes have been successfully saved!"; return(RedirectToAction("edit", new { id = model.EisSupplierSKU })); }