private void Button_Click_2(object sender, RoutedEventArgs e) { //we make sure there is at least one item in the cart and a sales person has been selected if (ShoppingCart.Count > 0 && cbSalesPersonel.SelectedIndex > -1) { //auto dispose after no longer in scope using (DBInvoiceSample db = new DBInvoiceSample()) { //All database transactions are considered 1 unit of work using (var dbTransaction = db.Database.BeginTransaction()) { try { //we create the invoice object Invoice inv = new Invoice(); inv.SaleDate = DateTime.Now; //assign sales person by querying the database using the Combobox selection inv.SalesPerson = db.SalesPersons.SingleOrDefault(s => s.Id == (int) cbSalesPersonel.SelectedValue); //for each product in the shopping cart we query the database foreach (var prod in ShoppingCart) { //get product record with id Product p = db.Products.SingleOrDefault(i => i.Id == prod.Id); //reduce inventory int RemainingItems = p.Qty - prod.Qty >= 0 ? (p.Qty - prod.Qty) : p.Qty; if (p.Qty == RemainingItems) { MessageBox.Show( string.Format( "Unable to sell Product #{0} not enough inventory, Do want to continue?", p.Id), "Not Enough Inventory", MessageBoxButton.OK, MessageBoxImage.Asterisk); //end transaction dbTransaction.Rollback(); //exit procedure return; } else { //If Qty is ok we sell the product p.Qty = RemainingItems; inv.SaleList.Add(p); } } //we add the generated invoice to the Invoice Entity (Table) db.Invoices.Add(inv); //Save Changed to the database db.SaveChanges(); //Make the changes permanent dbTransaction.Commit(); //We restore the form with defaults CleanUp(); //Show confirmation message to the user MessageBox.Show(string.Format("Transaction #{0} Saved", inv.InvoiceId), "Success", MessageBoxButton.OK, MessageBoxImage.Information); } catch { //if an error is produced, we rollback everything dbTransaction.Rollback(); //We notify the user of the error MessageBox.Show("Transaction Error, unable to generate invoice", "Fatal Error", MessageBoxButton.OK, MessageBoxImage.Error); } } } } else { MessageBox.Show("Please select at least one product and a Sales Person", "Data Error", MessageBoxButton.OK, MessageBoxImage.Stop); } }
private void Window_Loaded(object sender, RoutedEventArgs e) { //Populates the combobox when the window loads DBInvoiceSample db = new DBInvoiceSample(); cbSalesPersonel.ItemsSource = db.SalesPersons.ToList(); cbSalesPersonel.DisplayMemberPath = "Name"; cbSalesPersonel.SelectedValuePath = "Id"; cbSalesPersonel.SelectedIndex = 0; }
private void Button_Click_1(object sender, RoutedEventArgs e) { //If a product code is not empty we search the database if (Regex.IsMatch(TxtProdCode.Text.Trim(), @"^\d+$")) { DBInvoiceSample db = new DBInvoiceSample(); //parse the product code as int from the TextBox int id = int.Parse(TxtProdCode.Text); //We query the database for the product Product p = db.Products.SingleOrDefault(x => x.Id == id); if (p != null) //if product was found { //store in a temp variable (if user clicks on add we will need this for the Array) tmpProduct = p; //We display the product information on a label cprod.Content = string.Format("ID: {0}, Name: {1}, Price: {2}, InStock (Qty): {3}", p.Id, p.Name, p.Price, p.Qty); } else { //if product was not found we display a user notification window MessageBox.Show("Product not found. (Only numbers allowed)", "Product code error", MessageBoxButton.OK, MessageBoxImage.Exclamation); } } }