2024-02-02 09:05:04 -06:00
|
|
|
class AccountsController < ApplicationController
|
2024-07-26 18:00:41 +02:00
|
|
|
layout :with_sidebar
|
2024-05-17 09:09:32 -04:00
|
|
|
|
2024-03-01 17:17:34 -05:00
|
|
|
include Filterable
|
2024-08-23 10:06:24 -04:00
|
|
|
before_action :set_account, only: %i[edit show destroy sync update]
|
2024-02-02 10:39:16 -06:00
|
|
|
|
2024-04-18 07:56:51 -04:00
|
|
|
def index
|
2024-06-13 14:37:27 -04:00
|
|
|
@institutions = Current.family.institutions
|
|
|
|
@accounts = Current.family.accounts.ungrouped.alphabetically
|
2024-04-18 07:56:51 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def summary
|
|
|
|
snapshot = Current.family.snapshot(@period)
|
|
|
|
@net_worth_series = snapshot[:net_worth_series]
|
|
|
|
@asset_series = snapshot[:asset_series]
|
|
|
|
@liability_series = snapshot[:liability_series]
|
2024-05-03 12:11:31 +00:00
|
|
|
@accounts = Current.family.accounts
|
|
|
|
@account_groups = @accounts.by_group(period: @period, currency: Current.family.currency)
|
2024-04-18 07:56:51 -04:00
|
|
|
end
|
|
|
|
|
2024-04-29 14:56:38 +01:00
|
|
|
def list
|
2024-07-16 14:08:24 -04:00
|
|
|
render layout: false
|
2024-04-29 14:56:38 +01:00
|
|
|
end
|
|
|
|
|
2024-02-02 09:05:04 -06:00
|
|
|
def new
|
2024-07-17 08:57:17 -04:00
|
|
|
@account = Account.new(accountable: Accountable.from_type(params[:type])&.new)
|
2024-06-13 14:37:27 -04:00
|
|
|
|
2024-08-23 08:47:08 -04:00
|
|
|
@account.accountable.address = Address.new if @account.accountable.is_a?(Property)
|
|
|
|
|
2024-06-13 14:37:27 -04:00
|
|
|
if params[:institution_id]
|
|
|
|
@account.institution = Current.family.institutions.find_by(id: params[:institution_id])
|
|
|
|
end
|
2024-02-02 15:31:32 -06:00
|
|
|
end
|
|
|
|
|
2024-02-02 09:05:04 -06:00
|
|
|
def show
|
2024-08-02 17:09:25 -04:00
|
|
|
@series = @account.series(period: @period)
|
|
|
|
@trend = @series.trend
|
2024-02-02 09:05:04 -06:00
|
|
|
end
|
2024-02-02 10:39:16 -06:00
|
|
|
|
2024-06-13 14:37:27 -04:00
|
|
|
def edit
|
|
|
|
end
|
|
|
|
|
2024-03-07 10:55:51 -05:00
|
|
|
def update
|
2024-07-15 17:03:35 +03:00
|
|
|
Account.transaction do
|
|
|
|
@account.update! account_params.except(:accountable_type, :balance)
|
|
|
|
@account.update_balance!(account_params[:balance]) if account_params[:balance]
|
|
|
|
end
|
2024-07-12 13:47:39 -04:00
|
|
|
@account.sync_later
|
2024-06-11 18:47:38 -04:00
|
|
|
redirect_back_or_to account_path(@account), notice: t(".success")
|
2024-03-07 10:55:51 -05:00
|
|
|
end
|
|
|
|
|
2024-02-02 10:39:16 -06:00
|
|
|
def create
|
2024-06-13 09:16:00 -04:00
|
|
|
@account = Current.family
|
|
|
|
.accounts
|
|
|
|
.create_with_optional_start_balance! \
|
|
|
|
attributes: account_params.except(:start_date, :start_balance),
|
|
|
|
start_date: account_params[:start_date],
|
|
|
|
start_balance: account_params[:start_balance]
|
2024-07-08 14:53:45 +02:00
|
|
|
@account.sync_later
|
2024-06-13 14:37:27 -04:00
|
|
|
redirect_back_or_to account_path(@account), notice: t(".success")
|
2024-07-08 14:53:45 +02:00
|
|
|
rescue ActiveRecord::RecordInvalid => e
|
|
|
|
redirect_back_or_to accounts_path, alert: e.record.errors.full_messages.to_sentence
|
2024-02-02 10:39:16 -06:00
|
|
|
end
|
|
|
|
|
2024-03-31 23:36:54 +02:00
|
|
|
def destroy
|
|
|
|
@account.destroy!
|
|
|
|
redirect_to accounts_path, notice: t(".success")
|
|
|
|
end
|
|
|
|
|
2024-03-21 13:39:10 -04:00
|
|
|
def sync
|
2024-06-13 17:03:38 -04:00
|
|
|
unless @account.syncing?
|
2024-05-20 16:34:48 +02:00
|
|
|
@account.sync_later
|
2024-03-21 13:39:10 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-07-05 13:36:18 +02:00
|
|
|
def sync_all
|
2024-07-10 11:22:59 -04:00
|
|
|
Current.family.accounts.active.sync
|
|
|
|
redirect_back_or_to accounts_path, notice: t(".success")
|
2024-07-05 13:36:18 +02:00
|
|
|
end
|
|
|
|
|
2024-02-02 10:39:16 -06:00
|
|
|
private
|
|
|
|
|
2024-05-17 09:09:32 -04:00
|
|
|
def set_account
|
|
|
|
@account = Current.family.accounts.find(params[:id])
|
|
|
|
end
|
2024-03-31 23:36:54 +02:00
|
|
|
|
2024-05-17 09:09:32 -04:00
|
|
|
def account_params
|
2024-06-13 14:37:27 -04:00
|
|
|
params.require(:account).permit(:name, :accountable_type, :balance, :start_date, :start_balance, :currency, :subtype, :is_active, :institution_id)
|
2024-06-13 09:16:00 -04:00
|
|
|
end
|
2024-02-02 09:05:04 -06:00
|
|
|
end
|