diff --git a/analisis.py b/analisis.py new file mode 100644 index 0000000..6c45c16 --- /dev/null +++ b/analisis.py @@ -0,0 +1,113 @@ +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: + categoria = venta["categoria"] + total = calcular_total_venta(venta) + + if categoria in categorias: + categorias[categoria] += total + else: + categorias[categoria] = total + + return categorias + + +def producto_mas_vendido(ventas): + """Devuelve el nombre del producto con mayor ingreso total.""" + producto_top = "" + mayor_total = 0 + + for venta in ventas: + total = calcular_total_venta(venta) + + if total > mayor_total: + mayor_total = total + producto_top = venta["producto"] + + 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: + fecha_venta = venta["fecha"][:10] + + if fecha_venta == fecha_str: + ventas_filtradas.append(venta) + + return ventas_filtradas + + +def guardar_informe(informe, ruta): + """Guarda el informe en un archivo JSON.""" + with open(ruta, "w", encoding="utf-8") as archivo: + json.dump(informe, archivo, ensure_ascii=False, indent=4) + + +def main(): + ventas = cargar_ventas("ventas.json") + + print("============================") + print(" INFORME DE VENTAS") + print("============================") + + total_ventas = len(ventas) + + ingresos_totales = 0 + for venta in ventas: + ingresos_totales += calcular_total_venta(venta) + + print(f"\nTotal de ventas: {total_ventas}") + print(f"Ingresos totales: {ingresos_totales:.2f} €") + + print("\n--- Por categoría ---") + categorias = ventas_por_categoria(ventas) + + for categoria, total in categorias.items(): + print(f"{categoria}: {total:.2f} €") + + mejor_producto = producto_mas_vendido(ventas) + print(f"\nProducto más rentable: {mejor_producto}") + + print("\n--- Ventas del 2026-01-16 ---") + ventas_fecha = ventas_en_fecha(ventas, "2026-01-16") + + for venta in ventas_fecha: + total = calcular_total_venta(venta) + print(f"- {venta['producto']}: {total:.2f} €") + + informe = { + "generado_en": datetime.now().isoformat(), + "total_ventas": total_ventas, + "ingresos_totales": ingresos_totales, + "por_categoria": categorias, + "producto_top": mejor_producto + } + + guardar_informe(informe, "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..f4fb5d1 --- /dev/null +++ b/informe.json @@ -0,0 +1,11 @@ +{ + "generado_en": "2026-05-19T15:59:48.981257", + "total_ventas": 7, + "ingresos_totales": 4519.740000000001, + "por_categoria": { + "electronica": 3419.8599999999997, + "muebles": 799.98, + "libros": 299.9 + }, + "producto_top": "Laptop" +} \ No newline at end of file diff --git a/lab-python-d1/requirements.txt b/lab-python-d1/requirements.txt new file mode 100644 index 0000000..e5ecefb Binary files /dev/null and b/lab-python-d1/requirements.txt differ 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