Module: CustomerLifecycle

Extended by:
ActiveSupport::Concern
Included in:
Customer
Defined in:
app/models/concerns/customer_lifecycle.rb

Overview

State machine, merge lock, customer state derivation, and account-closure hooks.

See Also:

Instance Method Summary collapse

Instance Method Details

#blocked_for_opportunities?Boolean

Returns:

  • (Boolean)


138
139
140
# File 'app/models/concerns/customer_lifecycle.rb', line 138

def blocked_for_opportunities?
  customer.closed?
end

#blocked_for_orders?Boolean

Returns:

  • (Boolean)


142
143
144
# File 'app/models/concerns/customer_lifecycle.rb', line 142

def blocked_for_orders?
  customer.closed? || customer.bankrupt?
end

#can_be_merged?Boolean

Returns:

  • (Boolean)


104
105
106
# File 'app/models/concerns/customer_lifecycle.rb', line 104

def can_be_merged?
  !merging?
end

#completed_sales_orders_present?Boolean

Returns:

  • (Boolean)


114
115
116
# File 'app/models/concerns/customer_lifecycle.rb', line 114

def completed_sales_orders_present?
  orders.so_only.exists?(state: %w[shipped invoiced])
end

#customerObject



169
170
171
# File 'app/models/concerns/customer_lifecycle.rb', line 169

def customer
  self
end

#determine_customer_stateObject



122
123
124
125
126
127
128
129
130
131
132
# File 'app/models/concerns/customer_lifecycle.rb', line 122

def determine_customer_state
  if guest?
    contactable? ? :lead_qualify : :guest
  elsif completed_sales_orders_present?
    :customer
  elsif opportunities_or_cart_present?
    :prospect
  else
    :lead
  end
end

#exclude_manually_initiated_event?(event) ⇒ Boolean

rubocop:enable Metrics/BlockLength

Returns:

  • (Boolean)


91
92
93
# File 'app/models/concerns/customer_lifecycle.rb', line 91

def exclude_manually_initiated_event?(event) # :reek:UtilityFunction
  %i[merge_lock].include?(event.to_sym)
end

#leadifyObject



150
151
152
153
154
# File 'app/models/concerns/customer_lifecycle.rb', line 150

def leadify
  self.state = 'lead_qualify' if guest? || state.nil?
  self.must_have_email = true
  self.must_have_one_contact = self.perform_lead_verification = (profile && profile.organization?)
end

#ok_to_delete?Boolean

Returns:

  • (Boolean)


156
157
158
159
160
161
162
163
# File 'app/models/concerns/customer_lifecycle.rb', line 156

def ok_to_delete?
  opportunities.where.not(state: 'cancelled').empty? &&
    quotes.open_quotes.empty? &&
    orders.non_carts.empty? &&
    support_case_participants.empty? &&
    invoices.empty? &&
    credit_memos.empty?
end

#opportunities_or_cart_present?Boolean

Returns:

  • (Boolean)


118
119
120
# File 'app/models/concerns/customer_lifecycle.rb', line 118

def opportunities_or_cart_present?
  opportunities.exists? || orders.so_only.exists?(state: 'cart') || orders.mo_only.exists?
end

#reset_capabilitiesObject



165
166
167
# File 'app/models/concerns/customer_lifecycle.rb', line 165

def reset_capabilities
  CustomerResetCapabilities.call(self)
end

#set_qc_stateObject



146
147
148
# File 'app/models/concerns/customer_lifecycle.rb', line 146

def set_qc_state
  self.qc_orders = true if qc_orders.nil?
end

#set_statusObject



108
109
110
111
112
# File 'app/models/concerns/customer_lifecycle.rb', line 108

def set_status
  return if closed? || lead_qualify? || bankrupt? || merging? || sent_to_collections?

  self.state = determine_customer_state
end

#status_should_beObject



134
135
136
# File 'app/models/concerns/customer_lifecycle.rb', line 134

def status_should_be
  self.state = determine_customer_state
end

#versions_for_audit_trail(_params = {}) ⇒ Object



95
96
97
98
99
100
101
102
# File 'app/models/concerns/customer_lifecycle.rb', line 95

def versions_for_audit_trail(_params = {})
  query_sql = %q{
                (item_type = 'Party' AND item_id = :id)
                OR
                (item_type = 'CustomerRecord' AND reference_data @> '{"party_id": :id}')
              }
  RecordVersion.where(query_sql, id:)
end