2024-02-23 21:34:33 -05:00
|
|
|
class TransactionsController < ApplicationController
|
|
|
|
before_action :set_transaction, only: %i[ show edit update destroy ]
|
|
|
|
|
|
|
|
def index
|
2024-03-28 13:23:54 -04:00
|
|
|
search_params = session[ransack_session_key] || params[:q]
|
|
|
|
@q = Current.family.transactions.ransack(search_params)
|
|
|
|
result = @q.result.order(date: :desc)
|
|
|
|
@pagy, @transactions = pagy(result, items: 10)
|
|
|
|
@totals = {
|
|
|
|
count: result.count,
|
|
|
|
income: result.inflows.sum(&:amount_money).abs,
|
|
|
|
expense: result.outflows.sum(&:amount_money).abs
|
|
|
|
}
|
|
|
|
@filter_list = Transaction.build_filter_list(search_params, Current.family)
|
|
|
|
|
|
|
|
respond_to do |format|
|
|
|
|
format.html
|
|
|
|
format.turbo_stream
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def search
|
|
|
|
if params[:clear]
|
|
|
|
session.delete(ransack_session_key)
|
|
|
|
elsif params[:remove_param]
|
|
|
|
current_params = session[ransack_session_key] || {}
|
2024-04-19 12:03:16 -04:00
|
|
|
if params[:remove_param] == "date_range"
|
|
|
|
updated_params = current_params.except("date_gteq", "date_lteq")
|
|
|
|
elsif params[:remove_param_value]
|
|
|
|
key_to_remove = params[:remove_param]
|
|
|
|
value_to_remove = params[:remove_param_value]
|
|
|
|
updated_params = current_params.deep_dup
|
|
|
|
updated_params[key_to_remove] = updated_params[key_to_remove] - [ value_to_remove ]
|
|
|
|
else
|
|
|
|
updated_params = current_params.except(params[:remove_param])
|
|
|
|
end
|
2024-03-28 13:23:54 -04:00
|
|
|
session[ransack_session_key] = updated_params
|
|
|
|
elsif params[:q]
|
|
|
|
session[ransack_session_key] = params[:q]
|
2024-03-11 14:51:16 +02:00
|
|
|
end
|
|
|
|
|
2024-03-28 13:23:54 -04:00
|
|
|
index
|
2024-03-11 14:51:16 +02:00
|
|
|
|
|
|
|
respond_to do |format|
|
2024-03-28 13:23:54 -04:00
|
|
|
format.html { render :index }
|
|
|
|
format.turbo_stream do
|
|
|
|
render turbo_stream: [
|
|
|
|
turbo_stream.replace("transactions_summary", partial: "transactions/summary", locals: { totals: @totals }),
|
|
|
|
turbo_stream.replace("transactions_search_form", partial: "transactions/search_form", locals: { q: @q }),
|
|
|
|
turbo_stream.replace("transactions_filters", partial: "transactions/filters", locals: { filters: @filter_list }),
|
|
|
|
turbo_stream.replace("transactions_list", partial: "transactions/list", locals: { transactions: @transactions, pagy: @pagy })
|
|
|
|
]
|
|
|
|
end
|
2024-03-11 14:51:16 +02:00
|
|
|
end
|
2024-02-23 21:34:33 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def show
|
|
|
|
end
|
|
|
|
|
|
|
|
def new
|
2024-04-16 12:44:31 -06:00
|
|
|
@transaction = Transaction.new.tap do |txn|
|
|
|
|
if params[:account_id]
|
|
|
|
txn.account = Current.family.accounts.find(params[:account_id])
|
|
|
|
end
|
|
|
|
end
|
2024-02-23 21:34:33 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def edit
|
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
2024-04-16 12:44:31 -06:00
|
|
|
@transaction = Current.family.accounts
|
|
|
|
.find(params[:transaction][:account_id])
|
|
|
|
.transactions.build(transaction_params.merge(amount: amount))
|
2024-02-23 21:34:33 -05:00
|
|
|
|
|
|
|
respond_to do |format|
|
|
|
|
if @transaction.save
|
2024-04-24 21:55:52 +02:00
|
|
|
@transaction.account.sync_later(@transaction.date)
|
2024-03-18 11:21:00 -04:00
|
|
|
format.html { redirect_to transactions_url, notice: t(".success") }
|
2024-02-23 21:34:33 -05:00
|
|
|
else
|
|
|
|
format.html { render :new, status: :unprocessable_entity }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
|
|
|
respond_to do |format|
|
2024-05-02 07:24:31 -06:00
|
|
|
sync_start_date = if transaction_params[:date]
|
|
|
|
[ @transaction.date, Date.parse(transaction_params[:date]) ].compact.min
|
|
|
|
else
|
|
|
|
@transaction.date
|
|
|
|
end
|
|
|
|
|
2024-02-23 21:34:33 -05:00
|
|
|
if @transaction.update(transaction_params)
|
2024-04-24 21:55:52 +02:00
|
|
|
@transaction.account.sync_later(sync_start_date)
|
2024-03-29 12:53:08 -04:00
|
|
|
|
2024-03-14 15:30:36 +01:00
|
|
|
format.html { redirect_to transaction_url(@transaction), notice: t(".success") }
|
|
|
|
format.turbo_stream do
|
|
|
|
render turbo_stream: [
|
2024-04-16 19:33:51 +02:00
|
|
|
turbo_stream.append("notification-tray", partial: "shared/notification", locals: { type: "success", content: { body: t(".success") } }),
|
2024-03-14 15:30:36 +01:00
|
|
|
turbo_stream.replace("transaction_#{@transaction.id}", partial: "transactions/transaction", locals: { transaction: @transaction })
|
|
|
|
]
|
|
|
|
end
|
2024-02-23 21:34:33 -05:00
|
|
|
else
|
|
|
|
format.html { render :edit, status: :unprocessable_entity }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def destroy
|
2024-04-24 21:55:52 +02:00
|
|
|
@account = @transaction.account
|
|
|
|
sync_start_date = @account.transactions.where("date < ?", @transaction.date).order(date: :desc).first&.date
|
2024-02-23 21:34:33 -05:00
|
|
|
@transaction.destroy!
|
2024-04-24 21:55:52 +02:00
|
|
|
@account.sync_later(sync_start_date)
|
2024-02-23 21:34:33 -05:00
|
|
|
|
|
|
|
respond_to do |format|
|
2024-03-14 15:30:36 +01:00
|
|
|
format.html { redirect_to transactions_url, notice: t(".success") }
|
2024-02-23 21:34:33 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
2024-03-28 13:23:54 -04:00
|
|
|
def delete_search_param(params, key, value: nil)
|
|
|
|
if value
|
|
|
|
params[key]&.delete(value)
|
|
|
|
params.delete(key) if params[key].empty? # Remove key if it's empty after deleting value
|
|
|
|
else
|
|
|
|
params.delete(key)
|
|
|
|
end
|
|
|
|
|
|
|
|
params
|
|
|
|
end
|
|
|
|
|
|
|
|
def ransack_session_key
|
|
|
|
:ransack_transactions_q
|
|
|
|
end
|
|
|
|
|
2024-02-23 21:34:33 -05:00
|
|
|
# Use callbacks to share common setup or constraints between actions.
|
|
|
|
def set_transaction
|
|
|
|
@transaction = Transaction.find(params[:id])
|
|
|
|
end
|
|
|
|
|
2024-04-16 12:44:31 -06:00
|
|
|
def amount
|
|
|
|
if nature.income?
|
|
|
|
transaction_params[:amount].to_d * -1
|
|
|
|
else
|
|
|
|
transaction_params[:amount].to_d
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def nature
|
|
|
|
params[:transaction][:nature].to_s.inquiry
|
|
|
|
end
|
|
|
|
|
2024-02-23 21:34:33 -05:00
|
|
|
# Only allow a list of trusted parameters through.
|
|
|
|
def transaction_params
|
2024-04-29 21:17:28 +02:00
|
|
|
params.require(:transaction).permit(:name, :date, :amount, :currency, :notes, :excluded, :category_id, :merchant_id)
|
2024-02-23 21:34:33 -05:00
|
|
|
end
|
|
|
|
end
|