Class: Pdf::Document::ReturnInstructions

Inherits:
BaseService
  • Object
show all
Includes:
Base
Defined in:
app/services/pdf/document/return_instructions.rb

Overview

Service object: return instructions.

Defined Under Namespace

Classes: Result

Constant Summary collapse

QR_GRID_COLUMNS =

Per-package paperless QR codes are laid out this many per row in the grid
(instead of one full page per box). Label Broker IDs are short, so the QR
is low-density and scans fine at the resulting size; drop this if a counter
scanner ever struggles.

5

Constants included from Base

Base::FONT, Base::NIMBUS_SANS_PATH, Base::NIMBUS_SANS_PATH_BOLD, Base::WY_LOGO_PATH

Instance Method Summary collapse

Instance Method Details

#call(rma, options = {}) ⇒ Result

Builds the customer-facing return-instructions PDF.

When the RMA's return shipments carry a paperless QR/barcode (attached
by Delivery#generate_labels from ShipEngine's paperless_download
response), the PDF interleaves QR pages alongside the printable-label
fallback so the customer can pick whichever works at the counter.

Three layouts (paperless_layout_for):

  • :per_package — every return shipment has its own QR. Page order:
    instructions → (QR page + printable label) per shipment.
  • :shipment_level — one QR covers all boxes (typically USPS Label
    Broker on an MPS). Page order: instructions → single QR page → all
    printable labels.
  • :none — no QR, today's flow unchanged.

Parameters:

  • rma (Rma)

    the RMA to generate return instructions for

  • options (Hash) (defaults to: {})

    Options hash

Options Hash (options):

  • :output_to_file (Boolean)

    write the PDF to a file

  • :output_to_file_path (String)

    path to write the PDF to

Returns:

  • (Result)

    the generated PDF result



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'app/services/pdf/document/return_instructions.rb', line 47

def call(rma, options = {})
  layout = paperless_layout_for(rma)

  combined_pdf = PdfCombinator.new
  combined_pdf << generate_instructions_pdf(rma, paperless_layout: layout)

  case layout
  when :per_package
    # Paperless (USPS Label Broker): QR only. A paperless label has no
    # separate printable shipping label — `label_download` is the QR page
    # itself. Lay all the box QR codes out in one compact grid (one per box,
    # labeled Box N) rather than a full page each.
    qr_shipments = rma.return_shipments.select(&:paperless_qr_png)
    combined_pdf << generate_paperless_qr_grid(rma, qr_shipments) if qr_shipments.any?
  when :shipment_level
    qr_shipment = rma.return_shipments.detect(&:paperless_qr_png)
    combined_pdf << generate_paperless_qr_page(rma, qr_shipment, scope: :shipment_level)
  else # :none
    rma.return_labels.each do |upload|
      combined_pdf << PdfCombinator.load(upload.attachment.path)
    end
  end

  result_hsh = { rma: rma, options: options, file_name: rma.instructions_file_name }
  if options[:output_to_file]
    path = options[:output_to_file_path] ||
           File.join(Rails.application.config.x.temp_storage_path.to_s, result_hsh[:file_name])
    combined_pdf.save(path)
    result_hsh[:pdf_file_path] = path
  else
    result_hsh[:pdf_data] = combined_pdf.to_pdf
  end
  Result.new(**result_hsh)
end