|
|
@@ -281,6 +281,9 @@ def listar_cuentas(request):
|
|
|
cuentas = Cuenta.objects.filter(usuario=request.user).order_by('nombre')
|
|
|
form_filtro = FiltroForm(request.GET or None)
|
|
|
|
|
|
+ fecha_inicio = timezone.localtime()
|
|
|
+ fecha_fin = fecha_inicio.replace(day=1)
|
|
|
+
|
|
|
if form_filtro.is_valid():
|
|
|
busqueda = form_filtro.cleaned_data.get('buscar')
|
|
|
if busqueda:
|
|
|
@@ -289,7 +292,11 @@ def listar_cuentas(request):
|
|
|
)
|
|
|
|
|
|
return render(request, 'listar_cuentas.html',
|
|
|
- {'cuentas': cuentas, 'form_filtro': form_filtro})
|
|
|
+ {'cuentas': cuentas,
|
|
|
+ 'form_filtro': form_filtro,
|
|
|
+ 'fecha_inicio:': fecha_inicio,
|
|
|
+ 'fecha_fin': fecha_fin}
|
|
|
+ )
|
|
|
|
|
|
|
|
|
@login_required
|
|
|
@@ -465,15 +472,37 @@ def resumen_saldos_mensuales(request):
|
|
|
@login_required
|
|
|
def informe_cuenta(request):
|
|
|
cuenta_id = request.GET.get('cuenta_id')
|
|
|
- fecha_inicio = parse_date(request.GET.get('fecha_inicio')) or date.today().replace(day=1)
|
|
|
- fecha_fin = parse_date(request.GET.get('fecha_fin')) or date.today()
|
|
|
+
|
|
|
+ # obtener fechas desde GET o establecer por defecto (mes actual)
|
|
|
+ hoy = timezone.localtime()
|
|
|
+ primer_dia = hoy.replace(day=1)
|
|
|
+
|
|
|
+ fecha_inicio_str = request.GET.get('fecha_inicio')
|
|
|
+ fecha_fin_str = request.GET.get('fecha_fin')
|
|
|
+ logger.warning(f"informe_cuenta => fecha_inicio_str: {fecha_inicio_str}. fecha_fin_str: {fecha_fin_str}")
|
|
|
+
|
|
|
+ if fecha_inicio_str and fecha_fin_str:
|
|
|
+ fecha_inicio = datetime.strptime(fecha_inicio_str, '%Y-%m-%d').date()
|
|
|
+ fecha_fin = datetime.strptime(fecha_fin_str, '%Y-%m-%d').date()
|
|
|
+ else:
|
|
|
+ fecha_inicio = primer_dia
|
|
|
+ fecha_fin = hoy
|
|
|
+
|
|
|
+ # Convertir a datetime aware para evitar warnings
|
|
|
+ fecha_inicio_dt = timezone.make_aware(datetime.combine(fecha_inicio, time.min))
|
|
|
+ fecha_fin_dt = timezone.make_aware(datetime.combine(fecha_fin, time.max))
|
|
|
|
|
|
cuenta = get_object_or_404(Cuenta, id=cuenta_id)
|
|
|
+
|
|
|
+ logger.warning(f"informe_cuenta => cuenta: {cuenta}. fecha_inicio_dt: {fecha_inicio_dt}. fecha_fin_dt: {fecha_fin_dt}")
|
|
|
+
|
|
|
movimientos = Movimiento.objects.filter(
|
|
|
Q(cuenta_origen=cuenta) | Q(cuenta_destino=cuenta),
|
|
|
- fecha__date__range=(fecha_inicio, fecha_fin)
|
|
|
+ fecha__range=(fecha_inicio_dt, fecha_fin_dt)
|
|
|
).order_by('fecha')
|
|
|
|
|
|
+ logger.warning(f"informe_cuenta => salgo de la búsqueda de movimientos")
|
|
|
+
|
|
|
# Cálculo de saldos después de cada movimiento
|
|
|
saldo = 0
|
|
|
movimientos_con_saldo = []
|
|
|
@@ -488,80 +517,45 @@ def informe_cuenta(request):
|
|
|
context = {
|
|
|
'cuenta': cuenta,
|
|
|
'movimientos_con_saldo': movimientos_con_saldo,
|
|
|
- 'fecha_inicio': fecha_inicio,
|
|
|
- 'fecha_fin': fecha_fin,
|
|
|
+ 'fecha_inicio': fecha_inicio_str,
|
|
|
+ 'fecha_fin': fecha_fin_str,
|
|
|
}
|
|
|
- return render(request, 'informe_cuenta.html', context)
|
|
|
-
|
|
|
-
|
|
|
-@login_required
|
|
|
-def informe_cuenta_pdf(request, cuenta_id):
|
|
|
- cuenta = get_object_or_404(Cuenta, id=cuenta_id, usuario=request.user)
|
|
|
-
|
|
|
- movimientos = Movimiento.objects.filter(
|
|
|
- Q(cuenta_origen=cuenta) | Q(cuenta_destino=cuenta)
|
|
|
- ).order_by('fecha')
|
|
|
|
|
|
- saldo = 0
|
|
|
- historial = []
|
|
|
+ logger.warning(f"contexto: {context}")
|
|
|
|
|
|
- for mov in movimientos:
|
|
|
- if mov.cuenta_origen == cuenta:
|
|
|
- saldo -= mov.monto
|
|
|
- tipo = 'Salida'
|
|
|
- elif mov.cuenta_destino == cuenta:
|
|
|
- saldo += mov.monto
|
|
|
- tipo = 'Entrada'
|
|
|
- else:
|
|
|
- tipo = 'Otro'
|
|
|
-
|
|
|
- historial.append({
|
|
|
- 'fecha': mov.fecha,
|
|
|
- 'descripcion': mov.descripcion,
|
|
|
- 'monto': mov.monto,
|
|
|
- 'tipo': tipo,
|
|
|
- 'saldo_despues': saldo,
|
|
|
- })
|
|
|
-
|
|
|
- template = get_template('informe_cuenta_pdf.html')
|
|
|
- html = template.render({'cuenta': cuenta, 'historial': historial})
|
|
|
+ return render(request, 'informe_cuenta.html', context)
|
|
|
|
|
|
- with tempfile.NamedTemporaryFile(delete=True) as output:
|
|
|
- HTML(string=html).write_pdf(output.name)
|
|
|
|
|
|
- output.seek(0)
|
|
|
- response = HttpResponse(output.read(), content_type='application/pdf')
|
|
|
- response['Content-Disposition'] = 'inline; filename=informe_{cuenta.nombre}.pdf '
|
|
|
- return response
|
|
|
+@login_required
|
|
|
+def informe_cuenta_pdf(request):
|
|
|
+ cuenta_id = request.GET.get('cuenta_id')
|
|
|
|
|
|
+ cuenta = get_object_or_404(Cuenta, id=cuenta_id, usuario=request.user)
|
|
|
|
|
|
-@login_required
|
|
|
-def informe_cuenta_form(request):
|
|
|
- if request.method == 'POST':
|
|
|
- cuenta_id = request.GET.get('cuenta_id')
|
|
|
+ # obtener fechas desde GET o establecer por defecto (mes actual)
|
|
|
+ hoy = timezone.localtime()
|
|
|
+ primer_dia = hoy.replace(day=1)
|
|
|
|
|
|
- form = InformeCuentaForm(request.GET or None, cuenta_id=cuenta_id)
|
|
|
+ fecha_inicio_str = request.GET.get('fecha_inicio')
|
|
|
+ fecha_fin_str = request.GET.get('fecha_fin')
|
|
|
+ logger.warning(f"informe_cuenta_pdf: entre {fecha_inicio_str} y {fecha_fin_str}")
|
|
|
|
|
|
- if form.is_valid():
|
|
|
- cuenta = form.cleaned_data['cuenta']
|
|
|
- fecha_inicio = form.cleaned_data['fecha_inicio']
|
|
|
- fecha_fin = form.cleaned_data['fecha_fin']
|
|
|
- return redirect('informe_cuenta_pdf_rango', cuenta.id, fecha_inicio.isoformat(), fecha_fin.isoformat())
|
|
|
+ if fecha_inicio_str and fecha_fin_str:
|
|
|
+ fecha_inicio = datetime.strptime(fecha_inicio_str, '%Y-%m-%d').date()
|
|
|
+ fecha_fin = datetime.strptime(fecha_fin_str, '%Y-%m-%d').date()
|
|
|
else:
|
|
|
- form = InformeCuentaForm(user=request.user)
|
|
|
-
|
|
|
- return render(request, 'informe_cuenta_form.html', {'form': form})
|
|
|
+ fecha_inicio = primer_dia
|
|
|
+ fecha_fin = hoy
|
|
|
|
|
|
+ # Convertir a datetime aware para evitar warnings
|
|
|
+ fecha_inicio_dt = timezone.make_aware(datetime.combine(fecha_inicio, time.min))
|
|
|
+ fecha_fin_dt = timezone.make_aware(datetime.combine(fecha_fin, time.max))
|
|
|
|
|
|
-@login_required
|
|
|
-def informe_cuenta_pdf_rango(request, cuenta_id, fecha_inicio, fecha_fin):
|
|
|
- cuenta = get_object_or_404(Cuenta, id=cuenta_id, usuario=request.user)
|
|
|
- fi = datetime.fromisoformat(fecha_inicio)
|
|
|
- ff = datetime.fromisoformat(fecha_fin)
|
|
|
+ logger.warning(f"informe_cuenta_pdf: entre {fecha_inicio} y {fecha_fin}")
|
|
|
|
|
|
movimientos = Movimiento.objects.filter(
|
|
|
Q(cuenta_origen=cuenta) | Q(cuenta_destino=cuenta),
|
|
|
- fecha__date__range=(fi.date(), ff.date())
|
|
|
+ fecha__range=(fecha_inicio_dt, fecha_fin_dt)
|
|
|
).order_by('fecha')
|
|
|
|
|
|
saldo = 0
|
|
|
@@ -586,15 +580,12 @@ def informe_cuenta_pdf_rango(request, cuenta_id, fecha_inicio, fecha_fin):
|
|
|
})
|
|
|
|
|
|
template = get_template('informe_cuenta_pdf.html')
|
|
|
- html = template.render({
|
|
|
- 'cuenta': cuenta,
|
|
|
- 'historial': historial,
|
|
|
- 'rango': f"{fi.strftime('%d/%m/%Y')} – {ff.strftime('%d/%m/%Y')}",
|
|
|
- })
|
|
|
+ html = template.render({'cuenta': cuenta, 'historial': historial})
|
|
|
|
|
|
with tempfile.NamedTemporaryFile(delete=True) as output:
|
|
|
HTML(string=html).write_pdf(output.name)
|
|
|
+
|
|
|
output.seek(0)
|
|
|
response = HttpResponse(output.read(), content_type='application/pdf')
|
|
|
- response['Content-Disposition'] = f'filename=informe_{cuenta.nombre}.pdf'
|
|
|
+ response['Content-Disposition'] = 'inline; filename=informe_{cuenta.nombre}.pdf '
|
|
|
return response
|