mirror of
https://github.com/maybe-finance/maybe.git
synced 2025-07-21 14:19:39 +02:00
fix: Transfers should always total to zero (#1859)
This commit is contained in:
parent
8e339dcbe0
commit
f35b70e936
3 changed files with 26 additions and 4 deletions
|
@ -154,7 +154,7 @@ module ApplicationHelper
|
||||||
|
|
||||||
def totals_by_currency(collection:, money_method:, separator: " | ", negate: false)
|
def totals_by_currency(collection:, money_method:, separator: " | ", negate: false)
|
||||||
collection.group_by(&:currency)
|
collection.group_by(&:currency)
|
||||||
.transform_values { |item| negate ? item.sum(&money_method) * -1 : item.sum(&money_method) }
|
.transform_values { |item| calculate_total(item, money_method, negate) }
|
||||||
.map { |_currency, money| format_money(money) }
|
.map { |_currency, money| format_money(money) }
|
||||||
.join(separator)
|
.join(separator)
|
||||||
end
|
end
|
||||||
|
@ -166,4 +166,12 @@ module ApplicationHelper
|
||||||
|
|
||||||
cookies[:admin] == "true"
|
cookies[:admin] == "true"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def calculate_total(item, money_method, negate)
|
||||||
|
items = item.reject { |i| i.respond_to?(:entryable) && i.entryable.transfer? }
|
||||||
|
total = items.sum(&money_method)
|
||||||
|
negate ? -total : total
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -17,7 +17,9 @@
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<% if totals %>
|
<% if totals %>
|
||||||
<%= totals_by_currency(collection: entries, money_method: :amount_money, negate: true) %>
|
<div id="entry-group-<%= date %>-totals">
|
||||||
|
<%= totals_by_currency(collection: entries, money_method: :amount_money, negate: true) %>
|
||||||
|
</div>
|
||||||
<% end %>
|
<% end %>
|
||||||
</div>
|
</div>
|
||||||
<div class="bg-white shadow-xs rounded-md border border-alpha-black-25 divide-y divide-alpha-black-50">
|
<div class="bg-white shadow-xs rounded-md border border-alpha-black-25 divide-y divide-alpha-black-50">
|
||||||
|
|
|
@ -196,10 +196,22 @@ class TransactionsTest < ApplicationSystemTestCase
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
test "transfers should always sum to zero" do
|
||||||
|
asset_account = accounts(:other_asset)
|
||||||
|
investment_account = accounts(:investment)
|
||||||
|
outflow_entry = create_transaction("outflow", Date.current, 500, account: asset_account)
|
||||||
|
inflow_entry = create_transaction("inflow", 1.day.ago.to_date, -500, account: investment_account)
|
||||||
|
asset_account.auto_match_transfers!
|
||||||
|
visit transactions_url
|
||||||
|
within "#entry-group-" + Date.current.to_s + "-totals" do
|
||||||
|
assert_text "-$100.00" # transaction eleven from setup
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def create_transaction(name, date, amount, category: nil, merchant: nil, tags: [])
|
def create_transaction(name, date, amount, category: nil, merchant: nil, tags: [], account: nil)
|
||||||
account = accounts(:depository)
|
account ||= accounts(:depository)
|
||||||
|
|
||||||
account.entries.create! \
|
account.entries.create! \
|
||||||
name: name,
|
name: name,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue