models

package
v4.2.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 10, 2024 License: AGPL-3.0 Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
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")
)
View Source
var DB *gorm.DB

Functions

func Connect added in v4.2.0

func Connect(dsn string) error

Connect opens the SQLite database and configures the connection pool.

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

func (a Account) Balance(db *gorm.DB, time time.Time) (balance decimal.Decimal, err error)

Balance calculates the balance of the account at a specific point in time, including all transactions

func (*Account) BeforeSave

func (a *Account) BeforeSave(_ *gorm.DB) error

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

func (a Account) BeforeUpdate(tx *gorm.DB) (err error)

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

func (a Account) RecentEnvelopes(db *gorm.DB) ([]*uuid.UUID, error)

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) Self

func (Account) Self() string

func (Account) SumReconciled

func (a Account) SumReconciled(db *gorm.DB) (balance decimal.Decimal, err error)

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 AggregatedTransaction struct {
	Amount                     decimal.Decimal
	Date                       time.Time
	SourceAccountOnBudget      bool
	DestinationAccountOnBudget bool
}

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.

func (Budget) Allocated

func (b Budget) Allocated(db *gorm.DB, month types.Month) (allocated decimal.Decimal, err error)

Allocated calculates the sum that has been budgeted for a specific month.

func (Budget) Balance

func (b Budget) Balance(tx *gorm.DB) (balance decimal.Decimal, err error)

Balance calculates the balance for a budget.

func (*Budget) BeforeSave

func (b *Budget) BeforeSave(_ *gorm.DB) error

func (Budget) Income

func (b Budget) Income(db *gorm.DB, month types.Month) (income decimal.Decimal, err error)

Income returns the income for a budget in a given month.

func (Budget) Self

func (b Budget) Self() string

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) BeforeSave

func (c *Category) BeforeSave(_ *gorm.DB) error

func (*Category) BeforeUpdate

func (c *Category) BeforeUpdate(tx *gorm.DB) (err error)

BeforeUpdate archives all envelopes when the category is archived.

func (*Category) Envelopes

func (c *Category) Envelopes(tx *gorm.DB) ([]Envelope, error)

func (Category) Self

func (c Category) Self() string

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) Balance

func (e Envelope) Balance(db *gorm.DB, month types.Month) (decimal.Decimal, error)

Balance calculates the balance of an Envelope in a specific month.

func (*Envelope) BeforeSave

func (e *Envelope) BeforeSave(_ *gorm.DB) error

func (*Envelope) BeforeUpdate

func (e *Envelope) BeforeUpdate(tx *gorm.DB) (err error)

BeforeUpdate verifies the state of the envelope before committing an update to the database.

func (Envelope) Month

func (e Envelope) Month(db *gorm.DB, month types.Month) (EnvelopeMonth, error)

Month calculates the month specific values for an envelope and returns an EnvelopeMonth and allocation ID for them.

func (Envelope) Self

func (e Envelope) Self() string

func (Envelope) Spent

func (e Envelope) Spent(db *gorm.DB, month types.Month) decimal.Decimal

Spent returns the amount spent for the month the time.Time instance is in.

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 Goal struct {
	DefaultModel
	Name       string `gorm:"uniqueIndex:goal_name_envelope"`
	Note       string
	Envelope   Envelope
	EnvelopeID uuid.UUID       `gorm:"uniqueIndex:goal_name_envelope"`
	Amount     decimal.Decimal `gorm:"type:DECIMAL(20,8)"` // The target for the goal
	Month      types.Month
	Archived   bool
}

func (*Goal) AfterSave

func (g *Goal) AfterSave(_ *gorm.DB) error

func (*Goal) BeforeSave

func (g *Goal) BeforeSave(_ *gorm.DB) error

func (Goal) Self

func (g Goal) Self() string

type MatchRule

type MatchRule struct {
	DefaultModel
	AccountID uuid.UUID
	Priority  uint
	Match     string
}

func (MatchRule) Self

func (r MatchRule) Self() string

type Model

type Model interface {
	Self() 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

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL