Documentation ¶
Index ¶
- Variables
- func Connect(dsn string) error
- type Account
- func (a Account) Balance(db *gorm.DB, time time.Time) (balance decimal.Decimal, err error)
- func (a *Account) BeforeSave(_ *gorm.DB) error
- func (a Account) BeforeUpdate(tx *gorm.DB) (err error)
- func (a Account) GetBalanceMonth(db *gorm.DB, month types.Month) (balance, available decimal.Decimal, err error)
- func (a Account) RecentEnvelopes(db *gorm.DB) ([]*uuid.UUID, error)
- func (a Account) ReconciledBalance(db *gorm.DB, time time.Time) (balance decimal.Decimal, err error)
- func (Account) Self() string
- func (a Account) SumReconciled(db *gorm.DB) (balance decimal.Decimal, err error)
- func (a Account) Transactions(db *gorm.DB) []Transaction
- type AggregatedTransaction
- type Budget
- func (b Budget) Allocated(db *gorm.DB, month types.Month) (allocated decimal.Decimal, err error)
- func (b Budget) Balance(tx *gorm.DB) (balance decimal.Decimal, err error)
- func (b *Budget) BeforeSave(_ *gorm.DB) error
- func (b Budget) Income(db *gorm.DB, month types.Month) (income decimal.Decimal, err error)
- func (b Budget) Self() string
- type Category
- type DefaultModel
- type EZContext
- type Envelope
- func (e Envelope) Balance(db *gorm.DB, month types.Month) (decimal.Decimal, error)
- func (e *Envelope) BeforeSave(_ *gorm.DB) error
- func (e *Envelope) BeforeUpdate(tx *gorm.DB) (err error)
- func (e Envelope) Month(db *gorm.DB, month types.Month) (EnvelopeMonth, error)
- func (e Envelope) Self() string
- func (e Envelope) Spent(db *gorm.DB, month types.Month) decimal.Decimal
- type EnvelopeMonth
- type Goal
- type MatchRule
- type Model
- type MonthConfig
- type Timestamps
- type Transaction
Constants ¶
This section is empty.
Variables ¶
var ( ErrAllocationZero = errors.New("allocation amounts must be non-zero. Instead of setting to zero, delete the Allocation") ErrGoalAmountNotPositive = errors.New("goal amounts must be larger than zero") )
var DB *gorm.DB
Functions ¶
Types ¶
type Account ¶
type Account struct { DefaultModel Budget Budget BudgetID uuid.UUID `gorm:"uniqueIndex:account_name_budget_id"` Name string `gorm:"uniqueIndex:account_name_budget_id"` Note string OnBudget bool External bool InitialBalance decimal.Decimal `gorm:"type:DECIMAL(20,8)"` InitialBalanceDate *time.Time Archived bool ImportHash string // A SHA256 hash of a unique combination of values to use in duplicate detection for imports }
Account represents an asset account, e.g. a bank account.
func (Account) Balance ¶ added in v4.2.0
Balance calculates the balance of the account at a specific point in time, including all transactions
func (*Account) BeforeSave ¶
BeforeSave ensures consistency for the account
It enforces OnBudget to be false when the account is external.
It trims whitespace from all strings
func (Account) BeforeUpdate ¶
BeforeUpdate verifies the state of the account before committing an update to the database.
func (Account) GetBalanceMonth ¶
func (a Account) GetBalanceMonth(db *gorm.DB, month types.Month) (balance, available decimal.Decimal, err error)
GetBalanceMonth calculates the balance and available sums for a specific month.
The balance Decimal is the actual account balance, factoring in all transactions before the end of the month. The available Decimal is the sum that is available for budgeting at the end of the specified month.
TODO: Get rid of this in favor of Balance()
func (Account) RecentEnvelopes ¶
SetRecentEnvelopes returns the most common envelopes used in the last 50 transactions where the account is the destination account.
The list is sorted by decending frequency of the envelope being used. If two envelopes appear with the same frequency, the order is undefined since sqlite does not have ordering by datetime with more than second precision.
If creation times are more than a second apart, ordering is well defined.
func (Account) ReconciledBalance ¶ added in v4.2.0
func (a Account) ReconciledBalance(db *gorm.DB, time time.Time) (balance decimal.Decimal, err error)
ReconciledBalance calculates the reconciled balance at a specific point in time
func (Account) SumReconciled ¶
Transactions returns all transactions for this account. TODO: Remove in favor of ReconciledBalance
func (Account) Transactions ¶
func (a Account) Transactions(db *gorm.DB) []Transaction
Transactions returns all transactions for this account.
type AggregatedTransaction ¶
type Budget ¶
type Budget struct { DefaultModel Name string Note string Currency string }
Budget represents a budget
A budget is the highest level of organization in Envelope Zero, all other resources reference it directly or transitively.
type Category ¶
type Category struct { DefaultModel Budget Budget BudgetID uuid.UUID `gorm:"uniqueIndex:category_budget_name"` Name string `gorm:"uniqueIndex:category_budget_name"` Note string Archived bool }
Category represents a category of envelopes.
func (*Category) BeforeUpdate ¶
BeforeUpdate archives all envelopes when the category is archived.
type DefaultModel ¶
type DefaultModel struct { ID uuid.UUID `json:"id" example:"65392deb-5e92-4268-b114-297faad6cdce"` // UUID for the resource Timestamps }
DefaultModel is the base model for most models in Envelope Zero. As EnvelopeMonth uses the Envelope ID and the Month as primary key, the timestamps are managed in the Timestamps struct.
func (*DefaultModel) AfterFind ¶
func (m *DefaultModel) AfterFind(_ *gorm.DB) (err error)
AfterFind updates the timestamps to use UTC as timezone, not +0000. Yes, this is different.
We already store them in UTC, but somehow reading them from the database returns them as +0000.
func (*DefaultModel) BeforeCreate ¶
func (m *DefaultModel) BeforeCreate(_ *gorm.DB) (err error)
BeforeCreate is set to generate a UUID for the resource.
type EZContext ¶ added in v4.2.0
type EZContext string
const (
DBContextURL EZContext = "ez-backend-url"
)
type Envelope ¶
type Envelope struct { DefaultModel Category Category CategoryID uuid.UUID `gorm:"uniqueIndex:envelope_category_name"` Name string `gorm:"uniqueIndex:envelope_category_name"` Note string Archived bool }
Envelope represents an envelope in your budget.
func (*Envelope) BeforeUpdate ¶
BeforeUpdate verifies the state of the envelope before committing an update to the database.
type EnvelopeMonth ¶
type EnvelopeMonth struct { Envelope Spent decimal.Decimal `json:"spent" example:"73.12"` // The amount spent over the whole month Balance decimal.Decimal `json:"balance" example:"12.32"` // The balance at the end of the monht Allocation decimal.Decimal `json:"allocation" example:"85.44"` // The amount of money allocated }
EnvelopeMonth contains data about an Envelope for a specific month.
type Goal ¶
type MatchRule ¶
type MatchRule struct { DefaultModel AccountID uuid.UUID Priority uint Match string }
type MonthConfig ¶
type MonthConfig struct { Timestamps EnvelopeID uuid.UUID `gorm:"primaryKey"` // ID of the envelope Month types.Month `gorm:"primaryKey"` Allocation decimal.Decimal `gorm:"type:DECIMAL(20,8)"` Note string }
func (*MonthConfig) BeforeSave ¶
func (m *MonthConfig) BeforeSave(_ *gorm.DB) error
func (MonthConfig) Self ¶
func (m MonthConfig) Self() string
type Timestamps ¶
type Timestamps struct { CreatedAt time.Time `json:"createdAt" example:"2022-04-02T19:28:44.491514Z"` // Time the resource was created UpdatedAt time.Time `json:"updatedAt" example:"2022-04-17T20:14:01.048145Z"` // Last time the resource was updated DeletedAt *gorm.DeletedAt `json:"deletedAt" gorm:"index" example:"2022-04-22T21:01:05.058161Z" swaggertype:"primitive,string"` // Time the resource was marked as deleted }
Timestamps only contains the timestamps that gorm sets automatically to enable other primary keys than ID.
type Transaction ¶
type Transaction struct { DefaultModel BudgetID uuid.UUID Budget Budget SourceAccountID uuid.UUID `gorm:"check:source_destination_different,source_account_id != destination_account_id"` SourceAccount Account DestinationAccountID uuid.UUID DestinationAccount Account EnvelopeID *uuid.UUID Envelope Envelope Date time.Time // Time of day is currently only used for sorting Amount decimal.Decimal `gorm:"type:DECIMAL(20,8)"` Note string ReconciledSource bool // Is the transaction reconciled in the source account? ReconciledDestination bool // Is the transaction reconciled in the destination account? AvailableFrom types.Month // Only used for income transactions. Defaults to the transaction date. ImportHash string // The SHA256 hash of a unique combination of values to use in duplicate detection when importing transactions }
Transaction represents a transaction between two accounts.
func (*Transaction) AfterFind ¶
func (t *Transaction) AfterFind(tx *gorm.DB) (err error)
AfterFind updates the timestamps to use UTC as timezone, not +0000. Yes, this is different.
We already store them in UTC, but somehow reading them from the database returns them as +0000.
func (*Transaction) BeforeSave ¶
func (t *Transaction) BeforeSave(tx *gorm.DB) (err error)
BeforeSave
- sets the timezone for the Date for UTC
- ensures that ReconciledSource and ReconciledDestination are set to valid values
- trims whitespace from string fields
func (Transaction) Self ¶
func (t Transaction) Self() string