/// <summary> /// Edits an order in the database. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void EditButton_Click(object sender, RoutedEventArgs e) { string dateTimeFormat = "dd-MM-yyyy HH:mm"; DateTime CurrentTime = DateTime.Now; try { if (currentOrderIDInput.Text.Equals("") || currentOrderReferenceInput.Text.Equals("") || currentAccountIDInput.Text.Equals("") || newOrderReferenceInput.Text.Equals("") || newAccountIdInput.Text.Equals("") || newWarehouseIdInput.Text.Equals("") || newStatusInput.Text.Equals("") || newFirstNameInput.Text.Equals("") || newLastNameInput.Text.Equals("") || newAddressLine1Input.Text.Equals("") || newAddressLine2Input.Equals("") || newCityInput.Text.Equals("") || newPostcodeInput.Text.Equals("")) { MessageBox.Show("Please input all text boxes."); currentOrderIDInput.Focus(); return; } else if ((!Regex.IsMatch(currentOrderIDInput.Text, "^[0-9]*$")) || (!Regex.IsMatch(currentAccountIDInput.Text, "^[0-9]*$")) || (!Regex.IsMatch(newAccountIdInput.Text, "^[0-9]*$")) || (!Regex.IsMatch(newWarehouseIdInput.Text, "^[0-9]*$"))) { MessageBox.Show("Please input only numerical characters into the Current Order ID, Current Account ID, New Account ID and New Warehouse ID text boxes."); currentOrderIDInput.Focus(); return; } else if (!(newStatusInput.Text.ToLower().Equals("created") || newStatusInput.Text.ToLower().Equals("allocated") || newStatusInput.Text.ToLower().Equals("picked") || newStatusInput.Text.ToLower().Equals("dispatched"))) { MessageBox.Show("Please input only 'created', 'allocated', 'picked' or 'dispatched' into the New Status text box."); newStatusInput.Focus(); return; } // https://stackoverflow.com/questions/31817105/regex-for-date-pattern/31817136 else if (!DateTime.TryParseExact(newDispatchDateInput.Text, dateTimeFormat, null, DateTimeStyles.None, out CurrentTime)) { MessageBox.Show("Please input a valid date and time using the datetime picker."); newDispatchDateInput.Focus(); return; } else if ((!Regex.IsMatch(newFirstNameInput.Text, @"^[a-zA-Z]+$")) || (!Regex.IsMatch(newLastNameInput.Text, @"^[a-zA-Z]+$")) || (!Regex.IsMatch(newCityInput.Text, @"^[a-zA-Z]+$"))) { MessageBox.Show("Please input only alphabetical characters for the New First Name, New Last Name and New City text boxes"); newFirstNameInput.Focus(); return; } // https://en.wikipedia.org/wiki/Postcodes_in_the_United_Kingdom#Validation else if (!Regex.IsMatch(newPostcodeInput.Text, "^(([A-Z]{1,2}[0-9][A-Z0-9]?|ASCN|STHL|TDCU|BBND|[BFS]IQQ|PCRN|TKCA) ?[0-9][A-Z]{2}|BFPO ?[0-9]{1,4}|(KY[0-9]|MSR|VG|AI)[ -]?[0-9]{4}|[A-Z]{2} ?[0-9]{2}|GE ?CX|GIR ?0A{2}|SAN ?TA1)$")) { MessageBox.Show("Please input a valid UK Postcode (must be in capitals)."); newPostcodeInput.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()); } OrderObject order; OrderObject orderCurrentByID = businessLogicLayer.CheckOrdersByID(int.Parse(currentOrderIDInput.Text)); OrderObject orderCurrentByIDAndRefAndAccID = businessLogicLayer.CheckOrdersByIDAndRefAndAccID(int.Parse(currentOrderIDInput.Text), currentOrderReferenceInput.Text.ToLower(), int.Parse(currentAccountIDInput.Text)); OrderObject orderCurrent = businessLogicLayer.CheckOrdersByRefAndAccountID(currentOrderReferenceInput.Text.ToLower(), int.Parse(currentAccountIDInput.Text)); OrderObject orderNew = businessLogicLayer.CheckOrdersByRefAndAccountID(newOrderReferenceInput.Text.ToLower(), int.Parse(newAccountIdInput.Text)); AccountObject account = businessLogicLayer.CheckAccountsByID(int.Parse(newAccountIdInput.Text)); WarehouseObject warehouse = businessLogicLayer.CheckWarehousesByID(int.Parse(newWarehouseIdInput.Text)); try { if (!int.Parse(currentOrderIDInput.Text).Equals(orderCurrentByID.order_id)) { MessageBox.Show("The Current Order ID provided does not exist."); currentOrderIDInput.Focus(); return; } else if (!(int.Parse(currentOrderIDInput.Text).Equals(orderCurrentByIDAndRefAndAccID.order_id) && currentOrderReferenceInput.Text.Equals(orderCurrentByIDAndRefAndAccID.order_reference) && int.Parse(currentAccountIDInput.Text).Equals(orderCurrentByIDAndRefAndAccID.account_id))) { MessageBox.Show("The Current Order provided does not exist."); currentOrderIDInput.Focus(); return; } else if (!int.Parse(newAccountIdInput.Text).Equals(account.account_id)) { MessageBox.Show("The New Account ID provided does not exist."); newAccountIdInput.Focus(); return; } else if (!int.Parse(newWarehouseIdInput.Text).Equals(warehouse.warehouse_id)) { MessageBox.Show("The new Warehouse ID provided does not exist."); newWarehouseIdInput.Focus(); return; } else if (newOrderReferenceInput.Text.ToLower().Equals(orderNew.order_reference) && !newOrderReferenceInput.Text.ToLower().Equals(orderCurrent.order_reference) || int.Parse(newAccountIdInput.Text).Equals(orderNew.account_id) && !int.Parse(newAccountIdInput.Text).Equals(orderCurrent.account_id)) { MessageBox.Show("The Order Reference: " + newOrderReferenceInput.Text + " already exists for Account: " + newAccountIdInput.Text); newOrderReferenceInput.Focus(); return; } else { order = businessLogicLayer.EditCurrentOrder(newOrderReferenceInput.Text.ToLower(), int.Parse(newAccountIdInput.Text), int.Parse(newWarehouseIdInput.Text), newStatusInput.Text.ToLower(), DateTime.Parse(newDispatchDateInput.Text), newFirstNameInput.Text.ToLower(), newLastNameInput.Text.ToLower(), newAddressLine1Input.Text.ToLower(), newAddressLine2Input.Text.ToLower(), newCityInput.Text.ToLower(), newPostcodeInput.Text.ToLower(), int.Parse(currentOrderIDInput.Text)); MessageBox.Show("The provided order has been updated."); return; } } catch (Exception ex) { MessageBox.Show("An error has occurred, please contact your administrator." + "\n\n" + "The error message is: " + "\n\n" + ex.ToString()); } }