Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby
3.8 KB
Raw Blame
<%# locals: (transactions:, period_type:, start_date:, end_date:, filter_tags:) %>
<div>
  <%
    # Params every link back to this report keeps.
    base_params = {
      period_type: period_type,
      start_date: start_date,
      end_date: end_date,
      sort_by: params[:sort_by],
      sort_direction: params[:sort_direction],
      filter_tag_ids: filter_tags.map(&:id).presence
    }.compact
  %>

  <%# Export Controls %>
  <div class="flex items-center justify-end mb-4 flex-wrap gap-3">
    <%# Export Options %>
    <div class="flex flex-wrap items-center justify-end gap-2">
      <%= link_to export_transactions_reports_path(base_params.merge(format: :csv)),
                  class: "font-medium whitespace-nowrap inline-flex items-center gap-1 rounded-lg px-3 py-2 text-sm text-primary border border-secondary bg-transparent hover:bg-surface-hover" do %>
        <%= icon("download", class: "w-3 h-3") %>
        <span><%= t("reports.transactions_breakdown.export.csv") %></span>
      <% end %>
      <%= link_to google_sheets_instructions_reports_path(base_params),
                  class: "font-medium whitespace-nowrap inline-flex items-center gap-1 rounded-lg px-3 py-2 text-sm text-primary border border-secondary bg-transparent hover:bg-surface-hover",
                  data: { turbo_frame: "modal" } do %>
        <%= icon("external-link", class: "w-3 h-3") %>
        <span><%= t("reports.transactions_breakdown.export.google_sheets") %></span>
      <% end %>
    </div>
  </div>

  <%# 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. The helper keeps the period, trend
      # selection, and granularity, so sorting does not drop the chart.
      amount_sort_params = { sort_by: "amount", sort_direction: next_sort_direction }
    %>

    <div class="space-y-8">
      <%# Income Section %>
      <% if income_groups.any? %>
        <%= render "reports/breakdown_table",
              groups: income_groups,
              total: income_total,
              type: :income,
              amount_sort_params: amount_sort_params,
              current_sort_by: current_sort_by,
              current_sort_direction: current_sort_direction,
              start_date: start_date,
              end_date: end_date,
              filter_tags: filter_tags %>
      <% end %>

      <%# Expenses Section %>
      <% if expense_groups.any? %>
        <%= render "reports/breakdown_table",
              groups: expense_groups,
              total: expense_total,
              type: :expense,
              amount_sort_params: amount_sort_params,
              current_sort_by: current_sort_by,
              current_sort_direction: current_sort_direction,
              start_date: start_date,
              end_date: end_date,
              filter_tags: filter_tags %>
      <% end %>
    </div>

    <%# Summary Stats %>
    <div class="mt-4 text-xs text-subdued">
      <%= t("reports.transactions_breakdown.pagination.showing", count: transactions.sum { |g| g[:count] }) %>
    </div>
  <% else %>
    <div class="text-center py-8 text-subdued">
      <%= t("reports.transactions_breakdown.no_transactions") %>
    </div>
  <% end %>
</div>