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 @@