2025-04-14 11:40:34 -04:00
|
|
|
class TradeBuilder
|
2024-08-09 11:22:57 -04:00
|
|
|
include ActiveModel::Model
|
|
|
|
|
2024-11-27 16:01:50 -05:00
|
|
|
attr_accessor :account, :date, :amount, :currency, :qty,
|
2025-02-28 09:34:14 -05:00
|
|
|
:price, :ticker, :manual_ticker, :type, :transfer_account_id
|
2024-08-09 20:11:27 -04:00
|
|
|
|
2025-01-07 09:41:24 -05:00
|
|
|
attr_reader :buildable
|
|
|
|
|
|
|
|
def initialize(attributes = {})
|
|
|
|
super
|
|
|
|
@buildable = set_buildable
|
|
|
|
end
|
|
|
|
|
2024-11-27 16:01:50 -05:00
|
|
|
def save
|
|
|
|
buildable.save
|
|
|
|
end
|
2024-08-09 11:22:57 -04:00
|
|
|
|
2025-04-18 11:39:58 -04:00
|
|
|
def lock_saved_attributes!
|
|
|
|
if buildable.is_a?(Transfer)
|
|
|
|
buildable.inflow_transaction.entry.lock_saved_attributes!
|
|
|
|
buildable.outflow_transaction.entry.lock_saved_attributes!
|
|
|
|
else
|
|
|
|
buildable.lock_saved_attributes!
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def entryable
|
|
|
|
return nil if buildable.is_a?(Transfer)
|
|
|
|
|
|
|
|
buildable.entryable
|
|
|
|
end
|
|
|
|
|
2024-11-27 16:01:50 -05:00
|
|
|
def errors
|
|
|
|
buildable.errors
|
|
|
|
end
|
2024-08-09 11:22:57 -04:00
|
|
|
|
2024-11-27 16:01:50 -05:00
|
|
|
def sync_account_later
|
|
|
|
buildable.sync_account_later
|
2024-08-09 11:22:57 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
2025-01-07 09:41:24 -05:00
|
|
|
def set_buildable
|
2024-11-27 16:01:50 -05:00
|
|
|
case type
|
|
|
|
when "buy", "sell"
|
|
|
|
build_trade
|
|
|
|
when "deposit", "withdrawal"
|
|
|
|
build_transfer
|
|
|
|
when "interest"
|
|
|
|
build_interest
|
|
|
|
else
|
|
|
|
raise "Unknown trade type: #{type}"
|
|
|
|
end
|
|
|
|
end
|
2024-08-09 11:22:57 -04:00
|
|
|
|
2024-11-27 16:01:50 -05:00
|
|
|
def build_trade
|
2024-12-19 10:16:09 -05:00
|
|
|
prefix = type == "sell" ? "Sell " : "Buy "
|
|
|
|
trade_name = prefix + "#{qty.to_i.abs} shares of #{security.ticker}"
|
|
|
|
|
2024-11-27 16:01:50 -05:00
|
|
|
account.entries.new(
|
2024-12-19 10:16:09 -05:00
|
|
|
name: trade_name,
|
2024-08-09 11:22:57 -04:00
|
|
|
date: date,
|
2024-11-27 16:01:50 -05:00
|
|
|
amount: signed_amount,
|
|
|
|
currency: currency,
|
2025-04-14 11:40:34 -04:00
|
|
|
entryable: Trade.new(
|
2024-08-09 11:22:57 -04:00
|
|
|
qty: signed_qty,
|
2024-11-27 16:01:50 -05:00
|
|
|
price: price,
|
|
|
|
currency: currency,
|
|
|
|
security: security
|
|
|
|
)
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def build_transfer
|
|
|
|
transfer_account = family.accounts.find(transfer_account_id) if transfer_account_id.present?
|
|
|
|
|
|
|
|
if transfer_account
|
|
|
|
from_account = type == "withdrawal" ? account : transfer_account
|
|
|
|
to_account = type == "withdrawal" ? transfer_account : account
|
|
|
|
|
2025-01-07 09:41:24 -05:00
|
|
|
Transfer.from_accounts(
|
|
|
|
from_account: from_account,
|
|
|
|
to_account: to_account,
|
2024-11-27 16:01:50 -05:00
|
|
|
date: date,
|
|
|
|
amount: signed_amount
|
2024-08-09 11:22:57 -04:00
|
|
|
)
|
2024-11-27 16:01:50 -05:00
|
|
|
else
|
|
|
|
account.entries.build(
|
2024-12-10 17:41:20 -05:00
|
|
|
name: signed_amount < 0 ? "Deposit to #{account.name}" : "Withdrawal from #{account.name}",
|
2024-11-27 16:01:50 -05:00
|
|
|
date: date,
|
|
|
|
amount: signed_amount,
|
|
|
|
currency: currency,
|
2025-04-14 11:40:34 -04:00
|
|
|
entryable: Transaction.new
|
2024-11-27 16:01:50 -05:00
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def build_interest
|
|
|
|
account.entries.build(
|
|
|
|
name: "Interest payment",
|
|
|
|
date: date,
|
|
|
|
amount: signed_amount,
|
|
|
|
currency: currency,
|
2025-04-14 11:40:34 -04:00
|
|
|
entryable: Transaction.new
|
2024-11-27 16:01:50 -05:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def signed_qty
|
|
|
|
return nil unless type.in?([ "buy", "sell" ])
|
|
|
|
|
|
|
|
type == "sell" ? -qty.to_d : qty.to_d
|
|
|
|
end
|
|
|
|
|
|
|
|
def signed_amount
|
|
|
|
case type
|
|
|
|
when "buy", "sell"
|
|
|
|
signed_qty * price.to_d
|
|
|
|
when "deposit", "withdrawal"
|
|
|
|
type == "deposit" ? -amount.to_d : amount.to_d
|
|
|
|
when "interest"
|
|
|
|
amount.to_d * -1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def family
|
|
|
|
account.family
|
2024-08-09 11:22:57 -04:00
|
|
|
end
|
|
|
|
|
2025-02-28 09:34:14 -05:00
|
|
|
# Users can either look up a ticker from our provider (Synth) or enter a manual, "offline" ticker (that we won't fetch prices for)
|
2024-08-09 11:22:57 -04:00
|
|
|
def security
|
2025-02-28 09:34:14 -05:00
|
|
|
ticker_symbol, exchange_operating_mic = ticker.present? ? ticker.split("|") : [ manual_ticker, nil ]
|
2024-10-30 09:23:44 -04:00
|
|
|
|
2025-05-22 12:43:24 -04:00
|
|
|
Security::Resolver.new(
|
|
|
|
ticker_symbol,
|
2025-05-13 16:17:25 -04:00
|
|
|
exchange_operating_mic: exchange_operating_mic
|
2025-05-22 12:43:24 -04:00
|
|
|
).resolve
|
2024-08-09 11:22:57 -04:00
|
|
|
end
|
|
|
|
end
|