Class: Quote::Mover

Inherits:
Object
  • Object
show all
Defined in:
app/services/quote/mover.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(quote, options = {}) ⇒ Mover

Returns a new instance of Mover.



5
6
7
8
9
# File 'app/services/quote/mover.rb', line 5

def initialize(quote, options={})
  @options = options
  @quote = quote
  @logger = options[:logger] || Rails.logger
end

Instance Attribute Details

#loggerObject (readonly)

Returns the value of attribute logger.



3
4
5
# File 'app/services/quote/mover.rb', line 3

def logger
  @logger
end

#quoteObject (readonly)

Returns the value of attribute quote.



3
4
5
# File 'app/services/quote/mover.rb', line 3

def quote
  @quote
end

#resultsObject (readonly)

Returns the value of attribute results.



3
4
5
# File 'app/services/quote/mover.rb', line 3

def results
  @results
end

Instance Method Details

#move_room_configuration(room_configuration, opportunity) ⇒ Object



43
44
45
46
47
48
49
50
51
# File 'app/services/quote/mover.rb', line 43

def move_room_configuration(room_configuration, opportunity)
  room_configuration.opportunity = opportunity
  room_configuration.save!
  room_configuration.reload
  # Note: engineering prefers to keep all for context
  room_configuration.uploads.where(category: %w(electrical_plan_pdf installation_plan_pdf)).each{|u| u.update(category: 'archive', note: "Was #{u.category}") }
  # Queue for regeneration
  GenerateRoomPlansWorker.perform_async(room_configuration.id)
end

#move_to(opportunity) ⇒ Object



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
# File 'app/services/quote/mover.rb', line 11

def move_to(opportunity)
  @results = []
  success = false

  if @quote.orders.active.non_carts.present?
    @results << "Cannot move a quote with active orders, cancel them before."
    return false
  end

  Quote.transaction do
    begin
      @quote.opportunity = opportunity
      @quote.state = 'pending' unless quote.complete?
      @quote.contact_points.clear # Clear out to be safe
      @quote.save!
      @quote.reload
      @quote.room_configurations.each{ |room_configuration| move_room_configuration(room_configuration, opportunity) } if @quote.room_configurations.present?
      # Note: engineering prefers to keep all for context
      @quote.uploads.each{|u| u.update(category: 'archive', note: "Was #{u.category}") }

      success = true
    rescue StandardError => exc
      success = false
      @results << "Quote could not be moved: #{exc.to_s}"
      @logger.error exc.inspect
      raise ActiveRecord::Rollback
    end
  end

  success
end