Пример #1
0
 //Nút lưu
 private void btn_Luu_Click(object sender, EventArgs e)
 {
     try
     {
         HoaDonClient client = new HoaDonClient();
         HoaDon       hd     = new HoaDon();
         hd.MaHoaDon    = txt_MaHoaDon.Text;
         hd.NgayXuat    = Convert.ToDateTime(msk_NgayXuat.Text);
         hd.NhanVien    = txt_NhanVien.Text;
         hd.KhachHang   = txt_KhachHang.Text;
         hd.DonDatHang  = txt_DonHang.Text;
         hd.TongTien    = Convert.ToDecimal(txt_TongTien.Text);
         hd.DaThanhToan = Convert.ToDecimal(txt_DaThanhToan.Text);
         hd.ConLai      = Convert.ToDecimal(txt_ConLai.Text);
         List <HoaDonCT> dsct = new List <HoaDonCT>();
         foreach (DataGridViewRow row in dgv_DanhSachChiTiet.Rows)
         {
             HoaDonCT ct = new HoaDonCT();
             ct.MaHoaDon  = txt_MaHoaDon.Text;
             ct.MaSanPham = row.Cells["MaSanPham"].Value.ToString();
             ct.SoLuong   = Convert.ToInt32(row.Cells["SoLuong"].Value);
             ct.DonGia    = Convert.ToDecimal(row.Cells["DonGia"].Value);
             dsct.Add(ct);
         }
         hd.DSChiTiet = dsct.ToArray();
         if (IsInsertOrUpdate && !Checknull())
         {
             if (client.HoaDon_Insert(hd))
             {
                 MessageBox.Show("Thêm hoá đơn thành công");
                 DataTable dtb = new DataTable();
                 dtb.Columns.Add("MaSanPham");
                 dtb.Columns.Add("SoLuong");
                 dtb.Columns.Add("DonGia");
                 txt_MaHoaDon.Text    = client.HoaDon_GetNewID(DateTime.Today.Year, DateTime.Today.Month);
                 txt_MaHoaDon.Enabled = false;
                 txt_NhanVien.Text    = "";
                 txt_KhachHang.Text   = "";
                 txt_DonHang.Text     = "";
                 msk_NgayXuat.Text    = "";
                 txt_TongTien.Text    = "0";
                 txt_TongTien.Enabled = false;
                 txt_DaThanhToan.Text = "0";
                 txt_ConLai.Text      = "0";
                 txt_MaSanPham.Text   = "";
                 txt_TenSanPham.Text  = "";
                 txt_SoLuong.Text     = "";
                 txt_DonGia.Text      = "";
                 LoadMaNhanVien();
                 LoadMaKhachHang();
                 LoadSanPham();
                 LoadDonHang();
                 dgv_DanhSachChiTiet.DataSource = dtb;
             }
             else
             {
                 MessageBox.Show("Thêm hoá đơn thất bại");
             }
         }
         else if (!IsInsertOrUpdate && !Checknull())
         {
             if (client.HoaDon_Update(hd))
             {
                 MessageBox.Show("Cập nhật hoá đơn thành công");
             }
             else
             {
                 MessageBox.Show("Cập nhật hoá đơn thất bại");
             }
         }
     }
     catch (Exception ex)
     {
         MessageBox.Show(ex.Message);
     }
 }