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

kyc_reports.rb #26

Open geekelo opened 1 month ago

geekelo commented 1 month ago

FILE

require 'prawn'
require 'prawn/table'

ActiveAdmin.register Verification, as: 'KycReport' do
  menu parent: 'Reports', label: 'Kyc Reports'

  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 = Verification.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: "detailed_kyc_report.csv" }
        format.pdf { send_data generate_pdf(@transactions_by_date_range), filename: "summary_kyc_report.pdf", type: 'application/pdf' }
      end
    end

    private

    def generate_csv(transactions)
      CSV.generate(headers: true) do |csv|
        csv << ["User ID", "Status", "First Name", "Last Name", "DOB", "Phone Code", "Phone", "Address", "City", "State", "Zip Code", "Country", "Document Type", "Provider", "Review Result", "Review Status", "Provider Decision", "Transaction Data", "Webhook Data", "Applicant ID", "Inspection ID", "Correlation ID", "Provider Result", "Verified At", "Active", "Discarded At"]
        transactions.each do |verification|
          csv << [
            verification.user_id, verification.status, verification.first_name, verification.last_name,
            verification.dob, verification.phone_code, verification.phone, verification.address,
            verification.city, verification.state, verification.zip_code, verification.country,
            verification.doc_type, verification.provider, verification.review_result, verification.review_status,
            verification.provider_decision, verification.transaction_data?.to_s, verification.webhook_data?.to_s,
            verification.applicant_id, verification.inspection_id, verification.correlation_id,
            verification.provider_result, verification.verified_at ? verification.verified_at.strftime("%Y-%m-%d %H:%M:%S") : nil,
            verification.active.to_s, verification.discarded_at ? verification.discarded_at.strftime("%Y-%m-%d %H:%M:%S") : nil
          ]
        end
      end
    end

    def generate_pdf(transactions)
      Prawn::Document.new do |pdf|
        pdf.text "KYC 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 KYC", Verification.denied.between_dates(@start_date, @end_date).count],
          ["Number of Pending KYC", Verification.pending.between_dates(@start_date, @end_date).count],
          ["Number of Accepted KYC", Verification.accepted.between_dates(@start_date, @end_date).count],
          ["Total KYC", 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

  config.filters = false
  config.batch_actions = false

  index title: "KYC Reports by Date Range" do
    panel "Filter KYC Reports" do
      render 'admin/verifications/transactions_by_date_form'
    end
    panel "Report Downloads" do
      div do
        link_to "Download Detailed CSV", admin_kyc_reports_path(format: :csv), method: :get
        para "The CSV file contains detailed data for each verification record for the range selected."
        br
        link_to "Download Summary PDF", admin_kyc_reports_path(format: :pdf), method: :get
        para "The PDF file provides a summary overview of the KYC verifications for the range selected."
      end
    end
  end
end
geekelo commented 1 month ago

This ActiveAdmin configuration defines a custom report page for KYC (Know Your Customer) verifications, named KycReport. This page provides an interface to filter KYC verifications by date range and status, and allows the export of detailed CSV reports and summary PDF reports. Here’s a detailed breakdown of the configuration:

1. Menu Configuration

2. Controller Customizations

The controller is customized to manage the filtering and exporting of KYC data.

a. Date Range Filtering

b. Index Action

3. CSV Generation

4. PDF Generation

5. Index Page Configuration

6. Additional Configuration

Summary

This configuration provides a robust interface for generating and downloading KYC verification reports within ActiveAdmin. It allows administrators to filter verifications by date range and status, and to export the results in either a detailed CSV format or a summarized PDF format. The use of custom controllers and Prawn for PDF generation ensures that the reports are both comprehensive and visually appealing.