2024-06-20 13:32:44 -04:00
|
|
|
class Account::TransfersController < ApplicationController
|
2024-07-26 18:00:41 +02:00
|
|
|
layout :with_sidebar
|
2024-06-19 06:52:08 -04:00
|
|
|
|
2024-11-04 20:27:31 -05:00
|
|
|
before_action :set_transfer, only: %i[destroy show update]
|
2024-06-19 06:52:08 -04:00
|
|
|
|
|
|
|
def new
|
2024-06-20 13:32:44 -04:00
|
|
|
@transfer = Account::Transfer.new
|
2024-06-19 06:52:08 -04:00
|
|
|
end
|
|
|
|
|
2024-11-04 20:27:31 -05:00
|
|
|
def show
|
|
|
|
end
|
|
|
|
|
2024-06-19 06:52:08 -04:00
|
|
|
def create
|
|
|
|
from_account = Current.family.accounts.find(transfer_params[:from_account_id])
|
|
|
|
to_account = Current.family.accounts.find(transfer_params[:to_account_id])
|
|
|
|
|
2024-06-20 13:32:44 -04:00
|
|
|
@transfer = Account::Transfer.build_from_accounts from_account, to_account, \
|
2024-06-19 06:52:08 -04:00
|
|
|
date: transfer_params[:date],
|
2024-11-18 15:50:47 -05:00
|
|
|
amount: transfer_params[:amount].to_d
|
2024-06-19 06:52:08 -04:00
|
|
|
|
|
|
|
if @transfer.save
|
2024-07-04 12:57:26 +02:00
|
|
|
@transfer.entries.each(&:sync_account_later)
|
2024-06-19 06:52:08 -04:00
|
|
|
redirect_to transactions_path, notice: t(".success")
|
|
|
|
else
|
2024-06-20 13:32:44 -04:00
|
|
|
# TODO: this is not an ideal way to handle errors and should eventually be improved.
|
|
|
|
# See: https://github.com/hotwired/turbo-rails/pull/367
|
2024-07-18 14:39:38 -04:00
|
|
|
flash[:alert] = @transfer.errors.full_messages.to_sentence
|
2024-06-20 13:32:44 -04:00
|
|
|
redirect_to transactions_path
|
2024-06-19 06:52:08 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-11-04 20:27:31 -05:00
|
|
|
def update
|
|
|
|
@transfer.update_entries!(transfer_update_params)
|
|
|
|
redirect_back_or_to transactions_url, notice: t(".success")
|
|
|
|
end
|
|
|
|
|
2024-06-19 06:52:08 -04:00
|
|
|
def destroy
|
2024-11-04 20:27:31 -05:00
|
|
|
@transfer.destroy!
|
2024-06-19 06:52:08 -04:00
|
|
|
redirect_back_or_to transactions_url, notice: t(".success")
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def set_transfer
|
2024-11-04 20:27:31 -05:00
|
|
|
record = Account::Transfer.find(params[:id])
|
|
|
|
|
|
|
|
unless record.entries.all? { |entry| Current.family.accounts.include?(entry.account) }
|
|
|
|
raise ActiveRecord::RecordNotFound
|
|
|
|
end
|
|
|
|
|
|
|
|
@transfer = record
|
2024-06-19 06:52:08 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def transfer_params
|
2024-11-04 20:27:31 -05:00
|
|
|
params.require(:account_transfer).permit(:from_account_id, :to_account_id, :amount, :date, :name, :excluded)
|
|
|
|
end
|
|
|
|
|
|
|
|
def transfer_update_params
|
|
|
|
params.require(:account_transfer).permit(:excluded, :notes)
|
2024-06-19 06:52:08 -04:00
|
|
|
end
|
|
|
|
end
|