Esempio n. 1
0
        /// <summary>
        ///     Deletes a Stock from the database.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DeleteButton_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (stockIdInput.Text.Equals("") || locationIdInput.Text.Equals(""))
                {
                    MessageBox.Show("Please input the text boxes.");
                    stockIdInput.Focus();
                    return;
                }
                else if ((!Regex.IsMatch(stockIdInput.Text, "^[0-9]*$")) || (!Regex.IsMatch(locationIdInput.Text, "^[0-9]*$")))
                {
                    MessageBox.Show("Please input only numerical characters into the text boxes.");
                    stockIdInput.Focus();
                    return;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("An error has occurred, please contact your administrator." + "\n\n" + "The error message is: " + "\n\n" + ex.ToString());
            }

            StockObject    stock;
            StockObject    stockId = businessLogicLayer.CheckStockByID(int.Parse(stockIdInput.Text));
            StockObject    stockIdAndLocationId   = businessLogicLayer.CheckStockByIDAndLocation(int.Parse(stockIdInput.Text), int.Parse(locationIdInput.Text));
            StockObject    stockAllocated         = businessLogicLayer.CheckStockIsAllocated(int.Parse(stockIdInput.Text));
            int            stockAllocatedQuantity = stockAllocated.allocated_quantity;
            LocationObject locationId             = businessLogicLayer.CheckLocationsByID(int.Parse(locationIdInput.Text));
            LocationObject location;

            try
            {
                if (!int.Parse(stockIdInput.Text).Equals(stockId.stock_id))
                {
                    MessageBox.Show("The Stock ID provided does not exist.");
                    stockIdInput.Focus();
                    return;
                }
                else if (!int.Parse(locationIdInput.Text).Equals(locationId.location_id))
                {
                    MessageBox.Show("The Location ID provided does not exist.");
                    stockIdInput.Focus();
                    return;
                }
                else if (!int.Parse(stockIdInput.Text).Equals(stockIdAndLocationId.stock_id) && !int.Parse(locationIdInput.Text).Equals(stockIdAndLocationId.location_id))
                {
                    MessageBox.Show("The Stock ID and Location ID provided do not match.");
                    stockIdInput.Focus();
                    return;
                }
                else if (stockAllocatedQuantity > 0)
                {
                    MessageBox.Show("The Stock provided is currently allocated towards an order and so cannot be deleted. \n\nPlease contact an administrator if the stock still needs to be deleted.");
                    stockIdInput.Focus();
                    return;
                }
                else
                {
                    stock    = businessLogicLayer.DeleteCurrentStock(int.Parse(stockIdInput.Text));
                    location = businessLogicLayer.MarkLocationUnallocated(int.Parse(locationIdInput.Text));
                    MessageBox.Show("The provided stock has been deleted from the system. \n\nThe location it occupied has now been unallocated.");
                    return;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("An error has occurred, please contact your administrator." + "\n\n" + "The error message is: " + "\n\n" + ex.ToString());
            }
        }