diff --git a/app/controllers/transactions_controller.rb b/app/controllers/transactions_controller.rb index 55bb8100..accdeab3 100644 --- a/app/controllers/transactions_controller.rb +++ b/app/controllers/transactions_controller.rb @@ -67,9 +67,6 @@ class TransactionsController < ApplicationController redirect_back_or_to transactions_url, notice: t(".success") end - def rules - end - private def amount @@ -93,7 +90,8 @@ class TransactionsController < ApplicationController end def search_params - params.fetch(:q, {}).permit(:start_date, :end_date, :search, accounts: [], account_ids: [], categories: [], merchants: []) + params.fetch(:q, {}) + .permit(:start_date, :end_date, :search, :amount, :amount_operator, accounts: [], account_ids: [], categories: [], merchants: [], types: []) end def transaction_entry_params diff --git a/app/helpers/transactions_helper.rb b/app/helpers/transactions_helper.rb index eab4bce1..23bbede6 100644 --- a/app/helpers/transactions_helper.rb +++ b/app/helpers/transactions_helper.rb @@ -1,12 +1,12 @@ module TransactionsHelper def transaction_search_filters [ - { key: "account_filter", name: "Account", icon: "layers" }, - { key: "date_filter", name: "Date", icon: "calendar" }, - { key: "type_filter", name: "Type", icon: "shapes" }, - { key: "amount_filter", name: "Amount", icon: "hash" }, - { key: "category_filter", name: "Category", icon: "tag" }, - { key: "merchant_filter", name: "Merchant", icon: "store" } + { key: "account_filter", icon: "layers" }, + { key: "date_filter", icon: "calendar" }, + { key: "type_filter", icon: "shapes" }, + { key: "amount_filter", icon: "hash" }, + { key: "category_filter", icon: "tag" }, + { key: "merchant_filter", icon: "store" } ] end diff --git a/app/models/account/entry.rb b/app/models/account/entry.rb index 56754cb6..9bedbab0 100644 --- a/app/models/account/entry.rb +++ b/app/models/account/entry.rb @@ -148,6 +148,27 @@ class Account::Entry < ApplicationRecord query = query.where("account_entries.date >= ?", params[:start_date]) if params[:start_date].present? query = query.where("account_entries.date <= ?", params[:end_date]) if params[:end_date].present? + if params[:types].present? + query = query.where(marked_as_transfer: false) unless params[:types].include?("transfer") + + if params[:types].include?("income") && !params[:types].include?("expense") + query = query.where("account_entries.amount < 0") + elsif params[:types].include?("expense") && !params[:types].include?("income") + query = query.where("account_entries.amount >= 0") + end + end + + if params[:amount].present? && params[:amount_operator].present? + case params[:amount_operator] + when "equal" + query = query.where("ABS(ABS(account_entries.amount) - ?) <= 0.01", params[:amount].to_f.abs) + when "less" + query = query.where("ABS(account_entries.amount) < ?", params[:amount].to_f.abs) + when "greater" + query = query.where("ABS(account_entries.amount) > ?", params[:amount].to_f.abs) + end + end + if params[:accounts].present? || params[:account_ids].present? query = query.joins(:account) end diff --git a/app/views/transactions/searches/_menu.html.erb b/app/views/transactions/searches/_menu.html.erb index 2964ea1d..d474ca55 100644 --- a/app/views/transactions/searches/_menu.html.erb +++ b/app/views/transactions/searches/_menu.html.erb @@ -1,3 +1,5 @@ +<%# locals: (form:) %> +
<%= lucide_icon(filter[:icon], class: "w-5 h-5") %> - <%= filter[:name] %> + <%= t(".#{filter[:key]}") %> <% end %>
-
+
<% transaction_search_filters.each do |filter| %>
<%= render partial: get_transaction_search_filter_partial_path(filter), locals: { form: form } %>
<% end %>
-
- <%= button_tag type: "reset", data: { action: "menu#close" }, class: "py-2 px-3 bg-gray-50 rounded-lg text-sm text-gray-900 font-medium" do %> - Cancel - <% end %> - <%= form.submit "Apply", name: nil, class: "py-2 px-3 bg-gray-900 hover:bg-gray-700 rounded-lg text-sm text-white font-medium cursor-pointer" %> + +
+
+ <% if @q.present? %> + <%= link_to t(".clear_filters"), transactions_path, class: "text-sm underline text-gray-500" %> + <% end %> +
+ +
+ <%= button_tag type: "reset", data: { action: "menu#close" }, class: "py-2 px-3 bg-gray-50 rounded-lg text-sm text-gray-900 font-medium" do %> + <%= t(".cancel") %> + <% end %> + <%= form.submit t(".apply"), name: nil, class: "py-2 px-3 bg-gray-900 hover:bg-gray-700 rounded-lg text-sm text-white font-medium cursor-pointer" %> +
diff --git a/app/views/transactions/searches/_search.html.erb b/app/views/transactions/searches/_search.html.erb index 506444ea..0108d95a 100644 --- a/app/views/transactions/searches/_search.html.erb +++ b/app/views/transactions/searches/_search.html.erb @@ -1,10 +1,24 @@ <%= render "transactions/searches/form" %>