Class: Quote::CombinedPdfGenerator

Inherits:
BaseService show all
Defined in:
app/services/quote/combined_pdf_generator.rb

Defined Under Namespace

Classes: Result

Constant Summary collapse

TZ_COMPARISON_PDF_SKUS =
%w[TEMPZONE-FLOOR-HEATING-COMPARISON-CHART-USA TEMPZONE-FLOOR-HEATING-COMPARISON-CHART-CANADA].freeze
CUSTOM_AGREEMENT_PDF_SKU =
'CUSTOM-PRODUCT-ORDER-AGREEMENT-FORM'
CHECK_LIST_FH_SKU =
'PRO-ELECTRICIAN-FLOOR-HEATING-CHECKLIST'
CHECK_LIST_SM_SKU =
'SNOW-MELT-CHECKLIST-MATERIALS-AND-TOOLS-CHECKLIST'

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from BaseService

#log_debug, #log_error, #log_info, #log_warning, #logger, #options, #tagged_logger

Constructor Details

#initialize(options = {}) ⇒ CombinedPdfGenerator

Returns a new instance of CombinedPdfGenerator.



15
16
17
# File 'app/services/quote/combined_pdf_generator.rb', line 15

def initialize(options = {})
  @quote_letter_generator = options[:quote_letter_generator] || Quote::LetterPdfGenerator.new
end

Instance Attribute Details

#quote_letter_generatorObject (readonly)

Returns the value of attribute quote_letter_generator.



8
9
10
# File 'app/services/quote/combined_pdf_generator.rb', line 8

def quote_letter_generator
  @quote_letter_generator
end

Instance Method Details

#process(quote, options = nil) {|current_step, total_steps, "Building quote letter for #{quote.reference_number}"| ... } ⇒ Object

Yields:

  • (current_step, total_steps, "Building quote letter for #{quote.reference_number}")


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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'app/services/quote/combined_pdf_generator.rb', line 19

def process(quote, options = nil)
  options ||= default_pdf_options
  options = options.deep_symbolize_keys if options
  options[:output_to_file] = true if options[:output_to_file].nil?
  options[:show_line_items] = true if options[:show_line_items].nil?
  options[:show_installation_plans] = true if options[:show_installation_plans].nil?
  options[:show_electrical_plans] = true if options[:show_electrical_plans].nil?

  all_suggested_items = options[:show_suggested_add_ons] ? quote.all_suggested_items : {}

  build_plans = options[:show_installation_plans] || options[:show_electrical_plans] || options[:copy_of_plans]

  total_steps = 1
  if build_plans
    number_of_rooms = quote.room_configurations.count
    total_steps += number_of_rooms
  end
  current_step = 1
  yield(current_step, total_steps, "Building quote letter for #{quote.reference_number}") if block_given?

  additional_attachments = []
  combined_pdf = PdfCombinator.new
  copy_of_plans_pdf = PdfCombinator.new
  combined_pdf_files = []
  copy_of_plans_pdf_files = []
  Dir.mktmpdir do |dir|
    if options[:materials_only].to_b
      materials_pdf_blob = Pdf::Document::LetterMaterials.new(quote, options).call.pdf
      materials_file_path = "#{dir}/quote_materials.pdf"
      File.open(materials_file_path, 'wb') do |f|
        f.write materials_pdf_blob
        f.flush
        f.fsync
      end
      combined_pdf_files << materials_file_path
    elsif options[:show_line_items] || options[:show_room_summary]
      options[:suggested_goods] = all_suggested_items[:goods]
      options[:suggested_services] = all_suggested_items[:services]
      quote_pdf_blob = quote_letter_generator.process(quote, options).pdf
      letter_file_path = "#{dir}/quote_letter.pdf"
      File.open(letter_file_path, 'wb') do |f|
        f.write quote_pdf_blob
        f.flush
        f.fsync
      end
      combined_pdf_files << letter_file_path
    end

    if options[:include_custom_order_agreement] && pub = Item.find_publication(CUSTOM_AGREEMENT_PDF_SKU, quote.store)
      total_steps += 1
      current_step += 1
      yield(current_step, total_steps, "Attaching customer order agreement to #{quote.reference_number}") if block_given?
      additional_attachments << pub.literature.id
    end

    if options[:check_list_fh].to_b && (pub = Item.find_publication(CHECK_LIST_FH_SKU, quote.store))
      total_steps += 1
      current_step += 1
      yield(current_step, total_steps, "Attaching checklist floor heating to #{quote.reference_number}") if block_given?
      additional_attachments << pub.literature.id
    end

    if options[:check_list_sm].to_b && (pub = Item.find_publication(CHECK_LIST_SM_SKU, quote.store))
      total_steps += 1
      current_step += 1
      yield(current_step, total_steps, "Attaching checklist snow melting to #{quote.reference_number}") if block_given?
      additional_attachments << pub.literature.id
    end

    if options[:include_addendums] && quote.addendums.present?
      total_steps += quote.addendums.size
      quote.addendums.each do |p|
        current_step += 1
        yield(current_step, total_steps, "Attaching Quote Addendum #{p.id} to #{quote.reference_number}") if block_given?
        additional_attachments << p.id
      end
    end

    if build_plans
      # build plans for each room
      room_configurations = quote.room_configurations.order(:name)
      total_steps += room_configurations.size

      Rails.logger.debug { "master_rooms: room_configurations.map{|rc| rc.controlled_by}.compact.uniq.map{|r| r.id} #{room_configurations.map { |rc| rc.controlled_by }.compact.uniq.map { |r| r.id }.join(', ')}" }
      if (master_rooms = room_configurations.filter_map(&:controlled_by).compact.uniq).present?
        master_rooms.each_with_index do |rc, _i|
          current_step += 1
          Rails.logger.debug { "Building plans for room #{rc.reference_number}" }
          yield(current_step, total_steps, "Building plans for room #{rc.reference_number}") if block_given?

          next unless options[:show_installation_plans]

          multizone_plan = rc.get_or_generate_multizone_plan_pdf
          next unless multizone_plan&.attachment

          yield(current_step, total_steps, "Pulling multizone plan for room #{rc.reference_number}") if block_given?
          Rails.logger.debug { "Pulling multizone plan for room #{rc.reference_number}" }
          multizone_plan_file_path = "#{dir}/multizone_plan_#{rc.reference_number}.pdf"
          multizone_plan.attachment.to_file(multizone_plan_file_path)
          combined_pdf_files << multizone_plan_file_path
          copy_of_plans_pdf_files << multizone_plan_file_path if options[:copy_of_plans].to_b
        end
      end

      room_configurations.each_with_index do |rc, _i|
        current_step += 1
        yield(current_step, total_steps, "Building plans for room #{rc.reference_number}") if block_given?

        if options[:show_installation_plans]
          install_plan = rc.get_or_generate_installation_plan_pdf
          if install_plan&.attachment
            yield(current_step, total_steps, "Pulling installation plan for room #{rc.reference_number}") if block_given?
            install_plan_file_path = "#{dir}/install_plan_#{rc.reference_number}.pdf"
            install_plan.attachment.to_file(install_plan_file_path)
            combined_pdf_files << install_plan_file_path
            copy_of_plans_pdf_files << install_plan_file_path if options[:copy_of_plans].to_b
          end
        end

        if options[:show_electrical_plans].to_b
          electrical_plan = rc.get_or_generate_electrical_plan_pdf
          if electrical_plan&.attachment
            yield(current_step, total_steps, "Pulling electrical plan for room #{rc.reference_number}") if block_given?
            electrical_plan_file_path = "#{dir}/electrical_plan_#{rc.reference_number}.pdf"
            electrical_plan.attachment.to_file(electrical_plan_file_path)
            combined_pdf_files << electrical_plan_file_path
            copy_of_plans_pdf_files << electrical_plan_file_path if options[:copy_of_plans].to_b
          end
        end

        next unless options[:include_last_heat_loss].to_b

        next unless heat_loss_upload = rc.heat_loss_report_pdf && heat_loss_upload.attachment

        yield(current_step, total_steps, "Pulling heat loss for room #{rc.reference_number}") if block_given?
        heat_loss_file_path = "#{dir}/heat_loss_#{rc.reference_number}.pdf"
        heat_loss_upload.attachment.to_file(heat_loss_file_path)
        combined_pdf_files << heat_loss_file_path
        copy_of_plans_pdf_files << heat_loss_file_path if options[:copy_of_plans].to_b
      end

    end

    # Time to combine
    yield(current_step, total_steps, 'Assembling files and creating pdf') if block_given?
    combined_pdf_files.each do |file_path|
      combined_pdf.load(file_path)
    end
    combined_pdf.number_pages(location: [:bottom_right], margin_from_height: -4, font_size: 9)

    # Repeat for plan copy
    if copy_of_plans_pdf_files.present?
      copy_of_plans_pdf_files.each do |file_path|
        copy_of_plans_pdf.load(file_path)
      end
      copy_of_plans_pdf.number_pages(location: [:bottom_right], margin_from_height: -4, font_size: 9)
    end
  end # End of temporary directory

  file_name = "Quote_#{quote.reference_number}.pdf"

  result_hsh = {
    options:,
    file_name:,
    additional_attachments:
  }
  if options[:output_to_file]
    base_name = "Quote_#{quote.reference_number}_#{SecureRandom.hex(6)}"
    temp_path = Upload.temp_location("#{base_name}.pdf")
    result_hsh[:pdf_file_path] = options[:output_to_file_path] || temp_path
    combined_pdf.save result_hsh[:pdf_file_path]
    result_hsh[:pdf_file_path] = Pdf::Compressor.new(input_path: result_hsh[:pdf_file_path]).compress.output_path.to_s
    if copy_of_plans_pdf_files.present?
      extra_plans_file_name = "#{base_name}_plans.pdf"
      extra_plans_temp_path = Upload.temp_location(extra_plans_file_name)
      extra_plans_temp_path = Pdf::Compressor.new(input_path: extra_plans_temp_path).compress.output_path
      result_hsh[:copy_of_plans_pdf_file_path] = extra_plans_temp_path
      result_hsh[:copy_of_plans_file_name] = extra_plans_file_name
      copy_of_plans_pdf.save result_hsh[:copy_of_plans_pdf_file_path]
    end
  else # Stream
    result_hsh[:pdf_data] = Pdf::Compressor.new(input_blob: combined_pdf.to_pdf).compress.output_blob
  end

  Result.new(**result_hsh)
end