2024-02-23 21:34:33 -05:00
|
|
|
class TransactionsController < ApplicationController
|
2024-05-17 09:09:32 -04:00
|
|
|
layout "with_sidebar"
|
|
|
|
|
2024-02-23 21:34:33 -05:00
|
|
|
before_action :set_transaction, only: %i[ show edit update destroy ]
|
|
|
|
|
|
|
|
def index
|
2024-05-30 20:55:18 -04:00
|
|
|
@q = search_params
|
|
|
|
result = Current.family.transactions.search(@q).ordered
|
2024-06-21 21:34:40 +05:30
|
|
|
@pagy, @transactions = pagy(result, items: params[:per_page] || "10")
|
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 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
|
2024-05-23 08:09:33 -04:00
|
|
|
.find(params[:transaction][:account_id])
|
|
|
|
.transactions.build(transaction_params.merge(amount: amount))
|
2024-02-23 21:34:33 -05:00
|
|
|
|
2024-05-30 20:55:18 -04:00
|
|
|
@transaction.save!
|
|
|
|
@transaction.sync_account_later
|
|
|
|
redirect_to transactions_url, notice: t(".success")
|
2024-02-23 21:34:33 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
2024-05-30 20:55:18 -04:00
|
|
|
@transaction.update! transaction_params
|
|
|
|
@transaction.sync_account_later
|
2024-05-23 08:09:33 -04:00
|
|
|
|
2024-05-30 20:55:18 -04:00
|
|
|
redirect_to transaction_url(@transaction), notice: t(".success")
|
2024-02-23 21:34:33 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def destroy
|
|
|
|
@transaction.destroy!
|
2024-05-30 20:55:18 -04:00
|
|
|
@transaction.sync_account_later
|
|
|
|
redirect_to transactions_url, notice: t(".success")
|
2024-02-23 21:34:33 -05:00
|
|
|
end
|
|
|
|
|
2024-06-07 16:56:30 -04:00
|
|
|
def bulk_delete
|
|
|
|
destroyed = Current.family.transactions.destroy_by(id: bulk_delete_params[:transaction_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
|
|
|
|
transactions = Current.family.transactions.where(id: bulk_update_params[:transaction_ids])
|
2024-06-07 18:59:46 -04:00
|
|
|
if transactions.update_all(bulk_update_params.except(:transaction_ids).to_h.compact_blank!)
|
2024-06-19 06:52:08 -04:00
|
|
|
redirect_back_or_to transactions_url, notice: t(".success", count: transactions.count)
|
2024-06-07 16:56:30 -04:00
|
|
|
else
|
2024-06-07 18:59:46 -04:00
|
|
|
flash.now[:error] = t(".failure")
|
|
|
|
render :index, status: :unprocessable_entity
|
2024-06-07 16:56:30 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-06-19 06:52:08 -04:00
|
|
|
def mark_transfers
|
|
|
|
Current.family
|
|
|
|
.transactions
|
|
|
|
.where(id: bulk_update_params[:transaction_ids])
|
|
|
|
.mark_transfers!
|
|
|
|
|
|
|
|
redirect_back_or_to transactions_url, notice: t(".success")
|
|
|
|
end
|
|
|
|
|
|
|
|
def unmark_transfers
|
|
|
|
Current.family
|
|
|
|
.transactions
|
|
|
|
.where(id: bulk_update_params[:transaction_ids])
|
|
|
|
.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-02-23 21:34:33 -05:00
|
|
|
def set_transaction
|
2024-05-30 20:55:18 -04:00
|
|
|
@transaction = Current.family.transactions.find(params[:id])
|
2024-02-23 21:34:33 -05:00
|
|
|
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-06-07 16:56:30 -04:00
|
|
|
def bulk_delete_params
|
|
|
|
params.require(:bulk_delete).permit(transaction_ids: [])
|
|
|
|
end
|
|
|
|
|
|
|
|
def bulk_update_params
|
2024-06-07 18:59:46 -04:00
|
|
|
params.require(:bulk_update).permit(:date, :notes, :excluded, :category_id, :merchant_id, transaction_ids: [])
|
2024-06-07 16:56:30 -04:00
|
|
|
end
|
|
|
|
|
2024-05-30 20:55:18 -04:00
|
|
|
def search_params
|
|
|
|
params.fetch(:q, {}).permit(:start_date, :end_date, :search, accounts: [], account_ids: [], categories: [], merchants: [])
|
|
|
|
end
|
|
|
|
|
2024-02-23 21:34:33 -05:00
|
|
|
def transaction_params
|
2024-06-07 16:56:30 -04:00
|
|
|
params.require(:transaction).permit(:name, :date, :amount, :currency, :notes, :excluded, :category_id, :merchant_id, tag_ids: [])
|
2024-02-23 21:34:33 -05:00
|
|
|
end
|
|
|
|
end
|