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_load_reports.rb #8

Open geekelo opened 3 months ago

geekelo commented 3 months ago

FILE

require 'prawn'
require 'prawn/table'

ActiveAdmin.register Card::Load, as: 'CardLoadReport' do
  menu parent: 'Reports', label: 'Card Load Reports'

  config.filters = false
  config.batch_actions = false

  index title: "Card Load Reports by Date Range" do
    panel "Filter Card Load Reports" do
      render 'admin/card_loads/transactions_by_date_form'
    end
    panel "Report Downloads" do
      div do
        link_to "Download Detailed CSV", admin_card_load_reports_path(format: :csv), method: :get
        para "The CSV file contains detailed data for each card load record for the range selected."
        br
        link_to "Download Summary PDF", admin_card_load_reports_path(format: :pdf), method: :get
        para "The PDF file provides a summary overview of the card load 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::Load.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_load_report.csv" }
        format.pdf { send_data generate_pdf(@transactions_by_date_range), filename: "card_load_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", "Amount", "Total Provider Amount", "Provider Fee", "Bananacrystal Fee", "Total Amount"]
        transactions.each do |cl|
          csv << [
            cl.user_id,
            cl.external_id,
            cl.status,
            cl.user.name,
            cl.user.email,
            cl.amount,
            cl.total_provider_fee,
            cl.provider_fee,
            cl.total_bananacrystal_fee,
            cl.total_amount
          ]
        end
      end
    end

    def generate_pdf(transactions)
      Prawn::Document.new do |pdf|
        pdf.text "Card Load 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 Pending Card Loads", transactions.pending.between_dates(@start_date, @end_date).count],
          ["Number of Approved Card Loads", 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 the ActiveAdmin interface for managing and generating reports on Card::Load records, referred to as "Card Load Reports." Here's a detailed explanation:

File: card_load_report.rb

1. Dependencies

2. ActiveAdmin.register Card::Load, as: 'CardLoadReport' do

3. menu parent: 'Reports', label: 'Card Load Reports'

4. config.filters = false

5. config.batch_actions = false

Index Page Layout

6. index title: "Card Load Reports by Date Range" do

Controller Logic

7. controller do

Helper Methods

8. def generate_csv(transactions)

9. def generate_pdf(transactions)

Summary:

This setup is ideal for a system that requires detailed and summarized reporting on card load transactions, providing administrators with flexible tools to analyze and export data.