Exemplo n.º 1
0
        private void btnLuu_Click(object sender, EventArgs e)
        {
            if (trangThaiLuu == 0) // Thêm
            {
                eCongTrinh a  = new eCongTrinh(tbTenCongTrinh.Text, tbDiaChi.Text, (DateTime)dateNgayBatDau.Value, (DateTime)dateNgayKetThuc.Value);
                bool       kq = ctBLL.themCongTrinh(a);

                if (kq)
                {
                    XtraMessageBox.Show("Thêm công trình thành công");
                }
                else
                {
                    XtraMessageBox.Show("Thêm công trình thất bại !!!");
                }

                dataGridView1.DataSource = null;
                dataGridView1.DataSource = ctBLL.getAllCongTrinh();
                loadDataCellCongTrinh();
                btnCapNhat.Enabled = btnXoa.Enabled = btnThem.Enabled = true;
                btnLuu.Enabled     = false;
                enableOptions(false);
                trangThaiLuu = -1;
                btnThem.Text = "Thêm mới";
            }
            else //Cập nhật
            {
                eCongTrinh a  = new eCongTrinh(dataGridView1.CurrentRow.Cells[0].Value.ToString(), tbTenCongTrinh.Text, tbDiaChi.Text, (DateTime)dateNgayBatDau.Value, (DateTime)dateNgayKetThuc.Value, (DateTime)dateNgayHoanThanh.Value, comboBoxTrangThai.Text);
                bool       kq = ctBLL.capNhatCongTrinh(a);

                if (kq)
                {
                    XtraMessageBox.Show("Cập nhật công trình thành công");
                }
                else
                {
                    XtraMessageBox.Show("Cập nhật công trình thất bại !!!");
                }

                dataGridView1.DataSource = null;
                dataGridView1.DataSource = ctBLL.getAllCongTrinh();
                loadDataCellCongTrinh();
                btnCapNhat.Enabled = btnXoa.Enabled = btnThem.Enabled = true;
                btnLuu.Enabled     = false;
                enableOptions(false);
            }
        }
        private void btnLuu_Click(object sender, EventArgs e)
        {
            if (key == 1)
            {
                #region Lưu nút thêm.

                DialogResult dg = new DialogResult();
                dg = XtraMessageBox.Show("Bạn có muốn thêm công trình không !", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (dg == DialogResult.Yes)
                {
                    try
                    {
                        if (tbMaCongTrinh.Text == "" || tbTenCongTrinh.Text == "" || tbDiaDiemXayDung.Text == "")
                        {
                            XtraMessageBox.Show("Thiếu thông tin, vui lòng nhập đủ !");
                        }
                        else
                        {
                            //Chuỗi regex để kiểm tra
                            string reMa = @"^[CT]+[0-9]{4}$";
                            Regex  rgMa = new Regex(reMa);

                            string reLuong = @"^[0-9]{1,10}$";
                            Regex  rgLuong = new Regex(reLuong);

                            if (!rgMa.IsMatch(tbMaCongTrinh.Text))
                            {
                                XtraMessageBox.Show("Mã công trình phải có kí tự CT ở đầu và gồm 7 kí tự , vui lòng nhập lại !");
                            }
                            else if (Convert.ToDecimal(tbLuongCT.Text) < 0 || !rgLuong.IsMatch(tbLuongCT.Text))
                            {
                                XtraMessageBox.Show("Lương công trình phải lớn hơn 0 và phải là số , vui lòng nhập lại !");
                            }
                            else if (ctbll.kiemTraTenCongTrinhTonTai(tbTenCongTrinh.Text) != 0)
                            {
                                XtraMessageBox.Show("Tên công trình bị trùng , vui lòng nhập lại !");
                            }
                            else
                            {
                                //Thêm bảng công trinh
                                CongTrinh ct1 = new CongTrinh();
                                ct1.maCongTrinh    = tbMaCongTrinh.Text.Trim();
                                ct1.tenCongTrinh   = tbTenCongTrinh.Text.Trim();
                                ct1.diaDiemXayDung = tbDiaDiemXayDung.Text.Trim();
                                ct1.luongCongTrinh = Convert.ToDecimal(tbLuongCT.Text);
                                ct1.ngayCapPhep    = dtpNgayCapPhep.Value;
                                ct1.ngayKhoiCong   = dtpNgayKhoiCong.Value;
                                ct1.ngayHoanThanh  = dtpNgayHoanThanh.Value;
                                ct1.trangThai      = "DXD";

                                if (ctbll.themCongTrinh(ct1))
                                {
                                    btnXemHoanThanh.Enabled = true;
                                    XtraMessageBox.Show("Thêm thành công !");
                                    key = 0;
                                    #region Chỉnh trạng thái control sau khi thêm thành công
                                    dataGridView1.Enabled = true;
                                    voHieuTB();
                                    xoaAllTB();

                                    loadDataThiCong();
                                    btnLuu.Enabled  = false;
                                    btnHuy.Enabled  = false;
                                    btnThem.Enabled = true;

                                    #endregion
                                }
                                else
                                {
                                    XtraMessageBox.Show("Bị trùng mã công trình, vui lòng nhập mã khác !");
                                }
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        XtraMessageBox.Show("Lỗi: " + ex);
                    }
                }
                else
                {
                    dg = DialogResult.Cancel;
                }

                #endregion
            }
            else if (key == 2)
            {
                #region Lưu nút sửa.

                DialogResult dg = new DialogResult();
                dg = XtraMessageBox.Show("Bạn có muốn thay đổi thông tin công trình này không !", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (dg == DialogResult.Yes)
                {
                    try
                    {
                        if (tbMaCongTrinh.Text == "" || tbTenCongTrinh.Text == "" || tbDiaDiemXayDung.Text == "")
                        {
                            XtraMessageBox.Show("Thiếu thông tin, vui lòng nhập đủ !");
                        }
                        else
                        {
                            //Chuỗi regex để kiểm tra
                            string reMa    = @"^[CT]+[0-9]{4}$";
                            Regex  rgMa    = new Regex(reMa);
                            string reLuong = @"^[0-9]{1,10}$";
                            Regex  rgLuong = new Regex(reLuong);


                            if (!rgMa.IsMatch(tbMaCongTrinh.Text))
                            {
                                XtraMessageBox.Show("Mã nhân viên phải có kí tự CT ở đầu và gồm 7 kí tự , vui lòng nhập lại !");
                            }
                            else if (Convert.ToDecimal(tbLuongCT.Text) < 0 || !rgLuong.IsMatch(tbLuongCT.Text))
                            {
                                XtraMessageBox.Show("Lương công trình phải lớn hơn 0 và phải là số , vui lòng nhập lại !");
                            }
                            else
                            {
                                _maCongTrinh = tbMaCongTrinh.Text;
                                _trangThai   = ctbll.layTrangThai(tbMaCongTrinh.Text.Trim());
                                ////thêm bảng công trinh
                                CongTrinh ct1 = new CongTrinh();
                                ct1.maCongTrinh    = tbMaCongTrinh.Text.Trim();
                                ct1.tenCongTrinh   = tbTenCongTrinh.Text.Trim();
                                ct1.diaDiemXayDung = tbDiaDiemXayDung.Text.Trim();
                                ct1.luongCongTrinh = Convert.ToDecimal(tbLuongCT.Text);
                                ct1.ngayCapPhep    = dtpNgayCapPhep.Value;
                                ct1.ngayKhoiCong   = dtpNgayKhoiCong.Value;
                                ct1.ngayHoanThanh  = dtpNgayHoanThanh.Value;
                                ct1.trangThai      = "DXD";

                                if (ctbll.suaCongTrinh(ct1, _maCongTrinh))
                                {
                                    XtraMessageBox.Show("Lưu thành công !");
                                    key = 0;

                                    #region Chỉnh trạng thái control sau khi sửa thành công
                                    if (_trangThai.CompareTo("DXD") == 0)
                                    {
                                        voHieuTB();
                                        xoaAllTB();
                                        loadDataThiCong();
                                        btnLuu.Enabled          = false;
                                        btnHuy.Enabled          = false;
                                        btnThem.Enabled         = true;
                                        btnXemThiCong.Enabled   = false;
                                        dataGridView1.Enabled   = true;
                                        btnXemHoanThanh.Enabled = true;
                                    }
                                    else
                                    {
                                        voHieuTB();
                                        xoaAllTB();
                                        loadDataHoanThanh();
                                        btnLuu.Enabled          = false;
                                        btnHuy.Enabled          = false;
                                        btnThem.Enabled         = false;
                                        btnXemThiCong.Enabled   = true;
                                        btnXemHoanThanh.Enabled = false;
                                        dataGridView1.Enabled   = true;
                                    }
                                    #endregion
                                }
                                else
                                {
                                    XtraMessageBox.Show("Bị trùng mã công trình, vui lòng nhập mã khác !");
                                }
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        XtraMessageBox.Show("Lỗi: " + ex);
                    }
                }
                else
                {
                    dg = DialogResult.Cancel;
                }

                #endregion
            }
        }
        private void btnLuu_Click(object sender, EventArgs e)
        {
            if (trangThaiLuu == 0) // Thêm
            {
                if (tbTenCongTrinh.Text.Trim().Length == 0 || tbDiaChi.Text.Trim().Length == 0)
                {
                    XtraMessageBox.Show("Vui lòng nhập đầy đủ dữ liệu cho công trình mới!");
                }
                else
                {
                    if (dateNgayBatDau.Value > dateNgayKetThuc.Value)
                    {
                        XtraMessageBox.Show("Ngày bắt đầu phải nhỏ hơn hoặc bằng ngày kết thúc công trình!");
                    }
                    else
                    {
                        eCongTrinh a  = new eCongTrinh(tbTenCongTrinh.Text, tbDiaChi.Text, (DateTime)dateNgayBatDau.Value, (DateTime)dateNgayKetThuc.Value);
                        bool       kq = ctBLL.themCongTrinh(a);

                        if (kq)
                        {
                            XtraMessageBox.Show("Thêm công trình thành công");
                        }
                        else
                        {
                            XtraMessageBox.Show("Thêm công trình thất bại !!!");
                        }

                        dataGridView1.DataSource = null;
                        dataGridView1.DataSource = ctBLL.getAllCongTrinh();
                        loadDataCellCongTrinh();
                        btnCapNhat.Enabled = btnXoa.Enabled = btnThem.Enabled = true;
                        btnLuu.Enabled     = false;
                        enableOptions(false);
                        trangThaiLuu = -1;
                        btnThem.Text = "Thêm Mới";
                    }
                }
            }
            else //Cập nhật
            {
                if (tbTenCongTrinh.Text.Trim().Length == 0 || tbDiaChi.Text.Trim().Length == 0)
                {
                    XtraMessageBox.Show("Vui lòng nhập đầy đủ dữ liệu cho công trình!");
                }
                else
                {
                    if (dateNgayBatDau.Value > dateNgayKetThuc.Value)
                    {
                        XtraMessageBox.Show("Ngày bắt đầu phải nhỏ hơn hoặc bằng ngày kết thúc công trình!");
                    }
                    else
                    {
                        if (comboBoxTrangThai.Text == "Đang Thực Hiện")
                        {
                            XtraMessageBox.Show("Trạng thái chỉ được cập nhật khi Quá Hạn hoặc Hoàn Thành !");
                        }
                        else
                        {
                            eCongTrinh a  = new eCongTrinh(dataGridView1.CurrentRow.Cells[0].Value.ToString(), tbTenCongTrinh.Text, tbDiaChi.Text, (DateTime)dateNgayBatDau.Value, (DateTime)dateNgayKetThuc.Value, (DateTime)dateNgayHoanThanh.Value, comboBoxTrangThai.Text);
                            bool       kq = ctBLL.capNhatCongTrinh(a);

                            if (kq)
                            {
                                XtraMessageBox.Show("Cập nhật công trình thành công");
                            }
                            else
                            {
                                XtraMessageBox.Show("Cập nhật công trình thất bại !!!");
                            }

                            dataGridView1.DataSource = null;
                            dataGridView1.DataSource = ctBLL.getAllCongTrinh();
                            loadDataCellCongTrinh();
                            btnCapNhat.Enabled = btnXoa.Enabled = btnThem.Enabled = true;
                            btnLuu.Enabled     = false;
                            enableOptions(false);
                            trangThaiLuu    = -1;
                            btnCapNhat.Text = "Cập Nhật";
                        }
                    }
                }
            }
        }