Exemplo n.º 1
0
        /// <summary>
        ///     Edits a receipt 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 (currentReceiptReferenceInput.Text.Equals("") || currentAccountIdInput.Text.Equals("") || currentWarehouseIdInput.Text.Equals("") ||
                    newReceiptReferenceInput.Text.Equals("") || newAccountIdInput.Text.Equals("") || newWarehouseIdInput.Text.Equals(""))

                {
                    MessageBox.Show("Please input all text boxes.");
                    currentReceiptReferenceInput.Focus();
                    return;
                }
                else if ((!Regex.IsMatch(currentAccountIdInput.Text, "^[0-9]*$")) || (!Regex.IsMatch(currentWarehouseIdInput.Text, "^[0-9]*$")) ||
                         (!Regex.IsMatch(newAccountIdInput.Text, "^[0-9]*$")) || (!Regex.IsMatch(newWarehouseIdInput.Text, "^[0-9]*$")))
                {
                    MessageBox.Show("Please input only numerical characters into the Account ID and Warehouse ID text boxes.");
                    currentAccountIdInput.Focus();
                    return;
                }
                // https://stackoverflow.com/questions/31817105/regex-for-date-pattern/31817136
                else if ((!DateTime.TryParseExact(currentExpectedDateInput.Text, dateTimeFormat, null, DateTimeStyles.None, out CurrentTime)) ||
                         (!DateTime.TryParseExact(newExpectedDateInput.Text, dateTimeFormat, null, DateTimeStyles.None, out CurrentTime)))
                {
                    MessageBox.Show("Please input valid dates and times using the datetime pickers.");
                    currentExpectedDateInput.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());
            }

            ReceiptObject receipt;
            ReceiptObject receiptAllCurrent = businessLogicLayer.CheckReceiptsAll(currentReceiptReferenceInput.Text.ToLower(), int.Parse(currentAccountIdInput.Text),
                                                                                  int.Parse(currentWarehouseIdInput.Text), DateTime.Parse(currentExpectedDateInput.Text));
            ReceiptObject   receiptCurrent = businessLogicLayer.CheckReceiptsByRefAndAccountID(currentReceiptReferenceInput.Text.ToLower(), int.Parse(currentAccountIdInput.Text));
            ReceiptObject   receiptNew     = businessLogicLayer.CheckReceiptsByRefAndAccountID(newReceiptReferenceInput.Text.ToLower(), int.Parse(newAccountIdInput.Text));
            AccountObject   account        = businessLogicLayer.CheckAccountsByID(int.Parse(newAccountIdInput.Text));
            WarehouseObject warehouse      = businessLogicLayer.CheckWarehousesByID(int.Parse(newWarehouseIdInput.Text));

            try
            {
                if (!(currentReceiptReferenceInput.Text.ToLower().Equals(receiptAllCurrent.receipt_reference) &&
                      int.Parse(currentAccountIdInput.Text).Equals(receiptAllCurrent.account_id) &&
                      int.Parse(currentWarehouseIdInput.Text).Equals(receiptAllCurrent.warehouse_id) &&
                      DateTime.Parse(currentExpectedDateInput.Text).Equals(receiptAllCurrent.expected_date)))
                {
                    MessageBox.Show("The Current Receipt provided does not exist.");
                    currentReceiptReferenceInput.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 (newReceiptReferenceInput.Text.ToLower().Equals(receiptNew.receipt_reference) &&
                         !newReceiptReferenceInput.Text.ToLower().Equals(receiptCurrent.receipt_reference) ||
                         int.Parse(newAccountIdInput.Text).Equals(receiptNew.account_id) &&
                         !int.Parse(newAccountIdInput.Text).Equals(receiptCurrent.account_id))
                {
                    MessageBox.Show("The Receipt Reference: " + newReceiptReferenceInput.Text + " already exists for Account: " + newAccountIdInput.Text);
                    newReceiptReferenceInput.Focus();
                    return;
                }
                else if (receiptedDateInput.Value.HasValue.Equals(false))
                {
                    receipt = businessLogicLayer.EditCurrentReceiptNoRecDate(newReceiptReferenceInput.Text.ToLower(), int.Parse(newAccountIdInput.Text),
                                                                             int.Parse(newWarehouseIdInput.Text), DateTime.Parse(newExpectedDateInput.Text),
                                                                             currentReceiptReferenceInput.Text.ToLower(), int.Parse(currentAccountIdInput.Text),
                                                                             int.Parse(currentWarehouseIdInput.Text), DateTime.Parse(currentExpectedDateInput.Text));
                    MessageBox.Show(currentReceiptReferenceInput.Text + " has been updated without a Receipted Date.");
                    return;
                }
                else
                {
                    receipt = businessLogicLayer.EditCurrentReceipt(newReceiptReferenceInput.Text.ToLower(), int.Parse(newAccountIdInput.Text), int.Parse(newWarehouseIdInput.Text),
                                                                    DateTime.Parse(newExpectedDateInput.Text), DateTime.Parse(receiptedDateInput.Text),
                                                                    currentReceiptReferenceInput.Text.ToLower(), int.Parse(currentAccountIdInput.Text),
                                                                    int.Parse(currentWarehouseIdInput.Text), DateTime.Parse(currentExpectedDateInput.Text));
                    MessageBox.Show(currentReceiptReferenceInput.Text + " has been updated with a Receipt Date.");
                    return;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("An error has occurred, please contact your administrator." + "\n\n" + "The error message is: " + "\n\n" + ex.ToString());
            }
        }
        /// <summary>
        ///     Deletes a receipt from the database.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DeleteButton_Click(object sender, RoutedEventArgs e)
        {
            string   dateTimeFormat = "dd-MM-yyyy HH:mm";
            DateTime CurrentTime    = DateTime.Now;

            try
            {
                if (receiptReferenceInput.Text.Equals("") || accountIdInput.Text.Equals("") || warehouseIdInput.Text.Equals(""))
                {
                    MessageBox.Show("Please input all text boxes.");
                    receiptReferenceInput.Focus();
                    return;
                }
                else if ((!Regex.IsMatch(accountIdInput.Text, "^[0-9]*$")) || (!Regex.IsMatch(warehouseIdInput.Text, "^[0-9]*$")))
                {
                    MessageBox.Show("Please input only numerical characters into the Account ID and Warehouse ID text boxes.");
                    accountIdInput.Focus();
                    return;
                }
                // https://stackoverflow.com/questions/31817105/regex-for-date-pattern/31817136
                else if (!DateTime.TryParseExact(expectedDateInput.Text, dateTimeFormat, null, DateTimeStyles.None, out CurrentTime))
                {
                    MessageBox.Show("Please input a valid date and time using the datetime picker.");
                    expectedDateInput.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());
            }

            ReceiptObject receipt = businessLogicLayer.CheckReceiptsAll(receiptReferenceInput.Text.ToLower(), int.Parse(accountIdInput.Text), int.Parse(warehouseIdInput.Text),
                                                                        DateTime.Parse(expectedDateInput.Text));
            AccountObject   account   = businessLogicLayer.CheckAccountsByID(int.Parse(accountIdInput.Text));
            WarehouseObject warehouse = businessLogicLayer.CheckWarehousesByID(int.Parse(warehouseIdInput.Text));

            try
            {
                if (!int.Parse(accountIdInput.Text).Equals(account.account_id))
                {
                    MessageBox.Show("The Account ID provided does not exist.");
                    accountIdInput.Focus();
                    return;
                }
                else if (!int.Parse(warehouseIdInput.Text).Equals(warehouse.warehouse_id))
                {
                    MessageBox.Show("The Warehouse ID provided does not exist.");
                    warehouseIdInput.Focus();
                    return;
                }
                else if (!(receiptReferenceInput.Text.ToLower().Equals(receipt.receipt_reference) && int.Parse(accountIdInput.Text).Equals(receipt.account_id) &&
                           int.Parse(warehouseIdInput.Text).Equals(receipt.warehouse_id) && DateTime.Parse(expectedDateInput.Text).Equals(receipt.expected_date)))
                {
                    MessageBox.Show("The Receipt provided does not exist.");
                    receiptReferenceInput.Focus();
                    return;
                }
                else
                {
                    receipt = businessLogicLayer.DeleteCurrentReceipt(receiptReferenceInput.Text.ToLower(), int.Parse(accountIdInput.Text), int.Parse(warehouseIdInput.Text),
                                                                      DateTime.Parse(expectedDateInput.Text));
                    MessageBox.Show(receiptReferenceInput.Text + " has been deleted from the system.");
                    return;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("An error has occurred, please contact your administrator." + "\n\n" + "The error message is: " + "\n\n" + ex.ToString());
            }
        }