# 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 code_,type,input_currency,input_amount,output_currency,output_amount,usd_amount,status_,description_,date,direction,subaccount

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

account1 assets:nexo:%subaccount:%input_currency
amount %input_amount "%input_currency"

comment description:%description_, code:%code_, status:%status_, type:%type, direction:%direction

# Capture all movement
if %direction ^OUT$
  amount -%input_amount "%input_currency"

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

# Deposit
if %type ^Deposit$
  account2 equity:nexo:%subaccount:withdrawal:%input_currency
  comment code:%code_, status:%status_, type:%type, txid:%description_, direction:%direction

# Withdrawals
if %type ^Withdrawal$
  account2 equity:nexo:%subaccount:deposit:%input_currency

# Moving between savings wallet and credit line wallet
if %type (^TransferIn$|^TransferOut$)
  account2 assets:nexo:%subaccount:collateral:%input_currency

# Locking/unlocking 30 day terms for extra earned interest
if %type (^LockingTermDeposit$|^UnlockingTermDeposit$)
  account2 assets:nexo:%subaccount:locked:%input_currency

# ---------------------------------------------------------------------------- #
#                                    Borrow                                    #
# ---------------------------------------------------------------------------- #

# Loan withdrawal accounted by nexo (withdrawal *into* nexo account as USD even if it's a stablecoin)
if %type ^WithdrawalCredit$
  account1 liabilities:nexo:%subaccount:receivable:loans:%input_currency
  account2 assets:nexo:%subaccount:%input_currency

# Actual loan deposit into account (ex. USDC). Use for opening position.
# NOTE: for past years, this did not exist. You will need to manually add a borrow tag somehow
if %type ^Deposit$
& %description_ ^Credit Granting Top Up$
  account2 equity:nexo:%subaccount:loans:%input_currency
  comment description:%description_, code:%code_, status:%status_, type:%type, direction:%direction, taxed_as:BORROW
  comment2 %date +0000,BORROW,nexo:%subaccount,%input_currency,%input_amount,USD,%usd_amount,

# Charged compounding interest on loan
# NOTE: 2020-2021 rule: if %description_ Interest Paid
if %description_ (Paid|Overdraft)
  account1 liabilities:nexo:%subaccount:receivable:loans:%input_currency
  account2 assets:nexo:%subaccount:%input_currency
  comment description:%description_, code:%code_, status:%status_, direction:%direction, type:InterestPaid

# Charged compounding interest on loan cont.
if %type ^InterestAdditional$
  account1 liabilities:nexo:%subaccount:receivable:loans:%input_currency
  account2 assets:nexo:%subaccount:%input_currency
  comment description:%description_, code:%code_, status:%status_, direction:%direction, type:InterestPaid

# ---------------------------------------------------------------------------- #
#                                    Repay                                     #
# ---------------------------------------------------------------------------- #

# USD loan repayment
if %type ^Repayment$
  account1 liabilities:nexo:%subaccount:receivable:loans:%input_currency
  account2 assets:nexo:%subaccount:%input_currency

# Liquidation (selling of asset to pay off USD equivalent)
# TODO: really an expense?
if %type ^Liquidation$
& %description_ ^Crypto Repayment$
  amount -%input_amount "%input_currency" @@ %usd_amount USD
  account2 expenses:nexo:%subaccount:liquidations:USD
  comment description:%description_, code:%code_, status:%status_, type:%type, direction:%direction, taxed_as:REPAY
  comment2 %date +0000,REPAY,nexo:%subaccount,%input_currency,%input_amount,USD,%usd_amount,

################################################################################
#                                                                              #
#                                   INCOME                                     #
#                                                                              #
################################################################################

# Earned income by description
# NOTE: not a fixed string
if %description_ Interest Earned$
  amount %input_amount "%input_currency" @@ %usd_amount USD
  account2 equity:nexo:%subaccount:conversion:USD
  amount3 %usd_amount USD @@ %input_amount "%input_currency"
  account3 equity:nexo:%subaccount:conversion:USD
  amount4 -%input_amount "%input_currency"
  account4 income:nexo:%subaccount:interest:%input_currency
  comment description:%description_, code:%code_, status:%status_, type:InterestEarned, direction:%direction, taxed_as:INCOME
  comment2 %date +0000,INCOME,nexo:%subaccount,%input_currency,%input_amount,USD,%usd_amount,INTEREST

# Earned income by type
if %type ^FixedTermInterest$
  amount %input_amount "%input_currency" @@ %usd_amount USD
  account2 equity:nexo:%subaccount:conversion:USD
  amount3 %usd_amount USD @@ %input_amount "%input_currency"
  account3 equity:nexo:%subaccount:conversion:USD
  amount4 -%input_amount "%input_currency"
  account4 income:nexo:%subaccount:interest:%input_currency
  comment description:%description_, code:%code_, status:%status_, type:InterestEarned, direction:%direction, taxed_as:INCOME
  comment2 %date +0000,INCOME,nexo:%subaccount,%input_currency,%input_amount,USD,%usd_amount,INTEREST

# Airdrops
# NOTE: not a fixed string
if %description_ Airdrop
  amount %input_amount "%input_currency" @@ %usd_amount USD
  account2 equity:nexo:%subaccount:conversion:USD
  amount3 %usd_amount USD @@ %input_amount "%input_currency"
  account3 equity:nexo:%subaccount:conversion:USD
  amount4 -%input_amount "%input_currency"
  account4 income:nexo:%subaccount:interest:%input_currency
  comment description:%description_, code:%code_, status:%status_, type:Airdrop, direction:%direction, taxed_as:INCOME
  comment2 %date +0000,INCOME,nexo:%subaccount,%input_currency,%input_amount,USD,%usd_amount,AIRDROP

# This is not income. Apparently, Nexo puts this to debt balance (reduces loan amount).
if %type ^Administrator$
  account1 liabilities:nexo:%subaccount:receivable:loans:%input_currency
  account2 assets:nexo:%subaccount:%input_currency

# Dividends (now deprecated, last dividend was released 2021)
if %type ^Dividend$
  amount %input_amount "%input_currency" @@ %usd_amount USD
  account2 equity:nexo:%subaccount:conversion:USD
  amount3 %usd_amount USD @@ %input_amount "%input_currency"
  account3 equity:nexo:%subaccount:conversion:USD
  amount4 -%input_amount "%input_currency"
  account4 income:nexo:%subaccount:dividend:%input_currency
  comment description:%description_, code:%code_, status:%status_, type:%type, direction:%direction, taxed_as:INCOME
  comment2 %date +0000,INCOME,nexo:%subaccount,%input_currency,%input_amount,USD,%usd_amount,DIVIDEND

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

# Exchange
if %type ^Exchange$
  amount -%input_amount "%input_currency" @@ %output_amount "%output_currency"
  account2 assets:nexo:%subaccount:%output_currency
  comment description:%description_, code:%code_, status:%status_, type:%type, taxed_as:SELL
  comment2 %date +0000,SELL,nexo:%subaccount,%input_currency,%input_amount,%output_currency,%output_amount,,

# Exchange Cashback
# NOTE: cashback rewards are considered rebates and not taxable income
if %type ^Exchange Cashback$
  amount %input_amount "%input_currency" @@ %usd_amount USD
  account2 equity:nexo:%subaccount:conversion:USD
  amount3 %usd_amount USD @@ %input_amount %input_currency
  account3 equity:nexo:%subaccount:conversion:%input_currency
  amount4 -%input_amount %input_currency
  account4 income:nexo:%subaccount:exchange_cashback:%input_currency
  comment description:%description_, code:%code_, status:%status_, type:%type, taxed_as:REBATE
  comment2 %date +0000,BUY,nexo:%subaccount,%input_currency,%input_amount,USD,%usd_amount,,

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