mirror of
https://github.com/we-promise/sure.git
synced 2026-04-19 12:04:08 +00:00
Basic trade and holdings view (#1271)
* Add trade view * Lint fix * Fix stale placeholder variable * Add holding view
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user