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-11-04 20:27:31 -05:00
|
|
|
prev_amount = @entry.amount
|
|
|
|
prev_date = @entry.date
|
|
|
|
|
|
|
|
@entry.update!(entry_params.except(:origin))
|
|
|
|
@entry.sync_account_later if prev_amount != @entry.amount || prev_date != @entry.date
|
2024-08-09 11:22:57 -04:00
|
|
|
|
|
|
|
respond_to do |format|
|
|
|
|
format.html { redirect_to account_entry_path(@account, @entry), notice: t(".success") }
|
2024-11-04 20:27:31 -05:00
|
|
|
format.turbo_stream do
|
|
|
|
render turbo_stream: turbo_stream.replace(
|
|
|
|
@entry,
|
|
|
|
partial: "account/entries/entry",
|
|
|
|
locals: entry_locals.merge(entry: @entry)
|
|
|
|
)
|
|
|
|
end
|
2024-08-09 11:22:57 -04:00
|
|
|
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
|
|
|
|
|
2024-11-04 20:27:31 -05:00
|
|
|
def entry_locals
|
|
|
|
{
|
|
|
|
selectable: entry_params[:origin].present?,
|
|
|
|
show_balance: entry_params[:origin] == "account",
|
|
|
|
origin: entry_params[:origin]
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2024-08-09 11:22:57 -04:00
|
|
|
def entry_params
|
|
|
|
params.require(:account_entry)
|
|
|
|
.permit(
|
2024-11-04 20:27:31 -05:00
|
|
|
:name, :date, :amount, :currency, :excluded, :notes, :entryable_type, :nature, :origin,
|
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
|