2024-06-20 13:32:44 -04:00
|
|
|
class Account::TransfersController < ApplicationController
|
2024-06-19 06:52:08 -04:00
|
|
|
layout "with_sidebar"
|
|
|
|
|
|
|
|
before_action :set_transfer, only: :destroy
|
|
|
|
|
|
|
|
def new
|
2024-06-20 13:32:44 -04:00
|
|
|
@transfer = Account::Transfer.new
|
2024-06-19 06:52:08 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
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],
|
|
|
|
amount: transfer_params[:amount].to_d,
|
|
|
|
currency: transfer_params[:currency],
|
|
|
|
name: transfer_params[:name]
|
|
|
|
|
|
|
|
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
|
|
|
|
flash[:error] = @transfer.errors.full_messages.to_sentence
|
|
|
|
redirect_to transactions_path
|
2024-06-19 06:52:08 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def destroy
|
|
|
|
@transfer.destroy_and_remove_marks!
|
|
|
|
redirect_back_or_to transactions_url, notice: t(".success")
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def set_transfer
|
2024-06-20 13:32:44 -04:00
|
|
|
@transfer = Account::Transfer.find(params[:id])
|
2024-06-19 06:52:08 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def transfer_params
|
2024-06-20 13:32:44 -04:00
|
|
|
params.require(:account_transfer).permit(:from_account_id, :to_account_id, :amount, :currency, :date, :name)
|
2024-06-19 06:52:08 -04:00
|
|
|
end
|
|
|
|
end
|