/// <summary> /// Creates a new invoice after choosing a date /// </summary> /// <param name="date"></param> /// <returns></returns> public int CreateInvoice(DateTime date) { try { _dataAccess.ExecuteNonQuery(string.Format(SQL_CREATE_INVOICE, date)); } catch (Exception queryException) { throw new InvoiceDataException("Unable to insert record into Invoices table.", queryException); } int invoiceNum; try { string invoice = _dataAccess.ExecuteScalarSQL(SQL_GET_LASTINVOICEKEY); if (!int.TryParse(invoice, out invoiceNum)) { throw new InvoiceDataException("Unable to interpret invoice number retrieved from table."); } } catch (Exception queryException) { throw new InvoiceDataException("Unable to retrieve last key from Invoices table.", queryException); } return(invoiceNum); }
/// <summary> /// This method executes the SQL statement that updates a particular item in the database /// </summary> /// <param name="sqlStatement"></param> /// <returns></returns> public int updateItem(String sqlStatement) { try { return(db.ExecuteNonQuery(sqlStatement)); } catch (Exception e) { throw new Exception(MethodInfo.GetCurrentMethod().DeclaringType.Name + "." + MethodInfo.GetCurrentMethod().Name + " -> " + e.Message); } }
}//end remove item /// <summary> /// if given invoice id Updates the date and total charge. delete everything in the LineItem table, and write the data again with the data in the datatable /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnAddUpdate_Click(object sender, RoutedEventArgs e) { try { ///if statement to check and compare dates if (invoiceDatePicker.SelectedDate == null) { invoiceDatePicker.SelectedDate = DateTime.Now.Date; } // if no date is picked, default to today int count = 0; //keeps track of records inserted ///if invoice id is null if (invoiceId != "") { //updates the date, even if there were no changes. String sSQL = mydb.updateInvoiceDate(invoiceDatePicker.SelectedDate.Value.ToShortDateString(), invoiceId); db.ExecuteNonQuery(sSQL); System.Console.WriteLine(sSQL); //updates the cost of the associated invoiceId sSQL = mydb.updateTotalCharge(calculateTotal() + "", invoiceId); db.ExecuteNonQuery(sSQL); System.Console.WriteLine(sSQL); //removing all LineItems associated with that invoice number, and adding them again with the added/removed items sSQL = mydb.DeleteLineItems(invoiceId); db.ExecuteNonQuery(sSQL); //grabs the data from the DataTable, runs a sql statement adding each line individually. for (int i = 0; i < dtInvoice.Rows.Count; i++) { sSQL = mydb.addLineItem(invoiceId, i + 1 + "", inventoryDictionary[dtInvoice.Rows[i][0] + ""]); System.Console.WriteLine(sSQL); db.ExecuteNonQuery(sSQL); count++; } MessageBox.Show("Invoice: " + invoiceId + " added successfully!\n" + count + " items added"); } else { String sSQL = mydb.addInvoice(invoiceDatePicker.SelectedDate.Value.ToShortDateString(), calculateTotal() + ""); db.ExecuteNonQuery(sSQL); sSQL = mydb.latestInvoice(); invoiceId = db.ExecuteScalarSQL(sSQL); for (int i = 0; i < dtInvoice.Rows.Count; i++) { sSQL = mydb.addLineItem(invoiceId, i + 1 + "", inventoryDictionary[dtInvoice.Rows[i][0] + ""]); System.Console.WriteLine(sSQL); db.ExecuteNonQuery(sSQL); count++;//keeps track of items added. } MessageBox.Show("Invoice: " + invoiceId + " added successfully!\n" + count + " items added"); }//end else } catch (Exception) { MessageBox.Show(MethodInfo.GetCurrentMethod().DeclaringType.Name); } }//end add/update click
/// <summary> /// Delete an item from the inventory. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnDeleteItem_Click(object sender, RoutedEventArgs e) { try { // Prevent user from deleting an item that is in the current invoice. // Display a warning message to the user. // Delete item from database using SQL. lblErrorCantDeleteItem.Visibility = Visibility.Hidden; sSQL = mydb.CheckIfItemIsInAnInvoice(itemCode); ds = db.ExecuteSQLStatement(sSQL, ref iRetVal); if(iRetVal == 0) { ///check to see what the message box is showing if (MessageBox.Show("Are you sure you want to delete item: " + txtItemDesc.Text + "?", "Delete item?", MessageBoxButton.YesNo, MessageBoxImage.Warning) == MessageBoxResult.No) { //do no stuff } else { sSQL = mydb.DeleteInventoryItem(itemCode); db.ExecuteNonQuery(sSQL); EditWindow ew = new EditWindow(); ew.Show(); this.Close(); } } else { lblErrorCantDeleteItem.Visibility = Visibility.Visible; } } catch (Exception) { MessageBox.Show(MethodInfo.GetCurrentMethod().DeclaringType.Name); } }