private void button3_Click(object sender, EventArgs e) { //guardar //we make sure there is at least one item in the cart and a sales person has been selected if (ShoppingCart.Count > 0 && comboBox1.SelectedIndex > -1){ //auto dispose after no longer in scope using(bdPrincipal db = new bdPrincipal()){ //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 //comboBox1.SelectedIndex = 0; inv.id = db.Registro.SingleOrDefault(s => s.id == (int)comboBox1.SelectedValue); //for each product in the shopping cart we query the database foreach (var prod in ShoppingCart) { //get product record with id MiBD.Productos p = db.producto.SingleOrDefault(i => i.idPro == prod.idPro); //reduce inventory int RemainingItems = p.Qty - prod.Qty >= 0 ? (p.Qty - prod.Qty) : p.Qty; if (p.Qty == RemainingItems) { System.Windows.MessageBox.Show( string.Format( "Unable to sell Product #{0} not enough inventory, Do want to continue?", p.idPro), "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 System.Windows.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 System.Windows.MessageBox.Show("Transaction Error, unable to generate invoice", "Fatal Error", MessageBoxButton.OK, MessageBoxImage.Error); } } } } else { System.Windows.MessageBox.Show("Please select at least one product and a Sales Person", "Data Error", MessageBoxButton.OK, MessageBoxImage.Stop); } }
private void button3_Click(object sender, EventArgs e) { //guardar //we make sure there is at least one item in the cart and a sales person has been selected if (ShoppingCart.Count > 0 && comboBox1.SelectedIndex > -1) { //auto dispose after no longer in scope using (bdPrincipal db = new bdPrincipal()){ //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 //comboBox1.SelectedIndex = 0; inv.id = db.Registro.SingleOrDefault(s => s.id == (int)comboBox1.SelectedValue); //for each product in the shopping cart we query the database foreach (var prod in ShoppingCart) { //get product record with id MiBD.Productos p = db.producto.SingleOrDefault(i => i.idPro == prod.idPro); //reduce inventory int RemainingItems = p.Qty - prod.Qty >= 0 ? (p.Qty - prod.Qty) : p.Qty; if (p.Qty == RemainingItems) { System.Windows.MessageBox.Show( string.Format( "Unable to sell Product #{0} not enough inventory, Do want to continue?", p.idPro), "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 System.Windows.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 System.Windows.MessageBox.Show("Transaction Error, unable to generate invoice", "Fatal Error", MessageBoxButton.OK, MessageBoxImage.Error); } } } } else { System.Windows.MessageBox.Show("Please select at least one product and a Sales Person", "Data Error", MessageBoxButton.OK, MessageBoxImage.Stop); } }