# 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/>.

# TODO: leaving out balance assertions because coinbase-pro apparently rounds up fiat...
fields account_id,account_currency,id,amount_,balance_,created_at,type,details_transfer_id,details_transfer_type,details_order_id,details_product_id,details_trade_id,destination,subtotal,fee,total,txid,direction,subaccount

date %created_at
date-format %Y-%m-%d %H:%M:%S

description %created_at +0000

account1 assets:coinbase-pro:%subaccount:%account_currency
amount %amount_ %account_currency

comment type:%type, account_id:%account_id, order_id:%details_order_id, trade_id:%details_trade_id, txid:%id, details_product_id:%details_product_id

if %direction [A-Z]
  comment type:%type, account_id:%account_id, order_id:%details_order_id, trade_id:%details_trade_id, txid:%id, details_product_id:%details_product_id, direction:%direction

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

# Skip trade withdrawals, using withdraw impl
if %type ^transfer$
& %details_transfer_type ^withdraw$
  skip

if %type ^transfer$
  account2 equity:coinbase-pro:%subaccount:withdrawal:%account_currency
  comment type:%type, account_id:%account_id, details_transfer_id:%details_transfer_id, details_transfer_type:%details_transfer_type, txid:%id, direction:%direction

# Use custom withdraw
if %type ^transfer$
& %details_transfer_type ^transfer_out$
  amount -%total %account_currency
  account2 equity:coinbase-pro:%subaccount:deposit:%account_currency
  comment type:%type, account_id:%account_id, details_transfer_type:%details_transfer_type, destination:%destination, txid:%txid, direction:%direction

if %type ^transfer$
& %details_transfer_type ^transfer_out$
& %destination ^[^0-9a-z]*$
  comment type:%type, account_id:%account_id, details_transfer_type:%details_transfer_type, txid:%txid, direction:%direction

if %type ^transfer$
& %details_transfer_type ^transfer_out$
& %destination ^[^0-9a-z]*$
& %txid ^[^0-9a-z]*$
  comment type:%type, account_id:%account_id, details_transfer_type:%details_transfer_type, direction:%direction

# transfer w/ fees
if %type ^transfer$
& %details_transfer_type ^transfer_out$
& %fee [1-9]
  amount -%subtotal %account_currency
  account2 equity:coinbase-pro:%subaccount:deposit:%account_currency
  amount2 %subtotal %account_currency
  account3 assets:coinbase-pro:%subaccount:%account_currency
  amount3 -%fee %account_currency
  account4 expenses:coinbase-pro:%subaccount:fees:%account_currency
  amount4 %fee %account_currency
  comment type:%type, account_id:%account_id, details_transfer_type:%details_transfer_type, destination:%destination, txid:%txid, direction:%direction, taxed_as:SPEND
  comment3 %created_at +0000,SPEND,coinbase-pro:%subaccount,%account_currency,%fee,USD,,FEE

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

if %type ^match$
& %details_product_id [A-Z]
  account2 equity:coinbase-pro:%subaccount:withdrawal:%account_currency
  comment type:%type, account_id:%account_id, order_id:%details_order_id, trade_id:%details_trade_id, txid:%id, details_product_id:%details_product_id, taxed_as:MATCH
  comment2 %details_trade_id,%created_at +0000,BUY,coinbase-pro:%subaccount,%account_currency,%amount_

if %type ^match$
& %details_product_id [A-Z]
& %amount_ [\\-]
  account2 equity:coinbase-pro:%subaccount:deposit:%account_currency
  comment2 %details_trade_id,%created_at +0000,SELL,coinbase-pro:%subaccount,%account_currency,%amount_

if %type ^fee$
  account2 expenses:coinbase-pro:%subaccount:fees:%account_currency
  comment type:%type, account_id:%account_id, order_id:%details_order_id, trade_id:%details_trade_id, txid:%id, details_product_id:%details_product_id, taxed_as:MATCH
  comment2 %details_trade_id,%created_at +0000,FEE,coinbase-pro:%subaccount,%account_currency,%amount_

if %type ^conversion$
  comment type:%type, account_id:%account_id, txid:%id

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