Ejemplo n.º 1
0
        /// <summary>
        ///     Edits a stock in the database.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void EditButton_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (currentStockIdInput.Text.Equals("") || currentLocationIdInput.Text.Equals("") ||
                    newAccountIdInput.Text.Equals("") || newProductIdInput.Text.Equals("") || newWarehouseIdInput.Text.Equals("") || newLocationIdInput.Equals("") ||
                    newQuantityInput.Text.Equals("") || newAllocatedQuantityInput.Text.Equals("") || newAvailabilityStatusInput.Text.Equals(""))
                {
                    MessageBox.Show("Please input all text boxes.");
                    currentStockIdInput.Focus();
                    return;
                }
                else if ((!Regex.IsMatch(currentStockIdInput.Text, "^[0-9]*$")) || (!Regex.IsMatch(currentLocationIdInput.Text, "^[0-9]*$")) ||
                         (!Regex.IsMatch(newAccountIdInput.Text, "^[0-9]*$")) || (!Regex.IsMatch(newProductIdInput.Text, "^[0-9]*$")) ||
                         (!Regex.IsMatch(newWarehouseIdInput.Text, "^[0-9]*$")) || (!Regex.IsMatch(newLocationIdInput.Text, "^[0-9]*$")) ||
                         (!Regex.IsMatch(newQuantityInput.Text, "^[0-9]*$")) || (!Regex.IsMatch(newAllocatedQuantityInput.Text, "^[0-9]*$")))
                {
                    MessageBox.Show("Please input only numerical characters into all text boxes apart from the current and new 'Availability Status Input'.");
                    currentStockIdInput.Focus();
                    return;
                }
                else if (!(newAvailabilityStatusInput.Text.ToLower().Equals("true") || newAvailabilityStatusInput.Text.ToLower().Equals("false")))
                {
                    MessageBox.Show("Please input only 'True' or 'False' into the current and new Availability Status Input text boxes.");
                    newAvailabilityStatusInput.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 stockCurrentId = businessLogicLayer.CheckStockByID(int.Parse(currentStockIdInput.Text));
            StockObject stockCurrentIdAndWarehouseAndLocation = businessLogicLayer.CheckStockByIDAndWarehouseAndLocation(int.Parse(currentStockIdInput.Text),
                                                                                                                         int.Parse(currentWarehouseIdInput.Text),
                                                                                                                         int.Parse(currentLocationIdInput.Text));
            StockObject stockCurrentWarehouseAndLocation = businessLogicLayer.CheckStockByWarehouseAndLocation(int.Parse(currentWarehouseIdInput.Text),
                                                                                                               int.Parse(currentLocationIdInput.Text));
            StockObject stockNewWarehouseAndLocation = businessLogicLayer.CheckStockByWarehouseAndLocation(int.Parse(newWarehouseIdInput.Text),
                                                                                                           int.Parse(newLocationIdInput.Text));
            LocationObject locationNewLocationByWarehouse = businessLogicLayer.CheckLocationByIDAndWarehouse(int.Parse(newLocationIdInput.Text),
                                                                                                             int.Parse(newWarehouseIdInput.Text));
            AccountObject   accountId   = businessLogicLayer.CheckAccountsByID(int.Parse(newAccountIdInput.Text));
            ProductObject   productId   = businessLogicLayer.CheckProductsByID(int.Parse(newProductIdInput.Text));
            WarehouseObject warehouseId = businessLogicLayer.CheckWarehousesByID(int.Parse(newWarehouseIdInput.Text));
            LocationObject  locationId  = businessLogicLayer.CheckLocationsByID(int.Parse(newLocationIdInput.Text));
            LocationObject  locationUnallocated;
            LocationObject  locationAllocated;

            try
            {
                if (!int.Parse(currentStockIdInput.Text).Equals(stockCurrentId.stock_id))
                {
                    MessageBox.Show("The current Stock ID provided does not exist.");
                    currentStockIdInput.Focus();
                    return;
                }
                else if (!int.Parse(currentStockIdInput.Text).Equals(stockCurrentIdAndWarehouseAndLocation.stock_id) &&
                         !int.Parse(currentWarehouseIdInput.Text).Equals(stockCurrentIdAndWarehouseAndLocation.warehouse_id) &&
                         !int.Parse(currentLocationIdInput.Text).Equals(stockCurrentIdAndWarehouseAndLocation.location_id))
                {
                    MessageBox.Show("The current Stock provided does not exist in that Warehouse & Location.");
                    currentStockIdInput.Focus();
                    return;
                }
                else if (!int.Parse(newAccountIdInput.Text).Equals(accountId.account_id))
                {
                    MessageBox.Show("The new Account ID provided does not exist.");
                    newAccountIdInput.Focus();
                    return;
                }
                else if (!int.Parse(newProductIdInput.Text).Equals(productId.product_id))
                {
                    MessageBox.Show("The new product ID provided does not exist.");
                    newProductIdInput.Focus();
                    return;
                }
                else if (!int.Parse(newWarehouseIdInput.Text).Equals(warehouseId.warehouse_id))
                {
                    MessageBox.Show("The new Warehouse ID provided does not exist.");
                    newWarehouseIdInput.Focus();
                    return;
                }
                else if (!int.Parse(newLocationIdInput.Text).Equals(locationId.location_id))
                {
                    MessageBox.Show("The new Location ID provided does not exist.");
                    newLocationIdInput.Focus();
                    return;
                }
                else if (int.Parse(newQuantityInput.Text) < int.Parse(newAllocatedQuantityInput.Text))
                {
                    MessageBox.Show("The new Allocated Quantity cannot be greater than the new Quantity.");
                    newQuantityInput.Focus();
                    return;
                }
                else if (!int.Parse(newLocationIdInput.Text).Equals(locationNewLocationByWarehouse.location_id))
                {
                    MessageBox.Show("The new Location ID provided does not exist in that warehouse.");
                    currentStockIdInput.Focus();
                    return;
                }
                else if (int.Parse(newWarehouseIdInput.Text).Equals(stockNewWarehouseAndLocation.warehouse_id) &&
                         !int.Parse(newWarehouseIdInput.Text).Equals(stockCurrentWarehouseAndLocation.warehouse_id) ||
                         int.Parse(newLocationIdInput.Text).Equals(stockNewWarehouseAndLocation.location_id) &&
                         !int.Parse(newLocationIdInput.Text).Equals(stockCurrentWarehouseAndLocation.location_id))
                {
                    MessageBox.Show("Stock already exists in that Warehouse and Location.");
                    newWarehouseIdInput.Focus();
                    return;
                }
                else
                {
                    stock = businessLogicLayer.EditCurrentStock(int.Parse(newAccountIdInput.Text), int.Parse(newProductIdInput.Text), int.Parse(newWarehouseIdInput.Text),
                                                                int.Parse(newLocationIdInput.Text), int.Parse(newQuantityInput.Text), int.Parse(newAllocatedQuantityInput.Text),
                                                                bool.Parse(newAvailabilityStatusInput.Text.ToLower()),
                                                                int.Parse(currentStockIdInput.Text));
                    locationUnallocated = businessLogicLayer.MarkLocationUnallocated(int.Parse(currentLocationIdInput.Text));
                    locationAllocated   = businessLogicLayer.MarkLocationAllocated(int.Parse(newLocationIdInput.Text));
                    MessageBox.Show("The Stock provided has been updated. \n\nThe current Location has been marked unallocated. \n\nThe new Location has been marked allocated");
                    return;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("An error has occurred, please contact your administrator." + "\n\n" + "The error message is: " + "\n\n" + ex.ToString());
            }
        }