void XoaChiTietPhieuMuaHang_TonKho_dtDBMoi(DataTable dtmoi, DataTable dtcu) { string err = ""; foreach (DataRow rCu in dtcu.Rows) { if (KiemTraMaHangHoaTonTaiTrongDB(dtmoi, rCu[2].ToString()) == false)//ko ton tai trong dbmoi { //cap nhat tonkho TK.MaHangHoa = rCu[2].ToString(); TK.MaKho = lueKho.EditValue.ToString(); TK.SoLuong = Convert.ToInt32(rCu[3]); bool f = tonKhoBUS.XuatTonKhoBUS(ref err, TK); if (f == true) { MessageBox.Show("xuat ton kho thanh cong, Ma Hang Hoa:" + TK.MaHangHoa + ", Kho:" + TK.MaKho + ", so luong:" + TK.SoLuong); } // xoa chi tiet phieu CTPMH.MaChiTietPhieu = "1"; CTPMH.MaPhieu = txtMaPhieu.Text; CTPMH.MaHangHoa = rCu[2].ToString(); CTPMH.SoLuong = Convert.ToInt32(rCu[3]); CTPMH.DonGia = Convert.ToInt32(rCu[4]); CTPMH.ThanhTien = Convert.ToInt32(rCu[5]); bool f1 = chiTietPhieuMuaHangBUS.XoaChiTietPhieuMuaHangByMaPhieuMaHangHoaBUS(ref err, CTPMH); if (f1 == true) { MessageBox.Show("xoa thanh cong chi tietphieumuahang, MaPhieu: " + CTPMH.MaPhieu + ", MahangHoa:" + CTPMH.MaHangHoa); } } } }
private void btnXoa_Click(object sender, EventArgs e) { DialogResult result = MessageBox.Show("Bạn có muốn xóa không?", "Question", MessageBoxButtons.OKCancel, MessageBoxIcon.Question); if (result == DialogResult.OK) { string err = ""; try { bool f = muaHangBUS.XoaMuaHangBUS(ref err, MH); bool f1 = false; int demXoaTK = 0; if (f == true) { DataTable dtChiTietPMH = new DataTable(); dtChiTietPMH = chiTietPhieuMuaHangBUS.GetAllChiTietPhieuMuaHangByMaPhieuBUS(CTPMH); f1 = chiTietPhieuMuaHangBUS.XoaChiTietPhieuMuaHangByMaPhieuBUS(ref err, CTPMH); MessageBox.Show("Xoa thanh cong muahang"); if (f1 == true) { MessageBox.Show("Xoa thanh cong chitietphieumuahang"); loadData(); foreach (DataRow r in dtChiTietPMH.Rows) { TK.MaHangHoa = r[2].ToString(); TK.MaKho = MH.MaKho; TK.SoLuong = Convert.ToInt32(r[3]); bool f2 = tonKhoBUS.XuatTonKhoBUS(ref err, TK); if (f2 == true) { demXoaTK++; } } MessageBox.Show("Xoa thanh cong chitietmuahang, ma phieu:" + CTPMH.MaPhieu); } if (demXoaTK == dtChiTietPMH.Rows.Count) { MessageBox.Show("Xuat thanh cong ton kho,Soluong" + demXoaTK); } loadData(); } else { MessageBox.Show("Khong Xoa duoc mua hang. Loi: " + err); } } catch (SqlException) { MessageBox.Show("Khong Xoa duoc. Loi: " + err); } } }
private void btnLuu_Click(object sender, EventArgs e) { string err = ""; //them mua hang BH.MaPhieuBan = txtMaPhieuBan.Text; BH.TenPhieu = txtTenPhieu.Text; BH.NgayLapPhieu = Convert.ToDateTime(deNgayLapPhieu.EditValue); BH.MaKieuTT = lueKieuThanhToan.EditValue.ToString(); BH.MaHinhThucTT = lueHinhThucThanhToan.EditValue.ToString(); BH.MaKhachHang = lueKhachHang.EditValue.ToString(); BH.ThoiHanThanhToan = Convert.ToDateTime(deThoiHanThanhToan.EditValue); BH.NgayGiaoHang = Convert.ToDateTime(deNgayGiaoHang.EditValue); BH.GhiChu = txtGhiChu.Text; BH.MaNhanVien = lueNhanVien.EditValue.ToString(); BH.MaKho = lueKho.EditValue.ToString(); BH.TongTien = Convert.ToDouble(txtTongTien.Text); BH.PTramCK = Convert.ToInt32(txtPhanTramChuyenKhoan.Text); BH.Thue = Convert.ToInt32(txtPhanTramThue.Text); BH.TuongDuongTien = Convert.ToDouble(txtTuongDuongTien.Text); BH.TienThanhToan = Convert.ToDouble(txtTienThanhToan.Text); if (txtMaPhieuBan.Text == "" || txtTenPhieu.Text == "" || BH.NgayLapPhieu == Convert.ToDateTime("1/1/0001 12:00:00 AM") || BH.NgayGiaoHang == Convert.ToDateTime("1/1/0001 12:00:00 AM") || BH.ThoiHanThanhToan == Convert.ToDateTime("1/1/0001 12:00:00 AM") || BH.MaHinhThucTT == "" || BH.MaKieuTT == "" || BH.MaKhachHang == "" || BH.MaNhanVien == "" || BH.MaKho == "" || BH.TongTien == 0 || BH.TienThanhToan == 0) { MessageBox.Show("Bạn chưa nhập đầy đủ thông tin"); } else { if (themOrSua == 1) { try { bool f = banHangBUS.ThemBanHangBUS(ref err, BH); int demTonKho = 0, demChiTietPMH = 0; if (f == true) { foreach (DataRow r in dt.Rows) { TK.MaHangHoa = r[0].ToString(); TK.MaKho = lueKho.EditValue.ToString(); TK.SoLuong = Convert.ToInt32(r[4]); bool f1 = tonKhoBUS.XuatTonKhoBUS(ref err, TK); CTPBH.MaChiTietPhieuBan = "1"; CTPBH.MaPhieuBan = txtMaPhieuBan.Text; CTPBH.MaHangHoa = r[0].ToString(); CTPBH.SoLuong = Convert.ToInt32(r[4]); CTPBH.DonGia = Convert.ToInt32(r[5]); CTPBH.ThanhTien = Convert.ToInt32(r[6]); bool f2 = chiTietPhieuBanHangBUS.ThemChiTietPhieuBanHangBUS(ref err, CTPBH); if (f1 == true) { demTonKho++; } if (f2 == true) { demChiTietPMH++; } } MessageBox.Show("Them thanh cong"); } else { MessageBox.Show("Khong them duoc, Loi: " + err); } if (demTonKho == dt.Rows.Count) { MessageBox.Show("Them thanh cong Ton kho: " + demTonKho); } if (demChiTietPMH == dt.Rows.Count) { MessageBox.Show("Them thanh cong Chi tiet phieu mua hang: " + demChiTietPMH); } else { MessageBox.Show("Khong them duoc, so luong them: " + demChiTietPMH); } } catch (SqlException) { MessageBox.Show("Không thêm được. Lỗi: " + err); } } else { try { bool f = banHangBUS.CapNhatBanHangBUS(ref err, BH); if (f == true) { DataTable dtDBCu = chiTietPhieuBanHangBUS.GetAllChiTietPhieuBanHangByMaPhieuBanBUS(CTPBH); DataTable dtDBMoi = dt; // chay theo dtDBCu SuaChiTietPhieuMuaHang_TonKho_dtDBCu(dtDBMoi, dtDBCu); //ThemChiTietPhieuMuaHang_TonKho_dtDBCu(dtDBMoi,dtDBCu); XoaChiTietPhieuMuaHang_TonKho_dtDBMoi(dtDBMoi, dtDBCu); } } catch (SqlException) { MessageBox.Show("Không thêm được. Lỗi: " + err); } } } }