//protected  void PickUpSanPhamInfo(DMSanPhamInfo sanPhamInfo)
        //{
        //    likhong[dgvSanPhamKhong.Rows.IndexOf(dgvSanPhamKhong.CurrentRow)].MaSanPham = sanPhamInfo.MaSanPham;
        //    likhong[dgvSanPhamKhong.Rows.IndexOf(dgvSanPhamKhong.CurrentRow)].TenSanPham = sanPhamInfo.TenSanPham;
        //    likhong[dgvSanPhamKhong.Rows.IndexOf(dgvSanPhamKhong.CurrentRow)].TrungMaVach = sanPhamInfo.TrungMaVach;
        //    likhong[dgvSanPhamKhong.Rows.IndexOf(dgvSanPhamKhong.CurrentRow)].IdSanPham = sanPhamInfo.IdSanPham;
        //    if (dgvSanPhamKhong.DataSource == null)
        //        dgvSanPhamKhong.DataSource = new BindingList<KiemKeChiTietKhongMaVachInfor>(likhong);
        //    else
        //    {
        //        ((BindingList<KiemKeChiTietKhongMaVachInfor>)dgvSanPhamKhong.DataSource).ResetBindings();
        //    }
        //}

        // private bool isKeyPressed;

        //void Control_KeyPress(object sender, KeyPressEventArgs e)
        //{
        //    isKeyPressed = true;
        //}

        //void Control_TextChanged(object sender, EventArgs e)
        //{
        //    try
        //    {
        //        if (ColumnMaSanPham == null) return;
        //        if (!isKeyPressed || dgvSanPhamKhong.CurrentCell.ColumnIndex != dgvSanPhamKhong.Columns.IndexOf(ColumnMaSanPham) ||
        //            ((TextBox)sender).Text == String.Empty || ((TextBox)sender).Text == (string)dgvSanPhamKhong.CurrentCell.Value) return;

        //        frmLookUp_SanPham frm = new frmLookUp_SanPham(String.Format("%{0}%", ((TextBox)sender).Text));

        //        if (frm.ShowDialog() == DialogResult.OK)
        //        {
        //            isKeyPressed = false;
        //            PickUpSanPhamInfo(frm.SelectedItem);
        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //        EventLogProvider.Instance.WriteLog(ex.ToString(), "frmLookUp_SanPham");
        //    }
        //}
        //private void dgvSanPhamKhong_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        //{
        //    if (ColumnMaSanPham == null) return;
        //    if (dgvSanPhamKhong.CurrentCell != null && dgvSanPhamKhong.CurrentCell.ColumnIndex == dgvSanPhamKhong.Columns.IndexOf(ColumnMaSanPham))
        //    {
        //        e.Control.KeyPress += new KeyPressEventHandler(Control_KeyPress);
        //        e.Control.TextChanged += new EventHandler(Control_TextChanged);
        //    }
        //}
        //protected  DataGridViewTextBoxColumn ColumnMaSanPham
        //{
        //    get
        //    {
        //        return clMaSanPhamKhong;
        //    }
        //}
        private void Them()
        {
            if (trangThai == 1)
            {
                throw new ManagedException("Phiếu này đã được xác nhận!");
            }

            if (txtMaVach.Text == "")
            {
                throw new ManagedException("Mã vạch không được để trống !");
            }
            if (bteDotKiemKe.Tag == null)
            {
                throw new ManagedException("Bạn chưa chọn đợt kiểm kê!");
            }

            txtMaVach.Text = txtMaVach.Text.Trim();

            List <DMSanPhamInfoEx> listsp = KiemKeDataProvider.Instance.
                                            GetLookUpSanPhamTrungMV(txtMaVach.Text, MaTrungTam, MaKho, MaNganh,
                                                                    ((DotKiemKeInfor)bteDotKiemKe.Tag).IdDotKiemKe);

            List <ChungTu_ChiTietHangHoaKiemKeInfor> frm =
                KiemKeDataProvider.Instance.GetIdSanPhamByMaVach(txtMaVach.Text, MaTrungTam, MaKho, MaNganh,
                                                                 ((DotKiemKeInfor)bteDotKiemKe.Tag).IdDotKiemKe);

            if (frm != null && frm.Count != 0)
            {
                for (int i = 0; i < frm.Count; i++)
                {
                    HangHoa.IdChiTietHangHoa = frm[i].IdChiTietHangHoa;
                    HangHoa.IdSanPham        = frm[i].IdSanPham;
                    HangHoa.MaSanPham        = frm[i].MaSanPham;
                    HangHoa.TenSanPham       = frm[i].TenSanPham;
                    HangHoa.SoLuongSS        = frm[i].SoLuong;
                    HangHoa.GhiChu           = frm[i].GhiChu;
                    HangHoa.TrungMaVach      = frm[i].TrungMaVach;
                    HangHoa.IdKho            = frm[i].IdKho;
                    HangHoa.MaKho            = frm[i].MaKho;
                    //HangHoa.DonViTinh = frm[0].TenDonViTinh;}
                }
            }

            if (frm != null && frm.Count == 0 ||
                !chkAutoRegSub.Checked || //&& HangHoa.TrungMaVach == 1 ||
                KiemKeDataProvider.Instance.CheckMaVach(MaKho, HangHoa.IdSanPham, txtMaVach.Text.Trim(), MaNganh, MaTrungTam,
                                                        ((DotKiemKeInfor)bteDotKiemKe.Tag).IdDotKiemKe) == false)
            {
                if (MessageBox.Show(
                        !chkAutoRegSub.Checked && HangHoa.TrungMaVach == 1 ?
                        "Bạn có chắc chắn là serial thừa không?" :
                        "Mã vạch hiện không tìm thấy.\nBạn có muốn thêm mã vạch này không?",
                        "Xác nhận",
                        MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
                {
                    KiemKeChiTietKhongMaVachInfor matchInfo = new KiemKeChiTietKhongMaVachInfor();

                    DMTrungTamInfor trungTamInfor = DMTrungTamDataProvider.GetTrungTamByMa(((DotKiemKeInfor)bteDotKiemKe.Tag).TrungTam);


                    frmLookUp_Kho frmLookUpKho = new frmLookUp_Kho(false, String.Format("%{0}%", ((DotKiemKeInfor)bteDotKiemKe.Tag).TrungTam), trungTamInfor.IdTrungTam, -1);

                    if (frmLookUpKho.ShowDialog() == DialogResult.OK)
                    {
                        matchInfo.IdKho = frmLookUpKho.SelectedItem.IdKho;
                        matchInfo.MaKho = frmLookUpKho.SelectedItem.MaKho;
                    }
                    else
                    {
                        return;
                    }

                    frmLookUp_SanPham frmLookUpSanPham = new frmLookUp_SanPham("%%");
                    if (frmLookUpSanPham.ShowDialog() == DialogResult.OK)
                    {
                        matchInfo.IdSanPham  = frmLookUpSanPham.SelectedItem.IdSanPham;
                        matchInfo.MaSanPham  = frmLookUpSanPham.SelectedItem.MaSanPham;
                        matchInfo.TenSanPham = frmLookUpSanPham.SelectedItem.TenSanPham;
                    }
                    else
                    {
                        return;
                    }

                    foreach (KiemKeChiTietKhongMaVachInfor pt in liKhong)
                    {
                        if (pt.MaVach.ToLower() == txtMaVach.Text.Trim().ToLower() &&
                            pt.IdKho == matchInfo.IdKho && pt.IdSanPham == matchInfo.IdSanPham)
                        {
                            pt.SoLuong = pt.SoLuong + 1;

                            ((BindingList <KiemKeChiTietKhongMaVachInfor>)grvDanhSachKhong.DataSource).ResetBindings();

                            txtMaVach.Clear();

                            grvDanhSachKhong.TopRowIndex = liKhong.IndexOf(pt);

                            grvDanhSachKhong.ClearSelection();
                            grvDanhSachKhong.FocusedRowHandle = liKhong.IndexOf(pt);
                            return;
                        }
                    }

                    liKhong.Add(new KiemKeChiTietKhongMaVachInfor()
                    {
                        MaVach     = txtMaVach.Text.Trim(),
                        SoLuong    = 1,
                        IdSanPham  = matchInfo.IdSanPham,
                        MaSanPham  = matchInfo.MaSanPham,
                        TenSanPham = matchInfo.TenSanPham,
                        IdKho      = matchInfo.IdKho,
                        MaKho      = matchInfo.MaKho
                    });

                    ((BindingList <KiemKeChiTietKhongMaVachInfor>)grvDanhSachKhong.DataSource).ResetBindings();

                    txtMaVach.Clear();

                    grvDanhSachKhong.TopRowIndex = liKhong.Count - 1;

                    grvDanhSachKhong.ClearSelection();
                    grvDanhSachKhong.FocusedRowHandle = liKhong.Count - 1;
                }

                txtMaVach.Clear();
            }
            else
            {
                if (listsp.Count < 2)
                {
                    foreach (KiemKeChiTietHangHoaInfor pt in liCo)
                    {
                        if (pt.MaVach.ToLower() == txtMaVach.Text.Trim().ToLower() &&
                            pt.TrungMaVach == 1 &&
                            pt.MaKho == HangHoa.MaKho &&
                            pt.MaSanPham == HangHoa.MaSanPham)
                        {
                            pt.SoLuong   = pt.SoLuong + 1;
                            pt.SoLuongSs = pt.SoLuongSs;

                            ((BindingList <KiemKeChiTietHangHoaInfor>)grvDanhSachCo.DataSource).ResetBindings();

                            txtTenSanPham.Text = HangHoa.TenSanPham;
                            txtMaVach.Clear();

                            grvDanhSachCo.TopRowIndex = liCo.IndexOf(pt);

                            grvDanhSachCo.ClearSelection();
                            grvDanhSachCo.FocusedRowHandle = liCo.IndexOf(pt);
                            return;
                        }
                        if (pt.MaVach.ToLower() == txtMaVach.Text.Trim().ToLower() && pt.TrungMaVach == 0)
                        {
                            throw new ManagedException("Mã vạch không được trùng nhau!");
                        }
                    }
                    liCo.Add(new KiemKeChiTietHangHoaInfor
                    {
                        MaVach           = txtMaVach.Text.Trim(),
                        SoLuong          = 1,
                        TenDonViTinh     = HangHoa.DonViTinh,
                        IdSanPham        = HangHoa.IdSanPham,
                        IdChiTietHangHoa = HangHoa.IdChiTietHangHoa,
                        TenSanPham       = HangHoa.TenSanPham,
                        MaSanPham        = HangHoa.MaSanPham,
                        SoLuongSs        = HangHoa.SoLuongSS,
                        GhiChu           = HangHoa.GhiChu,
                        TrungMaVach      = HangHoa.TrungMaVach,
                        IdKho            = HangHoa.IdKho,
                        MaKho            = HangHoa.MaKho
                    });
                    ((BindingList <KiemKeChiTietHangHoaInfor>)grvDanhSachCo.DataSource).ResetBindings();

                    txtTenSanPham.Text = HangHoa.TenSanPham;
                    txtMaVach.Clear();

                    grvDanhSachCo.TopRowIndex = liCo.Count - 1;

                    grvDanhSachCo.ClearSelection();
                    grvDanhSachCo.FocusedRowHandle = liCo.Count - 1;
                }
                else
                {
                    frmLookup_SanPhamTrungMaVach frmsp = new frmLookup_SanPhamTrungMaVach(txtMaVach.Text, MaKho, MaTrungTam, MaNganh,
                                                                                          ((DotKiemKeInfor)bteDotKiemKe.Tag).IdDotKiemKe);
                    if (frmsp.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                    {
                        int idsanpham = frmsp.item.IdSanPham;
                        ChungTu_ChiTietHangHoaKiemKeInfor obj = KiemKeDataProvider.Instance.
                                                                GetSanPhamById_MaVach(idsanpham, txtMaVach.Text, MaTrungTam, frmsp.item.MaKho, MaNganh,
                                                                                      ((DotKiemKeInfor)bteDotKiemKe.Tag).IdDotKiemKe);

                        foreach (KiemKeChiTietHangHoaInfor pt in liCo)
                        {
                            if (pt.MaVach.ToLower() == txtMaVach.Text.Trim().ToLower() &&
                                pt.TrungMaVach == 1 &&
                                pt.MaKho == obj.MaKho &&
                                pt.MaSanPham == obj.MaSanPham)
                            {
                                pt.SoLuong   = pt.SoLuong + 1;
                                pt.SoLuongSs = pt.SoLuongSs;

                                ((BindingList <KiemKeChiTietHangHoaInfor>)grvDanhSachCo.DataSource).ResetBindings();

                                txtTenSanPham.Text = HangHoa.TenSanPham;
                                txtMaVach.Clear();

                                grvDanhSachCo.TopRowIndex = liCo.IndexOf(pt);

                                grvDanhSachCo.ClearSelection();
                                grvDanhSachCo.FocusedRowHandle = liCo.IndexOf(pt);

                                return;
                            }
                            if (pt.MaVach.ToLower() == txtMaVach.Text.Trim().ToLower() &&
                                pt.TrungMaVach == 0 &&
                                pt.MaKho == obj.MaKho &&
                                pt.MaSanPham == obj.MaSanPham)
                            {
                                throw new ManagedException("Mã vạch không được trùng nhau!");
                            }
                        }

                        liCo.Add(new KiemKeChiTietHangHoaInfor
                        {
                            MaVach           = txtMaVach.Text.Trim(),
                            SoLuong          = 1,
                            TenDonViTinh     = obj.TenDonViTinh,
                            IdSanPham        = obj.IdSanPham,
                            IdChiTietHangHoa = obj.IdChiTietHangHoa,
                            TenSanPham       = obj.TenSanPham,
                            MaSanPham        = obj.MaSanPham,
                            //SoLuongSs = obj.SoLuongSS,
                            GhiChu      = obj.GhiChu,
                            TrungMaVach = obj.TrungMaVach,
                            IdKho       = obj.IdKho,
                            MaKho       = obj.MaKho
                        });

                        ((BindingList <KiemKeChiTietHangHoaInfor>)grvDanhSachCo.DataSource).ResetBindings();

                        txtTenSanPham.Text = obj.TenSanPham;
                        txtMaVach.Clear();

                        grvDanhSachCo.TopRowIndex = liCo.Count - 1;

                        grvDanhSachCo.ClearSelection();
                        grvDanhSachCo.FocusedRowHandle = liCo.Count - 1;
                    }
                }
            }
        }
Exemple #2
0
        private void tsbImport_Click(object sender, EventArgs e)
        {
            if (!Declare.IS_SUPPER_USER && Declare.UserName != "anhdtn1174" && Declare.UserName != "superuser" &&

                ((NguoiDungInfor)Declare.USER_INFOR).NhomNguoiDung != "KTK")
            {
                MessageBox.Show("Bạn không có quyền thực hiện chức năng này!");

                return;
            }
            openFileDialog1.FileName = String.Empty;

            openFileDialog1.Filter = "Excell Files(*.xls, *.xlsx)|*.xls;*.xlsx";

            if (openFileDialog1.ShowDialog() != DialogResult.OK)
            {
                return;
            }

            frmProgress.Instance.DoWork(
                delegate
            {
                try
                {
                    frmProgress.Instance.Text        = this.Text;
                    frmProgress.Instance.Description = "Đang nạp dữ liệu ....";
                    frmProgress.Instance.MaxValue    = 100;

                    using (var oConn = new OleDbConnection())
                    {
                        var dsDotKiemKe        = new DataSet();
                        oConn.ConnectionString =
                            String.Format(
                                "Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties=\"Excel 8.0;HDR=Yes\"",
                                openFileDialog1.FileName);
                        oConn.Open();

                        string sql =
                            "Select [Đợt kiểm kê] as MaDotKiemKe, [Trung tâm] as MaTrungTam, [Ngành] as MaNganh from [Import$]";

                        using (var ad = new OleDbDataAdapter(sql, oConn))
                        {
                            ad.Fill(dsDotKiemKe, "DsDotKiemKe");

                            frmProgress.Instance.Description = "Đang kiểm tra dữ liệu ....";
                            frmProgress.Instance.MaxValue    = dsDotKiemKe.Tables["DsDotKiemKe"].Rows.Count;

                            foreach (
                                DataRow dataRow in
                                dsDotKiemKe.Tables["DsDotKiemKe"].Rows)
                            {
                                string sTmp = Convert.ToString(dataRow["MaDotKiemKe"]);
                                if (sTmp.Length > 100)
                                {
                                    throw new ManagedException(
                                        String.Format(
                                            "Mã đợt kiểm kê {0} vượt quá 100 ký tự!",
                                            sTmp));
                                }

                                if (sTmp.Contains("'"))
                                {
                                    throw new ManagedException(
                                        String.Format(
                                            "Mã đợt kiểm kê {0} chứa ký tự đặc biệt",
                                            sTmp));
                                }

                                if (!DotKiemKeDataProvider.Instance.MaDotKiemKeUnique(sTmp))
                                {
                                    throw new ManagedException(
                                        String.Format(
                                            "Mã đợt kiểm kê {0} không duy nhất",
                                            sTmp));
                                }

                                sTmp = Convert.ToString(dataRow["MaTrungTam"]);
                                if (DMTrungTamDataProvider.GetTrungTamByMa(sTmp) == null)
                                {
                                    throw new ManagedException(
                                        String.Format("Mã trung tâm {0} không tồn tại",
                                                      sTmp));
                                }

                                sTmp = Convert.ToString(dataRow["MaNganh"]);
                                if (
                                    DmNganhDataProvider.Instance.GetFullInfoByKey(sTmp) ==
                                    null)
                                {
                                    throw new ManagedException(
                                        String.Format("Mã ngành {0} không tồn tại", sTmp));
                                }

                                frmProgress.Instance.Value += 1;
                            }

                            frmProgress.Instance.Description = "Đang cập nhật dữ liệu ....";
                            frmProgress.Instance.Value       = 0;
                            frmProgress.Instance.MaxValue    = dsDotKiemKe.Tables["DsDotKiemKe"].Rows.Count;

                            foreach (
                                DataRow dataRow in
                                dsDotKiemKe.Tables["DsDotKiemKe"].Rows)
                            {
                                DotKiemKeDataProvider.Instance.Insert(
                                    new DotKiemKeInfor
                                {
                                    MaDotKiemKe =
                                        Convert.ToString(dataRow["MaDotKiemKe"]),
                                    NguoiTao = Declare.UserName,
                                    TrungTam =
                                        Convert.ToString(dataRow["MaTrungTam"]),
                                    Nganh     = Convert.ToString(dataRow["MaNganh"]),
                                    TenMayTao = Dns.GetHostName()
                                });

                                frmProgress.Instance.Value += 1;
                            }

                            frmProgress.Instance.Description = "Đã cập nhật thành công";
                            Thread.CurrentThread.Join(2000);

                            frmProgress.Instance.Value = 0;
                            LoadDuLieu();
                        }
                    }
                }
                catch (ManagedException ex)
                {
                    frmProgress.Instance.Description = "Không thành công!";
                    frmProgress.Instance.Value       = frmProgress.Instance.MaxValue;
                    MessageBox.Show(ex.Message);
                    frmProgress.Instance.IsCompleted = true;
                }
                catch (Exception ex)
                {
                    frmProgress.Instance.Description = "Không thành công!";
                    frmProgress.Instance.Value       = frmProgress.Instance.MaxValue;
#if DEBUG
                    MessageBox.Show(ex.ToString());
#else
                    MessageBox.Show(ex.Message);
#endif
                    frmProgress.Instance.IsCompleted = true;
                }
            });
        }