Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions app/controllers/api/v1/pdf_reports_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ def event_procedures_pdf
relation: event_procedures,
amount: total_amount_cents,
entity_name: permitted_query_params[:entity_name],
email: current_user.email
email: current_user.email,
hide_values: hide_values?
).generate_pdf
end

Expand All @@ -58,10 +59,15 @@ def medical_shifts_pdf
relation: medical_shifts,
amount: total_amount_cents,
entity_name: permitted_query_params[:entity_name],
email: current_user.email
email: current_user.email,
hide_values: hide_values?
).generate_pdf
end

def hide_values?
permitted_query_params[:hide_values] == "true"
end

def permitted_query_params
params.permit(
:page,
Expand All @@ -71,6 +77,8 @@ def permitted_query_params
:paid,
:entity_name,
:disposition,
:hide_values,
ids: [],
hospital: [:name],
health_insurance: [:name]
).to_h
Expand Down
5 changes: 5 additions & 0 deletions app/operations/event_procedures/list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ def apply_all_filters(query)
query = apply_year_filter(query)
query = apply_hospital_filter(query)
query = apply_health_insurance_filter(query)
query = apply_ids_filter(query)
apply_paid_filter(query)
end

Expand All @@ -58,5 +59,9 @@ def apply_health_insurance_filter(query)
query
end
end

def apply_ids_filter(query)
params[:ids].present? ? query.where(id: params[:ids]) : query
end
end
end
5 changes: 5 additions & 0 deletions app/operations/medical_shifts/list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ def filtered_query
query = apply_month_filter(query)
query = apply_year_filter(query)
query = apply_hospital_filter(query)
query = apply_ids_filter(query)
apply_paid_filter(query)
end

Expand All @@ -36,5 +37,9 @@ def apply_hospital_filter(query)
def apply_paid_filter(query)
%w[true false].include?(params[:paid]) ? query.by_paid(paid: params[:paid]) : query
end

def apply_ids_filter(query)
params[:ids].present? ? query.where(id: params[:ids]) : query
end
end
end
9 changes: 6 additions & 3 deletions app/pdfs/event_procedures_report_pdf.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
# frozen_string_literal: true

class EventProceduresReportPdf
attr_reader :pdf, :items, :amount, :title, :email
attr_reader :pdf, :items, :amount, :title, :email, :hide_values

def initialize(pdf:, items:, amount:, title:, email:)
def initialize(pdf:, items:, amount:, title:, email:, hide_values: false) # rubocop:disable Metrics/ParameterLists
@pdf = pdf
@items = items
@amount = amount
@title = title
@email = email
@hide_values = hide_values
@header_footer_height = 100
@line_spacing = 15
@text_box_padding = 10
Expand All @@ -31,7 +32,7 @@ def add_header

def add_footer
pdf.go_to_page(pdf.page_count)
FooterPdf.new(pdf: pdf, amount: amount).generate
FooterPdf.new(pdf: pdf, amount: amount, hide_values: hide_values).generate
end

def add_body
Expand Down Expand Up @@ -84,6 +85,8 @@ def item_date(item)
end

def item_amount(item)
return "" if hide_values

item.total_amount.format(thousands_separator: ".", decimal_mark: ",")
end
end
22 changes: 16 additions & 6 deletions app/pdfs/footer_pdf.rb
Original file line number Diff line number Diff line change
@@ -1,37 +1,47 @@
# frozen_string_literal: true

class FooterPdf
attr_reader :pdf, :amount
attr_reader :pdf, :amount, :hide_values

def initialize(pdf:, amount:)
def initialize(pdf:, amount:, hide_values: false)
@pdf = pdf
@amount = amount
@hide_values = hide_values
@footer_height = 50
@footer_spacing = 15
@footer_font_size = 10
@right_text_offset = 100
end

def generate
def generate # rubocop:disable Metrics/MethodLength
pdf.bounding_box([0, 30], width: @pdf.bounds.width, height: @footer_height) do
pdf.stroke_color "000000"
pdf.stroke_horizontal_line(0, pdf.bounds.width, at: 65)

pdf.text_box "Total", at: [0, pdf.cursor], size: @footer_font_size, align: :left, overflow: :shrink_to_fit
pdf.text_box amount.total, at: [pdf.bounds.width - @right_text_offset, pdf.cursor], size: @footer_font_size,
pdf.text_box(
hide_values ? "" : amount.total,
at: [pdf.bounds.width - @right_text_offset, pdf.cursor], size: @footer_font_size,
align: :right, overflow: :shrink_to_fit
)

pdf.move_down @footer_spacing

pdf.text_box "A Receber", at: [0, pdf.cursor], size: @footer_font_size, align: :left
pdf.text_box amount.unpaid, at: [pdf.bounds.width - @right_text_offset, pdf.cursor], size: @footer_font_size,
pdf.text_box(
hide_values ? "" : amount.unpaid,
at: [pdf.bounds.width - @right_text_offset, pdf.cursor], size: @footer_font_size,
align: :right, overflow: :shrink_to_fit
)

pdf.move_down @footer_spacing

pdf.text_box "Recebidos", at: [0, pdf.cursor], size: @footer_font_size, align: :left, overflow: :shrink_to_fit
pdf.text_box amount.paid, at: [pdf.bounds.width - @right_text_offset, pdf.cursor], size: @footer_font_size,
pdf.text_box(
hide_values ? "" : amount.paid,
at: [pdf.bounds.width - @right_text_offset, pdf.cursor], size: @footer_font_size,
align: :right, overflow: :shrink_to_fit
)
end
end
end
9 changes: 6 additions & 3 deletions app/pdfs/medical_shifts_report_pdf.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
# frozen_string_literal: true

class MedicalShiftsReportPdf
attr_reader :pdf, :items, :amount, :title, :email
attr_reader :pdf, :items, :amount, :title, :email, :hide_values

def initialize(pdf:, items:, amount:, title:, email:)
def initialize(pdf:, items:, amount:, title:, email:, hide_values: false) # rubocop:disable Metrics/ParameterLists
@pdf = pdf
@items = items
@amount = amount
@title = title
@email = email
@hide_values = hide_values
@header_footer_height = 100
@line_spacing = 15
@text_box_padding = 10
Expand All @@ -31,7 +32,7 @@ def add_header

def add_footer
pdf.repeat(:all, dynamic: true) do
FooterPdf.new(pdf: pdf, amount: amount).generate
FooterPdf.new(pdf: pdf, amount: amount, hide_values: hide_values).generate
end
end

Expand Down Expand Up @@ -89,6 +90,8 @@ def item_paid?(item)
end

def item_amount(item)
return "" if hide_values

item.amount.format(thousands_separator: ".", decimal_mark: ",")
end
end
11 changes: 7 additions & 4 deletions app/services/pdf_generator_service.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
# frozen_string_literal: true

class PdfGeneratorService
attr_reader :relation, :amount, :entity_name, :email
attr_reader :relation, :amount, :entity_name, :email, :hide_values

def initialize(relation:, amount:, entity_name:, email:)
def initialize(relation:, amount:, entity_name:, email:, hide_values: false)
@relation = relation
@amount = amount
@entity_name = entity_name
@email = email
@hide_values = hide_values
end

def generate_pdf
Expand All @@ -19,14 +20,16 @@ def generate_pdf
def generate_event_procedures_pdf
Prawn::Document.new do |pdf|
EventProceduresReportPdf.new(
pdf: pdf, items: relation, amount: amount, title: "Procedimentos", email: email
pdf: pdf, items: relation, amount: amount, title: "Procedimentos", email: email, hide_values: hide_values
).generate
end
end

def generate_medical_shifts_pdf
Prawn::Document.new do |pdf|
MedicalShiftsReportPdf.new(pdf: pdf, items: relation, amount: amount, title: "Plantões", email: email).generate
MedicalShiftsReportPdf.new(
pdf: pdf, items: relation, amount: amount, title: "Plantões", email: email, hide_values: hide_values
).generate
end
end
end
29 changes: 25 additions & 4 deletions spec/pdfs/event_procedures_report_pdf_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
require "rails_helper"

RSpec.describe EventProceduresReportPdf, type: :pdf do
let(:user) { create(:user) }
let(:patient) { create(:patient, user: user) }
let(:event_procedures) { create_list(:event_procedure, 3, user_id: user.id, patient: patient) }
let(:amount) { EventProcedures::TotalAmountCents.call(event_procedures: event_procedures) }

it "generates a report with the correct content" do
user = create(:user)
patient = create(:patient, user: user)
pdf = Prawn::Document.new
event_procedures = create_list(:event_procedure, 3, user_id: user.id, patient: patient)
amount = EventProcedures::TotalAmountCents.call(event_procedures: event_procedures)

described_class.new(
pdf: pdf, amount: amount, items: event_procedures, title: "Procedimentos", email: user.email
Expand All @@ -25,4 +26,24 @@
)
end
end

context "when hide_values is true" do
it "does not include monetary values in the report" do
pdf = Prawn::Document.new

described_class.new(
pdf: pdf, amount: amount, items: event_procedures, title: "Procedimentos", email: user.email,
hide_values: true
).generate
rendered_pdf = pdf.render
text_analysis = PDF::Inspector::Text.analyze(rendered_pdf)

event_procedures.each do |event_procedure|
expect(text_analysis.strings).not_to include(
event_procedure.total_amount.format(thousands_separator: ".", decimal_mark: ",")
)
end
expect(text_analysis.strings).not_to include(amount.total, amount.paid, amount.unpaid)
end
end
end
26 changes: 23 additions & 3 deletions spec/pdfs/medical_shifts_report_pdf_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
require "rails_helper"

RSpec.describe MedicalShiftsReportPdf, type: :pdf do
let(:user) { create(:user) }
let(:medical_shifts) { create_list(:medical_shift, 3, user_id: user.id) }
let(:amount) { MedicalShifts::TotalAmountCents.call(medical_shifts: medical_shifts) }

it "generates a report with the correct content" do
user = create(:user)
pdf = Prawn::Document.new
medical_shifts = create_list(:medical_shift, 20, user_id: user.id)
amount = MedicalShifts::TotalAmountCents.call(medical_shifts: medical_shifts)

described_class.new(pdf: pdf, amount: amount, items: medical_shifts, title: "Plantões", email: user.email).generate
rendered_pdf = pdf.render
Expand All @@ -21,4 +22,23 @@
)
end
end

context "when hide_values is true" do
it "does not include monetary values in the report" do
pdf = Prawn::Document.new

described_class.new(
pdf: pdf, amount: amount, items: medical_shifts, title: "Plantões", email: user.email, hide_values: true
).generate
rendered_pdf = pdf.render
text_analysis = PDF::Inspector::Text.analyze(rendered_pdf)

medical_shifts.each do |medical_shift|
expect(text_analysis.strings).not_to include(
medical_shift.amount.format(thousands_separator: ".", decimal_mark: ",")
)
end
expect(text_analysis.strings).not_to include(amount.total, amount.paid, amount.unpaid)
end
end
end
38 changes: 38 additions & 0 deletions spec/requests/api/v1/pdf_reports_request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,43 @@
)
end
end

context "with hide_values param" do
it "returns a PDF for event_procedures" do
create(:event_procedure, user: user)
get path, params: { entity_name: "event_procedures", hide_values: "true" }, headers: headers

expect(response).to have_http_status(:ok)
expect(response.content_type).to eq("application/pdf")
end

it "returns a PDF for medical_shifts" do
create(:medical_shift, user: user)
get path, params: { entity_name: "medical_shifts", hide_values: "true" }, headers: headers

expect(response).to have_http_status(:ok)
expect(response.content_type).to eq("application/pdf")
end
end

context "with ids param" do
it "returns PDF with only selected event_procedures" do
procedures = create_list(:event_procedure, 3, user: user)
selected = procedures.first(2)
get path, params: { entity_name: "event_procedures", ids: selected.map(&:id) }, headers: headers

expect(response).to have_http_status(:ok)
expect(response.content_type).to eq("application/pdf")
end

it "returns PDF with only selected medical_shifts" do
shifts = create_list(:medical_shift, 3, user: user)
selected = shifts.first(2)
get path, params: { entity_name: "medical_shifts", ids: selected.map(&:id) }, headers: headers

expect(response).to have_http_status(:ok)
expect(response.content_type).to eq("application/pdf")
end
end
end
end
Loading