Example #1
0
        public IActionResult CheckOut(IFormCollection fc)
        {
            var dh = new DonHang();

            if (!string.IsNullOrEmpty(fc["Id"].ToString()))
            {
                if (context.AspNetUsers.Find(fc["Id"]) != null)
                {
                    dh.MaKh = fc["Id"];
                    var a = dataAccess.GetUser(dh.MaKh);
                    dh.HoTen     = a.HoTen;
                    dh.Dienthoai = a.PhoneNumber;
                    dh.Ghichu    = fc["GhiChu"];
                    dh.Email     = a.Email;
                    dh.Diachi    = a.DiaChi;
                    HttpContext.Session.SetObjectAsJson("MaKh", fc["Id"]);
                }
                else
                {
                    dh.MaKh      = "null" + (context.DonHang.Count() + 1);
                    dh.HoTen     = fc["HoTen"];
                    dh.Diachi    = fc["DiaChi"];
                    dh.Ghichu    = fc["GhiChu"];
                    dh.Email     = fc["Email"];
                    dh.Dienthoai = fc["DienThoai"];
                }
            }
            else
            {
                dh.MaKh      = "null" + (context.DonHang.Count() + 1);
                dh.HoTen     = fc["HoTen"];
                dh.Diachi    = fc["DiaChi"];
                dh.Ghichu    = fc["GhiChu"];
                dh.Email     = fc["Email"];
                dh.Dienthoai = fc["DienThoai"];
            }

            dh.NgayDatMua   = DateTime.Now;
            dh.PhiVanChuyen = 1000000;
            dh.TinhTrangDh  = 0;
            dh.Tongtien     = double.Parse(fc["GiaTriDonHang"]);
            dh.GiamGia      = double.Parse(fc["GiamGia"]);

            dh.Ghichu      = fc["GhiChu"];
            dh.TinhTrangDh = 1;
            //dh.Diachi = fc["DiaChi"];
            context.DonHang.Add(dh);
            context.SaveChanges();

            _repository.NotifyDonHang();
            var content = System.IO.File.ReadAllText("GioHang.html");

            content = content.Replace("{{Hoten}}", dh.HoTen);

            var strCtdh = "";
            var index   = 0;
            var gh      = HttpContext.Session.GetObjectFromJson <List <ChiTietDonHang> >("GioHang");

            foreach (var item in gh)
            {
                item.MaCtdh = (context.ChiTietDonHang.ToList().Count + 1).ToString();
                item.MaDh   = dh.MaDh;
                context.SanPham.Find(item.MaSp).SoLuong =
                    context.SanPham.Find(item.MaSp).SoLuong - item.SoLuong;
                context.ChiTietDonHang.Add(item);
                context.SaveChanges();
                strCtdh = strCtdh + "<tr>";
                strCtdh = strCtdh + "<td style='text-align:center'>" + ++index + "</td><td>" +
                          context.SanPham.Find(item.MaSp).TenSp + "</td><td  style='text-align:center'>"
                          + item.SoLuong + "</td><td  style='text-align:center'>"
                          + ((context.SanPham.Find(item.MaSp).GiaGoc *item.SoLuong).HasValue
                              ? (context.SanPham.Find(item.MaSp).GiaGoc *item.SoLuong)?.ToString("N0")
                              : "NULL") + "</td><tr>";
            }

            content = content.Replace("{{thongtindonhang}}", HtmlEncoder.Default.Encode(strCtdh));
            content = content.Replace("{{madh}}", dh.MaDh.ToUpper());
            content = content.Replace("{{diachi}}", dh.Diachi);
            content = content.Replace("{{thanhtien}}",
                                      dh.Tongtien.Value.ToString("N0"));
            content = content.Replace("{{sdt}}", dh.Dienthoai);
            content = content.Replace("{{giamgia}}", dh.GiamGia.Value.ToString("N0"));
            content = content.Replace("{{thanhtoan}}", (-dh.GiamGia.Value + dh.Tongtien.Value).ToString("N0"));
            var Option = new AuthMessageSenderOptions
            {
                SendGridKey  = context.Parameters.Find("2").Value,
                SendGridUser = context.Parameters.Find("1").Value
            };

            var mailSender = new EmailSender(Option);


            mailSender.SendEmailAsync(dh.Email, "Chi tiết đơn hàng ", $"{WebUtility.HtmlDecode(content)}");

            HttpContext.Session.DeleteAllSession();

            return(RedirectToAction("ChiTietDonHang", "GioHang", new { id = dh.MaDh })
                   .WithSuccess("Đặt hàng thành công", ""));
        }