protected void Dat_hang_guest()
    {
        DonHangDTO donhang = new DonHangDTO();

        //Xac dinh tham so
        donhang.Ngay_gio_lap       = DateTime.Parse(Request["Ngay_lap"].ToString());
        donhang.Dia_chi_nhan       = Request["Dia_chi_nhan"];
        donhang.Nguoi_nhan         = Request["Nguoi_nhan"];
        donhang.Ngay_gio_giao_hang = DateTime.Parse(Request["Ngay_giao"].ToString());
        donhang.Gia_tri            = decimal.Parse(Request["Gia_tri"].ToString());
        donhang.Da_dat_hang        = true;
        donhang.Da_thanh_toan      = false;
        donhang.Da_giao_hang       = false;
        donhang.Tien_thue          = decimal.Parse(Request["Tien_thue"].ToString());

        int             madonhang;
        Gio_hang_online gio_hang = (Gio_hang_online)Session["Gio_hang"];

        XL_THE        the = new XL_THE("goc");
        XL_THUOC_TINH thuoc_tinh;

        try
        {
            madonhang = (new DonHangBUS()).ThemDonHang(donhang);
        }
        catch (Exception ex)
        {
            the.Danh_sach_thuoc_tinh.Add(new XL_THUOC_TINH("kq", "Dat hang loi"));
            the.Danh_sach_thuoc_tinh.Add(new XL_THUOC_TINH("flag", "false"));
            string chuoi = the.Chuoi();
            XL_CHUOI.XuatChuoi(Response, chuoi);
            return;
        }

        try
        {
            CTDonHangDTO ctdonhang = new CTDonHangDTO();
            CTDonHangBUS ctdhBUS   = new CTDonHangBUS();
            TagBUS       tagbus    = new TagBUS();

            for (int i = 0; i < gio_hang.DsMonAn.Count; i++)
            {
                Item_online item = (Item_online)gio_hang.DsMonAn[i];

                ctdonhang.Ma_don_hang = madonhang;
                ctdonhang.Ma_item     = item.Ma_item;
                ctdonhang.So_luong    = item.So_luong;
                ctdonhang.Thanh_tien  = item.So_luong * item.Gia;
                ctdonhang.Loai_item   = item.Loai_item;
                ctdonhang.La_qua_tang = false;

                ctdhBUS.ThemChiTietDonHang(ctdonhang);
                tagbus.CapNhatDoUuTien(ctdonhang.Ma_item, ctdonhang.Loai_item);
            }
        }
        catch (Exception ex)
        {
            the.Danh_sach_thuoc_tinh.Add(new XL_THUOC_TINH("kq", "Dat hang loi"));
            the.Danh_sach_thuoc_tinh.Add(new XL_THUOC_TINH("flag", "false"));
            string chuoi = the.Chuoi();
            XL_CHUOI.XuatChuoi(Response, chuoi);
            return;
        }

        the.Danh_sach_thuoc_tinh.Add(new XL_THUOC_TINH("kq", "Dat hang thanh cong"));
        the.Danh_sach_thuoc_tinh.Add(new XL_THUOC_TINH("flag", "true"));
        XL_CHUOI.XuatChuoi(Response, the.Chuoi());

        Session["Gio_hang"] = null;

        return;
    }
    protected void Dat_hang_customer()
    {
        DonHangDTO       donhang   = new DonHangDTO();
        DonHangDinhKyDTO donhangdk = new DonHangDinhKyDTO();

        //Xac dinh tham so
        viewKhachHangDTO vkhDto = (viewKhachHangDTO)Session["khachhang"];

        donhang.Ma_khach_hang        = vkhDto.Ma_nguoi_dung;
        donhang.Ngay_gio_lap         = DateTime.Parse(Request["Ngay_lap"].ToString());
        donhang.Dia_chi_nhan         = Request["Dia_chi_nhan"];
        donhang.Nguoi_nhan           = Request["Nguoi_nhan"];
        donhang.Hinh_thuc_khuyen_mai = int.Parse(Request["HTKM"].ToString());
        donhang.Tien_khuyen_mai      = decimal.Parse(Request["Tien_khuyen_mai"].ToString());
        donhang.Gia_tri = decimal.Parse(Request["Gia_tri"].ToString());
        donhang.Ma_hinh_thuc_thanh_toan = int.Parse(Request["HTTT"].ToString());

        int loaiyeucau = int.Parse(Request["Loai_yeu_cau"].ToString());

        if (loaiyeucau == 0 || loaiyeucau == 1) //lưu or đặt hàng bình thường
        {
            donhang.Loai_don_dat_hang  = 0;     //don hang binh thuong
            donhang.Ngay_gio_giao_hang = DateTime.Parse(Request["Ngay_giao"].ToString());
            if (loaiyeucau == 0)
            {
                donhang.Da_dat_hang = false;
            }
            else
            {
                donhang.Da_dat_hang = true;
            }
        }
        else if (loaiyeucau == 2 || loaiyeucau == 3) //lưu or đặt hàng định kỳ
        {
            donhang.Loai_don_dat_hang = 1;           //đơn hàng định kỳ
            if (loaiyeucau == 2)
            {
                donhang.Da_dat_hang = false;
            }
            else
            {
                donhang.Da_dat_hang = true;
            }

            donhangdk.Loai_dinh_ky  = Request["Loai_dk"].ToString();
            donhangdk.Ngay_bat_dau  = DateTime.Parse(Request["Ngay_bd"].ToString());
            donhangdk.Ngay_ket_thuc = DateTime.Parse(Request["Ngay_kt"].ToString());
            if (donhangdk.Loai_dinh_ky == "Hang Tuan")
            {
                donhangdk.Thu_giao = Request["Thoi_gian_giao"].ToString();
            }
            else if (donhangdk.Loai_dinh_ky == "Hang Thang")
            {
                donhangdk.Ngay_giao = Request["Thoi_gian_giao"].ToString();
            }

            donhangdk.Gio_giao   = DateTime.Parse(Request["Gio_giao"].ToString());
            donhangdk.Tinh_trang = true;
        }

        donhang.Da_thanh_toan = false;
        donhang.Da_giao_hang  = false;
        donhang.Tien_thue     = decimal.Parse(Request["Tien_thue"].ToString());


        int             madonhang;
        Gio_hang_online gio_hang     = (Gio_hang_online)Session["Gio_hang"];
        Gio_hang_online gio_qua_tang = new Gio_hang_online();

        if (Session["Gio_qua_tang"] != null)
        {
            gio_qua_tang = (Gio_hang_online)Session["Gio_qua_tang"];
        }

        XL_THE        the = new XL_THE("goc");
        XL_THUOC_TINH thuoc_tinh;

        try
        {
            madonhang = (new DonHangBUS()).ThemDonHang(donhang);
        }
        catch (Exception ex)
        {
            the.Danh_sach_thuoc_tinh.Add(new XL_THUOC_TINH("kq", "Dat hang loi"));
            the.Danh_sach_thuoc_tinh.Add(new XL_THUOC_TINH("flag", "false"));
            string chuoi = the.Chuoi();
            XL_CHUOI.XuatChuoi(Response, chuoi);
            return;
        }
        //lưu or đặt hàng định kỳ
        if (donhang.Loai_don_dat_hang == 1)
        {
            donhangdk.Ma_don_hang = madonhang;
            try
            {
                (new DonHangDinhKyBUS()).ThemDonHangDinhKy(donhangdk);
            }
            catch (Exception ex)
            {
                (new DonHangDinhKyBUS()).XoaDonHang(madonhang);  //rollback
                the.Danh_sach_thuoc_tinh.Add(new XL_THUOC_TINH("kq", "Dat hang dinh ky loi"));
                the.Danh_sach_thuoc_tinh.Add(new XL_THUOC_TINH("flag", "false"));
                string chuoi = the.Chuoi();
                XL_CHUOI.XuatChuoi(Response, chuoi);
                return;
            }
        }

        try
        {
            CTDonHangDTO ctdonhang = new CTDonHangDTO();
            CTDonHangBUS ctdhBUS   = new CTDonHangBUS();
            TagBUS       tagbus    = new TagBUS();

            for (int i = 0; i < gio_hang.DsMonAn.Count; i++)
            {
                Item_online item = (Item_online)gio_hang.DsMonAn[i];

                ctdonhang.Ma_don_hang = madonhang;
                ctdonhang.Ma_item     = item.Ma_item;
                ctdonhang.So_luong    = item.So_luong;
                ctdonhang.Thanh_tien  = item.So_luong * item.Gia;
                ctdonhang.Loai_item   = item.Loai_item;
                ctdonhang.La_qua_tang = false;

                ctdhBUS.ThemChiTietDonHang(ctdonhang);
                tagbus.CapNhatDoUuTien(ctdonhang.Ma_item, ctdonhang.Loai_item);
            }
            if (gio_qua_tang.DsMonAn != null)
            {
                for (int i = 0; i < gio_qua_tang.DsMonAn.Count; i++)
                {
                    Item_online item = (Item_online)gio_qua_tang.DsMonAn[i];
                    ctdonhang.Ma_don_hang = madonhang;
                    ctdonhang.Ma_item     = item.Ma_item;
                    ctdonhang.So_luong    = item.So_luong;
                    ctdonhang.Thanh_tien  = item.So_luong * item.Gia;
                    ctdonhang.Loai_item   = item.Loai_item;
                    ctdonhang.La_qua_tang = true;

                    ctdhBUS.ThemChiTietDonHang(ctdonhang);
                }
            }

            int diemmoi = (new DonHangBUS()).CapNhatDiemKMTheoQuiDinh(donhang.Ma_khach_hang, vkhDto.Diem_khuyen_mai, donhang.Gia_tri, donhang.Tien_khuyen_mai);
            vkhDto.Diem_khuyen_mai = diemmoi;
        }
        catch (Exception ex)
        {
            if (donhang.Loai_don_dat_hang == 1)
            {
                (new DonHangDinhKyBUS()).XoaDonHangDinhKy(madonhang);   //rollback
            }
            else
            {
                (new DonHangBUS()).XoaDonHang(madonhang);                                             //rollback
            }
            (new KhachHangBUS()).CapNhatDiemKhuyenMai(donhang.Ma_khach_hang, vkhDto.Diem_khuyen_mai); //rollback

            the.Danh_sach_thuoc_tinh.Add(new XL_THUOC_TINH("kq", "Dat hang loi"));
            the.Danh_sach_thuoc_tinh.Add(new XL_THUOC_TINH("flag", "false"));
            string chuoi = the.Chuoi();
            XL_CHUOI.XuatChuoi(Response, chuoi);
            return;
        }
        //neu ko fai chon luu dh
        if (donhang.Da_dat_hang == true)
        {
            // Dat hang thanh cong + chon thanh toan = the tin dung -> tien hanh thanh toan
            int Thanh_toan = int.Parse(Request["Thanh_toan"].ToString());
            if (Thanh_toan == 1)
            {
                WS_CardSystem.CardDTO cardDto1 = new WS_CardSystem.CardDTO();

                //Neu su dung the khac
                int The_khac = int.Parse(Request["The_khac"].ToString());
                ThanhToanTheTinDungDTO thett = new ThanhToanTheTinDungDTO();
                if (The_khac == 1)
                {
                    thett.Ma_don_hang  = madonhang; //don hang vua them
                    thett.Ma_loai_the  = int.Parse(Request["Ma_loai_the"].ToString());
                    thett.So_the       = Request["So_the"].ToString();
                    thett.Ngay_het_han = DateTime.Parse(Request["Ngay_hh"].ToString());

                    //them vao csdl the su dung
                    try
                    {
                        (new ThanhToanTheTinDungBUS()).ThemThanhToanTheTinDung(thett);
                    }
                    catch (Exception ex)
                    {
                        if (donhang.Loai_don_dat_hang == 1)
                        {
                            (new DonHangDinhKyBUS()).XoaDonHangDinhKy(madonhang);   //rollback
                        }
                        else
                        {
                            (new DonHangBUS()).XoaDonHang(madonhang);                                             //rollback
                        }
                        (new KhachHangBUS()).CapNhatDiemKhuyenMai(donhang.Ma_khach_hang, vkhDto.Diem_khuyen_mai); //rollback

                        the.Danh_sach_thuoc_tinh.Add(new XL_THUOC_TINH("kq", "Dat hang loi"));
                        the.Danh_sach_thuoc_tinh.Add(new XL_THUOC_TINH("flag", "false"));
                        string chuoi = the.Chuoi();
                        XL_CHUOI.XuatChuoi(Response, chuoi);
                        return;
                    }

                    //The khac
                    cardDto1.Code = thett.So_the;
                    string tenloaithe = Request["Ten_loai_the"].ToString();
                    cardDto1.Type = tenloaithe;
                }
                else
                {
                    //the mac dinh
                    cardDto1.Code = vkhDto.So_the;
                    cardDto1.Type = vkhDto.Ten_loai_the;
                }

                //thanh toan
                bool kq = ThanhToan(donhang.Gia_tri, the, cardDto1);
                if (kq == true)
                {
                    (new DonHangBUS()).CapNhatTrangThaiDaThanhToan(madonhang, true);
                }
                else
                {
                    (new ThanhToanTheTinDungBUS()).XoaThanhToanTheTinDung(thett.Ma_don_hang);
                    if (donhang.Loai_don_dat_hang == 1)
                    {
                        (new DonHangDinhKyBUS()).XoaDonHangDinhKy(madonhang);   //rollback
                    }
                    else
                    {
                        (new DonHangBUS()).XoaDonHang(madonhang);                                             //rollback
                    }
                    (new KhachHangBUS()).CapNhatDiemKhuyenMai(donhang.Ma_khach_hang, vkhDto.Diem_khuyen_mai); //rollback

                    the.Danh_sach_thuoc_tinh.Add(new XL_THUOC_TINH("kq", "Thanh toan loi, dat hang khong thanh cong \n Vui long kiem tra lai the su dung"));
                    the.Danh_sach_thuoc_tinh.Add(new XL_THUOC_TINH("flag", "false"));
                    string chuoi = the.Chuoi();
                    XL_CHUOI.XuatChuoi(Response, chuoi);
                    return;
                }
            }
            thuoc_tinh = new XL_THUOC_TINH("kq", "Dat hang thanh cong");
        }
        else
        {
            Session["madonhangluu"] = madonhang;    //luu lại mã đh để trh khách hàng chọn đặt mua sau đó
            thuoc_tinh = new XL_THUOC_TINH("kq", "Luu don hang thanh cong");
        }
        the.Danh_sach_thuoc_tinh.Add(thuoc_tinh);
        the.Danh_sach_thuoc_tinh.Add(new XL_THUOC_TINH("flag", "true"));
        XL_CHUOI.XuatChuoi(Response, the.Chuoi());

        Session["Gio_qua_tang"]      = null;
        Session["Gio_hang"]          = null;
        Session["FormDatHangDinhKy"] = null;
        Session["FormDatHang"]       = null;

        return;
    }