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
|
||||
}
|
||||
|
Reference in New Issue
Block a user