Ejemplo n.º 1
0
        private void frm_ChungTuNhap_FormClosing(object sender, FormClosingEventArgs e)
        {
            try {
                if (dtSoPhieuNhap != null && dtSoPhieuNhap.Rows.Count > 0 && !isFinished && e.CloseReason == CloseReason.UserClosing)
                {
                    if (MessageBox.Show("Các phiếu mua hàng chưa được cập nhật hết, bạn có thực sự muốn đóng lại không?", "Xác nhận", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
                    {
                        e.Cancel = true;
                        return;
                    }
                    if (!IsValid())
                    {
                        return;
                    }
                    btnGhi_Click(sender, e);
                }

                GtidCommand sqlCmd = ConnectionUtil.Instance.GetConnection().CreateCommand();
                sqlCmd.CommandText = "sp_LockFunction_Update";
                sqlCmd.CommandType = CommandType.StoredProcedure;
                sqlCmd.Parameters.AddWithValue("@FormName", this.GetType().Name);
                sqlCmd.Parameters.AddWithValue("@IdKho", Declare.IdKho);
                sqlCmd.ExecuteNonQuery();
            }
            catch (System.Exception ex) {
                EventLogProvider.Instance.WriteLog(ex.ToString(), this.Name);
#if DEBUG
                MessageBox.Show(ex.ToString());
#else
                MessageBox.Show(ex.Message);
#endif
            }
        }
Ejemplo n.º 2
0
        private void btnDong_Click(object sender, EventArgs e)
        {
            try {
                if (dtSoPhieuNhap != null && dtSoPhieuNhap.Rows.Count > 0)
                {
                    btnGhi_Click(sender, e);
                    GtidCommand sqlCmd = ConnectionUtil.Instance.GetConnection().CreateCommand();
                    sqlCmd.CommandTimeout = 0;
                    sqlCmd.CommandType    = CommandType.Text;
                    sqlCmd.CommandText    = String.Format("SELECT ct.IdChungTu, ct.SoChungTu, t1.ThucTe, t2.SoSach FROM tbl_ChungTu ct "
                                                          + "INNER JOIN (SELECT IdChungTu, SUM(ctcthh.SoLuong) as ThucTe FROM tbl_ChungTu_ChiTiet_HangHoa ctcthh "
                                                          + "INNER JOIN tbl_ChungTu_ChiTiet ctct ON ctct.IdChiTiet = ctcthh.IdChiTietChungTu "
                                                          + "GROUP BY IdChungTu) t1 ON ct.IdChungTu = t1.IdChungTu AND ct.LoaiChungTu={0} AND ct.IdKho={1} AND ct.ThoigianTao < '{2}' AND ct.ThoigianTao > '{3}' "

                                                          + "RIGHT OUTER JOIN (SELECT t1.SoPhieuNhap, t1.SoPO, ct.IdChungTu, SUM(SoLuong) as SoSach FROM tbl_Tmp_NhapHang t1 "
                                                          + "LEFT OUTER JOIN tbl_ChungTu ct ON ct.SoChungTu=t1.SoPhieuNhap AND ct.SoSeri=t1.SoPO AND ct.LoaiChungTu={0} AND ct.IdKho={1} "
                                                          + "AND (SELECT TOP (1) soluong FROM tbl_chungtu_chitiet WHERE tbl_chungtu_chitiet.idchungtu = ct.idchungtu)>=0 "
                                                          + "WHERE InventoryOrg='{4}' AND InventorySub='{5}' AND ThoiGian < '{2}' AND ThoiGian > '{3}' AND SoLuong >= 0 GROUP BY SoPhieuNhap, SoPO, IdChungTu "
                                                          + "UNION ALL "
                                                          + "SELECT t1.SoPhieuNhap, t1.SoPO, ct.IdChungTu, SUM(SoLuong) as SoSach FROM tbl_Tmp_NhapHang t1 "
                                                          + "LEFT OUTER JOIN tbl_ChungTu ct ON ct.SoChungTu=t1.SoPhieuNhap AND ct.SoSeri=t1.SoPO AND ct.LoaiChungTu={0} AND ct.IdKho={1} "
                                                          + "AND (SELECT TOP (1) soluong FROM tbl_chungtu_chitiet WHERE tbl_chungtu_chitiet.idchungtu = ct.idchungtu)<0 "
                                                          + "WHERE InventoryOrg='{4}' AND InventorySub='{5}' AND ThoiGian < '{2}' AND ThoiGian > '{3}' AND SoLuong < 0 GROUP BY SoPhieuNhap, SoPO, IdChungTu "
                                                          + ") t2 ON ct.IdChungTu = t2.IdChungTu",
                                                          (int)TransactionType.NHAP_PO,
                                                          Declare.IdKho,
                                                          DateTime.Now.ToString(new CultureInfo("en-US")),
                                                          //dtNgayDongBo.Value.ToString(new CultureInfo("en-US")),
                                                          lanDongBoTruoc.ToString(new CultureInfo("en-US")),
                                                          inventoryOrg,
                                                          inventorySub);
                    DataTable dt = DBTools.getData(sqlCmd, "Temp").Tables["Temp"];
                    if (dt.Rows.Count == 0)
                    {
                        MessageBox.Show("Bạn chưa nhập đủ số lượng hàng trong các phiếu mua này", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                        return;
                    }
                    foreach (DataRow dr in dt.Rows)
                    {
                        if (!int.Equals(dr["ThucTe"], dr["SoSach"]))
                        {
                            MessageBox.Show("Bạn chưa nhập đủ số lượng hàng trong các phiếu mua này", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                            return;
                        }
                    }
                    sqlCmd.CommandText = String.Format("Update tbl_DM_Kho set LanDongBoTruoc='{0}' WHERE IdKho={1}", dtNgayDongBo.Value.ToString(new CultureInfo("en-US")), DBTools.getValue(String.Format("SELECT IdKho FROM tbl_DM_Kho WHERE MaKho='{0}'", cboKho.SelectedValue)));
                    sqlCmd.ExecuteNonQuery();
                    MessageBox.Show("Đã cập nhật thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                isFinished = true;
                this.Close();
            }
            catch (System.Exception ex) {
#if DEBUG
                MessageBox.Show(ex.ToString());
#else
                MessageBox.Show(ex.Message);
#endif
            }
        }
Ejemplo n.º 3
0
        private void ImportDanhMuc(string workSheetName, string tableName, string[,] fieldAndFormats)
        {
            if (chkDeleteData.Checked)
            {
                Status = String.Format("Xóa bảng {0}", tableName);
                SqlComm.CommandText = String.Format("Delete from {0}", tableName);
                SqlComm.ExecuteNonQuery();

                SqlComm.CommandText = String.Format("DBCC CHECKIDENT ( {0}, RESEED, 0 ) ", tableName);
                SqlComm.ExecuteNonQuery();
            }

            Status = String.Format("Nạp dữ liệu cho bảng {0}", tableName);

            string sqlFormat  = "Insert into {0}({1}, SuDung) values({2}, 1)";
            string fieldName  = String.Empty;
            string format     = String.Empty;
            int    fieldCount = fieldAndFormats.Length / 3;

            for (int i = 0; i < fieldCount; i++)
            {
                fieldName += String.IsNullOrEmpty(fieldName) ? fieldAndFormats[i, 1] : String.Format(", {0}", fieldAndFormats[i, 1]);
                format    += String.IsNullOrEmpty(format) ? fieldAndFormats[i, 2] : String.Format(", {0}", fieldAndFormats[i, 2]);
            }

            sqlFormat = String.Format(sqlFormat, tableName, fieldName, format);

            ProgressMaxValue     = ds.Tables[workSheetName].Rows.Count;
            ProgressCurrentValue = 0;
            foreach (DataRow dr in ds.Tables[workSheetName].Rows)
            {
                object[] objParams = new object[fieldCount];

                for (int i = 0; i < fieldCount; i++)
                {
                    if (dr[fieldAndFormats[i, 0]] != DBNull.Value)
                    {
                        objParams[i] = Escape(dr[fieldAndFormats[i, 0]].ToString());
                    }
                    else
                    {
                        objParams[i] = DBNull.Value;
                    }
                }
                SqlComm.CommandText = String.Format(sqlFormat, objParams);
                SqlComm.ExecuteNonQuery();
                ProgressCurrentValue += 1;
            }
            Common.LogAction(String.Format("Khởi tạo dữ liệu bảng {0}", tableName), "Completed.", -1);
        }
Ejemplo n.º 4
0
        public static void Unlock(string functionName, int idKho)
        {
            GtidCommand sqlCmd = ConnectionUtil.Instance.GetConnection().CreateCommand();

            sqlCmd.CommandText = "sp_LockFunction_Update1";
            sqlCmd.CommandType = CommandType.StoredProcedure;
            sqlCmd.Parameters.AddWithValue("@FormName", functionName);
            sqlCmd.Parameters.AddWithValue("@IdKho", idKho);
            sqlCmd.Parameters.AddWithValue("@IdUser", Declare.UserId);
            sqlCmd.Parameters.AddWithValue("@Computer", Common.GetComputerName());
            sqlCmd.Parameters.AddWithValue("@ProcessId", Process.GetCurrentProcess().Id);
            sqlCmd.ExecuteNonQuery();
        }
Ejemplo n.º 5
0
        public void UpdateUserStatus(int userId, int status)
        {
            //CreateCommonCommand(Declare.StoreProcedureNamespace.spNguoiDungUpdateStatus);
            //Parameters.AddWithValue("@p_UserID", userId);
            //Parameters.AddWithValue("@p_Status", status);
            //ExecuteNoneQuery();

            string sql = "Update tbl_DM_NguoiDung nd Set nd.Status = " + status +
                         " Where nd.IdNguoiDung = " + userId;
            GtidCommand SqlComm = new GtidCommand(sql, ConnectionUtil.Instance.GetConnection());

            SqlComm.CommandType = CommandType.Text;
            SqlComm.ExecuteNonQuery();
        }
Ejemplo n.º 6
0
        public static void Lock(string functionName, int idKho)
        {
            string    sql = String.Format("SELECT lk.IdUser,nd.TenDangNhap,lk.Computer FROM tbl_Function_Locking lk INNER JOIN tbl_dm_nguoidung nd ON lk.IdUser = nd.IdNguoidung WHERE lk.FormName='{0}' AND lk.IdKho={1}", functionName, idKho);
            DataTable dt  = SqlHelper.ExecuteDataset(ConnectionUtil.Instance.GetConnection(), CommandType.Text, sql).Tables[0];

            if (dt != null && dt.Rows.Count > 0)
            {
                if (!Equals(dt.Rows[0]["Computer"], Common.GetComputerName()))
                {
                    throw new ManagedException(String.Format("Chức năng này đang được chạy bởi một máy khác [{0}]", dt.Rows[0]["Computer"]));
                }

                if (Common.IntValue(dt.Rows[0]["IdUser"]) != Declare.UserId)
                {
                    throw new ManagedException(String.Format("Chức năng này đang được chạy bởi một người dùng khác [{0}]", dt.Rows[0]["TenDangNhap"]));
                }

                if (dt.Rows[0]["ProcessId"] != DBNull.Value && Common.IntValue(dt.Rows[0]["ProcessId"]) != Process.GetCurrentProcess().Id)
                {
                    try
                    {
                        Process.GetProcessById(Common.IntValue(dt.Rows[0]["ProcessId"]));
                        throw new ManagedException("Chức năng này đang được chạy bởi ứng dụng khác");
                    }
                    catch (Exception)
                    {
                        //nothing
                    }
                }

                return;
            }

            GtidCommand sqlCmd = ConnectionUtil.Instance.GetConnection().CreateCommand();

            sqlCmd.CommandText = "sp_LockFunction_Insert1";
            sqlCmd.CommandType = CommandType.StoredProcedure;
            sqlCmd.Parameters.AddWithValue("@FormName", functionName);
            sqlCmd.Parameters.AddWithValue("@IdKho", idKho);
            sqlCmd.Parameters.AddWithValue("@IdUser", Declare.UserId);
            sqlCmd.Parameters.AddWithValue("@Computer", Common.GetComputerName());
            sqlCmd.Parameters.AddWithValue("@ProcessId", Process.GetCurrentProcess().Id);
            sqlCmd.ExecuteNonQuery();

            return;
        }
Ejemplo n.º 7
0
        private void btnUpdate_Click(object sender, EventArgs e)
        {
            try {
                string          sql;
                bool            InUse;
                int             index;
                CurrencyManager cm;
                if (String.IsNullOrEmpty(txtGroupId.Text))
                {
                    MessageBox.Show(Declare.msgInputData, Declare.titleNotice, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    txtGroupId.Focus();
                    return;
                }
                else
                {
                    sql = "select * from tbl_DM_NhomNguoiDung where MaNhom like N'" + txtGroupId.Text + "'";
                    if (!String.IsNullOrEmpty(txtId.Text))
                    {
                        sql += " and IdNhomNguoiDung <> " + txtId.Text;
                    }
                    if (DBTools.ExecuteScalar(sql) != null)
                    {
                        MessageBox.Show(Resources.MaDaDung, Declare.titleNotice, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        txtGroupId.Focus();
                        return;
                    }
                }
                if (String.IsNullOrEmpty(txtGroupName.Text))
                {
                    MessageBox.Show(Declare.msgInputData, Declare.titleNotice, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    txtGroupName.Focus();
                    return;
                }
                else
                {
                    sql = "select * from tbl_DM_NhomNguoiDung where TenNhom like N'" + txtGroupName.Text + "'";
                    if (!String.IsNullOrEmpty(txtId.Text))
                    {
                        sql += " and IdNhomNguoiDung <> " + txtId.Text;
                    }
                    if (DBTools.ExecuteScalar(sql) != null)
                    {
                        MessageBox.Show(Resources.TenDaDung, Declare.titleNotice, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        txtGroupName.Focus();
                        return;
                    }
                }

                if (chkSuDung.Checked)
                {
                    InUse = true;
                }
                else
                {
                    InUse = false;
                }
                if (flgUpdate || dgvGroupList.RowCount == 0)
                {
                    if (!DBTools.ExistData("tbl_DM_NhomNguoiDung", "MaNhom", typeof(string), txtGroupId.Text.Trim()))
                    {
                        //sql = "Insert Into tbl_DM_NhomNguoiDung(MaNhom, TenNhom, SuDung) Values(?,?,?)";
                        sql = "Insert Into tbl_DM_NhomNguoiDung Values(@MaNhom, @TenNhom, @SuDung)";

                        //SqlConnection SqlConn = Connection.Instance.GetSqlConnection();
                        //if (!(SqlConn.State == ConnectionState.Open)) SqlConn.Open();

                        GtidCommand SqlCmd = ConnectionUtil.Instance.GetConnection().CreateCommand();
                        SqlCmd.CommandText = "sp_DM_NhomNguoiDung_Insert";

                        SqlCmd.Parameters.AddWithValue("@IdNhomNguoiDung", 0).Direction         = ParameterDirection.Output;
                        SqlCmd.Parameters.AddWithValue("@MaNhom", txtGroupId.Text).Direction    = ParameterDirection.Input;
                        SqlCmd.Parameters.AddWithValue("@TenNhom", txtGroupName.Text).Direction = ParameterDirection.Input;
                        SqlCmd.Parameters.AddWithValue("@SuDung", InUse).Direction = ParameterDirection.Input;
                        SqlCmd.CommandType = CommandType.StoredProcedure;

                        SqlCmd.ExecuteNonQuery();
                        MessageBox.Show("Cập nhật thành công nhóm người dùng", Declare.titleNotice, MessageBoxButtons.OK, MessageBoxIcon.Information);
                        Common.LogAction("Thêm mới nhóm người dùng", "Tên nhóm " + txtGroupName.Text, -1);

                        object[] arrMang = { SqlCmd.Parameters[0].Value, this.txtGroupId.Text.Trim(), this.txtGroupName.Text, InUse };
                        dtUserGroup.Rows.Add(arrMang);

                        txtId.Text = SqlCmd.Parameters[0].Value.ToString();
                        //this.UpdateAutoColumn("STT", this.dtUserGroup.Rows.Count - 1);
                        index = this.dtUserGroup.Rows.Count - 1;

                        //sql = "Insert Into tbl_DM_NhomNguoiDung(MaNhom, TenNhom, SuDung) Values(N'" + txtGroupId.Text + "', N'" + txtGroupName.Text + "', " +InUse + ")";
                        //if (DBTools.ExecuteQuery(sql, CommandType.Text)!= null)
                        //{
                        //    this.RefreshGrid();

                        //    DataTable dtDanhSach = (DataTable)DBTools.getData("tbl_DM_NhomNguoiDung").Tables["tbl_DM_NhomNguoiDung"];
                        //    CurrencyManager cm = (CurrencyManager)this.BindingContext[dtDanhSach];
                        //    cm.Position = dtDanhSach.Rows.Count - 1;

                        //    dgvGroupList.Rows[dgvGroupList.Rows.Count - 1].Selected = true;
                        //    txtId.Text = dgvGroupList.Rows[dgvGroupList.Rows.Count - 1].Cells[1].Value.ToString();
                        //    txtGroupId.Text = dgvGroupList.Rows[dgvGroupList.Rows.Count - 1].Cells[2].Value.ToString();
                        //    txtGroupName.Text = dgvGroupList.Rows[dgvGroupList.Rows.Count - 1].Cells[3].Value.ToString();
                        //}
                        //else
                        //{
                        //    MessageBox.Show(Declare.msgUpdateErr, Declare.titleError, MessageBoxButtons.OK, MessageBoxIcon.Error);
                        //    return;
                        //}
                    }
                    else
                    {
                        MessageBox.Show(Declare.msgExistData, Declare.titleWarning, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        return;
                    }
                }
                else
                {
                    sql = "Update tbl_DM_NhomNguoiDung Set MaNhom = N'" + txtGroupId.Text + "', TenNhom = N'" + txtGroupName.Text + "', SuDung = " + (InUse ? 1 : 0) + " Where IdNhomNguoiDung = " + Common.IntValue(txtId.Text);
                    if (DBTools.ExecuteQuery(sql, CommandType.Text) != null)
                    {
                        dgvGroupList.CurrentRow.Cells["MaNhom"].Value  = txtGroupId.Text;
                        dgvGroupList.CurrentRow.Cells["TenNhom"].Value = txtGroupName.Text;
                        dgvGroupList.Rows[dgvGroupList.CurrentCell.RowIndex].Selected = true;

                        sql = "Delete From tbl_NhomND_ChucNang Where IdNhomNguoiDung = " + Common.IntValue(txtId.Text);
                        if (DBTools.ExecuteQuery(sql, CommandType.Text) == null)
                        {
                            MessageBox.Show(Declare.msgUpdateErr, Declare.titleError, MessageBoxButtons.OK, MessageBoxIcon.Error);
                            return;
                        }
                        index = dgvGroupList.Rows.IndexOf(dgvGroupList.CurrentRow);

                        Common.LogAction("Thêm mới nhóm người dùng", "GroupId " + txtId.Text, -1);
                    }
                    else
                    {
                        MessageBox.Show(Declare.msgUpdateErr, Declare.titleError, MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }
                }
                //MessageBox.Show("Cập nhật thông tin thành công!");
                for (int i = 0; i < chklstRightOfGroup.Items.Count; i++)
                {
                    if (chklstRightOfGroup.GetItemCheckState(i) == CheckState.Checked)
                    {
                        ListItem itemChecked = (ListItem)chklstRightOfGroup.Items[i];
                        sql = "Insert Into tbl_NhomND_ChucNang(IdNhomNguoiDung, IdChucNang) Values(" + Common.IntValue(txtId.Text) + ", " + Common.IntValue(itemChecked.ItemData.ToString()) + ")";
                        if (DBTools.ExecuteQuery(sql, CommandType.Text) == null)
                        {
                            MessageBox.Show(Declare.msgUpdateErr, Declare.titleError, MessageBoxButtons.OK, MessageBoxIcon.Error);
                            return;
                        }
                    }
                }
                MessageBox.Show("Cập nhật thông tin thành công!");
                RefreshGrid();
                cm          = (CurrencyManager)this.BindingContext[this.dtUserGroup];
                cm.Position = index;
            }
            catch (System.Exception ex) {
#if DEBUG
                MessageBox.Show("Lỗi ngoại lệ: " + ex.ToString(), Declare.titleError, MessageBoxButtons.OK, MessageBoxIcon.Error);
#else
                MessageBox.Show("Lỗi ngoại lệ: " + ex.Message, Declare.titleError, MessageBoxButtons.OK, MessageBoxIcon.Error);
#endif
            }
            finally {
                //this.dgvGroupList_SelectionChanged(null, EventArgs.Empty);
            }
        }
Ejemplo n.º 8
0
        private void btCapNhat_Click(object sender, EventArgs e)
        {
            try
            {
                if (txtMaCauHinh.Text.Trim() == "")
                {
                    MessageBox.Show("Mã loại cấu hình không được để trống!", Declare.titleNotice, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                if (txtCauHinh.Text.Trim() == "")
                {
                    MessageBox.Show("Tên cấu hình không được để trống!", Declare.titleNotice, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                if (KiemtraMaCauhinh(_listItem, txtMaCauHinh.Text.Trim()) == true)
                {
                    if (btThem.Enabled == false)
                    {
                        GtidCommand SqlCmd = ConnectionUtil.Instance.GetConnection().CreateCommand();
                        SqlCmd.CommandText = "sp_DM_CauHinh_Insert";
                        SqlCmd.CommandType = CommandType.StoredProcedure;
                        SqlCmd.Parameters.AddWithValue("@MaCauHinh", txtMaCauHinh.Text).Direction          = ParameterDirection.Input;
                        SqlCmd.Parameters.AddWithValue("@IdLoaiItem", cboLoaiItem.SelectedValue).Direction = ParameterDirection.Input;
                        SqlCmd.Parameters.AddWithValue("@TenCauHinh_ThietBi", txtCauHinh.Text).Direction   = ParameterDirection.Input;
                        SqlCmd.Parameters.AddWithValue("@GhiChu", txtGhiChu.Text).Direction   = ParameterDirection.Input;
                        SqlCmd.Parameters.AddWithValue("@SuDung", cbSuDung.Checked).Direction = ParameterDirection.Input;
                        SqlCmd.ExecuteNonQuery();

                        MessageBox.Show("Cập nhật danh mục thành công!", Declare.titleNotice, MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    else
                    {
                        int rowIndex = dgvList.CurrentRow.Index;

                        GtidCommand SqlCmd = ConnectionUtil.Instance.GetConnection().CreateCommand();
                        SqlCmd.CommandText = "sp_DM_CauHinh_Update";
                        SqlCmd.CommandType = CommandType.StoredProcedure;
                        SqlCmd.Parameters.AddWithValue("@MaCauHinh", txtMaCauHinh.Text).Direction = ParameterDirection.Input;
                        SqlCmd.Parameters.AddWithValue("@IdCauHinh_ThietBi", (int)dgvList["IdCauHinh_ThietBi", rowIndex].Value).Direction = ParameterDirection.Input;
                        SqlCmd.Parameters.AddWithValue("@IdLoaiItem", cboLoaiItem.SelectedValue).Direction = ParameterDirection.Input;
                        SqlCmd.Parameters.AddWithValue("@TenCauHinh_ThietBi", txtCauHinh.Text).Direction   = ParameterDirection.Input;
                        SqlCmd.Parameters.AddWithValue("@GhiChu", txtGhiChu.Text).Direction   = ParameterDirection.Input;
                        SqlCmd.Parameters.AddWithValue("@SuDung", cbSuDung.Checked).Direction = ParameterDirection.Input;
                        SqlCmd.ExecuteNonQuery();

                        MessageBox.Show("Cập nhật danh mục thành công!", Declare.titleNotice, MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    string strSql = "Select tb.*,li.* from tbl_DM_CauHinh tb inner join tbl_DM_LoaiItem li on tb.IdLoaiItem=li.IdLoaiItem";
                    _listItem = ut.getDataTable(strSql);
                    dgvList.AutoGenerateColumns = false;
                    dgvList.DataSource          = _listItem;
                    btThem.Enabled = true;
                }
                else
                {
                    MessageBox.Show("Mã loại cấu hình này đã được sử dụng", Declare.titleError, MessageBoxButtons.OK, MessageBoxIcon.Question);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Lỗi ngoại lệ: " + ex.Message, Declare.titleError, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
Ejemplo n.º 9
0
        private void btnImportNoiDungChiTiet_Click(object sender, EventArgs e)
        {
            try
            {
                string[]    columns = new string[] { "Số PO", "Số phiếu nhập", "Mã sản phẩm", "Tên sản phẩm", "Mã vạch", "Số lượng", "Đơn giá" };
                string      sql, inventoryOrg = currentTrungTam.MaTrungTam, inventorySub = currentKho.MaKho;
                int         _IdChungTuChiTiet = 0, _IdSanPham = 0;
                GtidCommand sqlCmd = ConnectionUtil.Instance.GetConnection().CreateCommand();
                sqlCmd.CommandTimeout = 0;
                DataSet ds = null;
                if (dtSoPhieuNhap == null || dtSoPhieuNhap.Rows.Count == 0)
                {
                    throw new ManagedException("Chưa nạp dữ liệu");
                }
                openFileDialog1.FileName = String.Empty;
                //openFileDialog1.Filter = "*.xls|*.xlsx";
                if (openFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    using (OleDbConnection oConn = new OleDbConnection())
                    {
                        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 fields = String.Empty;
                        Array.ForEach(columns,
                                      delegate(string s)
                        {
                            fields += String.IsNullOrEmpty(fields) ? String.Format("[{0}]", s) : String.Format(", [{0}]", s);
                        });

                        using (OleDbDataAdapter ad = new OleDbDataAdapter(String.Format("SELECT {0} from [{1}$]", fields, "Sheet1"), oConn))
                        {
                            if (ds == null)
                            {
                                ds = new DataSet();
                            }
                            ad.Fill(ds, "ImportTable");
                            sql = "SELECT [Số phiếu nhập] as SoPhieuNhap, [Số PO] as SoPO, SUM([Số lượng]) as TongSoLuong,"
                                  + " SUM([Số lượng]*[Đơn giá]) as ThanhTien FROM [Sheet1$] "
                                  + " GROUP BY [Số phiếu nhập], [Số PO]"
                                  + " ORDER BY [Số phiếu nhập], [Số PO]";
                            ad.SelectCommand.CommandText = sql;
                            ad.Fill(ds, "PhieuNhap");
                            ad.SelectCommand.CommandText = "SELECT [Số phiếu nhập] as SoPhieuNhap, [Số PO] as SoPO, [Mã sản phẩm] as MaSanPham, SUM([Số lượng]) as SoLuong,"
                                                           + " (SELECT TOP 1 [Đơn giá] FROM [Sheet1$] t2 WHERE t2.[Số phiếu nhập] = t1.[Số phiếu nhập]"
                                                           + " AND t2.[Số PO] = t1.[Số PO] AND t2.[Mã sản phẩm] = t1.[Mã sản phẩm]) as DonGia"
                                                           + " FROM [Sheet1$] as t1"
                                                           + " GROUP BY [Số phiếu nhập], [Số PO], [Mã sản phẩm]"
                                                           + " ORDER BY [Số phiếu nhập], [Số PO]";
                            ad.Fill(ds, "PhieuNhapChiTiet");
                        }
                        oConn.Close();
                    }
                    if (dtSoPhieuNhap.Rows.Count != ds.Tables["PhieuNhap"].Rows.Count)
                    {
                        throw new Exception("Tổng số phiếu nhập không khớp, hãy kiểm tra lại file import");
                    }
                    for (int i = 0; i <= ds.Tables["PhieuNhap"].Rows.Count - 1; i++)
                    {
                        if (!Object.Equals(dtSoPhieuNhap.Rows[i]["SoPhieuNhap"], ds.Tables["PhieuNhap"].Rows[i]["SoPhieuNhap"]))
                        {
                            throw new Exception(String.Format("Số phiếu nhập {0} không hợp lệ", ds.Tables["PhieuNhap"].Rows[i]["SoPhieuNhap"]));
                        }
                        if (!Object.Equals(dtSoPhieuNhap.Rows[i]["SoPO"], ds.Tables["PhieuNhap"].Rows[i]["SoPO"]))
                        {
                            throw new Exception(String.Format("Số PO {0} không hợp lệ", ds.Tables["PhieuNhap"].Rows[i]["SoPO"]));
                        }
                        if (!Object.Equals(dtSoPhieuNhap.Rows[i]["TongSoLuong"], int.Parse(ds.Tables["PhieuNhap"].Rows[i]["TongSoLuong"].ToString())))
                        {
                            throw new Exception(String.Format("Phiếu nhập {0} có tổng số lượng sản phẩm không hợp lệ", ds.Tables["PhieuNhap"].Rows[i]["SoPhieuNhap"]));
                        }
                    }

                    sqlCmd.CommandType = CommandType.StoredProcedure;
                    for (int i = 0; i <= ds.Tables["PhieuNhap"].Rows.Count - 1; i++)
                    {
                        sqlCmd.Transaction = ConnectionUtil.Instance.BeginTransaction();
                        DataRow[] foundRows = ds.Tables["PhieuNhapChiTiet"].Select(String.Format("SoPO ='{0}'", ds.Tables["PhieuNhap"].Rows[i]["SoPO"]));
                        if (foundRows.Length > 0)
                        {
                            if (dtSoPhieuNhap.Rows[i]["IdChungTu"] == DBNull.Value)
                            {
                                sqlCmd.CommandText = "sp_ChungTu_NhapHang_Insert";
                            }
                            else
                            {
                                sqlCmd.CommandText = "sp_ChungTu_NhapHang_Update";
                            }
                            sql = "SELECT TOP 1 * FROM tbl_Tmp_NhapHang WHERE SoPO =N'{0}' AND SoPhieuNhap=N'{1}' AND InventoryOrg=N'{2}' AND InventorySub=N'{3}'";
                            sql = String.Format(sql, dtSoPhieuNhap.Rows[i]["SoPO"], dtSoPhieuNhap.Rows[i]["SoPhieuNhap"], inventoryOrg, inventorySub);
                            DataTable dtTemp = DBTools.getData("Temp", sql).Tables["Temp"];

                            sqlCmd.Parameters.Clear();
                            sqlCmd.Parameters.AddWithValue("@IdChungTu", dtSoPhieuNhap.Rows[i]["IdChungTu"]).Direction = ParameterDirection.InputOutput;
                            sqlCmd.Parameters.AddWithValue("@LoaiChungTu", (int)TransactionType.NHAP_PO);
                            sqlCmd.Parameters.AddWithValue("@SoSeri", dtSoPhieuNhap.Rows[i]["SoPO"]);
                            sqlCmd.Parameters.AddWithValue("@SoChungTu", dtSoPhieuNhap.Rows[i]["SoPhieuNhap"]);
                            sqlCmd.Parameters.AddWithValue("@NgayLap", dtTemp.Rows[0]["NgayNhap"]);
                            sqlCmd.Parameters.AddWithValue("@IdKho", Declare.IdKho);
                            sqlCmd.Parameters.AddWithValue("@IdNhanVien", Declare.IdNhanVien);
                            sqlCmd.Parameters.AddWithValue("@TongTienHang", ds.Tables["PhieuNhap"].Rows[i]["ThanhTien"]);
                            sqlCmd.Parameters.AddWithValue("@GhiChu", dtTemp.Rows[0]["GhiChu"]);
                            sqlCmd.Parameters.AddWithValue("@NguoiTao", Declare.UserName);
                            sqlCmd.Parameters.AddWithValue("@ThoiGianTao", dtTemp.Rows[0]["ThoiGian"]);
                            sqlCmd.Parameters.AddWithValue("@TenMayTao", Declare.TenMay);
                            sqlCmd.ExecuteNonQuery();

                            if (dtSoPhieuNhap.Rows[i]["IdChungTu"] == DBNull.Value)
                            {
                                dtSoPhieuNhap.Rows[i]["IdChungTu"] = sqlCmd.Parameters["@IdChungTu"].Value;
                            }
                            _IdChungTuChiTiet = 0;
                            foreach (DataRow dr in foundRows)
                            {
                                sqlCmd.Parameters.Clear();
                                _IdSanPham = int.Parse(DBTools.getValue(String.Format("SELECT IdSanPham FROM tbl_SanPham WHERE MaSanPham=N'{0}'", dr["MaSanPham"])));
                                //string tmp =DBTools.getValue(String.Format("SELECT IdChiTiet FROM tbl_ChungTu_ChiTiet " +
                                //    "WHERE IdChungTu={0} AND IdSanPham = {1}",
                                //    dtSoPhieuNhap.Rows[i]["IdChungTu"], _IdSanPham));
                                if (int.TryParse(DBTools.getValue(String.Format("SELECT IdChiTiet FROM tbl_ChungTu_ChiTiet " +
                                                                                "WHERE IdChungTu={0} AND IdSanPham = {1}",
                                                                                dtSoPhieuNhap.Rows[i]["IdChungTu"], _IdSanPham)), out _IdChungTuChiTiet))
                                {
                                    sqlCmd.CommandText = "sp_ChungTu_NhapHang_ChiTiet_Update";
                                }
                                else
                                {
                                    sqlCmd.CommandText = "sp_ChungTu_NhapHang_ChiTiet_Insert";
                                }
                                sqlCmd.Parameters.AddWithValue("@IdChiTiet", _IdChungTuChiTiet).Direction = ParameterDirection.InputOutput;
                                sqlCmd.Parameters.AddWithValue("@IdChungTu", dtSoPhieuNhap.Rows[i]["IdChungTu"]);
                                sqlCmd.Parameters.AddWithValue("@IdSanPham", _IdSanPham);
                                sqlCmd.Parameters.AddWithValue("@SoLuong", dr["SoLuong"]);
                                sqlCmd.Parameters.AddWithValue("@DonGia", dr["DonGia"]);
                                sqlCmd.Parameters.AddWithValue("@ThanhTien", int.Parse(dr["SoLuong"].ToString()) * int.Parse(dr["DonGia"].ToString()));
                                sqlCmd.ExecuteNonQuery();

                                if (_IdChungTuChiTiet == 0)
                                {
                                    _IdChungTuChiTiet = Convert.ToInt32(sqlCmd.Parameters["@IdChiTiet"].Value);
                                }

                                sqlCmd.CommandText = "sp_ChungTu_NhapHang_ChiTiet_HangHoa_Delete";
                                sqlCmd.Parameters.Clear();
                                sqlCmd.Parameters.AddWithValue("@IdChiTietChungTu", _IdChungTuChiTiet);
                                sqlCmd.ExecuteNonQuery();

                                DataRow[] foundRows1 = ds.Tables["ImportTable"].Select(String.Format("[Số PO] ='{0}' AND [Số phiếu nhập]='{1}' AND [Mã sản phẩm]='{2}'"
                                                                                                     , dr["SoPO"]
                                                                                                     , dr["SoPhieuNhap"]
                                                                                                     , dr["MaSanPham"]));
                                foreach (DataRow dr1 in foundRows1)
                                {
                                    sqlCmd.CommandText = "sp_ChungTu_NhapHang_ChiTiet_HangHoa_Insert";
                                    sqlCmd.Parameters.Clear();
                                    sqlCmd.Parameters.AddWithValue("@IdChiTietChungTu", _IdChungTuChiTiet);
                                    sqlCmd.Parameters.AddWithValue("@IdKho", Declare.IdKho);
                                    sqlCmd.Parameters.AddWithValue("@IdSanPham", _IdSanPham);
                                    sqlCmd.Parameters.AddWithValue("@MaVach", dr1["Mã vạch"]);
                                    sqlCmd.Parameters.AddWithValue("@SoLuong", dr1["Số lượng"]);
                                    sqlCmd.ExecuteNonQuery();
                                }
                            }
                        }
                        ConnectionUtil.Instance.CommitTransaction();
                    }
                }
            }
            catch (System.Exception ex)
            {
                try
                {
                    ConnectionUtil.Instance.RollbackTransaction();
                }
                catch { };
#if DEBUG
                MessageBox.Show(ex.ToString());
#else
                MessageBox.Show(ex.Message);
#endif
            }
        }
Ejemplo n.º 10
0
        private void btnGhi_Click(object sender, EventArgs e)
        {
            try {
                if (this.ArrTemp != null && this.ArrTemp.Count > 0)
                {
                    GtidCommand sqlCmd = ConnectionUtil.Instance.GetConnection().CreateCommand();
                    sqlCmd.CommandType    = CommandType.StoredProcedure;
                    sqlCmd.CommandTimeout = 0;
                    sqlCmd.Transaction    = ConnectionUtil.Instance.BeginTransaction();
                    if (_IdChungTu == 0)
                    {
                        sqlCmd.CommandText = "sp_ChungTu_NhapHang_Insert";
                    }
                    else
                    {
                        sqlCmd.CommandText = "sp_ChungTu_NhapHang_Update";
                    }
                    sqlCmd.Parameters.AddWithValue("@IdChungTu", _IdChungTu).Direction = ParameterDirection.InputOutput;
                    sqlCmd.Parameters.AddWithValue("@LoaiChungTu", (int)TransactionType.NHAP_PO);
                    sqlCmd.Parameters.AddWithValue("@SoSeri", txtSoPO.Text);
                    sqlCmd.Parameters.AddWithValue("@SoChungTu", txtPhieuNhap.Text);
                    sqlCmd.Parameters.AddWithValue("@NgayLap", DateTime.Parse(txtNgayNhap.Text));
                    sqlCmd.Parameters.AddWithValue("@IdKho", Declare.IdKho);
                    sqlCmd.Parameters.AddWithValue("@IdNhanVien", Declare.IdNhanVien);
                    sqlCmd.Parameters.AddWithValue("@TongTienHang", Common.DoubleValue(txtTongTienHang.Text));
                    sqlCmd.Parameters.AddWithValue("@GhiChu", txtGhiChu.Text);
                    sqlCmd.Parameters.AddWithValue("@NguoiTao", Declare.UserName);
                    sqlCmd.Parameters.AddWithValue("@ThoiGianTao", DateTime.Parse(txtThoiGianTaoGD.Text));
                    sqlCmd.Parameters.AddWithValue("@TenMayTao", Declare.TenMay);
                    sqlCmd.ExecuteNonQuery();

                    if (_IdChungTu == 0)
                    {
                        _IdChungTu = Convert.ToInt32(sqlCmd.Parameters["@IdChungTu"].Value);
                        dtSoPhieuNhap.Rows[CurrentIndex]["IdChungTu"] = _IdChungTu;
                    }
                    foreach (DataGridViewRow dgr in dgvList.Rows)
                    {
                        foreach (DataTable dt in this.ArrTemp)
                        {
                            if (dt.TableName == dgr.Cells["IdSanPham"].Value.ToString())
                            {
                                int _IdChungTuChiTiet = dgr.Cells["IdChungTuChiTiet"].Value != DBNull.Value ? Convert.ToInt32(dgr.Cells["IdChungTuChiTiet"].Value) : 0;
                                if (_IdChungTuChiTiet == 0)
                                {
                                    sqlCmd.CommandText = "sp_ChungTu_NhapHang_ChiTiet_Insert";
                                }
                                else
                                {
                                    sqlCmd.CommandText = "sp_ChungTu_NhapHang_ChiTiet_Update";
                                }
                                sqlCmd.Parameters.Clear();
                                sqlCmd.Parameters.AddWithValue("@IdChiTiet", _IdChungTuChiTiet).Direction = ParameterDirection.InputOutput;
                                sqlCmd.Parameters.AddWithValue("@IdChungTu", _IdChungTu);
                                sqlCmd.Parameters.AddWithValue("@IdSanPham", dgr.Cells["IdSanPham"].Value);
                                sqlCmd.Parameters.AddWithValue("@SoLuong", dgr.Cells["SoLuong"].Value);
                                sqlCmd.Parameters.AddWithValue("@DonGia", dgr.Cells["DonGia"].Value);
                                sqlCmd.Parameters.AddWithValue("@ThanhTien", dgr.Cells["ThanhTien"].Value);
                                sqlCmd.ExecuteNonQuery();
                                if (_IdChungTuChiTiet == 0)
                                {
                                    dgr.Cells["IdChungTuChiTiet"].Value = Convert.ToInt32(sqlCmd.Parameters["@IdChiTiet"].Value);
                                }

                                sqlCmd.CommandText = "sp_ChungTu_NhapHang_ChiTiet_HangHoa_Delete";
                                sqlCmd.Parameters.Clear();
                                sqlCmd.Parameters.AddWithValue("@IdChiTietChungTu", dgr.Cells["IdChungTuChiTiet"].Value);
                                sqlCmd.ExecuteNonQuery();

                                foreach (DataRow dr in dt.Rows)
                                {
                                    if (dr.RowState == DataRowState.Deleted)
                                    {
                                        continue;
                                    }
                                    sqlCmd.CommandText = "sp_ChungTu_NhapHang_ChiTiet_HangHoa_Insert";
                                    sqlCmd.Parameters.Clear();
                                    sqlCmd.Parameters.AddWithValue("@IdChiTietChungTu", dgr.Cells["IdChungTuChiTiet"].Value);
                                    sqlCmd.Parameters.AddWithValue("@IdKho", Declare.IdKho);
                                    sqlCmd.Parameters.AddWithValue("@IdSanPham", dgr.Cells["IdSanPham"].Value);
                                    sqlCmd.Parameters.AddWithValue("@MaVach", dr["MaVach"]);
                                    sqlCmd.Parameters.AddWithValue("@SoLuong", dr["SoLuong"]);
                                    sqlCmd.ExecuteNonQuery();
                                }
                                break;
                            }
                        }
                    }
                    this.ArrTemp = null;
                    Common.LogERPSync("Đồng bộ chứng từ nhập", "IdChungTu = " + _IdChungTu + "; Số chứng từ " + txtPhieuNhap.Text, Declare.IdKho, String.Empty, "Completed", DateTime.Now);
                    ConnectionUtil.Instance.CommitTransaction();
                }
            }
            catch (System.Exception ex) {
                ConnectionUtil.Instance.RollbackTransaction();
                Common.LogERPSync("Đồng bộ chứng từ nhập", ex.Message, Declare.IdKho, String.Empty, "Failed", DateTime.Now);
                throw ex;
            }
        }