コード例 #1
0
        private void btnSave_Click(object sender, EventArgs e)
        {
            if (ValidateFields() == false)
            {
                return;
            }
            if (dgInventory.Rows.Count < 1 || InventoryAdjustmentCount == 0)
            {
                return;
            }
            // int selectedRows = dgInventory.SelectedRows.Count;

            int          selectedCount = 0;
            DialogResult result        = MessageBox.Show("You are about to adjust the inventory for " + InventoryAdjustmentCount.ToString() + " item(s). Are you sure you want proceed.?", "MICS", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

            if (result == DialogResult.No)
            {
                return;
            }
            int  curIndex = 0;
            bool success  = true;

            for (int row = 0; row < dgInventory.Rows.Count; row++)
            {
                if (dsProductInventory.Tables[0].Rows[row]["AdjustedQuantity"] == DBNull.Value)
                {
                    continue;
                }
                if (selectedCount > InventoryAdjustmentCount)
                {
                    break;
                }
                selectedCount++;
                //DataGridViewRow row = dgInventory.CurrentRow;
                //int curIndex = dgInventory.CurrentRow.Index;
                curIndex = row;
                //if (row == null) return;


                Cursor.Current = Cursors.WaitCursor;
                ProductInventory         pi  = new ProductInventory();
                ProductAdjustmentHistory pah = new ProductAdjustmentHistory();
                // int adjustedQuantity = Int32.Parse(txtAdjustedQuanity.Text.Trim());
                //   string reason = txtReason.Text.Trim();


                try
                {
                    int productid   = Int32.Parse(dsProductInventory.Tables[0].Rows[row]["productid"].ToString()); //Int32.Parse(dgInventory.CurrentRow.Cells["productid"].Value.ToString());
                    int CurQuantity = 0;
                    int NewQuantity = 0;
                    if (dsProductInventory.Tables[0].Rows[row]["AdjustedQuantity"] != DBNull.Value)
                    {
                        NewQuantity = Int32.Parse(dsProductInventory.Tables[0].Rows[row]["AdjustedQuantity"].ToString());
                    }
                    if (dsProductInventory.Tables[0].Rows[row]["Quantity"] != DBNull.Value)
                    {
                        CurQuantity = Int32.Parse(dsProductInventory.Tables[0].Rows[row]["Quantity"].ToString());
                    }


                    pah.ProductID        = productid;
                    pah.AdjustedQuantity = NewQuantity - CurQuantity;
                    if (dsProductInventory.Tables[0].Rows[row]["Reason"] != DBNull.Value)
                    {
                        pah.Reason = dsProductInventory.Tables[0].Rows[row]["Reason"].ToString();
                    }
                    else
                    {
                        pah.Reason = "";
                    }
                    // pah.AdjustedQuantity = adjustedQuantity;
                    pah.ModifiedDate = DateTime.Now;
                    pah.AddProductAdjustmentHistory(pah);


                    pi.ProductID    = productid;
                    pi.Quantity     = NewQuantity;
                    pi.LocationID   = 0;
                    pi.ModifiedDate = DateTime.Now;
                    pi.Shelf        = "";
                    pi.Bin          = 0;
                    ProductInventory inv = new ProductInventory();
                    inv = inv.GetProductInventorys(productid);
                    //  pi.Quantity = (long) adjustedQuantity;
                    //pi.ModifiedDate = DateTime.Now;
                    if (inv.ProductID > 0)
                    {
                        pi.UpdateProductInventory(pi);
                    }
                    else
                    {
                        pi.AddProductInventory(pi);
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Product Inventory", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    Cursor.Current = Cursors.Default;
                    success        = false;
                    break;
                }
                finally
                {
                    Cursor.Current = Cursors.Default;
                }
            }
            if (success)
            {
                MessageBox.Show("Inventory Adjusted successfully.", "MICS", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                MessageBox.Show("Inventory Adjustment failed.", "MICS", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            Search(curIndex);
        }