add support for bitcoin
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
@ -2,6 +2,7 @@ package ynab
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"net/url"
|
||||
)
|
||||
|
||||
@ -38,3 +39,43 @@ func (c *Client) GetAccount(accountID string) (*Accounts, error) {
|
||||
|
||||
return &response, nil
|
||||
}
|
||||
|
||||
// Creates a "Capital Gains or Losses" adjustment transaction or updates an existing one if it exists to ensure the account balance
|
||||
// for the accountID provided equals the balance provided
|
||||
func (c *Client) SetAccountBalance(accountID string, balance int) error {
|
||||
|
||||
ynabTransactionID, ynabTransactionAmount, err := c.getTodayYnabCapitalGainsTransaction(accountID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to get ynab capital gains transaction ID: %v", err)
|
||||
}
|
||||
|
||||
ynabAccount, err := c.GetAccount(accountID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to get ynab account with id '%s': %v", accountID, err)
|
||||
}
|
||||
|
||||
balanceDelta := balance - ynabAccount.Data.Account.Balance
|
||||
balanceDelta += ynabTransactionAmount // Take into account the existing transaction
|
||||
|
||||
if balanceDelta == 0 {
|
||||
return nil // If balanceDelta is 0 do not create a transaction i.e. market is closed today
|
||||
}
|
||||
|
||||
if ynabTransactionID == "" {
|
||||
// there is no transaction - so create a new one
|
||||
err = c.createTodayYNABCapitalGainsTransaction(accountID, balanceDelta)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to create YNAB capital gains transaction for account ID '%s': %v", accountID, err)
|
||||
}
|
||||
log.Printf("Creating new capital gains transaction for YNAB account '%s' for amount: %d", accountID, balanceDelta)
|
||||
|
||||
} else {
|
||||
// there is an existing transaction - so update the existing one
|
||||
err = c.updateTodayYNABCapitalGainsTransaction(accountID, ynabTransactionID, balanceDelta)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to update YNAB capital gains transaction for account ID '%s': %v", accountID, err)
|
||||
}
|
||||
log.Printf("Updating existing capital gains transaction for YNAB account '%s' for amount: %d", accountID, balanceDelta)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
@ -70,7 +70,7 @@ func (c *Client) GetAccountTransactions(accountID string, sinceDate time.Time) (
|
||||
|
||||
// Accepts a YNAB account ID and returns the transaction ID, amount and an error for the
|
||||
// the first transaction found with Payee Name "Capital Gains or Losses"
|
||||
func (c *Client) GetTodayYnabCapitalGainsTransaction(accountID string) (string, int, error) {
|
||||
func (c *Client) getTodayYnabCapitalGainsTransaction(accountID string) (string, int, error) {
|
||||
ynabTransactions, err := c.GetAccountTransactions(accountID, time.Now())
|
||||
if err != nil {
|
||||
return "", 0, fmt.Errorf("failed to get ynab transactions: %v", err)
|
||||
@ -87,7 +87,7 @@ func (c *Client) GetTodayYnabCapitalGainsTransaction(accountID string) (string,
|
||||
}
|
||||
|
||||
// Accepts a YNAB account ID and transaction amount and creates a new YNAB transaction
|
||||
func (c *Client) CreateTodayYNABCapitalGainsTransaction(accountID string, amount int) error {
|
||||
func (c *Client) createTodayYNABCapitalGainsTransaction(accountID string, amount int) error {
|
||||
transaction := TransactionRequest{}
|
||||
transaction.Transaction.AccountID = accountID
|
||||
transaction.Transaction.Amount = amount
|
||||
@ -106,7 +106,7 @@ func (c *Client) CreateTodayYNABCapitalGainsTransaction(accountID string, amount
|
||||
}
|
||||
|
||||
// Accepts a YNAB account ID, transaction ID and transaction amount and updates the YNAB transaction with the matching ID
|
||||
func (c *Client) UpdateTodayYNABCapitalGainsTransaction(accountID string, transactionID string, amount int) error {
|
||||
func (c *Client) updateTodayYNABCapitalGainsTransaction(accountID string, transactionID string, amount int) error {
|
||||
transaction := TransactionRequest{}
|
||||
transaction.Transaction.AccountID = accountID
|
||||
transaction.Transaction.ID = transactionID
|
||||
|
Reference in New Issue
Block a user