Пример #1
0
        private void btnLuu_Click(object sender, EventArgs e)
        {
            string err = "";

            //them mua hang
            MH.MaPhieu          = txtMaPhieu.Text;
            MH.TenPhieu         = txtTenPhieu.Text;
            MH.NgayLapPhieu     = Convert.ToDateTime(deNgayLapPhieu.EditValue);
            MH.SoHoaDonVAT      = txtSoHoaDonVAT.Text;
            MH.SoPhieuVietTay   = txtSoPhieuVietTay.Text;
            MH.MaThanhToan      = lueKieuThanhToan.EditValue.ToString();
            MH.MaHinhThuc       = lueHinhThucThanhToan.EditValue.ToString();
            MH.MaNhaCungCap     = lueNhaCungCap.EditValue.ToString();
            MH.ThoiHanThanhToan = Convert.ToDateTime(deThoiHanThanhToan.EditValue);
            MH.GhiChu           = txtGhiChu.Text;
            MH.MaNhanVien       = lueNhanVien.EditValue.ToString();
            MH.MaKho            = lueKho.EditValue.ToString();
            MH.TongTien         = Convert.ToDouble(txtTongTien.EditValue);
            MH.PTramCK          = Convert.ToInt32(txtPhanTramChuyenKhoan.EditValue);
            MH.Thue             = Convert.ToInt32(txtPhanTramThue.EditValue);
            MH.TuongDuongTien   = Convert.ToDouble(txtTuongDuongTien.EditValue);
            MH.TienThanhToan    = Convert.ToDouble(txtTienThanhToan.EditValue);
            //TK = LayTonKhoTuDataTable();
            if (txtMaPhieu.Text == "" || txtTenPhieu.Text == "" || MH.NgayLapPhieu == Convert.ToDateTime("1/1/0001 12:00:00 AM") || MH.ThoiHanThanhToan == Convert.ToDateTime("1/1/0001 12:00:00 AM") ||
                MH.MaThanhToan == "" || MH.MaHinhThuc == "" || MH.MaNhaCungCap == "" || MH.MaNhanVien == "" || MH.MaKho == "" || MH.TongTien == 0 || MH.TienThanhToan == 0)
            {
                MessageBox.Show("Bạn chưa nhập đầy đủ thông tin");
            }
            else
            {
                if (themOrSua == 1)
                {
                    try
                    {
                        bool f = muaHangBUS.ThemMuaHangBUS(ref err, MH);
                        int  demTonKho = 0, demChiTietPMH = 0;
                        if (f == true)
                        {
                            foreach (DataRow r in dt.Rows)
                            {
                                TK.MaHangHoa = r[2].ToString();
                                TK.MaKho     = lueKho.EditValue.ToString();
                                TK.SoLuong   = Convert.ToInt32(r[3]);
                                bool f1 = tonKhoBUS.ThemTonKhoBUS(ref err, TK);

                                CTPMH.MaChiTietPhieu = "1";
                                CTPMH.MaPhieu        = txtMaPhieu.Text;
                                CTPMH.MaHangHoa      = r[2].ToString();
                                CTPMH.SoLuong        = Convert.ToInt32(r[3]);
                                CTPMH.DonGia         = Convert.ToInt32(r[4]);
                                CTPMH.ThanhTien      = Convert.ToInt32(r[5]);
                                bool f2 = chiTietPhieuMuaHangBUS.ThemChiTietPhieuMuaHangBUS(ref err, CTPMH);
                                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 = muaHangBUS.CapNhatMuaHangBUS(ref err, MH);
                        if (f == true)
                        {
                            DataTable dtDBCu  = chiTietPhieuMuaHangBUS.GetAllChiTietPhieuMuaHangByMaPhieuBUS(CTPMH);
                            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);
                    }
                }
            }
        }