From 6b79452e9576e84b0649f262bbf685a5c856161b Mon Sep 17 00:00:00 2001 From: ai-bot Date: Sat, 14 Nov 2026 18:03:10 +0000 Subject: [PATCH] Export sats and whole kroner, and add the price column --- export.py | 44 +++++++++++++++++++++++++------------------- 1 file changed, 25 insertions(+), 19 deletions(-) diff --git a/export.py b/export.py index 5823f39..cd4292c 100644 --- a/export.py +++ b/export.py @@ -2,7 +2,8 @@ Semicolon separated with a UTF-8 BOM and Norwegian number formatting, so the file opens directly in a Norwegian spreadsheet. `report_csv()` starts the file -and carries the BOM; `transactions_csv()` is appended after it. +and carries the BOM; `transactions_csv()` is appended after it. Kroner are +written as whole kroner and BTC quantities as whole sats, as in the UI. """ import database @@ -29,8 +30,9 @@ TRANSACTION_HEADINGS = ( "Type", "Konto", "Til konto", - "Antall inn (BTC)", - "Antall ut (BTC)", + "Pris (kr)", + "Antall inn (sats)", + "Antall ut (sats)", "Beløp inn (kr)", "Beløp ut (kr)", "Verdi (kr)", @@ -52,13 +54,13 @@ def _cell(value): def _amount(ore): - """An amount in kroner, with Norwegian formatting.""" - return "" if ore is None else rates.ore_to_nok(ore) + """Whole kroner, empty when there is no amount.""" + return "" if ore is None else rates.kroner_whole(ore) -def _btc(sats): - """A BTC quantity, empty when there is none.""" - return "" if not sats else rates.sats_to_btc(sats) +def _sats(sats): + """A whole number of sats, empty when there is none.""" + return "" if not sats else rates.group_int(sats) def _percent(value): @@ -89,6 +91,9 @@ def report_csv(year): lines.append("Inntekt (kr)" + SEPARATOR + _amount(totals["inntekt_ore"])) lines.append("Skatt (kr)" + SEPARATOR + _amount(totals["skatt_ore"])) lines.append("Formuesskatt (kr)" + SEPARATOR + _amount(totals["formuesskatt_ore"])) + lines.append( + "Forvaltningskostnad (sats)" + SEPARATOR + _sats(totals["admin_cost_sats"]) + ) lines.append( "Forvaltningskostnad (kr)" + SEPARATOR + _amount(totals["admin_cost_ore"]) ) @@ -97,7 +102,7 @@ def report_csv(year): lines.append(SEPARATOR.join(( "Konto", "Behaldning %", - "Antall (BTC)", + "Antall (sats)", "Formue (kr)", "Gevinst (kr)", "Tap (kr)", @@ -108,7 +113,7 @@ def report_csv(year): lines.append(SEPARATOR.join(( _cell(account["name"]), _percent(account["share_percent"]), - _btc(account["sats"]), + _sats(account["sats"]), _amount(account["formue_ore"]), _amount(account["gevinst_ore"]), _amount(account["tap_ore"]), @@ -119,21 +124,21 @@ def report_csv(year): if data["non_taxable"]: lines.append(SEPARATOR.join(( - "Ikkje skattepliktig", "Antall (BTC)", "Gevinst (kr)", "Tap (kr)" + "Ikkje skattepliktig", "Antall (sats)", "Gevinst (kr)", "Tap (kr)" ))) for row in data["non_taxable"]: lines.append(SEPARATOR.join(( _cell(row["name"]), - _btc(row["sats"]), + _sats(row["sats"]), _amount(row["gevinst_ore"]), _amount(row["tap_ore"]), ))) lines.append("") if data["custody_totals"]: - lines.append("TOT (fysisk behaldning)" + SEPARATOR + "Antall (BTC)") + lines.append("TOT (fysisk behaldning)" + SEPARATOR + "Antall (sats)") for row in data["custody_totals"]: - lines.append(_cell(row["name"]) + SEPARATOR + _btc(row["sats"])) + lines.append(_cell(row["name"]) + SEPARATOR + _sats(row["sats"])) lines.append("") lines.append(_cell(data["skattemelding"]["note"])) @@ -141,7 +146,7 @@ def report_csv(year): def transactions_csv(year): - """The year's transactions with the cost basis each one consumed.""" + """The year's transactions with the consumed cost and the bitcoin price.""" year = int(year) conn = database.connect() try: @@ -151,7 +156,7 @@ def transactions_csv(year): " m.name AS market_name, d.name AS to_market_name," " COALESCE(SUM(c.cost_ore), 0) AS cost_ore" " FROM transactions t" - " JOIN markets m ON m.id = t.market_id" + " LEFT JOIN markets m ON m.id = t.market_id" " LEFT JOIN markets d ON d.id = t.to_market_id" " LEFT JOIN lot_consumptions c ON c.tx_id = t.id" " WHERE t.date >= ? AND t.date <= ?" @@ -169,12 +174,13 @@ def transactions_csv(year): _cell(TYPE_LABELS.get(row["type"], row["type"])), _cell(row["market_name"]), _cell(row["to_market_name"]), - _btc(row["leg_in_sats"]), - _btc(row["leg_out_sats"]), + _amount(tax.btc_price_ore(row)), + _sats(row["leg_in_sats"]), + _sats(row["leg_out_sats"]), _amount(row["leg_in_ore"]), _amount(row["leg_out_ore"]), _amount(row["value_ore"]), - _btc(row["fee_sats"]), + _sats(row["fee_sats"]), _amount(row["fee_ore"]), _amount(row["cost_ore"]), "Ja" if row["taxable"] else "Nei",