Improves Beancount entry generation and sanitization

Adds a function to sanitize strings for use as Beancount links,
ensuring compatibility with Beancount's link restrictions.

Refactors the journal entry creation process to use EUR-based
postings when fiat currency is provided, improving accuracy
and consistency. The legacy SATS-based fallback is retained for
cases without fiat currency information.

Adjusts reference generation for Beancount entries using the
sanitized description.
This commit is contained in:
padreug 2025-11-10 11:35:41 +01:00
parent 0e93fc5ffc
commit a6b67b7416
2 changed files with 57 additions and 11 deletions

View file

@ -611,19 +611,38 @@ async def api_create_journal_entry(
fiat_amount_str = line.metadata.get("fiat_amount")
fiat_amount = Decimal(fiat_amount_str) if fiat_amount_str else None
# Create posting metadata (excluding fiat fields that go in cost basis)
# Create posting metadata (excluding fiat fields that are used for primary amount)
posting_metadata = {k: v for k, v in line.metadata.items()
if k not in ["fiat_currency", "fiat_amount"]}
if line.description:
posting_metadata["description"] = line.description
posting = format_posting_with_cost(
account=account.name,
amount_sats=line.amount,
fiat_currency=fiat_currency,
fiat_amount=abs(fiat_amount) if fiat_amount else None,
metadata=posting_metadata if posting_metadata else None
)
# If fiat currency is provided, use EUR-based format (primary amount in EUR, sats in metadata)
# Otherwise, use SATS-based format
if fiat_currency and fiat_amount:
# EUR-based posting (current architecture)
posting_metadata["sats-equivalent"] = str(abs(line.amount))
# Apply the sign from line.amount to fiat_amount
# line.amount is positive for debits, negative for credits
signed_fiat_amount = fiat_amount if line.amount >= 0 else -fiat_amount
posting = {
"account": account.name,
"amount": f"{signed_fiat_amount:.2f} {fiat_currency}",
"meta": posting_metadata if posting_metadata else None
}
else:
# SATS-based posting (legacy/fallback)
if line.description:
posting_metadata["description"] = line.description
posting = format_posting_with_cost(
account=account.name,
amount_sats=line.amount,
fiat_currency=None,
fiat_amount=None,
metadata=posting_metadata if posting_metadata else None
)
postings.append(posting)
# Extract tags and links from meta