Class: Opportunity::Copier

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opportunity, quotes_ids_to_copy = nil, options = {}) ⇒ Copier

Returns a new instance of Copier.



4
5
6
7
8
9
# File 'app/services/opportunity/copier.rb', line 4

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

Instance Attribute Details

#loggerObject (readonly)

Returns the value of attribute logger.



2
3
4
# File 'app/services/opportunity/copier.rb', line 2

def logger
  @logger
end

#opportunityObject (readonly)

Returns the value of attribute opportunity.



2
3
4
# File 'app/services/opportunity/copier.rb', line 2

def opportunity
  @opportunity
end

#quote_idsObject (readonly)

Returns the value of attribute quote_ids.



2
3
4
# File 'app/services/opportunity/copier.rb', line 2

def quote_ids
  @quote_ids
end

#resultsObject (readonly)

Returns the value of attribute results.



2
3
4
# File 'app/services/opportunity/copier.rb', line 2

def results
  @results
end

Instance Method Details

#copy_to(customer, options = {}) ⇒ 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
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
# File 'app/services/opportunity/copier.rb', line 11

def copy_to(customer, options = {}, &)
  report = { errors: [], successes: [], new_opportunity: [] }
  total_steps = 4
  quotes = Quote.where(id: quote_ids)
  room_configurations = RoomConfiguration.joins(:quotes).merge(quotes)
  room_map = nil # declare outside of transaction scope
  Opportunity.transaction do
    yield(1, total_steps, 'Creating cloned opportunity') if block_given?
    # Create cloned opportunity
    @new_opportunity = @opportunity.deep_dup
    @new_opportunity.opportunity_participants.where(party_id: @opportunity.customer.id).destroy_all
    @new_opportunity.customer = customer
    @new_opportunity.contact_id = nil
    @new_opportunity.state = 'interest'
    @new_opportunity.primary_sales_rep_id = customer.primary_sales_rep_id
    @new_opportunity.secondary_sales_rep_id = customer.secondary_sales_rep_id
    @new_opportunity.local_sales_rep_id = customer.local_sales_rep_id
    # Only update source if opportunity doesn't have Google Ads attribution
    unless @new_opportunity.source_locked?
      @new_opportunity.source = customer.source if @new_opportunity.source.nil? || @new_opportunity.source.unknown_source?
    end
    @new_opportunity.save!

    # Copy RoomConfigurations and Quotes
    # We're going to do this in two step, first we will copy all rooms
    yield(2, total_steps, 'Copying room configurations') if block_given?
    # Defer room plan gen until out of transaction block
    room_map = RoomConfiguration::Copier.new(room_configurations, skip_room_plan_generation: true).copy_to(@new_opportunity, &).room_copy_results
    # Then each quote in turn, but feeding the room map
    yield(4, total_steps, 'Copying quotes configurations') if block_given?
    quotes.each do |quote|
      Quote::Copier.new(quote, skip_room_plan_generation: true).copy_to(@new_opportunity, room_map: room_map, &)
    end

    # customer.activities.create!(:new_note => "Opportunity: #{@new_opportunity.name} <br> Quote: #{quote_nos.to_sentence} <br> Copied to customer id: #{customer.id}") unless customer.nil?
    yield(3, total_steps, 'Finishing') if block_given?
    msgs = []
    msgs << "Copying Opportunity #{@opportunity.name} (#{@opportunity.reference_number}) to the target customer #{customer.full_name} (#{customer.reference_number}). New opportunity is #{@new_opportunity.reference_number}"
    # We remap the catalog, because you can copy to a new customer with a different catalog. This will also raise an error with strict mode and rollback if a catalog item cannot be matched
    msgs += @new_opportunity.reload.quotes.map { |q| CatalogItem::Remapper.new(q, { strict_mode: true, logger: logger }).fix_all }
    msg = msgs.join('. ')
    report[:successes] << msg
    report[:new_opportunity] << @new_opportunity.id.to_s
    logger.info msg
  rescue StandardError => e
    msg = "Cannot copy the opportunity #{@opportunity.name} (#{@opportunity.reference_number}) to the target customer #{customer.full_name} (#{customer.reference_number}) due to errors: #{e}"
    logger.error msg
    report[:errors] << msg
    raise ActiveRecord::Rollback
  end
  # Outside of the transaction block, we will load our new rooms and trigger plan generation
  if room_map.present?
    copied_rooms = RoomConfiguration.where(id: room_map.values)
    RoomConfiguration::Copier.generate_all_plans(copied_rooms)
  end
  report
end