Refactor: Simplify invoice PDF fetching and introduce InvoicePdfService for better organization

This commit is contained in:
2026-02-28 21:36:16 -05:00
parent e4cfb0674e
commit a34ae46358
2 changed files with 73 additions and 39 deletions

View File

@@ -8,48 +8,22 @@
# #
#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. #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 InvoiceController < ApplicationController class InvoiceController < ApplicationController
include AuthHelper include AuthHelper
require 'combine_pdf'
before_action :require_user, unless: proc {|c| session[:token].nil? } before_action :require_user, unless: -> { session[:token].nil? }
skip_before_action :verify_authenticity_token, :check_if_login_required, unless: proc {|c| session[:token].nil? } skip_before_action :verify_authenticity_token, :check_if_login_required, unless: -> { session[:token].nil? }
#
# Downloads and forwards the invoice pdf
#
def show def show
log "Processing request for URL: #{request.original_url}" log "Processing request for #{request.original_url}"
begin
qbo = Qbo.first
qbo.perform_authenticated_request do |access_token|
service = Quickbooks::Service::Invoice.new(company_id: qbo.realm_id, access_token: access_token)
# If multiple id's then pull each pdf & combine them invoice_ids = Array(params[:invoice_ids] || params[:id])
if params[:invoice_ids] pdf, ref = InvoicePdfService.new(qbo: Qbo.first) .fetch_pdf(invoice_ids: invoice_ids)
log "Grabbing pdfs for " + params[:invoice_ids].join(', ')
ref = ""
params[:invoice_ids].each do |i|
log "processing " + i
invoice = service.fetch_by_id(i)
ref += " #{invoice.doc_number}"
@pdf << CombinePDF.parse(service.pdf(invoice)) unless @pdf.nil?
if @pdf.nil?
@pdf = CombinePDF.parse(service.pdf(invoice))
end
end
@pdf = @pdf.to_pdf
else
invoice = service.fetch_by_id(params[:id])
@pdf = service.pdf(invoice)
ref = invoice.doc_number
end
send_data @pdf, filename: "invoice #{ref}.pdf", disposition: :inline, type: "application/pdf" send_data pdf, filename: "invoice #{ref}.pdf", disposition: :inline, type: "application/pdf"
end
rescue rescue StandardError => e
redirect_to :back, flash: { error: I18n.t(:notice_invoice_not_found) } log "Invoice PDF failure: #{e.message}"
end redirect_back fallback_location: root_path, flash: { error: I18n.t(:notice_invoice_not_found) }
end end
private private

View File

@@ -0,0 +1,60 @@
#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
require 'combine_pdf'
def initialize(qbo:)
@qbo = qbo
end
# Fetches the PDF for the given invoice IDs. If multiple IDs are provided, their PDFs are combined into a single document.
def fetch_pdf(invoice_ids:)
log "Fetching PDFs for invoice IDs: #{invoice_ids.join(', ')}"
@qbo.perform_authenticated_request do |access_token|
service = Quickbooks::Service::Invoice.new(
company_id: @qbo.realm_id,
access_token: access_token
)
return single_pdf(service, invoice_ids.first) if invoice_ids.size == 1
combined_pdf(service, invoice_ids)
end
end
private
# Fetches a single PDF for the given invoice ID.
def single_pdf(service, id)
log "Fetching PDF for invoice ID: #{id}"
invoice = service.fetch_by_id(id)
[service.pdf(invoice), invoice.doc_number]
end
# Combines PDFs for multiple invoice IDs into a single PDF document and returns it along with a reference string.
def combined_pdf(service, ids)
log "Combining PDFs for invoice IDs: #{ids.join(', ')}"
pdf = CombinePDF.new
ref = []
ids.each do |id|
invoice = service.fetch_by_id(id)
ref << invoice.doc_number
pdf << CombinePDF.parse(service.pdf(invoice))
end
[pdf.to_pdf, ref.join(" ")]
end
def log(msg)
Rails.logger.info "[InvoicePdfService] #{msg}"
end
end