2025-01-07 09:41:24 -05:00
|
|
|
class TransfersController < ApplicationController
|
2025-06-20 13:31:58 -04:00
|
|
|
include StreamExtensions
|
|
|
|
|
|
|
|
before_action :set_transfer, only: %i[show destroy update]
|
2025-01-07 09:41:24 -05:00
|
|
|
|
|
|
|
def new
|
|
|
|
@transfer = Transfer.new
|
|
|
|
end
|
|
|
|
|
|
|
|
def show
|
2025-01-16 14:36:37 -05:00
|
|
|
@categories = Current.family.categories.expenses
|
2025-01-07 09:41:24 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
2025-06-20 13:31:58 -04:00
|
|
|
@transfer = Transfer::Creator.new(
|
|
|
|
family: Current.family,
|
|
|
|
source_account_id: transfer_params[:from_account_id],
|
|
|
|
destination_account_id: transfer_params[:to_account_id],
|
2025-01-07 09:41:24 -05:00
|
|
|
date: transfer_params[:date],
|
|
|
|
amount: transfer_params[:amount].to_d
|
2025-06-20 13:31:58 -04:00
|
|
|
).create
|
2025-01-07 09:41:24 -05:00
|
|
|
|
2025-06-20 13:31:58 -04:00
|
|
|
if @transfer.persisted?
|
|
|
|
success_message = "Transfer created"
|
2025-01-07 09:41:24 -05:00
|
|
|
respond_to do |format|
|
2025-06-20 13:31:58 -04:00
|
|
|
format.html { redirect_back_or_to transactions_path, notice: success_message }
|
|
|
|
format.turbo_stream { stream_redirect_back_or_to transactions_path, notice: success_message }
|
2025-01-07 09:41:24 -05:00
|
|
|
end
|
|
|
|
else
|
|
|
|
render :new, status: :unprocessable_entity
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
2025-04-04 21:45:47 +05:30
|
|
|
Transfer.transaction do
|
|
|
|
update_transfer_status
|
2025-04-14 15:05:25 +02:00
|
|
|
update_transfer_details unless transfer_update_params[:status] == "rejected"
|
2025-01-16 14:36:37 -05:00
|
|
|
end
|
|
|
|
|
2025-01-07 09:41:24 -05:00
|
|
|
respond_to do |format|
|
|
|
|
format.html { redirect_back_or_to transactions_url, notice: t(".success") }
|
|
|
|
format.turbo_stream
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def destroy
|
|
|
|
@transfer.destroy!
|
|
|
|
redirect_back_or_to transactions_url, notice: t(".success")
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
def set_transfer
|
2025-06-20 13:31:58 -04:00
|
|
|
# Finds the transfer and ensures the family owns it
|
|
|
|
@transfer = Transfer
|
|
|
|
.where(id: params[:id])
|
|
|
|
.where(inflow_transaction_id: Current.family.transactions.select(:id))
|
|
|
|
.first
|
2025-01-07 09:41:24 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def transfer_params
|
|
|
|
params.require(:transfer).permit(:from_account_id, :to_account_id, :amount, :date, :name, :excluded)
|
|
|
|
end
|
|
|
|
|
|
|
|
def transfer_update_params
|
2025-01-16 14:36:37 -05:00
|
|
|
params.require(:transfer).permit(:notes, :status, :category_id)
|
2025-01-07 09:41:24 -05:00
|
|
|
end
|
2025-04-04 21:45:47 +05:30
|
|
|
|
|
|
|
def update_transfer_status
|
|
|
|
if transfer_update_params[:status] == "rejected"
|
|
|
|
@transfer.reject!
|
|
|
|
elsif transfer_update_params[:status] == "confirmed"
|
|
|
|
@transfer.confirm!
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def update_transfer_details
|
|
|
|
@transfer.outflow_transaction.update!(category_id: transfer_update_params[:category_id])
|
|
|
|
@transfer.update!(notes: transfer_update_params[:notes])
|
|
|
|
end
|
2025-01-07 09:41:24 -05:00
|
|
|
end
|