Module: Models::CustomerOrganization

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

Overview

Parent/child orgs, buying groups, marketplace predicates, company determination.

See Also:

Instance Method Summary collapse

Instance Method Details

#abbreviation_for_packing_listString?

Short name used on packing lists for marketplace/retailer accounts.

Prefers a known marketplace code when one applies, otherwise uses the
full name (when short enough) or an initialism.

Returns:

  • (String, nil)


87
88
89
90
91
# File 'app/concerns/models/customer_organization.rb', line 87

def abbreviation_for_packing_list
  abbreviation = name.length <= 18 ? name : nil
  abbreviation ||= name.split.filter_map(&:first).join
  marketplace_packing_abbreviation || abbreviation
end

#all_or_none_are_blank_in_address?Boolean

Whether any core address fields are populated.

Returns:

  • (Boolean)


406
407
408
# File 'app/concerns/models/customer_organization.rb', line 406

def all_or_none_are_blank_in_address?
  zip.present? || street1.present? || street2.present? || street3.present? || city.present?
end

#amazon_com?Boolean

Whether this customer is part of the Amazon.com ecosystem.

Returns:

  • (Boolean)


188
189
190
# File 'app/concerns/models/customer_organization.rb', line 188

def amazon_com?
  amazon_com_by_id? || amazon_com_by_parent_or_billing? || amazon_com_vendor_or_seller_ids?
end

#amazon_seller_central?Boolean

Whether this customer is an Amazon Seller Central account.

Returns:

  • (Boolean)


195
196
197
# File 'app/concerns/models/customer_organization.rb', line 195

def amazon_seller_central?
  CustomerConstants::AMAZON_SELLER_CENTRAL_CUSTOMER_IDS.include?(id)
end

#amazon_vendor_central?Boolean

Whether this customer is an Amazon Vendor Central account.

Returns:

  • (Boolean)


202
203
204
# File 'app/concerns/models/customer_organization.rb', line 202

def amazon_vendor_central?
  CustomerConstants::AMAZON_VENDOR_CENTRAL_CUSTOMER_IDS.include?(id)
end

#broadcast_buying_group_changed(buying_group_id_added: nil, buying_group_id_removed: nil) ⇒ void

This method returns an undefined value.

Parameters:

  • buying_group_id_added (Integer, nil) (defaults to: nil)

    group the customer just joined

  • buying_group_id_removed (Integer, nil) (defaults to: nil)

    group the customer just left



366
367
368
369
# File 'app/concerns/models/customer_organization.rb', line 366

def broadcast_buying_group_changed(buying_group_id_added: nil,
                                   buying_group_id_removed: nil)
  CustomerBuyingGroupUpdater.broadcast(self, added: buying_group_id_added, removed: buying_group_id_removed)
end

#build_com?Boolean

Whether this customer is the Build.com house account.

Returns:

  • (Boolean)


272
273
274
# File 'app/concerns/models/customer_organization.rb', line 272

def build_com?
  id == CustomerConstants::BUILD_COM_ID
end

#buying_group_company?Boolean

Whether this customer record represents a buying-group company.

Returns:

  • (Boolean)


318
319
320
# File 'app/concerns/models/customer_organization.rb', line 318

def buying_group_company?
  profile&.name&.include?('Buying Group') && !buying_group
end

#buying_group_nameString?

Name of the customer's buying group, if any.

Returns:

  • (String, nil)


70
71
72
# File 'app/concerns/models/customer_organization.rb', line 70

def buying_group_name
  buying_group&.name
end

#buying_groups_options_for_selectArray<Array(String, Integer)>

Options for a buying-group select control scoped to this customer.

Returns:

  • (Array<Array(String, Integer)>)


63
64
65
# File 'app/concerns/models/customer_organization.rb', line 63

def buying_groups_options_for_select
  BuyingGroup.options_for_select(include_id: buying_group_id, company_id: determine_company_id)
end

#can_apply_document_customer?(document_customer_id, billing_customer_id = nil) ⇒ Boolean

Whether a document (invoice / credit memo) owned by +document_customer_id+
— optionally consolidated-billed to +billing_customer_id+ — may be applied
to a receipt whose payor is this customer. True when this customer is the
document's billing customer or in the same organization family. Single
source of truth behind the receipt form's mismatch-warning endpoints and
the XLSX import's customer gate, so both honor the same rule.

Parameters:

  • document_customer_id (Integer, String, nil)

    customer_id claimed by the document

  • billing_customer_id (Integer, String, nil) (defaults to: nil)

    bill-to party on the document, when it names one

Returns:

  • (Boolean)


47
48
49
50
51
# File 'app/concerns/models/customer_organization.rb', line 47

def can_apply_document_customer?(document_customer_id, billing_customer_id = nil)
  return true if billing_customer_id.present? && billing_customer_id.to_i == id

  document_customer_id.present? && organization_family_ids.include?(document_customer_id.to_i)
end

#canadian_tire?Boolean

Whether this customer is the Canadian Tire house account.

Returns:

  • (Boolean)


265
266
267
# File 'app/concerns/models/customer_organization.rb', line 265

def canadian_tire?
  id == CustomerConstants::CANADIAN_TIRE_ID
end

#certified_installer?Boolean

Whether this customer has any active installer certifications.

Returns:

  • (Boolean)


286
287
288
# File 'app/concerns/models/customer_organization.rb', line 286

def certified_installer?
  certifications.active.any?
end

#company_nameString?

Organization name stored on the customer record.

Returns:

  • (String, nil)


110
111
112
# File 'app/concerns/models/customer_organization.rb', line 110

def company_name
  full_name if organization?
end

#company_name=(val) ⇒ void

This method returns an undefined value.

Parameters:

  • val (String, nil)

    the organization name; blank clears it



116
117
118
# File 'app/concerns/models/customer_organization.rb', line 116

def company_name=(val)
  self.full_name = val.presence
end

#costco_ca?Boolean

Whether this customer is the Costco Canada house account.

Returns:

  • (Boolean)


216
217
218
# File 'app/concerns/models/customer_organization.rb', line 216

def costco_ca?
  id == CustomerConstants::COSTCO_CA_ID
end

#customer_segmentString

One-letter segment code for this customer: TP (trade pro), BG (buying group),
or HO (homeowner).

Returns:

  • (String)


375
376
377
378
379
380
381
# File 'app/concerns/models/customer_organization.rb', line 375

def customer_segment
  if organization?
    buying_group.nil? ? 'TP' : 'BG'
  else
    'HO'
  end
end

#dealer?Boolean

Customer-level mirror of Profile#dealer?.

Returns:

  • (Boolean)


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

def dealer?
  profile&.dealer? || false
end

#dealer_or_trade_pro_for_locator?Boolean

Legacy method kept for the dealer-locator call sites. The _for_locator
suffix is now a misnomer — quote.rb also calls it — but the name is
preserved to avoid a cross-cutting rename in this PR. Implementation
now delegates to the new trade_pro? / dealer? predicates instead
of the deprecated profile.jde_code_1 attribute.

Returns:

  • (Boolean)


397
398
399
400
401
# File 'app/concerns/models/customer_organization.rb', line 397

def dealer_or_trade_pro_for_locator?
  trade_pro? || dealer?
rescue StandardError
  false
end

#determine_company_idInteger?

Company ID for this customer, falling back to the customer's catalog.

Returns:

  • (Integer, nil)


56
57
58
# File 'app/concerns/models/customer_organization.rb', line 56

def determine_company_id
  company_id || catalog&.company_id
end

#direct_buy?Boolean?

Whether this customer is in a direct-buy buying group.

Returns:

  • (Boolean, nil)


325
326
327
# File 'app/concerns/models/customer_organization.rb', line 325

def direct_buy?
  buying_group&.is_direct_buy?
end

#direct_buy_can?Boolean?

Whether this customer is in a Canada direct-buy buying group.

Returns:

  • (Boolean, nil)


339
340
341
# File 'app/concerns/models/customer_organization.rb', line 339

def direct_buy_can?
  buying_group&.is_direct_buy_can?
end

#direct_buy_usa?Boolean?

Whether this customer is in a USA direct-buy buying group.

Returns:

  • (Boolean, nil)


332
333
334
# File 'app/concerns/models/customer_organization.rb', line 332

def direct_buy_usa?
  buying_group&.is_direct_buy_usa?
end

#e_commerce_misc?Boolean?

Customer-level mirror of Profile#e_commerce_misc?.

Returns:

  • (Boolean, nil)


153
154
155
# File 'app/concerns/models/customer_organization.rb', line 153

def e_commerce_misc?
  profile&.e_commerce_misc?
end

#e_commerce_misc_or_direct_pro?Boolean

Whether this customer has an e-commerce-misc or direct-pro profile.

Returns:

  • (Boolean)


160
161
162
# File 'app/concerns/models/customer_organization.rb', line 160

def e_commerce_misc_or_direct_pro?
  profile && (profile.e_commerce_misc? || profile.direct_pro?)
end

#effective_report_groupingString

Report grouping bucket assigned to this customer.

Returns:

  • (String)


103
104
105
# File 'app/concerns/models/customer_organization.rb', line 103

def effective_report_grouping
  Customer::ReportGrouping.get_report_grouping_for_customer(self)
end

#home_depot_can?Boolean

Whether this customer is the Home Depot Canada house account.

Returns:

  • (Boolean)


174
175
176
# File 'app/concerns/models/customer_organization.rb', line 174

def home_depot_can?
  id == CustomerConstants::HOME_DEPOT_CAN_ID
end

#home_depot_usa?Boolean

Whether this customer is the Home Depot USA house account.

Returns:

  • (Boolean)


167
168
169
# File 'app/concerns/models/customer_organization.rb', line 167

def home_depot_usa?
  id == CustomerConstants::HOME_DEPOT_USA_ID
end

#homeowner?Boolean

Whether this customer is a homeowner / end consumer.

Returns:

  • (Boolean)


130
131
132
# File 'app/concerns/models/customer_organization.rb', line 130

def homeowner?
  profile_id == ProfileConstants::HOMEOWNER
end

#houzz?Boolean

Whether this customer is the Houzz house account.

Returns:

  • (Boolean)


258
259
260
# File 'app/concerns/models/customer_organization.rb', line 258

def houzz?
  id == CustomerConstants::HOUZZ_ID
end

#include_po_prefix?Boolean

Whether this customer's PO numbers should include a prefix.

Returns:

  • (Boolean)


96
97
98
# File 'app/concerns/models/customer_organization.rb', line 96

def include_po_prefix?
  CustomerConstants::SKIP_PREFIX_IN_PO_NUMBER_FOR_BILLING_ENTITY.exclude?(id)
end

#organization?Boolean

Whether this customer is an organization (not a homeowner).

Returns:

  • (Boolean)


123
124
125
# File 'app/concerns/models/customer_organization.rb', line 123

def organization?
  !homeowner?
end

#organization_family_idsArray<Integer>

Ids of the accounts whose credit memos may be applied to one
another's receipts: this customer, its parent, and its direct
children. Consolidated-billing parents and children share an AR
relationship, so a parent's receipt can offset a child's credit
memo and vice versa. Single-level hierarchy (parent_id) — no
grandchildren; siblings are reachable only through the shared parent.

Returns:

  • (Array<Integer>)


24
25
26
# File 'app/concerns/models/customer_organization.rb', line 24

def organization_family_ids
  ([id, parent_id].compact + child_organizations.ids).uniq
end

#overstock_com?Boolean

Whether this customer is the Overstock.com house account.

Matches by name, by known customer ID, or by catalog name.

Returns:

  • (Boolean)


295
296
297
298
299
# File 'app/concerns/models/customer_organization.rb', line 295

def overstock_com?
  name.to_s.casecmp('overstock.com').zero? ||
    (id == 303_919) ||
    catalog.name.to_s.casecmp('overstock.com').zero?
end

#parent_organization_nameString?

Name of the customer's parent organization, if any.

Returns:

  • (String, nil)


77
78
79
# File 'app/concerns/models/customer_organization.rb', line 77

def parent_organization_name
  parent_organization&.name
end

#part_of_lowes_com?Boolean

Whether this customer is the Lowe's.com house account.

Returns:

  • (Boolean)


230
231
232
# File 'app/concerns/models/customer_organization.rb', line 230

def part_of_lowes_com?
  [CustomerConstants::LOWES_COM_ID].include?(id)
end

#part_of_rona?Boolean

Whether this customer belongs to the RONA organization family.

Returns:

  • (Boolean)


223
224
225
# File 'app/concerns/models/customer_organization.rb', line 223

def part_of_rona?
  CustomerConstants::RONA_INC_CA_IDS.include?(id)
end

#person?Boolean

Whether this customer represents a person (homeowner).

Returns:

  • (Boolean)


386
387
388
# File 'app/concerns/models/customer_organization.rb', line 386

def person?
  homeowner?
end

#retailer_organization?Boolean

True for the marketplace/retailer house accounts (Amazon, Wayfair, Home
Depot, Lowe's, Costco, …) whose orders belong to the retailer rather
than the end consumer. Used by warranty registration to decide whether
an order's customer IS the registrant or a dedicated end-consumer
Customer must be attached instead.

Returns:

  • (Boolean)


308
309
310
311
312
313
# File 'app/concerns/models/customer_organization.rb', line 308

def retailer_organization?
  home_depot_usa? || home_depot_can? || walmart_ca? || amazon_com? ||
    amazon_seller_central? || amazon_vendor_central? || costco_ca? ||
    part_of_rona? || part_of_lowes_com? || wayfair? || houzz? ||
    canadian_tire? || build_com? || zoro? || overstock_com?
end

#same_organization_family?(other) ⇒ Boolean

Whether +other+ is in this customer's billing family — used to gate
cross-account credit-memo application on receipts.

Parameters:

Returns:

  • (Boolean)


33
34
35
# File 'app/concerns/models/customer_organization.rb', line 33

def same_organization_family?(other)
  other.present? && organization_family_ids.include?(other.id)
end

#self_and_children_idsArray<Integer>

IDs of this customer and its direct child organizations.

Returns:

  • (Array<Integer>)


12
13
14
# File 'app/concerns/models/customer_organization.rb', line 12

def self_and_children_ids
  [id] + child_organizations.ids
end

#trade_pro?Boolean

Customer-level mirrors of Profile#trade_pro? / Profile#dealer?.
Same delegation pattern as homeowner? (which reads profile_id).
Aliased to is_trade_pro? / is_dealer? in Models::CustomerAliases.

Returns:

  • (Boolean)


139
140
141
# File 'app/concerns/models/customer_organization.rb', line 139

def trade_pro?
  profile&.trade_pro? || false
end

#update_buying_group(buying_group_id) ⇒ void

This method returns an undefined value.

Parameters:

  • buying_group_id (Integer, String, nil)

    the buying group to move this customer into; nil detaches



359
360
361
# File 'app/concerns/models/customer_organization.rb', line 359

def update_buying_group(buying_group_id)
  CustomerBuyingGroupUpdater.call(self, buying_group_id)
end

#use_parent_billing_addressBoolean

Whether this customer currently uses its parent organization's billing
address.

Returns:

  • (Boolean)


347
348
349
# File 'app/concerns/models/customer_organization.rb', line 347

def use_parent_billing_address
  parent_organization&.billing_address_id && (parent_organization.billing_address_id == billing_address_id)
end

#use_parent_billing_address=(val) ⇒ void

This method returns an undefined value.

Parameters:

  • val (String, nil)

    the checkbox param — '1' opts into the parent's billing address



353
354
355
# File 'app/concerns/models/customer_organization.rb', line 353

def use_parent_billing_address=(val)
  self.record_parent_billing_address = ((val == '1') && val.present?)
end

#walmart_ca?Boolean

Whether this customer is the Walmart Canada house account.

Returns:

  • (Boolean)


181
182
183
# File 'app/concerns/models/customer_organization.rb', line 181

def walmart_ca?
  id == CustomerConstants::WALMART_CA_ID
end

#wasn4_or_wat0f?Boolean

Whether this customer is one of the WASN4 / WAT0F Vendor Central accounts.

Returns:

  • (Boolean)


209
210
211
# File 'app/concerns/models/customer_organization.rb', line 209

def wasn4_or_wat0f?
  [CustomerConstants::AMAZON_VENDOR_CENTRAL_WASN4_CUSTOMER_ID, CustomerConstants::AMAZON_VENDOR_CENTRAL_WAT0F_CUSTOMER_ID].include?(id)
end

#wayfair?Boolean

Whether this customer is a Wayfair USA or Canada house account.

Returns:

  • (Boolean)


237
238
239
# File 'app/concerns/models/customer_organization.rb', line 237

def wayfair?
  [CustomerConstants::WAYFAIR_USA_ID, CustomerConstants::WAYFAIR_CA_ID].include?(id)
end

#wayfair_ca?Boolean

Whether this customer is the Wayfair Canada house account.

Returns:

  • (Boolean)


251
252
253
# File 'app/concerns/models/customer_organization.rb', line 251

def wayfair_ca?
  id == CustomerConstants::WAYFAIR_CA_ID
end

#wayfair_usa?Boolean

Whether this customer is the Wayfair USA house account.

Returns:

  • (Boolean)


244
245
246
# File 'app/concerns/models/customer_organization.rb', line 244

def wayfair_usa?
  id == CustomerConstants::WAYFAIR_USA_ID
end

#zoro?Boolean

Whether this customer is the Zoro house account.

Returns:

  • (Boolean)


279
280
281
# File 'app/concerns/models/customer_organization.rb', line 279

def zoro?
  id == CustomerConstants::ZORO_ID
end