Skip to content

Commit

Permalink
adding external Report creation
Browse files Browse the repository at this point in the history
  • Loading branch information
anderson-mj committed Jan 16, 2025
1 parent 03b456c commit a8469bc
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 12 deletions.
21 changes: 20 additions & 1 deletion app/controllers/reports_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# frozen_string_literal: true

class ReportsController < ApplicationController
include ReportsHelper

before_action :set_report, only: [:download, :download_by_identifier, :invalidate]
before_action :check_downloadable, only: [:download, :download_by_identifier]
authorize_resource
Expand All @@ -14,9 +16,17 @@ class ReportsController < ApplicationController
config.show.columns = [:user, :file_name, :identifier, :created_at, :expires_at, :invalidated_by, :invalidated_at]
config.update.columns = [:expires_at]
config.columns = config.list.columns
config.list.sorting = { created_at: :desc }
config.columns[:user].clear_link
config.columns[:expires_at_or_invalid].label = I18n.t("activerecord.attributes.report.expires_at")
config.actions.exclude :create, :delete
config.create.columns = [:file_name, :document_title, :document_body, :expiration_in_months]
config.columns.add :document_body, :expiration_in_months
config.columns[:document_body].required = true
config.columns[:expiration_in_months].label = I18n.t("activerecord.attributes.report_configuration.expiration_in_months")
config.columns[:expiration_in_months].description = I18n.t("active_scaffold.expiration_in_months_description")
config.columns[:expires_at].description = I18n.t("active_scaffold.expires_at_description")
config.create.label = :create_report_label
config.actions.exclude :delete
config.action_links.add "download",
label: "
<i title='#{I18n.t("active_scaffold.download_link")}'
Expand All @@ -39,6 +49,15 @@ class ReportsController < ApplicationController
ignore_method: :cant_download?
end

def before_create_save(record)
record.user = current_user
record.expires_at = Date.today + record.expiration_in_months.to_i.months if record.expiration_in_months.present?
document_data = { body: params[:record][:document_body], title: params[:record][:document_title], expiration_in_months: params[:record][:expiration_in_months] }
extension = File.extname(record.file_name)[1..-1]
record.file_name = record.file_name + ".pdf" unless extension == "pdf"
create_external_report_pdf(record, document_data)
end

def download
redirect_to download_path(medium_hash: @report.carrierwave_file.medium_hash)
end
Expand Down
23 changes: 14 additions & 9 deletions app/helpers/pdf_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -363,13 +363,18 @@ def new_document(name, title, options = {}, &block)
uploader = PdfUploader.new
uploader.store!({ base64_contents: Base64.encode64(document), filename: name })

Report.create!(
expires_at: pdf_config.expiration_in_months.present? ? Date.today + pdf_config.expiration_in_months.months : nil,
user: current_user,
carrierwave_file: uploader.file&.file,
file_name: name,
identifier: @qrcode_identifier
)
if options[:report].present?
@report.identifier = @qrcode_identifier
@report.carrierwave_file = uploader.file&.file
else
Report.create!(
expires_at: pdf_config.expiration_in_months.present? ? Date.today + pdf_config.expiration_in_months.months : nil,
user: current_user,
carrierwave_file: uploader.file&.file,
file_name: name,
identifier: @qrcode_identifier
)
end
end

document
Expand Down Expand Up @@ -511,8 +516,8 @@ def setup_pdf_config(pdf_type, options)
config = options[:pdf_config] ||
ReportConfiguration.where(pdf_type_property => true).order(order: :desc).first ||
ReportConfiguration.new
if options[:signature_override].present?
config.signature_type = options[:signature_override]
if options[:override].present?
config.assign_attributes(options[:override])
end

config
Expand Down
37 changes: 37 additions & 0 deletions app/helpers/reports_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# frozen_string_literal: true

module ReportsHelper
include PdfHelper

def create_external_report_pdf(report, document_data = {})
render_to_string(
template: "reports/external_report_pdf",
type: "application/pdf",
formats: [:pdf],
assigns: { report: report, title: document_data[:title], document_body: document_data[:body], expiration_in_months: document_data[:expiration_in_months] }
)
end

def report_body_text(pdf, document_body)
pdf.bounding_box([(pdf.bounds.width - 500) / 2, pdf.cursor], width: 500, height: pdf.bounds.height - 98) do
pdf.font("Times-Roman", size: 12) do
pdf.move_down 30

pdf.text document_body, align: :justify
end
end
end

def document_body_form_column(record, options)
text_area :record, :document_body, options.merge!(rows: 20, cols: 80)
end

def document_title_form_column(record, options)
text_field :record, :document_title, options.merge!(value: "DECLARAÇÃO")
end

def expiration_in_months_form_column(record, options)
merge_options = { min: 1, value: ReportConfiguration.where(use_at_assertion: true).order(order: :desc).first&.expiration_in_months }
number_field :record, :expiration_in_months, options.merge!(merge_options)
end
end
3 changes: 3 additions & 0 deletions app/models/report.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
# frozen_string_literal: true

class Report < ApplicationRecord
attr_accessor :document_title, :document_body, :expiration_in_months
belongs_to :user, foreign_key: "generated_by_id"
belongs_to :invalidated_by, foreign_key: "invalidated_by_id", class_name: "User", optional: true
belongs_to :carrierwave_file, foreign_key: "carrierwave_file_id", class_name: "CarrierWave::Storage::ActiveRecord::ActiveRecordFile", optional: true

validates :file_name, presence: true

def to_label
"#{self.user.name} - #{I18n.l(self.created_at, format: '%d/%m/%Y %H:%M')}"
end
Expand Down
2 changes: 1 addition & 1 deletion app/views/enrollments/academic_transcript_pdf.pdf.prawn
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ new_document(
:generate_report_without_watermark, @enrollment
),
pdf_type: :transcript,
signature_override: can?(:override_report_signature_type, @enrollment) ? @signature_override : nil
override: can?(:override_report_signature_type, @enrollment) ? { signature_type: @signature_override } : nil
) do |pdf|
enrollment_student_header(pdf, enrollment: @enrollment)

Expand Down
2 changes: 1 addition & 1 deletion app/views/enrollments/grades_report_pdf.pdf.prawn
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ new_document(
)
),
pdf_type: :grades_report,
signature_override: can?(:override_report_signature_type, @enrollment) ? @signature_override : nil
override: can?(:override_report_signature_type, @enrollment) ? { signature_type: @signature_override } : nil
) do |pdf|
enrollment_student_header(pdf, enrollment: @enrollment)

Expand Down
16 changes: 16 additions & 0 deletions app/views/reports/external_report_pdf.pdf.prawn
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Copyright (c) Universidade Federal Fluminense (UFF).
# This file is part of SAPOS. Please, consult the license terms in the LICENSE file.

# frozen_string_literal: true

require "prawn/measurement_extensions"

new_document(
@report.file_name,
@title,
override: { signature_type: :qr_code, expiration_in_months: @expiration_in_months },
pdf_type: :assertion,
report: @report
) do |pdf|
report_body_text(pdf, @document_body)
end
7 changes: 7 additions & 0 deletions config/locales/reports.pt-BR.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,15 @@ pt-BR:
invalidated: "Invalidado"
invalidated_at: "Data de Invalidação"
invalidated_by: "Invalidado por"
document_title: "Título do Documento"
document_body: "Corpo do Documento"

models:
report:
one: "Documento Assinado"
other: "Documentos Assinados"

active_scaffold:
create_report_label: "Gerar Documento Assinado"
expiration_in_months_description: "Caso vazio, o documento não terá data de expiração."
expires_at_description: "O texto da validade no documento não será alterado."

0 comments on commit a8469bc

Please sign in to comment.