//Thanh toán với Payoo
        protected string btnPaynow_Click(string orderId, string TenDuAn, int SoTien, int NgayChuyenTien, EntityDauTu item)
        {
            EntityPhanThuong phanthuong;
            if (item.IdPhanthuong.GetValueOrDefault() == 0)
            {
                phanthuong = new EntityPhanThuong();
                phanthuong.NoiDung = "Bạn không nhận phần thưởng";
            }
            else
            {
                phanthuong = db.EntityPhanThuongs.Where(p => p.Id == item.IdPhanthuong).First();

            }
            PayooOrder order = new PayooOrder();
            order.Session = orderId;
            order.BusinessUsername = ConfigurationManager.AppSettings["BusinessUsername"];
            order.OrderCashAmount = SoTien;
            order.OrderNo = orderId;
            order.ShippingDays = short.Parse(NgayChuyenTien.ToString());
            order.ShopBackUrl = ConfigurationManager.AppSettings["ShopBackUrl"];
            order.ShopDomain = ConfigurationManager.AppSettings["ShopDomain"];
            order.ShopID = long.Parse(ConfigurationManager.AppSettings["ShopID"]);
            order.ShopTitle = ConfigurationManager.AppSettings["ShopTitle"];
            order.StartShippingDate = DateTime.Now.ToString("dd/MM/yyyy");
            order.NotifyUrl = ConfigurationManager.AppSettings["NotifyUrl"];
            //You can do

            string OrderHtml = "<table width='100%' border='1' cellspacing='0'><thead><tr><td width='30%' align='center'><b>Dự án bạn đã đầu tư</b></td><td width='45%' align='center'><b>Phần thưởng</b></td><td width='25%' align='center'><b>Số tiền</b></td></tr></thead><tbody>";
            OrderHtml += "<tr><td align='center'>" + TenDuAn + "</td><td align='center'>" + phanthuong.NoiDung + "</td><td align='right'>" + String.Format("{0:0,0 VNĐ}", SoTien) + "</td></tr>";
            //OrderHtml += "<tr><td align='left'>Chi phí thuế</td><td align='right'>50,000</td><td align='right'>50,000</td></tr>";
            OrderHtml += "<tr><td align='right' colspan='2'><b>Tổng tiền:</b></td><td align='right'>" + String.Format("{0:0,0 VNĐ}", SoTien) + "</tr>";
            OrderHtml += "<tr><td align='left' colspan='3'>Một vài lưu ý khác: </td></tr></tbody></table>";
            order.OrderDescription = HttpUtility.UrlEncode(OrderHtml);

            //order.OrderDescription = HttpUtility.UrlEncode("<table width='100%' border='1' cellspacing='0'><thead><tr><td width='45%' align='center'><b>Tên dự án</b></td><td width='30%' align='center'><b>Số tiền đầu tư</b></td><td width='25%' align='center'><b>Thành tiền</b></td></tr></thead><tbody><tr><td align='left'>Dự án chuồn chuồn giấy</td><td align='right'>500,000</td><td align='right'>500,000</td></tr><tr><td align='left'>Chi phí thuế</td><td align='right'>50,000</td><td align='right'>50,000</td></tr><tr><td align='right' colspan='2'><b>Tổng tiền:</b></td><td align='right'>550,000</td></tr><tr><td align='left' colspan='3'>Một vài lưu ý khác</td></tr></tbody></table>");

            string Checksum = string.Empty;

            //Su dung checksum ko ma hoa du lieu
            //string ChecksumKey = ConfigurationManager.AppSettings["ChecksumKey"];
            //string XML = PaymentXMLFactory.GetPaymentXML(order);
            //Checksum = SHA1encode.hash(ChecksumKey + XML);

            //khong su dung checksum, co ma hoa du lieu
            string XML = PaymentXMLFactory.GetPaymentXML(order, Server.MapPath(@"..\App_Data\Certificates\biz_pkcs12.p12"), "alpe", Server.MapPath(@"..\App_Data\Certificates\payoo_public_cert.pem"));

            return RedirectToProvider(ConfigurationManager.AppSettings["PayooCheckout"], XML, Checksum);
        }
        public ActionResult TaoDuAn_Buoc2(DuAnModel DA)
        {
            try
            {
                var duan = db.EntityDuAns.Where(p => p.Id == DA.Id).First();
                ViewBag.DanhMuc = db.EntityDanhMucs.Where(p => p.IdRoot == 1).ToList();

                #region Chỉnh sửa hình ảnh (chung cho toàn bộ)
                for (int i = 0; i < Request.Files.Count; i++)
                {
                    HttpPostedFileBase hpf = Request.Files[i];

                    #region phan sua khi chay du an  // hình ảnh - phần thưởng - tiếu sử

                    if (hpf.FileName != "" && i == 0)
                    {
                        ImageHelper imgHelper = new ImageHelper();
                        string encodestring = imgHelper.encodeImageFile(hpf);
                        duan.HinhAnh = encodestring;
                        db.SubmitChanges();
                        var path = Path.Combine(Server.MapPath("~/Content/Images/DuAn"), encodestring);
                        hpf.SaveAs(path);
                    }

                    #endregion

                    if (duan.TrangThai < 1)
                    {
                        if (hpf.FileName != "" && i == 1)
                        {
                            var ava = db.EntityUsers.Where(p => p.Email == Request.Cookies["ftusername"].Value).First();

                            if (ava.Avatar != null && ava.Avatar.IndexOf("ava0.jpg") == -1)
                            {
                                string[] link = ava.Avatar.Split('/');
                                string fileToDelete = Path.Combine(Server.MapPath("~/Content/Images/Avatar"), link[link.Count() - 1]); // file hinh cu
                                System.IO.File.Delete(fileToDelete);
                            }
                            ImageHelper imgHelper = new ImageHelper();
                            string encodestring = imgHelper.encodeImageFile(hpf);
                            string[] mang = Request.Url.AbsoluteUri.ToString().Split('/');
                            string url = mang[0] + "//" + mang[2];
                            ava.Avatar = url + "/Content/Images/Avatar/" + encodestring;
                            imgHelper.ResizeStream(180, hpf.InputStream, Path.Combine(AppDomain.CurrentDomain.BaseDirectory + "Content\\Images\\Avatar\\", encodestring));
                            //  ava.Avatar = "http://localhost:41372/Content/Images/Avatar/" + encodestring;
                            db.SubmitChanges();
                            //var path = Path.Combine(Server.MapPath("~/Content/Images/Avatar"), encodestring);
                            //hpf.SaveAs(path);
                            Response.Cookies["ftavatar"].Value = ava.Avatar;
                        }
                        if (hpf.FileName != "" && i == 2)
                        {
                            ImageHelper imgHelper = new ImageHelper();
                            string encodestring = imgHelper.encodeImageFile(hpf);
                            imgHelper.ResizeStream(180, hpf.InputStream, Path.Combine(AppDomain.CurrentDomain.BaseDirectory + "Content\\Images\\TaiKhoan\\", encodestring));
                            //var path = Path.Combine(Server.MapPath("~/Content/Images/TaiKhoan"), encodestring);
                            //hpf.SaveAs(path);
                            DA.cnHinhAnh = encodestring;
                        }
                        if (hpf.FileName != "" && i == 3)
                        {
                            ImageHelper imgHelper = new ImageHelper();
                            string encodestring = imgHelper.encodeImageFile(hpf);
                            imgHelper.ResizeStream(180, hpf.InputStream, Path.Combine(AppDomain.CurrentDomain.BaseDirectory + "Content\\Images\\TaiKhoan\\", encodestring));
                            //var path = Path.Combine(Server.MapPath("~/Content/Images/TaiKhoan"), encodestring);
                            //hpf.SaveAs(path);
                            DA.ctHinhAnh = encodestring;
                        }
                    }
                }
                #endregion

                try
                {
                    if(DA.flag != "XemTruoc")
                        duan.Flag = DA.flag;
                    #region Cập nhật dữ liệu TAB MÔ TẢ
                    if (DA.flag == "MoTa")
                    {
                        duan.NoiDung = DA.NoiDung;
                        duan.LinkVideo = DA.LinkVideo;
                        duan.RuiRo = DA.RuiRo;
                    }

                    #endregion

                    #region xử lý trạng thái XEM TRƯỚC
                    if (DA.flag == "XemTruoc")
                    {

                    }
                    #endregion

                    //if (duan.TrangThai < 1)
                    //{
                    #region Cập nhật dữ liệu TAB THÔNG TIN
                    if (DA.flag == "ThongTin")
                    {
                        duan.LoaiHinhGoiVon = DA.Loaihinhkeugoivon;
                        duan.TenDuAn = DA.TenDuAn;
                        duan.TienDauTuMucTieu = DA.TienDauTuMucTieu;
                        duan.ThongTinNgan = DA.ThongTinNgan;

                        duan.IdDanhMuc = DA.IdDanhMuc;
                        duan.TienDauTuHienTai = 0;

                        duan.ThongTinWhichPerson = DA.whichpreson;
                        if (DA.SoNgay != null && DA.SoNgay > 0)
                        {
                            duan.SoNgay = DA.SoNgay;
                            int i = Convert.ToInt16(DA.SoNgay);
                            duan.ThoiGianKetThuc = DateTime.Now.AddDays(i);
                        }
                        else
                        {
                            if (DA.ThoiGianKetThuc != null)
                            {
                                duan.ThoiGianKetThuc = Convert.ToDateTime(DA.ThoiGianKetThuc);
                                DateTime i = Convert.ToDateTime(DA.ThoiGianKetThuc);
                                // duan.SoNgay = Math.Abs(i.Subtract(DateTime.Now).Days);
                                duan.SoNgay = null;
                            }
                        }

                        //#region  // Phần thêm mới danh mục, hiện tại chưa dùng
                        //try
                        //{
                        //    var dm = db.EntityChiTietDanhMucs.Where(p => p.IdDuAn == DA.Id).ToList();
                        //    if (dm.Count() > 0)
                        //    {
                        //        dm.First().IdDanhMuc = Convert.ToInt16(DA.IdDanhMuc);
                        //        db.SubmitChanges();
                        //    }
                        //    else
                        //    {
                        //        EntityChiTietDanhMuc ct = new EntityChiTietDanhMuc();
                        //        ct.IdDanhMuc = Convert.ToInt16(DA.IdDanhMuc);
                        //        ct.IdDuAn = DA.Id;
                        //        db.EntityChiTietDanhMucs.InsertOnSubmit(ct);
                        //        db.SubmitChanges();
                        //    }
                        //}
                        //catch { }
                        //#endregion
                     }
                    //}
                    db.SubmitChanges();
                    #endregion

                    #region Cập nhật dữ liệu TAB PHẦN THƯỞNG
                    try
                    {
                        if (DA.flag == "PhanThuong")
                        {
                            if (DA.PhanThuong != null)
                            {
                                string[] arr1 = DA.PhanThuong.Split('#');
                                var phanthuong = db.EntityPhanThuongs.Where(g => g.IdDuAn == DA.Id).ToList();
                                for (int i = 0; i < DA.vt_pt_edit; i++)
                                {
                                    string[] arr2 = arr1[i].Split('^');
                                    for (int j = 0; j < arr2.Count() / 4; j = j + 4)
                                    {
                                        EntityPhanThuong pt = phanthuong.ElementAt(i);
                                        if (db.EntityDauTus.Any(g => g.IdPhanthuong == pt.Id) == true)
                                            continue;
                                        pt.IdDuAn = DA.Id;
                                        int tienhotro = 0;
                                        int.TryParse(arr2[j], out tienhotro);
                                        pt.TienHoTro = tienhotro;
                                        pt.HinhThucGiao = arr2[2];
                                        pt.NgayGiao = arr2[1];
                                        if (arr2[3] != "")
                                        {
                                            int SoLuong = 0;
                                            int.TryParse(arr2[3], out SoLuong);
                                            pt.SoLuong = SoLuong;
                                        }
                                        pt.NoiDung = arr2[4];

                                        //db.EntityPhanThuongs.InsertOnSubmit(pt);
                                        db.SubmitChanges();

                                    }
                                }
                                for (int i = DA.vt_pt_edit; i < arr1.Count() - 1; i++)
                                {
                                    string[] arr2 = arr1[i].Split('^');
                                    for (int j = 0; j < arr2.Count() / 4; j = j + 4)
                                    {
                                        EntityPhanThuong pt = new EntityPhanThuong();
                                        pt.IdDuAn = DA.Id;
                                        int tienhotro = 0;
                                        int.TryParse(arr2[j], out tienhotro);
                                        pt.TienHoTro = tienhotro;
                                        pt.HinhThucGiao = arr2[2];
                                        pt.NgayGiao = arr2[1];
                                        if (arr2[3] != "")
                                        {
                                            int SoLuong = 0;
                                            int.TryParse(arr2[3], out SoLuong);
                                            pt.SoLuong = SoLuong;
                                        }
                                        pt.NoiDung = arr2[4];

                                        db.EntityPhanThuongs.InsertOnSubmit(pt);
                                    }
                                }
                            }
                            db.SubmitChanges();
                        }
                    }
                    catch { }
                    #endregion

                    #region Cập nhật dữ liệu TAB GIỚI THIỆU
                    try
                    {
                        if (DA.flag == "GioiThieu")
                        {
                            var user = db.EntityUsers.Where(p => p.Id == Convert.ToInt16(Request.Cookies["ftid"].Value)).First();
                            user.HoTen = DA.HoTen;
                            user.GioiThieu = DA.GioiThieu;
                            user.DiaDiem = DA.ChoO;
                            user.Website = DA.WebCaNhan;
                            db.SubmitChanges();
                        }
                    }
                    catch { }
                    #endregion

                    #region Cập nhật dữ liệu TAB TÀI KHOẢN
                    try
                    {
                        if (DA.flag == "TaiKhoan")
                        {
                            //if (duan.TrangThai < 1 )
                            //{
                            EntityThongTinTaiKhoan thongtintaikhoan = db.EntityThongTinTaiKhoans.Where(g => g.Id == duan.IdThongTinTaiKhoan).FirstOrDefault();
                            if (thongtintaikhoan != null)
                            {
                                if (DA.whichpreson == "Cá nhân")
                                {
                                    thongtintaikhoan.Col1 = DA.cnName;
                                    thongtintaikhoan.Col2 = DA.cnYear;
                                    thongtintaikhoan.Col3 = DA.cnMonth;
                                    thongtintaikhoan.Col4 = DA.cnDay;
                                    thongtintaikhoan.Col5 = DA.cnHomeaddress1;
                                    thongtintaikhoan.Col6 = DA.cnHomeaddress2;
                                    thongtintaikhoan.Col7 = DA.cnHinhAnh != null && DA.cnHinhAnh != "" ? DA.cnHinhAnh : thongtintaikhoan.Col7;
                                }
                                else if (DA.whichpreson == "Doanh nghiệp")
                                {
                                    thongtintaikhoan.Col1 = DA.ctTenCongTy;
                                    thongtintaikhoan.Col2 = DA.ctDiaChi;
                                    thongtintaikhoan.Col3 = DA.ctMST;
                                    thongtintaikhoan.Col4 = DA.ctSDTBan;
                                    thongtintaikhoan.Col5 = DA.ctTenNguoiDaiDien;
                                    thongtintaikhoan.Col6 = DA.ctChucVu;
                                    thongtintaikhoan.Col7 = DA.ctHinhAnh != null && DA.ctHinhAnh != "" ? DA.ctHinhAnh : thongtintaikhoan.Col7;
                                }
                                db.SubmitChanges();
                            }
                            //}
                        }
                    }
                    catch { }
                    #endregion
                }
                catch { }

                #region Xuất Bản
                try
                {
                    if (DA.TrangThai == 1 && DA.CheckGioiThieu && DA.CheckMoTa && DA.CheckPhanThuong && DA.CheckThongTin && DA.CheckTaiKhoan)
                    {
                        duan.Flag = "XemTruoc";
                        //duan.ThoiGianBatDau =
                        duan.TrangThai = DA.TrangThai;
                        duan.ThoigianSummit = DateTime.Now;
                        string[] mang = Request.Url.AbsoluteUri.ToString().Split('/');
                        string url = mang[0] + "//" + mang[2];
                        string linkdanhmucduan = url + "/du-an?option=-category=" + duan.IdDanhMuc + "-blend=0-";
                        MailHelper.SendMail_DangKyDuAn(duan.EntityUser.HoTen, duan.EntityUser.Email, linkdanhmucduan);
                        db.SubmitChanges();
                    }
                }
                catch { }
                #endregion

                // return View(DuAnModel.LayModel(duan.Id));
                return RedirectToAction("ChiTietDuAn", "DuAn", new { Title = Utilities.Paste_Int64(Utilities.Encode(DA.TenDuAn), DA.Id.Value) });
            }
            catch
            { return View(DA); }
        }
        public ActionResult ThanhToan_Lan2(int IdDuAn, int PhanThuong, int SoTien)
        {
            var item = db.EntityPhanThuongs.Where(p => p.IdDuAn == IdDuAn).ToList();
            if (item.Count() > 0)
            {
                EntityPhanThuong phanthuong = item.Where(g => g.Id == PhanThuong).FirstOrDefault();
                if (phanthuong != null)
                    ViewBag.phanthuong = phanthuong;
                else
                {
                    phanthuong = new EntityPhanThuong();
                    phanthuong.Id = 0;
                    ViewBag.phanthuong = phanthuong;
                }
                // phanthuong.TienHoTro;
            }
            ViewBag.DuAn = db.EntityDuAns.Where(p => p.Id == IdDuAn).First();
            ViewBag.SoTien = SoTien;
            ViewBag.pt = "#" + PhanThuong;
            return View(item);
            //try
            //{
            //    int ketqua = 0;
            //    int idlogin = Convert.ToInt16(Request.Cookies["ftid"].Value);
            //    if (PhanThuong > 0)
            //    {
            //        if (db.EntityDauTus.Any(g => g.IdDuAn == IdDuAn && g.IdUser == idlogin) == true)
            //            return View();

            //        EntityDauTu item = new EntityDauTu();
            //        item.IdDuAn = IdDuAn;
            //        item.IdPhanthuong = PhanThuong;
            //        item.SoTienDauTu = SoTien;
            //        item.IdUser = idlogin;
            //        item.ThoiGian = DateTime.Now;
            //        item.TrangThai = 0;
            //        db.EntityDauTus.InsertOnSubmit(item);

            //        db.SubmitChanges();
            //        ketqua = item.Id;

            //        EntityDuAn duan = db.EntityDuAns.Where(g => g.Id == IdDuAn).First();
            //        int songuoidautu = duan.SoNguoiDaDauTu == null ? 0 : duan.SoNguoiDaDauTu.Value;
            //        songuoidautu++;
            //        duan.SoNguoiDaDauTu = songuoidautu;
            //        int tienhientai = duan.TienDauTuHienTai == null ? 0 : duan.TienDauTuHienTai.Value;
            //        duan.TienDauTuHienTai = tienhientai + SoTien;

            //        db.SubmitChanges();

            //        // them hoat động
            //        HoatDongModel.GetListFriend_Sent_HoatDong(idlogin, IdDuAn, 4);// đầu tư
            //        if (duan.TienDauTuHienTai >= duan.TienDauTuMucTieu)
            //        {
            //            duan.TrangThaiFund = 1;
            //            db.SubmitChanges();
            //            HoatDongModel.GetListFriend_Sent_HoatDong(idlogin, IdDuAn, 4);// dự án thành công
            //        }
            //    }

            //}
            //catch
            //{

            //}

            //return View();
            //var item = db.EntityDauTus.Where(p => p.Id == Id).FirstOrDefault();
            //return View(item);
        }