diff --git a/app/controllers/transactions_controller.rb b/app/controllers/transactions_controller.rb index eae4f9dc..5e3eb25c 100644 --- a/app/controllers/transactions_controller.rb +++ b/app/controllers/transactions_controller.rb @@ -17,7 +17,8 @@ class TransactionsController < ApplicationController .reverse_chronological .includes( { entry: :account }, - :category, :merchant, :tags + :category, :merchant, :tags, + :transfer_as_inflow, :transfer_as_outflow ) @pagy, @transactions = pagy(base_scope, limit: per_page, params: ->(p) { p.except(:focused_record_id) }) diff --git a/app/controllers/transfer_matches_controller.rb b/app/controllers/transfer_matches_controller.rb index 415d9377..f8cd5b49 100644 --- a/app/controllers/transfer_matches_controller.rb +++ b/app/controllers/transfer_matches_controller.rb @@ -8,7 +8,12 @@ class TransferMatchesController < ApplicationController def create @transfer = build_transfer - @transfer.save! + Transfer.transaction do + @transfer.save! + @transfer.outflow_transaction.update!(kind: Transfer.kind_for_account(@transfer.outflow_transaction.entry.account)) + @transfer.inflow_transaction.update!(kind: "funds_movement") + end + @transfer.sync_account_later redirect_back_or_to transactions_path, notice: "Transfer created" diff --git a/app/models/family/auto_transfer_matchable.rb b/app/models/family/auto_transfer_matchable.rb index 388ba5d6..754ca225 100644 --- a/app/models/family/auto_transfer_matchable.rb +++ b/app/models/family/auto_transfer_matchable.rb @@ -53,6 +53,9 @@ module Family::AutoTransferMatchable outflow_transaction_id: match.outflow_transaction_id, ) + Transaction.find(match.inflow_transaction_id).update!(kind: "funds_movement") + Transaction.find(match.outflow_transaction_id).update!(kind: Transfer.kind_for_account(Transaction.find(match.outflow_transaction_id).entry.account)) + used_transaction_ids << match.inflow_transaction_id used_transaction_ids << match.outflow_transaction_id end diff --git a/app/models/transfer.rb b/app/models/transfer.rb index 0721defc..f1186766 100644 --- a/app/models/transfer.rb +++ b/app/models/transfer.rb @@ -12,6 +12,18 @@ class Transfer < ApplicationRecord validate :transfer_within_date_range validate :transfer_has_same_family + class << self + def kind_for_account(account) + if account.loan? + "loan_payment" + elsif account.liability? + "cc_payment" + else + "funds_movement" + end + end + end + def reject! Transfer.transaction do RejectedTransfer.find_or_create_by!(inflow_transaction_id: inflow_transaction_id, outflow_transaction_id: outflow_transaction_id) @@ -19,6 +31,15 @@ class Transfer < ApplicationRecord end end + # Once transfer is destroyed, we need to mark the denormalized kind fields on the transactions + def destroy! + Transfer.transaction do + inflow_transaction.update!(kind: "standard") + outflow_transaction.update!(kind: "standard") + super + end + end + def confirm! update!(status: "confirmed") end diff --git a/app/views/transactions/_transaction.html.erb b/app/views/transactions/_transaction.html.erb index ecd6f2b3..b36e854b 100644 --- a/app/views/transactions/_transaction.html.erb +++ b/app/views/transactions/_transaction.html.erb @@ -10,7 +10,7 @@
<%= check_box_tag dom_id(entry, "selection"), - disabled: transaction.transfer?, + disabled: transaction.transfer.present?, class: "checkbox checkbox--light", data: { id: entry.id, @@ -39,7 +39,7 @@ <% if transaction.transfer? %> <%= link_to( entry.name, - entry_path(entry), + transaction.transfer.present? ? transfer_path(transaction.transfer) : entry_path(entry), data: { turbo_frame: "drawer", turbo_prefetch: false @@ -64,7 +64,7 @@ <% end %> - <% if transaction.transfer? %> + <% if transaction.transfer.present? %> <%= render "transactions/transfer_match", transaction: transaction %> <% end %>
@@ -91,7 +91,7 @@ <%= render "transactions/transaction_category", transaction: transaction %> -
+
<%= content_tag :p, transaction.transfer? && view_ctx == "global" ? "+/- #{format_money(entry.amount_money.abs)}" : format_money(-entry.amount_money), class: ["text-green-600": entry.amount.negative?] %> @@ -101,7 +101,7 @@ <% if balance_trend&.trend %> <%= tag.p format_money(balance_trend.trend.current), class: "font-medium text-sm text-primary" %> - <% else %> + <% elsif view_ctx != "global" %> <%= tag.p "--", class: "font-medium text-sm text-gray-400" %> <% end %>
diff --git a/test/controllers/api/v1/transactions_controller_test.rb b/test/controllers/api/v1/transactions_controller_test.rb index 92e4f953..7978a5f6 100644 --- a/test/controllers/api/v1/transactions_controller_test.rb +++ b/test/controllers/api/v1/transactions_controller_test.rb @@ -313,13 +313,13 @@ end accountable: Depository.new ) - transfer = Transfer.from_accounts( - from_account: from_account, - to_account: to_account, + transfer = Transfer::Creator.new( + family: @family, + source_account_id: from_account.id, + destination_account_id: to_account.id, date: Date.current, amount: 100 - ) - transfer.save! + ).create get api_v1_transaction_url(transfer.inflow_transaction), headers: api_headers(@api_key) assert_response :success