public static int IngresarDetalle(Constructor_Detalle_Factura det) { int retorno = 0; try { string query1 = "SELECT * FROM tbdetalle_factura a, tbfactura b, tbdetalles_producto c, tbproducto d WHERE a.id_detalle_producto = c.id_detalle_producto AND c.id_producto = d.id_producto AND a.id_factura = b.id_factura AND a.id_factura = ?paramfactura AND c.id_producto = ?paramproducto"; MySqlCommand cmdselect1 = new MySqlCommand(query1, Conexion_Config.ObtenerConexion()); cmdselect1.Parameters.Add(new MySqlParameter("paramfactura", det.id_factura)); cmdselect1.Parameters.Add(new MySqlParameter("paramproducto", det.id_producto)); bool existencia = Convert.ToBoolean(cmdselect1.ExecuteScalar()); if (existencia == false) { MySqlCommand cmdinsert = new MySqlCommand(string.Format("INSERT INTO tbdetalle_factura (id_detalle_producto, cantidad, id_factura, total_parcial) VALUES ('" + det.id_producto + "','" + det.cantidad + "','" + det.id_factura + "','" + det.total_parcial + "')"), Conexion_Config.ObtenerConexion()); retorno = Convert.ToInt16(cmdinsert.ExecuteNonQuery()); if (retorno < 1) { MessageBox.Show("El detalle no pudo ser agregado a la factura.", "Proceso no completado", MessageBoxButtons.OK, MessageBoxIcon.Error); } } else { //Actualizar detalle MySqlDataReader reader = cmdselect1.ExecuteReader(); while (reader.Read()) { int iddetalle = reader.GetInt16(0); int cantidad = reader.GetInt16(2); double pago = reader.GetDouble(4) + det.total_parcial; int nuevacantidad = cantidad + det.cantidad; MySqlCommand cmdupdate = new MySqlCommand(string.Format("UPDATE tbdetalle_factura SET cantidad = '" + nuevacantidad + "', total_parcial = '" + pago + "' WHERE id_detalle_factura = '" + iddetalle + "'"), Conexion_Config.ObtenerConexion()); retorno = Convert.ToInt16(cmdupdate.ExecuteNonQuery()); if (retorno <= 0) { MessageBox.Show("El detalle no pudo actualizarse.", "Proceso no completado", MessageBoxButtons.OK, MessageBoxIcon.Warning); } } } return(retorno); } catch (Exception ex) { MessageBox.Show("Ocurrio un fallo de conexión durante el ingreso del detalle de compra. " + ex, "Proceso no completado", MessageBoxButtons.OK, MessageBoxIcon.Error); return(retorno); } }
void AgregarDetalle() { try { if (txtCantidad.Text == "") { MessageBox.Show("La cantidad de productos no puede estar vacía.", "Error de cantidad", MessageBoxButtons.OK, MessageBoxIcon.Warning); } else if (Convert.ToInt16(txtCantidad.Text) == 0) { MessageBox.Show("La cantidad de productos no puede ser igual a cero.", "Error de cantidad", MessageBoxButtons.OK, MessageBoxIcon.Warning); } else if (Convert.ToInt16(txtCantidad.Text.Trim()) > Convert.ToInt16(txtDisponible.Text.Trim())) { MessageBox.Show("No hay suficientes productos para la cantidad que desea vender.", "Error de cantidad", MessageBoxButtons.OK, MessageBoxIcon.Warning); } else { Constructor_Detalle_Factura detalle = new Constructor_Detalle_Factura(); detalle.id_producto = Convert.ToInt16(txtIdDetalle.Text); detalle.cantidad = Convert.ToInt16(txtCantidad.Text); detalle.id_factura = Convert.ToInt16(txtIdFactura.Text); double pago_parcial = Convert.ToInt16(txtCantidad.Text) * Convert.ToDouble(txtPrecio.Text); detalle.total_parcial = pago_parcial; int resultado = ControlFacturacion.IngresarDetalle(detalle); if (resultado >= 1) { //Actualizar cantidad disponible int cantidad_antigua = Convert.ToInt16(txtDisponible.Text); int cantidad_saliente = Convert.ToInt16(txtCantidad.Text); int cantidad_nueva = cantidad_antigua - cantidad_saliente; int idproducto = Convert.ToInt16(txtIdProducto.Text); int iddetalle = Convert.ToInt16(txtIdDetalle.Text); ControlFacturacion.Actualizar_Cantidad_Productos(cantidad_nueva, iddetalle); LimpiarCampos(); Calcular_Pago(); } } } catch (Exception) { MessageBox.Show("Limite de tiempo superado.", "Limite de tiempo", MessageBoxButtons.OK, MessageBoxIcon.Information); } }
public static int Actualizar_Detalle_Factura(Constructor_Detalle_Factura det) { int retorno = 0; try { MySqlCommand cmdupdate = new MySqlCommand(string.Format("UPDATE tbdetalle_factura SET cantidad = '" + det.cantidad + "', total_parcial = '" + det.total_parcial + "' WHERE id_detalle_factura = '" + det.id_detalle_factura + "'"), Conexion_Config.ObtenerConexion()); retorno = Convert.ToInt16(cmdupdate.ExecuteNonQuery()); if (retorno < 1) { MessageBox.Show("El detalle no pudo ser actualizado.", "Proceso no completado", MessageBoxButtons.OK, MessageBoxIcon.Warning); } return(retorno); } catch (Exception) { MessageBox.Show("El detalle no pudo ser actualizado debido a un fallo de conexión, verifique que está conectado a internet.", "Error de conexión", MessageBoxButtons.OK, MessageBoxIcon.Error); return(retorno); } }
private void BtnActualizarDetalle_Click(object sender, EventArgs e) { nuevopedido = Convert.ToInt16(txtCantidad.Text); if (nuevopedido == pedidoantiguo) { CargarDetalle(); LimpiarCampos(); txtCodigoProducto.Enabled = true; //MessageBox.Show("NO PASO NADA"); } else if (nuevopedido > pedidoantiguo) { //MessageBox.Show("AUMENTAR PEDIDO"); int temp = nuevopedido - pedidoantiguo; if (temp <= Convert.ToInt16(txtDisponible.Text)) { Constructor_Detalle_Factura cdet = new Constructor_Detalle_Factura(); cdet.cantidad = nuevopedido; cdet.id_detalle_factura = Convert.ToInt16(txtIdDetalleFactura.Text); cdet.total_parcial = nuevopedido * Convert.ToDouble(txtPrecio.Text); int datos = ControlFacturacion.Actualizar_Detalle_Factura(cdet); if (datos >= 1) { Constructor_Producto prod = new Constructor_Producto(); int nuevacantidad = Convert.ToInt16(txtDisponible.Text) - temp; //int idproducto = Convert.ToInt16(txtIdDetalle.Text); int iddetalle = Convert.ToInt16(txtIdDetalle.Text); datos = ControlFacturacion.Actualizar_Cantidad_Productos(nuevacantidad, iddetalle); if (datos >= 1) { LimpiarCampos(); CargarDetalle(); Calcular_Pago(); txtCodigoProducto.Enabled = true; } } } } else if (nuevopedido < pedidoantiguo) { //MessageBox.Show("DEVOLVER PRODUCTOS"); int sumar = pedidoantiguo - nuevopedido; Constructor_Detalle_Factura cdet = new Constructor_Detalle_Factura(); cdet.cantidad = nuevopedido; cdet.id_detalle_factura = Convert.ToInt16(txtIdDetalleFactura.Text); cdet.total_parcial = nuevopedido * Convert.ToDouble(txtPrecio.Text); int datos = ControlFacturacion.Actualizar_Detalle_Factura(cdet); if (datos >= 1) { Constructor_Producto prod = new Constructor_Producto(); int nuevacantidad = Convert.ToInt16(txtDisponible.Text) + sumar; // int idproducto = Convert.ToInt16(txtIdProducto.Text); int iddetalle = Convert.ToInt16(txtIdDetalle.Text); datos = ControlFacturacion.Actualizar_Cantidad_Productos(nuevacantidad, iddetalle); if (datos >= 1) { LimpiarCampos(); CargarDetalle(); Calcular_Pago(); txtCodigoProducto.Enabled = true; } } } }