Module: Models::ContactDisplay

Extended by:
ActiveSupport::Concern
Included in:
Contact
Defined in:
app/concerns/models/contact_display.rb

Overview

Display helpers, formatting, CRM links, and lookup methods for Contact.

See Also:

Instance Method Summary collapse

Instance Method Details

#all_addressesArray<Address>

All distinct addresses across the contact and its customer.

Returns:

  • (Array<Address>)

    the combined, deduplicated addresses



98
99
100
101
102
103
# File 'app/concerns/models/contact_display.rb', line 98

def all_addresses
  addrs = []
  addrs.concat(addresses)
  addrs.concat(customer.all_addresses)
  addrs.uniq
end

#build_default_contact_points(required = nil) ⇒ Contact

Builds missing default contact points (phone, fax, email).

Parameters:

  • required (String, nil) (defaults to: nil)

    category to mark as required

Returns:



110
111
112
113
114
115
# File 'app/concerns/models/contact_display.rb', line 110

def build_default_contact_points(required = nil)
  %w[phone fax email].each do |cat|
    contact_points.build(category: cat, required: required == cat ? '1' : nil) if contact_points.none? { |cp| cp.category == cat }
  end
  self
end

#colleaguesActiveRecord::Relation<Contact>

Other contacts belonging to the same customer.

Returns:

  • (ActiveRecord::Relation<Contact>)

    the customer's contacts excluding this one



127
128
129
130
131
# File 'app/concerns/models/contact_display.rb', line 127

def colleagues
  return Contact.none unless customer

  customer.contacts.where.not(id:)
end

#countryCountry?

Country of the contact, falling back to the customer's store country.

Returns:

  • (Country, nil)

    the record's own country or the customer's store country



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

def country
  super || customer&.catalog&.store&.country
end

#first_addressAddress?

First address for the contact, falling back to the customer's.

Returns:

  • (Address, nil)

    the record's own first address or the customer's first address



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

def first_address
  super || customer&.first_address
end

#guest?Boolean

Whether this contact is a guest record.

Returns:

  • (Boolean)

    always false for contacts



147
148
149
# File 'app/concerns/models/contact_display.rb', line 147

def guest?
  false
end

#last_invoice_shipping_costNumeric

Shipping cost on the contact's customer's most recent invoice.

Returns:

  • (Numeric)

    the last invoice shipping cost, or 0 without a customer



26
27
28
# File 'app/concerns/models/contact_display.rb', line 26

def last_invoice_shipping_cost
  customer.nil? ? 0 : customer.last_invoice_shipping_cost
end

#localeString?

Locale for the contact, inherited from the customer when unset.

Returns:

  • (String, nil)

    the customer's locale or the record's own locale



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

def locale
  customer&.locale || super
end

#main_addressAddress?

Primary address for the contact, falling back to the customer's.

Returns:

  • (Address, nil)

    the contact's first address or the customer's main address



84
85
86
# File 'app/concerns/models/contact_display.rb', line 84

def main_address
  addresses.first || customer&.main_address
end

#open_activities_counterInteger

Number of open, visible-by-default activities for the contact.

Returns:

  • (Integer)

    the open activities count



120
121
122
# File 'app/concerns/models/contact_display.rb', line 120

def open_activities_counter
  all_activities.open_activities.visible_by_default.size
end

#options_for_reports_toArray<Array(String, Integer)>?

Options for the "reports to" select, active contacts first.

Returns:

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

    [label, id] pairs, or nil without a customer/supplier



136
137
138
139
140
141
142
# File 'app/concerns/models/contact_display.rb', line 136

def options_for_reports_to
  res = customer || supplier
  return if res.nil?

  contacts = res.contacts.where(Contact[:id].not_eq(id.to_i)).order(:inactive).reverse_order
  contacts.map { |c| [(c.inactive? ? "#{c.full_name} (inactive)" : c.full_name), c.id] }
end

#reference_numberString

CRM reference number for the contact.

Returns:

  • (String)

    the contact id prefixed with "CT"



47
48
49
# File 'app/concerns/models/contact_display.rb', line 47

def reference_number
  "CT#{id}"
end

#shipping_amount_last_30_daysNumeric

Total shipping amount charged to the contact's customer over the last 30 days.

Returns:

  • (Numeric)

    the customer's 30-day shipping amount, or 0 without a customer



19
20
21
# File 'app/concerns/models/contact_display.rb', line 19

def shipping_amount_last_30_days
  customer.nil? ? 0 : customer.shipping_amount_last_30_days
end

#shipping_amount_last_monthNumeric

Total shipping amount charged to the contact's customer last month.

Returns:

  • (Numeric)

    the customer's last-month shipping amount, or 0 without a customer



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

def shipping_amount_last_month
  customer.nil? ? 0 : customer.shipping_amount_last_month
end

#stateString

Human-readable activity state of the contact.

Returns:

  • (String)

    "inactive" or "active"



40
41
42
# File 'app/concerns/models/contact_display.rb', line 40

def state
  inactive ? 'inactive' : 'active'
end

#supplier_contact?Boolean

Whether this contact belongs to a supplier.

Returns:

  • (Boolean)

    true when a supplier is present



154
155
156
# File 'app/concerns/models/contact_display.rb', line 154

def supplier_contact?
  supplier.present?
end

#timezoneActiveSupport::TimeZone?

Time zone for the contact, falling back to the customer's.

Returns:

  • (ActiveSupport::TimeZone, nil)

    the tzinfo zone for +timezone_name+, or the customer's zone



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

def timezone
  ActiveSupport::TimeZone.find_tzinfo(timezone_name) || customer&.timezone
end

#to_labelString

Label used in pickers and dropdowns.

Returns:

  • (String)

    same as #to_s



54
55
56
# File 'app/concerns/models/contact_display.rb', line 54

def to_label
  to_s
end

#to_sString

Display name combining the contact's name with its customer's.

Returns:

  • (String)

    "Contact Name - Customer Name [CU123]" or just the contact's name



61
62
63
64
65
# File 'app/concerns/models/contact_display.rb', line 61

def to_s
  label_parts = [full_name]
  label_parts << "#{customer.full_name} [#{customer.reference_number}]" if customer
  label_parts.join(' - ')
end