Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add exchange and currency fields to trade imports #1822

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/controllers/import/configurations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ def import_params
:account_col_label,
:qty_col_label,
:ticker_col_label,
:exchange_col_label,
:price_col_label,
:entity_type_col_label,
:notes_col_label,
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/import/rows_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def show

private
def row_params
params.require(:import_row).permit(:type, :account, :date, :qty, :ticker, :price, :amount, :currency, :name, :category, :tags, :entity_type, :notes)
params.require(:import_row).permit(:type, :account, :date, :qty, :ticker, :exchange, :price, :amount, :currency, :name, :category, :tags, :entity_type, :notes)
end

def set_import_row
Expand Down
1 change: 1 addition & 0 deletions app/helpers/imports_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ def import_col_label(key)
notes: "Notes",
qty: "Quantity",
ticker: "Ticker",
exchange: "Exchange",
price: "Price",
entity_type: "Type"
}[key]
Expand Down
1 change: 1 addition & 0 deletions app/models/import.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ def generate_rows_from_csv
date: row[date_col_label].to_s,
qty: sanitize_number(row[qty_col_label]).to_s,
ticker: row[ticker_col_label].to_s,
exchange: row[exchange_col_label]&.strip.to_s,
price: sanitize_number(row[price_col_label]).to_s,
amount: sanitize_number(row[amount_col_label]).to_s,
currency: (row[currency_col_label] || default_currency).to_s,
Expand Down
1 change: 1 addition & 0 deletions app/models/import/row.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ class Import::Row < ApplicationRecord

validates :amount, numericality: true, allow_blank: true
validates :currency, presence: true
validates :exchange, presence: true, if: -> { import.type == "TradeImport" && import.exchange_col_label.present? }

validate :date_valid
validate :required_columns
Expand Down
26 changes: 18 additions & 8 deletions app/models/trade_import.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,24 @@ def import!

rows.each do |row|
account = mappings.accounts.mappable_for(row.account)
security = Security.find_or_create_by(ticker: row.ticker)
security = Security.find_or_create_by!(
ticker: row.ticker,
exchange_mic: row.exchange.presence || "UNKNOWN",
exchange_acronym: row.exchange.presence || "Unknown",
currency: row.currency.presence || account.currency
)

entry = account.entries.build \
date: row.date_iso,
amount: row.signed_amount,
name: row.name,
currency: row.currency,
entryable: Account::Trade.new(security: security, qty: row.qty, currency: row.currency, price: row.price),
currency: row.currency.presence || account.currency,
entryable: Account::Trade.new(
security: security,
qty: row.qty,
currency: row.currency.presence || account.currency,
price: row.price
),
import: self

entry.save!
Expand All @@ -29,7 +39,7 @@ def required_column_keys
end

def column_keys
%i[date ticker qty price currency account name]
%i[date ticker exchange currency qty price account name]
end

def dry_run
Expand All @@ -41,10 +51,10 @@ def dry_run

def csv_template
template = <<-CSV
date*,ticker*,qty*,price*,currency,account,name
05/15/2024,AAPL,10,150.00,USD,Trading Account,Apple Inc. Purchase
05/16/2024,GOOGL,-5,2500.00,USD,Investment Account,Alphabet Inc. Sale
05/17/2024,TSLA,2,700.50,USD,Retirement Account,Tesla Inc. Purchase
date*,ticker*,exchange,currency,qty*,price*,account,name
05/15/2024,AAPL,XNAS,USD,10,150.00,Trading Account,Apple Inc. Purchase
05/16/2024,GOOGL,XNAS,USD,-5,2500.00,Investment Account,Alphabet Inc. Sale
05/17/2024,TSLA,XNAS,USD,2,700.50,Retirement Account,Tesla Inc. Purchase
CSV

CSV.parse(template, headers: true)
Expand Down
2 changes: 2 additions & 0 deletions app/views/import/configurations/_trade_import.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
</div>

<%= form.select :ticker_col_label, import.csv_headers, { include_blank: "Leave empty", label: "Ticker" } %>
<%= form.select :exchange_col_label, import.csv_headers, { include_blank: "Leave empty", label: "Exchange" } %>
<%= form.select :currency_col_label, import.csv_headers, { include_blank: "Leave empty", label: "Currency" } %>
<%= form.select :price_col_label, import.csv_headers, { include_blank: "Leave empty", label: "Price" } %>
<%= form.select :account_col_label, import.csv_headers, { include_blank: "Leave empty", label: "Account (optional)" } %>
<%= form.select :name_col_label, import.csv_headers, { include_blank: "Leave empty", label: "Name (optional)" } %>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddExchangeAndCurrencyColumnsToImports < ActiveRecord::Migration[7.2]
def change
add_column :imports, :exchange_col_label, :string
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class AddExchangeAndCurrencyToSecurities < ActiveRecord::Migration[7.2]
def change
add_column :securities, :currency, :string
add_index :securities, :currency
end
end
5 changes: 5 additions & 0 deletions db/migrate/20250207062248_add_exchange_to_import_rows.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddExchangeToImportRows < ActiveRecord::Migration[7.2]
def change
add_column :import_rows, :exchange, :string
end
end
10 changes: 8 additions & 2 deletions db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.