mirror of
https://github.com/we-promise/sure.git
synced 2026-04-08 06:44:52 +00:00
* Add investment tracking to expenses Add new sections to dashboard and reporting around investments. * Create investment-integration-assessment.md * Delete .claude/settings.local.json Signed-off-by: soky srm <sokysrm@gmail.com> * Category trades * Simplify * Simplification and test fixes * FIX merge * Update views * Update 20251125141213_add_category_to_trades.rb * FIX tests * FIX statements and account status * cleanup * Add default cat for csv imports * Delete docs/roadmap/investment-integration-assessment.md Signed-off-by: soky srm <sokysrm@gmail.com> * Update trend calculation Use already existing column cost basis for trend calculation - Current value: qty * price (already stored as amount) - Cost basis total: qty * cost_basis - Unrealized gain: current value - cost basis total Fixes N+1 query also --------- Signed-off-by: soky srm <sokysrm@gmail.com>
39 lines
799 B
Ruby
39 lines
799 B
Ruby
class Trade < ApplicationRecord
|
|
include Entryable, Monetizable
|
|
|
|
monetize :price
|
|
|
|
belongs_to :security
|
|
belongs_to :category, optional: true
|
|
|
|
validates :qty, presence: true
|
|
validates :price, :currency, presence: true
|
|
|
|
# Trade types for categorization
|
|
def buy?
|
|
qty.positive?
|
|
end
|
|
|
|
def sell?
|
|
qty.negative?
|
|
end
|
|
|
|
class << self
|
|
def build_name(type, qty, ticker)
|
|
prefix = type == "buy" ? "Buy" : "Sell"
|
|
"#{prefix} #{qty.to_d.abs} shares of #{ticker}"
|
|
end
|
|
end
|
|
|
|
def unrealized_gain_loss
|
|
return nil if qty.negative?
|
|
current_price = security.current_price
|
|
return nil if current_price.nil?
|
|
|
|
current_value = current_price * qty.abs
|
|
cost_basis = price_money * qty.abs
|
|
|
|
Trend.new(current: current_value, previous: cost_basis)
|
|
end
|
|
end
|