public static bool LuuCTHoaDon_Topping(CTHoaDon_ToppingDTO CTHoaDon_Topping) { SqlConnection connection = DataProvider.GetConnection(); string query = "INSERT INTO CTHoaDon_Topping (ma_cthd, ma_topping, so_luong, don_gia, ghi_chu) VALUES (@maCTHD, @maTopping, @soLuong, @donGia, @ghiChu)"; SqlCommand command = new SqlCommand(query, connection); command.Parameters.Add("@maCTHD", System.Data.SqlDbType.Int, 0).Value = CTHoaDon_Topping.MaCTHD; command.Parameters.Add("@maTopping", System.Data.SqlDbType.Int, 0).Value = CTHoaDon_Topping.MaTopping; command.Parameters.Add("@soLuong", System.Data.SqlDbType.Int, 0).Value = CTHoaDon_Topping.SoLuong; command.Parameters.Add("@donGia", System.Data.SqlDbType.Float, 0).Value = CTHoaDon_Topping.DonGia; command.Parameters.Add("@ghiChu", System.Data.SqlDbType.NVarChar, 255).Value = CTHoaDon_Topping.GhiChu; connection.Open(); int reader = command.ExecuteNonQuery(); connection.Close(); if (reader > 0) { return(true); } return(false); }
private void btnXuatHoaDon_Click(object sender, EventArgs e) { HoaDonDTO hoaDon = new HoaDonDTO(); hoaDon.NhanVienLap = TaiKhoanBUS.LayTaiKhoanDangNhap(); hoaDon.NgayLap = DateTime.Now; hoaDon.TongTien = ((FrmBanHang)this.FindForm()).TinhThanhTien(); hoaDon.TienMat = Convert.ToDouble(txtTienMat.Text.Remove(txtTienMat.Text.Length - 1)); hoaDon.TienThua = Convert.ToDouble(txtTienThua.Text.Remove(txtTienThua.Text.Length - 1)); hoaDon.TrangThai = true; List <CTHoaDonDTO> lsCTHD = new List <CTHoaDonDTO>(); List <CTHoaDon_ToppingDTO> lsCTHD_Topping = new List <CTHoaDon_ToppingDTO>(); int idCTHDMark = 0; // Đánh dấu topping thuộc CTHĐ nào foreach (DataGridViewRow row in dgvHoaDon.Rows) { if (row.Tag.GetType() == typeof(MonDTO)) { CTHoaDonDTO cthd = new CTHoaDonDTO(); cthd.MaHoaDon = HoaDonBUS.LayMaHoaDonMoiNhat() + 1; cthd.MaMon = ((MonDTO)row.Tag).MaMon; cthd.SoLuong = Convert.ToInt32(row.Cells["colSoLuong"].Value); cthd.DonGia = Convert.ToDouble(((MonDTO)row.Tag).GiaTien); if (row.Cells["colGhiChu"].Value != null) { cthd.GhiChu = row.Cells["colGhiChu"].Value.ToString(); } else { cthd.GhiChu = string.Empty; } lsCTHD.Add(cthd); idCTHDMark++; } else { CTHoaDon_ToppingDTO cthd_topping = new CTHoaDon_ToppingDTO(); cthd_topping.MaCTHD = CTHoaDonBUS.LayMaCTHoaDonMoiNhat() + idCTHDMark; cthd_topping.MaTopping = ((ToppingDTO)row.Tag).MaTopping; cthd_topping.SoLuong = Convert.ToInt32(row.Cells["colSoLuong"].Value); cthd_topping.DonGia = Convert.ToDouble(((ToppingDTO)row.Tag).GiaTien); if (row.Cells["colGhiChu"].Value != null) { cthd_topping.GhiChu = row.Cells["colGhiChu"].Value.ToString(); } else { cthd_topping.GhiChu = string.Empty; } lsCTHD_Topping.Add(cthd_topping); } } if (HoaDonBUS.LuuHoaDon(hoaDon, lsCTHD, lsCTHD_Topping)) { if (DialogResult.Yes == MessageBox.Show("Bạn có muốn in hoá đơn?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question)) { FrmHienThiBaoCao frm = new FrmHienThiBaoCao(); frm.HienThiHoaDonMoiNhat(); FrmMain.Instance.TopMost = false; frm.Show(); } ((FrmBanHang)this.FindForm()).QuayLaiManHinhChonMon(true); } else { MessageBox.Show("Có lỗi xảy ra khi tạo hoá đơn", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error); } }