public ActionResult DeleteProduct(int id) { WarehouseInventoryDetailModel model = _context.WarehouseInventoryDetailModel .Where(p => p.WarehouseInventoryDetailId == id) .FirstOrDefault(); var Resuilt = ""; if (model == null) { Resuilt = "Không tìm thấy sản phẩm yêu cầu !"; } else { model.Actived = false; _context.Entry(model).State = System.Data.Entity.EntityState.Modified; var master = _context.WarehouseInventoryMasterModel.Where(p => p.WarehouseInventoryMasterId == model.WarehouseInventoryMasterId).FirstOrDefault(); master.TotalQty--; _context.Entry(master).State = System.Data.Entity.EntityState.Modified; _context.SaveChanges(); Resuilt = "success"; } return(Json(Resuilt, JsonRequestBehavior.AllowGet)); }
public ActionResult Import(HttpPostedFileBase excelfile, int WarehouseId) { try { if (excelfile == null || excelfile.ContentLength == 0) { return(Json("Bạn vui lòng chọn 1 file excel", JsonRequestBehavior.AllowGet)); } else { using (var package = new ExcelPackage(excelfile.InputStream)) { using (TransactionScope ts = new TransactionScope()) { ExcelWorksheet worksheet = package.Workbook.Worksheets[1]; int col = 1; #region // Bước 1 : insert WarehouseInventoryMasterModel WarehouseInventoryMasterModel master = new WarehouseInventoryMasterModel() { WarehouseInventoryMasterCode = GetWarehouseInventoryMasterCode(), CreatedDate = DateTime.Now, CreatedAccount = currentAccount.UserName, CreatedEmployeeId = currentEmployee.EmployeeId, CreatedIEOther = false, Actived = true // TotalQty : Tổng số sản phẩm kiểm kho update sau. }; master.WarehouseId = WarehouseId; _context.Entry(master).State = System.Data.Entity.EntityState.Added; _context.SaveChanges();// Lưu để lấy masterID #endregion #region // bước 2 : duyệt danh sách insert vào WarehouseInventoryDetailModel WarehouseInventoryDetailModel p; int TotalQty = 0; var rowCnt = worksheet.Dimension.End.Row; for (int row = 6; row < rowCnt; row++) { // Nếu Tồn thực tế có giá trị -> Có kiểm kho -> Insert vào database. if (worksheet.Cells[row, 7].Text != "") { try { TotalQty++; decimal tonkho; #region // Kiểm tra cột 2, 10 TH rỗng if (worksheet.Cells[row, 2].Text == "") { return(Json("Dòng " + row + " Cột 'ID sản phẩm' bị rỗng", JsonRequestBehavior.AllowGet)); } if (worksheet.Cells[row, 6].Text == "") { tonkho = 0; } else { tonkho = decimal.Parse(worksheet.Cells[row, 6].Value.ToString()); } #endregion p = new WarehouseInventoryDetailModel() { WarehouseInventoryMasterId = master.WarehouseInventoryMasterId, ProductId = int.Parse(worksheet.Cells[row, 2].Value.ToString()), Inventory = tonkho, ActualInventory = decimal.Parse(worksheet.Cells[row, 7].Value.ToString()), AmountDifference = decimal.Parse(worksheet.Cells[row, 7].Value.ToString()) - tonkho, Actived = true }; _context.Entry(p).State = System.Data.Entity.EntityState.Added; _context.SaveChanges(); } catch { } } } #endregion // Nếu TotalQty != 0 : Danh sách kiểm kho hợp lệ (có tối thiểu 1 sản phẩm kiểm kho), update TotalQty trong master if (TotalQty != 0) { master.TotalQty = TotalQty; _context.Entry(master).State = System.Data.Entity.EntityState.Modified; _context.SaveChanges(); ts.Complete(); return(Json("success", JsonRequestBehavior.AllowGet)); } else { return(Json("Danh sách kiểm kho không hợp lệ ! Vui lòng kiểm tra lại", JsonRequestBehavior.AllowGet)); } } } } } catch { return(Json("Lỗi! Vui lòng liên hệ kĩ thuật viên để được giúp đỡ !", JsonRequestBehavior.AllowGet)); } }
public ActionResult Save(WarehouseInventoryMasterModel model, List <WarehouseInventoryDetailViewModel> detail) { string resuilt = "Lưu phiếu kiểm kho không thành công !"; if (ModelState.IsValid) { using (TransactionScope ts = new TransactionScope()) { int TotalQty = 0; #region lưu WarehouseInventoryMasterModel WarehouseInventoryMasterModel master = new WarehouseInventoryMasterModel() { WarehouseInventoryMasterCode = model.WarehouseInventoryMasterCode, CreatedDate = DateTime.Now, CreatedAccount = currentAccount.UserName, CreatedEmployeeId = currentEmployee.EmployeeId, Note = model.Note, WarehouseId = model.WarehouseId, //TotalQty = detail.Count(), CreatedIEOther = false, Actived = true // TotalQty : Tổng số sản phẩm kiểm kho update sau. }; _context.Entry(master).State = System.Data.Entity.EntityState.Added; _context.SaveChanges();// Lưu để lấy masterID #endregion #region lưu WarehouseInventoryDetailModel if (detail != null) { if (detail.GroupBy(p => p.ProductId).ToList().Count < detail.Count) { //khong duoc trung san pham return(Json("Vui lòng không chọn thông tin sản phẩm trùng nhau !", JsonRequestBehavior.AllowGet)); } foreach (var item in detail) { if (item.ProductId == null) { return(Json("Vui lòng nhập thông tin sản phẩm !", JsonRequestBehavior.AllowGet)); } if (item.ActualInventory == null) { return(Json("Vui lòng nhập thông tin tồn thực tế sản phẩm (" + item.ProductName + ") !", JsonRequestBehavior.AllowGet)); } if (item.EndInventoryQty != item.ActualInventory) { WarehouseInventoryDetailModel p = new WarehouseInventoryDetailModel() { WarehouseInventoryMasterId = master.WarehouseInventoryMasterId, ProductId = item.ProductId, Inventory = item.EndInventoryQty, ActualInventory = item.ActualInventory, AmountDifference = item.AmountDifference, Specifications = item.Specifications, Actived = true }; _context.Entry(p).State = System.Data.Entity.EntityState.Added; _context.SaveChanges(); TotalQty++; } } } else { return(Json("Giá trị tồn hiện tại chưa được sửa đổi !", JsonRequestBehavior.AllowGet)); } #endregion #region Update TotalQty Master master.TotalQty = TotalQty; _context.Entry(master).State = System.Data.Entity.EntityState.Modified; _context.SaveChanges(); #endregion ts.Complete(); return(Json("success", JsonRequestBehavior.AllowGet)); } } return(Json(resuilt, JsonRequestBehavior.AllowGet)); }