Exemplo n.º 1
0
        private void btn_SaveHD_Click(object sender, EventArgs e)
        {
            string   tensp       = cbb_TenSP.SelectedItem.ToString();
            DateTime today       = DateTime.Today;
            string   sdt         = txt_SDT.Text;
            int      ma          = _khachHangDao.GetMaKH(sdt);
            int      masp        = _sanPham.GetMaSP(tensp);
            bool     kt          = KiemTra_ControlHD();
            int      soluong     = Convert.ToInt32(txt_SoLuong.Text);
            int      tongmathang = 0;
            int      tongtien    = 0;

            for (int i = 0; i < dataGridView_ChiTiet.Rows.Count; i++)
            {
                tongmathang += Convert.ToInt32(dataGridView_ChiTiet.Rows[i].Cells[3].Value.ToString());
                tongtien    += Convert.ToInt32(dataGridView_ChiTiet.Rows[i].Cells[5].Value.ToString());
            }
            if (kt == true)
            {
                HOADON hd = new HOADON
                {
                    MaKH        = ma,
                    TongMatHang = tongmathang,
                    TongTien    = tongtien,
                    NgayLap     = today
                };
                int kq = _hoaDonDao.Add(hd);

                dataGV_HoaDon.DataSource = ListModel_HD(_hoaDonDao.GetList());

                if (kq == 1)
                {
                    // add Chi Tiết Hóa Đơn
                    foreach (var item in dsChthd)
                    {
                        item.MaHD = hd.MaHD;
                        _chiTietHoaDonDao.Add(item);
                        SANPHAM sp = new SANPHAM {
                            MaSP    = item.MaSP,
                            SoLuong = item.SoLuong
                        };
                        _sanPham.UpdateSoLuong(sp, soluong);
                    }
                    // ===============================
                    MessageBox.Show("Thêm Hóa đơn Thành công", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    panel_themhoadon.Visible = false;
                    panel_dshoadon.Visible   = true;
                    dataGV_HoaDon.Visible    = true;
                    UC_HoaDon_Load(sender, e);
                }
            }
            else
            {
                MessageBox.Show("Bạn Phải Nhập Đủ Các Thuộc Tính", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }
        protected void btn_datHang_Click(object sender, EventArgs e)
        {
            if (Session["User"] == null)
            {
                Response.Redirect("~/Login/login.aspx");
            }
            else
            {
                HOADON  hd      = new HOADON();
                DONHANG donHang = new DONHANG();
                int     maKH    = Convert.ToInt32(Session["User"]);;
                hd.MaKH = maKH;

                if (hdDao.Add(hd) == 1)
                {
                    double tongtien = 0;
                    if (CartItemGridView.Rows.Count > 0)
                    {
                        foreach (GridViewRow row in CartItemGridView.Rows)
                        {
                            CHITIETHOADON ct = new CHITIETHOADON();
                            SANPHAM       sp = new SANPHAM();

                            ct.MaHD = hd.MaHD;
                            ct.MaSP = Convert.ToInt32(row.Cells[0].Text);
                            TextBox sl = (TextBox)row.FindControl("txt_SoLuong");
                            ct.SoLuong = Convert.ToInt32(sl.Text);
                            Label  giastr = (Label)row.FindControl("txt_gia");
                            double gia    = Convert.ToDouble(giastr.Text);
                            ct.ThanhTien = Convert.ToDouble(ct.SoLuong * gia);
                            chiTietHoaDonDao.Add(ct);
                            tongtien += ct.ThanhTien.GetValueOrDefault();

                            // Cập Nhật Lại Số Lượng Sản Phẩm
                            sp.MaSP = Convert.ToInt32(row.Cells[0].Text); // Gán giá trị cho MaSP

                            // SoLuong = số Lượng Hiện Tại - Số Lượng Bán
                            sp.SoLuong = sanPham.GetDVByMa(Convert.ToInt32(row.Cells[0].Text)).SoLuong - Convert.ToInt32(sl.Text);
                            sanPham.UpdateSoLuong(sp);
                        }
                    }
                    hd.TongTien = tongtien;
                    hdDao.Update(hd);
                    //Thêm Hóa Đơn
                    hdDao.Update(hd);

                    // Thêm đơn hàng
                    donHang.MaHD      = hd.MaHD;
                    donHang.MaKH      = maKH;
                    donHang.TrangThai = "Chưa Xác Nhận";
                    donHang.NgayDat   = DateTime.Now;
                    _donHangDao.Add(donHang);

                    Session.Remove("cart");


                    Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "Alert", "alert('Đặt Hàng thành công')", true);
                }
                else
                {
                    Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "Alert", "alert('Đặt Hàng không thành công')", true);
                }
            }

            Page.Response.Redirect(Page.Request.Url.ToString(), true);
        }