public ActionResult CreateReturnNote(ReceiptNoteModels model) { try { List <PurchaseOrderModels> _ListPurchaseOrder = new List <PurchaseOrderModels>(); //_ListPurchaseOrder.InsertRange(0, model.ListPurchaseOrder); foreach (var item in model.ListPurchaseOrder) { var obj = new PurchaseOrderModels(); obj.Additional = item.Additional; obj.AdditionalReason = item.AdditionalReason; obj.ColorAlert = item.ColorAlert; obj.CreatedBy = item.CreatedBy; obj.CreatedDate = item.CreatedDate; obj.Delete = item.Delete; obj.DeliveryDate = item.DeliveryDate; obj.Id = item.Id; obj.IsActived = item.IsActived; obj.ListItem = item.ListItem; obj.ListReceiptPO = item.ListReceiptPO; obj.ListStores = item.ListStores; obj.ListSupplier = item.ListSupplier; obj.ListTaxType = item.ListTaxType; obj.ModifierBy = item.ModifierBy; obj.ModifierDate = item.ModifierDate; obj.Note = item.Note; obj.PODate = item.PODate; obj.PONumber = item.PONumber; obj.POStatus = item.POStatus; obj.Store = item.Store; obj.StoreID = item.StoreID; obj.StoreName = item.StoreName; obj.SubTotal = item.SubTotal; obj.Supplier = item.Supplier; obj.SupplierId = item.SupplierId; obj.SupplierName = item.SupplierName; obj.Symbol = item.Symbol; obj.TaxAmount = item.TaxAmount; obj.TaxType = item.TaxType; obj.TaxValue = item.TaxValue; obj.Total = item.Total; _ListPurchaseOrder.Add(obj); } model.CreatedBy = CurrentUser.UserName; model.UpdatedBy = CurrentUser.UserName; model.ReceiptBy = CurrentUser.UserName; model.CreatedDate = DateTime.Now; model.UpdatedDate = DateTime.Now; model.ReceiptDate = DateTime.Now; model.ListPurchaseOrder.ForEach(x => { x.ListItem = x.ListItem.Where(z => z.ReturnQty > 0).ToList(); }); //========== ReturnNoteModels RTmodel = new ReturnNoteModels() { ReceiptNoteId = model.Id, CreatedBy = CurrentUser.UserName, CreatedDate = DateTime.Now, ModifierBy = CurrentUser.UserName, ModifierDate = DateTime.Now, IsActived = true }; RTmodel.ListPurchaseOrder = model.ListPurchaseOrder.Where(x => x.ListItem.Count > 0).ToList(); bool result = false; string msg = ""; double qtyCurrentStock = 0; bool isCheck = true; string ingredientName = string.Empty; if (RTmodel.ListPurchaseOrder.Count > 0) { foreach (var poDetail in RTmodel.ListPurchaseOrder) { foreach (var item in poDetail.ListItem) { if (item.ReturnQty < 0) { ModelState.AddModelError("error_msg" + item.Id, CurrentUser.GetLanguageTextFromKey("Please, can not input negative number")); break; } if (item.ReturnQty > item.ReceivingQty) { ModelState.AddModelError("error_msg" + item.Id, CurrentUser.GetLanguageTextFromKey("Can not return more than receiving quantity")); break; } ingredientName = string.Empty; qtyCurrentStock = 0; isCheck = _InventoryFactory.CheckStockForReturn(model.StoreId, item.IngredientId , item.ReturnQty, item.BaseQty, ref qtyCurrentStock, ref ingredientName); if (!isCheck) { ModelState.AddModelError("error_msg" + item.Id, string.Format("[{0}] " + CurrentUser.GetLanguageTextFromKey("is not enough stock to return!"), ingredientName)); break; } } } if (!ModelState.IsValid) { model.ListPurchaseOrder = _ListPurchaseOrder; //return PartialView("_CreateReturnNote", model); return(View("_CreateReturnNote", model)); } result = _RTFactory.Insert(RTmodel, model.StoreId, ref msg); } else { result = true; } //=== if (result) { return(RedirectToAction("Index")); } else { ModelState.AddModelError("StoreId", msg); return(View("_CreateReturnNote", model)); } } catch (Exception ex) { NSLog.Logger.Error("ReturnNote_Create: ", ex); return(new HttpStatusCodeResult(400, ex.Message)); } }