Quellcode durchsuchen

Preparo informe pdf de categorias

Celestino Rey vor 5 Monaten
Ursprung
Commit
95c8759024

+ 19 - 1
src/finanzas/views.py

@@ -725,8 +725,26 @@ def informe_categoria_pdf(request):
         fecha__range=(fecha_inicio_dt, fecha_fin_dt)
     ).order_by('fecha')
 
+    saldo = saldo_inicial
+
+    historial = []
+
+    for mov in movimientos:
+        saldo += mov.monto
+
+        historial.append({
+            'fecha': mov.fecha,
+            'descripcion': mov.descripcion,
+            'desde': mov.cuenta_origen.nombre if mov.cuenta_origen else 'N/A',
+            'hasta': mov.cuenta_destino.nombre if mov.cuenta_destino else 'N/A',
+            'monto': mov.monto,
+            'saldo_despues': saldo
+        })
+
+    saldo_final = saldo        
+
     template = get_template('informe_categoria_pdf.html')
-    html = template.render({'categoria': categoria, 'fecha_inicio': fecha_inicio_str, 'fecha_fin': fecha_fin_str, 'movimientos': movimientos})
+    html = template.render({'categoria': categoria, 'fecha_inicio': fecha_inicio_str, 'fecha_fin': fecha_fin_str, 'saldo_final': saldo, 'historial': historial})
 
     with tempfile.NamedTemporaryFile(delete=True) as output:
         HTML(string=html).write_pdf(output.name)

+ 4 - 1
src/templates/informe_categoria.html

@@ -66,7 +66,10 @@
             </table>
 
             <div class="row g-4 mb-4">
-                <a href="{% url 'listar_categorias' %}?fecha_inicio={{ fecha_inicio }}&fecha_fin={{ fecha_fin }}"><button class="btn btn-secondary" type="button">Volver a Categorías</button></a>
+                <a href="{% url 'listar_categorias' %}" class="btn btn-secondary">Volver</a>
+                <a href="{% url 'informe_categoria_pdf'%}?categoria_id={{ categoria.id }}&fecha_inicio={{ fecha_inicio }}&fecha_fin={{ fecha_fin }}" class="btn btn-outline-primary" target="_blank">
+                Descargar PDF
+                </a>
             </div>
         </div>
     </div>

+ 45 - 0
src/templates/informe_categoria_pdf.html

@@ -0,0 +1,45 @@
+<!DOCTYPE html>
+<html>
+<head>
+    <meta charset="utf-8">
+    {% load filtros_de_entorno %}
+    <style>
+        body { font-family: sans-serif; font-size: 12px; }
+        table { width: 100%; border-collapse: collapse; margin-top: 20px; }
+        th, td { border: 1px solid #ccc; padding: 4px; text-align: right; }
+        th { background: #f0f0f0; }
+        td.text-left, th.text-left { text-align: left; }
+    </style>
+</head>
+<body>
+    <h2 style="color: chocolate;">Informe de categoría: {{ categoria.nombre }}</h2>
+    <h4>
+        Periodo del {{ fecha_inicio }} hasta {{ fecha_fin }} -  Saldo final: {{ saldo_final|formato_numero:2 }} €
+    </h4>
+    <table>
+        <thead>
+            <tr>
+                <th class="text-left">Fecha</th>
+                <th class="text-left">Descripción</th>
+                <th class="text-left">Origen</th>
+                <th class="text-left">Destino</th>
+                <th>Monto (€)</th>
+                <th>Saldo después (€)</th>
+            </tr>
+        </thead>
+        <tbody>
+            {% for item in historial %}
+            <tr>
+                <td class="text-left">{{ item.fecha|date:"Y-m-d H:i" }}</td>
+                <td class="text-left">{{ item.descripcion }}</td>
+                <td class="text-left">{{ item.desde }}</td>
+                <td class="text-left">{{ item.hasta }}</td>
+                <td style="color: green" >{{ item.monto|formato_numero:2 }}</td>
+                {% endif %}
+                <td>{{ item.saldo_despues|formato_numero:2 }}</td>
+            </tr>
+            {% endfor %}
+        </tbody>
+    </table>
+</body>
+</html>