public ActionResult Create(ReceiptNoteSelfMadeModels model) { try { model.ListItemForSelect = null; RecipeIngredientUsageModels _objIngredientDependent = null; model.CreatedBy = CurrentUser.UserName; model.UpdatedBy = CurrentUser.UserName; model.ReceiptBy = CurrentUser.UserName; model.CreatedDate = DateTime.Now; model.UpdatedDate = DateTime.Now; model.ReceiptDate = DateTime.Now; string msg = ""; model.ListItem = model.ListItem.Where(x => x.Status != (int)Commons.EStatus.Deleted && x.Status != null).ToList(); //Check stock before save //var lstIngredientSeftMadeIds = model.ListItem.Where(ww => ww.IsSelfMode && ww.IsStockAble).Select(ss => ss.IngredientId).ToList(); var lstIngredientSeftMade = model.ListItem.Where(ww => ww.IsSelfMode && ww.IsStockAble).ToList(); if (lstIngredientSeftMade != null && lstIngredientSeftMade.Any()) { double currentQty = 0; string mess = string.Empty; //List<string> lstIngredientName = new List<string>(); List <ErrorEnoughModels> lstIngredientName = new List <ErrorEnoughModels>(); _objIngredientDependent = _recipeFactory.GetRecipesByIngredientSeftMade(lstIngredientSeftMade); bool resultCheck = _inventoryFactory.CheckStockBeforeRNSeftMade(model.StoreId, _objIngredientDependent.ListChilds , ref currentQty, ref lstIngredientName); if (!resultCheck) { if (lstIngredientName != null && lstIngredientName.Any()) { foreach (var item in lstIngredientName) { if (item.ListIngredientNameNotEnough != null && item.ListIngredientNameNotEnough.Count > 1) { mess = string.Format("Stock of [{0}] are not enough", string.Join(", ", item.ListIngredientNameNotEnough.ToArray())); } else { mess = string.Format("Stock of [{0}] is not enough", string.Join(", ", item.ListIngredientNameNotEnough.ToArray())); } ModelState.AddModelError("error_msg" + item.MixIngredientId, mess); } } // if (lstIngredientName != null && lstIngredientName.Count > 1) //{ // mess = string.Format("Stock of [{0}] are not enough", string.Join(", ",lstIngredientName.ToArray())); //} //else // mess = string.Format("Stock of [{0}] is not enough", string.Join(", ", lstIngredientName.ToArray())); } } if (!ModelState.IsValid) { return(View(model)); } //check item for WO var lstWoId = model.ListItem.Where(ww => !string.IsNullOrEmpty(ww.WOId)).Select(ss => ss.WOId).ToList(); if (lstWoId != null && lstWoId.Any() && model.ListWorkOrder != null && model.ListWorkOrder.Any()) { lstWoId = lstWoId.Distinct().ToList(); model.ListWorkOrder = model.ListWorkOrder.Where(ww => lstWoId.Contains(ww.Id)).ToList(); } bool result = _factory.Insert(model, _objIngredientDependent, ref msg); if (result) { return(RedirectToAction("Index")); } else { ModelState.AddModelError("StoreId", msg); return(View("Create")); } } catch (Exception ex) { _logger.Error("ReceiptNoteSelfMade_Create: " + ex); return(new HttpStatusCodeResult(400, ex.Message)); } }