2025-04-14 11:40:34 -04:00
|
|
|
class ValuationsController < ApplicationController
|
2025-07-03 09:33:07 -04:00
|
|
|
include EntryableResource, StreamExtensions
|
2025-04-14 11:40:34 -04:00
|
|
|
|
2025-07-15 18:58:40 -04:00
|
|
|
def confirm_create
|
|
|
|
@account = Current.family.accounts.find(params.dig(:entry, :account_id))
|
|
|
|
@entry = @account.entries.build(entry_params.merge(currency: @account.currency))
|
|
|
|
|
2025-07-16 11:31:47 -04:00
|
|
|
@reconciliation_dry_run = @entry.account.create_reconciliation(
|
|
|
|
balance: entry_params[:amount],
|
|
|
|
date: entry_params[:date],
|
|
|
|
dry_run: true
|
|
|
|
)
|
|
|
|
|
2025-07-15 18:58:40 -04:00
|
|
|
render :confirm_create
|
|
|
|
end
|
|
|
|
|
|
|
|
def confirm_update
|
|
|
|
@entry = Current.family.entries.find(params[:id])
|
|
|
|
@account = @entry.account
|
|
|
|
@entry.assign_attributes(entry_params.merge(currency: @account.currency))
|
|
|
|
|
2025-07-16 11:31:47 -04:00
|
|
|
@reconciliation_dry_run = @entry.account.update_reconciliation(
|
|
|
|
@entry,
|
|
|
|
balance: entry_params[:amount],
|
|
|
|
date: entry_params[:date],
|
|
|
|
dry_run: true
|
|
|
|
)
|
|
|
|
|
2025-07-15 18:58:40 -04:00
|
|
|
render :confirm_update
|
|
|
|
end
|
|
|
|
|
2025-04-14 11:40:34 -04:00
|
|
|
def create
|
|
|
|
account = Current.family.accounts.find(params.dig(:entry, :account_id))
|
|
|
|
|
2025-07-16 11:31:47 -04:00
|
|
|
result = account.create_reconciliation(
|
|
|
|
balance: entry_params[:amount],
|
|
|
|
date: entry_params[:date],
|
|
|
|
)
|
2025-04-14 11:40:34 -04:00
|
|
|
|
2025-07-16 11:31:47 -04:00
|
|
|
if result.success?
|
2025-04-14 11:40:34 -04:00
|
|
|
respond_to do |format|
|
2025-07-16 11:31:47 -04:00
|
|
|
format.html { redirect_back_or_to account_path(account), notice: "Account updated" }
|
|
|
|
format.turbo_stream { stream_redirect_back_or_to(account_path(account), notice: "Account updated") }
|
2025-04-14 11:40:34 -04:00
|
|
|
end
|
|
|
|
else
|
2025-07-03 09:33:07 -04:00
|
|
|
@error_message = result.error_message
|
2025-04-14 11:40:34 -04:00
|
|
|
render :new, status: :unprocessable_entity
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
2025-07-16 11:31:47 -04:00
|
|
|
# Notes updating is independent of reconciliation, just a simple CRUD operation
|
|
|
|
@entry.update!(notes: entry_params[:notes]) if entry_params[:notes].present?
|
2025-07-03 09:33:07 -04:00
|
|
|
|
2025-07-16 11:31:47 -04:00
|
|
|
if entry_params[:date].present? && entry_params[:amount].present?
|
|
|
|
result = @entry.account.update_reconciliation(
|
|
|
|
@entry,
|
|
|
|
balance: entry_params[:amount],
|
|
|
|
date: entry_params[:date],
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
if result.nil? || result.success?
|
2025-07-03 09:33:07 -04:00
|
|
|
@entry.reload
|
2025-04-14 11:40:34 -04:00
|
|
|
|
|
|
|
respond_to do |format|
|
2025-07-16 11:31:47 -04:00
|
|
|
format.html { redirect_back_or_to account_path(@entry.account), notice: "Entry updated" }
|
2025-04-14 11:40:34 -04:00
|
|
|
format.turbo_stream do
|
|
|
|
render turbo_stream: [
|
|
|
|
turbo_stream.replace(
|
|
|
|
dom_id(@entry, :header),
|
|
|
|
partial: "valuations/header",
|
|
|
|
locals: { entry: @entry }
|
|
|
|
),
|
|
|
|
turbo_stream.replace(@entry)
|
|
|
|
]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
else
|
2025-07-03 09:33:07 -04:00
|
|
|
@error_message = result.error_message
|
2025-04-14 11:40:34 -04:00
|
|
|
render :show, status: :unprocessable_entity
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
def entry_params
|
2025-07-16 11:31:47 -04:00
|
|
|
params.require(:entry).permit(:date, :amount, :notes)
|
2025-07-15 18:58:40 -04:00
|
|
|
end
|
2025-04-14 11:40:34 -04:00
|
|
|
end
|