public ActionResult CreateOrUpdate(KeViewModel model) { var ke = string.IsNullOrEmpty(model.Id) ? new Ke { NgayCapNhat = DateTime.Now } : _keService.Get(m => m.Id == model.Id); ViewBag.Tus = SelectListItemFromDomain.SelectListItem_Tu(_tuService.GetAll(m => m.OrderBy(t => t.Ten))); var checkName = _keService.Get(m => m.Ten == model.Ten); if (checkName != null) { TempData["AlertMessage"] = "Kệ Có Cùng Tên Đã Tồn Tại"; return(View(model)); } var userId = User.Identity.GetUserId(); var cthd = "kệ: " + model.Ten; ke.Ten = model.Ten; ke.SoThuTu = model.SoThuTu; ke.SoHopToiDa = model.SoHopToiDa; ke.NamBatDau = model.NamBatDau; ke.NamKetThuc = model.NamKetThuc; ke.TinhTrang = model.TinhTrang; ke.UserId = User.Identity.GetUserId(); ke.Tuid = model.Tuid; if (string.IsNullOrEmpty(model.Id)) { ke.SoHopHienTai = 0; var result = UpdateTu_SoLuongHienTai(model.Tuid, ActionWithObject.Update); if (!result) { TempData["AlertMessage"] = "Số Lượng Kệ Trong Tủ Bạn Chọn Đã Đầy"; return(View(model)); } _keService.Insert(ke); _functionLichSuHoatDongService.Create(ActionWithObject.Create, userId, cthd); TempData["AlertMessage"] = "Tạo Mới Thành Công"; } else { _keService.Update(ke); _functionLichSuHoatDongService.Create(ActionWithObject.Update, userId, cthd); TempData["AlertMessage"] = "Cập Nhật Thành Công"; } return(RedirectToAction("Index")); }
public bool UpdateTu_SoHopHienTai(string id, ActionWithObject action) { var ke = _keService.GetById(id); if (ke == null) { return(false); } var soluong = Compute.ComputeAmountWithAction(ke.SoHopHienTai, action); if (soluong > ke.SoHopToiDa) { DropDownList(); return(false); } else { ke.SoHopHienTai = soluong; _keService.Update(ke); return(true); } }