|
|
@@ -6,12 +6,14 @@ from django.contrib.auth.decorators import login_required
|
|
|
from django.contrib import messages
|
|
|
from django.utils.timezone import now
|
|
|
from django.core.serializers import serialize
|
|
|
+from datetime import date, timedelta
|
|
|
|
|
|
import logging
|
|
|
import re
|
|
|
+import calendar
|
|
|
|
|
|
from accounts import models
|
|
|
-from .models import Movimiento, Cuenta, MovimientoFrecuente, Categoria
|
|
|
+from .models import Movimiento, Cuenta, MovimientoFrecuente, Categoria, TransaccionRecurrente
|
|
|
from .forms import MovimientoForm, FiltroForm, CuentaForm
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
@@ -273,3 +275,56 @@ def api_datos_completos(request):
|
|
|
'favoritos': list(MovimientoFrecuente.objects.filter(usuario=request.user).values()),
|
|
|
}
|
|
|
return JsonResponse(data, safe=False, json_dumps_params={'ensure_ascii': False, 'indent': 2})
|
|
|
+
|
|
|
+
|
|
|
+def procesar_transacciones_recurrentes(hoy=None):
|
|
|
+ if hoy is None:
|
|
|
+ hoy = date.today()
|
|
|
+
|
|
|
+ for tr in TransaccionRecurrente.objects.filter(activa=True):
|
|
|
+ if tr.ultima_ejecucion and tr.ultima_ejecucion >= hoy.replace(day=1):
|
|
|
+ continue # Ya se ejecutó este mes
|
|
|
+
|
|
|
+ ejecutar = False
|
|
|
+
|
|
|
+ if tr.frecuencia == 'mensual_dia' and tr.dia_del_mes:
|
|
|
+ ejecutar = hoy.day == tr.dia_del_mes
|
|
|
+
|
|
|
+ elif tr.frecuencia == 'mensual_semana' and tr.semana and tr.dia_semana is not None:
|
|
|
+ # Calcular fecha del primer (por ejemplo) lunes
|
|
|
+ primer_dia = hoy.replace(day=1)
|
|
|
+ weekday_count = 0
|
|
|
+ for i in range(31): # máx 31 días
|
|
|
+ d = primer_dia + timedelta(days=i)
|
|
|
+ if d.month != hoy.month:
|
|
|
+ break
|
|
|
+ if d.weekday() == tr.dia_semana:
|
|
|
+ weekday_count += 1
|
|
|
+ if weekday_count == tr.semana and d == hoy:
|
|
|
+ ejecutar = True
|
|
|
+ break
|
|
|
+
|
|
|
+ if ejecutar:
|
|
|
+ Movimiento.objects.create(
|
|
|
+ usuario=tr.usuario,
|
|
|
+ cuenta_origen=tr.cuenta_origen,
|
|
|
+ cuenta_destino=tr.cuenta_destino,
|
|
|
+ monto=tr.monto,
|
|
|
+ fecha=hoy,
|
|
|
+ descripcion=tr.descripcion,
|
|
|
+ categoria=tr.categoria,
|
|
|
+ )
|
|
|
+ tr.ultima_ejecucion = hoy
|
|
|
+ tr.save()
|
|
|
+
|
|
|
+ # Actualizar saldos
|
|
|
+ tr.cuenta_origen.saldo -= tr.monto
|
|
|
+ tr.cuenta_origen.save()
|
|
|
+ tr.cuenta_destino.saldo += tr.monto
|
|
|
+ tr.cuenta_destino.save()
|
|
|
+
|
|
|
+
|
|
|
+@login_required
|
|
|
+def ejecutar_recurrentes(request):
|
|
|
+ procesar_transacciones_recurrentes()
|
|
|
+ return redirect('dashboard')
|