1
0
crypto-tax/crypto_tax/output_excel.py

96 lines
3.2 KiB
Python
Raw Normal View History

2024-12-22 22:25:24 +00:00
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):
2024-12-22 23:08:24 +00:00
if a == 0 or b == 0:
return 0
2024-12-22 22:25:24 +00:00
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)
2024-12-22 23:08:24 +00:00
#assert exchange_rate_asset_to_dkk is not None, asset
2024-12-22 22:25:24 +00:00
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_fifo_sheet(sheet, tax_report: TaxReport):
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_buy_sales = workbook.create_sheet('FIFO')
write_fifo_sheet(sheet_buy_sales, report)
workbook.save(output_path)