Class: Opportunity::Merger

Inherits:
BaseService show all
Defined in:
app/services/opportunity/merger.rb

Defined Under Namespace

Classes: Result

Instance Method Summary collapse

Methods inherited from BaseService

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

Constructor Details

This class inherits a constructor from BaseService

Instance Method Details

#process(duplicate_opp:, survivor_opp:) ⇒ 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
# File 'app/services/opportunity/merger.rb', line 8

def process(duplicate_opp:, survivor_opp:)
  return Result.new(merged: false, messages: ['Cannot merge opportunity unto itself']) if duplicate_opp == survivor_opp
  return Result.new(merged: false, messages: ['Cannot merge opportunities belonging to different accounts, move opportunity first']) unless duplicate_opp.customer_id == survivor_opp.customer_id
  messages = []
  merged = false
  Opportunity.transaction do
    survivor_opp.contact_id ||= duplicate_opp.contact_id
    survivor_opp.media ||= duplicate_opp.media
    survivor_opp.summary ||= duplicate_opp.summary
    survivor_opp.opportunity_reception_type ||= duplicate_opp.opportunity_reception_type
    survivor_opp.technical_support_rep_sec_id ||= duplicate_opp.technical_support_rep_sec_id
    survivor_opp.installation_postal_code ||= duplicate_opp.installation_postal_code
    survivor_opp. ||= duplicate_opp.
    survivor_opp.buying_group_id ||= duplicate_opp.buying_group_id
    survivor_opp.local_sales_rep_id ||= duplicate_opp.local_sales_rep_id
    survivor_opp.primary_sales_rep_id ||= duplicate_opp.primary_sales_rep_id
    survivor_opp.secondary_sales_rep_id ||= duplicate_opp.secondary_sales_rep_id
    survivor_opp.specs_eta ||= duplicate_opp.specs_eta
    survivor_opp.planned_installation_date ||= duplicate_opp.planned_installation_date
    survivor_opp.contact_id ||= duplicate_opp.contact_id
    survivor_opp.original_sales_rep_id ||= duplicate_opp.original_sales_rep_id
    survivor_opp.state_code ||= duplicate_opp.state_code
    survivor_opp.source_id ||= duplicate_opp.source_id
    survivor_opp.technical_support_rep_id ||= duplicate_opp.technical_support_rep_id
    survivor_opp.electricity_rate ||= duplicate_opp.electricity_rate
    survivor_opp.visit_id ||= duplicate_opp.visit_id
    survivor_opp.save!
    duplicate_opp.orders.update_all(opportunity_id: survivor_opp.id)
    duplicate_opp.quotes.update_all(opportunity_id: survivor_opp.id)
    duplicate_opp.room_configurations.update_all(opportunity_id: survivor_opp.id)
    duplicate_opp.activities.update_all(resource_id: survivor_opp.id)
    # Move over all participants that are not already in the target opp
    duplicate_opp.opportunity_participants.where.not(party_id: survivor_opp.opportunity_participants.pluck(:party_id)).update_all(opportunity_id: survivor_opp.id)
    messages << "Opportunity  #{duplicate_opp.reference_number} #{duplicate_opp.name} was merged into opportunity id #{survivor_opp.reference_number} #{survivor_opp.name}"
    duplicate_opp.reload.destroy
    if duplicate_opp.errors.any?
      messages += duplicate_opp.errors.full_messages
      merged = false
    else
      merged = true
    end
  end
  return Result.new(merged: merged, survivor_opp: survivor_opp, messages: messages)
end