Procházet zdrojové kódy

Importa sin detectar duplicados (no hace falta)

Celestino Rey před 1 rokem
rodič
revize
38f9a7aee2

+ 8 - 294
Firefly/ff.json

@@ -32,7 +32,7 @@
         "group_title": "",
         "type": "Withdrawal",
         "currency_code": "EUR",
-        "amount": -18,
+        "amount": "18",
         "foreign_currency_code": "",
         "foreign_amount": "",
         "native_currency_code": "EUR",
@@ -1367,30 +1367,6 @@
         "tags": "",
         "notes": ""
     },
-    {
-        "user_id": 1,
-        "group_id": 6984,
-        "journal_id": 6984,
-        "created_at": "2025-05-02T12:52:05+02:00",
-        "updated_at": "2025-05-02T12:52:05+02:00",
-        "group_title": "",
-        "type": "Withdrawal",
-        "currency_code": "EUR",
-        "amount": "12,8",
-        "foreign_currency_code": "",
-        "foreign_amount": "",
-        "native_currency_code": "EUR",
-        "description": "Supermercado -Compra",
-        "date": "2025-05-02T13:28:00+02:00",
-        "source_name": "Efectivo",
-        "source_type": "Asset account",
-        "destination_name": "Ahorramas",
-        "destination_type": "Expense account",
-        "category": "Alimentación",
-        "budget": "Comida",
-        "tags": "",
-        "notes": ""
-    },
     {
         "user_id": 1,
         "group_id": 6989,
@@ -3109,7 +3085,7 @@
         "foreign_amount": "",
         "native_currency_code": "EUR",
         "description": "Devolución playtomic",
-        "date": "2025-04-16T10:22:00+02:00",
+        "date": "2025-04-16T10:21:00+02:00",
         "source_name": "Playtomic",
         "source_type": "Revenue account",
         "destination_name": "ING",
@@ -3157,7 +3133,7 @@
         "foreign_amount": "",
         "native_currency_code": "EUR",
         "description": "Devolución playtomic",
-        "date": "2025-04-16T10:22:00+02:00",
+        "date": "2025-04-16T10:23:00+02:00",
         "source_name": "Playtomic",
         "source_type": "Revenue account",
         "destination_name": "ING",
@@ -3397,7 +3373,7 @@
         "foreign_amount": "",
         "native_currency_code": "EUR",
         "description": "Reserva pádel",
-        "date": "2025-04-15T08:05:00+02:00",
+        "date": "2025-04-15T08:06:00+02:00",
         "source_name": "ING",
         "source_type": "Asset account",
         "destination_name": "Playtomic",
@@ -7885,7 +7861,7 @@
         "foreign_amount": "",
         "native_currency_code": "EUR",
         "description": "Devolución playtomic",
-        "date": "2025-02-24T11:36:00+01:00",
+        "date": "2025-02-24T11:37:00+01:00",
         "source_name": "Playtomic",
         "source_type": "Revenue account",
         "destination_name": "ING",
@@ -137327,30 +137303,7 @@
             "tags": "",
             "notes": ""
         },
-        {
-            "user_id": 1,
-            "group_id": 2261,
-            "journal_id": 2261,
-            "created_at": "2022-02-17T11:06:51+01:00",
-            "updated_at": "2024-01-09T08:48:00+01:00",
-            "group_title": "",
-            "type": "Opening balance",
-            "currency_code": "EUR",
-            "amount": "26303,52",
-            "foreign_currency_code": "",
-            "foreign_amount": "",
-            "native_currency_code": "EUR",
-            "description": "Balance inicial para \"Jorge Rey Mota\"",
-            "date": "2022-01-01T01:00:00+01:00",
-            "source_name": "Jorge Rey Mota",
-            "source_type": "Asset account",
-            "destination_name": "Balance inicial de la cuenta Jorge Rey Mota",
-            "destination_type": "Initial balance account",
-            "category": "",
-            "budget": "",
-            "tags": "",
-            "notes": ""
-        },
+
         {
             "user_id": 1,
             "group_id": 2122,
@@ -144263,30 +144216,7 @@
             "tags": "",
             "notes": ""
         },
-        {
-            "user_id": 1,
-            "group_id": 1782,
-            "journal_id": 1782,
-            "created_at": "2021-10-27T11:42:11+02:00",
-            "updated_at": "2024-01-09T08:48:00+01:00",
-            "group_title": "",
-            "type": "Opening balance",
-            "currency_code": "EUR",
-            "amount": "9,69",
-            "foreign_currency_code": "",
-            "foreign_amount": "",
-            "native_currency_code": "EUR",
-            "description": "Balance inicial para \"El Parking\"",
-            "date": "2021-10-27T02:00:00+02:00",
-            "source_name": "Balance inicial para \"El Parking\"",
-            "source_type": "Initial balance account",
-            "destination_name": "El Parking",
-            "destination_type": "Asset account",
-            "category": "",
-            "budget": "",
-            "tags": "",
-            "notes": ""
-        },
+       
         {
             "user_id": 1,
             "group_id": 1997,
@@ -161303,30 +161233,6 @@
             "tags": "",
             "notes": ""
         },
-        {
-            "user_id": 1,
-            "group_id": 1961,
-            "journal_id": 1961,
-            "created_at": "2021-12-06T18:28:22+01:00",
-            "updated_at": "2024-01-09T08:48:00+01:00",
-            "group_title": "",
-            "type": "Opening balance",
-            "currency_code": "EUR",
-            "amount": 15000,
-            "foreign_currency_code": "",
-            "foreign_amount": "",
-            "native_currency_code": "EUR",
-            "description": "Balance inicial para \"Gala Garrido Sánchez\"",
-            "date": "2021-03-29T02:00:00+02:00",
-            "source_name": "Balance inicial para \"Gala Garrido Sánchez\"",
-            "source_type": "Initial balance account",
-            "destination_name": "Gala Garrido Sánchez",
-            "destination_type": "Asset account",
-            "category": "",
-            "budget": "",
-            "tags": "",
-            "notes": ""
-        },
         {
             "user_id": 1,
             "group_id": 1065,
@@ -163175,30 +163081,6 @@
             "tags": "",
             "notes": ""
         },
-        {
-            "user_id": 1,
-            "group_id": 948,
-            "journal_id": 948,
-            "created_at": "2021-03-04T09:03:42+01:00",
-            "updated_at": "2024-01-09T08:48:00+01:00",
-            "group_title": "",
-            "type": "Opening balance",
-            "currency_code": "EUR",
-            "amount": 5,
-            "foreign_currency_code": "",
-            "foreign_amount": "",
-            "native_currency_code": "EUR",
-            "description": "Balance inicial para \"Twyp\"",
-            "date": "2021-03-04T01:00:00+01:00",
-            "source_name": "Balance inicial para \"Twyp\"",
-            "source_type": "Initial balance account",
-            "destination_name": "Twyp",
-            "destination_type": "Asset account",
-            "category": "",
-            "budget": "",
-            "tags": "",
-            "notes": ""
-        },
         {
             "user_id": 1,
             "group_id": 947,
@@ -164495,30 +164377,6 @@
             "tags": "",
             "notes": ""
         },
-        {
-            "user_id": 1,
-            "group_id": 97,
-            "journal_id": 97,
-            "created_at": "2021-02-10T18:45:58+01:00",
-            "updated_at": "2024-01-09T08:48:00+01:00",
-            "group_title": "",
-            "type": "Opening balance",
-            "currency_code": "EUR",
-            "amount": "23,57",
-            "foreign_currency_code": "",
-            "foreign_amount": "",
-            "native_currency_code": "EUR",
-            "description": "Balance inicial para \"Santander\"",
-            "date": "2021-02-10T01:00:00+01:00",
-            "source_name": "Balance inicial para \"Santander\"",
-            "source_type": "Initial balance account",
-            "destination_name": "Santander",
-            "destination_type": "Asset account",
-            "category": "",
-            "budget": "",
-            "tags": "",
-            "notes": ""
-        },
         {
             "user_id": 1,
             "group_id": 96,
@@ -185638,149 +185496,5 @@
             "budget": "",
             "tags": "",
             "notes": ""
-        },
-        {
-            "user_id": 1,
-            "group_id": 5,
-            "journal_id": 5,
-            "created_at": "2021-01-25T14:16:47+01:00",
-            "updated_at": "2024-01-09T08:48:00+01:00",
-            "group_title": "",
-            "type": "Opening balance",
-            "currency_code": "EUR",
-            "amount": "10283,04",
-            "foreign_currency_code": "",
-            "foreign_amount": "",
-            "native_currency_code": "EUR",
-            "description": "Balance inicial para \"Bankia\"",
-            "date": "2020-01-01T01:00:00+01:00",
-            "source_name": "Balance inicial para \"Bankia\"",
-            "source_type": "Initial balance account",
-            "destination_name": "Bankia",
-            "destination_type": "Asset account",
-            "category": "",
-            "budget": "",
-            "tags": "",
-            "notes": ""
-        },
-        {
-            "user_id": 1,
-            "group_id": 4,
-            "journal_id": 4,
-            "created_at": "2021-01-25T14:16:33+01:00",
-            "updated_at": "2024-01-09T08:48:00+01:00",
-            "group_title": "",
-            "type": "Opening balance",
-            "currency_code": "EUR",
-            "amount": "120240,35",
-            "foreign_currency_code": "",
-            "foreign_amount": "",
-            "native_currency_code": "EUR",
-            "description": "Balance inicial para \"ING Ahorro\"",
-            "date": "2020-01-01T01:00:00+01:00",
-            "source_name": "Balance inicial para \"ING Ahorro\"",
-            "source_type": "Initial balance account",
-            "destination_name": "ING Ahorro",
-            "destination_type": "Asset account",
-            "category": "",
-            "budget": "",
-            "tags": "",
-            "notes": ""
-        },
-        {
-            "user_id": 1,
-            "group_id": 3,
-            "journal_id": 3,
-            "created_at": "2021-01-25T14:16:17+01:00",
-            "updated_at": "2024-01-09T08:48:00+01:00",
-            "group_title": "",
-            "type": "Opening balance",
-            "currency_code": "EUR",
-            "amount": "21419,22",
-            "foreign_currency_code": "",
-            "foreign_amount": "",
-            "native_currency_code": "EUR",
-            "description": "Balance inicial para \"BBVA Ahorro\"",
-            "date": "2020-01-01T01:00:00+01:00",
-            "source_name": "Balance inicial para \"BBVA Ahorro\"",
-            "source_type": "Initial balance account",
-            "destination_name": "BBVA Ahorro",
-            "destination_type": "Asset account",
-            "category": "",
-            "budget": "",
-            "tags": "",
-            "notes": ""
-        },
-        {
-            "user_id": 1,
-            "group_id": 2,
-            "journal_id": 2,
-            "created_at": "2021-01-25T14:15:54+01:00",
-            "updated_at": "2024-01-09T08:48:00+01:00",
-            "group_title": "",
-            "type": "Opening balance",
-            "currency_code": "EUR",
-            "amount": "2096,73",
-            "foreign_currency_code": "",
-            "foreign_amount": "",
-            "native_currency_code": "EUR",
-            "description": "Balance inicial para \"BBVA Nómina\"",
-            "date": "2020-01-01T01:00:00+01:00",
-            "source_name": "Balance inicial para \"BBVA Nómina\"",
-            "source_type": "Initial balance account",
-            "destination_name": "BBVA Nómina",
-            "destination_type": "Asset account",
-            "category": "Nómina",
-            "budget": "",
-            "tags": "",
-            "notes": ""
-        },
-        {
-            "user_id": 1,
-            "group_id": 1,
-            "journal_id": 1,
-            "created_at": "2021-01-25T14:06:52+01:00",
-            "updated_at": "2024-01-09T08:48:00+01:00",
-            "group_title": "",
-            "type": "Opening balance",
-            "currency_code": "EUR",
-            "amount": "22336,58",
-            "foreign_currency_code": "",
-            "foreign_amount": "",
-            "native_currency_code": "EUR",
-            "description": "Initial balance for \"ING\"",
-            "date": "2020-01-01T01:00:00+01:00",
-            "source_name": "Balance inicial para \"ING\"",
-            "source_type": "Initial balance account",
-            "destination_name": "ING",
-            "destination_type": "Asset account",
-            "category": "",
-            "budget": "",
-            "tags": "",
-            "notes": ""
-        },
-        {
-            "user_id": 1,
-            "group_id": 5699,
-            "journal_id": 5699,
-            "created_at": "2024-07-02T08:34:37+02:00",
-            "updated_at": "2024-07-02T08:34:37+02:00",
-            "group_title": "",
-            "type": "Opening balance",
-            "currency_code": "EUR",
-            "amount": 32400,
-            "foreign_currency_code": "",
-            "foreign_amount": "",
-            "native_currency_code": "EUR",
-            "description": "Balance inicial para \"Indexa Capital\"",
-            "date": "2019-12-31T01:00:00+01:00",
-            "source_name": "Balance inicial para \"Indexa Capital\"",
-            "source_type": "Initial balance account",
-            "destination_name": "Indexa Capital",
-            "destination_type": "Asset account",
-            "category": "",
-            "budget": "",
-            "tags": "",
-            "notes": ""
-}
+        }
 ]

+ 1 - 0
src/.gitignore

@@ -1,2 +1,3 @@
 profile_images/
+importar_movimientos.log
 venv/

+ 6 - 17
src/finanzas/management/commands/importar_movimientos_externos.py

@@ -45,35 +45,24 @@ class Command(BaseCommand):
             nombre_origen = registro['source_name']
             nombre_destino = registro['destination_name']
             monto_raw = str(registro['amount']).replace(',', '.')
+
             try:
                 monto = abs(Decimal(monto_raw))
             except:
-                logger.warning(f"Monto inválido: {registro['amount']}")
+                logger.warning(f"Error al convertir: {registro['amount']} a {monto}")
                 continue
 
             if nombre_origen == nombre_destino:
                 logger.info(f"Movimiento con cuentas iguales omitido: {nombre_origen}")
                 continue
 
-            cuenta_origen, _ = Cuenta.objects.get_or_create(nombre=nombre_origen, usuario=user, defaults={'tipo': 'gasto'})
-            cuenta_destino, _ = Cuenta.objects.get_or_create(nombre=nombre_destino, usuario=user, defaults={'tipo': 'ingreso'})
+            cuenta_origen, _ = Cuenta.objects.get_or_create(nombre=nombre_origen, usuario=user, defaults={'saldo': Decimal('0.00'), 'tipo': 'ingreso'})
+            cuenta_destino, _ = Cuenta.objects.get_or_create(nombre=nombre_destino, usuario=user, defaults={'saldo': Decimal('0.00'), 'tipo': 'gasto'})
 
             fecha = make_aware(datetime.fromisoformat(registro['date'][:19]))
             descripcion = registro.get('description', '')
 
             # Detección de duplicado
-            duplicado = Movimiento.objects.filter(
-                usuario=user,
-                cuenta_origen=cuenta_origen,
-                cuenta_destino=cuenta_destino,
-                monto=monto,
-                fecha=fecha,
-                descripcion=descripcion
-            ).exists()
-
-            if duplicado:
-                logger.info(f"Duplicado omitido: {descripcion} - {monto} ({fecha})")
-                continue
 
             mov = Movimiento.objects.create(
                 usuario=user,
@@ -93,14 +82,14 @@ class Command(BaseCommand):
                 cuenta_origen.saldo -= monto
                 cuenta_origen.save()
             except:
-                logger.warning(f"Monto inválido: {registro['amount']} {registro['journal_id']}")
+                logger.warning(f"Monto inválido (origen): {registro['amount']} {registro['journal_id']}")
                 continue
 
             try:
                 cuenta_destino.saldo += monto
                 cuenta_destino.save()
             except:
-                logger.warning(f"Monto inválido: {registro['amount']}")
+                logger.warning(f"Monto inválido (destino): {registro['amount']} {registro['journal_id']}")
                 continue
 
             logger.info(f"Movimiento importado: {descripcion} - {monto} ({fecha})")

+ 2 - 0
src/finanzas/views.py

@@ -36,6 +36,8 @@ def dashboard(request):
     mes = int(request.GET.get('mes', hoy.month))
     anio = int(request.GET.get('anio', hoy.year))
 
+    logger.warning(f"dashboard: mes: {mes} / año: {anio}")
+
     # Obtener último día del mes
     ultimo_dia = monthrange(anio, mes)[1]
 

+ 2 - 0
src/templates/dashboard.html

@@ -20,6 +20,8 @@
                 <span>{{ f.saldo|floatformat:2|intcomma }}</span>
             </div>
         </div>
+        {% empty %}
+            <li>Sin cuentas favoritas configuradas</li>
         {% endfor %}
     </div>