/// <summary> /// Open Search Window Logic /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Search_Click(object sender, RoutedEventArgs e) { try { //Reset UI ResetInvoiceData(); grpboxInvoice.IsEnabled = false; btnPanel.IsEnabled = true; Hide(); //Hide this window searchWindow.ShowDialog(); //display search window Show(); //show this window //Get Select Invoice from searchWindow and Populate the data in the current window's form currentInvoice = clsDBQueries.CurrentInvoice; //Update UI lblInvoiceNum.Content = (currentInvoice != null) ? $"{currentInvoice.InvoiceNumber}" : "None Selected"; ResetInvoiceData(); } catch (Exception ex) { HandleError(MethodInfo.GetCurrentMethod().DeclaringType.Name, MethodInfo.GetCurrentMethod().Name, ex.Message); } }
/// <summary> /// Add a new invoice to the database /// </summary> /// <param name="newInvoice">Invoice Object to add</param> /// <returns>New Invoice's Number</returns> public string AddInvoice(clsInvoice newInvoice) { try { string sSQL = sql.AddInvoice(newInvoice.InvoiceDate, newInvoice.InvoiceTotal); //Insert new invoice into Invoices table int iNumReturned = db.ExecuteNonQuery(sSQL); //Get new invoice number string newInvoiceNumber = GetNewestInvoiceNumber(); //Insert Each Line Item for (int i = 0; i < newInvoice.Items.Count; ++i) { sSQL = sql.AddLineItem(newInvoiceNumber, $"{i + 1}", newInvoice.Items[i].Code); iNumReturned = db.ExecuteNonQuery(sSQL); } return(newInvoiceNumber); } catch (Exception ex) { throw new Exception(MethodInfo.GetCurrentMethod().DeclaringType.Name + "." + MethodInfo.GetCurrentMethod().Name + " -> " + ex.Message); } }
/// <summary> /// Main Window Constructor /// </summary> public MainWindow() { try { InitializeComponent(); Application.Current.ShutdownMode = ShutdownMode.OnMainWindowClose; //shut down application only when main window is closed //Initialize windows searchWindow = new wndSearch(); editWindow = new wndEdit(); //Initialize Main Window objects and attributes queries = new clsDBQueries(); currentInvoice = new clsInvoice(); selectedItems = new List <clsItem>(); currentTotal = 0; currentInvoice = null; //Bind ComboBox to Items in database and datagrid to selectedItems cmbBoxItems.ItemsSource = queries.GetItems(); dtgrdInvoiceItems.ItemsSource = selectedItems; } catch (Exception ex) { HandleError(MethodInfo.GetCurrentMethod().DeclaringType.Name, MethodInfo.GetCurrentMethod().Name, ex.Message); } }
/// <summary> /// Updates an invoice currently in the database /// </summary> /// <param name="invoice">Invoice object with all changes</param> public void UpdateInvoice(clsInvoice invoice) { try { string sSQL = sql.UpdateInvoice(invoice.InvoiceNumber, invoice.InvoiceDate, invoice.InvoiceTotal); //Update Invoice Table int iNumReturned = db.ExecuteNonQuery(sSQL); //Delete All Current Line Items to replace them later sSQL = sql.DeleteInvoiceLineItems(invoice.InvoiceNumber); iNumReturned = db.ExecuteNonQuery(sSQL); //Insert Each Line Item as new for (int i = 0; i < invoice.Items.Count; ++i) { sSQL = sql.AddLineItem(invoice.InvoiceNumber, $"{i + 1}", invoice.Items[i].Code); iNumReturned = db.ExecuteNonQuery(sSQL); } } catch (Exception ex) { throw new Exception(MethodInfo.GetCurrentMethod().DeclaringType.Name + "." + MethodInfo.GetCurrentMethod().Name + " -> " + ex.Message); } }
/// <summary> /// Handles Save Changes event /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void SaveChanges_Click(object sender, RoutedEventArgs e) { try { //Check for valid inputs if (dpInvoiceDate.SelectedDate == null || selectedItems.Count < 1) { MessageBox.Show("Invalid Date or No Line Items Selected", "Save Invoice Error", MessageBoxButton.OK, MessageBoxImage.Error); } else { //If adding a new invoice if (currentInvoice == null) { //Add invoice to database and get New Invoice Number currentInvoice = new clsInvoice("New", dpInvoiceDate.SelectedDate.Value.Date, selectedItems, currentTotal); currentInvoice.InvoiceNumber = queries.AddInvoice(currentInvoice); //Update UI lblInvoiceNum.Content = currentInvoice.InvoiceNumber; btnDeleteInvoice.IsEnabled = true; btnEditInvoice.IsEnabled = true; } //If updating an existing invoice else { //Update currentInvoice Information currentInvoice.InvoiceTotal = currentTotal; currentInvoice.InvoiceDate = dpInvoiceDate.SelectedDate.Value.Date; currentInvoice.Items.Clear(); foreach (var item in selectedItems) { currentInvoice.Items.Add(item); } //Update Invoice in database queries.UpdateInvoice(currentInvoice); } //disable UI cmbBoxItems.SelectedIndex = -1; grpboxInvoice.IsEnabled = false; btnPanel.IsEnabled = true; } } catch (Exception ex) { HandleError(MethodInfo.GetCurrentMethod().DeclaringType.Name, MethodInfo.GetCurrentMethod().Name, ex.Message); } }
/// <summary> /// Handles New Invoice Click Event /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void NewInvoice_Click(object sender, RoutedEventArgs e) { try { //Enable UI btnPanel.IsEnabled = false; grpboxInvoice.IsEnabled = true; //Clear current invoice for edits currentInvoice = null; ResetInvoiceData(); lblInvoiceNum.Content = "TBD"; } catch (Exception ex) { HandleError(MethodInfo.GetCurrentMethod().DeclaringType.Name, MethodInfo.GetCurrentMethod().Name, ex.Message); } }
/// <summary> /// retrive invoice and set CurrentInvoice to retrieved invoice /// </summary> /// <param name="sInvoiceNum">Invoice Number</param> public void GetInvoiceByNum(string sInvoiceNum) { try { int iNumReturned = 0; DataSet ds = db.ExecuteSQLStatement(sql.GetInvoice(sInvoiceNum), ref iNumReturned); clsInvoice invoice = new clsInvoice(); invoice.InvoiceNumber = ds.Tables[0].Rows[0]["InvoiceNum"].ToString(); invoice.InvoiceDate = Convert.ToDateTime(ds.Tables[0].Rows[0]["InvoiceDate"].ToString()); invoice.InvoiceTotal = Convert.ToDecimal(ds.Tables[0].Rows[0]["TotalCharge"]); invoice.Items = GetInvoiceContents(sInvoiceNum); currentInvoice = invoice; } catch (Exception ex) { throw new Exception(MethodInfo.GetCurrentMethod().DeclaringType.Name + "." + MethodInfo.GetCurrentMethod().Name + " -> " + ex.Message); } }
/// <summary> /// Handles Delete Invoice Click Event /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void DeleteInvoice_Click(object sender, RoutedEventArgs e) { try { //Check if an invoice is selected if (currentInvoice == null) { MessageBox.Show("No Invoice is currently selected", "Delete Invoice Error", MessageBoxButton.OK, MessageBoxImage.Error); } else { //Delete Invoice queries.DeleteInvoice(currentInvoice.InvoiceNumber); //Reset UI currentInvoice = null; ResetInvoiceData(); } } catch (Exception ex) { HandleError(MethodInfo.GetCurrentMethod().DeclaringType.Name, MethodInfo.GetCurrentMethod().Name, ex.Message); } }