mirror of
https://github.com/maybe-finance/maybe.git
synced 2025-08-07 14:35:23 +02:00
Remove trend calculator
This commit is contained in:
parent
df367b420a
commit
3baaf84538
5 changed files with 11 additions and 58 deletions
|
@ -6,11 +6,17 @@ class Account::ActivityFeedData
|
||||||
|
|
||||||
def initialize(account, entries)
|
def initialize(account, entries)
|
||||||
@account = account
|
@account = account
|
||||||
@entries = entries
|
@entries = entries.to_a
|
||||||
end
|
end
|
||||||
|
|
||||||
# We read balances so we can show "start of day" -> "end of day" balances for each entry date group in the feed
|
# We read balances so we can show "start of day" -> "end of day" balances for each entry date group in the feed
|
||||||
def balances
|
def balances
|
||||||
|
@balances ||= begin
|
||||||
|
min_date = entries.min_by(&:date).date.prev_day
|
||||||
|
max_date = entries.max_by(&:date).date
|
||||||
|
|
||||||
|
account.balances.where(date: min_date..max_date, currency: account.currency).order(:date)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -1,30 +0,0 @@
|
||||||
# The current system calculates a single, end-of-day balance every day for each account for simplicity.
|
|
||||||
# In most cases, this is sufficient. However, for the "Activity View", we need to show intraday balances
|
|
||||||
# to show users how each entry affects their balances. This class calculates intraday balances by
|
|
||||||
# interpolating between end-of-day balances.
|
|
||||||
class Balance::TrendCalculator
|
|
||||||
BalanceTrend = Struct.new(:trend, :cash, keyword_init: true)
|
|
||||||
|
|
||||||
def initialize(balances)
|
|
||||||
@balances = balances
|
|
||||||
end
|
|
||||||
|
|
||||||
def trend_for(date)
|
|
||||||
balance = @balances.find { |b| b.date == date }
|
|
||||||
prior_balance = @balances.find { |b| b.date == date - 1.day }
|
|
||||||
|
|
||||||
return BalanceTrend.new(trend: nil) unless balance.present?
|
|
||||||
|
|
||||||
BalanceTrend.new(
|
|
||||||
trend: Trend.new(
|
|
||||||
current: Money.new(balance.balance, balance.currency),
|
|
||||||
previous: prior_balance.present? ? Money.new(prior_balance.balance, balance.currency) : nil,
|
|
||||||
favorable_direction: balance.account.favorable_direction
|
|
||||||
),
|
|
||||||
cash: Money.new(balance.cash_balance, balance.currency),
|
|
||||||
)
|
|
||||||
end
|
|
||||||
|
|
||||||
private
|
|
||||||
attr_reader :balances
|
|
||||||
end
|
|
|
@ -76,11 +76,9 @@
|
||||||
|
|
||||||
<div>
|
<div>
|
||||||
<div class="space-y-4">
|
<div class="space-y-4">
|
||||||
<% calculator = Balance::TrendCalculator.new(@account.balances) %>
|
|
||||||
|
|
||||||
<%= entries_by_date(@entries) do |entries| %>
|
<%= entries_by_date(@entries) do |entries| %>
|
||||||
<% entries.each_with_index do |entry, index| %>
|
<% entries.each_with_index do |entry, index| %>
|
||||||
<%= render entry, balance_trend: index == 0 ? calculator.trend_for(entry.date) : nil, view_ctx: "account" %>
|
<%= render entry, view_ctx: "account" %>
|
||||||
<% end %>
|
<% end %>
|
||||||
<% end %>
|
<% end %>
|
||||||
</div>
|
</div>
|
||||||
|
|
|
@ -38,16 +38,6 @@
|
||||||
format_money(-entry.amount_money),
|
format_money(-entry.amount_money),
|
||||||
class: ["text-green-600": entry.amount.negative?] %>
|
class: ["text-green-600": entry.amount.negative?] %>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="col-span-2 justify-self-end">
|
|
||||||
<% if balance_trend&.trend %>
|
|
||||||
<div class="flex items-center gap-2">
|
|
||||||
<%= tag.p format_money(balance_trend.trend.current), class: "font-medium text-sm text-primary" %>
|
|
||||||
</div>
|
|
||||||
<% else %>
|
|
||||||
<%= tag.p "--", class: "font-medium text-sm text-gray-400" %>
|
|
||||||
<% end %>
|
|
||||||
</div>
|
|
||||||
</div>
|
</div>
|
||||||
<% end %>
|
<% end %>
|
||||||
<% end %>
|
<% end %>
|
||||||
|
|
|
@ -6,7 +6,7 @@
|
||||||
<%= turbo_frame_tag dom_id(transaction) do %>
|
<%= turbo_frame_tag dom_id(transaction) do %>
|
||||||
<div class="grid grid-cols-12 items-center text-primary text-sm font-medium p-4 lg:p-4 <%= entry.excluded ? "opacity-50 text-gray-400" : "" %>">
|
<div class="grid grid-cols-12 items-center text-primary text-sm font-medium p-4 lg:p-4 <%= entry.excluded ? "opacity-50 text-gray-400" : "" %>">
|
||||||
|
|
||||||
<div class="pr-4 lg:pr-10 flex items-center gap-3 lg:gap-4 col-span-8 <%= view_ctx == "global" ? "lg:col-span-8" : "lg:col-span-6" %>">
|
<div class="pr-4 lg:pr-10 flex items-center gap-3 lg:gap-4 col-span-8">
|
||||||
<%= check_box_tag dom_id(entry, "selection"),
|
<%= check_box_tag dom_id(entry, "selection"),
|
||||||
disabled: transaction.transfer.present?,
|
disabled: transaction.transfer.present?,
|
||||||
class: "checkbox checkbox--light",
|
class: "checkbox checkbox--light",
|
||||||
|
@ -93,22 +93,11 @@
|
||||||
<%= render "transactions/transaction_category", transaction: transaction %>
|
<%= render "transactions/transaction_category", transaction: transaction %>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="col-span-4 lg:col-span-2 ml-auto text-right">
|
<div class="col-span-2 ml-auto text-right">
|
||||||
<%= content_tag :p,
|
<%= content_tag :p,
|
||||||
transaction.transfer? && view_ctx == "global" ? "+/- #{format_money(entry.amount_money.abs)}" : format_money(-entry.amount_money),
|
transaction.transfer? && view_ctx == "global" ? "+/- #{format_money(entry.amount_money.abs)}" : format_money(-entry.amount_money),
|
||||||
class: ["text-green-600": entry.amount.negative?] %>
|
class: ["text-green-600": entry.amount.negative?] %>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<% if view_ctx != "global" %>
|
|
||||||
<div class="col-span-2 justify-self-end hidden lg:block">
|
|
||||||
<% if balance_trend&.trend %>
|
|
||||||
<%= tag.p format_money(balance_trend.trend.current),
|
|
||||||
class: "font-medium text-sm text-primary" %>
|
|
||||||
<% else %>
|
|
||||||
<%= tag.p "--", class: "font-medium text-sm text-gray-400" %>
|
|
||||||
<% end %>
|
|
||||||
</div>
|
|
||||||
<% end %>
|
|
||||||
</div>
|
</div>
|
||||||
<% end %>
|
<% end %>
|
||||||
<% end %>
|
<% end %>
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue