/// <summary> /// Obtiene la lista de productos del sistema. /// </summary> /// <returns>La lista de productos.</returns> /// <exception cref="ReglasNegocioException">Si ocurre un error de negocio.</exception> public List<Producto> ObtenerProductos() { List<Producto> productos = new List<Producto>(); try { string sql = "SELECT Codigo, Descripcion, Precio FROM Productos"; BaseDatos db = new BaseDatos(); db.Conectar(); db.CrearComando(sql); DbDataReader datos = db.EjecutarConsulta(); Producto p = null; while (datos.Read()) { try { p = new Producto(datos.GetInt32(0), datos.GetString(1), datos.GetDouble(2)); productos.Add(p); } catch (InvalidCastException ex) { throw new ReglasNegocioException("Los tipos no coinciden.", ex); } catch (DataException ex) { throw new ReglasNegocioException("Error de ADO.NET.", ex); } } datos.Close(); db.Desconectar(); } catch (BaseDatosException) { throw new ReglasNegocioException("Error al acceder a la base de datos para obtener los productos."); } catch (ReglasNegocioException) { throw new ReglasNegocioException("Error a obtener los productos."); } return productos; }
/// <summary> /// Obtiene un producto en base a su código. /// </summary> /// <param name="codigo">El código del producto.</param> /// <returns>El producto encontrado.</returns> /// <exception cref="ReglasNegocioException">Si ocurre un error de negocio.</exception> public Producto ObtenerProducto(int codigo) { Producto producto = null; try { if (codigo <= 0) { throw new ReglasNegocioException("El código es inválido."); } string sql = "SELECT Codigo, Descripcion, Precio FROM Productos WHERE Codigo=@codigo"; BaseDatos db = new BaseDatos(); db.Conectar(); db.CrearComando(sql); db.AsignarParametroEntero("@codigo", codigo); DbDataReader datos = db.EjecutarConsulta(); while (datos.Read()) { try { producto = new Producto(datos.GetInt32(0), datos.GetString(1), datos.GetDouble(2)); } catch (InvalidCastException ex) { throw new ReglasNegocioException("Los tipos no coinciden.", ex); } catch (DataException ex) { throw new ReglasNegocioException("Error de ADO.NET.", ex); } } datos.Close(); db.Desconectar(); } catch (BaseDatosException) { throw new ReglasNegocioException("Error al acceder a la base de datos para obtener los productos."); } catch (ReglasNegocioException ex) { throw new ReglasNegocioException("Error a obtener los productos. " + ex.Message); } return producto; }
/// <summary> /// Obtiene los ítems de una venta. /// </summary> /// <param name="venta">La venta cuyos ítems se van a obtener.</param> /// <returns>Los ítems de la venta.</returns> /// <exception cref="ReglasNegocioException">Si ocurre un error de negocio.</exception> private List<ItemVenta> ObtenerItemsVenta(Venta venta) { List<ItemVenta> items = new List<ItemVenta>(); try { string sql = "SELECT Codigo, Cantidad, CodigoProducto FROM ItemsVenta WHERE CodigoVenta=@venta"; BaseDatos db = new BaseDatos(); db.Conectar(); db.CrearComando(sql); db.AsignarParametroEntero("@venta", venta.Codigo); DbDataReader datosItems = db.EjecutarConsulta(); ItemVenta item = null; CatalogoProductos catalogoProd = new CatalogoProductos(); while (datosItems.Read()) { try { Producto producto = catalogoProd.ObtenerProducto(datosItems.GetInt32(2)); item = new ItemVenta(datosItems.GetInt32(0), producto, datosItems.GetInt32(1)); venta.AgregarItem(item); } catch (InvalidCastException ex) { throw new ReglasNegocioException("Los tipos no coinciden.", ex); } catch (DataException ex) { throw new ReglasNegocioException("Error de ADO.NET.", ex); } } datosItems.Close(); db.Desconectar(); } catch (BaseDatosException) { throw new ReglasNegocioException("Error al acceder a la base de datos para obtener los ítems de venta."); } catch (ReglasNegocioException ex) { throw new ReglasNegocioException("Error a obtener los ítems de venta. " + ex.Message); } return items; }
/// <summary> /// Obtiene el listado de ventas registradas en el sistema. /// </summary> /// <returns>El listado de ventas registradas.</returns> /// <exception cref="ReglasNegocioException">Si ocurre un error de negocio.</exception> public List<Venta> ObtenerVentas() { List<Venta> ventas = new List<Venta>(); try { string sql = "SELECT Codigo, Fecha FROM Ventas"; BaseDatos db = new BaseDatos(); db.Conectar(); db.CrearComando(sql); DbDataReader datosVentas = db.EjecutarConsulta(); Venta venta = null; while (datosVentas.Read()) { try { venta = new Venta(); venta.Codigo = datosVentas.GetInt32(0); venta.Fecha = datosVentas.GetDateTime(1); List<ItemVenta> items = ObtenerItemsVenta(venta); foreach (ItemVenta item in items) { venta.AgregarItem(item); } ventas.Add(venta); } catch (InvalidCastException ex) { throw new ReglasNegocioException("Los tipos no coinciden.", ex); } catch (DataException ex) { throw new ReglasNegocioException("Error de ADO.NET.", ex); } } datosVentas.Close(); db.Desconectar(); } catch (BaseDatosException) { throw new ReglasNegocioException("Error al acceder a la base de datos para obtener las ventas."); } catch (ReglasNegocioException ex) { throw new ReglasNegocioException("Error a obtener las ventas. " + ex.Message); } return ventas; }