2024-08-09 11:22:57 -04:00
|
|
|
class Account::TradesController < ApplicationController
|
2024-11-27 16:01:50 -05:00
|
|
|
include EntryableResource
|
2024-08-09 11:22:57 -04:00
|
|
|
|
2024-11-27 16:01:50 -05:00
|
|
|
permitted_entryable_attributes :id, :qty, :price
|
2024-08-09 11:22:57 -04:00
|
|
|
|
2024-11-27 16:01:50 -05:00
|
|
|
private
|
|
|
|
def build_entry
|
|
|
|
Account::TradeBuilder.new(create_entry_params)
|
2024-08-09 11:22:57 -04:00
|
|
|
end
|
2024-10-09 14:59:18 -04:00
|
|
|
|
2024-11-27 16:01:50 -05:00
|
|
|
def create_entry_params
|
|
|
|
params.require(:account_entry).permit(
|
|
|
|
:account_id, :date, :amount, :currency, :qty, :price, :ticker, :type, :transfer_account_id
|
|
|
|
).tap do |params|
|
|
|
|
account_id = params.delete(:account_id)
|
|
|
|
params[:account] = Current.family.accounts.find(account_id)
|
|
|
|
end
|
2024-10-09 14:59:18 -04:00
|
|
|
end
|
|
|
|
|
2024-11-27 16:01:50 -05:00
|
|
|
def update_entry_params
|
|
|
|
return entry_params unless entry_params[:entryable_attributes].present?
|
2024-10-30 09:23:44 -04:00
|
|
|
|
2024-11-27 16:01:50 -05:00
|
|
|
update_params = entry_params
|
|
|
|
update_params = update_params.merge(entryable_type: "Account::Trade")
|
2024-10-28 15:49:19 -04:00
|
|
|
|
2024-11-27 16:01:50 -05:00
|
|
|
qty = update_params[:entryable_attributes][:qty]
|
|
|
|
price = update_params[:entryable_attributes][:price]
|
2024-08-09 11:22:57 -04:00
|
|
|
|
2024-11-27 16:01:50 -05:00
|
|
|
if qty.present? && price.present?
|
|
|
|
qty = update_params[:nature] == "inflow" ? -qty.to_d : qty.to_d
|
|
|
|
update_params[:entryable_attributes][:qty] = qty
|
|
|
|
update_params[:amount] = qty * price.to_d
|
|
|
|
end
|
2024-10-09 14:59:18 -04:00
|
|
|
|
2024-11-27 16:01:50 -05:00
|
|
|
update_params.except(:nature)
|
2024-08-09 11:22:57 -04:00
|
|
|
end
|
|
|
|
end
|