maybe-finance / maybe

The OS for your personal finances
https://maybe.co
GNU Affero General Public License v3.0
33.93k stars 2.44k forks source link

Add Investment Transaction Type #888

Closed zachgoll closed 4 months ago

zachgoll commented 4 months ago

In order to support portfolio management, we will need to introduce an InvestmentTransaction type.

Account "Entries"

To support this new hierarchy, I came up with the following delegated types hierarchy:

class Account::Entry < ApplicationRecord
  belongs_to :account

  delegated_type :entryable, types: %w[ Valuation Transaction Trade CryptoTrade ]
end

module Account::Entryable
  extend ActiveSupport::Concern

  included do
    has_one :entry, as: :entryable, touch: true
  end
end

# Point-in-time valuation of an account
class Valuation < ApplicationRecord
  include Account::Entryable
end

class Transaction < ApplicationRecord
  include Account::Entryable
end

# i.e. buy/sell of AAPL
class Trade < ApplicationRecord
  include Account::Entryable
end

# Future extension, will not be implemented yet
class CryptoTrade < ApplicationRecord
  include Account::Entryable
end

Base "Entry"

In this schema, an Account::Entry represents anything that affects an Account value. Each "Entry" will have a common set of attributes:

Naming rationale

I had originally come up with Account::Event for this base model, but I now believe Account::Entry is a more appropriate name. An "event" is too generic and could easily collide with non-financial (i.e. has no amount field) events on an account such as a "creation", "closure", "sync", or even "edit"s.

Additionally, an "Entry" fits well in the finance space as it overlaps with the accounting concept of a "Journal Entry" or "Ledger Entry" that indicates a change to the account's value.

Valuation Entries

This will require a slight refactor as the existing sync process incorporates both transactions and valuations.

Right now, this will be an empty model, but in the future will hold metadata about the valuation (i.e. valuation source, valuation method, etc.)

Transaction Entries

Represents a regular transaction, often an "income" or "expense" transaction that can have categories, tags, merchants, and other "budgeting" related metadata.

Trade Entries (investment)

Represents a buy/sell/other "stock trade" entry.

Crypto Entries

This will not be implemented here for this issue, but is a preview of how this hierarchy might work as we incorporate new event types like a "crypto trade"

zachgoll commented 4 months ago

See #892 for some additional context around this. I'll be doing namespace changes prior to this implementation.

oliveratgithub commented 4 months ago

+1 !

I was really surprised about seeing, that Maybe does not (yet) support Investments / Investment Portfolios – I was under the impression that was the main purpose for Maybe ^^ So very much looking forward to this!!

zachgoll commented 4 months ago

@oliveratgithub yep, the prior version of Maybe was heavily focused on investments from the get-go. This rework will also put a huge emphasis on investing, but we've started with a lot of the budgeting/transactions stuff early on so we can support all types of use cases for users across different walks of life!