Ejemplo n.º 1
0
        private void btnLuu_Click(object sender, EventArgs e)
        {
            if (grChiTietPhieuDatHang.Rows.Count <= 1)
            {
                MessageBox.Show("Danh sách sản phẩm rỗng, vui lòng nhập thêm sản phẩm", "Thông báo");
                return;
            }

            PhieuDatHang phieuDatHang = new PhieuDatHang();

            phieuDatHang.NgayDatHang = DateTime.Now;
            phieuDatHang.MaNCC       = int.Parse(cbNCC.SelectedValue.ToString());
            db.PhieuDatHangs.InsertOnSubmit(phieuDatHang);
            db.SubmitChanges();

            for (int i = 0; i < grChiTietPhieuDatHang.Rows.Count - 1; i++)
            {
                DataGridViewRow hang = grChiTietPhieuDatHang.Rows[i];

                ChiTietPhieuDatHang chiTietPDH = new ChiTietPhieuDatHang();
                chiTietPDH.MaPDH      = phieuDatHang.MaPDH;
                chiTietPDH.MaSP       = int.Parse(hang.Cells[0].Value.ToString());
                chiTietPDH.SoLuongDat = int.Parse(hang.Cells[2].Value.ToString());
                chiTietPDH.DonGiaDat  = int.Parse(hang.Cells[3].Value.ToString());

                db.ChiTietPhieuDatHangs.InsertOnSubmit(chiTietPDH);
                db.SubmitChanges();
            }

            MessageBox.Show("Tạo đơn đặt hàng thành công", "Thông báo");
            btnLuu.Enabled = false;
        }
        public ActionResult DeleteConfirmed(int id)
        {
            ChiTietPhieuDatHang chiTietPhieuDatHang = db.ChiTietPhieuDatHangs.Find(id);

            db.ChiTietPhieuDatHangs.Remove(chiTietPhieuDatHang);
            db.SaveChanges();
            return(RedirectToAction("Index"));
        }
        public void MinusCart(int MAHANGHOA, float GIA, float GIAMGIA)
        {
            var currentCart           = Session["Cart"] as List <ChiTietPhieuDatHang>; //to check current cart
            ChiTietPhieuDatHang ctpdh = currentCart.Find(o => o.MaHangHoa == MAHANGHOA);

            ctpdh.SoLuong   -= 1;
            ctpdh.ThanhTien -= (decimal)(GIA * (1 - GIAMGIA));
        }
 public ActionResult Edit([Bind(Include = "SoPhieuDatHang,MaHangHoa,SoLuong,Gia,ThanhTien")] ChiTietPhieuDatHang chiTietPhieuDatHang)
 {
     if (ModelState.IsValid)
     {
         db.Entry(chiTietPhieuDatHang).State = EntityState.Modified;
         db.SaveChanges();
         return(RedirectToAction("Index"));
     }
     ViewBag.MaHangHoa      = new SelectList(db.HangHoas, "MaHangHoa", "TenHangHoa", chiTietPhieuDatHang.MaHangHoa);
     ViewBag.SoPhieuDatHang = new SelectList(db.PhieuDatHangs, "SoPhieuDatHang", "TenKhachHang", chiTietPhieuDatHang.SoPhieuDatHang);
     return(View(chiTietPhieuDatHang));
 }
Ejemplo n.º 5
0
        public async Task <ActionResult> ThanhToan(PhieuDatHangViewModel datHang)
        {
            var phieuDatHang = new PhieuDatHang();

            phieuDatHang.NgayDat           = DateTime.Now;
            phieuDatHang.TenKhachHang      = datHang.tenKhachHang;
            phieuDatHang.SoDienThoai       = datHang.soDienThoai;
            phieuDatHang.Diachi            = datHang.diaChi;
            phieuDatHang.Email             = datHang.email;
            phieuDatHang.HinhThucThanhToan = datHang.hinhThucThanhToan;
            phieuDatHang.Ghichu            = datHang.ghiChu;
            phieuDatHang.NgayChinhSua      = DateTime.Now;
            phieuDatHang.DaThanhToan       = false;
            phieuDatHang.DaXacNhan         = false;
            phieuDatHang.TrangThai         = true;

            try
            {
                var     soPhieuBanHang = _phieuDatHangBus.Insert(phieuDatHang);
                var     cart           = (List <PhieuDatHangViewModel>)Session[CartSession];
                decimal total          = 0;
                foreach (var item in cart)
                {
                    var chiTietPhieuDatHang = new ChiTietPhieuDatHang();
                    chiTietPhieuDatHang.MaHangHoa      = item.maHangHoa;
                    chiTietPhieuDatHang.SoPhieuDatHang = soPhieuBanHang;
                    chiTietPhieuDatHang.SoLuong        = item.soLuong;
                    if (item.giamGia <= 0)
                    {
                        chiTietPhieuDatHang.Gia       = item.giaBan;
                        chiTietPhieuDatHang.ThanhTien = item.giaBan * item.soLuong;
                        total += (item.giaBan * item.soLuong);
                    }
                    else
                    {
                        chiTietPhieuDatHang.Gia       = item.giamGia;
                        chiTietPhieuDatHang.ThanhTien = item.giamGia * item.soLuong;
                        total += (item.giamGia * item.soLuong);
                    }

                    _chiTietPhieuDatHangBus.Insert(chiTietPhieuDatHang);
                }
                phieuDatHang.TongTien = total;
                _phieuDatHangBus.UpdateTongTien(phieuDatHang);
            }
            catch (Exception)
            {
                SetAlert("Đã xảy ra lỗi trong quá trình đặt hàng! Vui lòng thực hiện lại!!!", "error");
                return(RedirectToAction("ThanhToan"));
            }
            Session[CartSession] = null;
            return(RedirectToAction("ThongBaoThanhCong"));
        }
 public bool Insert(ChiTietPhieuDatHang detail)
 {
     try
     {
         dbContext.ChiTietPhieuDatHanges.Add(detail);
         dbContext.SaveChanges();
         return(true);
     }
     catch
     {
         return(false);
     }
 }
        // GET: ChiTietPhieuDatHang/Details/5
        public ActionResult Details(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            ChiTietPhieuDatHang chiTietPhieuDatHang = db.ChiTietPhieuDatHangs.Find(id);

            if (chiTietPhieuDatHang == null)
            {
                return(HttpNotFound());
            }
            return(View(chiTietPhieuDatHang));
        }
        // GET: ChiTietPhieuDatHang/Edit/5
        public ActionResult Edit(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            ChiTietPhieuDatHang chiTietPhieuDatHang = db.ChiTietPhieuDatHangs.Find(id);

            if (chiTietPhieuDatHang == null)
            {
                return(HttpNotFound());
            }
            ViewBag.MaHangHoa      = new SelectList(db.HangHoas, "MaHangHoa", "TenHangHoa", chiTietPhieuDatHang.MaHangHoa);
            ViewBag.SoPhieuDatHang = new SelectList(db.PhieuDatHangs, "SoPhieuDatHang", "TenKhachHang", chiTietPhieuDatHang.SoPhieuDatHang);
            return(View(chiTietPhieuDatHang));
        }
        public void AddToCart(int MAHANGHOA, float GIA, float GIAMGIA)
        {
            var currentCart = Session["Cart"] as List <ChiTietPhieuDatHang>; //to check current cart

            if (!currentCart.Exists(o => o.MaHangHoa == MAHANGHOA))
            {
                ChiTietPhieuDatHang ctpdh = new ChiTietPhieuDatHang
                {
                    MaHangHoa = MAHANGHOA,
                    SoLuong   = 1,
                    Gia       = (decimal)(GIA * (1 - GIAMGIA)),
                    ThanhTien = (decimal)(GIA * (1 - GIAMGIA))
                };
                currentCart.Add(ctpdh);
                Session["Cart"] = currentCart;
            }
            else
            {
                ChiTietPhieuDatHang ctpdh = currentCart.Find(o => o.MaHangHoa == MAHANGHOA);
                ctpdh.SoLuong   += 1;
                ctpdh.ThanhTien += (decimal)(GIA * (1 - GIAMGIA));
            }
        }