public FrontController()
     : base()
 {
     this._khoitao_cookie();
     this._khachhang = null;
     this._giohang = new DonHang();
 }
 public int add(DonHang obj_)
 {
     DonHang obj = this._Clone(obj_);//rất quan trọng thưa các bạn,
     //tính toán các dữ liệu cần thiết
         //cập nhật trạng thái đơn hàng
         obj.trangthai = "chualienlac";
         //tính phí
         obj.phivanchuyen = obj.nguoinhan_diachi_tinhtp.phivanchuyen;
         obj.tongtien = obj.__get_tongtien_notinclude_phivanchuyen() + obj.phivanchuyen;
         //tính điểm cho KH nếu là kh mua
         if (obj.khachhang != null)
         {
             obj.khachhang.diem += obj.tongtien / 10000;//10000 = 1 điểm
             //cập nhật lại loại KH
             obj.khachhang._Update_LoaiKhachHang(this._db);
             //giảm giá trực tiếp trên tổng tiền nếu KH có uu đãi
             UuDai udtmp = obj.khachhang.loaikhachhang._get_uudai();
             if (udtmp != null)
             {
                 obj.tongtien -= udtmp.giatri * obj.tongtien / 100;
             }
         }
         //trừ tồn kho ngay lập tức
         foreach (var item in obj.ds_chitiet_donhang)
         {
             item.chitietsp.soluong -= item.soluong;
             if (item.chitietsp.soluong < 0)
             {
                 item.chitietsp.soluong = 0;
             }
         }
     //call add
     this._db.ds_donhang.Add(obj);
     //commit
     this._db.SaveChanges();
     //return ma moi nhat
     return this._db.ds_donhang.Max(x => x.id);
 }
 /* Sử dụng khi lưu đon hàng, do có quá nhiều DBContext tracking nên bắt buộc phải Clone ra obj mới theo
  * this._db thì mới lưu được không sẽ bị báo lỗi
  *
  */
 private DonHang _Clone(DonHang obj)
 {
     DonHang tmp = new DonHang();
     tmp.active = obj.active;
     tmp.ds_chitiet_donhang = new List<ChiTiet_DonHang>();
     foreach (var item in obj.ds_chitiet_donhang)
     {
         ChiTiet_DonHang ct = new ChiTiet_DonHang();
         ct.chitietsp = this._db.ds_chitietsp.Where(x => x.id == item.chitietsp.id).FirstOrDefault();
         ct.dongia = item.dongia;
         ct.soluong = item.soluong;
         tmp.ds_chitiet_donhang.Add(ct);
     }
     if (obj.khachhang != null)
     {
         tmp.khachhang = this._db.ds_khachhang.Where(x => x.id == obj.khachhang.id).FirstOrDefault();
     }
     if (obj.khachhang_nhanvien != null)
     {
         tmp.khachhang_nhanvien = this._db.ds_nhanvien.Where(x => x.id == obj.khachhang_nhanvien.id).FirstOrDefault();
     }
     tmp.ngay = obj.ngay;
     tmp.nguoinhan_diachi = obj.nguoinhan_diachi;
     tmp.nguoinhan_diachi_tinhtp = this._db.ds_tinhtp.Where(x => x.id == obj.nguoinhan_diachi_tinhtp.id).FirstOrDefault();
     tmp.nguoinhan_sdt = obj.nguoinhan_sdt;
     tmp.nguoinhan_ten = obj.nguoinhan_ten;
     if (obj.nhanvien != null)
     {
         tmp.nhanvien = this._db.ds_nhanvien.Where(x => x.id == obj.nhanvien.id).FirstOrDefault();
     }
     tmp.phivanchuyen = obj.phivanchuyen;
     tmp.thanhtoan_tructuyen = obj.thanhtoan_tructuyen;
     tmp.tongtien = obj.tongtien;
     tmp.trangthai = obj.trangthai;
     return tmp;
 }
 public List<string> validate_checkout(DonHang giohang, out DonHang giohang_return)
 {
     List<string> re = new List<string>();
     if (giohang.nguoinhan_ten.Equals(""))
     {
         re.Add("ten_fail");
     }
     if (giohang.nguoinhan_sdt.Equals(""))
     {
         re.Add("sdt_fail");
     }
     if (giohang.nguoinhan_diachi.Equals(""))
     {
         re.Add("diachi_fail");
     }
     if (giohang.nguoinhan_diachi_tinhtp == null)
     {
         re.Add("tinhtp_fail");
         giohang.nguoinhan_diachi_tinhtp = this._db.ds_tinhtp.FirstOrDefault();
     }
     giohang_return = giohang;
     return re;
 }
        public List<string> validate(DonHang giohang, out DonHang giohang_return)
        {
            //
            ChiTietSPController ctr = new ChiTietSPController(this._db);
            //
            List<string> re = new List<string>();
            if (giohang.ds_chitiet_donhang.Count <= 0)
            {
                re.Add("rong_fail");
            }
            //xet soluong vuot qua ton kho hoac dat qua 3...
            ChiTietSP tmp;
            foreach (var item in giohang.ds_chitiet_donhang)
            {
                tmp = ctr.get_by_id(item.chitietsp.id);
                if (item.soluong > 3)
                {
                    re.Add(item.chitietsp.id + "_vuot3_fail");
                    item.soluong = 3;
                }
                if (item.soluong <= 0)
                {
                    re.Add(item.chitietsp.id + "_fail");
                    item.soluong = 1;
                }
                //xet ton kho cuoi cung
                if (tmp.soluong < item.soluong)
                {
                    re.Add(item.chitietsp.id + "_vuottonkho_fail");
                    item.soluong = tmp.soluong;
                }
            }

            giohang_return = giohang;
            return re;
        }
 protected override void OnActionExecuting(ActionExecutingContext filterContext)
 {
     base.OnActionExecuting(filterContext);
     KhachHangController ctr_kh = new KhachHangController();
     NhomSanPhamController ctr = new NhomSanPhamController();
     List<NhomSanPham2> list1 = ctr.timkiem("", "", "", "1");
     SanPhamController ctr2 = new SanPhamController(ctr._db);
     List<SanPham>list2=ctr2.get_bestseller(4);
     if (list1 != null && list2 != null)
     {
         ViewBag.NhomSanPham2_List_All = list1;
         ViewBag.SanPham_BestSeller = list2;
     }
     else
     {
         ViewBag.NhomSanPham2_List_All = new List<NhomSanPham2>();
         ViewBag.SanPham_BestSeller = new List<SanPham>();
     }
     //tim kiem
     //build timkiem_sanpham
     if (Request.Cookies.Get("front_timkiem_sanpham") == null)
     {
         //chưa set cookies trước => tiến hành set cookies
         this._khoitao_cookie();
         this._luu_cookie();
     }
     else
     {
         try
         {
             this.front_timkiem_sanpham = CookieLibrary.Base64Decode(Request.Cookies.Get("front_timkiem_sanpham"));
         }
         catch (Exception)
         {
             this._khoitao_cookie();
             this._luu_cookie();
         }
     }
     ViewBag.front_timkiem_sanpham = this.front_timkiem_sanpham;
     //Load thong tin KhachHang
     if (!this._is_logged_in())
     {
         //Chưa có ai đăng nhập hệ thống
         if (Session["khachhang"] != null)
         {
             //nếu như KH đã đăng nhập rồi
             this._khachhang = ctr_kh.get_by_id(((KhachHang)Session["khachhang"]).id);
         }
         else
         {
             //lấy từ cookies lên
             //lay thong tin tu cookies
             HttpCookie _tmp = Request.Cookies.Get("khachhang");
             if (_tmp != null)
             {
                 int uid = TextLibrary.ToInt(_tmp["khachhang_id"].ToString());
                 string password = TextLibrary.ToString(_tmp["khachhang_password"].ToString());
                 //lay thong tin user theo yeu cau dang nhap
                 this._khachhang = ctr_kh.get_by_id_hash_password(uid, password);
             }
         }
     }
     //
     //
     ViewBag.nhanvien = this._nhanvien;
     ViewBag.khachhang = this._khachhang;
     //get cart
         if (Session["giohang"] != null)
         {
             try
             {
                 this._giohang = (DonHang)Session["giohang"];
             }
             catch (Exception)
             {
                 this._giohang = new DonHang();
             }
         }
         else
         {
             this._giohang = new DonHang();
         }
         //gán khach hang va nhan vien
         this._giohang.khachhang = this._khachhang;
         this._giohang.khachhang_nhanvien = this._nhanvien;
         //save cart
         this._save_cart_to_session();
     ViewBag.giohang = this._giohang;
 }
        public void Generate_DonHang_Html(DonHang obj)
        {
            {
                this.receive_title = "Thông tin đơn hàng mã số ["+obj.id+"] từ Website Cửa hàng giày dép BigFoot";
                this.receive_html = "<div style=\"width:680px\"><a href=\"http://localhost:53655/" + "\" title=\"Cửa hàng BigFoot\" target=\"_blank\"><img src=\"https://lh6.googleusercontent.com/-P2R_CIG6H7E/Upzdb6dcFmI/AAAAAAAAABY/rJJOpMz5wJo/w680-h353-no/CHIEW.gif" + "\" alt=\"Cửa hàng BigFoot\" style=\"margin-bottom:20px;box-shadow: 0px 0px 3px 4px #a1a1a1;max-height:110px\"></a> <p style=\"margin-top:0px;margin-bottom:20px;\">Cám ơn bạn đã quan tâm sản phẩm Cửa hàng BigFoot. Đơn hàng của bạn đang được xử lý, chúng tôi sẽ liên hệ với bạn trong thời gian vòng 72 giờ kể từ khi nhận được email này.</p>";
                this.receive_html += "<table style=\"border-collapse:collapse;width:100%;border-top:1px solid #dddddd;border-left:1px solid #dddddd;margin-bottom:20px\"><thead><tr><td style=\"font-size:12px;border-right:1px solid #dddddd;border-bottom:1px solid #dddddd;background-color:#efefef;font-weight:bold;text-align:left;padding:7px;color:#222222\" colspan=\"2\">Chi tiết đơn hàng</td></tr></thead><tbody><tr>";
                this.receive_html += "<td style=\"font-size:12px;border-right:1px solid #dddddd;border-bottom:1px solid #dddddd;text-align:left;padding:7px\"><b>Mã Đơn Hàng:</b> "+obj.id+"<br>";
                this.receive_html += "<b>Ngày Đặt:</b> "+obj._get_ngay()+"<br>";
                string thanhtoan = "Thanh toán trực tuyến qua OnePay";
                if(obj.thanhtoan_tructuyen==false) thanhtoan="Thanh toán tại nhà (Chưa thanh toán)";
                this.receive_html += "<b>Phương thức thanh toán:</b>" + thanhtoan + "<br> </td>";
                this.receive_html += "<td style=\"font-size:12px;border-right:1px solid #dddddd;border-bottom:1px solid #dddddd;text-align:left;padding:7px\">";
                this.receive_html += "<b>Họ tên khách hàng:</b> "+obj._get_khachhang_tendaydu()+"<br>";
                this.receive_html += "<b>Email:</b> <a href=\"mailto:"+obj._get_khachhang_email()+"\" target=\"_blank\">"+obj._get_khachhang_email()+"</a><br>";
                this.receive_html += "<b>Điện thoại:</b> "+obj.nguoinhan_sdt+"<br></td>";
                this.receive_html += " </tr></tbody>  </table>";
                this.receive_html+="<table style=\"border-collapse:collapse;width:100%;border-top:1px solid #dddddd;border-left:1px solid #dddddd;margin-bottom:20px\"><thead><tr><td style=\"font-size:12px;border-right:1px solid #dddddd;border-bottom:1px solid #dddddd;background-color:#efefef;font-weight:bold;text-align:left;padding:7px;color:#222222\">Địa chỉ nhận hàng</td>";
                this.receive_html += " </tr></thead><tbody><tr> <td style=\"font-size:12px;border-right:1px solid #dddddd;border-bottom:1px solid #dddddd;text-align:left;padding:7px\">"+obj.nguoinhan_diachi+"<br>"+obj.nguoinhan_diachi_tinhtp.ten.ToString()+"<br>Việt Nam</td></tr></tbody></table>";
                this.receive_html += "<table style=\"border-collapse:collapse;width:100%;border-top:1px solid #dddddd;border-left:1px solid #dddddd;margin-bottom:20px\"><thead><tr><td style=\"font-size:12px;border-right:1px solid #dddddd;border-bottom:1px solid #dddddd;background-color:#efefef;font-weight:bold;text-align:left;padding:7px;color:#222222\">Mã sản phẩm</td><td style=\"font-size:12px;border-right:1px solid #dddddd;border-bottom:1px solid #dddddd;background-color:#efefef;font-weight:bold;text-align:left;padding:7px;color:#222222\">Tên sản phẩm</td> <td style=\"font-size:12px;border-right:1px solid #dddddd;border-bottom:1px solid #dddddd;background-color:#efefef;font-weight:bold;text-align:left;padding:7px;color:#222222\">Màu sắc</td> <td style=\"font-size:12px;border-right:1px solid #dddddd;border-bottom:1px solid #dddddd;background-color:#efefef;font-weight:bold;text-align:left;padding:7px;color:#222222\">Kích thước</td><td style=\"font-size:12px;border-right:1px solid #dddddd;border-bottom:1px solid #dddddd;background-color:#efefef;font-weight:bold;text-align:right;padding:7px;color:#222222\">Số lượng</td><td style=\"font-size:12px;border-right:1px solid #dddddd;border-bottom:1px solid #dddddd;background-color:#efefef;font-weight:bold;text-align:right;padding:7px;color:#222222\">Giá</td><td style=\"font-size:12px;border-right:1px solid #dddddd;border-bottom:1px solid #dddddd;background-color:#efefef;font-weight:bold;text-align:right;padding:7px;color:#222222\">Tổng cộng</td> </tr></thead>";
                //this.receive_html = "Xin chào "+obj._get_khachhang_tendaydu();
                //this.receive_html += "<br>";
                //this.receive_html += "Bạn đã đặt hàng tại của hàng chúng tôi với thông tin chi tiết như sau";
                //this.receive_html += "<br><br>";
                //this.receive_html += "Mã SP | Tên SP | Màu | Kích thước | Số lượng x Đơn giá = Tổng cộng";
                //this.receive_html += "<br>";
                foreach (var item in obj.ds_chitiet_donhang)
                {
                    this.receive_html += " <tbody><tr>";
                    this.receive_html+="<td style=\"font-size:12px;border-right:1px solid #dddddd;border-bottom:1px solid #dddddd;text-align:left;padding:7px\">";
                    this.receive_html += item.chitietsp.sanpham.masp;
                    this.receive_html += "          </td> ";
                    this.receive_html += "<td style=\"font-size:12px;border-right:1px solid #dddddd;border-bottom:1px solid #dddddd;text-align:left;padding:7px\">";
                    this.receive_html += item.chitietsp.sanpham.ten;
                    this.receive_html += " </td> ";
                    this.receive_html += "<td style=\"font-size:12px;border-right:1px solid #dddddd;border-bottom:1px solid #dddddd;text-align:left;padding:7px\">";
                    this.receive_html += item.chitietsp.mausac.giatri;
                    this.receive_html += " </td> ";
                    this.receive_html += "<td style=\"font-size:12px;border-right:1px solid #dddddd;border-bottom:1px solid #dddddd;text-align:left;padding:7px\">";
                    this.receive_html += item.chitietsp.kichthuoc.giatri;
                    this.receive_html += " </td> ";
                    this.receive_html += "<td style=\"font-size:12px;border-right:1px solid #dddddd;border-bottom:1px solid #dddddd;text-align:left;padding:7px\">";
                    this.receive_html += item.soluong;
                    this.receive_html += " </td> ";
                    this.receive_html += "<td style=\"font-size:12px;border-right:1px solid #dddddd;border-bottom:1px solid #dddddd;text-align:right;padding:7px\">";
                    this.receive_html += item.dongia + "VNĐ";
                    this.receive_html += " </td> ";
                    this.receive_html += "<td style=\"font-size:12px;border-right:1px solid #dddddd;border-bottom:1px solid #dddddd;text-align:right;padding:7px\">";
                    this.receive_html += item._get_total() + "VNĐ";
                    this.receive_html += " </td> ";
                    this.receive_html += "  </tr></tbody>";
                }
                this.receive_html += "  <tfoot>";
                this.receive_html += "<tr><td style=\"font-size:12px;border-right:1px solid #dddddd;border-bottom:1px solid #dddddd;text-align:right;padding:7px\" colspan=\"6\"><b>Thành tiền::</b></td>";
                this.receive_html+="<td style=\"font-size:12px;border-right:1px solid #dddddd;border-bottom:1px solid #dddddd;text-align:right;padding:7px\">"+obj._get_tongtien_notinclude_phivanchuyen() + "VNĐ </td></tr>";
                this.receive_html += "<tr><td style=\"font-size:12px;border-right:1px solid #dddddd;border-bottom:1px solid #dddddd;text-align:right;padding:7px\" colspan=\"6\"><b>Phí vận chuyển::</b></td>";
                this.receive_html += "<td style=\"font-size:12px;border-right:1px solid #dddddd;border-bottom:1px solid #dddddd;text-align:right;padding:7px\">" + obj._get_phivanchuyen() + "VNĐ </td></tr>";
                this.receive_html += "<tr><td style=\"font-size:12px;border-right:1px solid #dddddd;border-bottom:1px solid #dddddd;text-align:right;padding:7px\" colspan=\"6\"><b>Giảm giá::</b></td>";
                this.receive_html += "<td style=\"font-size:12px;border-right:1px solid #dddddd;border-bottom:1px solid #dddddd;text-align:right;padding:7px\">" + obj._get_giamgia_tuloaikh() + "VNĐ </td></tr>";
                this.receive_html += "<tr><td style=\"font-size:12px;border-right:1px solid #dddddd;border-bottom:1px solid #dddddd;text-align:right;padding:7px\" colspan=\"6\"><b>Tổng tiền::</b></td>";
                this.receive_html += "<td style=\"font-size:12px;border-right:1px solid #dddddd;border-bottom:1px solid #dddddd;text-align:right;padding:7px\">" + obj._get_tongtien_include_phivanchuyen_giamgia_tuloaikh() + "VNĐ </td></tr>";

                //this.receive_html += "<br>";
                //this.receive_html += "Phí vận chuyển: " + obj._get_phivanchuyen() + "VNĐ";
                //this.receive_html += "<br>";
                //this.receive_html += "Giảm giá: " + obj._get_giamgia_tuloaikh() + "VNĐ";
                //this.receive_html += "<br>";
                //this.receive_html += "Tổng tiền phải thanh toán: " + obj._get_tongtien_include_phivanchuyen_giamgia_tuloaikh() + "VNĐ";
                //this.receive_html += "<hr>";
                //if (obj.thanhtoan_tructuyen)
                //{
                //    this.receive_html += "<br>";
                //    this.receive_html += "Đơn hàng đã được thanh toán qua cổng thanh toán trực tuyến PnePay";
                //}
                //else
                //{
                //    this.receive_html += "<br>";
                //    this.receive_html += "Đơn hàng chưa thanh toán, nhân viên chúng tôi sẽ liên lạc với bạn và giao hàng trong thời giân ngắn nhất (tối đa là 72h kể từ khi đơn hàng được ghi nhận), bạn sẽ thanh toán khi nhân viên giao hàng yêu cầu";
                //}
                this.receive_html += "</tfoot></table>";
                this.receive_html += "<p style=\"margin-top:0px;margin-bottom:20px\">Vui lòng trả lời thư này nếu có bất kì câu hỏi nào.</p>";
                this.receive_html+="<p style=\"margin-top:0px;margin-bottom:20px\">Nhân viên của chúng tôi sẽ liên lạc với bạn và giao hàng trong thời giân ngắn nhất (tối đa là 72h kể từ khi đơn hàng được ghi nhận).</p>";
                this.receive_html+="<p style=\"margin-top:0px;margin-bottom:20px\">Đối với đơn hàng thanh toán tại nhà, nhân viên giao hàng sẽ nhận tiền thanh toán khi giao hàng.</p>";
                this.receive_html += "<p style=\"margin-top:0px;margin-bottom:20px\">Copyright © 2013 quocdunginfo ft kienkimkhung Corp. | Special thanks to Joomla for this email template.</p><div class=\"yj6qo\"></div><div class=\"adL\"></div></div>";
            }
        }