Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby

Exclude pending transactions from balances (#2897)

Atlas00d35993eb4f
diff --git a/app/models/balance/base_calculator.rb b/app/models/balance/base_calculator.rb
index ec78a3145a..404d7b43f1 100644
--- a/app/models/balance/base_calculator.rb
+++ b/app/models/balance/base_calculator.rb
@@ -32,7 +32,7 @@ class Balance::BaseCalculator
# with large entry histories. Calculator instances are per-sync, so there is
# no staleness concern.
def calculation_start_date
- @calculation_start_date ||= [ account.opening_anchor_date, account.entries.minimum(:date) ].compact.min
+ @calculation_start_date ||= [ account.opening_anchor_date, account.entries.excluding_pending.minimum(:date) ].compact.min
end
private
diff --git a/app/models/balance/forward_calculator.rb b/app/models/balance/forward_calculator.rb
index 521473693c..22094a3c75 100644
--- a/app/models/balance/forward_calculator.rb
+++ b/app/models/balance/forward_calculator.rb
@@ -103,7 +103,7 @@ class Balance::ForwardCalculator < Balance::BaseCalculator
# that may have been missing (fallback_rate: 1) on a prior sync and later
# imported — so we must do a full recalculation to pick them up.
def multi_currency_account?
- account.entries.where.not(currency: account.currency).exists? ||
+ account.entries.excluding_pending.where.not(currency: account.currency).exists? ||
account.currency != account.family.currency
end
@@ -135,7 +135,7 @@ class Balance::ForwardCalculator < Balance::BaseCalculator
end
def calc_end_date
- [ account.entries.order(:date).last&.date, account.holdings.order(:date).last&.date ].compact.max || Date.current
+ [ account.entries.excluding_pending.maximum(:date), account.holdings.maximum(:date) ].compact.max || Date.current
end
# Negative entries amount on an "asset" account means, "account value has increased"
diff --git a/app/models/balance/linked_investment_series_normalizer.rb b/app/models/balance/linked_investment_series_normalizer.rb
index ee5fa3ab17..f0fc414228 100644
--- a/app/models/balance/linked_investment_series_normalizer.rb
+++ b/app/models/balance/linked_investment_series_normalizer.rb
@@ -43,6 +43,7 @@ class Balance::LinkedInvestmentSeriesNormalizer
return if account_ids.empty?
activity_dates = Entry.where(account_id: account_ids)
+ .excluding_pending
.where.not(source: nil)
.where.not(entryable_type: "Valuation")
.group(:account_id)
@@ -107,6 +108,7 @@ class Balance::LinkedInvestmentSeriesNormalizer
def first_provider_activity_date
@first_provider_activity_date ||= account.entries
+ .excluding_pending
.where.not(source: nil)
.where.not(entryable_type: "Valuation")
.minimum(:date)
diff --git a/app/models/balance/sync_cache.rb b/app/models/balance/sync_cache.rb
index 02d82f87dc..6209753c24 100644
--- a/app/models/balance/sync_cache.rb
+++ b/app/models/balance/sync_cache.rb
@@ -34,7 +34,7 @@ class Balance::SyncCache
end
def converted_entries
- @converted_entries ||= account.entries.excluding_split_parents.includes(:entryable).order(:date).to_a.map do |e|
+ @converted_entries ||= account.entries.excluding_pending.excluding_split_parents.includes(:entryable).order(:date).to_a.map do |e|
custom_rate = e.entryable.exchange_rate if e.entryable.respond_to?(:exchange_rate)
# Use Money#exchange_to with custom rate if available, standard lookup otherwise.
diff --git a/test/models/balance/forward_calculator_test.rb b/test/models/balance/forward_calculator_test.rb
index bdd24dc81b..db0f7613a4 100644
--- a/test/models/balance/forward_calculator_test.rb
+++ b/test/models/balance/forward_calculator_test.rb
@@ -66,6 +66,44 @@ class Balance::ForwardCalculatorTest < ActiveSupport::TestCase
)
end
+ test "pending transactions do not affect materialized balances" do
+ account = create_account_with_ledger(
+ account: { type: Depository, currency: "USD" },
+ entries: [
+ { type: "opening_anchor", date: 3.days.ago.to_date, balance: 1000 },
+ { type: "transaction", date: 2.days.ago.to_date, amount: -200 },
+ { type: "transaction", date: 1.day.ago.to_date, amount: -500 }
+ ]
+ )
+ account.entries.find_by!(date: 1.day.ago.to_date).entryable.update!(
+ extra: { "simplefin" => { "pending" => true } }
+ )
+
+ calculated = Balance::ForwardCalculator.new(account).calculate
+
+ assert_equal 1200, calculated.last.balance
+ assert_equal 200, calculated.sum(&:cash_inflows)
+ assert_not_includes calculated.map(&:date), 1.day.ago.to_date
+ end
+
+ test "calculation start date ignores a pending entry before the opening anchor" do
+ account = create_account_with_ledger(
+ account: { type: Depository, currency: "USD" },
+ entries: [
+ { type: "transaction", date: 5.days.ago.to_date, amount: -100 },
+ { type: "opening_anchor", date: 3.days.ago.to_date, balance: 1000 },
+ { type: "transaction", date: 2.days.ago.to_date, amount: -200 }
+ ]
+ )
+ account.entries.find_by!(date: 5.days.ago.to_date).entryable.update!(
+ extra: { "plaid" => { "pending" => true } }
+ )
+
+ calculator = Balance::ForwardCalculator.new(account)
+
+ assert_equal 3.days.ago.to_date, calculator.calculation_start_date
+ end
+
test "reconciliation valuation sets absolute balance before applying subsequent transactions" do
account = create_account_with_ledger(
account: { type: Depository, currency: "USD" },
@@ -808,6 +846,27 @@ class Balance::ForwardCalculatorTest < ActiveSupport::TestCase
assert_not calculator.incremental?, "Should not be incremental for foreign currency accounts"
end
+ test "pending foreign-currency entries do not force a full recalculation" do
+ account = create_account_with_ledger(
+ account: { type: Depository, currency: "USD" },
+ entries: [
+ { type: "opening_anchor", date: 3.days.ago.to_date, balance: 1000 },
+ { type: "transaction", date: 2.days.ago.to_date, amount: -100 },
+ { type: "transaction", date: 1.day.ago.to_date, amount: -500, currency: "EUR" }
+ ]
+ )
+ account.entries.find_by!(date: 1.day.ago.to_date).entryable.update!(
+ extra: { "plaid" => { "pending" => true } }
+ )
+
+ Balance::Materializer.new(account, strategy: :forward).materialize_balances
+ calculator = Balance::ForwardCalculator.new(account, window_start_date: 2.days.ago.to_date)
+ result = calculator.calculate
+
+ assert calculator.incremental?, "Pending foreign-currency entries should be ignored by the guard"
+ assert_equal [ 2.days.ago.to_date ], result.map(&:date)
+ end
+
# Regression: a reconciliation (or any entry) backfilled with a date EARLIER
# than the opening anchor must still be materialized into Balance rows. The
# window is bounded on min(opening_anchor_date, oldest_entry_date), and the
diff --git a/test/models/balance/linked_investment_series_normalizer_test.rb b/test/models/balance/linked_investment_series_normalizer_test.rb
new file mode 100644
index 0000000000..a3113beb01
--- /dev/null
+++ b/test/models/balance/linked_investment_series_normalizer_test.rb
@@ -0,0 +1,36 @@
+require "test_helper"
+
+class Balance::LinkedInvestmentSeriesNormalizerTest < ActiveSupport::TestCase
+ test "pending transactions do not establish supported balance history" do
+ account = families(:empty).accounts.create!(
+ name: "Linked Investment",
+ balance: 0,
+ currency: "USD",
+ accountable: Investment.new
+ )
+ pending_date = 5.days.ago.to_date
+ posted_date = 2.days.ago.to_date
+
+ account.entries.create!(
+ date: pending_date,
+ name: "Pending Transaction",
+ amount: 100,
+ currency: "USD",
+ source: "plaid",
+ entryable: Transaction.new(extra: { "plaid" => { "pending" => true } })
+ )
+ account.entries.create!(
+ date: posted_date,
+ name: "Posted Transaction",
+ amount: 100,
+ currency: "USD",
+ source: "plaid",
+ entryable: Transaction.new
+ )
+
+ start_date = Balance::LinkedInvestmentSeriesNormalizer
+ .send(:common_supported_history_start_date, [ account.id ])
+
+ assert_equal posted_date, start_date
+ end
+end
diff --git a/test/models/balance/sync_cache_test.rb b/test/models/balance/sync_cache_test.rb
index cbc14df7b3..d657ed9ada 100644
--- a/test/models/balance/sync_cache_test.rb
+++ b/test/models/balance/sync_cache_test.rb
@@ -85,6 +85,37 @@ class Balance::SyncCacheTest < ActiveSupport::TestCase
assert_equal 150.0, converted_entry.amount
end
+ test "excludes pending transactions while retaining posted transactions and non-transaction entries" do
+ pending = @account.entries.create!(
+ date: Date.current,
+ name: "Pending Transaction",
+ amount: 100,
+ currency: "USD",
+ entryable: Transaction.new(extra: { "plaid" => { "pending" => true } })
+ )
+ posted = @account.entries.create!(
+ date: Date.current,
+ name: "Posted Transaction",
+ amount: 200,
+ currency: "USD",
+ entryable: Transaction.new(extra: { "plaid" => { "pending" => false } })
+ )
+ security = Security.create!(ticker: "KEPT", name: "Kept Trade")
+ trade = @account.entries.create!(
+ date: Date.current,
+ name: "Trade",
+ amount: 300,
+ currency: "USD",
+ entryable: Trade.new(security: security, qty: 1, price: 300, currency: "USD")
+ )
+
+ entry_names = Balance::SyncCache.new(@account).send(:converted_entries).map(&:name)
+
+ assert_not_includes entry_names, pending.name
+ assert_includes entry_names, posted.name
+ assert_includes entry_names, trade.name
+ end
+
test "converts multiple entries with correct rates" do
# Create exchange rates
ExchangeRate.create!(