Exemple #1
0
        public IActionResult XemGioHang(IFormCollection fc)
        {
            var giohang = HttpContext.Session.GetObjectFromJson <List <ChiTietDonHang> >("GioHang");

            if (giohang == null)
            {
                giohang = new List <ChiTietDonHang>();
            }

            double?thanhTien     = 0;
            double?giaTriDonHang = 0;


            foreach (var item in giohang)
            {
                item.ThanhTien = (double?)(_sanPhamRepository.GetSanPhamById(item.MaSp).GiaGoc *item.SoLuong);
                thanhTien      = thanhTien + item.ThanhTien;
                giaTriDonHang  = giaTriDonHang + item.ThanhTien;
            }

            ViewBag.GiaTriDonHang = giaTriDonHang - thanhTien;

            var listVoucher = HttpContext.Session.GetObjectFromJson <List <Voucher> >("Vouchers");

            if (listVoucher == null)
            {
                listVoucher = new List <Voucher>();
            }
            else
            {
                ViewBag.Vouchers = listVoucher;
            }

            var _voucher = _shopRepo.GetVoucherById(fc["voucher"].ToString());

            if (_voucher != null)
            {
                if (!listVoucher.Exists(x => x.VoucherCode == _voucher.VoucherCode))
                {
                    listVoucher.Add(_voucher);
                }
                else
                {
                    ViewBag.Vouchers = listVoucher;


                    ViewBag.ThanhTien     = thanhTien;
                    ViewBag.GiamGia       = giaTriDonHang - thanhTien;
                    ViewBag.GiaTriDonHang = giaTriDonHang;
                    ViewBag.GioHang       = giohang;
                    ViewBag.Added         = 0;
                    return(View().WithWarning("", "Voucher đã sử dụng"));
                }

                HttpContext.Session.SetObjectAsJson("Vouchers", listVoucher);
                ViewBag.Vouchers = listVoucher;
            }


            foreach (var item in listVoucher)
            {
                if (item.VoucherType.Contains("VCT003"))
                {
                    thanhTien = thanhTien - item.VoucherDiscount;
                }
                else if (item.VoucherType.Contains("VCT002"))
                {
                    if (item.VoucherDiscount != null)
                    {
                        thanhTien = thanhTien - thanhTien * ((double)item.VoucherDiscount / 100);
                    }
                }
            }


            // string s = String.Format("{0:N0}", thanhTien.ToString());
            ViewBag.ThanhTien     = thanhTien;
            ViewBag.GiamGia       = giaTriDonHang - thanhTien;
            ViewBag.GiaTriDonHang = giaTriDonHang;
            thanhTien             = 0;
            giaTriDonHang         = 0;
            ViewBag.GioHang       = giohang;
            ViewBag.Added         = 0;

            return(View());
        }