generated from espoo-dev/rails_boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 1
refactor: endpoint for pdf generation #231
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| class EventProceduresReportPdf | ||
| attr_reader :pdf, :items, :amount, :title | ||
|
|
||
| def initialize(pdf:, items:, amount:, title:) | ||
| @pdf = pdf | ||
| @items = items | ||
| @amount = amount | ||
| @title = title | ||
| end | ||
|
|
||
| def generate | ||
| add_header | ||
| add_body | ||
| add_footer | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def add_header | ||
| pdf.repeat(:all) do | ||
| HeaderPdf.new(pdf: pdf, title: title).generate | ||
| end | ||
| end | ||
|
|
||
| def add_footer | ||
| pdf.repeat(:all, dynamic: true) do | ||
| FooterPdf.new(pdf: pdf, amount: amount).generate | ||
| end | ||
| end | ||
|
|
||
| def add_body | ||
| pdf.move_down 10 | ||
|
|
||
| items.each do |item| | ||
| start_new_page_if_needed | ||
| pdf.stroke_horizontal_rule | ||
| add_item_details(item) | ||
| pdf.move_down 10 | ||
| end | ||
| end | ||
|
|
||
| def start_new_page_if_needed | ||
| return unless pdf.cursor < 100 | ||
|
|
||
| pdf.start_new_page | ||
| add_header | ||
| end | ||
|
|
||
| def add_item_details(item) | ||
| add_item_line(truncate_text(item.patient.name), item_paid?(item)) | ||
| add_item_line(truncate_text(item.procedure.name), item.procedure.amount.format) | ||
| add_item_line(truncate_text(item.health_insurance.name), item_date(item)) | ||
| end | ||
|
|
||
| def add_item_line(left_text, right_text) | ||
| pdf.bounding_box([0, pdf.cursor], width: pdf.bounds.width) do | ||
| pdf.text_box left_text, at: [3, pdf.cursor - 10], size: 10, align: :left | ||
| pdf.text_box right_text, at: [pdf.bounds.width - 110, pdf.cursor - 10], size: 10, align: :right | ||
| end | ||
| pdf.move_down 15 | ||
| end | ||
|
|
||
| def truncate_text(text, length = 35) | ||
| text.length > length ? "#..." : text | ||
| end | ||
|
|
||
| def item_paid?(item) | ||
| item.payd ? "Pago" : "A Receber" | ||
| end | ||
|
|
||
| def item_date(item) | ||
| item.date.strftime("%d/%m/%Y") | ||
| end | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| class FooterPdf | ||
| attr_reader :pdf, :amount | ||
|
|
||
| def initialize(pdf:, amount:) | ||
| @pdf = pdf | ||
| @amount = amount | ||
| end | ||
|
|
||
| def generate | ||
| pdf.bounding_box([0, 30], width: @pdf.bounds.width, height: 50) do | ||
| pdf.stroke_color "000000" | ||
| pdf.stroke_horizontal_line(0, pdf.bounds.width, at: 65) | ||
|
|
||
| pdf.text_box "Total", at: [0, pdf.cursor], size: 10, align: :left | ||
| pdf.text_box amount.total, at: [pdf.bounds.width - 50, pdf.cursor], size: 10, align: :right | ||
|
|
||
| pdf.move_down 15 | ||
|
|
||
| pdf.text_box "A Receber", at: [0, pdf.cursor], size: 10, align: :left | ||
| pdf.text_box amount.unpaid, at: [pdf.bounds.width - 50, pdf.cursor], size: 10, align: :right | ||
|
|
||
| pdf.move_down 15 | ||
|
|
||
| pdf.text_box "Recebidos", at: [0, pdf.cursor], size: 10, align: :left | ||
| pdf.text_box amount.payd, at: [pdf.bounds.width - 50, pdf.cursor], size: 10, align: :right | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| class HeaderPdf | ||
| attr_reader :pdf, :title | ||
|
|
||
| def initialize(pdf:, title:) | ||
| @pdf = pdf | ||
| @title = title | ||
| end | ||
|
|
||
| def generate | ||
| pdf.text "Data: #{Time.zone.now.strftime('%d/%m/%Y')}", align: :right, size: 12 | ||
| pdf.move_down 20 | ||
| pdf.text title, align: :center, size: 20, style: :bold | ||
| pdf.move_down 20 | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| class MedicalShiftsReportPdf | ||
| attr_reader :pdf, :items, :amount, :title | ||
|
|
||
| def initialize(pdf:, items:, amount:, title:) | ||
| @pdf = pdf | ||
| @items = items | ||
| @amount = amount | ||
| @title = title | ||
| end | ||
|
|
||
| def generate | ||
| add_header | ||
| add_body | ||
| add_footer | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def add_header | ||
| pdf.repeat(:all) do | ||
| HeaderPdf.new(pdf: pdf, title: title).generate | ||
| end | ||
| end | ||
|
|
||
| def add_footer | ||
| pdf.repeat(:all, dynamic: true) do | ||
| FooterPdf.new(pdf: pdf, amount: amount).generate | ||
| end | ||
| end | ||
|
|
||
| def add_body | ||
| pdf.move_down 10 | ||
|
|
||
| items.each do |item| | ||
| start_new_page_if_needed | ||
| pdf.stroke_horizontal_rule | ||
| add_item_details(item) | ||
| pdf.move_down 10 | ||
| end | ||
| end | ||
|
|
||
| def start_new_page_if_needed | ||
| return unless pdf.cursor < 100 | ||
|
|
||
| pdf.start_new_page | ||
| add_header | ||
| end | ||
|
|
||
| def add_item_details(item) | ||
| add_item_line(truncate_text(item.hospital_name), item_start_date(item)) | ||
| add_item_line(item_workload(item), item.amount.format) | ||
| add_item_line(item_start_hour(item), item_paid?(item)) | ||
| end | ||
|
|
||
| def add_item_line(left_text, right_text) | ||
| pdf.bounding_box([0, pdf.cursor], width: pdf.bounds.width) do | ||
| pdf.text_box left_text, at: [3, pdf.cursor - 10], size: 10, align: :left | ||
| pdf.text_box right_text, at: [pdf.bounds.width - 110, pdf.cursor - 10], size: 10, align: :right | ||
| end | ||
| pdf.move_down 15 | ||
| end | ||
|
|
||
| def truncate_text(text, length = 35) | ||
| text.length > length ? "#..." : text | ||
| end | ||
|
|
||
| def item_shift(item) | ||
| item.shift == "Daytime" ? "Diurno" : "Noturno" | ||
| end | ||
|
|
||
| def item_workload(item) | ||
| "#{item_shift(item)} - #{item.workload_humanize}" | ||
| end | ||
|
|
||
| def item_start_date(item) | ||
| item.start_date.strftime("%d/%m/%Y") | ||
| end | ||
|
|
||
| def item_start_hour(item) | ||
| "Início: #{item.start_hour.strftime('%H:%M')}" | ||
| end | ||
|
|
||
| def item_paid?(item) | ||
| item.payd ? "Pago" : "A Receber" | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| class PdfGeneratorService | ||
| attr_reader :relation, :amount, :entity_name | ||
|
|
||
| def initialize(relation:, amount:, entity_name:) | ||
| @relation = relation | ||
| @amount = amount | ||
| @entity_name = entity_name | ||
| end | ||
|
|
||
| def generate_pdf | ||
| entity_name == "event_procedures" ? generate_event_procedures_pdf : generate_medical_shifts_pdf | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def generate_event_procedures_pdf | ||
| Prawn::Document.new do |pdf| | ||
| EventProceduresReportPdf.new(pdf: pdf, items: relation, amount: amount, title: "Procedimentos").generate | ||
| end | ||
| end | ||
|
|
||
| def generate_medical_shifts_pdf | ||
| Prawn::Document.new do |pdf| | ||
| MedicalShiftsReportPdf.new(pdf: pdf, items: relation, amount: amount, title: "Plantões").generate | ||
| end | ||
| end | ||
| end |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.