|
@@ -1,5 +1,6 @@
|
|
|
from django.shortcuts import render, redirect, get_object_or_404
|
|
from django.shortcuts import render, redirect, get_object_or_404
|
|
|
from django.http import JsonResponse
|
|
from django.http import JsonResponse
|
|
|
|
|
+from django.views.decorators.csrf import csrf_exempt
|
|
|
from decimal import Decimal, InvalidOperation
|
|
from decimal import Decimal, InvalidOperation
|
|
|
from django.db import models as db_models
|
|
from django.db import models as db_models
|
|
|
from django.contrib.auth.decorators import login_required
|
|
from django.contrib.auth.decorators import login_required
|
|
@@ -7,6 +8,7 @@ from django.contrib import messages
|
|
|
from django.utils.timezone import now
|
|
from django.utils.timezone import now
|
|
|
from django.core.serializers import serialize
|
|
from django.core.serializers import serialize
|
|
|
from datetime import date, timedelta
|
|
from datetime import date, timedelta
|
|
|
|
|
+from django.conf import settings
|
|
|
|
|
|
|
|
import logging
|
|
import logging
|
|
|
import re
|
|
import re
|
|
@@ -15,6 +17,8 @@ import calendar
|
|
|
from accounts import models
|
|
from accounts import models
|
|
|
from .models import Movimiento, Cuenta, MovimientoFrecuente, Categoria, TransaccionRecurrente
|
|
from .models import Movimiento, Cuenta, MovimientoFrecuente, Categoria, TransaccionRecurrente
|
|
|
from .forms import MovimientoForm, FiltroForm, CuentaForm
|
|
from .forms import MovimientoForm, FiltroForm, CuentaForm
|
|
|
|
|
+from .recurrentes import procesar_transacciones_recurrentes
|
|
|
|
|
+
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
@@ -277,54 +281,18 @@ def api_datos_completos(request):
|
|
|
return JsonResponse(data, safe=False, json_dumps_params={'ensure_ascii': False, 'indent': 2})
|
|
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
|
|
@login_required
|
|
|
def ejecutar_recurrentes(request):
|
|
def ejecutar_recurrentes(request):
|
|
|
procesar_transacciones_recurrentes()
|
|
procesar_transacciones_recurrentes()
|
|
|
return redirect('dashboard')
|
|
return redirect('dashboard')
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+@csrf_exempt
|
|
|
|
|
+def api_ejecutar_recurrentes(request):
|
|
|
|
|
+ token_enviado = request.headers.get('X-API-KEY') or request.GET.get('token')
|
|
|
|
|
+
|
|
|
|
|
+ if token_enviado != settings.RECURRENTES_API_TOKEN:
|
|
|
|
|
+ return JsonResponse({'error': 'No autorizado'}, status=403)
|
|
|
|
|
+
|
|
|
|
|
+ procesar_transacciones_recurrentes(hoy=now().date())
|
|
|
|
|
+ return JsonResponse({'ok': True, 'mensaje': 'Transacciones recurrentes ejecutadas'})
|