Class: Sales::DropRep

Inherits:
BaseService
  • Object
show all
Defined in:
app/services/sales/drop_rep.rb

Overview

Operation to set a new source on invoices and related order and opportunity

Defined Under Namespace

Classes: Result

Instance Method Summary collapse

Instance Method Details

#process(customer_drop_event) ⇒ Object

Pass a customer_drop_event that is not yet saved, it is simplest to reuse
this model as a command pattern.



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
# File 'app/services/sales/drop_rep.rb', line 10

def process(customer_drop_event)
  return Result.new(success: false) unless customer_drop_event.new_record? && customer_drop_event.valid?
  success = false
  remove_rep_ids = []
  CustomerDropEvent.transaction do
    customer = customer_drop_event.customer
    sales_rep_id = customer_drop_event.sales_rep_id
    reason_code = customer_drop_event.reason_code
    close_result = evaluate_for_closure(customer, reason_code)
    if close_result == :close_customer
      # All reps will be removed
      remove_rep_ids = [customer.primary_sales_rep_id,customer.secondary_sales_rep_id,customer.local_sales_rep_id].compact.uniq
    elsif is_primary_rep = sales_rep_id == customer.primary_sales_rep_id && customer.secondary_sales_rep_id
      # Secondary is automatically removed when primary goes
      remove_rep_ids = [customer.primary_sales_rep_id,customer.secondary_sales_rep_id].compact.uniq
    else
      # just the designated rep goes
      remove_rep_ids = [sales_rep_id]
    end
    remove_rep_ids.compact.uniq.each do |sales_rep_id|
      remove_existing_rep(sales_rep_id, customer)
      cancel_rep_activities(customer,sales_rep_id)
    end

    # move customer back to lead_qualify if reassign and we're dropping a primary rep
    customer.state = 'lead_qualify' if customer_drop_event.reassign_rep? && is_primary_rep
    customer.save!
    success = true
  end
  return Result.new(success: success, remove_rep_ids: remove_rep_ids)

end