diff --git a/.vs/VSWorkspaceState.json b/.vs/VSWorkspaceState.json index 04825ae..fc52d31 100644 --- a/.vs/VSWorkspaceState.json +++ b/.vs/VSWorkspaceState.json @@ -2,6 +2,10 @@ "ExpandedNodes": [ "" ], +<<<<<<< HEAD "SelectedNode": "\\agregarmermas.cs", +======= + "SelectedNode": "\\vender.cs", +>>>>>>> 53dd047ba99d9fe2f8dc36472f5d1441d992e695 "PreviewInSolutionExplorer": false } \ No newline at end of file diff --git a/database/DAO.cs b/database/DAO.cs index 6c10cc6..d9fa8b1 100644 --- a/database/DAO.cs +++ b/database/DAO.cs @@ -1,4 +1,5 @@ using System; +using System.Collections; using System.Collections.Generic; using System.Data.SQLite; using System.Diagnostics.Contracts; @@ -534,9 +535,22 @@ namespace compabetov2.database foreach (var item in stockNuevo) { string sql = "UPDATE Producto SET stock = @stock WHERE idproducto = @idproducto;"; + string sqlProductoStock = "SELECT stock FROM Producto WHERE idproducto = @idproducto"; + + SQLiteCommand commandProdcutoStock = new SQLiteCommand(sqlProductoStock, conexion); + commandProdcutoStock.Parameters.AddWithValue("@idproducto", item.idProducto); + SQLiteDataReader stockReader = commandProdcutoStock.ExecuteReader(); + + int stockActual = -1; + while (stockReader.Read()) + { + stockActual = int.Parse(stockReader["stock"].ToString()); + + } + SQLiteCommand command = new SQLiteCommand(sql, conexion); command.Parameters.AddWithValue("@idproducto", item.idProducto); - command.Parameters.AddWithValue("@stock", item.stock); + command.Parameters.AddWithValue("@stock", stockActual + item.stock); command.ExecuteNonQuery(); } @@ -767,6 +781,8 @@ namespace compabetov2.database string sqlVenta = "INSERT INTO venta (fecha, total) VALUES(@fecha, @total);"; string sqlDetalle = "INSERT INTO detalle_venta (fk_venta, fk_producto, cantidad) VALUES(@idVenta, @idProducto, @cantidada);"; string sqlIdVenta = "SELECT idVenta FROM venta ORDER BY idVenta DESC LIMIT 1"; + string sqlProductoStock = "SELECT stock FROM Producto WHERE idproducto = @idproducto"; + string sqlProductoUpdate = "UPDATE Producto SET stock = @stock WHERE idproducto = @idproducto"; SQLiteCommand commandVenta = new SQLiteCommand(sqlVenta, conexion); @@ -790,7 +806,25 @@ namespace compabetov2.database commandEmpleado.Parameters.AddWithValue("@idProducto", detalleVenta.producto.idProducto); commandEmpleado.Parameters.AddWithValue("@cantidada", detalleVenta.cantidad); commandEmpleado.ExecuteNonQuery(); - } + + SQLiteCommand commandProdcutoStock = new SQLiteCommand(sqlProductoStock, conexion); + commandProdcutoStock.Parameters.AddWithValue("@idproducto", detalleVenta.producto.idProducto); + SQLiteDataReader stockReader = commandProdcutoStock.ExecuteReader(); + + int stockActual = -1; + while (stockReader.Read()) + { + stockActual = int.Parse(stockReader["stock"].ToString()); + + } + + SQLiteCommand commandProdcutoUpdate = new SQLiteCommand(sqlProductoUpdate, conexion); + commandProdcutoUpdate.Parameters.AddWithValue("@stock", stockActual - detalleVenta.cantidad); + commandProdcutoUpdate.Parameters.AddWithValue("@idproducto", detalleVenta.producto.idProducto); + commandProdcutoUpdate.ExecuteNonQuery(); + + + } transaccion.Commit(); return true; diff --git a/inventario.cs b/inventario.cs index 7c1d485..96acc55 100644 --- a/inventario.cs +++ b/inventario.cs @@ -103,8 +103,15 @@ namespace compabetov2 btnMenos.Anchor = AnchorStyles.None; btnMenos.Click += new EventHandler(delegate (object sender, EventArgs e) { - int numeroContador = int.Parse(txtStockAgregar.Text); - txtStockAgregar.Text = (numeroContador - 1).ToString(); + try + { + int numeroContador = int.Parse(txtStockAgregar.Text); + txtStockAgregar.Text = (numeroContador - 1).ToString(); + } + catch (Exception ex) + { + txtStockAgregar.Text = "0"; + } }); Button btnMas = new Button(); @@ -119,8 +126,15 @@ namespace compabetov2 btnMas.Anchor = AnchorStyles.None; btnMas.Click += new EventHandler(delegate (object sender, EventArgs e) { - int numeroContador = int.Parse(txtStockAgregar.Text); - txtStockAgregar.Text = (numeroContador + 1).ToString(); + try + { + int numeroContador = int.Parse(txtStockAgregar.Text); + txtStockAgregar.Text = (numeroContador + 1).ToString(); + }catch (Exception ex) + { + txtStockAgregar.Text = "0"; + } + }); @@ -149,19 +163,7 @@ namespace compabetov2 private void button6_Click(object sender, EventArgs e) { - /*Categoria categoria = new Categoria(comboBox1.SelectedIndex + 1); - Producto producto = new Producto( - 0, - txtnombreproducto.Text, - txtdescripcionproducto.Text, - decimal.Parse(txtprecioproducto.Text), - "", - int.Parse(txtstockproducto.Text), - categoria); - if (service.insertarProducto(producto)) { - - }*/ } @@ -169,44 +171,51 @@ namespace compabetov2 private void button6_Click_1(object sender, EventArgs e) { List stockNuevo = new List(); - foreach(Control control in stockPanel.Controls) - { - TableLayoutPanel informacionProducto = control.Controls[0] as TableLayoutPanel; - TableLayoutPanel agregarPanel = control.Controls[1] as TableLayoutPanel; - - //id - Label lbIdProducto = informacionProducto.Controls[0].Controls[0] as Label; - int idProducto = int.Parse(lbIdProducto.Text); - - //stock a agregar - TextBox txtStock = agregarPanel.Controls[0] as TextBox; - int stock = int.Parse(txtStock.Text); - - if(stock > 0) { - Producto productoStockNuuevo = new Producto(idProducto, "", "", 0, "", stock, null); - stockNuevo.Add(productoStockNuuevo); - } - - } - - string mensajeConfirmacion = "Se aumentara el stock a los\nsiguientes productos\n"; - for(int i = 0; i < stockNuevo.Count; i++) - { - - mensajeConfirmacion += productos[i].nombre + ": " + stockNuevo[i].stock + "\n"; - - } - - - if(stockNuevo.Count > 0) - { - DialogResult result = MessageBox.Show(mensajeConfirmacion, "Confirmación", MessageBoxButtons.YesNo, MessageBoxIcon.Question); - if (result == DialogResult.Yes) + try { + foreach (Control control in stockPanel.Controls) { - service.aumentarStock(stockNuevo); - llenarPanel(); + TableLayoutPanel informacionProducto = control.Controls[0] as TableLayoutPanel; + TableLayoutPanel agregarPanel = control.Controls[1] as TableLayoutPanel; + + //id + Label lbIdProducto = informacionProducto.Controls[0].Controls[0] as Label; + int idProducto = int.Parse(lbIdProducto.Text); + + //stock a agregar + TextBox txtStock = agregarPanel.Controls[0] as TextBox; + int stock = int.Parse(txtStock.Text); + + if (stock != 0) + { + Producto productoStockNuuevo = new Producto(idProducto, "", "", 0, "", stock, null); + stockNuevo.Add(productoStockNuuevo); + } + + } + + string mensajeConfirmacion = "Se aumentara el stock a los\nsiguientes productos\n"; + for (int i = 0; i < stockNuevo.Count; i++) + { + + mensajeConfirmacion += productos[i].nombre + ": " + stockNuevo[i].stock + "\n"; + + } + + + if (stockNuevo.Count > 0) + { + DialogResult result = MessageBox.Show(mensajeConfirmacion, "Confirmación", MessageBoxButtons.YesNo, MessageBoxIcon.Question); + if (result == DialogResult.Yes) + { + service.aumentarStock(stockNuevo); + llenarPanel(); + } } } + catch(Exception ex) { + MessageBox.Show("Solo debe introducir numeros","Error", MessageBoxButtons.OK,MessageBoxIcon.Error); + } + }