diff --git a/app/controllers/estimate_controller.rb b/app/controllers/estimate_controller.rb index 5bee5db..9ef7710 100644 --- a/app/controllers/estimate_controller.rb +++ b/app/controllers/estimate_controller.rb @@ -65,7 +65,7 @@ class EstimateController < ApplicationController # Renders the estimate PDF or redirects with an error if rendering fails. def render_pdf(estimate) - pdf, ref = EstimatePdfService.new(qbo: QboConnectionService.current!).fetch_pdf(doc_ids: [estimate.id]) + pdf, ref = PdfService.new(entity: Estimate).fetch_pdf(doc_ids: [estimate.id]) send_data( pdf, filename: "estimate #{ref}.pdf", disposition: :inline, type: "application/pdf" ) rescue StandardError => e log "PDF render failed for Estimate #{estimate&.id}: #{e.message}" diff --git a/app/controllers/invoice_controller.rb b/app/controllers/invoice_controller.rb index a59b581..8239dad 100644 --- a/app/controllers/invoice_controller.rb +++ b/app/controllers/invoice_controller.rb @@ -18,7 +18,7 @@ class InvoiceController < ApplicationController log "Processing request for #{request.original_url}" invoice_ids = Array(params[:invoice_ids] || params[:id]) - pdf, ref = InvoicePdfService.new(qbo: QboConnectionService.current!).fetch_pdf(doc_ids: invoice_ids) + pdf, ref = PdfService.new(entity: Invoice).fetch_pdf(doc_ids: invoice_ids) send_data pdf, filename: "invoice #{ref}.pdf", disposition: :inline, type: "application/pdf" diff --git a/app/services/estimate_pdf_service.rb b/app/services/estimate_pdf_service.rb deleted file mode 100644 index dced8b8..0000000 --- a/app/services/estimate_pdf_service.rb +++ /dev/null @@ -1,16 +0,0 @@ -#The MIT License (MIT) -# -#Copyright (c) 2016 - 2026 rick barrette -# -#Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: -# -#The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. -# -#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -class EstimatePdfService < PdfServiceBase - - def self.model_class - Estimate - end - -end \ No newline at end of file diff --git a/app/services/invoice_pdf_service.rb b/app/services/invoice_pdf_service.rb deleted file mode 100644 index 94259ad..0000000 --- a/app/services/invoice_pdf_service.rb +++ /dev/null @@ -1,16 +0,0 @@ -#The MIT License (MIT) -# -#Copyright (c) 2016 - 2026 rick barrette -# -#Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: -# -#The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. -# -#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -class InvoicePdfService < PdfServiceBase - - def self.model_class - Invoice - end - -end \ No newline at end of file diff --git a/app/services/pdf_service_base.rb b/app/services/pdf_service.rb similarity index 90% rename from app/services/pdf_service_base.rb rename to app/services/pdf_service.rb index 4635a0d..f0aa840 100644 --- a/app/services/pdf_service_base.rb +++ b/app/services/pdf_service.rb @@ -7,19 +7,16 @@ #The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. # #THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -class PdfServiceBase +class PdfService require 'combine_pdf' # Subclasses should initialize with a QBO client instance - def initialize(qbo:) - @qbo = qbo - @entity = self.class.model_class - end - - # Subclasses must implement this to specify which document model to download pdf (e.g. Estimate, Invoice) - def self.model_class - raise NotImplementedError + def initialize(entity: entity) + raise "An entity to sync is required" unless entity + @entity = entity + @qbo = QboConnectionService.current! + raise "No QBO configuration found" unless @qbo end # Fetches the PDF for the given entity IDs. If multiple IDs are provided, their PDFs are combined into a single document. diff --git a/lib/redmine_qbo/patches/pdf_patch.rb b/lib/redmine_qbo/patches/pdf_patch.rb index 8566e93..b0074b2 100644 --- a/lib/redmine_qbo/patches/pdf_patch.rb +++ b/lib/redmine_qbo/patches/pdf_patch.rb @@ -260,7 +260,7 @@ module RedmineQbo # Check to see if there is an estimate attached, then combine them if issue.estimate - e_pdf, ref = EstimatePdfService.new(qbo: QboConnectionService.current!).fetch_pdf(doc_ids: [issue.estimate.id]) + e_pdf, ref = PdfService.new(entity: Estimate).fetch_pdf(doc_ids: [issue.estimate.id]) pdf = CombinePDF.parse(pdf.output, allow_optional_content: true) pdf << CombinePDF.parse(e_pdf) return pdf.to_pdf