Skip to content

Open-source personal finance manager. Self-hosted, privacy-first.

feat(assets): import investment orders from a broker CSV (#646)

Tássio Noronha42976f93a3cc
diff --git a/backend/app/api/assets.py b/backend/app/api/assets.py
index 13645508..7bc76f92 100644
--- a/backend/app/api/assets.py
+++ b/backend/app/api/assets.py
@@ -1,8 +1,10 @@
+import json
import logging
import uuid
from decimal import Decimal
-from fastapi import APIRouter, Depends, HTTPException, Query, status
+from fastapi import APIRouter, Depends, File, Form, HTTPException, Query, UploadFile, status
+from fastapi.responses import Response
from sqlalchemy.ext.asyncio import AsyncSession
from app.core.auth import current_active_user
@@ -17,6 +19,11 @@ from app.providers.market_price import (
MarketPriceRateLimitedError,
get_market_price_provider,
)
+from app.schemas.asset_import import (
+ AssetImportPreview,
+ AssetImportRequest,
+ AssetImportResult,
+)
from app.schemas.asset import (
AssetBuyCreate,
AssetCreate,
@@ -30,7 +37,7 @@ from app.schemas.asset import (
MarketSymbolMatch,
MarketSymbolQuote,
)
-from app.services import asset_service, asset_transaction_service
+from app.services import asset_import_service, asset_service, asset_transaction_service
from app.services.fx_rate_service import convert
logger = logging.getLogger(__name__)
@@ -216,6 +223,98 @@ async def list_workspace_transactions(
)
[email protected]("/import/template")
+async def asset_import_template(
+ ctx: WorkspaceContext = Depends(current_workspace),
+):
+ """A starter CSV, so the first upload is a fill-in rather than a guess."""
+ return Response(
+ content=asset_import_service.csv_template(),
+ media_type="text/csv",
+ headers={"Content-Disposition": 'attachment; filename="securo-asset-orders.csv"'},
+ )
+
+
[email protected]("/import/preview", response_model=AssetImportPreview)
+async def preview_asset_import(
+ file: UploadFile = File(...),
+ column_mapping: str | None = Form(None),
+ date_format: str | None = Form(None),
+ group_id: uuid.UUID | None = Form(None),
+ ctx: WorkspaceContext = Depends(current_workspace),
+ session: AsyncSession = Depends(get_async_session),
+):
+ """Read the file and say what importing it would do. Writes nothing.
+
+ Read-gated on purpose, like the transaction preview: a viewer may look at
+ a file without being able to commit it.
+ """
+ content = await file.read()
+ mapping = None
+ if column_mapping:
+ try:
+ mapping = json.loads(column_mapping)
+ except json.JSONDecodeError:
+ raise HTTPException(status_code=400, detail="column_mapping must be valid JSON")
+
+ try:
+ orders, errors, columns = asset_import_service.parse_orders_csv(
+ content, column_mapping=mapping, date_format=date_format
+ )
+ except ValueError as exc:
+ # Soft failure: hand back the headers so the UI can offer the mapping
+ # dropdowns instead of a dead end.
+ return AssetImportPreview(
+ orders=[],
+ errors=[],
+ csv_columns=asset_import_service.detect_columns(content),
+ parse_error=str(exc),
+ )
+
+ try:
+ summary = await asset_import_service.import_orders(
+ session, ctx.workspace.id, ctx.user_id, orders, group_id=group_id, dry_run=True
+ )
+ except MarketPriceRateLimitedError:
+ raise HTTPException(
+ status_code=status.HTTP_429_TOO_MANY_REQUESTS,
+ detail="Market data provider is currently rate-limiting. Try again in a minute.",
+ )
+
+ # The dry run rejects rows the parser could not know about — an unknown
+ # ticker, a sell with nothing to sell. Drop those from the list too, so the
+ # table, the count and the button all describe the same import.
+ rejected = {e.row for e in summary["errors"]}
+ return AssetImportPreview(
+ orders=[o for o in orders if o.row not in rejected],
+ errors=errors + summary["errors"],
+ warnings=summary["warnings"],
+ csv_columns=columns,
+ holdings_created=summary["holdings_created"],
+ holdings_matched=summary["holdings_matched"],
+ skipped=summary["skipped"],
+ )
+
+
[email protected]("/import", response_model=AssetImportResult)
+async def import_asset_orders(
+ data: AssetImportRequest,
+ ctx: WorkspaceContext = Depends(current_writable_workspace),
+ session: AsyncSession = Depends(get_async_session),
+):
+ """Apply the previewed orders to the workspace's holdings."""
+ try:
+ summary = await asset_import_service.import_orders(
+ session, ctx.workspace.id, ctx.user_id, data.orders, group_id=data.group_id
+ )
+ except MarketPriceRateLimitedError:
+ raise HTTPException(
+ status_code=status.HTTP_429_TOO_MANY_REQUESTS,
+ detail="Market data provider is currently rate-limiting. Try again in a minute.",
+ )
+ return AssetImportResult(**summary)
+
+
@router.post("/buy", response_model=AssetRead, status_code=status.HTTP_201_CREATED)
async def buy_into_holding(
data: AssetBuyCreate,
diff --git a/backend/app/schemas/asset_import.py b/backend/app/schemas/asset_import.py
new file mode 100644
index 00000000..0c64848b
--- /dev/null
+++ b/backend/app/schemas/asset_import.py
@@ -0,0 +1,67 @@
+from datetime import date
+from decimal import Decimal
+from typing import Optional
+from uuid import UUID
+
+from pydantic import BaseModel
+
+
+class AssetOrderImport(BaseModel):
+ """One buy or sell read from the file, before it reaches a holding."""
+
+ row: int # 1-based line in the source file, so an error can point at it
+ ticker: str
+ date: date
+ kind: str # buy | sell
+ quantity: Decimal
+ price: Decimal
+ fee: Decimal = Decimal("0")
+ currency: Optional[str] = None
+ name: Optional[str] = None
+ notes: Optional[str] = None
+ external_id: Optional[str] = None
+
+
+class AssetImportRowError(BaseModel):
+ """A row that will not be imported, and why, named by its line number."""
+
+ row: int
+ reason: str # missing_ticker | invalid_date | invalid_quantity | invalid_price | invalid_kind | unknown_ticker | oversell
+ ticker: Optional[str] = None
+ detail: Optional[str] = None
+
+
+class AssetImportWarning(BaseModel):
+ """Not a reason to refuse a row, but something to see before confirming."""
+
+ ticker: str
+ reason: str # exists_in_other_wallet | orders_already_in_other_wallet
+ wallet: Optional[str] = None
+
+
+class AssetImportPreview(BaseModel):
+ orders: list[AssetOrderImport]
+ errors: list[AssetImportRowError] = []
+ warnings: list[AssetImportWarning] = []
+ csv_columns: list[str] = []
+ #: Set when the file could not be read at all, so the UI can show the
+ #: mapping dropdowns instead of an empty preview.
+ parse_error: Optional[str] = None
+ #: What the import would do, without doing it.
+ holdings_created: int = 0
+ holdings_matched: int = 0
+ skipped: int = 0
+
+
+class AssetImportRequest(BaseModel):
+ orders: list[AssetOrderImport]
+ group_id: Optional[UUID] = None
+
+
+class AssetImportResult(BaseModel):
+ imported: int
+ skipped: int
+ holdings_created: int
+ holdings_matched: int
+ errors: list[AssetImportRowError] = []
+ warnings: list[AssetImportWarning] = []
diff --git a/backend/app/services/asset_import_service.py b/backend/app/services/asset_import_service.py
new file mode 100644
index 00000000..6e0a0208
--- /dev/null
+++ b/backend/app/services/asset_import_service.py
@@ -0,0 +1,581 @@
+"""Import investment orders (buys and sells) from a broker CSV.
+
+A portfolio arrives as a list of orders, not as positions: a hundred rows of
+"ticker, date, quantity, price, fee". Securo already knows how to turn orders
+into a position — `asset_transaction_service._recompute` does the weighted
+average, the fees and the realized gain — so this module's whole job is to get
+the rows out of the file and onto the right holdings.
+
+Two things shape the design:
+
+- **Tickers are resolved once, not per row.** Creating a market-priced holding
+ needs a live quote, and a broker file with 200 rows usually covers 20 or 30
+ tickers. Resolving per row would make 200 provider calls and get rate-limited
+ halfway through, so the distinct tickers are looked up in one batch before
+ anything is written, and the preview reports the ones that came back empty.
+- **The whole file is checked before a single row lands.** A sell of more units
+ than the ledger holds is refused by the ledger itself, and a file that starts
+ mid-history will do exactly that. Failing on row 140 after writing 139 leaves
+ a portfolio that is neither the old one nor the new one, so the run either
+ applies completely or not at all.
+"""
+import csv
+import io
+import uuid
+from datetime import date as date_type
+from datetime import datetime, timezone
+from decimal import Decimal, InvalidOperation
+from typing import Optional
+
+from sqlalchemy import select
+from sqlalchemy.ext.asyncio import AsyncSession
+
+from app.models.asset import Asset
+from app.models.asset_group import AssetGroup
+from app.models.asset_transaction import AssetTransaction
+from app.providers.market_price import MarketPriceProvider, get_market_price_provider
+from app.schemas.asset_import import (
+ AssetImportRowError,
+ AssetImportWarning,
+ AssetOrderImport,
+)
+from app.services import asset_transaction_service
+from app.services.import_service import (
+ DATE_FORMAT_MAP,
+ _sniff_csv_dialect,
+ normalize_amount,
+)
+from app.services.rule_engine import _strip_accents
+
+#: Securo fields a CSV column can be mapped to, and which of them a file cannot
+#: do without. Mirrors the transaction importer's `CSV_MAPPABLE_FIELDS`, and
+#: drives both the mapping dropdowns and the downloadable template.
+ASSET_CSV_MAPPABLE_FIELDS = (
+ 'ticker', 'date', 'quantity', 'price', 'fee', 'kind', 'currency', 'name', 'notes', 'external_id',
+)
+ASSET_CSV_REQUIRED_FIELDS = ('ticker', 'date', 'quantity', 'price')
+
+#: Header names brokers actually use, matched case- and accent-insensitively
+#: after normalization. A file whose headers are recognised needs no mapping
+#: step at all; anything else falls through to the dropdowns.
+_COLUMN_CANDIDATES: dict[str, tuple[str, ...]] = {
+ # One entry per language Securo is translated into, because a broker
+ # export is written in the language of the person who downloaded it.
+ # Diacritics are folded before matching, so `Preço` finds `preco`; the
+ # Cyrillic and Polish entries are spelled as they actually appear.
+ 'ticker': (
+ 'ticker', 'symbol', 'code', 'isin',
+ 'simbolo', 'ativo', 'papel', 'codigo', # pt
+ 'activo', 'valor', # es
+ 'symbole', 'titre', 'actif', # fr
+ 'wertpapier', 'kuerzel', 'kurzel', 'wkn', # de
+ 'titolo', 'strumento', # it
+ 'walor', 'instrument', # pl
+ 'тикер', 'символ', 'бумага', # ru
+ 'тікер', 'папір', # uk
+ ),
+ 'date': (
+ 'date', 'trade date', 'settlement date',
+ 'data', 'data do negocio', 'data negocio', # pt
+ 'fecha', 'fecha operacion', # es
+ 'date de transaction', 'date operation', # fr
+ 'datum', 'handelstag', 'buchungstag', # de
+ 'data operazione', # it
+ 'data transakcji', # pl
+ 'дата', 'дата сделки', # ru
+ 'дата операції', # uk
+ ),
+ 'quantity': (
+ 'quantity', 'qty', 'shares', 'units', 'amount of shares',
+ 'quantidade', # pt
+ 'cantidad', 'titulos', # es
+ 'quantite', 'nombre', 'titres', # fr
+ 'menge', 'stueck', 'stuck', 'anzahl', 'stueckzahl', # de
+ 'quantita', 'numero', # it
+ 'ilosc', 'ilość', 'liczba', 'wolumen', # pl
+ 'количество', 'кол-во', 'объем', # ru
+ 'кількість', 'обсяг', # uk
+ ),
+ 'price': (
+ 'price', 'unit price', 'price per share',
+ 'preco', 'preco unitario', 'valor unitario', # pt
+ 'precio', 'precio unitario', 'cotizacion', # es
+ 'prix', 'cours', 'prix unitaire', # fr
+ 'preis', 'kurs', 'stueckpreis', # de
+ 'prezzo', 'prezzo unitario', 'quotazione', # it
+ 'cena', 'cena jednostkowa', # pl
+ 'цена', 'курс', # ru
+ 'ціна', # uk
+ ),
+ 'fee': (
+ 'fee', 'fees', 'commission', 'costs',
+ 'taxa', 'taxas', 'corretagem', 'custos', # pt
+ 'comision', 'comisiones', 'gastos', # es
+ 'frais', 'courtage', # fr
+ 'gebuehr', 'gebuhr', 'gebuehren', 'provision', 'kosten', # de
+ 'commissione', 'commissioni', 'spese', # it
+ 'prowizja', 'oplata', 'opłata', 'koszty', # pl
+ 'комиссия', 'сбор', # ru
+ 'комісія', 'збір', # uk
+ ),
+ 'kind': (
+ 'kind', 'type', 'side', 'operation', 'buy/sell',
+ 'tipo', 'operacao', 'c/v', # pt
+ 'operacion', 'compra/venta', 'sentido', # es
+ 'sens', 'achat/vente', # fr
+ 'art', 'richtung', 'kauf/verkauf', 'transaktionsart', # de
+ 'operazione', 'segno', 'acquisto/vendita', # it
+ 'rodzaj', 'operacja', 'kupno/sprzedaz', 'strona', # pl
+ 'операция', 'направление', 'покупка/продажа', # ru
+ 'операція', 'купівля/продаж', # uk
+ ),
+ 'currency': (
+ 'currency', 'ccy', 'moeda', 'moneda', 'divisa', 'devise', 'monnaie',
+ 'waehrung', 'wahrung', 'valuta', 'waluta', 'валюта',
+ ),
+ 'name': (
+ 'name', 'description', 'security',
+ 'nome', 'descricao', 'nombre', 'descripcion', 'nom', 'libelle',
+ 'bezeichnung', 'beschreibung', 'descrizione', 'nazwa', 'opis',
+ 'название', 'наименование', 'назва',
+ ),
+ 'notes': (
+ 'notes', 'note', 'observacao', 'observacoes', 'obs', 'observaciones',
+ 'remarques', 'notizen', 'bemerkung', 'notatki', 'uwagi',
+ 'заметки', 'примечание', 'нотатки', 'примітки',
+ ),
+ 'external_id': ('external_id', 'id', 'order id', 'trade id', 'reference'),
+}
+
+#: Values that mean "this row is a sale". Everything else is read as a buy,
+#: except a negative quantity, which is the convention most brokers export.
+_SELL_WORDS = {
+ 'sell', 'sale', 'sold', 's',
+ 'venda', 'vender', 'saida', 'v', # pt
+ 'venta', # es
+ 'vendre', 'vente', # fr
+ 'verkauf', 'verkaufen', 'vk', # de
+ 'vendita', 'vendere', # it
+ 'sprzedaz', 'sprzedaż', 'sprzedac', # pl
+ 'продажа', 'продать', 'продаж', 'продати', # ru/uk
+}
+_BUY_WORDS = {
+ 'buy', 'purchase', 'bought', 'b',
+ 'compra', 'comprar', 'entrada', 'c', # pt/es
+ 'achat', 'acheter', # fr
+ 'kauf', 'kaufen', 'kf', # de
+ 'acquisto', 'acquistare', # it
+ 'kupno', 'zakup', 'kupic', 'kupić', # pl
+ 'покупка', 'купить', 'купівля', 'купити', # ru/uk
+}
+
+
+def _decode(content: bytes) -> str:
+ """Broker exports are not always UTF-8; fall back rather than blow up."""
+ for encoding in ('utf-8-sig', 'latin-1'):
+ try:
+ return content.decode(encoding)
+ except UnicodeDecodeError:
+ continue
+ return content.decode('utf-8', errors='replace')
+
+
+def detect_columns(content: bytes) -> list[str]:
+ """The file's header names, as written, for the mapping dropdowns."""
+ text = _decode(content)
+ reader = csv.DictReader(io.StringIO(text), dialect=_sniff_csv_dialect(text))
+ return [f.strip() for f in (reader.fieldnames or []) if f and f.strip()]
+
+
+def _normalize_header(value: str) -> str:
+ """Fold a header (or a buy/sell word) to its comparable form.
+
+ Accents come off because a Brazilian export writes `Preço` and `Operação`,
+ and a header that only differs by a diacritic is the same header.
+ """
+ folded = _strip_accents(value.strip().lower().replace('_', ' '))
+ return ' '.join(folded.split())
+
+
+def _auto_mapping(headers: list[str]) -> dict[str, str]:
+ """Guess which column is which, so a recognisable file needs no mapping."""
+ normalized = {_normalize_header(h): h for h in headers}
+ mapping: dict[str, str] = {}
+ for field, candidates in _COLUMN_CANDIDATES.items():
+ for candidate in candidates:
+ if candidate in normalized:
+ mapping[field] = normalized[candidate]
+ break
+ return mapping
+
+
+def _parse_date(raw: str, date_format: Optional[str]) -> Optional[date_type]:
+ raw = raw.strip()
+ if not raw:
+ return None
+ formats = []
+ if date_format and date_format in DATE_FORMAT_MAP:
+ formats.append(DATE_FORMAT_MAP[date_format])
+ formats.extend(['%Y-%m-%d', '%d/%m/%Y', '%m/%d/%Y', '%d-%m-%Y', '%Y/%m/%d', '%d.%m.%Y'])
+ for fmt in formats:
+ try:
+ return datetime.strptime(raw[:10] if len(raw) > 10 and fmt == '%Y-%m-%d' else raw, fmt).date()
+ except ValueError:
+ continue
+ return None
+
+
+def _parse_decimal(raw: str) -> Optional[Decimal]:
+ raw = (raw or '').strip()
+ if not raw:
+ return None
+ try:
+ return Decimal(str(normalize_amount(raw)))
+ except (InvalidOperation, ValueError, TypeError):
+ return None
+
+
+def parse_orders_csv(
+ content: bytes,
+ column_mapping: Optional[dict[str, str]] = None,
+ date_format: Optional[str] = None,
+) -> tuple[list[AssetOrderImport], list[AssetImportRowError], list[str]]:
+ """Read a broker CSV into orders, plus one error per row that could not be read.
+
+ Bad rows are reported rather than skipped in silence: a file where a third
+ of the rows had an unreadable date should say so before anything is
+ imported, not quietly bring in the other two thirds.
+ """
+ text = _decode(content)
+ dialect = _sniff_csv_dialect(text)
+ reader = csv.DictReader(io.StringIO(text), dialect=dialect)
+ headers = [f.strip() for f in (reader.fieldnames or []) if f and f.strip()]
+ if not headers:
+ raise ValueError('CSV has no header row')
+
+ mapping = {k: v for k, v in (column_mapping or {}).items() if v}
+ for field, header in _auto_mapping(headers).items():
+ mapping.setdefault(field, header)
+
+ missing = [f for f in ASSET_CSV_REQUIRED_FIELDS if f not in mapping]
+ if missing:
+ raise ValueError(f"Missing required column mapping: {', '.join(missing)}")
+
+ def cell(row: dict, field: str) -> str:
+ header = mapping.get(field)
+ if not header:
+ return ''
+ return (row.get(header) or '').strip()
+
+ orders: list[AssetOrderImport] = []
+ errors: list[AssetImportRowError] = []
+
+ for index, row in enumerate(reader, start=2): # row 1 is the header
+ if not any((v or '').strip() for v in row.values()):
+ continue
+
+ ticker = cell(row, 'ticker').upper()
+ if not ticker:
+ errors.append(AssetImportRowError(row=index, reason='missing_ticker'))
+ continue
+
+ order_date = _parse_date(cell(row, 'date'), date_format)
+ if order_date is None:
+ errors.append(AssetImportRowError(row=index, reason='invalid_date', ticker=ticker))
+ continue
+
+ quantity = _parse_decimal(cell(row, 'quantity'))
+ if quantity is None or quantity == 0:
+ errors.append(AssetImportRowError(row=index, reason='invalid_quantity', ticker=ticker))
+ continue
+
+ price = _parse_decimal(cell(row, 'price'))
+ if price is None or price < 0:
+ errors.append(AssetImportRowError(row=index, reason='invalid_price', ticker=ticker))
+ continue
+
+ # Buy or sell comes from an explicit column when the file has one, and
+ # from the sign of the quantity otherwise — the convention brokers use.
+ kind_word = _normalize_header(cell(row, 'kind'))
+ if kind_word in _SELL_WORDS:
+ kind = 'sell'
+ elif kind_word in _BUY_WORDS:
+ kind = 'buy'
+ elif kind_word:
+ errors.append(AssetImportRowError(row=index, reason='invalid_kind', ticker=ticker))
+ continue
+ else:
+ kind = 'sell' if quantity < 0 else 'buy'
+
+ orders.append(AssetOrderImport(
+ row=index,
+ ticker=ticker,
+ date=order_date,
+ kind=kind,
+ quantity=abs(quantity),
+ price=price,
+ fee=_parse_decimal(cell(row, 'fee')) or Decimal('0'),
+ currency=(cell(row, 'currency') or None),
+ name=(cell(row, 'name') or None),
+ notes=(cell(row, 'notes') or None),
+ external_id=(cell(row, 'external_id') or None),
+ ))
+
+ return orders, errors, headers
+
+
+async def resolve_tickers(
+ tickers: list[str],
+ *,
+ market_provider: Optional[MarketPriceProvider] = None,
+) -> dict[str, bool]:
+ """Which of these tickers the price provider recognises.
+
+ One batch call for the whole file. A ticker the provider does not know can
+ still be imported onto a holding that already exists in the workspace; it
+ is only a problem when the holding would have to be created.
+ """
+ provider = market_provider or get_market_price_provider()
+ unique = sorted({t.upper() for t in tickers if t})
+ if not unique:
+ return {}
+ try:
+ prices = await provider.get_latest_prices(unique)
+ except Exception: # provider down or rate-limited: nothing is resolvable
+ return {t: False for t in unique}
+ return {t: prices.get(t) is not None for t in unique}
+
+
+async def _existing_holdings(
+ session: AsyncSession,
+ workspace_id: uuid.UUID,
+ group_id: Optional[uuid.UUID],
+ tickers: list[str],
+) -> dict[str, Asset]:
+ if not tickers:
+ return {}
+ result = await session.execute(
+ select(Asset).where(
+ Asset.workspace_id == workspace_id,
+ Asset.valuation_method == 'market_price',
+ Asset.group_id == group_id,
+ Asset.ticker.in_(sorted({t.upper() for t in tickers})),
+ )
+ )
+ return {a.ticker.upper(): a for a in result.scalars().all() if a.ticker}
+
+
+async def _holdings_in_other_wallets(
+ session: AsyncSession,
+ workspace_id: uuid.UUID,
+ group_id: Optional[uuid.UUID],
+ tickers: list[str],
+) -> dict[str, tuple[Asset, Optional[str]]]:
+ """The same tickers held under a *different* wallet, with that wallet's name.
+
+ Holdings are scoped per wallet, so importing AAPL into one wallet while
+ AAPL already sits in another is a legitimate thing to do — two brokers,
+ two positions. It is also exactly what a mis-picked wallet looks like, and
+ the portfolio then counts the same shares twice, so the preview says it out
+ loud instead of leaving it to be noticed later.
+ """
+ if not tickers:
+ return {}
+ result = await session.execute(
+ select(Asset, AssetGroup.name)
+ .outerjoin(AssetGroup, AssetGroup.id == Asset.group_id)
+ .where(
+ Asset.workspace_id == workspace_id,
+ Asset.valuation_method == 'market_price',
+ Asset.group_id.is_not(None) if group_id is None else Asset.group_id != group_id,
+ Asset.ticker.in_(sorted({t.upper() for t in tickers})),
+ )
+ )
+ return {asset.ticker.upper(): (asset, name) for asset, name in result.all() if asset.ticker}
+
+
+async def _already_imported(
+ session: AsyncSession,
+ asset_ids: list[uuid.UUID],
+) -> set[tuple]:
+ """Fingerprints of the ledger rows these holdings already carry.
+
+ Re-uploading the same file is the normal way people fix a mapping mistake,
+ so a repeat run should add nothing rather than double the position.
+ """
+ if not asset_ids:
+ return set()
+ result = await session.execute(
+ select(AssetTransaction).where(AssetTransaction.asset_id.in_(asset_ids))
+ )
+ seen = set()
+ for tx in result.scalars().all():
+ if tx.external_id:
+ seen.add(('external', tx.asset_id, tx.external_id))
+ seen.add(('row', tx.asset_id, tx.date, tx.kind, tx.quantity, tx.price))
+ return seen
+
+
+async def import_orders(
+ session: AsyncSession,
+ workspace_id: uuid.UUID,
+ user_id: uuid.UUID,
+ orders: list[AssetOrderImport],
+ *,
+ group_id: Optional[uuid.UUID] = None,
+ dry_run: bool = False,
+ market_provider: Optional[MarketPriceProvider] = None,
+) -> dict:
+ """Apply a file of orders to the workspace's holdings.
+
+ Returns the counts the UI reports, and — on a dry run — the same numbers
+ without writing anything, so the preview can promise what the import will
+ do instead of guessing.
+ """
+ ordered = sorted(orders, key=lambda o: (o.date, o.row))
+ tickers = [o.ticker for o in ordered]
+
+ holdings = await _existing_holdings(session, workspace_id, group_id, tickers)
+ seen = await _already_imported(session, [a.id for a in holdings.values()])
+
+ elsewhere = await _holdings_in_other_wallets(session, workspace_id, group_id, tickers)
+ warnings: list[AssetImportWarning] = []
+ if elsewhere:
+ seen_elsewhere = await _already_imported(session, [a.id for a, _ in elsewhere.values()])
+ for ticker, (other, wallet_name) in sorted(elsewhere.items()):
+ # Same orders already on the other holding is the strong signal:
+ # this is the same position about to be counted twice.
+ duplicated = any(
+ ('row', other.id, o.date, o.kind, o.quantity, o.price) in seen_elsewhere
+ for o in ordered if o.ticker == ticker
+ )
+ warnings.append(AssetImportWarning(
+ ticker=ticker,
+ reason='orders_already_in_other_wallet' if duplicated else 'exists_in_other_wallet',
+ wallet=wallet_name,
+ ))
+
+ missing_tickers = sorted({t for t in tickers if t not in holdings})
+ resolvable = await resolve_tickers(missing_tickers, market_provider=market_provider) if missing_tickers else {}
+
+ errors: list[AssetImportRowError] = []
+ accepted: list[AssetOrderImport] = []
+ skipped = 0
+
+ # Units per ticker as the file is replayed, so a sell that would leave the
+ # position negative is caught here rather than by the ledger halfway
+ # through the write.
+ units: dict[str, Decimal] = {
+ ticker: Decimal(str(asset.units or 0)) for ticker, asset in holdings.items()
+ }
+
+ for order in ordered:
+ if order.ticker not in holdings and not resolvable.get(order.ticker, False):
+ errors.append(AssetImportRowError(row=order.row, reason='unknown_ticker', ticker=order.ticker))
+ continue
+
+ asset = holdings.get(order.ticker)
+ if asset is not None:
+ fingerprint = ('row', asset.id, order.date, order.kind, order.quantity, order.price)
+ external = ('external', asset.id, order.external_id) if order.external_id else None
+ if fingerprint in seen or (external and external in seen):
+ skipped += 1
+ continue
+
+ held = units.get(order.ticker, Decimal('0'))
+ if order.kind == 'sell' and order.quantity > held:
+ errors.append(AssetImportRowError(
+ row=order.row, reason='oversell', ticker=order.ticker,
+ detail=f'selling {order.quantity} with {held} held',
+ ))
+ continue
+
+ units[order.ticker] = held + (order.quantity if order.kind == 'buy' else -order.quantity)
+ accepted.append(order)
+
+ to_create = sorted({o.ticker for o in accepted if o.ticker not in holdings})
+ summary = {
+ 'imported': len(accepted),
+ 'skipped': skipped,
+ 'holdings_created': len(to_create),
+ 'holdings_matched': len({o.ticker for o in accepted if o.ticker in holdings}),
+ 'errors': errors,
+ 'warnings': warnings,
+ }
+ if dry_run or not accepted:
+ return summary
+
+ quotes = {}
+ if to_create:
+ provider = market_provider or get_market_price_provider()
+ quotes = await provider.get_quotes(to_create)
+
+ touched: dict[str, Asset] = {}
+ written = 0
+ for order in accepted:
+ asset = holdings.get(order.ticker)
+ if asset is None:
+ quote = quotes.get(order.ticker)
+ if quote is None:
+ # Resolvable a moment ago in the batch check, gone now. Report
+ # it rather than inventing a holding with no price.
+ errors.append(AssetImportRowError(row=order.row, reason='unknown_ticker', ticker=order.ticker))
+ continue
+ asset = Asset(
+ user_id=user_id,
+ workspace_id=workspace_id,
+ name=order.name or quote.name or order.ticker,
+ type=asset_transaction_service._type_from_quote(quote.quote_type),
+ # The quote's currency wins, exactly as when a holding is
+ # created by hand: a file that reports an American stock in
+ # BRL would otherwise label the holding BRL while its price
+ # feed keeps returning USD, and the portfolio total drifts.
+ currency=quote.currency,
+ valuation_method='market_price',
+ group_id=group_id,
+ ticker=order.ticker,
+ ticker_exchange=quote.exchange,
+ last_price=Decimal(str(quote.price)),
+ last_price_at=datetime.now(timezone.utc),
+ logo_url=quote.logo_url,
+ source='yfinance',
+ )
+ session.add(asset)
+ await session.flush()
+ holdings[order.ticker] = asset
+
+ session.add(AssetTransaction(
+ asset_id=asset.id,
+ workspace_id=workspace_id,
+ kind=order.kind,
+ quantity=order.quantity,
+ price=order.price,
+ fee=order.fee or Decimal('0'),
+ date=order.date,
+ source='import',
+ external_id=order.external_id,
+ notes=order.notes,
+ ))
+ touched[order.ticker] = asset
+ written += 1
+
+ await session.flush()
+ # Once per holding, not once per row: the recompute walks the whole ledger.
+ for asset in touched.values():
+ await asset_transaction_service.recompute_and_cache(session, asset)
+ await session.commit()
+
+ summary['errors'] = errors
+ summary['imported'] = written
+ summary['holdings_created'] = len([t for t in to_create if t in touched])
+ return summary
+
+
+def csv_template() -> str:
+ """A file someone can fill in, with the required columns marked."""
+ return (
+ 'ticker*,date*,quantity*,price*,fee,kind,currency,notes\n'
+ 'AAPL,2026-01-15,10,150.00,1.20,buy,USD,\n'
+ 'AAPL,2026-03-02,-4,178.30,1.20,sell,USD,partial exit\n'
+ 'PETR4.SA,2026-02-10,100,38.50,2.90,buy,BRL,\n'
+ )
diff --git a/backend/tests/test_asset_import_api.py b/backend/tests/test_asset_import_api.py
new file mode 100644
index 00000000..962f5e44
--- /dev/null
+++ b/backend/tests/test_asset_import_api.py
@@ -0,0 +1,156 @@
+"""HTTP surface of the asset-order importer: template, preview, commit, auth."""
+import json
+from decimal import Decimal
+from typing import Optional
+
+import pytest
+import pytest_asyncio
+from httpx import AsyncClient
+
+from app.providers.market_price import (
+ MarketPriceProvider,
+ MarketSymbolQuote,
+ set_market_price_provider,
+)
+
+
+class StubProvider(MarketPriceProvider):
+ name = "stub"
+
+ async def search(self, query: str, limit: int = 20):
+ return []
+
+ async def get_quote(self, symbol: str) -> Optional[MarketSymbolQuote]:
+ if symbol.upper() != "AAPL":
+ return None
+ return MarketSymbolQuote(
+ symbol="AAPL", name="Apple Inc", exchange="NASDAQ",
+ currency="USD", price=180.0, quote_type="EQUITY",
+ )
+
+ async def get_latest_prices(self, symbols: list[str]) -> dict[str, Optional[Decimal]]:
+ return {s.upper(): (Decimal("180") if s.upper() == "AAPL" else None) for s in symbols}
+
+
+@pytest_asyncio.fixture(autouse=True)
+async def stub_provider():
+ set_market_price_provider(StubProvider())
+ yield
+ set_market_price_provider(None)
+
+
+CSV = b"ticker,date,quantity,price,fee\nAAPL,2026-01-15,10,150.00,1.20\nAAPL,2026-02-15,-4,180.00,1.20\n"
+
+
+async def test_template_is_downloadable(client: AsyncClient, auth_headers):
+ resp = await client.get("/api/assets/import/template", headers=auth_headers)
+ assert resp.status_code == 200
+ assert "attachment" in resp.headers["content-disposition"]
+ assert resp.text.splitlines()[0].startswith("ticker*,date*,quantity*,price*")
+
+
+async def test_preview_reports_what_the_import_would_do(client: AsyncClient, auth_headers):
+ resp = await client.post(
+ "/api/assets/import/preview",
+ files={"file": ("orders.csv", CSV, "text/csv")},
+ headers=auth_headers,
+ )
+ assert resp.status_code == 200
+ body = resp.json()
+ assert len(body["orders"]) == 2
+ assert body["holdings_created"] == 1
+ assert body["errors"] == []
+ assert body["csv_columns"] == ["ticker", "date", "quantity", "price", "fee"]
+
+
+async def test_preview_writes_nothing(client: AsyncClient, auth_headers):
+ await client.post(
+ "/api/assets/import/preview",
+ files={"file": ("orders.csv", CSV, "text/csv")},
+ headers=auth_headers,
+ )
+ assets = await client.get("/api/assets", headers=auth_headers)
+ assert assets.json() == []
+
+
+async def test_unmappable_file_returns_the_headers_for_the_mapping_step(
+ client: AsyncClient, auth_headers
+):
+ """A soft failure, like the transaction preview: the UI needs the columns."""
+ resp = await client.post(
+ "/api/assets/import/preview",
+ files={"file": ("orders.csv", b"col_a,col_b\nAAPL,10\n", "text/csv")},
+ headers=auth_headers,
+ )
+ assert resp.status_code == 200
+ body = resp.json()
+ assert body["parse_error"]
+ assert body["csv_columns"] == ["col_a", "col_b"]
+ assert body["orders"] == []
+
+
+async def test_preview_honours_an_explicit_mapping(client: AsyncClient, auth_headers):
+ resp = await client.post(
+ "/api/assets/import/preview",
+ files={"file": ("orders.csv", b"a,b,c,d\nAAPL,2026-01-15,10,150.00\n", "text/csv")},
+ data={"column_mapping": json.dumps({"ticker": "a", "date": "b", "quantity": "c", "price": "d"})},
+ headers=auth_headers,
+ )
+ assert resp.status_code == 200
+ assert len(resp.json()["orders"]) == 1
+
+
+async def test_import_creates_the_holding(client: AsyncClient, auth_headers):
+ preview = await client.post(
+ "/api/assets/import/preview",
+ files={"file": ("orders.csv", CSV, "text/csv")},
+ headers=auth_headers,
+ )
+ resp = await client.post(
+ "/api/assets/import",
+ json={"orders": preview.json()["orders"]},
+ headers=auth_headers,
+ )
+ assert resp.status_code == 200
+ assert resp.json()["imported"] == 2
+ assert resp.json()["holdings_created"] == 1
+
+ assets = (await client.get("/api/assets", headers=auth_headers)).json()
+ assert [a["ticker"] for a in assets] == ["AAPL"]
+ assert Decimal(str(assets[0]["units"])) == Decimal("6") # 10 bought, 4 sold
+
+
+async def test_import_requires_authentication(client: AsyncClient):
+ resp = await client.post("/api/assets/import", json={"orders": []})
+ assert resp.status_code == 401
+
+
+async def test_preview_requires_authentication(client: AsyncClient):
+ resp = await client.post(
+ "/api/assets/import/preview",
+ files={"file": ("orders.csv", CSV, "text/csv")},
+ )
+ assert resp.status_code == 401
+
+
+async def test_preview_lists_only_the_rows_it_will_import(client: AsyncClient, auth_headers):
+ """A row the dry run rejects must not sit in the table above a button that
+ promises to import it."""
+ csv = b"ticker,date,quantity,price\nAAPL,2026-01-15,10,150.00\nNOSUCH,2026-01-16,5,10.00\n"
+ resp = await client.post(
+ "/api/assets/import/preview",
+ files={"file": ("orders.csv", csv, "text/csv")},
+ headers=auth_headers,
+ )
+ body = resp.json()
+ assert [o["ticker"] for o in body["orders"]] == ["AAPL"]
+ assert [(e["row"], e["reason"]) for e in body["errors"]] == [(3, "unknown_ticker")]
diff --git a/backend/tests/test_asset_import_service.py b/backend/tests/test_asset_import_service.py
new file mode 100644
index 00000000..ec58a895
--- /dev/null
+++ b/backend/tests/test_asset_import_service.py
@@ -0,0 +1,375 @@
+"""Importing broker orders: reading the file, and applying it to holdings."""
+import uuid
+from decimal import Decimal
+
+import pytest
+from sqlalchemy import select
+from sqlalchemy.ext.asyncio import AsyncSession
+
+from app.models.asset import Asset
+from app.models.asset_transaction import AssetTransaction
+from app.models.user import User
+from app.models.workspace import Workspace
+from app.providers.market_price import MarketSymbolQuote
+from app.services import asset_import_service
+
+
+class FakeProvider:
+ """Knows two tickers, and counts calls so the batching can be asserted."""
+
+ def __init__(self, known=("AAPL", "PETR4.SA")):
+ self.known = {t.upper() for t in known}
+ self.latest_price_calls = 0
+ self.quote_calls = 0
+
+ async def get_latest_prices(self, symbols):
+ self.latest_price_calls += 1
+ return {s.upper(): (Decimal("100") if s.upper() in self.known else None) for s in symbols}
+
+ async def get_quote(self, symbol):
+ self.quote_calls += 1
+ if symbol.upper() not in self.known:
+ return None
+ return MarketSymbolQuote(
+ symbol=symbol.upper(), name=f"{symbol.upper()} Inc", price=100.0,
+ currency="USD", exchange="XNAS", quote_type="EQUITY", logo_url=None,
+ )
+
+ async def get_quotes(self, symbols):
+ return {s.upper(): await self.get_quote(s) for s in symbols}
+
+
+def _csv(*rows: str) -> bytes:
+ return "\n".join(rows).encode("utf-8")
+
+
+async def _only_asset(session: AsyncSession, workspace: Workspace) -> Asset:
+ """The single holding the import created, asserted rather than assumed."""
+ result = await session.execute(select(Asset).where(Asset.workspace_id == workspace.id))
+ assets = result.scalars().all()
+ assert len(assets) == 1, f"expected one holding, found {len(assets)}"
+ return assets[0]
+
+
+# ---------------------------------------------------------------------------
+# Reading the file
+# ---------------------------------------------------------------------------
+
+
+def test_headers_are_recognised_without_a_mapping():
+ orders, errors, columns = asset_import_service.parse_orders_csv(_csv(
+ "ticker,date,quantity,price,fee",
+ "AAPL,2026-01-15,10,150.00,1.20",
+ ))
+ assert errors == []
+ assert columns == ["ticker", "date", "quantity", "price", "fee"]
+ assert (orders[0].ticker, orders[0].kind, orders[0].quantity) == ("AAPL", "buy", Decimal("10"))
+ assert orders[0].fee == Decimal("1.20")
+
+
+def test_portuguese_broker_headers_are_recognised():
+ """A Brazilian export names its columns in Portuguese and prices with commas."""
+ orders, errors, _ = asset_import_service.parse_orders_csv(_csv(
+ "Ativo;Data;Quantidade;Preço;Corretagem",
+ "PETR4.SA;10/02/2026;100;38,50;2,90",
+ ))
+ assert errors == []
+ assert orders[0].ticker == "PETR4.SA"
+ assert orders[0].price == Decimal("38.50")
+ assert orders[0].fee == Decimal("2.90")
+ assert str(orders[0].date) == "2026-02-10"
+
+
+def test_negative_quantity_reads_as_a_sale():
+ """The convention most broker exports use for a sale."""
+ orders, _, _ = asset_import_service.parse_orders_csv(_csv(
+ "ticker,date,quantity,price",
+ "AAPL,2026-03-02,-4,178.30",
+ ))
+ assert orders[0].kind == "sell"
+ assert orders[0].quantity == Decimal("4") # stored unsigned
+
+
+def test_explicit_kind_column_wins_over_the_sign():
+ orders, _, _ = asset_import_service.parse_orders_csv(_csv(
+ "ticker,date,quantity,price,tipo",
+ "AAPL,2026-03-02,4,178.30,venda",
+ ))
+ assert orders[0].kind == "sell"
+
+
+def test_unreadable_rows_are_reported_not_silently_dropped():
+ orders, errors, _ = asset_import_service.parse_orders_csv(_csv(
+ "ticker,date,quantity,price",
+ "AAPL,2026-01-15,10,150.00",
+ ",2026-01-15,10,150.00",
+ "MSFT,not-a-date,10,150.00",
+ "MSFT,2026-01-15,zero,150.00",
+ ))
+ assert len(orders) == 1
+ assert [(e.row, e.reason) for e in errors] == [
+ (3, "missing_ticker"), (4, "invalid_date"), (5, "invalid_quantity"),
+ ]
+
+
+def test_latin1_file_does_not_blow_up():
+ """Broker exports are not always UTF-8; the transaction importer raises here."""
+ content = "ticker,date,quantity,price,name\nAAPL,2026-01-15,10,150.00,Ação\n".encode("latin-1")
+ orders, errors, _ = asset_import_service.parse_orders_csv(content)
+ assert errors == []
+ assert orders[0].ticker == "AAPL"
+
+
+def test_missing_required_column_is_a_parse_error():
+ with pytest.raises(ValueError, match="quantity"):
+ asset_import_service.parse_orders_csv(_csv("ticker,date,price", "AAPL,2026-01-15,150.00"))
+
+
+def test_explicit_mapping_overrides_the_guess():
+ orders, errors, _ = asset_import_service.parse_orders_csv(
+ _csv("col_a,col_b,col_c,col_d", "AAPL,2026-01-15,10,150.00"),
+ column_mapping={"ticker": "col_a", "date": "col_b", "quantity": "col_c", "price": "col_d"},
+ )
+ assert errors == []
+ assert orders[0].ticker == "AAPL"
+
+
+# ---------------------------------------------------------------------------
+# Applying it
+# ---------------------------------------------------------------------------
+
+
+def provider():
+ return FakeProvider()
+
+
+async def _import(session, workspace, user, csv_bytes, provider, **kwargs):
+ orders, _, _ = asset_import_service.parse_orders_csv(csv_bytes)
+ return await asset_import_service.import_orders(
+ session, workspace.id, user.id, orders, market_provider=provider, **kwargs
+ )
+
+
+async def test_import_creates_the_holding_and_the_position(
+ session: AsyncSession, test_user: User, test_workspace: Workspace, provider
+):
+ summary = await _import(session, test_workspace, test_user, _csv(
+ "ticker,date,quantity,price,fee",
+ "AAPL,2026-01-15,10,100.00,0",
+ "AAPL,2026-02-15,10,200.00,0",
+ ), provider)
+
+ assert summary["imported"] == 2
+ assert summary["holdings_created"] == 1
+
+ stored = await _only_asset(session, test_workspace)
+ assert stored.ticker == "AAPL"
+ assert stored.units == Decimal("20")
+ assert stored.average_price == Decimal("150") # the math the ledger already had
+
+
+async def test_distinct_tickers_are_resolved_in_one_call(
+ session: AsyncSession, test_user: User, test_workspace: Workspace, provider
+):
+ """200 rows over 2 tickers must not mean 200 provider calls."""
+ rows = ["ticker,date,quantity,price"]
+ for i in range(1, 51):
+ rows.append(f"AAPL,2026-01-{i % 28 + 1:02d},1,100.00")
+ rows.append(f"PETR4.SA,2026-01-{i % 28 + 1:02d},1,30.00")
+ await _import(session, test_workspace, test_user, _csv(*rows), provider)
+ assert provider.latest_price_calls == 1
+
+
+async def test_unknown_ticker_is_refused_with_its_row(
+ session: AsyncSession, test_user: User, test_workspace: Workspace, provider
+):
+ summary = await _import(session, test_workspace, test_user, _csv(
+ "ticker,date,quantity,price",
+ "AAPL,2026-01-15,10,100.00",
+ "NOSUCH,2026-01-16,5,10.00",
+ ), provider)
+
+ assert summary["imported"] == 1
+ assert [(e.row, e.reason, e.ticker) for e in summary["errors"]] == [(3, "unknown_ticker", "NOSUCH")]
+
+
+async def test_a_sell_beyond_the_position_is_caught_before_anything_is_written(
+ session: AsyncSession, test_user: User, test_workspace: Workspace, provider
+):
+ """A file that starts mid-history would otherwise fail halfway through."""
+ summary = await _import(session, test_workspace, test_user, _csv(
+ "ticker,date,quantity,price",
+ "AAPL,2026-01-15,5,100.00",
+ "AAPL,2026-02-15,-9,120.00",
+ ), provider)
+
+ assert summary["imported"] == 1
+ assert [(e.row, e.reason) for e in summary["errors"]] == [(3, "oversell")]
+ assert (await _only_asset(session, test_workspace)).units == Decimal("5")
+
+
+async def test_rows_are_replayed_in_date_order_not_file_order(
+ session: AsyncSession, test_user: User, test_workspace: Workspace, provider
+):
+ """Brokers export newest-first; a sell listed above its buy is still valid."""
+ summary = await _import(session, test_workspace, test_user, _csv(
+ "ticker,date,quantity,price",
+ "AAPL,2026-02-15,-4,120.00",
+ "AAPL,2026-01-15,10,100.00",
+ ), provider)
+
+ assert summary["imported"] == 2
+ assert summary["errors"] == []
+
+
+async def test_reimporting_the_same_file_adds_nothing(
+ session: AsyncSession, test_user: User, test_workspace: Workspace, provider
+):
+ """Fixing a mapping and re-uploading must not double the position."""
+ content = _csv("ticker,date,quantity,price", "AAPL,2026-01-15,10,100.00")
+ await _import(session, test_workspace, test_user, content, provider)
+ summary = await _import(session, test_workspace, test_user, content, provider)
+
+ assert summary["imported"] == 0
+ assert summary["skipped"] == 1
+ rows = (await session.execute(AssetTransaction.__table__.select())).all()
+ assert len(rows) == 1
+
+
+async def test_orders_land_on_an_existing_holding(
+ session: AsyncSession, test_user: User, test_workspace: Workspace, provider
+):
+ existing = Asset(
+ id=uuid.uuid4(), user_id=test_user.id, workspace_id=test_workspace.id,
+ name="Apple", type="stock", currency="USD", valuation_method="market_price",
+ ticker="AAPL", units=Decimal("0"),
+ )
+ session.add(existing)
+ await session.commit()
+
+ summary = await _import(session, test_workspace, test_user, _csv(
+ "ticker,date,quantity,price",
+ "AAPL,2026-01-15,10,100.00",
+ ), provider)
+
+ assert summary["holdings_created"] == 0
+ assert summary["holdings_matched"] == 1
+ reloaded = await session.get(Asset, existing.id)
+ assert reloaded is not None and reloaded.units == Decimal("10")
+
+
+async def test_dry_run_writes_nothing(
+ session: AsyncSession, test_user: User, test_workspace: Workspace, provider
+):
+ summary = await _import(session, test_workspace, test_user, _csv(
+ "ticker,date,quantity,price",
+ "AAPL,2026-01-15,10,100.00",
+ ), provider, dry_run=True)
+
+ assert summary["imported"] == 1
+ assert summary["holdings_created"] == 1
+ assert (await session.execute(select(Asset))).scalars().first() is None
+
+
+ "header,row,expected_kind",
+ [
+ # One export per language Securo is translated into.
+ ("Symbol,Date,Quantity,Price,Fee,Side", "AAPL,2026-01-15,10,150.00,1.20,buy", "buy"),
+ ("Ativo;Data;Quantidade;Preço;Corretagem;Operação", "AAPL;15/01/2026;10;150,00;1,20;compra", "buy"),
+ ("Activo,Fecha,Cantidad,Precio,Comisión,Operación", "AAPL,15/01/2026,10,150.00,1.20,venta", "sell"),
+ ("Symbole,Date,Quantité,Cours,Frais,Sens", "AAPL,15/01/2026,10,150.00,1.20,achat", "buy"),
+ ("Wertpapier;Datum;Stück;Kurs;Gebühr;Art", "AAPL;15.01.2026;10;150,00;1,20;verkauf", "sell"),
+ ("Titolo,Data,Quantità,Prezzo,Commissioni,Operazione", "AAPL,15/01/2026,10,150.00,1.20,acquisto", "buy"),
+ ("Walor;Data;Ilość;Cena;Prowizja;Rodzaj", "AAPL;15/01/2026;10;150,00;1,20;kupno", "buy"),
+ ("Тикер,Дата,Количество,Цена,Комиссия,Операция", "AAPL,15/01/2026,10,150.00,1.20,продажа", "sell"),
+ ("Тікер,Дата,Кількість,Ціна,Комісія,Операція", "AAPL,15/01/2026,10,150.00,1.20,купівля", "buy"),
+ ],
+)
+def test_headers_are_recognised_in_every_language_the_app_ships(header, row, expected_kind):
+ """A broker export is written in the language of whoever downloaded it."""
+ orders, errors, _ = asset_import_service.parse_orders_csv(_csv(header, row))
+ assert errors == [], header
+ assert len(orders) == 1, header
+ assert orders[0].ticker == "AAPL"
+ assert orders[0].quantity == Decimal("10")
+ assert orders[0].price == Decimal("150.00")
+ assert orders[0].fee == Decimal("1.20")
+ assert orders[0].kind == expected_kind
+ assert str(orders[0].date) == "2026-01-15"
+
+
+async def test_created_holding_takes_the_quote_currency_not_the_file(
+ session: AsyncSession, test_user: User, test_workspace: Workspace, provider
+):
+ """A file reporting a US stock in BRL must not label the holding BRL while
+ its price feed keeps returning USD."""
+ summary = await _import(session, test_workspace, test_user, _csv(
+ "ticker,date,quantity,price,currency",
+ "AAPL,2026-01-15,10,150.00,BRL",
+ ), provider)
+
+ assert summary["imported"] == 1
+ stored = await _only_asset(session, test_workspace)
+ assert stored.currency == "USD" # what the provider quotes it in
+
+
+async def test_warns_when_the_ticker_already_sits_in_another_wallet(
+ session: AsyncSession, test_user: User, test_workspace: Workspace, provider
+):
+ """Two brokers, two positions is legitimate; a mis-picked wallet looks the
+ same, so the preview says it rather than leaving it to be noticed later."""
+ from app.models.asset_group import AssetGroup
+
+ wallet = AssetGroup(id=uuid.uuid4(), workspace_id=test_workspace.id, user_id=test_user.id, name="Corretora B")
+ session.add(wallet)
+ await session.flush()
+ session.add(Asset(
+ id=uuid.uuid4(), user_id=test_user.id, workspace_id=test_workspace.id,
+ name="Apple", type="stock", currency="USD", valuation_method="market_price",
+ ticker="AAPL", group_id=wallet.id, units=Decimal("5"),
+ ))
+ await session.commit()
+
+ summary = await _import(session, test_workspace, test_user, _csv(
+ "ticker,date,quantity,price",
+ "AAPL,2026-01-15,10,100.00",
+ ), provider, dry_run=True)
+
+ assert [(w.ticker, w.reason, w.wallet) for w in summary["warnings"]] == [
+ ("AAPL", "exists_in_other_wallet", "Corretora B"),
+ ]
+
+
+async def test_warns_harder_when_the_same_orders_are_already_in_another_wallet(
+ session: AsyncSession, test_user: User, test_workspace: Workspace, provider
+):
+ """Importing the same file into a second wallet counts the shares twice,
+ and the wallet-scoped dedup cannot see it."""
+ from app.models.asset_group import AssetGroup
+
+ wallet = AssetGroup(id=uuid.uuid4(), workspace_id=test_workspace.id, user_id=test_user.id, name="Corretora B")
+ session.add(wallet)
+ await session.flush()
+ await session.commit()
+
+ content = _csv("ticker,date,quantity,price", "AAPL,2026-01-15,10,100.00")
+ await _import(session, test_workspace, test_user, content, provider, group_id=wallet.id)
+
+ summary = await _import(session, test_workspace, test_user, content, provider, dry_run=True)
+
+ assert [(w.ticker, w.reason, w.wallet) for w in summary["warnings"]] == [
+ ("AAPL", "orders_already_in_other_wallet", "Corretora B"),
+ ]
diff --git a/backend/tests/test_write_permission_coverage.py b/backend/tests/test_write_permission_coverage.py
index 7268747e..114a7143 100644
--- a/backend/tests/test_write_permission_coverage.py
+++ b/backend/tests/test_write_permission_coverage.py
@@ -71,6 +71,9 @@ ALLOWLIST: dict[tuple[str, str], str] = {
("DELETE", "/api/workspaces/{workspace_id}/members/{member_user_id}"): "owner floor inside the handler",
# Deliberate: a POST that persists nothing. See the comment on the route.
("POST", "/api/transactions/import/preview"): "parses an upload and returns a preview; writes nothing",
+ # Same shape for investment orders: the upload has to be a body, and the
+ # dry run only reports what an import would do.
+ ("POST", "/api/assets/import/preview"): "parses an upload and returns a preview; writes nothing",
# A read that has to be a POST: the backup password belongs in a body,
# not in a query string. Same read permission as GET /api/export/backup.
("POST", "/api/export/backup"): "exports the workspace it can already read; writes nothing",
diff --git a/frontend/src/App.tsx b/frontend/src/App.tsx
index aab07862..6197daf7 100644
--- a/frontend/src/App.tsx
+++ b/frontend/src/App.tsx
@@ -20,6 +20,7 @@ const TransactionsPage = lazy(() => import('@/pages/transactions'))
const AccountsPage = lazy(() => import('@/pages/accounts'))
const AccountDetailPage = lazy(() => import('@/pages/account-detail'))
const ImportPage = lazy(() => import('@/pages/import'))
+const AssetImportPage = lazy(() => import('@/pages/asset-import'))
const RulesPage = lazy(() => import('@/pages/rules'))
const CategoriesPage = lazy(() => import('@/pages/categories'))
const CollectionsPage = lazy(() => import('@/pages/collections'))
@@ -93,6 +94,7 @@ function App() {
<Route path="/goals" element={<ModuleRoute module="goals"><GoalsPage /></ModuleRoute>} />
<Route path="/recurring" element={<ModuleRoute module="recurring"><RecurringPage /></ModuleRoute>} />
<Route path="/assets" element={<ModuleRoute module="assets"><AssetsPage /></ModuleRoute>} />
+ <Route path="/assets/import" element={<ModuleRoute module="assets"><AssetImportPage /></ModuleRoute>} />
<Route path="/reports" element={<ModuleRoute module="reports"><ReportsPage /></ModuleRoute>} />
<Route path="/payees" element={<ModuleRoute module="payees"><PayeesPage /></ModuleRoute>} />
<Route path="/groups" element={<ModuleRoute module="split_groups"><GroupsPage /></ModuleRoute>} />
diff --git a/frontend/src/lib/api.ts b/frontend/src/lib/api.ts
index 68d382ac..772b7dd9 100644
--- a/frontend/src/lib/api.ts
+++ b/frontend/src/lib/api.ts
@@ -36,6 +36,9 @@ import type {
WorkspaceRole,
Asset,
AssetGroup,
+ AssetImportPreview,
+ AssetImportResult,
+ AssetOrderImport,
AssetTransaction,
AssetValue,
MarketSymbolMatch,
@@ -1140,6 +1143,38 @@ export const assets = {
const { data } = await api.post('/assets/buy', tx)
return data
},
+ previewImport: async (
+ file: File,
+ options?: { column_mapping?: Record<string, string>; date_format?: string; group_id?: string | null },
+ ): Promise<AssetImportPreview> => {
+ const formData = new FormData()
+ formData.append('file', file)
+ if (options?.date_format) formData.append('date_format', options.date_format)
+ if (options?.group_id) formData.append('group_id', options.group_id)
+ if (options?.column_mapping && Object.keys(options.column_mapping).length > 0) {
+ formData.append('column_mapping', JSON.stringify(options.column_mapping))
+ }
+ const { data } = await api.post('/assets/import/preview', formData)
+ return data
+ },
+ importOrders: async (
+ orders: AssetOrderImport[],
+ group_id?: string | null,
+ ): Promise<AssetImportResult> => {
+ const { data } = await api.post('/assets/import', { orders, group_id: group_id || null })
+ return data
+ },
+ importTemplate: async (): Promise<void> => {
+ const { data } = await api.get('/assets/import/template', { responseType: 'blob' })
+ const url = URL.createObjectURL(new Blob([data], { type: 'text/csv;charset=utf-8;' }))
+ const a = document.createElement('a')
+ a.href = url
+ a.download = 'securo-asset-orders.csv'
+ document.body.appendChild(a)
+ a.click()
+ document.body.removeChild(a)
+ URL.revokeObjectURL(url)
+ },
}
// Asset Groups ("wallets")
diff --git a/frontend/src/locales/de.json b/frontend/src/locales/de.json
index c8d22836..f1f7e3bb 100644
--- a/frontend/src/locales/de.json
+++ b/frontend/src/locales/de.json
@@ -1038,6 +1038,58 @@
"mapExternalId": "Spalte Transaktions-ID",
"mapNotes": "Spalte Notizen"
},
+ "assetImport": {
+ "title": "Wertpapierorders importieren",
+ "action": "Orders importieren",
+ "choose": "CSV-Datei auswählen",
+ "chooseHint": "Ticker, Datum, Stückzahl und Preis sind Pflicht. Eine negative Stückzahl gilt als Verkauf.",
+ "downloadTemplate": "Vorlage herunterladen",
+ "wallet": "Depot",
+ "noWallet": "Kein Depot",
+ "reading": "Datei wird gelesen…",
+ "mapPrompt": "Die Spalten konnten nicht erkannt werden. Ordnen Sie jedem Feld eine Spalte zu.",
+ "ignoreColumn": "Ignorieren",
+ "previewError": "Datei konnte nicht gelesen werden",
+ "importError": "Orders konnten nicht importiert werden",
+ "imported": "{{count}} Orders importiert",
+ "importing": "Wird importiert…",
+ "confirm": "{{count}} Orders importieren",
+ "summaryOrders": "{{count}} Orders bereit",
+ "summaryHoldings": "{{created}} Positionen angelegt, {{matched}} zugeordnet",
+ "summarySkipped": "{{count}} bereits importiert",
+ "rowsSkipped": "{{count}} Zeilen werden übersprungen",
+ "rowError": "Zeile {{row}} ({{ticker}}): {{reason}}",
+ "moreErrors": "und {{count}} weitere",
+ "moreRows": "und {{count}} weitere Orders",
+ "field": {
+ "ticker": "Ticker",
+ "date": "Datum",
+ "quantity": "Stückzahl",
+ "price": "Preis",
+ "fee": "Gebühr",
+ "kind": "Kauf/Verkauf",
+ "currency": "Währung",
+ "notes": "Notizen"
+ },
+ "walletWarningTitle": "Bereits im Portfolio",
+ "warning": {
+ "exists_in_other_wallet": "{{ticker}} liegt bereits in {{wallet}}. Ein Import hier legt eine zweite Position an.",
+ "orders_already_in_other_wallet": "{{ticker}}: dieselben Orders liegen bereits in {{wallet}}. Ein Import hier würde die Stücke doppelt zählen."
+ },
+ "kind": {
+ "buy": "Kauf",
+ "sell": "Verkauf"
+ },
+ "reason": {
+ "missing_ticker": "kein Ticker",
+ "invalid_date": "unlesbares Datum",
+ "invalid_quantity": "unlesbare Stückzahl",
+ "invalid_price": "unlesbarer Preis",
+ "invalid_kind": "Kauf/Verkauf nicht erkannt",
+ "unknown_ticker": "Ticker beim Kursanbieter nicht gefunden",
+ "oversell": "Verkauf größer als die Position"
+ }
+ },
"assets": {
"title": "Vermögenswerte",
"addAsset": "Vermögenswert hinzufügen",
diff --git a/frontend/src/locales/en.json b/frontend/src/locales/en.json
index c04509da..ca2dd761 100644
--- a/frontend/src/locales/en.json
+++ b/frontend/src/locales/en.json
@@ -1038,6 +1038,58 @@
"mapExternalId": "Transaction ID column",
"mapNotes": "Notes column"
},
+ "assetImport": {
+ "title": "Import investment orders",
+ "action": "Import orders",
+ "choose": "Choose a CSV file",
+ "chooseHint": "Ticker, date, quantity and price are required. A negative quantity is read as a sale.",
+ "downloadTemplate": "Download template",
+ "wallet": "Wallet",
+ "noWallet": "No wallet",
+ "reading": "Reading the file…",
+ "mapPrompt": "We could not tell which column is which. Point each field at a column from your file.",
+ "ignoreColumn": "Ignore",
+ "previewError": "Could not read that file",
+ "importError": "Could not import the orders",
+ "imported": "{{count}} orders imported",
+ "importing": "Importing…",
+ "confirm": "Import {{count}} orders",
+ "summaryOrders": "{{count}} orders ready",
+ "summaryHoldings": "{{created}} holdings created, {{matched}} matched",
+ "summarySkipped": "{{count}} already imported",
+ "rowsSkipped": "{{count}} rows will be skipped",
+ "rowError": "Row {{row}} ({{ticker}}): {{reason}}",
+ "moreErrors": "and {{count}} more",
+ "moreRows": "and {{count}} more orders",
+ "field": {
+ "ticker": "Ticker",
+ "date": "Date",
+ "quantity": "Quantity",
+ "price": "Price",
+ "fee": "Fee",
+ "kind": "Buy/Sell",
+ "currency": "Currency",
+ "notes": "Notes"
+ },
+ "walletWarningTitle": "Already in your portfolio",
+ "warning": {
+ "exists_in_other_wallet": "{{ticker}} is already held in {{wallet}}. Importing here creates a second position.",
+ "orders_already_in_other_wallet": "{{ticker}}: these same orders are already in {{wallet}}. Importing here would count the shares twice."
+ },
+ "kind": {
+ "buy": "Buy",
+ "sell": "Sell"
+ },
+ "reason": {
+ "missing_ticker": "no ticker",
+ "invalid_date": "unreadable date",
+ "invalid_quantity": "unreadable quantity",
+ "invalid_price": "unreadable price",
+ "invalid_kind": "unrecognised buy/sell value",
+ "unknown_ticker": "ticker not found by the price provider",
+ "oversell": "selling more units than the position holds"
+ }
+ },
"assets": {
"title": "Assets",
"addAsset": "Add Asset",
diff --git a/frontend/src/locales/es.json b/frontend/src/locales/es.json
index bcee3bbf..1543071d 100644
--- a/frontend/src/locales/es.json
+++ b/frontend/src/locales/es.json
@@ -1038,6 +1038,58 @@
"mapExternalId": "Columna de ID de transacción",
"mapNotes": "Columna de notas"
},
+ "assetImport": {
+ "title": "Importar órdenes de inversión",
+ "action": "Importar órdenes",
+ "choose": "Elegir un archivo CSV",
+ "chooseHint": "Ticker, fecha, cantidad y precio son obligatorios. Una cantidad negativa se lee como venta.",
+ "downloadTemplate": "Descargar plantilla",
+ "wallet": "Cartera",
+ "noWallet": "Sin cartera",
+ "reading": "Leyendo el archivo…",
+ "mapPrompt": "No pudimos identificar las columnas. Asigna cada campo a una columna de tu archivo.",
+ "ignoreColumn": "Ignorar",
+ "previewError": "No se pudo leer ese archivo",
+ "importError": "No se pudieron importar las órdenes",
+ "imported": "{{count}} órdenes importadas",
+ "importing": "Importando…",
+ "confirm": "Importar {{count}} órdenes",
+ "summaryOrders": "{{count}} órdenes listas",
+ "summaryHoldings": "{{created}} activos creados, {{matched}} encontrados",
+ "summarySkipped": "{{count}} ya importadas",
+ "rowsSkipped": "{{count}} filas se omitirán",
+ "rowError": "Fila {{row}} ({{ticker}}): {{reason}}",
+ "moreErrors": "y {{count}} más",
+ "moreRows": "y {{count}} órdenes más",
+ "field": {
+ "ticker": "Ticker",
+ "date": "Fecha",
+ "quantity": "Cantidad",
+ "price": "Precio",
+ "fee": "Comisión",
+ "kind": "Compra/Venta",
+ "currency": "Moneda",
+ "notes": "Notas"
+ },
+ "walletWarningTitle": "Ya está en tu cartera",
+ "warning": {
+ "exists_in_other_wallet": "{{ticker}} ya existe en {{wallet}}. Importar aquí crea una segunda posición.",
+ "orders_already_in_other_wallet": "{{ticker}}: estas mismas órdenes ya están en {{wallet}}. Importar aquí contaría las acciones dos veces."
+ },
+ "kind": {
+ "buy": "Compra",
+ "sell": "Venta"
+ },
+ "reason": {
+ "missing_ticker": "sin ticker",
+ "invalid_date": "fecha ilegible",
+ "invalid_quantity": "cantidad ilegible",
+ "invalid_price": "precio ilegible",
+ "invalid_kind": "compra/venta no reconocida",
+ "unknown_ticker": "ticker no encontrado por el proveedor de precios",
+ "oversell": "venta mayor que la posición"
+ }
+ },
"assets": {
"title": "Activos",
"addAsset": "Agregar activo",
diff --git a/frontend/src/locales/fr.json b/frontend/src/locales/fr.json
index ce117dff..e9679d75 100644
--- a/frontend/src/locales/fr.json
+++ b/frontend/src/locales/fr.json
@@ -1038,6 +1038,58 @@
"mapExternalId": "Colonne d'ID de transaction",
"mapNotes": "Colonne des notes"
},
+ "assetImport": {
+ "title": "Importer des ordres d'investissement",
+ "action": "Importer des ordres",
+ "choose": "Choisir un fichier CSV",
+ "chooseHint": "Ticker, date, quantité et prix sont obligatoires. Une quantité négative est lue comme une vente.",
+ "downloadTemplate": "Télécharger le modèle",
+ "wallet": "Portefeuille",
+ "noWallet": "Aucun portefeuille",
+ "reading": "Lecture du fichier…",
+ "mapPrompt": "Impossible d'identifier les colonnes. Associez chaque champ à une colonne de votre fichier.",
+ "ignoreColumn": "Ignorer",
+ "previewError": "Impossible de lire ce fichier",
+ "importError": "Impossible d'importer les ordres",
+ "imported": "{{count}} ordres importés",
+ "importing": "Importation…",
+ "confirm": "Importer {{count}} ordres",
+ "summaryOrders": "{{count}} ordres prêts",
+ "summaryHoldings": "{{created}} actifs créés, {{matched}} trouvés",
+ "summarySkipped": "{{count}} déjà importés",
+ "rowsSkipped": "{{count}} lignes seront ignorées",
+ "rowError": "Ligne {{row}} ({{ticker}}) : {{reason}}",
+ "moreErrors": "et {{count}} de plus",
+ "moreRows": "et {{count}} ordres de plus",
+ "field": {
+ "ticker": "Ticker",
+ "date": "Date",
+ "quantity": "Quantité",
+ "price": "Prix",
+ "fee": "Frais",
+ "kind": "Achat/Vente",
+ "currency": "Devise",
+ "notes": "Notes"
+ },
+ "walletWarningTitle": "Déjà dans votre portefeuille",
+ "warning": {
+ "exists_in_other_wallet": "{{ticker}} est déjà détenu dans {{wallet}}. Importer ici crée une seconde position.",
+ "orders_already_in_other_wallet": "{{ticker}} : ces mêmes ordres sont déjà dans {{wallet}}. Importer ici compterait les titres deux fois."
+ },
+ "kind": {
+ "buy": "Achat",
+ "sell": "Vente"
+ },
+ "reason": {
+ "missing_ticker": "aucun ticker",
+ "invalid_date": "date illisible",
+ "invalid_quantity": "quantité illisible",
+ "invalid_price": "prix illisible",
+ "invalid_kind": "achat/vente non reconnu",
+ "unknown_ticker": "ticker introuvable chez le fournisseur de cours",
+ "oversell": "vente supérieure à la position"
+ }
+ },
"assets": {
"title": "Actifs",
"addAsset": "Ajouter un actif",
diff --git a/frontend/src/locales/it.json b/frontend/src/locales/it.json
index 65fea3a9..f0df3536 100644
--- a/frontend/src/locales/it.json
+++ b/frontend/src/locales/it.json
@@ -1038,6 +1038,58 @@
"mapExternalId": "Colonna dell'ID transazione",
"mapNotes": "Colonna delle note"
},
+ "assetImport": {
+ "title": "Importa ordini di investimento",
+ "action": "Importa ordini",
+ "choose": "Scegli un file CSV",
+ "chooseHint": "Ticker, data, quantità e prezzo sono obbligatori. Una quantità negativa vale come vendita.",
+ "downloadTemplate": "Scarica il modello",
+ "wallet": "Portafoglio",
+ "noWallet": "Nessun portafoglio",
+ "reading": "Lettura del file…",
+ "mapPrompt": "Non siamo riusciti a riconoscere le colonne. Associa ogni campo a una colonna del file.",
+ "ignoreColumn": "Ignora",
+ "previewError": "Impossibile leggere il file",
+ "importError": "Impossibile importare gli ordini",
+ "imported": "{{count}} ordini importati",
+ "importing": "Importazione…",
+ "confirm": "Importa {{count}} ordini",
+ "summaryOrders": "{{count}} ordini pronti",
+ "summaryHoldings": "{{created}} titoli creati, {{matched}} trovati",
+ "summarySkipped": "{{count}} già importati",
+ "rowsSkipped": "{{count}} righe saranno saltate",
+ "rowError": "Riga {{row}} ({{ticker}}): {{reason}}",
+ "moreErrors": "e altri {{count}}",
+ "moreRows": "e altri {{count}} ordini",
+ "field": {
+ "ticker": "Ticker",
+ "date": "Data",
+ "quantity": "Quantità",
+ "price": "Prezzo",
+ "fee": "Commissioni",
+ "kind": "Acquisto/Vendita",
+ "currency": "Valuta",
+ "notes": "Note"
+ },
+ "walletWarningTitle": "Già nel tuo portafoglio",
+ "warning": {
+ "exists_in_other_wallet": "{{ticker}} è già presente in {{wallet}}. Importare qui crea una seconda posizione.",
+ "orders_already_in_other_wallet": "{{ticker}}: questi stessi ordini sono già in {{wallet}}. Importare qui conterebbe i titoli due volte."
+ },
+ "kind": {
+ "buy": "Acquisto",
+ "sell": "Vendita"
+ },
+ "reason": {
+ "missing_ticker": "nessun ticker",
+ "invalid_date": "data illeggibile",
+ "invalid_quantity": "quantità illeggibile",
+ "invalid_price": "prezzo illeggibile",
+ "invalid_kind": "acquisto/vendita non riconosciuto",
+ "unknown_ticker": "ticker non trovato dal fornitore di prezzi",
+ "oversell": "vendita superiore alla posizione"
+ }
+ },
"assets": {
"title": "Beni",
"addAsset": "Aggiungi bene",
diff --git a/frontend/src/locales/pl.json b/frontend/src/locales/pl.json
index cc7822f7..e8f11a16 100644
--- a/frontend/src/locales/pl.json
+++ b/frontend/src/locales/pl.json
@@ -1113,6 +1113,58 @@
"mapExternalId": "Kolumna ID transakcji",
"mapNotes": "Kolumna notatek"
},
+ "assetImport": {
+ "title": "Importuj zlecenia inwestycyjne",
+ "action": "Importuj zlecenia",
+ "choose": "Wybierz plik CSV",
+ "chooseHint": "Ticker, data, ilość i cena są wymagane. Ujemna ilość oznacza sprzedaż.",
+ "downloadTemplate": "Pobierz szablon",
+ "wallet": "Portfel",
+ "noWallet": "Bez portfela",
+ "reading": "Wczytywanie pliku…",
+ "mapPrompt": "Nie rozpoznaliśmy kolumn. Przypisz każde pole do kolumny z pliku.",
+ "ignoreColumn": "Pomiń",
+ "previewError": "Nie udało się odczytać pliku",
+ "importError": "Nie udało się zaimportować zleceń",
+ "imported": "Zaimportowano {{count}} zleceń",
+ "importing": "Importowanie…",
+ "confirm": "Importuj {{count}} zleceń",
+ "summaryOrders": "{{count}} zleceń gotowych",
+ "summaryHoldings": "utworzono {{created}} pozycji, dopasowano {{matched}}",
+ "summarySkipped": "{{count}} już zaimportowanych",
+ "rowsSkipped": "{{count}} wierszy zostanie pominiętych",
+ "rowError": "Wiersz {{row}} ({{ticker}}): {{reason}}",
+ "moreErrors": "i {{count}} więcej",
+ "moreRows": "i {{count}} zleceń więcej",
+ "field": {
+ "ticker": "Ticker",
+ "date": "Data",
+ "quantity": "Ilość",
+ "price": "Cena",
+ "fee": "Prowizja",
+ "kind": "Kupno/Sprzedaż",
+ "currency": "Waluta",
+ "notes": "Notatki"
+ },
+ "walletWarningTitle": "Już w Twoim portfelu",
+ "warning": {
+ "exists_in_other_wallet": "{{ticker}} znajduje się już w {{wallet}}. Import tutaj utworzy drugą pozycję.",
+ "orders_already_in_other_wallet": "{{ticker}}: te same zlecenia są już w {{wallet}}. Import tutaj policzyłby walory dwa razy."
+ },
+ "kind": {
+ "buy": "Kupno",
+ "sell": "Sprzedaż"
+ },
+ "reason": {
+ "missing_ticker": "brak tickera",
+ "invalid_date": "nieczytelna data",
+ "invalid_quantity": "nieczytelna ilość",
+ "invalid_price": "nieczytelna cena",
+ "invalid_kind": "nierozpoznane kupno/sprzedaż",
+ "unknown_ticker": "ticker nieznany dostawcy notowań",
+ "oversell": "sprzedaż większa niż pozycja"
+ }
+ },
"assets": {
"title": "Aktywa",
"addAsset": "Dodaj aktywo",
diff --git a/frontend/src/locales/pt-BR.json b/frontend/src/locales/pt-BR.json
index 079aad6c..fd8e177e 100644
--- a/frontend/src/locales/pt-BR.json
+++ b/frontend/src/locales/pt-BR.json
@@ -1038,6 +1038,58 @@
"mapExternalId": "Coluna de ID da transação",
"mapNotes": "Coluna de notas"
},
+ "assetImport": {
+ "title": "Importar ordens de investimento",
+ "action": "Importar ordens",
+ "choose": "Escolher um arquivo CSV",
+ "chooseHint": "Ativo, data, quantidade e preço são obrigatórios. Quantidade negativa é lida como venda.",
+ "downloadTemplate": "Baixar modelo",
+ "wallet": "Carteira",
+ "noWallet": "Sem carteira",
+ "reading": "Lendo o arquivo…",
+ "mapPrompt": "Não deu para identificar as colunas. Aponte cada campo para uma coluna do seu arquivo.",
+ "ignoreColumn": "Ignorar",
+ "previewError": "Não foi possível ler esse arquivo",
+ "importError": "Não foi possível importar as ordens",
+ "imported": "{{count}} ordens importadas",
+ "importing": "Importando…",
+ "confirm": "Importar {{count}} ordens",
+ "summaryOrders": "{{count}} ordens prontas",
+ "summaryHoldings": "{{created}} ativos criados, {{matched}} encontrados",
+ "summarySkipped": "{{count}} já importadas",
+ "rowsSkipped": "{{count}} linhas serão ignoradas",
+ "rowError": "Linha {{row}} ({{ticker}}): {{reason}}",
+ "moreErrors": "e mais {{count}}",
+ "moreRows": "e mais {{count}} ordens",
+ "field": {
+ "ticker": "Ativo",
+ "date": "Data",
+ "quantity": "Quantidade",
+ "price": "Preço",
+ "fee": "Taxas",
+ "kind": "Compra/Venda",
+ "currency": "Moeda",
+ "notes": "Observações"
+ },
+ "walletWarningTitle": "Já está no seu patrimônio",
+ "warning": {
+ "exists_in_other_wallet": "{{ticker}} já existe em {{wallet}}. Importar aqui cria uma segunda posição.",
+ "orders_already_in_other_wallet": "{{ticker}}: essas mesmas ordens já estão em {{wallet}}. Importar aqui contaria as cotas duas vezes."
+ },
+ "kind": {
+ "buy": "Compra",
+ "sell": "Venda"
+ },
+ "reason": {
+ "missing_ticker": "sem ativo",
+ "invalid_date": "data ilegível",
+ "invalid_quantity": "quantidade ilegível",
+ "invalid_price": "preço ilegível",
+ "invalid_kind": "compra/venda não reconhecida",
+ "unknown_ticker": "ativo não encontrado no provedor de cotações",
+ "oversell": "venda maior do que a posição"
+ }
+ },
"assets": {
"title": "Patrimônio",
"addAsset": "Adicionar Ativo",
diff --git a/frontend/src/locales/pt-PT.json b/frontend/src/locales/pt-PT.json
index 0ec9254c..eebd2df6 100644
--- a/frontend/src/locales/pt-PT.json
+++ b/frontend/src/locales/pt-PT.json
@@ -1038,6 +1038,58 @@
"suggested": "Sugerida",
"page": "Página {{current}} de {{total}}"
},
+ "assetImport": {
+ "title": "Importar ordens de investimento",
+ "action": "Importar ordens",
+ "choose": "Escolher um ficheiro CSV",
+ "chooseHint": "Ativo, data, quantidade e preço são obrigatórios. Quantidade negativa é lida como venda.",
+ "downloadTemplate": "Descarregar modelo",
+ "wallet": "Carteira",
+ "noWallet": "Sem carteira",
+ "reading": "A ler o ficheiro…",
+ "mapPrompt": "Não foi possível identificar as colunas. Aponte cada campo para uma coluna do seu ficheiro.",
+ "ignoreColumn": "Ignorar",
+ "previewError": "Não foi possível ler esse ficheiro",
+ "importError": "Não foi possível importar as ordens",
+ "imported": "{{count}} ordens importadas",
+ "importing": "A importar…",
+ "confirm": "Importar {{count}} ordens",
+ "summaryOrders": "{{count}} ordens prontas",
+ "summaryHoldings": "{{created}} ativos criados, {{matched}} encontrados",
+ "summarySkipped": "{{count}} já importadas",
+ "rowsSkipped": "{{count}} linhas serão ignoradas",
+ "rowError": "Linha {{row}} ({{ticker}}): {{reason}}",
+ "moreErrors": "e mais {{count}}",
+ "moreRows": "e mais {{count}} ordens",
+ "field": {
+ "ticker": "Ativo",
+ "date": "Data",
+ "quantity": "Quantidade",
+ "price": "Preço",
+ "fee": "Taxas",
+ "kind": "Compra/Venda",
+ "currency": "Moeda",
+ "notes": "Observações"
+ },
+ "walletWarningTitle": "Já está no seu património",
+ "warning": {
+ "exists_in_other_wallet": "{{ticker}} já existe em {{wallet}}. Importar aqui cria uma segunda posição.",
+ "orders_already_in_other_wallet": "{{ticker}}: estas mesmas ordens já estão em {{wallet}}. Importar aqui contaria as ações duas vezes."
+ },
+ "kind": {
+ "buy": "Compra",
+ "sell": "Venda"
+ },
+ "reason": {
+ "missing_ticker": "sem ativo",
+ "invalid_date": "data ilegível",
+ "invalid_quantity": "quantidade ilegível",
+ "invalid_price": "preço ilegível",
+ "invalid_kind": "compra/venda não reconhecida",
+ "unknown_ticker": "ativo não encontrado no fornecedor de cotações",
+ "oversell": "venda maior do que a posição"
+ }
+ },
"assets": {
"title": "Património",
"addAsset": "Adicionar Ativo",
diff --git a/frontend/src/locales/ru.json b/frontend/src/locales/ru.json
index 72a7f78f..94722b20 100644
--- a/frontend/src/locales/ru.json
+++ b/frontend/src/locales/ru.json
@@ -1044,6 +1044,58 @@
"mapExternalId": "Колонка ID транзакции",
"mapNotes": "Колонка заметок"
},
+ "assetImport": {
+ "title": "Импорт инвестиционных заявок",
+ "action": "Импорт заявок",
+ "choose": "Выбрать файл CSV",
+ "chooseHint": "Тикер, дата, количество и цена обязательны. Отрицательное количество читается как продажа.",
+ "downloadTemplate": "Скачать шаблон",
+ "wallet": "Портфель",
+ "noWallet": "Без портфеля",
+ "reading": "Чтение файла…",
+ "mapPrompt": "Не удалось определить колонки. Укажите, какой колонке соответствует каждое поле.",
+ "ignoreColumn": "Пропустить",
+ "previewError": "Не удалось прочитать файл",
+ "importError": "Не удалось импортировать заявки",
+ "imported": "Импортировано заявок: {{count}}",
+ "importing": "Импорт…",
+ "confirm": "Импортировать {{count}} заявок",
+ "summaryOrders": "Готово заявок: {{count}}",
+ "summaryHoldings": "создано позиций: {{created}}, найдено: {{matched}}",
+ "summarySkipped": "уже импортировано: {{count}}",
+ "rowsSkipped": "Строк будет пропущено: {{count}}",
+ "rowError": "Строка {{row}} ({{ticker}}): {{reason}}",
+ "moreErrors": "и ещё {{count}}",
+ "moreRows": "и ещё {{count}} заявок",
+ "field": {
+ "ticker": "Тикер",
+ "date": "Дата",
+ "quantity": "Количество",
+ "price": "Цена",
+ "fee": "Комиссия",
+ "kind": "Покупка/Продажа",
+ "currency": "Валюта",
+ "notes": "Заметки"
+ },
+ "walletWarningTitle": "Уже в вашем портфеле",
+ "warning": {
+ "exists_in_other_wallet": "{{ticker}} уже есть в {{wallet}}. Импорт сюда создаст вторую позицию.",
+ "orders_already_in_other_wallet": "{{ticker}}: эти же заявки уже есть в {{wallet}}. Импорт сюда посчитает бумаги дважды."
+ },
+ "kind": {
+ "buy": "Покупка",
+ "sell": "Продажа"
+ },
+ "reason": {
+ "missing_ticker": "нет тикера",
+ "invalid_date": "нечитаемая дата",
+ "invalid_quantity": "нечитаемое количество",
+ "invalid_price": "нечитаемая цена",
+ "invalid_kind": "не распознана покупка/продажа",
+ "unknown_ticker": "тикер не найден у поставщика котировок",
+ "oversell": "продажа больше позиции"
+ }
+ },
"assets": {
"title": "Активы",
"addAsset": "Добавить актив",
diff --git a/frontend/src/locales/uk.json b/frontend/src/locales/uk.json
index 8535b782..0ede600b 100644
--- a/frontend/src/locales/uk.json
+++ b/frontend/src/locales/uk.json
@@ -1044,6 +1044,58 @@
"mapExternalId": "Стовпець ID транзакції",
"mapNotes": "Стовпець приміток"
},
+ "assetImport": {
+ "title": "Імпорт інвестиційних заявок",
+ "action": "Імпорт заявок",
+ "choose": "Обрати файл CSV",
+ "chooseHint": "Тикер, дата, кількість і ціна обов'язкові. Від'ємна кількість читається як продаж.",
+ "downloadTemplate": "Завантажити шаблон",
+ "wallet": "Портфель",
+ "noWallet": "Без портфеля",
+ "reading": "Читання файлу…",
+ "mapPrompt": "Не вдалося визначити колонки. Вкажіть, якій колонці відповідає кожне поле.",
+ "ignoreColumn": "Пропустити",
+ "previewError": "Не вдалося прочитати файл",
+ "importError": "Не вдалося імпортувати заявки",
+ "imported": "Імпортовано заявок: {{count}}",
+ "importing": "Імпорт…",
+ "confirm": "Імпортувати {{count}} заявок",
+ "summaryOrders": "Готово заявок: {{count}}",
+ "summaryHoldings": "створено позицій: {{created}}, знайдено: {{matched}}",
+ "summarySkipped": "вже імпортовано: {{count}}",
+ "rowsSkipped": "Рядків буде пропущено: {{count}}",
+ "rowError": "Рядок {{row}} ({{ticker}}): {{reason}}",
+ "moreErrors": "і ще {{count}}",
+ "moreRows": "і ще {{count}} заявок",
+ "field": {
+ "ticker": "Тикер",
+ "date": "Дата",
+ "quantity": "Кількість",
+ "price": "Ціна",
+ "fee": "Комісія",
+ "kind": "Купівля/Продаж",
+ "currency": "Валюта",
+ "notes": "Нотатки"
+ },
+ "walletWarningTitle": "Уже у вашому портфелі",
+ "warning": {
+ "exists_in_other_wallet": "{{ticker}} уже є в {{wallet}}. Імпорт сюди створить другу позицію.",
+ "orders_already_in_other_wallet": "{{ticker}}: ці самі заявки вже є в {{wallet}}. Імпорт сюди порахує папери двічі."
+ },
+ "kind": {
+ "buy": "Купівля",
+ "sell": "Продаж"
+ },
+ "reason": {
+ "missing_ticker": "немає тикера",
+ "invalid_date": "нечитабельна дата",
+ "invalid_quantity": "нечитабельна кількість",
+ "invalid_price": "нечитабельна ціна",
+ "invalid_kind": "не розпізнано купівлю/продаж",
+ "unknown_ticker": "тикер не знайдено в постачальника котирувань",
+ "oversell": "продаж більший за позицію"
+ }
+ },
"assets": {
"title": "Активи",
"addAsset": "Додати актив",
diff --git a/frontend/src/pages/asset-import.tsx b/frontend/src/pages/asset-import.tsx
new file mode 100644
index 00000000..8558efa3
--- /dev/null
+++ b/frontend/src/pages/asset-import.tsx
@@ -0,0 +1,301 @@
+import { useState } from 'react'
+import { useTranslation } from 'react-i18next'
+import { useNavigate } from 'react-router-dom'
+import { useQuery, useQueryClient } from '@tanstack/react-query'
+import { toast } from 'sonner'
+import { AlertTriangle, Download, FileUp, Info, Upload, X } from 'lucide-react'
+
+import { assets as assetsApi, assetGroups as assetGroupsApi } from '@/lib/api'
+import type { AssetImportPreview, AssetOrderImport } from '@/types'
+import { PageHeader } from '@/components/page-header'
+import { Button } from '@/components/ui/button'
+import { Label } from '@/components/ui/label'
+
+/** The Securo fields a CSV column can be mapped to; `*` marks the required ones. */
+const MAPPABLE_FIELDS = [
+ { key: 'ticker', required: true },
+ { key: 'date', required: true },
+ { key: 'quantity', required: true },
+ { key: 'price', required: true },
+ { key: 'fee', required: false },
+ { key: 'kind', required: false },
+ { key: 'currency', required: false },
+ { key: 'notes', required: false },
+] as const
+
+/** Matches the card the other configuration pages use (see rules.tsx). */
+function SectionCard({ children }: { children: React.ReactNode }) {
+ return (
+ <div className="bg-card border border-border rounded-xl p-4 sm:p-6">{children}</div>
+ )
+}
+
+const SELECT_CLASS =
+ 'border border-border rounded-lg px-2 py-1.5 text-sm bg-card text-foreground focus:outline-none focus:ring-2 focus:ring-primary'
+
+export default function AssetImportPage() {
+ const { t } = useTranslation()
+ const navigate = useNavigate()
+ const queryClient = useQueryClient()
+
+ const [file, setFile] = useState<File | null>(null)
+ const [preview, setPreview] = useState<AssetImportPreview | null>(null)
+ const [mapping, setMapping] = useState<Record<string, string>>({})
+ const [groupId, setGroupId] = useState<string>('')
+ const [loading, setLoading] = useState(false)
+ const [importing, setImporting] = useState(false)
+
+ const { data: wallets } = useQuery({
+ queryKey: ['asset-groups'],
+ queryFn: assetGroupsApi.list,
+ })
+
+ async function runPreview(
+ selected: File,
+ nextMapping: Record<string, string>,
+ nextGroup: string,
+ ) {
+ setLoading(true)
+ try {
+ const result = await assetsApi.previewImport(selected, {
+ column_mapping: nextMapping,
+ group_id: nextGroup || null,
+ })
+ setPreview(result)
+ } catch {
+ toast.error(t('assetImport.previewError'))
+ setPreview(null)
+ } finally {
+ setLoading(false)
+ }
+ }
+
+ function handleFile(selected: File | null) {
+ setFile(selected)
+ setPreview(null)
+ setMapping({})
+ if (selected) runPreview(selected, {}, groupId)
+ }
+
+ // Re-preview on every change, so the counts on screen always describe the
+ // import that would actually run.
+ function handleMappingChange(field: string, column: string) {
+ const next = { ...mapping, [field]: column }
+ if (!column) delete next[field]
+ setMapping(next)
+ if (file) runPreview(file, next, groupId)
+ }
+
+ function handleWalletChange(value: string) {
+ setGroupId(value)
+ if (file) runPreview(file, mapping, value)
+ }
+
+ async function handleImport() {
+ if (!preview || preview.orders.length === 0) return
+ setImporting(true)
+ try {
+ const result = await assetsApi.importOrders(preview.orders as AssetOrderImport[], groupId || null)
+ queryClient.invalidateQueries({ queryKey: ['assets'] })
+ queryClient.invalidateQueries({ queryKey: ['asset-groups'] })
+ toast.success(t('assetImport.imported', { count: result.imported }))
+ navigate('/assets')
+ } catch {
+ toast.error(t('assetImport.importError'))
+ } finally {
+ setImporting(false)
+ }
+ }
+
+ const importable = preview?.orders.length ?? 0
+ const rowErrors = preview?.errors ?? []
+ const walletWarnings = preview?.warnings ?? []
+ const needsMapping = !!preview?.parse_error
+
+ return (
+ <div>
+ <PageHeader
+ section={t('assets.title')}
+ title={t('assetImport.title')}
+ action={
+ <Button variant="outline" onClick={() => assetsApi.importTemplate()}>
+ <Download size={14} className="mr-1" />
+ {t('assetImport.downloadTemplate')}
+ </Button>
+ }
+ />
+
+ <SectionCard>
+ <div className="mb-4 grid gap-2 sm:max-w-xs">
+ <Label htmlFor="asset-import-wallet">{t('assetImport.wallet')}</Label>
+ <select
+ id="asset-import-wallet"
+ className={SELECT_CLASS}
+ value={groupId}
+ onChange={(e) => handleWalletChange(e.target.value)}
+ >
+ <option value="">{t('assetImport.noWallet')}</option>
+ {(wallets ?? []).map((w) => (
+ <option key={w.id} value={w.id}>{w.name}</option>
+ ))}
+ </select>
+ </div>
+
+ <label className="flex cursor-pointer flex-col items-center justify-center gap-2 rounded-lg border border-dashed border-border px-6 py-10 text-center transition-colors hover:border-primary/50">
+ <FileUp size={22} className="text-muted-foreground" />
+ <span className="text-sm font-medium">
+ {file ? file.name : t('assetImport.choose')}
+ </span>
+ <span className="text-xs text-muted-foreground">{t('assetImport.chooseHint')}</span>
+ <input
+ type="file"
+ accept=".csv,text/csv"
+ className="hidden"
+ onChange={(e) => handleFile(e.target.files?.[0] ?? null)}
+ />
+ </label>
+
+ {loading && (
+ <p className="mt-4 text-sm text-muted-foreground">{t('assetImport.reading')}</p>
+ )}
+
+ {needsMapping && (
+ <div className="mt-6">
+ <p className="mb-3 text-sm text-amber-500">{t('assetImport.mapPrompt')}</p>
+ <div className="grid gap-3 sm:grid-cols-2">
+ {MAPPABLE_FIELDS.map(({ key, required }) => (
+ <div key={key} className="grid gap-1">
+ <Label htmlFor={`map-${key}`} className="text-xs">
+ {t(`assetImport.field.${key}`)}{required ? ' *' : ''}
+ </Label>
+ <select
+ id={`map-${key}`}
+ className={SELECT_CLASS}
+ value={mapping[key] ?? ''}
+ onChange={(e) => handleMappingChange(key, e.target.value)}
+ >
+ <option value="">{t('assetImport.ignoreColumn')}</option>
+ {(preview?.csv_columns ?? []).map((col) => (
+ <option key={col} value={col}>{col}</option>
+ ))}
+ </select>
+ </div>
+ ))}
+ </div>
+ </div>
+ )}
+
+ {preview && !needsMapping && (
+ <div className="mt-6 space-y-4">
+ <div className="flex flex-wrap gap-4 text-sm">
+ <span>{t('assetImport.summaryOrders', { count: importable })}</span>
+ <span className="text-muted-foreground">
+ {t('assetImport.summaryHoldings', {
+ created: preview.holdings_created,
+ matched: preview.holdings_matched,
+ })}
+ </span>
+ {preview.skipped > 0 && (
+ <span className="text-muted-foreground">
+ {t('assetImport.summarySkipped', { count: preview.skipped })}
+ </span>
+ )}
+ </div>
+
+ {walletWarnings.length > 0 && (
+ <div className="rounded-md border border-blue-200 bg-blue-50 p-3 dark:border-blue-800 dark:bg-blue-950">
+ <p className="mb-2 flex items-center gap-2 text-sm font-medium text-blue-700 dark:text-blue-300">
+ <Info size={14} />
+ {t('assetImport.walletWarningTitle')}
+ </p>
+ <ul className="space-y-1 text-xs text-blue-600 dark:text-blue-300/80">
+ {walletWarnings.map((w) => (
+ <li key={`${w.ticker}-${w.reason}`}>
+ {t(`assetImport.warning.${w.reason}`, {
+ ticker: w.ticker,
+ wallet: w.wallet ?? '—',
+ })}
+ </li>
+ ))}
+ </ul>
+ </div>
+ )}
+
+ {rowErrors.length > 0 && (
+ <div className="rounded-md border border-amber-500/30 bg-amber-500/10 p-3">
+ <p className="mb-2 flex items-center gap-2 text-sm font-medium text-amber-700 dark:text-amber-400">
+ <AlertTriangle size={14} />
+ {t('assetImport.rowsSkipped', { count: rowErrors.length })}
+ </p>
+ <ul className="space-y-1 text-xs text-muted-foreground">
+ {rowErrors.slice(0, 8).map((err) => (
+ <li key={`${err.row}-${err.reason}`}>
+ {t('assetImport.rowError', {
+ row: err.row,
+ ticker: err.ticker ?? '—',
+ reason: t(`assetImport.reason.${err.reason}`, err.reason),
+ })}
+ {err.detail ? ` (${err.detail})` : ''}
+ </li>
+ ))}
+ {rowErrors.length > 8 && (
+ <li>{t('assetImport.moreErrors', { count: rowErrors.length - 8 })}</li>
+ )}
+ </ul>
+ </div>
+ )}
+
+ {importable > 0 && (
+ <div className="overflow-x-auto rounded-lg border border-border">
+ <table className="w-full text-sm">
+ <thead className="bg-muted/40 text-xs uppercase text-muted-foreground">
+ <tr>
+ <th className="px-3 py-2 text-left">{t('assetImport.field.ticker')}</th>
+ <th className="px-3 py-2 text-left">{t('assetImport.field.date')}</th>
+ <th className="px-3 py-2 text-left">{t('assetImport.field.kind')}</th>
+ <th className="px-3 py-2 text-right">{t('assetImport.field.quantity')}</th>
+ <th className="px-3 py-2 text-right">{t('assetImport.field.price')}</th>
+ <th className="px-3 py-2 text-right">{t('assetImport.field.fee')}</th>
+ </tr>
+ </thead>
+ <tbody>
+ {preview.orders.slice(0, 50).map((order) => (
+ <tr key={order.row} className="border-t border-border/60">
+ <td className="px-3 py-1.5 font-medium">{order.ticker}</td>
+ <td className="px-3 py-1.5">{order.date}</td>
+ <td className="px-3 py-1.5">
+ <span className={order.kind === 'sell' ? 'text-rose-500' : 'text-emerald-500'}>
+ {t(`assetImport.kind.${order.kind}`)}
+ </span>
+ </td>
+ <td className="px-3 py-1.5 text-right tabular-nums">{order.quantity}</td>
+ <td className="px-3 py-1.5 text-right tabular-nums">{order.price}</td>
+ <td className="px-3 py-1.5 text-right tabular-nums">{order.fee}</td>
+ </tr>
+ ))}
+ </tbody>
+ </table>
+ {preview.orders.length > 50 && (
+ <p className="border-t border-border/60 px-3 py-2 text-xs text-muted-foreground">
+ {t('assetImport.moreRows', { count: preview.orders.length - 50 })}
+ </p>
+ )}
+ </div>
+ )}
+
+ <div className="flex gap-2">
+ <Button onClick={handleImport} disabled={importing || importable === 0}>
+ <Upload size={14} className="mr-1" />
+ {importing ? t('assetImport.importing') : t('assetImport.confirm', { count: importable })}
+ </Button>
+ <Button variant="outline" onClick={() => handleFile(null)}>
+ <X size={14} className="mr-1" />
+ {t('common.cancel')}
+ </Button>
+ </div>
+ </div>
+ )}
+ </SectionCard>
+ </div>
+ )
+}
diff --git a/frontend/src/pages/assets.tsx b/frontend/src/pages/assets.tsx
index 669c6f2f..d79abd1c 100644
--- a/frontend/src/pages/assets.tsx
+++ b/frontend/src/pages/assets.tsx
@@ -40,6 +40,7 @@ import {
Bitcoin,
PieChart,
AlertTriangle,
+ Upload,
} from 'lucide-react'
import {
AreaChart,
@@ -50,6 +51,7 @@ import {
ResponsiveContainer,
CartesianGrid,
} from 'recharts'
+import { useNavigate } from 'react-router-dom'
import { PageHeader } from '@/components/page-header'
import { usePrivacyMode } from '@/hooks/use-privacy-mode'
import { useAuth } from '@/contexts/auth-context'
@@ -183,6 +185,7 @@ function assetErrorMessage(e: unknown, fallback: string): string {
export default function AssetsPage() {
const { t } = useTranslation()
+ const navigate = useNavigate()
const locale = useDisplayLocale()
const dateLocale = useDateLocale()
const { mask } = usePrivacyMode()
@@ -1018,6 +1021,10 @@ export default function AssetsPage() {
action={
canWrite ? (
<div className="flex items-center gap-2">
+ <Button onClick={() => navigate('/assets/import')} variant="outline" className="gap-1.5">
+ <Upload size={16} />
+ {t('assetImport.action')}
+ </Button>
<Button onClick={openCreateWallet} variant="outline" className="gap-1.5">
<Wallet size={16} />
{t('assets.newWallet')}
diff --git a/frontend/src/types/index.ts b/frontend/src/types/index.ts
index a7a90622..ad270cd6 100644
--- a/frontend/src/types/index.ts
+++ b/frontend/src/types/index.ts
@@ -724,6 +724,54 @@ export interface Asset {
transaction_count: number
}
+/** One order read from a broker CSV, before it reaches a holding. */
+export interface AssetOrderImport {
+ row: number
+ ticker: string
+ date: string
+ kind: 'buy' | 'sell'
+ quantity: number
+ price: number
+ fee: number
+ currency: string | null
+ name: string | null
+ notes: string | null
+ external_id: string | null
+}
+
+export interface AssetImportRowError {
+ row: number
+ reason: string
+ ticker: string | null
+ detail: string | null
+}
+
+export interface AssetImportWarning {
+ ticker: string
+ reason: string
+ wallet: string | null
+}
+
+export interface AssetImportPreview {
+ orders: AssetOrderImport[]
+ errors: AssetImportRowError[]
+ warnings: AssetImportWarning[]
+ csv_columns: string[]
+ parse_error: string | null
+ holdings_created: number
+ holdings_matched: number
+ skipped: number
+}
+
+export interface AssetImportResult {
+ imported: number
+ skipped: number
+ holdings_created: number
+ holdings_matched: number
+ errors: AssetImportRowError[]
+ warnings: AssetImportWarning[]
+}
+
export interface AssetTransaction {
id: string
asset_id: string