Class: Opportunity::Mover

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

Overview

Service object: mover.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Mover.



6
7
8
9
10
# File 'app/services/opportunity/mover.rb', line 6

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

Instance Attribute Details

#loggerObject (readonly)

Returns the value of attribute logger.



4
5
6
# File 'app/services/opportunity/mover.rb', line 4

def logger
  @logger
end

#opportunityObject (readonly)

Returns the value of attribute opportunity.



4
5
6
# File 'app/services/opportunity/mover.rb', line 4

def opportunity
  @opportunity
end

#resultsObject (readonly)

Returns the value of attribute results.



4
5
6
# File 'app/services/opportunity/mover.rb', line 4

def results
  @results
end

Instance Method Details

#convert_catalog(itemizable, catalog) ⇒ Object



92
93
94
95
# File 'app/services/opportunity/mover.rb', line 92

def convert_catalog(itemizable, catalog)
  cm = CatalogItem::Remapper.new(itemizable, { logger: @logger, target_catalog: catalog })
  cm.fix_catalog
end

#move_quote(quote, customer) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'app/services/opportunity/mover.rb', line 66

def move_quote(quote, customer)
  # quote.primary_sales_rep_id = customer.primary_sales_rep_id
  # quote.secondary_sales_rep_id = customer.secondary_sales_rep_id
  # quote.local_sales_rep_id ||= customer.local_sales_rep_id
  quote.shipping_address = customer.shipping_address
  quote.pricing_program_description = customer.pricing_program_description
  quote.pricing_program_discount = customer.pricing_program_discount
  quote.state = 'pending' unless quote.complete?
  quote.contact_points.clear # Clear out to be safe
  quote.save!
  quote.reload
  convert_catalog quote, customer.catalog
  # NOTE: engineering prefers to keep all for context
  quote.uploads.each { |u| u.update(category: 'archive', note: "Was #{u.category}") }
end

#move_room_configuration(room_configuration, customer) ⇒ Object



82
83
84
85
86
87
88
89
90
# File 'app/services/opportunity/mover.rb', line 82

def move_room_configuration(room_configuration, customer)
  convert_catalog room_configuration, customer.catalog
  # NOTE: engineering prefers to keep all for context
  room_configuration.uploads.where(category: %w[electrical_plan_pdf installation_plan_pdf]).find_each { |u| u.update(category: 'archive', note: "Was #{u.category}") }
  # Queue for regeneration after the surrounding Opportunity.transaction commits
  ActiveRecord.after_all_transactions_commit do
    GenerateRoomPlansWorker.perform_async(room_configuration.id)
  end
end

#move_to(customer) ⇒ Object



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
63
64
# File 'app/services/opportunity/mover.rb', line 12

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

  if @opportunity.orders.active.non_carts.present?
    @results << 'Cannot move an opportunity with active orders, cancel them before.'
    return false
  end

  Opportunity.transaction do
    # Garbage stays behind
    @opportunity.orders.cancelled.each(&:destroy)
    @opportunity.orders.carts.each(&:destroy)

    # Name collision detection
    @opportunity.name = Opportunity.get_unique_name(customer, @opportunity.name)
    # Remove the original customer from the participants
    @opportunity.opportunity_participants.destroy_by(party_id: @opportunity.customer.id)
    # Map a new customer
    @opportunity.customer = customer
    @opportunity.contact_id = nil
    @opportunity.primary_sales_rep_id = customer.primary_sales_rep_id
    @opportunity.secondary_sales_rep_id = customer.secondary_sales_rep_id
    @opportunity.local_sales_rep_id ||= customer.local_sales_rep_id
    # Only update source if opportunity doesn't have Google Ads attribution
    unless @opportunity.source_locked?
      @opportunity.source = customer.source if @opportunity.source.nil? || @opportunity.source.unknown_source?
    end
    @opportunity.save!
    @opportunity.room_configurations.each { |room_configuration| move_room_configuration(room_configuration, customer) }
    @opportunity.quotes.each { |quote| move_quote(quote, customer) }

    # All of this opportunitie's activities need to be remapped to the new customer
    @opportunity.linked_activities.each do |a|
      a.new_note = "Moved from #{a.customer&.full_name} to #{@opportunity.customer.full_name} during opportunity move"
      a.customer = @opportunity.customer
      a.party = @opportunity.customer
      a.save!
    end

    quote_nos = @opportunity.quotes.pluck(:reference_number)
    customer&.activities&.create!(new_note: "Opportunity: #{@opportunity.name} <br> Quote: #{quote_nos.to_sentence} <br> Moved to customer id: #{customer.id}")

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

  success
end