mirror of
https://github.com/maybe-finance/maybe.git
synced 2025-07-25 08:09:38 +02:00
Allow user to add buy and sell trade transactions for investment accounts (#1066)
* Consolidate modal form structure into partial + helper * Scaffold out trade transaction form * Normalize translations * Add buy and sell trade form with tests * Move entryable lists to dedicated controllers * Delegate entry group contents rendering * More cleanup * Extract transaction and valuation update logic from entries controller * Delegate edit and show actions to entryables * Trade builder * Update paths for transaction updates
This commit is contained in:
parent
6bca35fa22
commit
e05f03b314
75 changed files with 801 additions and 624 deletions
46
app/models/account/trade_builder.rb
Normal file
46
app/models/account/trade_builder.rb
Normal file
|
@ -0,0 +1,46 @@
|
|||
class Account::TradeBuilder
|
||||
TYPES = %w[ buy sell ].freeze
|
||||
|
||||
include ActiveModel::Model
|
||||
|
||||
attr_accessor :type, :qty, :price, :ticker, :date, :account
|
||||
|
||||
validates :type, :qty, :price, :ticker, :date, presence: true
|
||||
validates :price, numericality: { greater_than: 0 }
|
||||
validates :type, inclusion: { in: TYPES }
|
||||
|
||||
def save
|
||||
if valid?
|
||||
create_entry
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def create_entry
|
||||
account.entries.account_trades.create! \
|
||||
date: date,
|
||||
amount: amount,
|
||||
currency: account.currency,
|
||||
entryable: Account::Trade.new(
|
||||
security: security,
|
||||
qty: signed_qty,
|
||||
price: price.to_d,
|
||||
currency: account.currency
|
||||
)
|
||||
end
|
||||
|
||||
def security
|
||||
Security.find_or_create_by(ticker: ticker)
|
||||
end
|
||||
|
||||
def amount
|
||||
price.to_d * signed_qty
|
||||
end
|
||||
|
||||
def signed_qty
|
||||
_qty = qty.to_d
|
||||
_qty = _qty * -1 if type == "sell"
|
||||
_qty
|
||||
end
|
||||
end
|
Loading…
Add table
Add a link
Reference in a new issue