/// <summary> /// method for update entity /// </summary> /// <param name="product"></param> /// <param name="lsProductMaterial"></param> /// <returns></returns> private bool MethodForUpdateEntity(TBL_PRODUCT product, List <TBL_PRODUCT_MATERIAL> lsProductMaterial) { try { using (var scope = new TransactionScope()) { //update entity int resultExcuteRow = DA_Food.Instance.Update(product); bool resultExucte = true; if (resultExcuteRow > 0 && lsProductMaterial != null && lsProductMaterial.Count > 0) { lsProductMaterial = setProductIdForListProductMaterial(lsProductMaterial, product.ProductID); //get list object old of entity product List <TBL_PRODUCT_MATERIAL> lsProductMaterialOld = DA_Product_Material.Instance.getEntityBaseIdProduct(product.ProductID); lsProductMaterialOld = lsProductMaterialOld == null ? new List <TBL_PRODUCT_MATERIAL>() : lsProductMaterialOld; // list productMaterial old List <TBL_PRODUCT_MATERIAL> lsProductMaterialNew = lsProductMaterial == null ? new List <TBL_PRODUCT_MATERIAL>() : lsProductMaterial; // productMaterial new if (lsProductMaterialNew.Count > 0 && lsProductMaterialOld.Count == 0) { resultExucte = DA_Product_Material.Instance.Insert(lsProductMaterialNew) > 0 ? true : false; } if (lsProductMaterialNew.Count == 0 && lsProductMaterialOld.Count > 0) { resultExucte = DA_Product_Material.Instance.deleteListEntity(lsProductMaterialOld); } if (lsProductMaterialNew.Count > 0 && lsProductMaterialOld.Count > 0) { List <int> lsProductIdNew = lsProductMaterialNew.Select(n => n.ProductMaterialID).ToList(); List <int> lsProductIdOld = lsProductMaterialOld.Select(n => n.ProductMaterialID).ToList(); //array productId List <int> productIdDelete = lsProductIdOld.Except(lsProductIdNew).ToList(); List <int> productIdUpdate = lsProductIdNew.Where(n => lsProductIdOld.Any(p => p == n)).ToList(); //item exist array old not exist array new is array delete List <TBL_PRODUCT_MATERIAL> lsDelete = lsProductMaterialOld.Where(n => productIdDelete.Any(p => p == n.ProductMaterialID)).ToList(); //item exist array new not exist array old is array delete List <TBL_PRODUCT_MATERIAL> lsInsert = lsProductMaterialNew.Where(n => n.ProductMaterialID == 0).ToList(); // item exist array new and array old is array insert List <TBL_PRODUCT_MATERIAL> lsUpdate = lsProductMaterialNew.Where(n => productIdUpdate.Any(p => p == n.ProductMaterialID)).ToList(); //excute if (lsDelete != null && lsDelete.Count > 0) { resultExucte = DA_Product_Material.Instance.deleteListEntity(lsDelete); } if (lsInsert != null && lsInsert.Count > 0 && resultExucte) { resultExucte = DA_Product_Material.Instance.Insert(lsInsert) > 0 ? true : false; } if (lsUpdate != null && lsUpdate.Count > 0 && resultExucte) { resultExucte = DA_Product_Material.Instance.Update(lsUpdate) > 0 ? true : false; } } } scope.Complete(); return(resultExucte); } } catch (Exception ex) { } return(false); }
/// <summary> /// method for update entity /// </summary> /// <param name="product"></param> /// <param name="lsProductMaterial"></param> /// <returns></returns> //private bool MethodForUpdateEntity(TBL_PRODUCT product, List<TBL_PRODUCT_MATERIAL> lsProductMaterial) //{ // try // { // using (var scope = new TransactionScope()) // { // //update entity // int resultExcuteRow = DA_Food.Instance.Update(product); // bool resultExucte = resultExcuteRow == 0 ? false : true; // if (resultExucte) // { // // setProductIdForListProductMaterial(ref lsProductMaterial, product.ProductID); // //get list object old of entity product // List<TBL_PRODUCT_MATERIAL> lsProductMaterialOld = DA_Product_Material.Instance.getEntityBaseIdProduct(product.ProductID); // lsProductMaterialOld = lsProductMaterialOld == null ? new List<TBL_PRODUCT_MATERIAL>() : lsProductMaterialOld; // list productMaterial old // List<TBL_PRODUCT_MATERIAL> lsProductMaterialNew = lsProductMaterial == null ? new List<TBL_PRODUCT_MATERIAL>() : lsProductMaterial;// productMaterial new // if (lsProductMaterialNew.Count > 0 && lsProductMaterialOld.Count == 0) // { // resultExucte = DA_Product_Material.Instance.Insert(lsProductMaterialNew) > 0 ? true : false; // } // if (lsProductMaterialNew.Count == 0 && lsProductMaterialOld.Count > 0) // { // resultExucte = DA_Product_Material.Instance.deleteListEntity(lsProductMaterialOld); // } // if (lsProductMaterialNew.Count > 0 && lsProductMaterialOld.Count > 0) // { // List<int> lsProductIdNew = lsProductMaterialNew.Select(n => n.ProductMaterialID).ToList(); // List<int> lsProductIdOld = lsProductMaterialOld.Select(n => n.ProductMaterialID).ToList(); // //array productId // List<int> productIdDelete = lsProductIdOld.Except(lsProductIdNew).ToList(); // List<int> productIdUpdate = lsProductIdNew.Where(n => lsProductIdOld.Any(p => p == n)).ToList(); // //item exist array old not exist array new is array delete // List<TBL_PRODUCT_MATERIAL> lsDelete = lsProductMaterialOld.Where(n => productIdDelete.Any(p => p == n.ProductMaterialID)).ToList(); // //item exist array new not exist array old is array delete // List<TBL_PRODUCT_MATERIAL> lsInsert = lsProductMaterialNew.Where(n => n.ProductMaterialID == 0).ToList(); // // item exist array new and array old is array insert // List<TBL_PRODUCT_MATERIAL> lsUpdate = lsProductMaterialNew.Where(n => productIdUpdate.Any(p => p == n.ProductMaterialID)).ToList(); // //excute // if (lsDelete != null && lsDelete.Count > 0) // { // resultExucte = DA_Product_Material.Instance.deleteListEntity(lsDelete); // } // if (lsInsert != null && lsInsert.Count > 0 && resultExucte) // { // resultExucte = DA_Product_Material.Instance.Insert(lsInsert) > 0 ? true : false; // } // if (lsUpdate != null && lsUpdate.Count > 0 && resultExucte) // { // resultExucte = DA_Product_Material.Instance.Update(lsUpdate) > 0 ? true : false; // } // } // } // scope.Complete(); // return resultExucte; // } // } // catch (Exception ex) { } // return false; //} private bool MethodForUpdateEntity(TBL_PRODUCT product, List <TBL_PRODUCT_MATERIAL> lsProductMaterial) { try { using (var scope = new TransactionScope()) { int resultExcuteRow = DA_Food.Instance.Update(product); bool resultExucte = resultExcuteRow == 0 ? false : true; if (resultExucte) { var lsProductMaterialOld = DA_Product_Material.Instance.getEntityBaseIdProduct(product.ProductID); if (lsProductMaterialOld != null && lsProductMaterialOld.Count > 0) { DA_Product_Material.Instance.deleteListEntity(lsProductMaterialOld); } if (lsProductMaterial != null && lsProductMaterial.Count > 0) { DA_Product_Material.Instance.Insert(lsProductMaterial); } } scope.Complete(); return(resultExucte); } } catch (Exception ex) { return(false); } }
public JsonResult DeleteMultiProduct(string lstProductID) { bool flag = true; var lstID = JArray.Parse(lstProductID); foreach (var item in lstID) { if (!DA_Product.Instance.CheckBeforeDelete(int.Parse(item.ToString()))) { flag = false; break; } } if (flag == true) { // Tạo List Old Product List <TBL_PRODUCT> lstOldProduct = new List <TBL_PRODUCT>(); foreach (var item in lstID) { // Add Vào List Old Product TBL_PRODUCT oldProduct = DA_Product.Instance.GetById(int.Parse(item.ToString())); lstOldProduct.Add(oldProduct); DA_Product.Instance.Delete(int.Parse(item.ToString())); } // Ghi log WriteLog("Product", "MultiDelete", lstOldProduct, null); } return(Json(flag, JsonRequestBehavior.AllowGet)); }
public JsonResult SaveData(TBL_PRODUCT item, FormCollection fc) { string flag = ""; try { ViewBag.ListProductType = DA_ProductType.Instance.GetComboboxProductType(); string a = fc["hdImage"]; if (!string.IsNullOrEmpty(a)) { string[] lA = a.Split(','); byte[] byteArray = Convert.FromBase64String(lA[1]); //byteArray = Utils.Globals.Resize2Max50Kbytes(byteArray); item.ImageProduct = byteArray; } DA_Product.Instance.Insert(item); // Ghi log WriteLog("Product", "Create", null, item); flag = "1"; return(Json(flag, JsonRequestBehavior.AllowGet)); } catch { flag = "-1"; throw; } }
public ActionResult Edit(int ProductID) { ViewBag.ListProductType = DA_ProductType.Instance.GetComboboxProductType(); TBL_PRODUCT cus = DA_Product.Instance.GetById(ProductID); if (cus == null) { return(HttpNotFound()); } return(View(cus)); }
public ActionResult ProductEdit(TBL_PRODUCT tbl, HttpPostedFileBase file) { string pic = null; if (file != null) { pic = System.IO.Path.GetFileName(file.FileName); string path = System.IO.Path.Combine(Server.MapPath("~/ProductImg/"), pic); file.SaveAs(path); } tbl.PRODUCTIMAGE = file != null?pic:tbl.PRODUCTIMAGE; tbl.MODIFIEDDATE = DateTime.Now; _unitOfWork.GetRepositoryInstance <TBL_PRODUCT>().Update(tbl); return(RedirectToAction("Product")); }
public ActionResult addToCart1(TBL_PRODUCT pl, string qty, int id) { TBL_PRODUCT p = db.TBL_PRODUCT.Where(x => x.PROD_ID == id).SingleOrDefault(); cart c = new cart(); c.productid = p.PROD_ID; c.productname = p.PROD_NAME; c.price = (float)p.PROD_PRICE; c.qty = Convert.ToInt32(qty); c.bill = c.price * c.qty; if (TempData["cart"] == null) { adcart.Add(c); TempData["cart"] = adcart; } else { List <cart> adcart2 = TempData["cart"] as List <cart>; int flag = 0; foreach (var item in adcart2) { if (item.productid == c.productid) { item.qty += c.qty; item.bill += c.bill; flag = 1; } } if (flag == 0) { adcart2.Add(c); //here item is new it will append to list } TempData["cart"] = adcart2; } TempData.Keep(); return(RedirectToAction("Index")); }
public JsonResult AddOrUpdateEntity(TBL_PRODUCT product, List <TBL_PRODUCT_MATERIAL> productDetail, bool isEdit) { try { if (!string.IsNullOrWhiteSpace(product.ProductName)) { if (!isEdit) { return(Json(MethodForInsertEntity(product, productDetail) ? 1 : 0)); } else { return(Json(MethodForUpdateEntity(product, productDetail) ? 1 : 0)); } } } catch (Exception ex) { } return(Json(0)); }
public JsonResult DeleteProduct(int ProductID) { bool flag = true; if (!DA_Product.Instance.CheckBeforeDelete(ProductID)) { flag = false; } else { // Ghi log TBL_PRODUCT oldProduct = DA_Product.Instance.GetById(ProductID); WriteLog("Product", "Delete", oldProduct, null); DA_Product.Instance.Delete(ProductID); flag = true; } return(Json(flag, JsonRequestBehavior.AllowGet)); }
/// <summary> /// method use for insert entity /// </summary> /// <param name="product"></param> /// <param name="lsProductMaterial"></param> /// <returns></returns> private bool MethodForInsertEntity(TBL_PRODUCT product, List <TBL_PRODUCT_MATERIAL> lsProductMaterial) { try { using (var scope = new TransactionScope()) { //insert entity int resultExcuteRow = DA_Food.Instance.Insert(product); bool resultExucte = true; //insert list entity tbl_product_material if (resultExcuteRow > 0 && lsProductMaterial != null && lsProductMaterial.Count > 0) { lsProductMaterial = setProductIdForListProductMaterial(lsProductMaterial, product.ProductID); resultExucte = DA_Product_Material.Instance.Insert(lsProductMaterial) > 0 ? true : false; } return(resultExucte); } } catch (Exception ex) { } return(false); }
public ActionResult Edit(TBL_PRODUCT item, FormCollection fc) { try { ViewBag.ListProductType = DA_ProductType.Instance.GetComboboxProductType(); string a = fc["hdImage"]; if (!string.IsNullOrEmpty(a)) { string[] lA = a.Split(','); byte[] byteArray = Convert.FromBase64String(lA[1]); item.ImageProduct = byteArray; } DA_Product.Instance.Update(item); // Ghi log TBL_PRODUCT oldProduct = DA_Product.Instance.GetById(item.ProductID); WriteLog("Product", "Edit", oldProduct, item); return(RedirectToAction("Index")); } catch { return(View()); } }
/// <summary> /// method use for insert entity /// </summary> /// <param name="product"></param> /// <param name="lsProductMaterial"></param> /// <returns></returns> private bool MethodForInsertEntity(TBL_PRODUCT product, List <TBL_PRODUCT_MATERIAL> lsProductMaterial) { try { using (var scope = new TransactionScope()) { //insert entity int resultExcuteRow = DA_Food.Instance.Insert(product); bool resultExucte = resultExcuteRow == 0 ? false : true; //insert list entity tbl_product_material if (resultExucte && lsProductMaterial != null) { // setProductIdForListProductMaterial(ref lsProductMaterial, product.ProductID); int result = DA_Product_Material.Instance.Insert(lsProductMaterial); resultExucte = (result > 0 || (result == 0 && lsProductMaterial == null)) ? true : false; } scope.Complete(); return(resultExucte); } } catch (Exception ex) { } return(false); }
public JsonResult deleteMany(List <int> lsIdItem) { try { using (var scope = new TransactionScope()) { foreach (var id in lsIdItem) { if (!DA_Food.Instance.checkProduct(Convert.ToInt32(id))) { DA_Food.Instance.Delete(Convert.ToInt32(id)); } else { TBL_PRODUCT item = DA_Food.Instance.GetById(Convert.ToInt32(id)); throw new Exception("Món ăn " + item.ProductName + " đang sử dụng"); } } scope.Complete(); return(Json(1)); } } catch (Exception ex) { return(Json(ex.Message)); } }
public ActionResult addToCart1(int?id) { TBL_PRODUCT p = db.TBL_PRODUCT.Where(x => x.PROD_ID == id).SingleOrDefault(); return(View(p)); }
public ActionResult Index(HttpPostedFileBase fileUpload, IEnumerable <HttpPostedFileBase> fileUploadAll) { if (!CheckPermission("Product", 1)) { return(RedirectToAction("Login", "Login", "Login")); } #region FileUpload if (fileUpload != null) { string path = ""; OleDbConnection connection = new OleDbConnection(); try { var file = Path.GetFileName(fileUpload.FileName); if (fileUpload != null && fileUpload.ContentLength > 0) { path = Path.Combine(Server.MapPath(@"Content/Excel/"), file); fileUpload.SaveAs(path); string excelConnectionString = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=Excel 8.0", path); connection.ConnectionString = excelConnectionString; OleDbCommand command = new OleDbCommand("select * from [Product$]", connection); OleDbCommand command_2 = new OleDbCommand("select Distinct(ProductCode) from [Product$]", connection); connection.Open(); OleDbDataAdapter oleAdapter = new OleDbDataAdapter(); oleAdapter.SelectCommand = command; OleDbDataAdapter oleAdapter_2 = new OleDbDataAdapter(); oleAdapter_2.SelectCommand = command_2; // Sao chép các dòng dữ liệu từ file excel vào Datatable dt = new DataTable(); oleAdapter.FillSchema(dt, SchemaType.Source); oleAdapter.Fill(dt); dt_2 = new DataTable(); oleAdapter_2.FillSchema(dt_2, SchemaType.Source); oleAdapter_2.Fill(dt_2); // Đóng các kết nối lại command.Dispose(); command = null; oleAdapter.Dispose(); oleAdapter = null; command_2.Dispose(); command_2 = null; oleAdapter_2.Dispose(); oleAdapter_2 = null; connection.Dispose(); connection = null; if (System.IO.File.Exists(path)) { System.IO.File.Delete(path); } ArrayList arr = new ArrayList(); string flag = "1"; string errorProductCodeNull = ""; string errorProductCodeExist = ""; string errorProductNameNull = ""; string errorProductNameLenght = ""; string errorProductTypeNull = ""; string errorProductTypeNotExist = ""; string errorDescriptionLenght = ""; //Kiểm tra null và độ dài for (int i = 0; i < dt.Rows.Count; i++) { if (string.IsNullOrEmpty(dt.Rows[i][0].ToString().Trim())) { errorProductCodeNull += (i + 2) + ", "; flag = "-1"; } if (string.IsNullOrEmpty(dt.Rows[i][1].ToString().Trim())) { errorProductNameNull += (i + 2) + ", "; flag = "-1"; } if (dt.Rows[i][1].ToString().Trim().Length > 150) { errorProductNameLenght += (i + 2) + ", "; flag = "-1"; } if (string.IsNullOrEmpty(dt.Rows[i][2].ToString().Trim())) { errorProductTypeNull += (i + 2) + ", "; flag = "-1"; } if (dt.Rows[i][3].ToString().Trim().Length > 200) { errorDescriptionLenght += (i + 2) + ", "; flag = "-1"; } } //------ //Kiểm tra trùng product code if (dt.Rows.Count > dt_2.Rows.Count && flag == "1") { string lineSameProductCode = ""; int count = 0; for (int i = 0; i < dt.Rows.Count; i++) { for (int j = 0; j < dt.Rows.Count; j++) { if (dt.Rows[i]["ProductCode"].ToString() == dt.Rows[j]["ProductCode"].ToString() && dt.Rows[i]["ProductCode"].ToString() != "") { if (i != j) { lineSameProductCode += (i + 2) + ", " + (j + 2) + ", "; arr.Add((i + 2) + "," + (j + 2)); count++; } } } } if (count > 1) { errorProductCodeExist += lineSameProductCode; flag = "-1"; } } //------ //Kiểm tra Product Type if (flag == "1") { for (int i = 0; i < dt.Rows.Count; i++) { SYS_PRODUCT_TYPE ProductType = DA_ProductType.Instance.GetAll().FirstOrDefault(x => x.ProductTypeCode == dt.Rows[i][2].ToString()); if (ProductType == null) { errorProductTypeNotExist += (i + 2) + ", "; } } } //------ if (flag == "-1") { ViewBag.errorDetails = "<div class='alert alert-danger alert-dismissable'>" + "<p>" + (errorProductCodeNull != "" ? "Mã món không được bỏ trống dòng thứ: " + errorProductCodeNull : "") + "</p>" + "<p>" + (errorProductCodeExist != "" ? "Trùng Product Code dòng thứ: " + errorProductCodeExist : "") + "</p>" + "<p>" + (errorProductNameNull != "" ? "Tên món không được bỏ trống dòng thứ: " + errorProductNameNull : "") + "</p>" + "<p>" + (errorProductNameLenght != "" ? "Tên món không được quá 150 ký tự dòng thứ: " + errorProductNameLenght : "") + "</p>" + "<p>" + (errorProductTypeNull != "" ? "Mã loại món không được bỏ trống dòng thứ: " + errorProductTypeNull : "") + "</p>" + "<p>" + (errorProductTypeNotExist != "" ? "Mã loại món không hợp lệ dòng thứ: " + errorProductTypeNotExist : "") + "</p>" + "<p>" + (errorDescriptionLenght != "" ? "Mô tả không được quá 200 ký tự dòng thứ: " + errorDescriptionLenght : "") + "</p>" + "</div>"; } if (flag == "1") { for (int i = 0; i < dt.Rows.Count; i++) { if (!string.IsNullOrEmpty(dt.Rows[i][0].ToString().Trim())) { int ProductTypeID = DA_ProductType.Instance.GetAll().FirstOrDefault(x => x.ProductTypeCode == dt.Rows[i][2].ToString()).ProductTypeID; TBL_PRODUCT Product = DA_Product.Instance.GetAll().FirstOrDefault(x => x.ProductCode == dt.Rows[i][0].ToString()); TBL_PRODUCT ProductInsert = new TBL_PRODUCT(); ProductInsert.ProductCode = dt.Rows[i][0].ToString(); ProductInsert.ProductName = dt.Rows[i][1].ToString(); ProductInsert.ProductTypeID = ProductTypeID; ProductInsert.Description = dt.Rows[i][3].ToString(); ProductInsert.IsActive = true; if (Product == null) { DA_Product.Instance.Insert(ProductInsert); } else { ProductInsert.ProductID = Product.ProductID; DA_Product.Instance.Update(ProductInsert); } } } } } return(View()); } catch (Exception ex) { ViewBag.errorDetails = "<div class='alert alert-danger alert-dismissable'> (" + ex + ")File excel không hợp lệ, vui lòng kiểm tra lại hoặc có thể tại file mẫu tại đây <a href='Content/Excel/FileMau/01_Import_Product.xlsx'><span class='fa fa-cloud-download'></span></a></div>"; if (connection != null) { connection.Close(); connection.Dispose(); } if (path != "") { if (System.IO.File.Exists(path)) { System.IO.File.Delete(path); } } return(View()); } } #endregion #region FileUploadAll else if (fileUploadAll != null) { string error = ""; foreach (var file in fileUploadAll) { string[] folder = file.FileName.Split('/'); string fileName = folder[folder.Length - 1]; string code = folder[folder.Length - 1].Split('.')[0]; TBL_PRODUCT tblProduct = DA_Product.Instance.GetAll().FirstOrDefault(x => x.ProductCode == code); if (tblProduct != null) { string path = Path.Combine(Server.MapPath(@"~/Content/Images/Product"), fileName); file.SaveAs(path); byte[] bytesImg = System.IO.File.ReadAllBytes(path); tblProduct.ImageProduct = bytesImg; DA_Product.Instance.Update(tblProduct); System.IO.File.Delete(path); } else { error += file.FileName + ", "; } } if (error != "") { ViewBag.errorDetails = @"<div class='alert alert-danger alert-dismissable'> Những hình ảnh sao đây không tìm thấy mã trong cơ sở dữ liệu:" + error + "</div>"; } } return(View()); #endregion }