<%# Export Controls %>
<% # Build params hash for links base_params = { period_type: period_type, start_date: start_date, end_date: end_date, sort_by: params[:sort_by], sort_direction: params[:sort_direction] }.compact %> <%# Export Options %>
<%= t("reports.transactions_breakdown.export.label") %>: <%= link_to export_transactions_reports_path(base_params.merge(format: :csv)), class: "inline-flex items-center gap-1 text-sm px-3 py-1 bg-surface-inset text-secondary hover:bg-surface-hover rounded-lg" do %> <%= icon("download", class: "w-3 h-3") %> <%= t("reports.transactions_breakdown.export.csv") %> <% end %> <%= link_to google_sheets_instructions_reports_path(base_params), class: "inline-flex items-center gap-1 text-sm px-3 py-1 bg-surface-inset text-secondary hover:bg-surface-hover rounded-lg", data: { turbo_frame: "modal" } do %> <%= icon("external-link", class: "w-3 h-3") %> <%= t("reports.transactions_breakdown.export.google_sheets") %> <% end %>
<%# Transactions Tables - Split by Income and Expenses %> <% if transactions.any? %> <% # Separate income and expenses income_groups = transactions.select { |g| g[:type] == "income" } expense_groups = transactions.select { |g| g[:type] == "expense" } # Calculate totals income_total = income_groups.sum { |g| g[:total] } expense_total = expense_groups.sum { |g| g[:total] } # Determine sort direction for Amount column current_sort_by = params[:sort_by] current_sort_direction = params[:sort_direction] # Toggle sort direction: if currently sorting by amount desc, switch to asc; otherwise default to desc next_sort_direction = (current_sort_by == "amount" && current_sort_direction == "desc") ? "asc" : "desc" # Build params for amount sort link amount_sort_params = base_params.merge(sort_by: "amount", sort_direction: next_sort_direction) %>
<%# Income Section %> <% if income_groups.any? %>

<%= icon("trending-up", class: "w-5 h-5") %> <%= t("reports.transactions_breakdown.table.income") %> (<%= Money.new(income_total, Current.family.currency).format %>)

<% income_groups.each do |group| %> <% percentage = income_total.zero? ? 0 : (group[:total].to_f / income_total * 100).round(1) %> <% end %>
<%= t("reports.transactions_breakdown.table.category") %> <%= link_to reports_path(amount_sort_params), class: "inline-flex items-center gap-1 hover:text-primary" do %> <%= t("reports.transactions_breakdown.table.amount") %> <% if current_sort_by == "amount" %> <%= icon(current_sort_direction == "desc" ? "chevron-down" : "chevron-up", class: "w-3 h-3") %> <% end %> <% end %> <%= t("reports.transactions_breakdown.table.percentage") %>
<%= group[:category_name] %> (<%= t("reports.transactions_breakdown.table.entries", count: group[:count]) %>)
<%= Money.new(group[:total], Current.family.currency).format %> <%= percentage %>%
<% end %> <%# Expenses Section %> <% if expense_groups.any? %>

<%= icon("trending-down", class: "w-5 h-5") %> <%= t("reports.transactions_breakdown.table.expense") %> (<%= Money.new(expense_total, Current.family.currency).format %>)

<% expense_groups.each do |group| %> <% percentage = expense_total.zero? ? 0 : (group[:total].to_f / expense_total * 100).round(1) %> <% end %>
<%= t("reports.transactions_breakdown.table.category") %> <%= link_to reports_path(amount_sort_params), class: "inline-flex items-center gap-1 hover:text-primary" do %> <%= t("reports.transactions_breakdown.table.amount") %> <% if current_sort_by == "amount" %> <%= icon(current_sort_direction == "desc" ? "chevron-down" : "chevron-up", class: "w-3 h-3") %> <% end %> <% end %> <%= t("reports.transactions_breakdown.table.percentage") %>
<%= group[:category_name] %> (<%= t("reports.transactions_breakdown.table.entries", count: group[:count]) %>)
<%= Money.new(group[:total], Current.family.currency).format %> <%= percentage %>%
<% end %>
<%# Summary Stats %>
<%= t("reports.transactions_breakdown.pagination.showing", count: transactions.sum { |g| g[:count] }) %>
<% else %>
<%= t("reports.transactions_breakdown.no_transactions") %>
<% end %>