public async Task <IActionResult> Edit(int id, [Bind("IdsanPham,TenSp,GiaSp,MoTa,DanhMuc")] Sanpham sanpham)
        {
            if (id != sanpham.IdsanPham)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(sanpham);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!SanphamExists(sanpham.IdsanPham))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            ViewData["DanhMuc"] = new SelectList(_context.Danhmuc, "IddanhMuc", "IddanhMuc", sanpham.DanhMuc);
            return(View(sanpham));
        }
 private void deleteImages(Sanpham sp)
 {
     if (!string.IsNullOrWhiteSpace(sp.Hinh1))
     {
         var oldpath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", sp.Hinh1);
         if (System.IO.File.Exists(oldpath))
         {
             System.IO.File.Delete(oldpath);
         }
     }
     if (!string.IsNullOrWhiteSpace(sp.Hinh2))
     {
         var oldpath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", sp.Hinh2);
         if (System.IO.File.Exists(oldpath))
         {
             System.IO.File.Delete(oldpath);
         }
     }
     if (!string.IsNullOrWhiteSpace(sp.Hinh3))
     {
         var oldpath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", sp.Hinh3);
         if (System.IO.File.Exists(oldpath))
         {
             System.IO.File.Delete(oldpath);
         }
     }
     if (!string.IsNullOrWhiteSpace(sp.Hinh4))
     {
         var oldpath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", sp.Hinh4);
         if (System.IO.File.Exists(oldpath))
         {
             System.IO.File.Delete(oldpath);
         }
     }
 }
        public ActionResult them(FormCollection form, Sanpham sp)
        {
            //int ma = int.Parse(form["Brand"]);
            //ViewBag.Brand= new SelectList(lt.Nhanhieux.ToList(), "Mahieu", "Tenhieu",ma);
            //sp.Mahieu = int.Parse(form["Brand"]);
            int ma      = int.Parse(form["Maloai"]);
            var chilCat = lt.ChildCategories.FirstOrDefault(x => x.ID == ma);
            int?cat     = chilCat.Maloai;

            sp.Maloai = cat;
            if (ModelState.IsValid)
            {
                try
                {
                    var dao = new InsertProduct();
                    //ViewBag.Brand = new SelectList(lt.Nhanhieux.ToList(), "Mahieu", "Tenhieu", ma);
                    // var ht = lt.ChildCategories.ToList();
                    int id = dao.insert(sp);
                    ViewBag.gia = sp.Giaban;
                    ViewBag.gkm = sp.Giakhuyenmai;
                    if (id > 0)
                    {
                        SetAlert("Thêm sản phẩm thành công", "success");
                        return(RedirectToAction("hiensp"));
                    }
                }
                catch (Exception ex)
                {
                    ModelState.AddModelError("", "Thêm không thành công");
                }
            }
            return(View());
        }
Exemple #4
0
        public List <Kichthuoc> LayKichThuoc(int maspp)
        {
            var dbContext = new shopContext();
            var kichthuoc = (from kt in dbContext.Kichthuoc
                             join sp in dbContext.Sanpham
                             on kt.SanPhamId equals sp.SanPhamId
                             where kt.SanPhamId == maspp
                             select new
            {
                SanPhamId = sp.SanPhamId,
                TenSanPham = sp.TenSanPham,
                KichThuocId = kt.KichThuocid,
                TenKichThuoc = kt.TenKichThuoc,
                GiaBanLe = sp.GiaBanLe,
                GiaThem = kt.GiaThem,
            });
            List <Kichthuoc> ListKT = new List <Kichthuoc>();

            foreach (var item in kichthuoc)
            {
                Sanpham sp = new Sanpham();
                sp.SanPhamId  = item.SanPhamId;
                sp.GiaBanLe   = item.GiaBanLe;
                sp.TenSanPham = item.TenSanPham;
                Kichthuoc kt = new Kichthuoc();
                kt.TenKichThuoc = item.TenKichThuoc;
                kt.KichThuocid  = item.KichThuocId;
                kt.GiaThem      = item.GiaThem;
                kt.SanPham      = sp;
                ListKT.Add(kt);
            }

            return(ListKT);
        }
        public async Task <IActionResult> PostSanpham([FromBody] Sanpham sanpham)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            _context.Sanpham.Add(sanpham);
            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateException)
            {
                if (SanphamExists(sanpham.Masp))
                {
                    return(new StatusCodeResult(StatusCodes.Status409Conflict));
                }
                else
                {
                    throw;
                }
            }

            return(CreatedAtAction("GetSanpham", new { id = sanpham.Masp }, sanpham));
        }
        public JsonResult Update(string id)
        {
            //JsonConvert.DeserializeObject<IEnumerable<Sanpham>>(taikhoanJsonString);
            var listSession = JsonConvert.DeserializeObject <List <CartItem> >(id);
            var Sessioncart = HttpContext.Session.GetComplexData <List <CartItem> >(CartSession);

            foreach (var item in Sessioncart)
            {
                var jsonItem = listSession.SingleOrDefault(x => x.SP.MaSp == item.SP.MaSp);
                if (jsonItem != null)
                {
                    Sanpham sanpham = GetSanphamAsync(item.SP.MaSp).GetAwaiter().GetResult();
                    if (sanpham.SoLuongTon >= jsonItem.SoLuong)
                    {
                        item.SoLuong = jsonItem.SoLuong;
                    }
                    else
                    {
                        return(Json(new
                        {
                            status = false
                        }));
                    }
                }
            }
            HttpContext.Session.SetComplexData(CartSession, Sessioncart);
            return(Json(new
            {
                status = true
            }));
        }
        public async Task <IActionResult> PutSanpham([FromRoute] string id, [FromBody] Sanpham sanpham)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != sanpham.Masp)
            {
                return(BadRequest());
            }

            _context.Entry(sanpham).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!SanphamExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
Exemple #8
0
        public async void CapNhatSLSanPhamAsync(Sanpham sanpham, int SoLuong)
        {
            sanpham.SoLuongTon += SoLuong;
            HttpResponseMessage respond = await GetAPI("SanPhamUrl").PutAsJsonAsync($"{pathSP}/{sanpham.MaSp}", sanpham);

            respond.EnsureSuccessStatusCode();
        }
Exemple #9
0
        public async Task <IActionResult> Detail(string id)
        {
            if (id == null)
            {
                ModelState.AddModelError("", "Chưa nhập mã sản phẩm");
                return(RedirectToAction(nameof(Index)));
            }
            else
            {
                Sanpham sanpham = null;

                HttpResponseMessage respond = await GetAPI("SanPhamUrl").GetAsync($"{pathSP}/{id}");

                if (respond.IsSuccessStatusCode)
                {
                    sanpham = await respond.Content.ReadAsAsync <Sanpham>();

                    return(View(sanpham));
                }
                else
                {
                    ModelState.AddModelError("", "Không tìm được mã sản phẩm!");
                    return(RedirectToAction(nameof(Index)));
                }
            }
        }
Exemple #10
0
        public async Task <IActionResult> Cancel(string id)
        {
            HttpClient          client  = GetAPI("DonHangUrl");
            HttpResponseMessage respond = await client.GetAsync($"{pathDH}/{id}");

            if (!respond.IsSuccessStatusCode)
            {
                ModelState.AddModelError("", "Hủy đơn hàng thất bại!");
            }
            else
            {
                Donhang donhang = await respond.Content.ReadAsAsync <Donhang>();

                List <Chitietdonhang> chitietdonhang = GetChiTietDonHangAsync(donhang.MaDh).GetAwaiter().GetResult();
                foreach (var item in chitietdonhang)
                {
                    Sanpham sanpham = GetSanphamAsync(item.MaSp).GetAwaiter().GetResult();
                    CapNhatSLSanPhamAsync(sanpham, (int)item.SoLuong);
                    item.Status = 0;
                    capNhatChiTietDonHangAsync(item);
                }
                donhang.Status = 0;
                capNhatDonHangAsync(donhang);
            }

            return(RedirectToAction("Index", "DonHang"));
        }
Exemple #11
0
        public IEnumerable <Sanpham> GetSanPhamBanChayNhat()
        {
            IEnumerable <Sanpham> list = GetSanPhams;
            int?Max1 = 1000000000;
            int?Max2 = 0;

            for (int i = 1; i <= 4; i++)
            {
                Sanpham sp = new Sanpham();
                foreach (Sanpham item in list)
                {
                    if (item.IdLoaiSanPham == 4)
                    {
                        if (item.IdLoaiSanPham * 1000 > Max2 && item.IdLoaiSanPham * 1000 < Max1)
                        {
                            Max2 = item.IdLoaiSanPham * 1000;
                            sp   = item;
                        }
                    }
                    else
                    {
                        if (item.IdLoaiSanPham > Max2 && item.IdLoaiSanPham < Max1)
                        {
                            Max2 = item.IdLoaiSanPham;
                            sp   = item;
                        }
                    }
                }
                Max1 = Max2;
                Max2 = 0;
                yield return(sp);
            }
        }
        public async Task <IActionResult> Edit(int id, [Bind("Masp,Tensp,Hang,Mota,Namsx,Gia,Giakhuyenmai,Anhdaidien,Manhinh,Cameratruoc,Camerasau,Ram,Bonhotrong,Cpu,Gpu,Pin,Os,Sim")] Sanpham sanpham)
        {
            if (id != sanpham.Masp)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(sanpham);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!SanphamExists(sanpham.Masp))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            ViewData["Hang"] = new SelectList(_context.Hang, "Mahang", "Tenhang", sanpham.Hang);
            return(View(sanpham));
        }
Exemple #13
0
        // Loại sản phẩm
        public Loaisanpham GetLoaiSanPham(int Id)
        {
            Sanpham     a   = GetSanPham(Id);
            Loaisanpham res = db.Loaisanpham.Find(a.IdLoaiSanPham);

            return(res);
        }
Exemple #14
0
        public void updateMetatitle(int?Id, string metatitle)
        {
            Sanpham a = db.Sanpham.Find(Id);

            a.metatitle = metatitle;
            db.SaveChanges();
        }
        public IViewComponentResult Invoke()
        {
            //Select random san pham here
            int            i      = 4;
            List <Sanpham> splist = _context.Sanpham.ToList();
            //List<Sanpham> splist = GetSanphams();
            List <Sanpham> dmLst  = new List <Sanpham>();
            Sanpham        sptemp = new Sanpham();

            while (i >= 0)
            {
                int r = rnd.Next(splist.Count);
                if (dmLst.Count <= 0)
                {
                    dmLst.Add(splist[r]);
                    sptemp = splist[r];
                    i--;
                }
                else if (sptemp != splist[r])
                {
                    dmLst.Add(splist[r]);
                    sptemp = splist[r];
                    i--;
                }
            }
            return(View("sanphamHotLoader", dmLst));
        }
Exemple #16
0
        public void removeSanPham(int Id)
        {
            Sanpham res = db.Sanpham.Find(Id);

            db.Sanpham.Remove(res);
            db.SaveChanges();
        }
Exemple #17
0
        public IActionResult SingleProduct(string metatitle)
        {
            getSession();
            Sanpham     tmp = _Sanpham.GetSanPhamByMetaTitle(metatitle);
            Loaisanpham a   = _Sanpham.GetLoaiSanPham((int)tmp.IdLoaiSanPham);

            ViewBag.CateTitle = a.metatitle;
            ViewBag.MetaTitle = metatitle;
            var Id = tmp.Id;

            ViewBag.SanPham        = _Sanpham.GetSanPham(Id);
            ViewBag.ChiTietSanPham = _Sanpham.GetChiTietSanPham(Id);
            ViewBag.Loai           = _Sanpham.GetLoaiSanPham(Id);

            ViewBag.ss = HttpContext.Session.GetInt32("Id");
            ViewBag.SanPhamCungLoai    = _Sanpham.GetSanPhamsByIdLoaiSanPham(_Sanpham.GetLoaiSanPham(Id).Id);
            ViewBag.ListChiTietSanPham = _Sanpham.GetChiTietSanPhams;
            //ViewBag.ListSanPham = _Sanpham.GetSanPhams;

            Chitietdonhang dh = new Chitietdonhang();

            dh.Id        = HttpContext.Session.GetInt32("Id");
            dh.IdSanPham = Id;
            dh.SoLuong   = 0;
            dh.Gia       = 0;

            return(View(dh));
        }
Exemple #18
0
        private async Task <List <CartItem> > chuyenThanhListSPAsync(List <Chitietdonhang> chitietdonhang)
        {
            List <CartItem> result = new List <CartItem>();

            foreach (var item in chitietdonhang)
            {
                Sanpham             sanpham = null;
                HttpResponseMessage respond = await GetAPI("SanPhamUrl").GetAsync($"{pathSP}/{item.MaSp}");

                if (respond.IsSuccessStatusCode)
                {
                    sanpham = await respond.Content.ReadAsAsync <Sanpham>();

                    CartItem newItem = new CartItem();
                    newItem.SP      = sanpham;
                    newItem.SoLuong = (int)item.SoLuong;
                    result.Add(newItem);
                }
                else
                {
                    return(result);
                }
            }
            return(result);
        }
Exemple #19
0
        public List <Sanpham> TimKiemSanPham(string key)
        {
            List <Sanpham> List      = new List <Sanpham>();
            var            dbContext = new shopContext();

            if (key == "xxx")
            {
                var sanpham = (from sp in dbContext.Sanpham
                               join h in dbContext.Hinhanh
                               on sp.HinhAnhId equals h.HinhAnhId
                               select new
                {
                    SanPhamId = sp.SanPhamId,
                    TenSanPham = sp.TenSanPham,
                    GiaBanLe = sp.GiaBanLe,
                    HinhAnh = h.TenFile,
                    PhanLoaiId = sp.PhanLoaiId
                });
                foreach (var item in sanpham)
                {
                    Sanpham sp = new Sanpham();
                    sp.SanPhamId  = item.SanPhamId;
                    sp.TenSanPham = item.TenSanPham;
                    sp.GiaBanLe   = item.GiaBanLe;
                    sp.PhanLoaiId = item.PhanLoaiId;
                    Hinhanh h = new Hinhanh();
                    h.TenFile  = item.HinhAnh;
                    sp.HinhAnh = h;
                    List.Add(sp);
                }
            }
            else
            {
                var sanpham = (from sp in dbContext.Sanpham
                               join h in dbContext.Hinhanh
                               on sp.HinhAnhId equals h.HinhAnhId
                               where sp.TenSanPham.StartsWith(key)
                               select new
                {
                    SanPhamId = sp.SanPhamId,
                    TenSanPham = sp.TenSanPham,
                    GiaBanLe = sp.GiaBanLe,
                    HinhAnh = h.TenFile,
                    PhanLoaiId = sp.PhanLoaiId
                });
                foreach (var item in sanpham)
                {
                    Sanpham sp = new Sanpham();
                    sp.SanPhamId  = item.SanPhamId;
                    sp.TenSanPham = item.TenSanPham;
                    sp.GiaBanLe   = item.GiaBanLe;
                    sp.PhanLoaiId = item.PhanLoaiId;
                    Hinhanh h = new Hinhanh();
                    h.TenFile  = item.HinhAnh;
                    sp.HinhAnh = h;
                    List.Add(sp);
                }
            }
            return(List);
        }
Exemple #20
0
 public void Nhap()
 {
     Console.Clear();
     Console.WriteLine("NHẬP THÔNG TIN SẢN PHẨM");
     Sanpham sp = new Sanpham();
     string nhapthem;
     do { 
         do
         {
             Console.Write("Nhap ma san pham:"); sp.Masp = Console.ReadLine();
         } while (string.IsNullOrEmpty(sp.Masp));
         do {
             Console.Write("Nhap ma loai:"); sp.Maloai = (Console.ReadLine());
         } while (string.IsNullOrEmpty(sp.Maloai));
         do {
             Console.Write("Nhap ten san pham :"); sp.Tensp = Console.ReadLine();
         } while (string.IsNullOrEmpty(sp.Tensp));
         do {
             Console.Write("Nhap so luong san pham:"); sp.Soluong = int.Parse(Console.ReadLine());
         } while (sp.Soluong<0);
         do {
             Console.Write("Nhap gia ban san pham :"); sp.Giaban = int.Parse(Console.ReadLine());
         } while (sp.Giaban<10000);
         do {
             Console.Write("Nhap Nha san xuat :"); sp.Nhasx = Console.ReadLine();
         } while (string.IsNullOrEmpty(sp.Nhasx));
         spBLL.ThemSanPham(sp);
         Console.Write("Bạn có muốn nhập tiếp không ?(Y or y để nhập tiếp,nhấn bất kì để thoát )");
         do
         {
             nhapthem = Console.ReadLine();
         } while (nhapthem == "");
     } while (nhapthem == "Y" || nhapthem == "y");
 }
 public ActionResult CreateKH(Sanpham model)
 {
     model.Id = Guid.NewGuid();
     _context.Sanphams.Add(model);
     _context.SaveChanges();
     return(NoContent());
 }
Exemple #22
0
        public List <Sanpham> ThemGioHang(int maspp)
        {
            var dbContext = new shopContext();
            var sanpham   = (from sp in dbContext.Sanpham
                             join h in dbContext.Hinhanh
                             on sp.HinhAnhId equals h.HinhAnhId
                             where sp.SanPhamId == maspp
                             select new
            {
                SanPhamId = sp.SanPhamId,
                TenSp = sp.TenSanPham,
                HinhAnh = h.TenFile,
                GiaBanLe = sp.GiaBanLe,
            });
            List <Sanpham> ListSP = new List <Sanpham>();

            foreach (var item in sanpham)
            {
                Sanpham sp = new Sanpham();
                sp.SanPhamId  = item.SanPhamId;
                sp.TenSanPham = item.TenSp;
                sp.GiaBanLe   = item.GiaBanLe;
                Hinhanh h = new Hinhanh();
                h.TenFile  = item.HinhAnh;
                sp.HinhAnh = h;
                ListSP.Add(sp);
            }
            return(ListSP);
        }
Exemple #23
0
        public IHttpActionResult PutSanPham(int id, Sanpham sanpham)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != sanpham.id)
            {
                return(BadRequest());
            }

            db.Entry(sanpham).State = EntityState.Modified;

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!SanphamExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(StatusCode(HttpStatusCode.NoContent));
        }
Exemple #24
0
 private void ValidateSanpham(Sanpham model)
 {
     if (model.Gia <= 0)
     {
         ModelState.AddModelError("Gia", SanPhamError.PRICE_LESS_0);
     }
 }
Exemple #25
0
        public ActionResult DeleteConfirmed(int id)
        {
            Sanpham sanpham = db.Sanphams.Find(id);

            db.Sanphams.Remove(sanpham);
            db.SaveChanges();
            return(RedirectToAction("Index"));
        }
 public static int xoa_SP(Sanpham a)
 {
     SqlParameter[] para = new SqlParameter[]
     {
         new SqlParameter("@masp", a.Masp),
     };
     return(DataProvider.ExecuteNonQuery("xoa_SP", para));
 }
Exemple #27
0
        //Chèn một bản ghi san pham vào tệp
        public void ThemSanPham(Sanpham sp)
        {
            string       masanpham = sp.Masp;
            StreamWriter fwrite    = File.AppendText(txtSanPham);

            fwrite.WriteLine();
            fwrite.Write(masanpham + "#" + sp.Maloai + "#" + sp.Tensp + "#" + sp.Soluong + "#" + sp.Giaban + "#" + sp.Nhasx);
            fwrite.Close();
        }
        public IActionResult Edit(int Id)
        {
            ViewBag.ListLoaiSanPham = ILoaiSanPham.GetLoaisanphams;
            ViewBag.ListNhaCungCap  = INhaCungCap.GetNhacungcaps;
            Sanpham res = IFSanpham.GetSanPham(Id);

            ViewBag.HinhAnh = res.HinhAnh;
            return(View(res));
        }
        public ActionResult Muahang(int id)
        {
            SanPhamDAO     db = new SanPhamDAO();
            var            ds = db.LaySP();
            List <GioHang> gh = null;

            if (Session["GioHang"] == null)
            {
                GioHang a  = new GioHang();
                Sanpham sp = db.LaySP().SingleOrDefault(s => s.MaSP == id);
                if (sp != null)
                {
                    a.ID     = sp.MaSP;
                    a.Name   = sp.TenSP;
                    a.SL     = 1;
                    a.DonGia = (float)sp.GiaBan;
                    a.Image  = sp.AnhSP;
                    gh       = new List <GioHang>();
                    gh.Add(a);
                    Session["GioHang"] = gh;
                }
            }
            else
            {
                gh = (List <GioHang>)Session["GioHang"];
                var test = gh.Find(s => s.ID == id);
                if (test == null)
                {
                    GioHang a  = new GioHang();
                    var     sp = db.LaySP().Where(s => s.MaSP == id).Single();
                    a.ID     = sp.MaSP;
                    a.Name   = sp.TenSP;
                    a.SL     = 1;
                    a.DonGia = (float)sp.GiaBan;
                    a.Image  = sp.AnhSP;
                    gh.Add(a);
                }
                else
                {
                    test.SL = int.Parse(test.SL.ToString()) + 1;
                }
                Session["GioHang"] = gh;
            }
            float tongtien = 0;
            int   soluong  = 0;

            if (gh != null)
            {
                foreach (GioHang sp in gh)
                {
                    tongtien += sp.SL * sp.DonGia;
                }
                soluong = gh.Count;
            }
            return(Json(new { GioHang = gh, tongtien = tongtien, soluong = soluong }, JsonRequestBehavior.AllowGet));
        }
Exemple #30
0
 public ActionResult Edit([Bind(Include = "TenSP,MaSP,LoaiSP,Mausac,GiaSP,Soluong")] Sanpham sanpham)
 {
     if (ModelState.IsValid)
     {
         db.Entry(sanpham).State = EntityState.Modified;
         db.SaveChanges();
         return(RedirectToAction("Index"));
     }
     return(View(sanpham));
 }