From 93a06caedde000c0c221448bb0b3e1f0fc36c9ab Mon Sep 17 00:00:00 2001 From: Kilian Date: Wed, 20 May 2026 15:25:27 +0100 Subject: [PATCH] go --- analisis.py | 80 ++++++++++++++++++++++++++++++++++++++++++++++++++++ informe.json | 11 ++++++++ ventas.json | 58 +++++++++++++++++++++++++++++++++++++ 3 files changed, 149 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..f578953 --- /dev/null +++ b/analisis.py @@ -0,0 +1,80 @@ +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 f: + return json.load(f) + +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 v in ventas: + cat = v['categoria'] + total = calcular_total_venta(v) + categorias[cat] = categorias.get(cat, 0) + total + return categorias + +def producto_mas_vendido(ventas): + """Devuelve el nombre del producto con mayor ingreso total.""" + ingresos_por_producto = {} + for v in ventas: + prod = v['producto'] + total = calcular_total_venta(v) + ingresos_por_producto[prod] = ingresos_por_producto.get(prod, 0) + total + + producto_top = max(ingresos_por_producto, key=ingresos_por_producto.get) + return producto_top, ingresos_por_producto[producto_top] + +def ventas_en_fecha(ventas, fecha_str): + """Filtra ventas de una fecha específica (formato YYYY-MM-DD).""" + return [v for v in ventas if v['fecha'] == fecha_str] + +def guardar_informe(informe, ruta): + """Guarda el informe en informe.json.""" + with open(ruta, 'w', encoding='utf-8') as f: + json.dump(informe, f, indent=2, ensure_ascii=False) + +def main(): + ruta = 'ventas.json' + ventas = cargar_ventas(ruta) + + total_ingresos = sum(calcular_total_venta(v) for v in ventas) + por_categoria = ventas_por_categoria(ventas) + prod_top_nombre, prod_top_valor = producto_mas_vendido(ventas) + ventas_16 = ventas_en_fecha(ventas, "2026-01-16") + + # Imprimir Informe + print("============================") + print(" INFORME DE VENTAS") + print("============================") + print(f"\nTotal de ventas: {len(ventas)}") + print(f"Ingresos totales: {total_ingresos:,.2f} €".replace(",", "X").replace(".", ",").replace("X", ".")) + + print("\n--- Por categoría ---") + for cat, total in por_categoria.items(): + print(f"{cat}: {total:>12,.2f} €".replace(",", "X").replace(".", ",").replace("X", ".")) + + print(f"\nProducto más rentable: {prod_top_nombre} ({prod_top_valor:,.2f} €)".replace(",", "X").replace(".", ",").replace("X", ".")) + + print("\n--- Ventas del 16/01/2026 ---") + for v in ventas_16: + subtotal = calcular_total_venta(v) + print(f"- {v['producto']}: {subtotal:,.2f} €".replace(",", "X").replace(".", ",").replace("X", ".")) + + # Preparar y guardar JSON + informe_json = { + "generado_en": datetime.now().isoformat(timespec='seconds'), + "total_ventas": len(ventas), + "ingresos_totales": round(total_ingresos, 2), + "por_categoria": {k: round(v, 2) for k, v in por_categoria.items()}, + "producto_top": prod_top_nombre + } + guardar_informe(informe_json, 'informe.json') + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/informe.json b/informe.json new file mode 100644 index 0000000..8e159c9 --- /dev/null +++ b/informe.json @@ -0,0 +1,11 @@ +{ + "generado_en": "2026-05-20T15:20:37", + "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..017c073 --- /dev/null +++ b/ventas.json @@ -0,0 +1,58 @@ +[ + { + "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" + } +]