BananaCrystal / email-templates

This repository contains in-house email templates that serve as structure for our emails. Each template can be updated and modified to fit requirements.
MIT License
0 stars 0 forks source link

card_request_reports.rb #10

Open geekelo opened 3 months ago

geekelo commented 3 months ago

FILE

require 'prawn'
require 'prawn/table'

ActiveAdmin.register Card::Request, as: 'CardRequestReport' do
  menu parent: 'Reports', label: 'Card Request Reports'

  config.filters = false
  config.batch_actions = false

  index title: "Card Request Reports by Date Range" do
    panel "Filter Card Request Reports" do
      render 'admin/card_requests/transactions_by_date_form'
    end
    panel "Report Downloads" do
      div do
        link_to "Download Detailed CSV", admin_card_request_reports_path(format: :csv), method: :get
        para "The CSV file contains detailed data for each card request record for the range selected."
        br
        link_to "Download Summary PDF", admin_card_request_reports_path(format: :pdf), method: :get
        para "The PDF file provides a summary overview of the card request for the range selected."
      end
    end
  end

  controller do
    before_action :set_date_range, only: [:index]

    def set_date_range
      @start_date = params.dig(:report, :start_date).present? ? Date.parse(params[:report][:start_date]) : Time.zone.today
      @end_date = params.dig(:report, :end_date).present? ? Date.parse(params[:report][:end_date]) : Time.zone.today
    rescue ArgumentError
      @start_date = Time.zone.today
      @end_date = Time.zone.today
    end

    def index
      @transactions_by_date_range = Card::Request.between_dates(@start_date, @end_date)
      if params.dig(:report, :status).present?
        @transactions_by_date_range = @transactions_by_date_range.where(status: params[:report][:status])
      end

      respond_to do |format|
        format.html
        format.csv { send_data generate_csv(@transactions_by_date_range), filename: "card_request_report.csv" }
        format.pdf { send_data generate_pdf(@transactions_by_date_range), filename: "card_request_summary_report.pdf", type: 'application/pdf' }
      end
    end

    private

    def generate_csv(transactions)
      CSV.generate(headers: true) do |csv|
        csv << ["User ID", "External Id", "Status", "Name", "Email", "Card Type", "Total Amount", "Provider Amount", "Bananacrystal Amount"]
        transactions.each do |cr|
          csv << [
            cr.user_id,
            cr.external_id,
            cr.status,
            cr.user.name,
            cr.user.email,
            cr.card_type,
            cr.total_fee,
            cr.total_provider_fee,
            cr.total_bananacrystal_fee
          ]
        end
      end
    end

    def generate_pdf(transactions)
      Prawn::Document.new do |pdf|
        pdf.text "Card Request Report Summary", size: 20, style: :bold
        pdf.move_down 10
        pdf.text "Date Range: #{@start_date.strftime('%Y-%m-%d')} to #{@end_date.strftime('%Y-%m-%d')}"
        pdf.move_down 10

        data = [
          ["Metric", "Count"],
          ["Number of Denied Card Requests", transactions.declined.between_dates(@start_date, @end_date).count],
          ["Number of Pending Card Requests", transactions.pending.between_dates(@start_date, @end_date).count],
          ["Number of Approved Card Requests", transactions.completed.between_dates(@start_date, @end_date).count],
          ["Total Card Requests", transactions.count]
        ]

        pdf.table(data, header: true) do
          row(0).font_style = :bold
          columns(0..1).align = :center
          self.header = true
          self.column_widths = [200, 200]
          self.row_colors = ["DDDDDD", "FFFFFF"]
        end
      end.render
    end
  end
end
geekelo commented 3 months ago

This file configures an ActiveAdmin interface to manage reports related to card requests. The interface provides options to filter, view, and download reports for Card::Request records within a specified date range. Here’s a breakdown of the key elements:

File: card_request_report.rb

1. Menu Configuration

2. Filters and Batch Actions

3. Index Page

4. Controller

Summary:

This configuration is particularly useful for administrators who need to monitor, analyze, and export data related to card requests over specific time periods.