Ejemplo n.º 1
0
        public ActionResult ThemSanPham(ThemSanPhamGioHangVM vm)
        {
            rd = new Random();

            try
            {
                vuong_cms_context __db = new vuong_cms_context();
                var sanpham            = __db.Product.Find(vm.SanPhamId);

                CTGioHang ct = new CTGioHang();
                ct.SanPhamId  = vm.SanPhamId;
                ct.TenSanPham = sanpham.ProductName;
                ct.HinhAnh    = sanpham.ThumbnailImage;
                ct.SoLuong    = vm.SoLuong;
                ct.Link       = Url.Action("Detail", "SanPham", new { id = sanpham.Id, seo_title = sanpham.ProductName.URLFriendly() });
                GioHang.ThemSanPham(ct);
            }
            catch (Exception ex)
            {
                return(Content("Lỗi: " + ex.Message));
            }
            return(RedirectToAction("Index"));
        }
Ejemplo n.º 2
0
        public ActionResult AddItem(string id, int quantity)
        {
            var SanPham = new SanPhamDAO().ViewDetail(id);
            var cart    = Session[CommomConstants.CartSession];

            // lấy về người đăng nhập hiện tại
            var sessnd = Session[CommomConstants.NguoiDungSession];
            var nd     = new NguoiDungLogin();

            nd = (NguoiDungLogin)sessnd;

            // nếu chưa đăng nhập
            if (sessnd == null)
            {
                if (cart != null)
                {
                    var list = (List <CartItem>)cart;           //ép kiểu về list cartitem lấy từ session(dữ liệu trong session là kiểu list
                    if (list.Exists(x => x.SanPham.MaSP == id)) //nếu list chứa productid truyền vào
                    {
                        foreach (var item in list)
                        {
                            if (item.SanPham.MaSP == id) // nếu đã tồn tại id mà thêm một id => tăng thêm số lượng
                            {
                                item.Quantity += quantity;
                            }
                        }
                    }
                    else // add mới một sản phẩm
                    {
                        //tạo mới đối tượng cart item
                        var item = new CartItem();
                        item.SanPham  = SanPham;
                        item.Quantity = quantity;
                        list.Add(item);
                    }
                    //Gán vào session
                    Session[CommomConstants.CartSession] = list;
                }
                else
                {
                    //tạo mới đối tượng cart item
                    var item = new CartItem();
                    item.SanPham  = SanPham;
                    item.Quantity = quantity;
                    var list = new List <CartItem>();
                    list.Add(item);
                    //Gán vào Session
                    Session[CommomConstants.CartSession] = list;
                }
            }
            else //đã đăng nhập
            {
                DBModels db = new DBModels();
                // kiểm tra giỏ hàng trong db
                var gh = new GioHang();
                gh = db.GioHangs.SingleOrDefault(x => x.MaND == nd.ID); // lấy giỏ hàng của người dùng hiện tại

                var item = db.CTGioHangs.Find(gh.MaGH, SanPham.MaSP);
                if (item != null)
                {
                    item.SoLuong += quantity;
                    db.SaveChanges();
                }
                else
                {
                    var tem = new CTGioHang();
                    tem.MaGH    = gh.MaGH;
                    tem.MaSP    = SanPham.MaSP;
                    tem.SoLuong = quantity;
                    db.CTGioHangs.Add(tem);
                    db.SaveChanges();
                }
            }


            return(RedirectToAction("Index"));
        }
Ejemplo n.º 3
0
        public ActionResult Payment()
        {
            try
            {
                var acc = Session[AccountSession];
                if (acc != null)
                {
                    double totalQty = 0, totalAmount = 0;
                    // lấy giỏ hàng
                    var ssCart = (List <CartItem>)Session[CartSession];
                    // lấy user đăng nhập
                    var ssAcc = (List <NguoiDung>)Session[AccountSession];

                    #region Save giohang
                    var order = new GioHang();
                    order.IdGioHang   = Guid.NewGuid();
                    order.IdKhachHang = ssAcc[0].IdNguoiDung;
                    order.IsDeleted   = false;
                    order.TinhTrang   = false;
                    order.CreatedDate = DateTime.Now;
                    order.UpdatedDate = DateTime.Now;

                    foreach (var item in ssCart)
                    {
                        var orderD = new CTGioHang();
                        orderD.IdGioHang = order.IdGioHang;
                        //orderD.IdOrder = order.ID;
                        orderD.IdMaXe      = item.SanPham.IdXe;
                        orderD.Gia         = item.SanPham.GiaNiemYetXe;
                        orderD.SoLuong     = item.SoLuong;
                        orderD.ThanhTien   = (item.SanPham.GiaNiemYetXe * item.SoLuong);
                        orderD.IsDeleted   = false;
                        orderD.TinhTrang   = false;
                        orderD.CreatedDate = DateTime.Now;
                        orderD.UpdatedDate = DateTime.Now;
                        daw.CTGioHangs.Add(orderD);
                        totalQty    += (double)item.SoLuong;
                        totalAmount += (double)(item.SanPham.GiaNiemYetXe * item.SoLuong);
                    }
                    order.TongSoLuong   = Convert.ToInt32(totalQty);
                    order.TongThanhTien = totalAmount;
                    daw.GioHangs.Add(order);
                    #endregion

                    //#region Save PhieuThu
                    //var invoice = new HoaDonThu();
                    //invoice.MaHoaDonThu = string.Format("{0}{1:yyyyMMddhhmmss}", "THU", DateTime.Now);
                    //invoice.KhachHang = ssAcc[0].IdNguoiDung;
                    //invoice.NgayThu = DateTime.Now;
                    //invoice.TongSoLuong = Convert.ToInt32(totalQty);
                    //invoice.TongTien = totalAmount;
                    //invoice.IsDeleted = false;
                    //invoice.TinhTrang = true;
                    //invoice.CreatedDate = DateTime.Now;
                    //invoice.UpdatedDate = DateTime.Now;
                    //invoice.CreatedBy = ssAcc[0].IdNguoiDung;
                    //invoice.UpdatedBy = ssAcc[0].IdNguoiDung;
                    //daw.HoaDonThus.Add(invoice);
                    //#endregion

                    #region cập nhật sl bảng xe, tình trạng loaixe
                    foreach (var item in ssCart)
                    {
                        var xe = daw.Xes.Where(x => x.IdXe == item.SanPham.IdXe).FirstOrDefault();
                        if (xe != null)
                        {
                            var loaixe = daw.LoaiXes.Where(l => l.IdLoaiXe == xe.MaLoaiXe).FirstOrDefault();
                            if (loaixe != null)
                            {
                                // cập nhật lại sl cho bảng loại xe
                                loaixe.SoLuong -= item.SoLuong;
                                daw.LoaiXes.Attach(loaixe);
                            }
                            // cập nhật lại tình trạng cho bảng xe
                            xe.TinhTrang = "SOLD";
                            daw.Xes.Attach(xe);
                        }
                    }
                    #endregion
                    // xóa giỏ hàng
                    Session[CartSession] = null;

                    var user = daw.NguoiDungs.Where(l => l.IdNguoiDung == order.IdKhachHang).FirstOrDefault();
                    ViewBag.Name  = user.TenNguoiDung;
                    ViewBag.Phone = user.SoDienThoai;
                    ViewBag.Email = user.Email;
                    daw.SaveChanges();
                    return(View(ssCart));
                }
                else
                {
                    return(RedirectToAction("Login", "Login"));
                }
            }
            catch (Exception ex)
            {
                return(RedirectToAction("Index", "Home"));
            }
        }