public async Task<ActionResult> Inventory_Update([DataSourceRequest] DataSourceRequest request, InventoryViewModel inventoryViewModel) { if (inventoryViewModel != null && ModelState.IsValid) { Inventory data = db.Inventories.Find(inventoryViewModel.ID); //data.StockInById = inventoryViewModel.StockInUserId; //data.StockInDate = inventoryViewModel.StockInDate; //data.DealerId = inventoryViewModel.DealerId; List<SalesTransaction> sales = db.SalesTransactions.Where(s => s.InventoryId == inventoryViewModel.ID).ToList(); if (sales.Count == 1) { sales[0].ProductId = inventoryViewModel.ProductId; } data.ProductId = inventoryViewModel.ProductId; data.IMEI = inventoryViewModel.IMEI; data.Type = inventoryViewModel.Type; data.Note = inventoryViewModel.Note; if (data.Type == InventoryType.Purchased) { data.PurchasedFrom = string.IsNullOrEmpty(inventoryViewModel.PurchasedFrom) ? null : inventoryViewModel.PurchasedFrom; data.PurchasedById = inventoryViewModel.PurchasedById; data.DefferCode = null; data.DefferName = null; data.DefferOrderNo = null; inventoryViewModel.DefferCode = null; inventoryViewModel.DefferName = null; inventoryViewModel.DefferOrderNo = null; } else if (data.Type == InventoryType.Deffer) { data.DefferCode = string.IsNullOrEmpty(inventoryViewModel.DefferCode) ? null : inventoryViewModel.DefferCode; data.DefferName = string.IsNullOrEmpty(inventoryViewModel.DefferName) ? null : inventoryViewModel.DefferName; data.DefferOrderNo = string.IsNullOrEmpty(inventoryViewModel.DefferOrderNo) ? null : inventoryViewModel.DefferOrderNo; data.PurchasedFrom = null; data.PurchasedById = null; inventoryViewModel.PurchasedFrom = null; inventoryViewModel.PurchasedById = null; } else { data.PurchasedFrom = null; data.PurchasedById = null; data.DefferCode = null; data.DefferName = null; data.DefferOrderNo = null; inventoryViewModel.PurchasedFrom = null; inventoryViewModel.PurchasedById = null; inventoryViewModel.DefferCode = null; inventoryViewModel.DefferName = null; inventoryViewModel.DefferOrderNo = null; } db.Entry(data).State = EntityState.Modified; await db.SaveChangesAsync(); } return Json(new[] { inventoryViewModel }.ToDataSourceResult(request, ModelState)); }
public async Task<ActionResult> Inventory_Create([DataSourceRequest] DataSourceRequest request, InventoryViewModel inventoryViewModel) { //if (inventory != null && ModelState.IsValid) //{ // db.Inventories.Add(inventory); // //await db.SaveChangesAsync(); //} ////var invetoryChangeHistory = new InvetoryChangeHistory ////{ //// ChangeDate = DateTime.Now, //// Inventory = inventory, //// OperatedByEmployeeID = User.Identity.GetUserId(), //// OperationType = InventoryOperationType.In ////}; ////db.InvetoryChangeHistories.Add(invetoryChangeHistory); //await db.SaveChangesAsync(); //return Json(new[] { inventory }.ToDataSourceResult(request, ModelState)); if (inventoryViewModel != null && ModelState.IsValid) { inventoryViewModel.StockInUserId = User.Identity.GetUserId(); if (HelperMethods.GetDealerId().HasValue) inventoryViewModel.DealerId = HelperMethods.GetDealerId().GetValueOrDefault(); else { //return HttpNotFound("No Current Dealer Found"); } inventoryViewModel.StockInDate = DateTime.Now; Inventory data = new Inventory { StockInById = inventoryViewModel.StockInUserId, StockInDate = inventoryViewModel.StockInDate, DealerId = inventoryViewModel.DealerId, ProductId = inventoryViewModel.ProductId, IMEI = inventoryViewModel.IMEI, Type = inventoryViewModel.Type, Note = inventoryViewModel.Note, PurchasedFrom = string.IsNullOrEmpty(inventoryViewModel.PurchasedFrom) ? null:inventoryViewModel.PurchasedFrom, PurchasedById = inventoryViewModel.PurchasedById, DefferCode = string.IsNullOrEmpty(inventoryViewModel.DefferCode) ? null : inventoryViewModel.DefferCode, DefferName = string.IsNullOrEmpty(inventoryViewModel.DefferName) ? null : inventoryViewModel.DefferName, DefferOrderNo = string.IsNullOrEmpty(inventoryViewModel.DefferOrderNo) ? null : inventoryViewModel.DefferOrderNo }; db.Inventories.Add(data); //add change record to history InvetoryChangeHistory changeHistory2 = new InvetoryChangeHistory(); changeHistory2.IMEI = data.IMEI; changeHistory2.ChangeDate = data.StockInDate; changeHistory2.OldOperationType = null; changeHistory2.NewOperationType = InventoryOperationType.In; changeHistory2.OperatedByEmployeeID = data.StockInById; var result = await HelperMethods.GetCurrentDealer(); changeHistory2.ToDealer = result.DealerName; db.InvetoryChangeHistories.Add(changeHistory2); await db.SaveChangesAsync(); //db.SaveChanges(); //inventoryViewModel.ID = data.InventoryId; await db.SaveChangesAsync(); inventoryViewModel.ID = data.InventoryId; } //salesTransaction.LinkedPlan = db.Plans.FirstOrDefault(p => p.PlanId == salesTransaction.PlanId); //await db.SaveChangesAsync(); return Json(new[] { inventoryViewModel }.ToDataSourceResult(request, ModelState)); }