diff --git a/app/controllers/api/v1/pdf_reports_controller.rb b/app/controllers/api/v1/pdf_reports_controller.rb index d9aa534..34669d5 100644 --- a/app/controllers/api/v1/pdf_reports_controller.rb +++ b/app/controllers/api/v1/pdf_reports_controller.rb @@ -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 @@ -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, @@ -71,6 +77,8 @@ def permitted_query_params :paid, :entity_name, :disposition, + :hide_values, + ids: [], hospital: [:name], health_insurance: [:name] ).to_h diff --git a/app/operations/event_procedures/list.rb b/app/operations/event_procedures/list.rb index 5766b22..81755e5 100644 --- a/app/operations/event_procedures/list.rb +++ b/app/operations/event_procedures/list.rb @@ -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 @@ -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 diff --git a/app/operations/medical_shifts/list.rb b/app/operations/medical_shifts/list.rb index d0b0378..79b0a5e 100644 --- a/app/operations/medical_shifts/list.rb +++ b/app/operations/medical_shifts/list.rb @@ -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 @@ -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 diff --git a/app/pdfs/event_procedures_report_pdf.rb b/app/pdfs/event_procedures_report_pdf.rb index 74401a7..1ae01a7 100644 --- a/app/pdfs/event_procedures_report_pdf.rb +++ b/app/pdfs/event_procedures_report_pdf.rb @@ -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 @@ -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 @@ -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 diff --git a/app/pdfs/footer_pdf.rb b/app/pdfs/footer_pdf.rb index 0f95ff2..4332a8f 100644 --- a/app/pdfs/footer_pdf.rb +++ b/app/pdfs/footer_pdf.rb @@ -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 diff --git a/app/pdfs/medical_shifts_report_pdf.rb b/app/pdfs/medical_shifts_report_pdf.rb index 550ba68..f35a0ee 100644 --- a/app/pdfs/medical_shifts_report_pdf.rb +++ b/app/pdfs/medical_shifts_report_pdf.rb @@ -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 @@ -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 @@ -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 diff --git a/app/services/pdf_generator_service.rb b/app/services/pdf_generator_service.rb index 856cdb1..aaf81a2 100644 --- a/app/services/pdf_generator_service.rb +++ b/app/services/pdf_generator_service.rb @@ -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 @@ -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 diff --git a/spec/pdfs/event_procedures_report_pdf_spec.rb b/spec/pdfs/event_procedures_report_pdf_spec.rb index 2af2a94..b7dcbd2 100644 --- a/spec/pdfs/event_procedures_report_pdf_spec.rb +++ b/spec/pdfs/event_procedures_report_pdf_spec.rb @@ -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 @@ -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 diff --git a/spec/pdfs/medical_shifts_report_pdf_spec.rb b/spec/pdfs/medical_shifts_report_pdf_spec.rb index 38e489f..1a04493 100644 --- a/spec/pdfs/medical_shifts_report_pdf_spec.rb +++ b/spec/pdfs/medical_shifts_report_pdf_spec.rb @@ -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 @@ -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 diff --git a/spec/requests/api/v1/pdf_reports_request_spec.rb b/spec/requests/api/v1/pdf_reports_request_spec.rb index c856665..9727fad 100644 --- a/spec/requests/api/v1/pdf_reports_request_spec.rb +++ b/spec/requests/api/v1/pdf_reports_request_spec.rb @@ -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 diff --git a/spec/services/pdf_generator_service_spec.rb b/spec/services/pdf_generator_service_spec.rb index 5ccebd1..f8a4d8c 100644 --- a/spec/services/pdf_generator_service_spec.rb +++ b/spec/services/pdf_generator_service_spec.rb @@ -22,6 +22,22 @@ expect(text_analysis.strings).to include(event_procedure.procedure.name) end end + + context "when hide_values is true" do + it "generates pdf without monetary values" do + user = create(:user) + event_procedures = create_list(:event_procedure, 3) + amount = EventProcedures::TotalAmountCents.call(event_procedures: event_procedures) + pdf = described_class.new( + relation: event_procedures, amount: amount, entity_name: "event_procedures", + email: user.email, hide_values: true + ).generate_pdf + + text_analysis = PDF::Inspector::Text.analyze(pdf.render) + + expect(text_analysis.strings).not_to include(amount.total, amount.paid, amount.unpaid) + end + end end context "when entity_name is medical_shifts" do @@ -42,6 +58,22 @@ expect(text_analysis.strings).to include(medical_shift.hospital_name) end end + + context "when hide_values is true" do + it "generates pdf without monetary values" do + user = create(:user) + medical_shifts = create_list(:medical_shift, 3, user: user) + amount = MedicalShifts::TotalAmountCents.call(medical_shifts: medical_shifts) + pdf = described_class.new( + relation: medical_shifts, amount: amount, entity_name: "medical_shifts", + email: user.email, hide_values: true + ).generate_pdf + + text_analysis = PDF::Inspector::Text.analyze(pdf.render) + + expect(text_analysis.strings).not_to include(amount.total, amount.paid, amount.unpaid) + end + end end end end