public ActionResult Autoimex(int id) { ProductUpdateMasterModel model = _context.ProductUpdateMasterModel .Where(p => p.ProductUpdateMasterId == id && p.Actived == true && p.CreatedIEOther == false) .FirstOrDefault(); var Resuilt = ""; if (model == null) { Resuilt = "Không tìm thấy phiếu cập nhật yêu cầu !"; } else { try { using (TransactionScope ts = new TransactionScope()) { using (System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString)) { using (System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand()) { cmd.CommandText = "usp_UpdateProduct"; cmd.Parameters.AddWithValue("@ProductUpdateMasterId", id); cmd.Connection = conn; cmd.CommandType = CommandType.StoredProcedure; conn.Open(); cmd.ExecuteNonQuery(); conn.Close(); } } ts.Complete(); Resuilt = "success"; } } catch { Resuilt = "Xảy ra lỗi trong quá trình cập nhật sản phẩm !"; } } return(Json(Resuilt, JsonRequestBehavior.AllowGet)); }
public ActionResult Cancel(int id) { ProductUpdateMasterModel model = _context.ProductUpdateMasterModel .Where(p => p.ProductUpdateMasterId == id) .FirstOrDefault(); var Resuilt = ""; if (model == null) { Resuilt = "Không tìm thấy đơn hàng yêu cầu !"; } else { model.Actived = false; _context.Entry(model).State = System.Data.Entity.EntityState.Modified; _context.SaveChanges(); Resuilt = "success"; } return(Json(Resuilt, JsonRequestBehavior.AllowGet)); }
public ActionResult Import(HttpPostedFileBase excelfile) { 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; int TotalQty = 0; #region // Bước 1 : insert ProductUpdateMasterModel ProductUpdateMasterModel master = new ProductUpdateMasterModel() { ProductUpdateMasterCode = GetProductUpdateMasterCode(), CreateDate = DateTime.Now, CreateAccount = currentAccount.UserName, CreatedEmployeeId = currentEmployee.EmployeeId, CreatedIEOther = false, Actived = true }; _context.Entry(master).State = System.Data.Entity.EntityState.Added; _context.SaveChanges();// Lưu để lấy masterID #endregion #region // Bước 2 : insert ProductUpdateMasterModel var list = (from p in _context.ProductModel join v1 in _context.ProductPriceModel on p.ProductId equals v1.ProductId join v2 in _context.ProductPriceModel on p.ProductId equals v2.ProductId join v3 in _context.ProductPriceModel on p.ProductId equals v3.ProductId join v4 in _context.ProductPriceModel on p.ProductId equals v4.ProductId where v1.CustomerLevelId == 1 && v2.CustomerLevelId == 2 && v3.CustomerLevelId == 3 && v4.CustomerLevelId == 4 && p.Actived == true select new { ProductId = p.ProductId, ProductCode = p.ProductCode, ProductStoreCode = p.ProductStoreCode, ProductName = p.ProductName, ImportPrice = p.ImportPrice, ExchangeRate = p.ExchangeRate, ShippingFee = p.ShippingFee, Vip = v1.Price, VipBac = v2.Price, VipVang = v3.Price, VipBachkim = v4.Price }).ToList(); ProductUpdateDetailModel details; for (int row = 7; worksheet.Cells[row, col].Value != null; row++) { #region//Gán giá trị tạm biến số int ProductId = 0; decimal ImportPrice = 0; decimal ShippingFee = 0; decimal ExchangeRate = 0; decimal pprice1 = 0; decimal pprice2 = 0; decimal pprice3 = 0; decimal pprice4 = 0; #endregion #region // Kiểm tra giá trị cột //Mã sản phẩm if (worksheet.Cells[row, 2].Text == "") { return(Json(" Dòng " + row + " Cột 'Mã Sản Phẩm ID' bị rỗng !", JsonRequestBehavior.AllowGet)); } else if (int.TryParse(worksheet.Cells[row, 2].Value.ToString(), out ProductId) == false) { return(Json(" Dòng " + row + " Cột 'Mã Sản Phẩm ID' không phải số !", JsonRequestBehavior.AllowGet)); } //Mã Sản Phẩm Cửa hàng if (worksheet.Cells[row, 3].Text == "") { return(Json(" Dòng " + row + " Cột 'Mã Sản Phẩm Cửa hàng' bị rỗng !", JsonRequestBehavior.AllowGet)); } //Tên Sản Phẩm if (worksheet.Cells[row, 5].Text == "") { return(Json(" Dòng " + row + " Cột 'Tên Sản Phẩm' bị rỗng !", JsonRequestBehavior.AllowGet)); } //Giá nhập if (worksheet.Cells[row, 6].Text == "") { return(Json(" Dòng " + row + " Cột 'Giá nhập' bị rỗng !", JsonRequestBehavior.AllowGet)); } else if (decimal.TryParse(worksheet.Cells[row, 6].Value.ToString(), out ImportPrice) == false) { return(Json(" Dòng " + row + " Cột 'Giá nhập' không phải số !", JsonRequestBehavior.AllowGet)); } //Tỷ giá if (worksheet.Cells[row, 7].Text == "") { return(Json(" Dòng " + row + " Cột 'Tỷ giá' bị rỗng !", JsonRequestBehavior.AllowGet)); } else if (decimal.TryParse(worksheet.Cells[row, 7].Value.ToString(), out ExchangeRate) == false) { return(Json(" Dòng " + row + " Cột 'Tỷ giá' không phải số !", JsonRequestBehavior.AllowGet)); } //Phí vận chuyển if (worksheet.Cells[row, 8].Text == "") { return(Json(" Dòng " + row + " Cột 'Phí vận chuyển' bị rỗng !", JsonRequestBehavior.AllowGet)); } else if (decimal.TryParse(worksheet.Cells[row, 8].Value.ToString(), out ShippingFee) == false) { return(Json(" Dòng " + row + " Cột 'Phí vận chuyển' không phải số !", JsonRequestBehavior.AllowGet)); } //Vip if (worksheet.Cells[row, 9].Text == "") { return(Json(" Dòng " + row + " Cột 'Vip' bị rỗng !", JsonRequestBehavior.AllowGet)); } else if (decimal.TryParse(worksheet.Cells[row, 9].Value.ToString(), out pprice1) == false) { return(Json(" Dòng " + row + " Cột 'Vip' không phải số !", JsonRequestBehavior.AllowGet)); } //Vip-Bạc if (worksheet.Cells[row, 10].Text == "") { return(Json(" Dòng " + row + " Cột 'Vip-Bạc' bị rỗng !", JsonRequestBehavior.AllowGet)); } else if (decimal.TryParse(worksheet.Cells[row, 10].Value.ToString(), out pprice2) == false) { return(Json(" Dòng " + row + " Cột 'Vip-Bạc' không phải số !", JsonRequestBehavior.AllowGet)); } //Vip-Vàng if (worksheet.Cells[row, 11].Text == "") { return(Json(" Dòng " + row + " Cột 'Vip-Vàng' bị rỗng !", JsonRequestBehavior.AllowGet)); } else if (decimal.TryParse(worksheet.Cells[row, 11].Value.ToString(), out pprice3) == false) { return(Json(" Dòng " + row + " Cột 'Vip-Vàng' không phải số !", JsonRequestBehavior.AllowGet)); } //Vip-Bạch kim if (worksheet.Cells[row, 12].Text == "") { return(Json(" Dòng " + row + " Cột 'Vip-Bạch kim' bị rỗng !", JsonRequestBehavior.AllowGet)); } else if (decimal.TryParse(worksheet.Cells[row, 12].Value.ToString(), out pprice4) == false) { return(Json(" Dòng " + row + " Cột 'Vip-Bạch kim' không phải số !", JsonRequestBehavior.AllowGet)); } #endregion #region//Gán giá trị biến string string ProductStoreCode = worksheet.Cells[row, 3].Value.ToString(); string ProductCode = ""; if (worksheet.Cells[row, 4].Text != "") { ProductCode = worksheet.Cells[row, 4].Value.ToString(); } string ProductName = worksheet.Cells[row, 5].Value.ToString(); #endregion foreach (var item in list) { if (item.ProductId == ProductId) { if (item.ProductCode != ProductCode || item.ProductStoreCode != ProductStoreCode || item.ProductName != ProductName || item.ImportPrice != ImportPrice || item.ExchangeRate != ExchangeRate || item.ShippingFee != ShippingFee || item.Vip != pprice1 || item.VipBac != pprice2 || item.VipVang != pprice3 || item.VipBachkim != pprice4) { #region // Update details = new ProductUpdateDetailModel(); details.ProductUpdateMasterId = master.ProductUpdateMasterId; details.ProductId = ProductId; details.ProductStoreCode = ProductStoreCode; details.ProductCode = ProductCode; details.ProductName = ProductName; details.ImportPrice = ImportPrice; details.ExchangeRate = ExchangeRate; details.ShippingFee = ShippingFee; details.Price1 = pprice1; details.Price2 = pprice2; details.Price3 = pprice3; details.Price4 = pprice4; details.Actived = true; _context.Entry(details).State = System.Data.Entity.EntityState.Added; _context.SaveChanges(); #endregion TotalQty++; } } } //ProductModel p = _context.ProductModel.Where(m => m.ProductId == ProductId).FirstOrDefault(); //if (p != null) //{ // decimal? Price1 = _context.ProductPriceModel.Where(c => c.ProductId == ProductId && c.CustomerLevelId == 1).Select(c => c.Price).FirstOrDefault(); // decimal? Price2 = _context.ProductPriceModel.Where(c => c.ProductId == ProductId && c.CustomerLevelId == 2).Select(c => c.Price).FirstOrDefault(); // decimal? Price3 = _context.ProductPriceModel.Where(c => c.ProductId == ProductId && c.CustomerLevelId == 3).Select(c => c.Price).FirstOrDefault(); // decimal? Price4 = _context.ProductPriceModel.Where(c => c.ProductId == ProductId && c.CustomerLevelId == 4).Select(c => c.Price).FirstOrDefault(); // if ((p.ProductCode != ProductCode || // p.ProductStoreCode != ProductStoreCode || // p.ProductName != ProductName || // p.ImportPrice != ImportPrice || // p.ExchangeRate != ExchangeRate || // p.ShippingFee != ShippingFee || // Price1 != pprice1 || // Price2 != pprice2 || // Price3 != pprice3 || // Price4 != pprice4)) // { // #region // Update // details = new ProductUpdateDetailModel(); // details.ProductUpdateMasterId = master.ProductUpdateMasterId; // details.ProductId = ProductId; // details.ProductStoreCode = ProductStoreCode; // details.ProductCode = ProductCode; // details.ProductName = ProductName; // details.ImportPrice = ImportPrice; // details.ExchangeRate = ExchangeRate; // details.ShippingFee = ShippingFee; // details.Price1 = pprice1; // details.Price2 = pprice2; // details.Price3 = pprice3; // details.Price4 = pprice4; // details.Actived = true; // _context.Entry(details).State = System.Data.Entity.EntityState.Added; // _context.SaveChanges(); // #endregion // TotalQty++; // } //} } #endregion #region // Bước 3: Cập nhật master master.TotalQty = TotalQty; _context.Entry(master).State = System.Data.Entity.EntityState.Modified; _context.SaveChanges();// Lưu để lấy masterID #endregion ts.Complete(); if (TotalQty == 0) { return(Json("Danh sách sản phẩm chưa được sửa đổi !", JsonRequestBehavior.AllowGet)); } else { return(Json("success", JsonRequestBehavior.AllowGet)); } } } } } catch { return(Json("Lỗi! Vui lòng liên hệ kĩ thuật viên để được giúp đỡ !", JsonRequestBehavior.AllowGet)); } }