1
0
Fork 0
mirror of https://github.com/maybe-finance/maybe.git synced 2025-08-04 21:15:19 +02:00

Basic trade and holdings view (#1271)

* Add trade view

* Lint fix

* Fix stale placeholder variable

* Add holding view
This commit is contained in:
Zach Gollwitzer 2024-10-09 14:59:18 -04:00 committed by GitHub
parent f5cb13b42f
commit 4bfe47540d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
25 changed files with 387 additions and 68 deletions

View file

@ -109,8 +109,8 @@ class Account::Entry < ApplicationRecord
def bulk_update!(bulk_update_params)
bulk_attributes = {
date: bulk_update_params[:date],
notes: bulk_update_params[:notes],
entryable_attributes: {
notes: bulk_update_params[:notes],
category_id: bulk_update_params[:category_id],
merchant_id: bulk_update_params[:merchant_id]
}.compact_blank

View file

@ -37,6 +37,19 @@ class Account::Holding < ApplicationRecord
@trend ||= calculate_trend
end
def trades
account.entries.where(entryable: account.trades.where(security: security)).reverse_chronological
end
def destroy_holding_and_entries!
transaction do
account.entries.where(entryable: account.trades.where(security: security)).destroy_all
destroy
end
account.sync_later
end
private
def calculate_trend

View file

@ -25,4 +25,15 @@ class Account::Trade < ApplicationRecord
def buy?
qty > 0
end
def unrealized_gain_loss
return nil if sell?
current_price = security.current_price
return nil if current_price.nil?
current_value = current_price * qty.abs
cost_basis = price_money * qty.abs
TimeSeries::Trend.new(current: current_value, previous: cost_basis)
end
end

View file

@ -34,7 +34,8 @@ class MintImport < Import
amount: row.signed_amount,
name: row.name,
currency: row.currency,
entryable: Account::Transaction.new(category: category, tags: tags, notes: row.notes),
notes: row.notes,
entryable: Account::Transaction.new(category: category, tags: tags),
import: self
entry.save!

View file

@ -5,6 +5,12 @@ class Security < ApplicationRecord
validates :ticker, presence: true, uniqueness: { case_sensitive: false }
def current_price
@current_price ||= Security::Price.find_price(ticker:, date: Date.current)
return nil if @current_price.nil?
Money.new(@current_price.price, @current_price.currency)
end
private
def upcase_ticker

View file

@ -13,7 +13,8 @@ class TransactionImport < Import
amount: row.signed_amount,
name: row.name,
currency: row.currency,
entryable: Account::Transaction.new(category: category, tags: tags, notes: row.notes),
notes: row.notes,
entryable: Account::Transaction.new(category: category, tags: tags),
import: self
entry.save!