Refactor: Introduce PdfServiceBase for shared PDF fetching functionality and update Invoice/Estimate controllers to utilize new services

This commit is contained in:
2026-02-28 21:59:01 -05:00
parent a34ae46358
commit f921f227e2
5 changed files with 90 additions and 49 deletions

View File

@@ -65,7 +65,8 @@ class EstimateController < ApplicationController
# Renders the estimate PDF or redirects with an error if rendering fails.
def render_pdf(estimate)
send_data( estimate.pdf, filename: "estimate #{estimate.doc_number}.pdf", disposition: :inline, type: "application/pdf" )
pdf, ref = EstimatePdfService.new(qbo: Qbo.first).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}"
redirect_back fallback_location: root_path, flash: { error: I18n.t(:notice_estimate_not_found) }

View File

@@ -13,11 +13,12 @@ class InvoiceController < ApplicationController
before_action :require_user, unless: -> { session[:token].nil? }
skip_before_action :verify_authenticity_token, :check_if_login_required, unless: -> { session[:token].nil? }
# Displays the invoice PDF in the browser or redirects with an error if not found.
def show
log "Processing request for #{request.original_url}"
invoice_ids = Array(params[:invoice_ids] || params[:id])
pdf, ref = InvoicePdfService.new(qbo: Qbo.first) .fetch_pdf(invoice_ids: invoice_ids)
pdf, ref = InvoicePdfService.new(qbo: Qbo.first).fetch_pdf(doc_ids: invoice_ids)
send_data pdf, filename: "invoice #{ref}.pdf", disposition: :inline, type: "application/pdf"
@@ -28,6 +29,7 @@ class InvoiceController < ApplicationController
private
# Logs messages with a consistent prefix for easier debugging.
def log(msg)
Rails.logger.info "[InvoiceController] #{msg}"
end