1
0
Fork 0
mirror of https://github.com/maybe-finance/maybe.git synced 2025-08-10 07:55:21 +02:00

Implement signage

This commit is contained in:
Zach Gollwitzer 2025-04-18 09:44:49 -04:00
parent 92a723131a
commit 4f90ceff01
6 changed files with 28 additions and 15 deletions

View file

@ -31,7 +31,7 @@ class Import < ApplicationRecord
validates :type, inclusion: { in: TYPES }
validates :amount_type_strategy, inclusion: { in: AMOUNT_TYPE_STRATEGIES }
validates :col_sep, inclusion: { in: SEPARATORS.map(&:last) }
validates :signage_convention, inclusion: { in: SIGNAGE_CONVENTIONS }
validates :signage_convention, inclusion: { in: SIGNAGE_CONVENTIONS }, allow_nil: true
validates :number_format, presence: true, inclusion: { in: NUMBER_FORMATS.keys }
has_many :rows, dependent: :destroy

View file

@ -44,7 +44,19 @@ class Import::Row < ApplicationRecord
# In the Maybe system, positive amounts == "outflows", so we must reverse signage
def apply_transaction_signage_convention(value)
value * (import.signage_convention == "inflows_positive" ? -1 : 1)
if import.amount_type_strategy == "signed_amount"
value * (import.signage_convention == "inflows_positive" ? -1 : 1)
elsif import.amount_type_strategy == "custom_column"
inflow_value = import.amount_type_inflow_value
if entity_type == inflow_value
value * -1
else
value
end
else
raise "Unknown amount type strategy for import: #{import.amount_type_strategy}"
end
end
def required_columns

View file

@ -31,6 +31,7 @@ class TradeImport < Import
),
)
end
Trade.import!(trades, recursive: true)
end
end

View file

@ -277,6 +277,7 @@ module ImportInterfaceTest
csv_data = "date,amount,name\n01/01/2024,1234.56,Test"
import.update!(raw_file_str: csv_data)
import.update!(
date_col_label: "date",
date_format: "%Y-%m-%d" # Does not match the raw CSV date, so rows will be invalid, but still generated
)

View file

@ -36,6 +36,7 @@ class TradeImportTest < ActiveSupport::TestCase
CSV
@import.update!(
account: accounts(:depository),
raw_file_str: import,
date_col_label: "date",
ticker_col_label: "ticker",
@ -52,16 +53,11 @@ class TradeImportTest < ActiveSupport::TestCase
@import.reload
assert_difference [
-> { Entry.count },
-> { Trade.count }
], 2 do
assert_difference [
-> { Security.count },
-> { Account.count }
], 1 do
@import.publish
end
assert_difference -> { Entry.count } => 2,
-> { Trade.count } => 2,
-> { Security.count } => 1,
-> { Account.count } => 1 do
@import.publish
end
assert_equal "complete", @import.status

View file

@ -78,20 +78,23 @@ class TransactionImportTest < ActiveSupport::TestCase
CSV
@import.update!(
account: accounts(:depository),
raw_file_str: import,
date_col_label: "date",
date_format: "%m/%d/%Y",
amount_col_label: "amount",
amount_type_col_label: "amount_type",
entity_type_col_label: "amount_type",
amount_type_inflow_value: "debit",
amount_type_strategy: "custom_column",
signage_convention: nil # Explicitly set to nil to prove this is not needed
)
@import.generate_rows_from_csv
@import.reload
assert_difference -> { Account::Entry.count } => 3,
-> { Account::Transaction.count } => 3 do
assert_difference -> { Entry.count } => 3,
-> { Transaction.count } => 3 do
@import.publish
end