Class: Opportunity::Mover
- Inherits:
-
Object
- Object
- Opportunity::Mover
- Defined in:
- app/services/opportunity/mover.rb
Instance Attribute Summary collapse
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
-
#opportunity ⇒ Object
readonly
Returns the value of attribute opportunity.
-
#results ⇒ Object
readonly
Returns the value of attribute results.
Instance Method Summary collapse
- #convert_catalog(itemizable, catalog) ⇒ Object
-
#initialize(opportunity, options = {}) ⇒ Mover
constructor
A new instance of Mover.
- #move_quote(quote, customer) ⇒ Object
- #move_room_configuration(room_configuration, customer) ⇒ Object
- #move_to(customer) ⇒ Object
Constructor Details
#initialize(opportunity, options = {}) ⇒ Mover
Returns a new instance of Mover.
4 5 6 7 8 |
# File 'app/services/opportunity/mover.rb', line 4 def initialize(opportunity, = {}) @options = @opportunity = opportunity @logger = [:logger] || Rails.logger end |
Instance Attribute Details
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
2 3 4 |
# File 'app/services/opportunity/mover.rb', line 2 def logger @logger end |
#opportunity ⇒ Object (readonly)
Returns the value of attribute opportunity.
2 3 4 |
# File 'app/services/opportunity/mover.rb', line 2 def opportunity @opportunity end |
#results ⇒ Object (readonly)
Returns the value of attribute results.
2 3 4 |
# File 'app/services/opportunity/mover.rb', line 2 def results @results end |
Instance Method Details
#convert_catalog(itemizable, catalog) ⇒ Object
88 89 90 91 |
# File 'app/services/opportunity/mover.rb', line 88 def convert_catalog(itemizable, catalog) cm = CatalogItem::Remapper.new(itemizable, { logger: @logger, target_catalog: catalog }) cm.fix_catalog end |
#move_quote(quote, customer) ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'app/services/opportunity/mover.rb', line 64 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
80 81 82 83 84 85 86 |
# File 'app/services/opportunity/mover.rb', line 80 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]).each { |u| u.update(category: 'archive', note: "Was #{u.category}") } # Queue for regeneration GenerateRoomPlansWorker.perform_async(room_configuration.id) end |
#move_to(customer) ⇒ Object
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 52 53 54 55 56 57 58 59 60 61 62 |
# File 'app/services/opportunity/mover.rb', line 10 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.where(party_id: @opportunity.customer.id).destroy_all # 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}") unless customer.nil? success = true rescue StandardError => e success = false @results << "Opportunity could not be moved: #{e}" @logger.error e.inspect raise ActiveRecord::Rollback end success end |