From f3b423c69a88db9c07c9bd279d6d692da453fbd6 Mon Sep 17 00:00:00 2001 From: Dani Date: Tue, 19 May 2026 14:01:29 +0100 Subject: [PATCH] lab ok --- analisis.py | 94 ++++++++++++++++++++++++++++++++++++++++++++++++++++ informe.json | 11 ++++++ ventas.json | 9 +++++ 3 files changed, 114 insertions(+) create mode 100644 analisis.py create mode 100644 informe.json create mode 100644 ventas.json diff --git a/analisis.py b/analisis.py new file mode 100644 index 0000000..ffc5d63 --- /dev/null +++ b/analisis.py @@ -0,0 +1,94 @@ +import json +from datetime import datetime + +def cargar_ventas(ruta_archivo): + """Lee el archivo JSON y devuelve la lista de ventas.""" + with open(ruta_archivo, 'r', encoding='utf-8') as archivo: + return json.load(archivo) + +def calcular_total_venta(venta): + """Devuelve precio * cantidad para una venta.""" + return venta['precio']*venta['cantidad'] + +def ventas_por_categoria(ventas): + """ + Agrupa las ventas por categoría. + Devuelve un dict: { "categoria": total_euros } + """ + categorias = {} + for venta in ventas: + cat = venta['categoria'] + total_venta = calcular_total_venta(venta) + if cat in categorias: + categorias[cat] += total_venta + else: + categorias[cat] = total_venta + return categorias + +def producto_mas_vendido(ventas): + """Devuelve el nombre del producto con mayor ingreso total.""" + ingresos_productos = {} + for venta in ventas: + prod = venta['producto'] + total_venta = calcular_total_venta(venta) + if prod in ingresos_productos: + ingresos_productos[prod] += total_venta + else: + ingresos_productos[prod] = total_venta + + producto_top = max(ingresos_productos, key=ingresos_productos.get) + return producto_top + +def ventas_en_fecha(ventas, fecha_str): + """Filtra ventas de una fecha específica (formato YYYY-MM-DD).""" + ventas_filtradas = [] + for venta in ventas: + if venta['fecha'] == fecha_str: + ventas_filtradas.append(venta) + return ventas_filtradas + +def main(): + ventas = cargar_ventas("ventas.json") + + total_ventas = len(ventas) + ingresos_totales = sum(calcular_total_venta(v) for v in ventas) + + por_categoria = ventas_por_categoria(ventas) + producto_top = producto_mas_vendido(ventas) + + ingreso_producto_top = sum(calcular_total_venta(v) for v in ventas if v['producto'] == producto_top) + + print("============================") + print(" INFORME DE VENTAS") + print("============================") + print(f"Total de ventas: {total_ventas}") + print(f"Ingresos totales: {ingresos_totales:,.2f} €".replace(",", "X").replace(".", ",").replace("X", ".")) + print() + print("--- Por categoría ---") + for cat, total in por_categoria.items(): + espacios = " " * (12 - len(cat)) + print(f"{cat}:{espacios}{total:,.2f} €".replace(",", "X").replace(".", ",").replace("X", ".")) + print() + print(f"Producto más rentable: {producto_top} ({ingreso_producto_top:,.2f} €)".replace(",", "X").replace(".", ",").replace("X", ".")) + print() + print("--- Ventas del 16/01/2026 ---") + ventas_fecha = ventas_en_fecha(ventas, "2026-01-16") + for v in ventas_fecha: + total_v = calcular_total_venta(v) + print(f"- {v['producto']}: {total_v:,.2f} €".replace(",", "X").replace(".", ",").replace("X", ".")) + guardar_informe(ventas, por_categoria, producto_top, ingresos_totales) + +def guardar_informe(ventas, por_categoria, producto_top, ingresos_totales, ruta="informe.json"): + datos_informe = { + "generado_en": datetime.now().isoformat(timespec='seconds'), + "total_ventas": len(ventas), + "ingresos_totales": round(ingresos_totales, 2), + "por_categoria": {cat: round(total, 2) for cat, total in por_categoria.items()}, + "producto_top": producto_top + } + + with open(ruta, 'w', encoding='utf-8') as archivo: + json.dump(datos_informe, archivo, indent=2, ensure_ascii=False) + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/informe.json b/informe.json new file mode 100644 index 0000000..2419e25 --- /dev/null +++ b/informe.json @@ -0,0 +1,11 @@ +{ + "generado_en": "2026-05-19T13:48:25", + "total_ventas": 7, + "ingresos_totales": 4519.74, + "por_categoria": { + "electronica": 3419.86, + "muebles": 799.98, + "libros": 299.9 + }, + "producto_top": "Laptop" +} \ No newline at end of file diff --git a/ventas.json b/ventas.json new file mode 100644 index 0000000..6c5022e --- /dev/null +++ b/ventas.json @@ -0,0 +1,9 @@ +[ + { "id": 1, "producto": "Laptop", "precio": 899.99, "cantidad": 2, "categoria": "electronica", "fecha": "2026-01-15" }, + { "id": 2, "producto": "Teclado", "precio": 49.99, "cantidad": 5, "categoria": "electronica", "fecha": "2026-01-16" }, + { "id": 3, "producto": "Silla", "precio": 299.99, "cantidad": 1, "categoria": "muebles", "fecha": "2026-01-16" }, + { "id": 4, "producto": "Monitor", "precio": 349.99, "cantidad": 3, "categoria": "electronica", "fecha": "2026-01-17" }, + { "id": 5, "producto": "Libro Python", "precio": 29.99, "cantidad": 10, "categoria": "libros", "fecha": "2026-01-18" }, + { "id": 6, "producto": "Escritorio", "precio": 499.99, "cantidad": 1, "categoria": "muebles", "fecha": "2026-01-18" }, + { "id": 7, "producto": "Auriculares", "precio": 79.99, "cantidad": 4, "categoria": "electronica", "fecha": "2026-01-19" } +] \ No newline at end of file