Class: CartonLabelWorker

Inherits:
Object
  • Object
show all
Includes:
Sidekiq::Job, Workers::StatusBroadcastable
Defined in:
app/workers/carton_label_worker.rb

Instance Attribute Summary

Attributes included from Workers::StatusBroadcastable

#broadcast_status_updates

Instance Method Summary collapse

Methods included from Workers::StatusBroadcastable::Overrides

#at, #store, #total

Instance Method Details

#perform(options = {}) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'app/workers/carton_label_worker.rb', line 8

def perform(options={})
  options = options.with_indifferent_access
  # Load up our shipments
  shipment_ids = options['shipment_ids'].presence || []
  shipments = Shipment.where(id: shipment_ids)
  return_existing = options[:return_existing].to_b
  print_profile_id = options['print_profile_id']
  print_profile_id = nil if print_profile_id == 'pdf'

  # If we don't pass a print profile to send the printouts to, we'll generate a pdf
  if print_profile_id
    print_profile = PrintProfile.find(print_profile_id)
  else
    pdfs = [] # Will contain upload objects
  end

  # How many and set current position
  total_shipments = shipments.size
  total total_shipments

  num_labels = 0

  shipments.each do |shipment|
    num_labels += 1
    # Generate the sscc label and number
    upload = shipment.generate_and_attach_sscc_label(return_existing: return_existing)
    # Send status back to job engine
    at(num_labels, "At #{num_labels} of #{total_shipments}, shipment #{shipment.reference_number}")
    if print_profile
      # pipe the upload, this sends it to print node
      print_profile.print_upload(upload)
    else
      # Keep an array of uploads we will combine them later
      pdfs << upload
    end
  end
  results = {}
  # Pass through the redirect parameter.  when we print directly this will be used
  # when we generate an upload, the upload id will take precedence
  results[:redirect_to] = options['redirect_to']

  if pdfs.present?
    file_name = "combined_carton_label_#{Time.current.to_fs(:no_spaces)}.pdf"
    temp_location = Upload.temp_location(file_name)
    if pdfs.size > 1
      pdf = PdfTools.combine(pdfs, output_file_path: temp_location)
      upload = Upload.uploadify(pdf, 'carton_label', nil, file_name, false, 24.hours.from_now)
    else # pdfs is already an upload so just get the first one.
      upload = pdfs.first
    end
    results[:upload_id] = upload.id
  end
  store upload_id: results[:upload_id]
  store redirect_to: results[:redirect_to]
end