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
|
|
|
|
|
|
|
def create
|
|
|
|
account = Current.family.accounts.find(params.dig(:entry, :account_id))
|
|
|
|
|
2025-07-10 10:31:40 -04:00
|
|
|
if entry_params[:date].to_date == Date.current
|
|
|
|
account.update_current_balance!(balance: entry_params[:amount].to_d)
|
|
|
|
else
|
|
|
|
account.reconcile_balance!(
|
|
|
|
balance: entry_params[:amount].to_d,
|
|
|
|
date: entry_params[:date].to_date
|
|
|
|
)
|
|
|
|
end
|
2025-04-14 11:40:34 -04:00
|
|
|
|
2025-07-10 10:31:40 -04:00
|
|
|
account.sync_later
|
2025-04-14 11:40:34 -04:00
|
|
|
|
2025-07-10 10:31:40 -04:00
|
|
|
respond_to do |format|
|
|
|
|
format.html { redirect_back_or_to account_path(account), notice: "Account value updated" }
|
|
|
|
format.turbo_stream { stream_redirect_back_or_to(account_path(account), notice: "Account value updated") }
|
2025-04-14 11:40:34 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
2025-07-10 10:31:40 -04:00
|
|
|
# ActiveRecord::Base.transaction do
|
|
|
|
@entry.account.reconcile_balance!(
|
|
|
|
balance: entry_params[:amount].to_d,
|
|
|
|
date: entry_params[:date].to_date
|
2025-07-03 09:33:07 -04:00
|
|
|
)
|
|
|
|
|
2025-07-10 10:31:40 -04:00
|
|
|
if entry_params[:notes].present?
|
|
|
|
@entry.update!(notes: entry_params[:notes])
|
|
|
|
end
|
|
|
|
|
|
|
|
@entry.account.sync_later
|
|
|
|
|
|
|
|
@entry.reload
|
|
|
|
|
|
|
|
respond_to do |format|
|
|
|
|
format.html { redirect_back_or_to account_path(@entry.account), notice: "Account value updated" }
|
|
|
|
format.turbo_stream do
|
|
|
|
render turbo_stream: [
|
|
|
|
turbo_stream.replace(
|
|
|
|
dom_id(@entry, :header),
|
|
|
|
partial: "valuations/header",
|
|
|
|
locals: { entry: @entry }
|
|
|
|
),
|
|
|
|
turbo_stream.replace(@entry)
|
|
|
|
]
|
2025-04-14 11:40:34 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
def entry_params
|
2025-07-10 10:31:40 -04:00
|
|
|
params.require(:entry).permit(:date, :amount, :notes)
|
2025-04-14 11:40:34 -04:00
|
|
|
end
|
|
|
|
end
|