public string GetNextEisSKUForCompany(int companyId) { string eisSKU = null; using (var context = new EisInventoryContext()) { // 1st get the StartSKUCode for this product via its Company var company = context.companies.FirstOrDefault(x => x.Id == companyId); if (company == null) { return(null); } // get the SKU start code and also its serial SKU code... var skuCodeStart = company.SKUCodeStart.Trim(); var serialSkuCode = company.SearialSKUCode.Trim(); // get the max EisSKU for this company var maxEisSKU = context.products .Where(x => x.CompanyId == companyId && x.SkuType == SkuType.Normal) .Max(x => x.EisSKU); // get the serial SKU code for this EIS SKU if (!string.IsNullOrEmpty(maxEisSKU)) { serialSkuCode = maxEisSKU.RightStartAt(skuCodeStart.Length); } // get the next SKU code and assign it to the model serialSkuCode = EisHelper.GetNextCode(serialSkuCode); eisSKU = string.Format("{0}{1}", skuCodeStart, serialSkuCode); } return(eisSKU); }
public ActionResult Save(string id, ProductDto model) { // create view bag first for the vendors populateViewBags(); var originpage = string.IsNullOrEmpty(id) ? "create" : "edit"; model.ModifiedBy = User.Identity.Name; 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 return(View(originpage, model)); } if (string.IsNullOrEmpty(id)) { // generate the EisSKU for this product // 1st get the StartSKUCode for this product via its Company var company = _companyService.GetCompany(model.CompanyId); var skuCodeStart = company.SKUCodeStart.Trim(); var serialSkuCode = company.SearialSKUCode.Trim(); // get the max EisSKU for this company var maxEisSKU = _productService.GetMaxEisSKUByCompany(model.CompanyId); if (!string.IsNullOrEmpty(maxEisSKU)) { serialSkuCode = maxEisSKU.RightStartAt(skuCodeStart.Length); } // get the next SKU code and assign it to the model serialSkuCode = EisHelper.GetNextCode(serialSkuCode); model.EisSKU = string.Format("{0}{1}", skuCodeStart, serialSkuCode); // check if the EisSKU exists if (_productService.IsEisSKUExists(model.EisSKU)) { ModelState.AddModelError("", string.Format("The \'{0}\' EIS SKU is already exist!", model.EisSKU)); return(View(originpage, model)); } // if we got this far, everything is OK _productService.SaveProduct(model); } else { // if we got this far, everything is OK _productService.UpdateProduct(id, model); } TempData["Message"] = "Changes have been successfully saved!"; return(RedirectToAction("Edit", new { id = model.EisSKU })); }