2024-02-23 21:34:33 -05:00
|
|
|
class TransactionsController < ApplicationController
|
2024-07-26 18:00:41 +02:00
|
|
|
layout :with_sidebar
|
2024-05-17 09:09:32 -04:00
|
|
|
|
2024-02-23 21:34:33 -05:00
|
|
|
def index
|
2024-05-30 20:55:18 -04:00
|
|
|
@q = search_params
|
2024-07-01 10:49:43 -04:00
|
|
|
result = Current.family.entries.account_transactions.search(@q).reverse_chronological
|
2024-07-22 15:49:53 +02:00
|
|
|
@pagy, @transaction_entries = pagy(result, limit: params[:per_page] || "50")
|
2024-05-30 20:55:18 -04:00
|
|
|
|
2024-03-28 13:23:54 -04:00
|
|
|
@totals = {
|
2024-06-19 06:52:08 -04:00
|
|
|
count: result.select { |t| t.currency == Current.family.currency }.count,
|
|
|
|
income: result.income_total(Current.family.currency).abs,
|
|
|
|
expense: result.expense_total(Current.family.currency)
|
2024-03-28 13:23:54 -04:00
|
|
|
}
|
2024-02-23 21:34:33 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def new
|
2024-07-01 10:49:43 -04:00
|
|
|
@entry = Current.family.entries.new(entryable: Account::Transaction.new).tap do |e|
|
2024-04-16 12:44:31 -06:00
|
|
|
if params[:account_id]
|
2024-07-01 10:49:43 -04:00
|
|
|
e.account = Current.family.accounts.find(params[:account_id])
|
2024-09-20 08:38:19 -04:00
|
|
|
e.currency = e.account.currency
|
|
|
|
else
|
|
|
|
e.currency = Current.family.currency
|
2024-04-16 12:44:31 -06:00
|
|
|
end
|
|
|
|
end
|
2024-02-23 21:34:33 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
2024-07-01 10:49:43 -04:00
|
|
|
@entry = Current.family
|
|
|
|
.accounts
|
|
|
|
.find(params[:account_entry][:account_id])
|
|
|
|
.entries
|
|
|
|
.create!(transaction_entry_params.merge(amount: amount))
|
|
|
|
|
|
|
|
@entry.sync_account_later
|
|
|
|
redirect_back_or_to account_path(@entry.account), notice: t(".success")
|
2024-02-23 21:34:33 -05:00
|
|
|
end
|
|
|
|
|
2024-06-07 16:56:30 -04:00
|
|
|
def bulk_delete
|
2024-07-01 10:49:43 -04:00
|
|
|
destroyed = Current.family.entries.destroy_by(id: bulk_delete_params[:entry_ids])
|
2024-06-19 06:52:08 -04:00
|
|
|
redirect_back_or_to transactions_url, notice: t(".success", count: destroyed.count)
|
2024-06-07 16:56:30 -04:00
|
|
|
end
|
|
|
|
|
2024-06-07 18:59:46 -04:00
|
|
|
def bulk_edit
|
|
|
|
end
|
|
|
|
|
2024-06-07 16:56:30 -04:00
|
|
|
def bulk_update
|
2024-07-01 10:49:43 -04:00
|
|
|
updated = Current.family
|
|
|
|
.entries
|
|
|
|
.where(id: bulk_update_params[:entry_ids])
|
|
|
|
.bulk_update!(bulk_update_params)
|
|
|
|
|
|
|
|
redirect_back_or_to transactions_url, notice: t(".success", count: updated)
|
2024-06-07 16:56:30 -04:00
|
|
|
end
|
|
|
|
|
2024-06-19 06:52:08 -04:00
|
|
|
def mark_transfers
|
|
|
|
Current.family
|
2024-07-01 10:49:43 -04:00
|
|
|
.entries
|
|
|
|
.where(id: bulk_update_params[:entry_ids])
|
2024-06-19 06:52:08 -04:00
|
|
|
.mark_transfers!
|
|
|
|
|
|
|
|
redirect_back_or_to transactions_url, notice: t(".success")
|
|
|
|
end
|
|
|
|
|
|
|
|
def unmark_transfers
|
|
|
|
Current.family
|
2024-07-01 10:49:43 -04:00
|
|
|
.entries
|
|
|
|
.where(id: bulk_update_params[:entry_ids])
|
2024-06-19 06:52:08 -04:00
|
|
|
.update_all marked_as_transfer: false
|
|
|
|
|
|
|
|
redirect_back_or_to transactions_url, notice: t(".success")
|
|
|
|
end
|
|
|
|
|
2024-02-23 21:34:33 -05:00
|
|
|
private
|
2024-05-23 08:09:33 -04:00
|
|
|
|
2024-04-16 12:44:31 -06:00
|
|
|
def amount
|
|
|
|
if nature.income?
|
2024-07-01 10:49:43 -04:00
|
|
|
transaction_entry_params[:amount].to_d * -1
|
2024-04-16 12:44:31 -06:00
|
|
|
else
|
2024-07-01 10:49:43 -04:00
|
|
|
transaction_entry_params[:amount].to_d
|
2024-04-16 12:44:31 -06:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def nature
|
2024-07-01 10:49:43 -04:00
|
|
|
params[:account_entry][:nature].to_s.inquiry
|
2024-04-16 12:44:31 -06:00
|
|
|
end
|
|
|
|
|
2024-06-07 16:56:30 -04:00
|
|
|
def bulk_delete_params
|
2024-07-01 10:49:43 -04:00
|
|
|
params.require(:bulk_delete).permit(entry_ids: [])
|
2024-06-07 16:56:30 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def bulk_update_params
|
2024-07-01 10:49:43 -04:00
|
|
|
params.require(:bulk_update).permit(:date, :notes, :category_id, :merchant_id, entry_ids: [])
|
2024-06-07 16:56:30 -04:00
|
|
|
end
|
|
|
|
|
2024-05-30 20:55:18 -04:00
|
|
|
def search_params
|
2024-09-17 10:38:02 -04:00
|
|
|
params.fetch(:q, {})
|
2024-10-04 09:17:48 -04:00
|
|
|
.permit(:start_date, :end_date, :search, :amount, :amount_operator, accounts: [], account_ids: [], categories: [], merchants: [], types: [], tags: [])
|
2024-05-30 20:55:18 -04:00
|
|
|
end
|
|
|
|
|
2024-07-01 10:49:43 -04:00
|
|
|
def transaction_entry_params
|
|
|
|
params.require(:account_entry)
|
|
|
|
.permit(:name, :date, :amount, :currency, :entryable_type, entryable_attributes: [ :category_id ])
|
|
|
|
.with_defaults(entryable_type: "Account::Transaction", entryable_attributes: {})
|
2024-02-23 21:34:33 -05:00
|
|
|
end
|
|
|
|
end
|