feat(import): pick the account when running an import

An import run can now be given an account, and every transaction it
creates goes into that account instead of the one the profile maps. This
lets a single profile serve several accounts at the same bank, for
statements that only name the account in a header cell or not at all,
instead of keeping one copy of the profile per account.

- ImportRun gets an optional `account` (SET_NULL), shown on the run card.
- The upload form offers the user's active accounts when the profile
  imports transactions, and leaves the field out for profiles that import
  accounts, currencies, categories, tags or entities.
- CSV and Excel rows take the run's account before deduplication, so
  rules comparing `account` check against the picked account. The
  profile's account mapping is skipped for these rows.
- QIF files use the run's account instead of the account named after the
  file.
- The import API accepts an optional `account_id`, limited to accounts
  the user can access, rejected for profiles that don't import
  transactions, and returns `account` on import runs.

Leaving the account empty keeps the current behaviour.

Closes #538
This commit is contained in:
Asish Kumar committed 2026-11-11 22:41:37 +05:30
1 parent 6c3c4c03ec
commit 1ed5dc5d31
9 files changed
+497 -11

No files matched your search

+18 -1
View File
@@ -1,5 +1,7 @@
from django.utils.translation import gettext_lazy as _
from rest_framework import serializers
from apps.accounts.models import Account
from apps.import_app.models import ImportProfile, ImportRun
@@ -21,6 +23,7 @@ class ImportRunSerializer(serializers.ModelSerializer):
"status",
"profile",
"file_name",
"account",
"logs",
"processed_rows",
"total_rows",
@@ -39,8 +42,22 @@ class ImportFileSerializer(serializers.Serializer):
queryset=ImportProfile.objects.all(), source="profile"
)
file = serializers.FileField()
account_id = serializers.PrimaryKeyRelatedField(
queryset=Account.objects.all(),
source="account",
required=False,
allow_null=True,
)
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
# Reload the queryset to get an updated version with the requesting user
# Reload the querysets to get an updated version with the requesting user
self.fields["profile_id"].queryset = ImportProfile.objects.all()
self.fields["account_id"].queryset = Account.objects.all()
def validate(self, data):
if data.get("account") and not data["profile"].imports_transactions:
raise serializers.ValidationError(
{"account_id": _("Only transaction imports can use an account.")}
)
return data
+5 -1
View File
@@ -102,6 +102,7 @@ class ImportRunViewSet(viewsets.ReadOnlyModelViewSet):
"properties": {
"profile_id": {"type": "integer", "description": "ID of the ImportProfile to use"},
"file": {"type": "string", "format": "binary", "description": "CSV or XLSX file to import"},
"account_id": {"type": "integer", "description": "ID of the Account to import every transaction into. Leave empty to let the profile pick the account. Only for profiles that import transactions"},
},
"required": ["profile_id", "file"],
},
@@ -137,7 +138,10 @@ class ImportViewSet(viewsets.ViewSet):
# Create ImportRun record
import_run = ImportRun.objects.create(
profile=profile, file_name=filename, owner=request.user
profile=profile,
file_name=filename,
account=serializer.validated_data.get("account"),
owner=request.user,
)
# Queue import task