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.
Instance Method Summary collapse
- #blocked_for_opportunities? ⇒ Boolean
- #blocked_for_orders? ⇒ Boolean
- #can_be_merged? ⇒ Boolean
- #completed_sales_orders_present? ⇒ Boolean
- #customer ⇒ Object
- #determine_customer_state ⇒ Object
-
#exclude_manually_initiated_event?(event) ⇒ Boolean
:reek:UtilityFunction.
- #leadify ⇒ Object
- #ok_to_delete? ⇒ Boolean
- #opportunities_or_cart_present? ⇒ Boolean
- #reset_capabilities ⇒ Object
- #set_qc_state ⇒ Object
- #set_status ⇒ Object
- #status_should_be ⇒ Object
- #versions_for_audit_trail(_params = {}) ⇒ Object
Instance Method Details
#blocked_for_opportunities? ⇒ Boolean
157 158 159 |
# File 'app/models/concerns/customer_lifecycle.rb', line 157 def blocked_for_opportunities? customer.closed? end |
#blocked_for_orders? ⇒ Boolean
161 162 163 |
# File 'app/models/concerns/customer_lifecycle.rb', line 161 def blocked_for_orders? customer.closed? || customer.bankrupt? end |
#can_be_merged? ⇒ Boolean
123 124 125 |
# File 'app/models/concerns/customer_lifecycle.rb', line 123 def can_be_merged? !merging? end |
#completed_sales_orders_present? ⇒ Boolean
133 134 135 |
# File 'app/models/concerns/customer_lifecycle.rb', line 133 def completed_sales_orders_present? orders.so_only.exists?(state: %w[shipped invoiced]) end |
#customer ⇒ Object
188 189 190 |
# File 'app/models/concerns/customer_lifecycle.rb', line 188 def customer self end |
#determine_customer_state ⇒ Object
141 142 143 144 145 146 147 148 149 150 151 |
# File 'app/models/concerns/customer_lifecycle.rb', line 141 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
:reek:UtilityFunction
110 111 112 |
# File 'app/models/concerns/customer_lifecycle.rb', line 110 def exclude_manually_initiated_event?(event) %i[merge_lock].include?(event.to_sym) end |
#leadify ⇒ Object
169 170 171 172 173 |
# File 'app/models/concerns/customer_lifecycle.rb', line 169 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
175 176 177 178 179 180 181 182 |
# File 'app/models/concerns/customer_lifecycle.rb', line 175 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
137 138 139 |
# File 'app/models/concerns/customer_lifecycle.rb', line 137 def opportunities_or_cart_present? opportunities.exists? || orders.so_only.exists?(state: 'cart') || orders.mo_only.exists? end |
#reset_capabilities ⇒ Object
184 185 186 |
# File 'app/models/concerns/customer_lifecycle.rb', line 184 def reset_capabilities CustomerResetCapabilities.call(self) end |
#set_qc_state ⇒ Object
165 166 167 |
# File 'app/models/concerns/customer_lifecycle.rb', line 165 def set_qc_state self.qc_orders = true if qc_orders.nil? end |
#set_status ⇒ Object
127 128 129 130 131 |
# File 'app/models/concerns/customer_lifecycle.rb', line 127 def set_status return if closed? || lead_qualify? || bankrupt? || merging? || sent_to_collections? self.state = determine_customer_state end |
#status_should_be ⇒ Object
153 154 155 |
# File 'app/models/concerns/customer_lifecycle.rb', line 153 def status_should_be self.state = determine_customer_state end |
#versions_for_audit_trail(_params = {}) ⇒ Object
114 115 116 117 118 119 120 121 |
# File 'app/models/concerns/customer_lifecycle.rb', line 114 def versions_for_audit_trail(_params = {}) query_sql = " (item_type = 'Party' AND item_id = :id) OR (item_type = 'CustomerRecord' AND reference_data @> :party_id_json) " RecordVersion.where(query_sql, id:, party_id_json: { party_id: id }.to_json) end |