2024-08-09 11:22:57 -04:00
|
|
|
class Account::TransactionsController < ApplicationController
|
|
|
|
layout :with_sidebar
|
|
|
|
|
|
|
|
before_action :set_account
|
|
|
|
before_action :set_entry, only: :update
|
|
|
|
|
|
|
|
def index
|
2024-08-16 15:00:05 +02:00
|
|
|
@pagy, @entries = pagy(
|
|
|
|
@account.entries.account_transactions.reverse_chronological,
|
|
|
|
limit: params[:per_page] || "10"
|
|
|
|
)
|
2024-08-09 11:22:57 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
2024-08-26 19:10:17 -04:00
|
|
|
@entry.update!(entry_params)
|
2024-08-09 11:22:57 -04:00
|
|
|
|
|
|
|
respond_to do |format|
|
|
|
|
format.html { redirect_to account_entry_path(@account, @entry), notice: t(".success") }
|
|
|
|
format.turbo_stream { render turbo_stream: turbo_stream.replace(@entry) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def set_account
|
|
|
|
@account = Current.family.accounts.find(params[:account_id])
|
|
|
|
end
|
|
|
|
|
|
|
|
def set_entry
|
|
|
|
@entry = @account.entries.find(params[:id])
|
|
|
|
end
|
|
|
|
|
|
|
|
def entry_params
|
|
|
|
params.require(:account_entry)
|
|
|
|
.permit(
|
2024-10-09 14:59:18 -04:00
|
|
|
:name, :date, :amount, :currency, :excluded, :notes, :entryable_type, :nature,
|
2024-08-09 11:22:57 -04:00
|
|
|
entryable_attributes: [
|
|
|
|
:id,
|
|
|
|
:category_id,
|
|
|
|
:merchant_id,
|
|
|
|
{ tag_ids: [] }
|
|
|
|
]
|
2024-08-26 19:10:17 -04:00
|
|
|
).tap do |permitted_params|
|
|
|
|
nature = permitted_params.delete(:nature)
|
|
|
|
|
|
|
|
if permitted_params[:amount]
|
|
|
|
amount_value = permitted_params[:amount].to_d
|
|
|
|
|
|
|
|
if nature == "income"
|
|
|
|
amount_value *= -1
|
|
|
|
end
|
2024-08-09 11:22:57 -04:00
|
|
|
|
2024-08-26 19:10:17 -04:00
|
|
|
permitted_params[:amount] = amount_value
|
|
|
|
end
|
|
|
|
end
|
2024-08-09 11:22:57 -04:00
|
|
|
end
|
|
|
|
end
|