Ejemplo n.º 1
0
        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (!btnDelete.Enabled)
            {
                return;
            }
            if (!grdStockAdjustments.Redraw)
            {
                return;
            }
            if (grdStockAdjustments.DataSource == null)
            {
                return;
            }
            if (grdStockAdjustments.RowSel < grdStockAdjustments.Rows.Fixed)
            {
                return;
            }
            if (Materia.IsNullOrNothing(grdStockAdjustments[grdStockAdjustments.RowSel, "ReferenceNo"]))
            {
                return;
            }

            string    _referenceno      = grdStockAdjustments[grdStockAdjustments.RowSel, "ReferenceNo"].ToString();
            DataTable _stockadjustments = Cache.GetCachedTable("stockadjustments");

            if (_stockadjustments != null)
            {
                DataRow[] _rows = _stockadjustments.Select("[ReferenceNo] LIKE '" + _referenceno.ToSqlValidString(true) + "'");
                if (_rows.Length > 0)
                {
                    DataRow _row = _rows[0];
                    if (VisualBasic.IsNumeric(_row["Closed"]))
                    {
                        if (VisualBasic.CBool(_row["Closed"]))
                        {
                            MsgBoxEx.Shout("Cannot delete stock adjustment : <font color=\"blue\">" + _referenceno + "</font> because it is already marked as final.", "Delete Stock Adjustment");
                            return;
                        }
                    }

                    if (MsgBoxEx.Ask("Delete stock adjustment <font color=\"blue\">" + _referenceno + "</font> permanently from the list?", "Delete Stock Adjustments") != System.Windows.Forms.DialogResult.Yes)
                    {
                        return;
                    }

                    string       _query      = "DELETE FROM `stockadjustments` WHERE (`ReferenceNo` LIKE '" + _referenceno.ToSqlValidString(true) + "')";
                    IAsyncResult _execresult = Que.BeginExecution(SCMS.Connection, _query);

                    btnNew.Enabled     = false; btnEdit.Enabled = false; btnDelete.Enabled = false;
                    btnRefresh.Enabled = false; txtSearch.Enabled = false;

                    while (!_execresult.IsCompleted &&
                           !_cancelled)
                    {
                        Thread.Sleep(1); Application.DoEvents();
                    }

                    if (_cancelled)
                    {
                        if (!_execresult.IsCompleted)
                        {
                            try { _execresult = null; }
                            catch { }
                            finally { Materia.RefreshAndManageCurrentProcess(); }
                        }

                        return;
                    }
                    else
                    {
                        QueResult _result = Que.EndExecution(_execresult);
                        if (string.IsNullOrEmpty(_result.Error.RLTrim()))
                        {
                            _row.Delete(); _stockadjustments.AcceptChanges();

                            if (grdStockAdjustments.Redraw)
                            {
                                grdStockAdjustments.BeginUpdate();
                            }

                            DataTable _datasource = null;

                            try { _datasource = (DataTable)grdStockAdjustments.DataSource; }
                            catch { }

                            if (_datasource != null)
                            {
                                DataRow[] _currows = _datasource.Select("[ReferenceNo] LIKE '" + _referenceno.ToSqlValidString(true) + "'");
                                if (_currows.Length > 0)
                                {
                                    _currows[0].Delete();
                                }
                                _datasource.AcceptChanges();
                            }

                            FormatGrid(); ResizeGrid();

                            Cursor = Cursors.WaitCursor;
                            IAsyncResult _logresult = SCMS.CurrentSystemUser.LogActionAsync(UserAction.Delete, "Deletes stock adjustment : " + _referenceno + ".", _referenceno);
                            _logresult.WaitToFinish(); Cursor = Cursors.Default;

                            while (!grdStockAdjustments.Redraw)
                            {
                                grdStockAdjustments.EndUpdate();
                            }
                        }
                        else
                        {
                            SCMS.LogError(this.GetType().Name, new Exception(_result.Error));
                            MsgBoxEx.Alert("Failed to delete the specified stock adjustment.", "Delete Stock Adjustment");
                        }

                        _result.Dispose(); EnableButtons(); DisplayInfo();
                    }
                }
            }
        }
Ejemplo n.º 2
0
        private void btnSave_Click(object sender, EventArgs e)
        {
            if (!btnSave.Enabled)
            {
                return;
            }

            Validator _validator = SCMS.Validators[this];

            if (!Materia.Valid(_validator, txtCurrency, !string.IsNullOrEmpty(txtCurrency.Text.RLTrim()), "Please specify currency."))
            {
                return;
            }
            if (!Materia.Valid(_validator, cboAccount, cboAccount.SelectedIndex >= 0, "Please specify a valid account."))
            {
                return;
            }
            if (!Materia.Valid(_validator, cboExchangeRateDiff, cboExchangeRateDiff.SelectedIndex >= 0, "Please specify a valid exchange rate diff. account."))
            {
                return;
            }

            DataTable _currencies = Cache.GetCachedTable("currencies");

            if (_currencies != null)
            {
                DataRow[] _rows = _currencies.Select("([Currency] LIKE '" + txtCurrency.Text.ToSqlValidString(true) + "') AND\n" +
                                                     "NOT ([Currency] LIKE '" + _currency.ToSqlValidString(true) + "')");

                if (!Materia.Valid(_validator, txtCurrency, _rows.Length <= 0, "Currency already exists."))
                {
                    return;
                }

                string _query = "";
                DataColumnCollection _cols = _currencies.Columns;

                if (_isnew)
                {
                    _query = "INSERT INTO `currencies`\n" +
                             "(`Currency`, `Description`, `AccountCode`, `ExchangeRateAccountCode`, `DateCreated`)\n" +
                             "VALUES\n" +
                             "('" + txtCurrency.Text.ToSqlValidString() + "', '" + txtDescription.Text.ToSqlValidString() + "', " + cboAccount.SelectedValue.ToString() + ", " + cboExchangeRateDiff.SelectedValue.ToString() + ", NOW());";

                    object[] _values = new object[_cols.Count];
                    _values[_cols["Currency"].Ordinal]                = txtCurrency.Text;
                    _values[_cols["Description"].Ordinal]             = txtDescription.Text;
                    _values[_cols["AccountCode"].Ordinal]             = cboAccount.SelectedValue;
                    _values[_cols["ExchangeRateAccountCode"].Ordinal] = cboExchangeRateDiff.SelectedValue;
                    _values[_cols["DateCreated"].Ordinal]             = DateTime.Now;
                    _values[_cols["LastModified"].Ordinal]            = DateTime.Now;
                    _currencies.Rows.Add(_values);
                }
                else
                {
                    _query = "UPDATE `currencies` SET\n" +
                             "`Currency` = '" + txtCurrency.Text.ToSqlValidString() + "', `Description` = '" + txtDescription.Text.ToSqlValidString() + "', `AccountCode` = " + cboAccount.SelectedValue.ToString() + ", `ExchangeRateAccountCode` = " + cboExchangeRateDiff.SelectedValue.ToString() + "\n" +
                             "WHERE\n" +
                             "(`Currency` LIKE '" + _currency.ToSqlValidString() + "');";

                    DataRow[] _existing = _currencies.Select("[Currency] LIKE '" + _currency.ToSqlValidString(true) + "'");
                    if (_existing.Length > 0)
                    {
                        _existing[0]["Currency"]                = txtCurrency.Text;
                        _existing[0]["Description"]             = txtDescription.Text;
                        _existing[0]["AccountCode"]             = cboAccount.SelectedValue;
                        _existing[0]["ExchangeRateAccountCode"] = cboExchangeRateDiff.SelectedValue;
                    }
                }

                if (!string.IsNullOrEmpty(_query.RLTrim()))
                {
                    btnSave.Enabled = false; btnSaveAndClose.Enabled = false;

                    IAsyncResult _result = Que.BeginExecution(SCMS.Connection, _query);

                    while (!_result.IsCompleted &&
                           !_cancelled)
                    {
                        Thread.Sleep(1); Application.DoEvents();
                    }

                    if (_cancelled)
                    {
                        if (!_result.IsCompleted)
                        {
                            try { _result = null; }
                            catch { }
                            finally { Materia.RefreshAndManageCurrentProcess(); }
                        }

                        return;
                    }
                    else
                    {
                        QueResult _queresult = Que.EndExecution(_result);

                        if (string.IsNullOrEmpty(_queresult.Error.RLTrim()))
                        {
                            UserAction _action = UserAction.Add;
                            if (!_isnew)
                            {
                                _action = UserAction.Edit;
                            }

                            string _log = "Added a new currency : " + txtCurrency.Text + ".";
                            if (!_isnew)
                            {
                                _log = "Updated currency : " + _currency + (_currency != txtCurrency.Text ? " to " + txtCurrency.Text : "").ToString() + ".";
                            }

                            _currencies.AcceptChanges(); _currency = txtCurrency.Text;
                            if (_isnew)
                            {
                                _isnew = false;
                            }
                            if (_updated)
                            {
                                _updated = false;
                            }
                            if (!_withupdates)
                            {
                                _withupdates = true;
                            }
                            Text   = Text.Replace(" *", "").Replace("*", "");
                            Cursor = Cursors.WaitCursor;

                            IAsyncResult _logresult = SCMS.CurrentSystemUser.LogActionAsync(_action, _log);
                            _logresult.WaitToFinish();

                            Cursor = Cursors.Default;

                            if (sender == btnSaveAndClose)
                            {
                                DialogResult = System.Windows.Forms.DialogResult.OK; Close();
                            }
                        }
                        else
                        {
                            if (_queresult.Error.Contains("duplicate"))
                            {
                                bool _invalid = Materia.Valid(_validator, txtCurrency, false, "Currency already exists.");
                            }
                            else
                            {
                                SCMS.LogError(this.GetType().Name, new Exception(_queresult.Error));
                                MsgBoxEx.Alert("Failed to save the current currency.", "Save Currency");
                            }

                            _currencies.RejectChanges();
                        }

                        _queresult.Dispose();
                    }

                    btnSave.Enabled = true; btnSaveAndClose.Enabled = true;
                }
            }
            else
            {
                if (sender == btnSaveAndClose)
                {
                    DialogResult = System.Windows.Forms.DialogResult.None; Close();
                }
            }
        }
Ejemplo n.º 3
0
        private DataTable GetDataSource()
        {
            DataTable _datasource = null;

            Cache.SyncTable(SCMS.Connection, "stockadjustments");
            Cache.SyncTable(SCMS.Connection, "stockadjustmentdetails");
            Cache.SyncTable(SCMS.Connection, "users");
            Cache.SyncTable(SCMS.Connection, "parts");
            Cache.SyncTable(SCMS.Connection, "measurements");
            Cache.SyncTable(SCMS.Connection, "stockledger");

            DataTable _adjustments = Cache.GetCachedTable("stockadjustments");
            DataTable _users       = Cache.GetCachedTable("users");

            if (_adjustments != null &&
                _users != null)
            {
                DataTable _approver  = _users.Replicate();
                DataTable _canceller = _users.Replicate();
                DataTable _closing   = _users.Replicate();

                var _query = from _adj in _adjustments.AsEnumerable()
                             join _creator in _users.AsEnumerable() on _adj.Field <string>("Username") equals _creator.Field <string>("Username")
                             join _app in _approver.AsEnumerable() on _adj.Field <string>("ApprovedBy") equals _app.Field <string>("Username") into _a
                             join _cancel in _canceller.AsEnumerable() on _adj.Field <string>("CancelledBy") equals _cancel.Field <string>("Username") into _c
                             join _close in _closing.AsEnumerable() on _adj.Field <string>("ClosedBy") equals _close.Field <string>("Username") into _cl
                                 where _adj.Field <string>("Company") == SCMS.CurrentCompany.Company
                             from _app in _a.DefaultIfEmpty(_approver.NewRow())
                             from _cancel in _c.DefaultIfEmpty(_canceller.NewRow())
                             from _close in _cl.DefaultIfEmpty(_closing.NewRow())
                             select new
                {
                    ReferenceNo = _adj.Field <string>("ReferenceNo"),
                    Date        = _adj.Field <DateTime>("Dated"),
                    Summary     = _adj.Field <string>("Summary"),
                    Approval    = (_adj.Field <Int16>("Approved") == 1 || _adj.Field <Int16>("Cancelled") == 1  ? (_adj.Field <Int16>("Approved") == 1 ? "Approved" : "") + (_adj.Field <Int16>("Cancelled") == 1 ? "Cancelled" : "") : "For Approval"),
                    Status      = (_adj.Field <Int16>("Closed") == 1? "Closed" : "Open"),
                    DateCreated = _adj.Field <DateTime>("DateCreated"),
                    CreatedBy   = _creator.Field <string>("FirstName") + " " + _creator.Field <string>("LastName"),
                    RefDate     = (_adj.Field <Int16>("Approved") == 1 || _adj.Field <Int16>("Cancelled") == 1 ? (_adj.Field <Int16>("Approved") == 1 ? _adj.Field <DateTime>("DateApproved") : _adj.Field <DateTime>("DateCancelled")) : VisualBasic.CDate("1/1/1900")),
                    RefName     = (_adj.Field <Int16>("Approved") == 1 || _adj.Field <Int16>("Cancelled") == 1 ? (_adj.Field <Int16>("Approved") == 1 ? _app.Field <string>("FirstName") + " " + _app.Field <string>("LastName") : _cancel.Field <string>("FirstName") + " " + _cancel.Field <string>("LastName")) : ""),
                    DateClosed  = _adj.Field <DateTime>("DateClosed"),
                    ClosedBy    = _close.Field <string>("FirstName") + " " + _close.Field <string>("LastName")
                };

                _datasource           = new DataTable();
                _datasource.TableName = "stockadjustments";
                DataColumnCollection _cols = _datasource.Columns;

                DataColumn _pk = _cols.Add("ReferenceNo", typeof(string));
                _cols.Add("Dated", typeof(DateTime));
                _cols.Add("Summary", typeof(string));
                _cols.Add("Approval", typeof(string));
                _cols.Add("Status", typeof(string));
                _cols.Add("DateCreated", typeof(DateTime));
                DataColumn _refdatecol = _cols.Add("RefDate", typeof(DateTime));
                _cols.Add("RefName", typeof(string));
                DataColumn _dateclosedcol = _cols.Add("DateClosed", typeof(DateTime));
                _cols.Add("ClosedBy", typeof(string));

                _datasource.Constraints.Add("PK", _pk, true);
                _refdatecol.AllowDBNull = true; _dateclosedcol.AllowDBNull = true;

                try
                {
                    foreach (var _row in _query)
                    {
                        object _refdate = DBNull.Value;
                        if (_row.RefDate != VisualBasic.CDate("1/1/1900"))
                        {
                            _refdate = _row.RefDate;
                        }

                        object _dateclosed = DBNull.Value;
                        if (_row.DateClosed != VisualBasic.CDate("1/1/1900"))
                        {
                            _dateclosed = _row.DateClosed;
                        }

                        _datasource.Rows.Add(new object[] {
                            _row.ReferenceNo, _row.Date, _row.Summary,
                            _row.Approval, _row.Status, _row.DateCreated,
                            _refdate, _row.RefName, _dateclosed,
                            _row.ClosedBy
                        });
                    }
                }
                catch { }

                _datasource.AcceptChanges();
            }

            return(_datasource);
        }