diff --git a/Envios/envios.cs b/Envios/envios.cs index eab8111..4136381 100644 --- a/Envios/envios.cs +++ b/Envios/envios.cs @@ -175,9 +175,10 @@ namespace compabetov2 { DialogResult result = MessageBox.Show($"¿Está seguro de que desea eliminar este envio?", "Confirmación", MessageBoxButtons.YesNo, MessageBoxIcon.Question); + List detalles = service.conseguirDetalleEnvio(envio.id); if (result == DialogResult.Yes) { - if (service.cancelarEnvio(envio.id)) + if (service.cancelarEnvio(envio, detalles)) { llenarPanel(); } @@ -291,7 +292,8 @@ namespace compabetov2 string mensaje = ""; foreach (DetalleEnvioModel detalle in detalles) { - mensaje += $"{detalle.producto.nombre} - {detalle.cantidad}\n"; + mensaje += $"{detalle.producto.nombre} - {detalle.cantidad}\n" + + $"Total: {envio.total}"; } MessageBox.Show(mensaje); }); diff --git a/Ventas/vender.cs b/Ventas/vender.cs index eba9b15..9d32a90 100644 --- a/Ventas/vender.cs +++ b/Ventas/vender.cs @@ -675,7 +675,7 @@ namespace compabetov2 return resultado; } - private void button7_Click(object sender, EventArgs e) + /*private void button7_Click(object sender, EventArgs e) { List detalleVentas = listaProductosVenta(); if (detalleVentas.Count > 0) @@ -731,7 +731,7 @@ namespace compabetov2 } return detalleVentas; - } + }*/ private void button6_Click(object sender, EventArgs e) { @@ -836,49 +836,13 @@ namespace compabetov2 } } - private List listaProductosEnvio() - { - List detallesEnvio = new List(); - int contador_saltar_2_vueltas = 1; - foreach (var item in flowLayoutPanel2.Controls) - { - if (contador_saltar_2_vueltas <= 3) - { - contador_saltar_2_vueltas++; - continue; - } - TableLayoutPanel panelProducto = item as TableLayoutPanel; - TableLayoutPanel panelInfo = panelProducto.Controls[1] as TableLayoutPanel; - PictureBox pic = panelProducto.Controls[0] as PictureBox; - Label idLabel = null; - foreach (Control control in pic.Controls) - { - if (control is Label) - { - idLabel = (Label)control; - break; - } - } - int idProducto = int.Parse(idLabel.Text); - Producto producto = new Producto(idProducto); - - Label Lbcantidad = panelInfo.Controls[2] as Label; - string textcantidad = Lbcantidad.Text; - string[] cantidadCadenas = textcantidad.Split(' '); - int cantidad = int.Parse(cantidadCadenas[1]); - - DetalleEnvioModel detalleEnvio = new DetalleEnvioModel(-1, producto, cantidad); - - - detallesEnvio.Add(detalleEnvio); - } - - return detallesEnvio; - } - private void btnEnviar_Click(object sender, EventArgs e) { - List detallesEnvio = listaProductosEnvio(); + List detallesEnvio = new List(); + foreach(DetalleVentaModel detalleVenta in carrito) + { + detallesEnvio.Add(detalleVenta.convertirADetalleEnvio()); + } string responsable = responsablecb.Text.Equals("") ? loginPrincipal.usuario : responsablecb.Text; Envio envio = new Envio(-1, DateTime.Now.ToString(), "", "", responsable, "", "", total); if (detallesEnvio.Count > 0) diff --git a/bin/Debug/compabetov2.exe b/bin/Debug/compabetov2.exe index d5e3f55..293df4a 100644 Binary files a/bin/Debug/compabetov2.exe and b/bin/Debug/compabetov2.exe differ diff --git a/bin/Debug/compabetov2.pdb b/bin/Debug/compabetov2.pdb index dc6431c..da74f7d 100644 Binary files a/bin/Debug/compabetov2.pdb and b/bin/Debug/compabetov2.pdb differ diff --git a/bin/Debug/data.db b/bin/Debug/data.db index f9e47b6..60a4827 100644 Binary files a/bin/Debug/data.db and b/bin/Debug/data.db differ diff --git a/database/DAO.cs b/database/DAO.cs index d3de237..cce6f4e 100644 --- a/database/DAO.cs +++ b/database/DAO.cs @@ -820,7 +820,7 @@ namespace compabetov2.database } SQLiteCommand commandProdcutoUpdate = new SQLiteCommand(sqlProductoUpdate, conexion); - commandProdcutoUpdate.Parameters.AddWithValue("@stock", stockActual - detalleVenta.cantidad); + commandProdcutoUpdate.Parameters.AddWithValue("@stock", stockActual - (detalleVenta.cantidad * detalleVenta.producto.cantidad)); commandProdcutoUpdate.Parameters.AddWithValue("@idproducto", detalleVenta.producto.fkProducto); commandProdcutoUpdate.ExecuteNonQuery(); } @@ -1262,7 +1262,7 @@ namespace compabetov2.database try { string sqlEnvio = "INSERT INTO envios (fecha, direccion, telefono,responzable,total,cliente) VALUES(@fecha,@direccion , @telefono, @responzable, @total,@cliente);"; - string sqlDetalle = "INSERT INTO detalle_envios (fk_envios, fk_producto, cantidad) VALUES(@fk_envios, @fk_producto, @cantidad);"; + string sqlDetalle = "INSERT INTO detalle_envios (fk_envios, fk_variante, cantidad) VALUES(@fk_envios, @fk_variante, @cantidad);"; string sqlIdCuenta = "SELECT idEnvios FROM envios ORDER BY idEnvios DESC LIMIT 1"; SQLiteCommand commandCuenta = new SQLiteCommand(sqlEnvio, conexion); @@ -1287,7 +1287,7 @@ namespace compabetov2.database { SQLiteCommand commandDetalle = new SQLiteCommand(sqlDetalle, conexion); commandDetalle.Parameters.AddWithValue("@fk_envios", idEnvio); - commandDetalle.Parameters.AddWithValue("@fk_producto", detalleEnvio.producto.idProducto); + commandDetalle.Parameters.AddWithValue("@fk_variante", detalleEnvio.producto.idVariante); commandDetalle.Parameters.AddWithValue("@cantidad", detalleEnvio.cantidad); commandDetalle.ExecuteNonQuery(); } @@ -1345,7 +1345,8 @@ namespace compabetov2.database conexion.Open(); - string sql = "Select p.idproducto, p.nombre , de.cantidad from detalle_envios de, Producto p WHERE de.fk_producto = p.idproducto and de.fk_envios = @fk_envios;"; + string sql = "Select v.idVariante, v.nombre, v.cantidad, v.fk_producto, de.cantidad from detalle_envios de,variantes v " + + "WHERE de.fk_variante = v.idVariante and de.fk_envios = @fk_envios;"; SQLiteCommand command = new SQLiteCommand(sql, conexion); command.Parameters.AddWithValue("@fk_envios", idEnvio); reader = command.ExecuteReader(); @@ -1402,7 +1403,7 @@ namespace compabetov2.database } } - public static bool cancelarEnvioDAO(int idEnvio) + public static bool cancelarEnvioDAO(Envio envio, List detalles) { var db_conexion = new Conexion(); try @@ -1416,13 +1417,28 @@ namespace compabetov2.database try { string sqlCuenta = "UPDATE envios SET estado = @estado WHERE idEnvios = @idEnvios;"; + string sqlStock = "select stock from Producto p where idproducto = @idProducto"; + string sqlActualizarStock = "UPDATE Producto set stock = @stock WHERE idproducto = @idProducto"; SQLiteCommand command = new SQLiteCommand(sqlCuenta, conexion); - command.Parameters.AddWithValue("@idEnvios", idEnvio); + command.Parameters.AddWithValue("@idEnvios", envio.id); command.Parameters.AddWithValue("@estado", "CANCELADO"); - command.ExecuteNonQuery(); + foreach (DetalleEnvioModel detalleEnvio in detalles) + { + //actualizar stock + SQLiteCommand commandStock = new SQLiteCommand(sqlStock, conexion); + commandStock.Parameters.AddWithValue("@idProducto", detalleEnvio.producto.fkProducto); + SQLiteDataReader readerStock = commandStock.ExecuteReader(); + long stock = 0; + while (readerStock.Read()) stock = readerStock.GetInt64(0); + + SQLiteCommand commandActualizarStock = new SQLiteCommand(sqlActualizarStock, conexion); + commandActualizarStock.Parameters.AddWithValue("@stock", stock - (detalleEnvio.cantidad * detalleEnvio.producto.cantidad)); + commandActualizarStock.Parameters.AddWithValue("@idProducto", detalleEnvio.producto.fkProducto); + commandActualizarStock.ExecuteNonQuery(); + } transaccion.Commit(); return true; @@ -1457,10 +1473,11 @@ namespace compabetov2.database try { string sqlVenta = "INSERT INTO venta (fecha, total,responzable) VALUES(@fecha, @total,@responzable);"; - string sqlDetalle = "INSERT INTO detalle_venta (fk_venta, fk_producto, cantidad) VALUES(@idVenta, @idProducto, @cantidada);"; + string sqlDetalle = "INSERT INTO detalle_venta (fk_venta, fk_variante, cantidad) VALUES(@idVenta, @fk_variante, @cantidada);"; string sqlEnvio = "UPDATE envios SET estado ='CONCLUIDO' WHERE idEnvios = @idEnvios;"; string sqlIdVenta = "SELECT idVenta FROM venta ORDER BY idVenta DESC LIMIT 1"; - + string sqlStock = "select stock from Producto p where idproducto = @idProducto"; + string sqlActualizarStock = "UPDATE Producto set stock = @stock WHERE idproducto = @idProducto"; SQLiteCommand commandVenta = new SQLiteCommand(sqlVenta, conexion); commandVenta.Parameters.AddWithValue("@fecha", DateTime.Now.ToString()); @@ -1476,17 +1493,27 @@ namespace compabetov2.database SQLiteDataReader reader = commandConsulta.ExecuteReader(); long idVenta = -1; - while (reader.Read()) - { - idVenta = reader.GetInt64(0); - } + while (reader.Read()) idVenta = reader.GetInt64(0); + foreach (DetalleEnvioModel detalleEnvio in detallesEnvio) { SQLiteCommand commandEmpleado = new SQLiteCommand(sqlDetalle, conexion); commandEmpleado.Parameters.AddWithValue("@idVenta", idVenta); - commandEmpleado.Parameters.AddWithValue("@idProducto", detalleEnvio.producto.idProducto); + commandEmpleado.Parameters.AddWithValue("@fk_variante", detalleEnvio.producto.idVariante); commandEmpleado.Parameters.AddWithValue("@cantidada", detalleEnvio.cantidad); commandEmpleado.ExecuteNonQuery(); + + //actualizar stock + SQLiteCommand commandStock = new SQLiteCommand(sqlStock, conexion); + commandStock.Parameters.AddWithValue("@idProducto", detalleEnvio.producto.fkProducto); + SQLiteDataReader readerStock = commandStock.ExecuteReader(); + long stock = 0; + while (readerStock.Read()) stock = readerStock.GetInt64(0); + + SQLiteCommand commandActualizarStock = new SQLiteCommand(sqlActualizarStock, conexion); + commandActualizarStock.Parameters.AddWithValue("@stock", stock - (detalleEnvio.cantidad * detalleEnvio.producto.cantidad)); + commandActualizarStock.Parameters.AddWithValue("@idProducto", detalleEnvio.producto.fkProducto); + commandActualizarStock.ExecuteNonQuery(); } transaccion.Commit(); @@ -1615,8 +1642,8 @@ namespace compabetov2.database "CREATE TABLE envios (idEnvios INTEGER PRIMARY KEY AUTOINCREMENT, fecha TEXT, direccion TEXT, telefono TEXT,responzable TEXT DEFAULT '',estado TEXT DEFAULT 'EN PROCESO', total DECIMAL(10,2) DEFAULT 0,cliente TEXT)", - "CREATE TABLE detalle_envios (fk_envios INTEGER, fk_producto INTEGER,cantidad INTEGER, PRIMARY KEY (fk_envios, fk_producto), " + - "FOREIGN KEY (fk_envios) REFERENCES envios(idEnvios),FOREIGN KEY (fk_producto) REFERENCES producto(idproducto))", + "CREATE TABLE detalle_envios (fk_envios INTEGER, fk_variante INTEGER,cantidad INTEGER, PRIMARY KEY (fk_envios, fk_variante)," + + "FOREIGN KEY (fk_envios) REFERENCES envios(idEnvios),FOREIGN KEY (fk_variante) REFERENCES variantes(idVariante))", "INSERT INTO Login ( tipo, usuario, contra) VALUES('admin', 'admin', '12345');", "INSERT INTO Empleado ( nombre, apellidos, calle, fecha_contratacion,estado, colonia, cp, no_ext,no_int, referencia, fecha_nac, telefono,fk_idLogin) VALUES('jefe', '', '', '', '', '', '', '', '', '', '', '',1);", diff --git a/database/modelos/DetalleEnvioModel.cs b/database/modelos/DetalleEnvioModel.cs index 3636e84..5d80bdf 100644 --- a/database/modelos/DetalleEnvioModel.cs +++ b/database/modelos/DetalleEnvioModel.cs @@ -9,10 +9,10 @@ namespace compabetov2.database.modelos public class DetalleEnvioModel { public int idEnvio; - public Producto producto; + public Variante producto; public int cantidad; - public DetalleEnvioModel(int idEnvio, Producto producto, int cantidad) + public DetalleEnvioModel(int idEnvio, Variante producto, int cantidad) { this.idEnvio = idEnvio; this.producto = producto; diff --git a/database/modelos/DetalleVentaModel.cs b/database/modelos/DetalleVentaModel.cs index a8e3f8b..b180338 100644 --- a/database/modelos/DetalleVentaModel.cs +++ b/database/modelos/DetalleVentaModel.cs @@ -20,5 +20,10 @@ namespace compabetov2.database.modelos this.idVenta = id; this.total = total; } + + public DetalleEnvioModel convertirADetalleEnvio() + { + return new DetalleEnvioModel(-1,this.producto,this.cantidad); + } } } diff --git a/database/service.cs b/database/service.cs index 42d8e1c..9e1d9e2 100644 --- a/database/service.cs +++ b/database/service.cs @@ -352,11 +352,11 @@ namespace compabetov2.database while (reader.Read()) { - int idProducto = (int)reader.GetInt64(0); + int idVariante = (int)reader.GetInt64(0); DetalleEnvioModel detalleEnvio = new DetalleEnvioModel( idEnvio, - new Producto(idProducto, reader["nombre"].ToString()), - (int)reader.GetInt64(2) + new Variante(idVariante, reader["nombre"].ToString(),0,(int)reader.GetInt64(2),"", (int)reader.GetInt64(3)),//fkproducto + (int)reader.GetInt64(4)//cantidad ); detallesEnvio.Add(detalleEnvio); } @@ -370,9 +370,9 @@ namespace compabetov2.database } - public static bool cancelarEnvio(int idEnvio) + public static bool cancelarEnvio(Envio envio, List detalles) { - return DAO.cancelarEnvioDAO(idEnvio); + return DAO.cancelarEnvioDAO(envio, detalles); } public static bool concluirEnvio(Envio envio, List detallesEnvio) diff --git a/obj/Debug/DesignTimeResolveAssemblyReferences.cache b/obj/Debug/DesignTimeResolveAssemblyReferences.cache index f8982ea..4382aa9 100644 Binary files a/obj/Debug/DesignTimeResolveAssemblyReferences.cache and b/obj/Debug/DesignTimeResolveAssemblyReferences.cache differ diff --git a/obj/Debug/compabetov2.exe b/obj/Debug/compabetov2.exe index d5e3f55..293df4a 100644 Binary files a/obj/Debug/compabetov2.exe and b/obj/Debug/compabetov2.exe differ diff --git a/obj/Debug/compabetov2.pdb b/obj/Debug/compabetov2.pdb index dc6431c..da74f7d 100644 Binary files a/obj/Debug/compabetov2.pdb and b/obj/Debug/compabetov2.pdb differ