Skip to content

Commit 728a773

Browse files
committed
feat: add date for initial balance
With this, the initial balance of an account can be set to a specific date so that calculations prior to creation of the account can be correct, e.g. the sum available to budget. The YNAB 4 importer sets the initial balance date automatically from the YNAB 4 starting balance transactions.
1 parent 97cdba0 commit 728a773

File tree

5 files changed

+39
-13
lines changed

5 files changed

+39
-13
lines changed

api/docs.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2727,6 +2727,10 @@ const docTemplate = `{
27272727
"default": 0,
27282728
"example": 173.12
27292729
},
2730+
"initialBalanceDate": {
2731+
"type": "string",
2732+
"example": "2017-05-12T00:00:00Z"
2733+
},
27302734
"links": {
27312735
"$ref": "#/definitions/controllers.AccountLinks"
27322736
},
@@ -3453,6 +3457,10 @@ const docTemplate = `{
34533457
"default": 0,
34543458
"example": 173.12
34553459
},
3460+
"initialBalanceDate": {
3461+
"type": "string",
3462+
"example": "2017-05-12T00:00:00Z"
3463+
},
34563464
"name": {
34573465
"type": "string",
34583466
"example": "Cash"

api/swagger.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2715,6 +2715,10 @@
27152715
"default": 0,
27162716
"example": 173.12
27172717
},
2718+
"initialBalanceDate": {
2719+
"type": "string",
2720+
"example": "2017-05-12T00:00:00Z"
2721+
},
27182722
"links": {
27192723
"$ref": "#/definitions/controllers.AccountLinks"
27202724
},
@@ -3441,6 +3445,10 @@
34413445
"default": 0,
34423446
"example": 173.12
34433447
},
3448+
"initialBalanceDate": {
3449+
"type": "string",
3450+
"example": "2017-05-12T00:00:00Z"
3451+
},
34443452
"name": {
34453453
"type": "string",
34463454
"example": "Cash"

api/swagger.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ definitions:
2828
default: 0
2929
example: 173.12
3030
type: number
31+
initialBalanceDate:
32+
example: "2017-05-12T00:00:00Z"
33+
type: string
3134
links:
3235
$ref: '#/definitions/controllers.AccountLinks'
3336
name:
@@ -552,6 +555,9 @@ definitions:
552555
default: 0
553556
example: 173.12
554557
type: number
558+
initialBalanceDate:
559+
example: "2017-05-12T00:00:00Z"
560+
type: string
555561
name:
556562
example: Cash
557563
type: string

pkg/importer/parser/ynab4/parse.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -262,24 +262,25 @@ func parseTransactions(resources *types.ParsedResources, transactions []Transact
262262
addNoPayee = true
263263
}
264264

265+
// Parse the date of the transaction
266+
date, err := time.Parse("2006-01-02", transaction.Date)
267+
if err != nil {
268+
return fmt.Errorf("could not parse date, the Budget.yfull file seems to be corrupt: %w", err)
269+
}
270+
265271
// Envelope Zero does not use a magic “Starting Balance” account, instead
266272
// every account has a field for the starting balance
267273
if payee == "Starting Balance" {
268274
account := resources.Accounts[accountIDNames[transaction.AccountID]]
269275
account.Model.InitialBalance = transaction.Amount
276+
account.Model.InitialBalanceDate = &date
270277

271278
resources.Accounts[accountIDNames[transaction.AccountID]] = account
272279

273280
// Initial balance is set, no more processing needed
274281
continue
275282
}
276283

277-
// Parse the date of the transaction
278-
date, err := time.Parse("2006-01-02", transaction.Date)
279-
if err != nil {
280-
return fmt.Errorf("could not parse date, the Budget.yfull file seems to be corrupt: %w", err)
281-
}
282-
283284
newTransaction := types.Transaction{
284285
Model: models.Transaction{
285286
TransactionCreate: models.TransactionCreate{

pkg/models/account.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package models
22

33
import (
4+
"time"
5+
46
"github.com/google/uuid"
57
"github.com/shopspring/decimal"
68
"gorm.io/gorm"
@@ -16,13 +18,14 @@ type Account struct {
1618
}
1719

1820
type AccountCreate struct {
19-
Name string `json:"name" example:"Cash" default:""`
20-
Note string `json:"note" example:"Money in my wallet" default:""`
21-
BudgetID uuid.UUID `json:"budgetId" example:"550dc009-cea6-4c12-b2a5-03446eb7b7cf"`
22-
OnBudget bool `json:"onBudget" example:"true" default:"false"` // Always false when external: true
23-
External bool `json:"external" example:"false" default:"false"`
24-
InitialBalance decimal.Decimal `json:"initialBalance" example:"173.12" default:"0"`
25-
Hidden bool `json:"hidden" example:"true" default:"false"`
21+
Name string `json:"name" example:"Cash" default:""`
22+
Note string `json:"note" example:"Money in my wallet" default:""`
23+
BudgetID uuid.UUID `json:"budgetId" example:"550dc009-cea6-4c12-b2a5-03446eb7b7cf"`
24+
OnBudget bool `json:"onBudget" example:"true" default:"false"` // Always false when external: true
25+
External bool `json:"external" example:"false" default:"false"`
26+
InitialBalance decimal.Decimal `json:"initialBalance" example:"173.12" default:"0"`
27+
InitialBalanceDate *time.Time `json:"initialBalanceDate" example:"2017-05-12T00:00:00Z"`
28+
Hidden bool `json:"hidden" example:"true" default:"false"`
2629
}
2730

2831
func (a Account) WithCalculations(db *gorm.DB) Account {

0 commit comments

Comments
 (0)