# docker-finance | modern accounting for the power-user
#
# Copyright (C) 2021-2025 Aaron Fiore (Founder, Evergreen Crypto LLC)
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.

fields txid,refid,date,type,subtype,aclass,asset,amount_,fee,balance_,direction,subaccount

date-format %Y-%m-%d %H:%M:%S
description %date +0000

account1 assets:kraken:%subaccount:%asset
amount %amount_ %asset

comment type:%type, aclass:%aclass, refid:%refid, direction:%direction

if %txid [a-z]
  comment type:%type, aclass:%aclass, refid:%refid, txid:%txid, direction:%direction

################################################################################
#                                                                              #
#                                 TRANSFER                                     #
#                                                                              #
################################################################################

if %direction ^IN$
  account2 equity:kraken:%subaccount:withdrawal:%asset

if %direction ^OUT$
  account2 equity:kraken:%subaccount:deposit:%asset

# If refid present but no txid present, it's not yet confirmed and will be counted as a double-spend
if %direction (^IN$|^OUT$)
& %refid [A-Z]
& %txid ^[^A-Z0-9]*$
  skip

if %direction ^OUT$
& %fee [1-9]
  amount3 -%fee %asset
  account3 assets:kraken:%subaccount:%asset
  amount4 %fee %asset
  account4 expenses:kraken:%subaccount:fees:withdrawals:%asset
  comment type:%type, aclass:%aclass, refid:%refid, txid:%txid, direction:%direction

if %direction ^OUT$
& %fee [1-9]
& %asset ^[^USD]*$
  comment type:%type, aclass:%aclass, refid:%refid, txid:%txid, direction:%direction, taxed_as:SPEND
  comment3 %date +0000,SPEND,kraken:%subaccount,%asset,%fee,USD,,FEE

################################################################################
#                                                                              #
#                                   TRADE                                      #
#                                                                              #
################################################################################

if %type (^trade$|^margin$|^settled$|^rollover$)
  account2 equity:kraken:%subaccount:trades:%direction:%asset

if %type (^trade$|^margin$|^settled$|^rollover$)
& %fee [1-9]
  amount3 -%fee %asset
  account3 assets:kraken:%subaccount:%asset
  amount4 %fee %asset
  account4 expenses:kraken:%subaccount:fees:trades:%direction:%asset

if %type ^trade$
  comment type:%type, aclass:%aclass, refid:%refid, txid:%txid, direction:%direction, taxed_as:MATCH
  comment2 %refid,%date +0000,%direction,kraken:%subaccount,%asset,%amount_

if %type ^trade$
& %fee [1-9]
  comment3 %refid,%date +0000,FEE,kraken:%subaccount,%asset,%fee

#
# TODO: margin/settled/rollover needs work
#

if %type (^margin$|^settled$|^rollover$)
& %amount_ ^[^1-9]*$
  account1
  account2

if %type (^margin$|^settled$|^rollover$)
& %amount_ [1-9]
  account2 income:kraken:%subaccount:trades:%direction:%asset

if %type (^margin$|^settled$|^rollover$)
& %amount_ [1-9]
& %asset ^[^USD]*$
  comment2 %date +0000,INCOME,kraken:%subaccount,%asset,%amount_,USD,,FEE

if %type (^margin$|^settled$|^rollover$)
& %amount_ ^-
  account2 expenses:kraken:%subaccount:trades:%direction:%asset

if %type (^margin$|^settled$|^rollover$)
& %amount_ ^-
& %asset ^[^USD]*$
  comment2 %date +0000,SPEND,kraken:%subaccount,%asset,%amount_,USD,,FEE

# vim: sw=2 sts=2 si ai et
