Esempio n. 1
0
        public GioHang AddItem(int id, int idgh, int soluong)
        {
            GioHang        gioHang = context.GioHangs.Find(idgh);
            SanPhamGioHang spgh    = Check(id, idgh);

            if (spgh != null)
            {
                spgh.SoLuong += soluong;
                gioHang.SanPhamGioHangs.Add(spgh);
            }
            else
            {
                SanPham sp = context.SanPhams.Find(id);
                spgh           = new SanPhamGioHang();
                spgh.IDGH      = idgh;
                spgh.IDSP      = id;
                spgh.SoLuong   = soluong;
                spgh.DonGia    = sp.Gia;
                spgh.ThanhTien = soluong * sp.Gia;
                spgh.GhiChu    = "";
                context.SanPhamGioHangs.Add(spgh);
            }
            decimal thanhTien = 0;

            foreach (SanPhamGioHang sp in gioHang.SanPhamGioHangs)
            {
                thanhTien += sp.ThanhTien ?? 0;
            }
            gioHang.TongTien = thanhTien;
            context.SaveChanges();
            return(gioHang);
        }
Esempio n. 2
0
 public string ChangeItem(int id, int idgh, int soluong)
 {
     if (soluong > 0)
     {
         SanPhamGioHang spgh = context.SanPhamGioHangs.Where(a => a.IDSP == id && a.IDGH == idgh).FirstOrDefault();
         spgh.SoLuong   = soluong;
         spgh.ThanhTien = soluong * spgh.DonGia;
         context.SaveChanges();
         GioHang gioHang   = context.GioHangs.Where(a => a.ID == idgh).FirstOrDefault();
         decimal thanhTien = 0;
         foreach (SanPhamGioHang sp in gioHang.SanPhamGioHangs)
         {
             thanhTien += sp.ThanhTien ?? 0;
         }
         gioHang.TongTien = thanhTien;
         context.SaveChanges();
         return(String.Format(@"{{""TongTien"":{0}, ""ThanhTien"":{1}}}", gioHang.TongTien, spgh.ThanhTien));
     }
     else
     {
         SanPhamGioHang spgh = context.SanPhamGioHangs.Where(a => a.IDSP == id && a.IDGH == idgh).FirstOrDefault();
         context.SanPhamGioHangs.Remove(spgh);
         GioHang gioHang   = context.GioHangs.Where(a => a.ID == idgh).FirstOrDefault();
         decimal thanhTien = 0;
         foreach (SanPhamGioHang sp in gioHang.SanPhamGioHangs)
         {
             thanhTien += sp.ThanhTien ?? 0;
         }
         gioHang.TongTien = thanhTien;
         context.SaveChanges();
         return(String.Format(@"{{""TongTien"":{0}}}", gioHang.TongTien));
     }
 }
Esempio n. 3
0
        public SanPhamGioHang Check(int id, int idgh)
        {
            SanPhamGioHang spgh = context.SanPhamGioHangs.Where(x => x.IDSP == id && x.IDGH == idgh).FirstOrDefault();

            if (spgh != null)
            {
                return(spgh);
            }
            else
            {
                return(null);
            }
        }