Exemplo n.º 1
0
        private void btnTransfer_Click(object sender, EventArgs e)
        {
            if (dgvProductList.Rows.Count == 0)
            {
                return;
            }

            try
            {
                foreach (DataGridViewRow row in dgvProductList.Rows)
                {
                    string fromLoc = row.Cells["colFromLocation"] == null ? string.Empty : row.Cells["colFromLocation"].Value.ToString();
                    string toLoc   = row.Cells["colToLocation"].Value == null ? string.Empty : row.Cells["colToLocation"].Value.ToString();


                    if (fromLoc == toLoc)
                    {
                        row.DefaultCellStyle.BackColor = Color.Tomato;
                        Helper.ShowMessage("Stock cannot be tranfered to the same location",
                                           "Illegal Operation", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

                        return;
                    }
                    else
                    {
                        row.DefaultCellStyle.BackColor = Color.White;
                    }

                    if (string.IsNullOrEmpty(fromLoc) || string.IsNullOrEmpty(toLoc))
                    {
                        row.DefaultCellStyle.BackColor = Color.Tomato;
                        Helper.ShowMessage("Verify and set all locations involved in the current tranfer",
                                           "Illegal Operation", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

                        return;
                    }
                    else
                    {
                        row.DefaultCellStyle.BackColor = Color.White;
                    }

                    if (row.Cells["colProductID"].Value == null)
                    {
                        row.DefaultCellStyle.BackColor = Color.Tomato;
                        Helper.ShowMessage("Make sure a valid product is selected",
                                           "Illegal Operation", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

                        return;
                    }
                    else
                    {
                        row.DefaultCellStyle.BackColor = Color.White;
                    }

                    int oldqty     = row.Cells["colCurrQty"].Value == null ? 0 : row.Cells["colCurrQty"].Value.ToInt();
                    int currentqty = row.Cells["colQuantity"].Value.ToInt();
                    int productID  = row.Cells["colProductID"].Value.ToInt();

                    int newqty = oldqty - currentqty;

                    if (newqty < 0)
                    {
                        row.DefaultCellStyle.BackColor = Color.Tomato;
                        Helper.ShowMessage("Cannot transfer more stock than is currently at location",
                                           "Illegal Operation", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

                        return;
                    }
                    else
                    {
                        row.DefaultCellStyle.BackColor = Color.White;
                    }

                    ProductLocation prodloc = new ProductLocationService().GetSingle(new ProductLocation {
                        LocationID = toLoc.ToInt(), ProductID = productID
                    });

                    int fromLocQtyBeforeTransfer = 0;
                    int ToLocQtyBeforeTransfer   = 0;

                    if (prodloc != null)
                    {
                        fromLocQtyBeforeTransfer = prodloc.Quantity;
                        ToLocQtyBeforeTransfer   = prodloc == null ? 0 : prodloc.Quantity;

                        _productService.AddInventory(currentqty, productID, toLoc.ToInt());
                    }
                    else
                    {
                        new ProductLocationService().Add(new ProductLocation
                        {
                            ProductID  = productID,
                            LocationID = toLoc.ToInt(),
                            Quantity   = currentqty
                        });
                    }

                    _productService.ReduceInventory(currentqty, productID, fromLoc.ToInt());


                    new StockTranferService().Add(new StockTranfer
                    {
                        FromLocationID = fromLoc.ToInt(),
                        FromLocationQtyAfterTransfer = (fromLocQtyBeforeTransfer - currentqty),
                        FromLocationQtyBeforeTranfer = fromLocQtyBeforeTransfer,
                        ProductID = productID,
                        ToLocationAfterTranfer   = (ToLocQtyBeforeTransfer + currentqty),
                        ToLocationBeforeTransfer = ToLocQtyBeforeTransfer,
                        ToLocationID             = toLoc.ToInt(),
                        TransferDate             = DateTime.Now,
                        TransferQty = currentqty,
                        UserID      = 0
                    });
                }

                Helper.ShowMessage("Stock tranfer was completed succesfully ", "Tranfer Successful", MessageBoxButtons.OK, MessageBoxIcon.Information);
                UpdateRow = false;
                dgvProductList.Rows.Clear();
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "An error occurred", "ucTransferStock", "btnTransfer_Click");
                Helper.ShowMessage("An error occured " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }