public ActionResult Index(CheckOutInfoModel model)
 {
     if (ModelState.IsValid)
     {
         List <cartModel> li = Session["giohang"] as List <cartModel>;
         var id = Session["idKH"];
         //return Json(model);
         if (id == null)
         {
             new CheckOutDao().ThanhToanKoCoSan(li, model);
             ViewBag.success = "Đặt hàng thành công";
             //CheckOutInfoModel md = new CheckOutInfoModel();
         }
         else
         {
             int ketqua = new CheckOutDao().ThanhToan(li, (int)id, model);
             if (ketqua > 0)
             {
                 ViewBag.success = "Đặt hàng thành công";
             }
             else
             {
                 ViewBag.success = "Đặt hàng không thành công";
             }
             //CheckOutInfoModel md = new CheckOutInfoModel();
         }
         Session["giohang"] = null;
     }
     return(View());
 }
Exemple #2
0
        public void ThanhToanKoCoSan(List <cartModel> li, CheckOutInfoModel info)
        {
            try
            {
                khachhang kh = new khachhang();
                kh.diachi   = info.diaChi;
                kh.email    = info.gmail;
                kh.ghichu   = null;
                kh.gioitinh = info.gioitinh;
                kh.hoten    = info.tenKH;
                kh.sodt     = info.sdt;
                kh.matk     = null;
                context.khachhangs.Add(kh);
                context.SaveChanges();
                khachhang temp = context.khachhangs.Find(kh.makh);

                donhang dh = new donhang();
                dh.makh     = temp.makh;
                dh.ngaydat  = DateTime.Today;
                dh.tongtien = cartModel.tongTien;
                dh.ghichu   = null;
                dh.httt     = info.httt;
                context.donhangs.Add(dh);
                context.SaveChanges();
                donhang dhtemp = context.donhangs.Find(dh.madh);
                foreach (var sp in li)
                {
                    ctdh ct = new ctdh();
                    ct.madh    = dhtemp.madh;
                    ct.masp    = sp.maSP;
                    ct.soluong = sp.soLuong;
                    ct.gia     = sp.donGia;
                    context.ctdhs.Add(ct);
                }
                context.SaveChanges();
                cartModel.tongSoHang = 0;
                cartModel.tongTien   = 0;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }
        //private CoffeeHouseDbContext context = new CoffeeHouseDbContext();
        public int ThanhToan(List <cartModel> li, int makh, CheckOutInfoModel info)
        {
            using (var ctx = new CoffeeHouseDbContext())
            {
                int     madh;
                donhang dh = new donhang();
                //dh.khachhang = kh;
                dh.makh     = makh;
                dh.ngaydat  = DateTime.Today;
                dh.tongtien = cartModel.tongTien;
                dh.ghichu   = info.ghichu;
                dh.httt     = info.httt;
                dh.tttt     = 0;
                ctx.donhangs.Add(dh);
                ctx.SaveChanges();
                //ma = dh.madh;
                //donhang temp = context.donhangs.Find(dh.madh);
                madh = (int)dh.madh;
                foreach (var sp in li)
                {
                    ctdh ct = new ctdh();
                    //ct.madh = ma;
                    /*ct.donhang = dh;*/
                    //ct.madh = temp.madh;
                    ct.madh    = madh;
                    ct.masp    = sp.maSP;
                    ct.soluong = sp.soLuong;
                    ct.gia     = sp.donGia;
                    ctx.ctdhs.Add(ct);
                    //context.SaveChanges();
                }

                //try
                //{
                //    context.SaveChanges();
                //}
                //catch (Exception exc)
                //{
                //    string message = exc.GetType().FullName + ": " + exc.Message;
                //}
                cartModel.tongSoHang = 0;
                cartModel.tongTien   = 0;
                return(ctx.SaveChanges());
            }
            //long ma;
            //khachhang kh = context.khachhangs.FirstOrDefault(x => x.makh == makh);
            //kh.diachi = info.diaChi;
            //kh.email = info.gmail;
            //kh.ghichu = info.ghichu;
            //kh.gioitinh = info.gioitinh;
            //kh.hoten = info.tenKH;
            //kh.sodt = info.sdt;
            //kh.matk = null;
            //context.khachhangs.Add(kh);
            //context.SaveChanges();
            //    int madh;
            //    donhang dh = new donhang();
            //    //dh.khachhang = kh;
            //    dh.makh = makh;
            //    dh.ngaydat = DateTime.Today;
            //    dh.tongtien = cartModel.tongTien;
            //    dh.ghichu = info.ghichu;
            //    dh.httt = info.httt;
            //    dh.tttt = 0;
            //    context.donhangs.Add(dh);
            //    context.SaveChanges();
            //    //ma = dh.madh;
            //    //donhang temp = context.donhangs.Find(dh.madh);
            //    madh = (int) dh.madh;
            //    foreach (var sp in li)
            //    {
            //        ctdh ct = new ctdh();
            //        //ct.madh = ma;
            //        /*ct.donhang = dh;*/
            //        //ct.madh = temp.madh;
            //        ct.madh = madh;
            //        ct.masp = sp.maSP;
            //        ct.soluong = sp.soLuong;
            //        ct.gia = sp.donGia;
            //        context.ctdhs.Add(ct);
            //        //context.SaveChanges();
            //    }

            //    //try
            //    //{
            //    //    context.SaveChanges();
            //    //}
            //    //catch (Exception exc)
            //    //{
            //    //    string message = exc.GetType().FullName + ": " + exc.Message;
            //    //}
            //    cartModel.tongSoHang = 0;
            //    cartModel.tongTien = 0;
            //return context.SaveChanges();
        }