1
0
crypto-tax/crypto_tax/output_excel.py
2024-12-24 20:12:50 +01:00

124 lines
4.1 KiB
Python

import sys
import requests
import fin_depo
from . import secrets
import fin_defs
from decimal import Decimal
from collections import deque
from fin_depo.data import *
import datetime
import dataclasses
import logging
from .data import TaxReport, BoughtAndSold, BoughtAndNotYetSold
from pathlib import Path
import openpyxl
TAX_TYPES: dict[fin_defs.Asset, str] = {
fin_defs.DKK: 'Fiat',
fin_defs.EUR: 'Fiat',
fin_defs.BTC: 'Krypto-valuta',
fin_defs.MPC: 'Krypto-valuta',
fin_defs.WELL_KNOWN_SYMBOLS['MATIC']: 'Krypto-valuta',
fin_defs.USDT: 'Stable Coin (Financiel Kontrakt)',
}
NOW = datetime.datetime.now(tz=datetime.UTC)
def mult_a(a, b):
if a == 0 or b == 0:
return 0
if a is None or b is None:
return 'Unknown'
return a * b
def write_current_assets(sheet, tax_report: TaxReport):
column_headers = ['Værdipapir', 'Mængde', 'Estimeret værdi (DKK)',
'Beskatningstype']
sheet.append(column_headers)
for asset, positions in tax_report.current_assets.items():
total_amount: Decimal = sum((p.amount.amount for p in positions), start=Decimal(0))
exchange_rate_asset_to_dkk = tax_report.exchange_rate_at_time(asset, fin_defs.DKK, NOW)
#assert exchange_rate_asset_to_dkk is not None, asset
row = [
asset.raw_short_name(),
total_amount,
mult_a(total_amount, exchange_rate_asset_to_dkk),
TAX_TYPES[asset],
]
sheet.append(row)
del asset, positions, row, total_amount
def write_ledger_sheet(sheet, tax_report: TaxReport):
column_headers = [
'Tidspunkt (UTC)',
'Type',
'Værdipapir',
'Mængde',
'Balance',
'Kontoudbyder',
]
sheet.append(column_headers)
row_idx = 2
for ledger_entry in tax_report.ledger_entries:
row = [
ledger_entry.time.replace(tzinfo=None),
ledger_entry.type,
ledger_entry.amount.asset.raw_short_name(),
ledger_entry.amount.amount,
ledger_entry.balance.amount,
ledger_entry.account_provider,
]
sheet.append(row)
row_idx+= 1
del ledger_entry, row
def write_fifo_sheet(sheet, tax_report: TaxReport):
# TODO: Account for transfers
column_headers = [
'Værdipapir', 'Mængde', 'Anskaffelsesværdi (DKK)', 'Købsdato (UTC)',
'Salgsværdi (DKK)', 'Salgsdato (UTC)', 'Profit (DKK)', 'Tab (DKK)',
'Beskatningstype',
]
sheet.append(column_headers)
row_idx = 2
for fifo_entry in tax_report.bought_and_sold_for:
print(fifo_entry)
asset = fifo_entry.amount.asset
exchange_rate_dkk_bought = tax_report.exchange_rate_at_time(asset, fin_defs.DKK, fifo_entry.time_bought)
#assert exchange_rate_dkk_bought is not None, asset
exchange_rate_dkk_sold = tax_report.exchange_rate_at_time(asset, fin_defs.DKK, fifo_entry.time_sold)
#assert exchange_rate_dkk_bought is not None, asset
row = [
asset.raw_short_name(),
fifo_entry.amount.amount,
mult_a(fifo_entry.amount.amount , exchange_rate_dkk_bought),
fifo_entry.time_bought.replace(tzinfo=None),
mult_a(fifo_entry.amount.amount , exchange_rate_dkk_sold),
fifo_entry.time_sold.replace(tzinfo=None),
f'=max(0, E{row_idx} - C{row_idx})',
f'=max(0, C{row_idx} - E{row_idx})',
TAX_TYPES[asset],
]
sheet.append(row)
row_idx+= 1
del fifo_entry, asset, row
def produce_excel_report(report: TaxReport, output_path: Path):
workbook = openpyxl.Workbook()
workbook.active.title = 'Current Assets'
write_current_assets(workbook.active, report)
sheet_ledger = workbook.create_sheet('Ledger')
write_ledger_sheet(sheet_ledger , report)
sheet_fifo = workbook.create_sheet('FIFO')
write_fifo_sheet(sheet_fifo, report)
workbook.save(output_path)