public void XoaSanPham(SanPham sanpham) { var hinhanhs = db.HinhAnhs.ToList().Where(h => h.SanPham == sanpham); if (hinhanhs != null) { foreach (var hinhanh in hinhanhs) { string anhgoc = hinhanh.AnhGoc; XoaAnhTrongThuMuc(anhgoc); string thumbnail = hinhanh.Thumbnail; XoaAnhTrongThuMuc(thumbnail); db.HinhAnhs.Remove(hinhanh); db.SaveChanges(); } } string hinhdaidien = sanpham.HinhDaiDien; XoaAnhTrongThuMuc(hinhdaidien); string thumbnailsp = sanpham.Thumbnail; XoaAnhTrongThuMuc(thumbnailsp); db.SanPhams.Remove(sanpham); db.SaveChanges(); }
public ActionResult ThemMoi(SanPham sanpham, string luu, FormCollection collection) { ViewBag.Loi = TempData["Loi"]; if (db.SanPhams.ToList().Any(s => s.MaSP == sanpham.MaSP)) { ModelState.AddModelError("MaSP", "Mã sản phẩm đã tồn tại trong CSDL"); } if (ModelState.IsValid) { var image = WebImage.GetImageFromRequest("anhdaidien"); if (image != null) { string ImageName = Path.GetFileName(image.FileName); ImageName = EditString.BoDauTrenChuoi(ImageName); //anh goc ImageName = "main" + Utilities.EditString.BoDauTrenChuoi(System.DateTime.Now.ToString()) + "-" + ImageName; var pathToSave = Path.Combine(Server.MapPath("~/Content/Images/SanPham"), ImageName); image.Save(pathToSave); sanpham.HinhDaiDien = ImageName; //thumbnail/ ImageName = "thumbnail-" + Utilities.EditString.BoDauTrenChuoi(System.DateTime.Now.ToString()) + "-" + ImageName; var pathToSaveThumbnail = Path.Combine(Server.MapPath("~/Content/Images/SanPham"), ImageName); image.Resize(250, 250, true, true); image.Save(pathToSaveThumbnail); sanpham.Thumbnail = ImageName; } else { sanpham.HinhDaiDien = "noimageyet.jpg"; sanpham.Thumbnail = "thumb-noimageyet.jpg"; } db.SanPhams.Add(sanpham); db.SaveChanges(); LuuChiTietAnh(sanpham, 10); string tungay = collection["from"]; string denngay = collection["to"]; if (!String.IsNullOrEmpty(tungay) && !String.IsNullOrEmpty(denngay)) { LuuKhuyenMai(sanpham, collection); } if ("Lưu và đóng".Equals(luu, StringComparison.OrdinalIgnoreCase)) { return RedirectToAction("DanhSach"); } if ("Lưu và tạo mới".Equals(luu, StringComparison.OrdinalIgnoreCase)) { return RedirectToAction("ThemMoi"); } } ViewBag.IdThuongHieu = new SelectList(db.ThuongHieus.ToList(), "IdThuongHieu", "TenThuongHieu"); ViewBag.IdDanhMuc = new SelectList(DropdownListDanhMuc(), "Value", "Text"); return View(sanpham); }
public ActionResult Sua(SanPham sanpham, string sua, FormCollection collection) { if (!Models.User.CurrentUser.Quyen.CoChucNang("sua_san_pham")) { return RedirectToAction("Error", "Admin"); } if ("Upload".Equals(sua, StringComparison.OrdinalIgnoreCase)) { //LuuChiTietAnh(sanpham,(10 - (db.HinhAnhs.ToList().Where(h => h.SanPham == sanpham).Count()))); var sp = db.SanPhams.SingleOrDefault(s => s.IdSanPham == sanpham.IdSanPham); LuuChiTietAnh(sp, (10 - (db.HinhAnhs.ToList().Where(h => h.SanPham == sp).Count()))); ViewBag.HinhAnh = db.HinhAnhs.ToList().Where(h => h.SanPham == sp); ViewBag.SoHinhCoTheUp = 10 - (db.HinhAnhs.ToList().Where(h => h.SanPham == sp).Count()); ViewBag.IdDanhMuc = new SelectList(DropdownListDanhMuc(), "Value", "Text", sanpham.IdDanhMuc); ViewBag.IdThuongHieu = new SelectList(db.ThuongHieus.ToList(), "IdThuongHieu", "TenThuongHieu", sanpham.IdDanhMuc); return View(sanpham); } if (ModelState.IsValid) { var image = WebImage.GetImageFromRequest("anhdaidien"); if (image != null) { string ImageName = Path.GetFileName(image.FileName); ImageName = EditString.BoDauTrenChuoi(ImageName); //anh goc ImageName = "main-" + Utilities.EditString.BoDauTrenChuoi(System.DateTime.Now.ToString()) + "-" + ImageName; var pathToSave = Path.Combine(Server.MapPath("~/Content/Images/SanPham"), ImageName); image.Save(pathToSave); sanpham.HinhDaiDien = ImageName; //thumbnail ImageName = "thumbnail-" + Utilities.EditString.BoDauTrenChuoi(System.DateTime.Now.ToString()) + "-" + ImageName; var pathToSaveThumbnail = Path.Combine(Server.MapPath("~/Content/Images/SanPham"), ImageName); image.Resize(250, 250, true, true); image.Save(pathToSaveThumbnail); sanpham.Thumbnail = ImageName; } //else //{ // sanpham.HinhDaiDien = "noimageyet.jpg"; // sanpham.Thumbnail = "thumb-noimageyet.jpg"; //} db.Entry(sanpham).State = EntityState.Unchanged; db.Entry(sanpham).State = EntityState.Modified; db.SaveChanges(); if ("Lưu".Equals(sua, StringComparison.OrdinalIgnoreCase)) { SuaKhuyenMai(collection); return RedirectToAction("Sua", new { id = sanpham.IdSanPham }); } if ("Lưu và đóng".Equals(sua, StringComparison.OrdinalIgnoreCase)) { return RedirectToAction("DanhSach"); } if ("Lưu và tiếp tục".Equals(sua, StringComparison.OrdinalIgnoreCase)) { return RedirectToAction("Sua", new { id = sanpham.IdSanPham }); } } ViewBag.HinhAnh = db.HinhAnhs.ToList().Where(h => h.SanPham == sanpham); ViewBag.SoHinhCoTheUp = 10 - (db.HinhAnhs.ToList().Where(h => h.SanPham == sanpham).Count()); ViewBag.IdDanhMuc = new SelectList(DropdownListDanhMuc(), "Value", "Text", sanpham.IdDanhMuc); ViewBag.IdThuongHieu = new SelectList(db.ThuongHieus.ToList(), "IdThuongHieu", "TenThuongHieu", sanpham.IdDanhMuc); return View(sanpham); }
public void LuuKhuyenMai(SanPham sanpham, FormCollection collection) { string tungay = collection["from"]; string denngay = collection["to"]; string giam = collection["giamgia"]; SanPham sp = db.SanPhams.SingleOrDefault(s => s.IdSanPham == sanpham.IdSanPham); KhuyenMai khuyenmai = new KhuyenMai { TuNgay = DateTime.ParseExact(tungay, "dd/MM/yyyy", null), DenNgay = DateTime.ParseExact(denngay, "dd/MM/yyyy", null), PhanTramGiam = giam != null ? int.Parse(giam.ToString()) : 0, SanPham=sp }; db.KhuyenMais.Add(khuyenmai); db.SaveChanges(); }
public void LuuChiTietAnh(SanPham sanpham, int number) { for (int i = 100; i < 111; i++) { var image = WebImage.GetImageFromRequest("FileUpLoad" + i); if (image != null) { string ImageName = Path.GetFileName(image.FileName); string main = "main-" + Utilities.EditString.BoDauTrenChuoi(System.DateTime.Now.ToString()) + "-" + ImageName; var pathToSave = Path.Combine(Server.MapPath("~/Content/Images/SanPham"), main); image.Save(pathToSave); string thumbnail = "thumbnail-" + Utilities.EditString.BoDauTrenChuoi(System.DateTime.Now.ToString()) + "-" + ImageName; var pathToSaveThumbnail = Path.Combine(Server.MapPath("~/Content/Images/SanPham"), thumbnail); image.Resize(250, 250, true, true); image.Save(pathToSaveThumbnail); HinhAnh hinh = new HinhAnh { SanPham = sanpham, AnhGoc = main, Thumbnail = thumbnail }; db.HinhAnhs.Add(hinh); db.SaveChanges(); } } }
public void XuLiTonKho(SanPham sanpham, int soluong) { var tonkho = db.TonKhos.Where(t => t.IdSanPham == sanpham.IdSanPham).ToList().OrderByDescending(t => t.Id).FirstOrDefault(); int soluongmoi = soluong; if (tonkho != null) { soluongmoi = tonkho.SoLuong + soluong; } TonKho tonkhomoi = new TonKho { SanPham = sanpham, SoLuong = soluongmoi, ThoiGian = System.DateTime.Now,Xuat=0,Nhap=soluong }; db.TonKhos.Add(tonkhomoi); db.SaveChanges(); }