public int add(ChiTietSP obj) { this._db.ds_chitietsp.Add(obj); this._db.SaveChanges(); //return ma moi nhat return this._db.ds_chitietsp.Max(x => x.id); }
public Boolean edit(ChiTietSP spt) { if (this.is_exist(spt.id)) { ChiTietSP obj = get_by_id(spt.id); obj.kichthuoc = spt.kichthuoc; obj.mausac = spt.mausac; obj.soluong = spt.soluong; _db.SaveChanges(); return true; } return false; }
public ActionResult ChiTietSP_Delete(int id) { if (!this._nhanvien_permission.Contains("chitietsp_delete")) { return _fail_permission("chitietsp_delete"); } SanPhamController ctr = new SanPhamController(); ChiTietSPController ctr_chitietsp = new ChiTietSPController(ctr._db); ChiTietSP ctsp = new ChiTietSP(); SanPham sanpham = (SanPham)Session["sanpham_new_tmp"]; if (sanpham != null) { //vẫn còn đang ở un_save mode thì xóa từ session ctsp = sanpham.ds_chitietsp.Where(x => x.id == id).FirstOrDefault(); sanpham.ds_chitietsp.Remove(ctsp); return Redirect(Url.Action("Index", "AdminSanPham", new { id = 0, chitietsp_id = ctsp.id }) + "#qd_chitietsp");//return RedirectToAction("Index", "AdminSanPham", new { id = 0, chitietsp_id = ctsp.id }); } //đã save rồi else { ctsp = ctr_chitietsp.get_by_id(id); sanpham = ctr.get_by_id(ctsp.sanpham.id); if (ctsp == null) { return RedirectToAction("Index", "AdminSanPhams"); } //call xóa ctsp try { ctr_chitietsp.delete(ctsp.id); } catch (Exception) { return _show_notification("Chi tiết sản phẩm này có dính khóa ngoại với đơn hàng hiện có nên không xóa được"); } return Redirect(Url.Action("Index", "AdminSanPham", new { id = sanpham.id, chitietsp_id = id }) + "#qd_chitietsp");//return RedirectToAction("Index", "AdminSanPham", new { id = ctsp.sanpham.id, chitietsp_id = ctsp.id }); } }
public ActionResult ChiTietSP_Edit(int id) { if (!this._nhanvien_permission.Contains("chitietsp_edit")) { return _fail_permission("chitietsp_edit"); } SanPhamController ctr = new SanPhamController(); ChiTietSPController ctr_chitietsp = new ChiTietSPController(); ChiTietSP ctsp = new ChiTietSP(); SanPham sanpham =(SanPham) Session["sanpham_new_tmp"]; if (sanpham != null) { //vẫn còn đang ở un_save mode thì lấy từ session ra ctsp = sanpham.ds_chitietsp.Where(x => x.id == id).FirstOrDefault(); return Redirect(Url.Action("Index", "AdminSanPham", new { id = 0, chitietsp_id = ctsp.id }) + "#qd_chitietsp");//return RedirectToAction("Index", "AdminSanPham", new { id = 0, chitietsp_id = ctsp.id }); } //đã save rồi else { ctsp = ctr_chitietsp.get_by_id(id); sanpham = ctr.get_by_id(ctsp.sanpham.id); if (ctsp == null) { return RedirectToAction("Index", "AdminSanPhams"); } return Redirect(Url.Action("Index", "AdminSanPham", new { id = ctsp.sanpham.id, chitietsp_id = ctsp.id }) + "#qd_chitietsp");//return RedirectToAction("Index", "AdminSanPham", new { id = ctsp.sanpham.id, chitietsp_id = ctsp.id }); } }
//WORK OK public ActionResult Index(int id = 0, int chitietsp_id = 0) { if (!this._nhanvien_permission.Contains("sanpham_view")) { return _fail_permission("sanpham_view"); } SanPhamController ctr = new SanPhamController(); SanPham obj = ctr.get_by_id(id); Boolean un_save = false; if (obj == null)//tuwsc laf id=0, ddang owr chees dooj SANPHAM chuwa luuw { //lay tu session ra if (Session != null && Session["sanpham_new_tmp"] != null) { obj = (SanPham)Session["sanpham_new_tmp"]; } else { //Chưa gọi set session trước return RedirectToAction("Index", "AdminSanPhams"); } un_save = true; } else { //một khi đã hiển thị sản phẩm đã được save thì xóa ngay và luôn session if (Session != null && Session["sanpham_new_tmp"] != null) { Session["sanpham_new_tmp"] = null; } } ViewBag.SanPham = obj; //ON ActionEx... ViewBag.Title += " - View"; ChiTietSPController ctr_chitietsp = new ChiTietSPController(ctr._db); ChiTietSP ctsp; if (un_save) { if (obj.ds_chitietsp.Where(x => x.id == chitietsp_id).FirstOrDefault()!=null) { //lay tư trong session ra ctsp = obj.ds_chitietsp.Where(x => x.id == chitietsp_id).FirstOrDefault(); } else { ctsp = new ChiTietSP(); } } else { ctsp = ctr_chitietsp.get_by_id(chitietsp_id); } ViewBag.ChiTietSP = ctsp == null ? new ChiTietSP() : ctsp; //từ bên controller trước đưa sang this._state.AddRange(this._get_state_tempdata()); ViewBag.State = this._state; return View(); }
public ActionResult ChiTietSP_Submit() { int sanpham_id = TextLibrary.ToInt(Request["sanpham_id"]); //must use same dbcontext neu khong se bao loi (chi can thiet khi co cap nhat thao tac tren doi tuong sanpham trong session) SanPhamController ctr = new SanPhamController(this._get_current_dbcontext()); ChiTietSPController ctr_chitietsp = new ChiTietSPController(ctr._db); KichThuocController ctr_kichthuoc = new KichThuocController(ctr._db); MauSacController ctr_mausac = new MauSacController(ctr._db); SanPham sanpham_obj = ctr.get_by_id(sanpham_id); Boolean un_save_mode = false; //neu sanpham_id=0, dang o mode chua save if (sanpham_obj == null) { sanpham_obj = (SanPham)Session["sanpham_new_tmp"]; un_save_mode = true; } //create new ChiTietSP obj; int chitietsp_id = TextLibrary.ToInt(Request["chitietsp_id"]); Boolean edit_mode; if (un_save_mode) { edit_mode = sanpham_obj.ds_chitietsp.Where(x=>x.id==chitietsp_id).FirstOrDefault()!=null?true:false; if (edit_mode) { if (!this._nhanvien_permission.Contains("chitietsp_edit")) { return _fail_permission("chitietsp_edit"); } obj = sanpham_obj.ds_chitietsp.Where(x => x.id==chitietsp_id).FirstOrDefault(); } else { if (!this._nhanvien_permission.Contains("chitietsp_add")) { return _fail_permission("chitietsp_add"); } obj = new ChiTietSP(); //kiểm tra phải có ít nhất 1 kích thước if (ctr_kichthuoc.timkiem_count("", "", "", "1") <= 0) { return _show_notification("Yêu cầu phải có ít nhất 1 kích thước active mới thêm được chi tiết sản phẩm"); } //kiểm tra phải có ít nhất 1 màu sắc if (ctr_mausac.timkiem_count("", "", "", "1") <= 0) { return _show_notification("Yêu cầu phải có ít nhất 1 màu sắc active mới thêm được chi tiết sản phẩm"); } } } //chế dộ đã save rồi else { edit_mode = ctr_chitietsp.get_by_id(chitietsp_id) != null; if (edit_mode) { if (!this._nhanvien_permission.Contains("chitietsp_edit")) { return _fail_permission("chitietsp_edit"); } obj = ctr_chitietsp.get_by_id(chitietsp_id); } else { if (!this._nhanvien_permission.Contains("chitietsp_add")) { return _fail_permission("chitietsp_add"); } obj = new ChiTietSP(); //kiểm tra phải có ít nhất 1 kích thước if (ctr_kichthuoc.timkiem_count("", "", "", "1") <= 0) { return _show_notification("Yêu cầu phải có ít nhất 1 kích thước active mới thêm được chi tiết sản phẩm"); } //kiểm tra phải có ít nhất 1 màu sắc if (ctr_mausac.timkiem_count("", "", "", "1") <= 0) { return _show_notification("Yêu cầu phải có ít nhất 1 màu sắc active mới thêm được chi tiết sản phẩm"); } } } //assign data obj.soluong = TextLibrary.ToInt(Request["chitietsp_soluong"]); //ctr_kichthuoc = new KichThuocController(ctr._db); obj.kichthuoc = ctr_kichthuoc.get_by_id(TextLibrary.ToInt(Request["chitietsp_kichthuoc_id"])); //ctr_mausac = new MauSacController(ctr._db); obj.mausac = ctr_mausac.get_by_id(TextLibrary.ToInt(Request["chitietsp_mausac_id"])); obj.active = TextLibrary.ToBoolean(Request["chitietsp_active"]); if (un_save_mode) { if (edit_mode) { //do nothing } else { //add to sanpham tmp //must set id try { obj.id = sanpham_obj.ds_chitietsp.Max(x => x.id) + 1; } catch (Exception) { obj.id = 1; } sanpham_obj.ds_chitietsp.Add(obj); //re assign to session Session["sanpham_new_tmp"] = sanpham_obj; } } //chế độ đã save rồi else { if (edit_mode) { //nothing } else { sanpham_obj.ds_chitietsp.Add(obj); } //finally call update ctr._db.SaveChanges(); } return Redirect(Url.Action("Index", "AdminSanPham", new { id = sanpham_id }) + "#qd_ds_chitietsp"); //return RedirectToAction("Index", "AdminSanPham", new { id = sanpham_id }); }