Exemplo n.º 1
0
        private void btnThem_Click(object sender, EventArgs e)
        {
            if (cbmanv.Text.Trim().Length == 0)
            {
                MessageBox.Show("Bạn phải nhập mã nhân viên!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                cbmanv.Focus();
                return;
            }

            //Kiểm tra xem có thống kê nào trong tháng được tạo chưa để cập nhật mới
            DataTable dt = BUS_HT.KiemtraTKHTTonTai(DateTime.Now.Month.ToString(), DateTime.Now.Year.ToString());

            if (dt.Rows.Count == 1)
            {
                DataRow row = dt.Rows[0];

                //Cập nhật thống kê hàng tồn chi tiết
                dt = BUS_SP.hienthisp();
                if (dt.Rows.Count > 0)
                {
                    for (int i = 0; i < dt.Rows.Count; ++i)
                    {
                        DataRow r = dt.Rows[i];
                        BUS_HTCT.CapnhatTKHTCT(row[0].ToString(), r[0].ToString(), r[7].ToString());
                    }
                }
                //Cập nhật ngày thống kê
                BUS_HT.CapnhatNgayTK(row[0].ToString(), DateTime.Now.ToString());

                BUS_HT.FillComboMaTKHT(cbmatkht, "IdHT", "IdHT");
                cbmatkht.SelectedIndex = -1;
                ResetValues();
                LoadDataGridView();

                DANGNHAP.thaotac += "Thêm, ";

                MessageBox.Show("Đã thống kê xong!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            else
            {
                //tạo mã ngẫu nhiên
                int    value;
                bool   kt   = false;
                Random rand = new Random();
                value = rand.Next(100000000, 999999999);
                string  maht = "HT" + value;
                DataRow dr;
                if (BUS_HT.hienthiHT().Rows.Count > 0)
                {
                    while (kt == false)
                    {
                        for (int i = 0; i < BUS_HT.hienthiHT().Rows.Count; ++i)
                        {
                            dr = BUS_HT.hienthiHT().Rows[i];
                            if (maht == dr["IdHT"].ToString())
                            {
                                kt    = false;
                                value = rand.Next(100000000, 999999999);
                                maht  = "HT" + value;
                                break;
                            }
                            else
                            {
                                kt = true;
                            }
                        }
                    }
                }

                //Kiểm tra đã tồn tại mã thống kê hàng tồn
                if (!BUS_HT.ktHTtrung(maht))
                {
                    MessageBox.Show("Mã hàng tồn đã tồn tại! Hãy nhấn Thêm lần nữa để lấy mã khác!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    btnThem.Focus();
                    return;
                }

                //Chèn thêm
                //Thêm thống kê hàng tồn
                DTO_HT HT = new DTO_HT(maht, cbmanv.Text.Trim(), DateTime.Now.ToString());
                BUS_HT.themHT(HT);

                //Thêm thống kê hàng tồn chi tiết
                dt = BUS_SP.hienthisp();
                DTO_HTCT HTCT;
                if (dt.Rows.Count > 0)
                {
                    for (int i = 0; i < dt.Rows.Count; ++i)
                    {
                        DataRow r = dt.Rows[i];
                        HTCT = new DTO_HTCT(maht, r[0].ToString(), r[2].ToString(), r[3].ToString(), r[4].ToString(), r[5].ToString(), "0");
                        BUS_HTCT.themHTCT(HTCT);
                    }
                }

                //giữ thông tin chung của thống kê vừa thêm trên các control
                dt = BUS_HT.hienthiHTcuthe(maht);
                if (dt.Rows.Count == 1)
                {
                    DataRow r = dt.Rows[0];
                    cbmatkht.Text  = r[0].ToString();
                    cbmanv.Text    = r[1].ToString();
                    dtpngaytk.Text = r[2].ToString();
                }

                BUS_HT.FillComboMaTKHT(cbmatkht, "IdHT", "IdHT");
                cbmatkht.SelectedIndex = -1;
                ResetValues();
                LoadDataGridView();

                DANGNHAP.thaotac += "Thêm, ";

                MessageBox.Show("Đã thống kê xong!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
        }