Ejemplo n.º 1
0
 /// <summary>
 /// Handles the Save Invoice Button being clicked. Checks if items exists in the datagrid.
 /// If no items exist the Invoice will not be saved.
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 private void saveInvoiceButton_Click(object sender, RoutedEventArgs e)
 {
     try
     {
         if (invoiceNumberTextBox.Text == "TBD") //if invoiceNumberTextBox is TBD then the invoice is a new invoice.
         {                                       //and an INSERT INTO statement is required.
             if (dataGridList.Count == 0)
             {
                 noItemsAddedLabel.Visibility = (Visibility)0;
             }
             else
             {
                 mainLogic.AddInvoice(datePicker.SelectedDate.ToString(), totalCost.ToString(), dataGridList); //add invoice to the DB
                 addItemsCanvas.IsEnabled     = false;
                 invoiceNumberTextBox.Text    = "";
                 invoiceSavedLabel.Visibility = (Visibility)0;
                 itemsComboBox.Text           = "";
                 invoiceComboBox.Text         = "";
                 datePicker.Text = "Please select a date";
             }
         }
         else //if invoicenumbertextbox is not TBD then the invoice is one already in the database and
         {    // an UPDATE statement will be required.
             var selectedInvoice = (clsInvoices)invoiceComboBox.SelectedItem;
             mainLogic.UpdateInvoice(selectedInvoice.InvoiceNum, totalCost.ToString(), dataGridList);
             invoiceLookUpCanvas.IsEnabled = true;
             invoiceSavedLabel.Visibility  = (Visibility)0;
             itemsComboBox.Text            = "";
             invoiceComboBox.Text          = "";
             datePicker.Text = "Please select a date";
         }
         dataGridList.Clear();
         invoiceDataGrid.ItemsSource = dataGridList;
         Cost_TextBox.Text           = "";
         Total_TextBox.Text          = "";
     }
     catch (Exception ex)
     {               //this is reflection
         HandleError(MethodInfo.GetCurrentMethod().DeclaringType.Name,
                     MethodInfo.GetCurrentMethod().Name, ex.Message);
     }
 }
Ejemplo n.º 2
0
        /// <summary>
        /// Handle when the save button is clicked. Saves the invoice if valid
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSaveInvoice_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (string.IsNullOrWhiteSpace(invoice.sInvoiceDate))
                {
                    MessageBox.Show("An invoice must have a date before it can be saved.");
                    return;
                }
                else if (string.IsNullOrWhiteSpace(invoice.sTotalCost) || Convert.ToInt32(invoice.sTotalCost) == 0)
                {
                    MessageBox.Show("An invoice must have items before it can be saved.");
                    return;
                }
                bool invoiceSaved;
                if (isNewInvoice)
                {
                    invoiceSaved = sqlProvider.InsertInvoice(invoice);
                    //get the max invoice that was recently saved
                    invoice.sInvoiceNum = sqlProvider.GetMaxInvoiceID();
                    if (string.IsNullOrWhiteSpace(invoice.sInvoiceNum))
                    {
                        throw new Exception("Failed to retrieve the invoice number after saving");
                    }
                    lblInvoiceNum.Content = $"Invoice #{invoice.sInvoiceNum}";
                }
                else
                {
                    invoiceSaved = sqlProvider.UpdateInvoice(invoice);
                }
                if (!invoiceSaved)
                {
                    MessageBox.Show("An error occurred while saving the invoice, changes have not been saved.");
                    return;
                }

                //assume this is the case for instances like new invoices
                bool isOkToSaveItems = true;
                if (!isNewInvoice)
                {
                    isOkToSaveItems = sqlProvider.DeleteInvoiceLineItems(invoice);
                }
                if (!isOkToSaveItems)
                {
                    MessageBox.Show("An error occurred while attempting to reset the line items in the database. Invoice saved, items not.");
                    return;
                }
                bool itemsSaved = sqlProvider.SaveAllLineItems(invoice.sInvoiceNum, lineItems);
                if (itemsSaved)
                {
                    MessageBox.Show("Invoice saved successfully!");
                    isNewInvoice               = false;
                    btnEditInvoice.IsEnabled   = true;
                    btnDeleteInvoice.IsEnabled = true;
                    LockInvoice();
                }
                else
                {
                    MessageBox.Show("An error occurred while attempting to save the line items in the database. Invoice saved, items not. Previous items still saved.");
                }
            }
            catch (Exception ex)
            {
                //This is the top level method so we want to handle the exception
                HandleError(MethodInfo.GetCurrentMethod().DeclaringType.Name,
                            MethodInfo.GetCurrentMethod().Name, ex.Message);
            }
        }