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.
Instance Method Summary collapse
-
#all_addresses ⇒ Array<Address>
All distinct addresses across the contact and its customer.
-
#build_default_contact_points(required = nil) ⇒ Contact
Builds missing default contact points (phone, fax, email).
-
#colleagues ⇒ ActiveRecord::Relation<Contact>
Other contacts belonging to the same customer.
-
#country ⇒ Country?
Country of the contact, falling back to the customer's store country.
-
#first_address ⇒ Address?
First address for the contact, falling back to the customer's.
-
#guest? ⇒ Boolean
Whether this contact is a guest record.
-
#last_invoice_shipping_cost ⇒ Numeric
Shipping cost on the contact's customer's most recent invoice.
-
#locale ⇒ String?
Locale for the contact, inherited from the customer when unset.
-
#main_address ⇒ Address?
Primary address for the contact, falling back to the customer's.
-
#open_activities_counter ⇒ Integer
Number of open, visible-by-default activities for the contact.
-
#options_for_reports_to ⇒ Array<Array(String, Integer)>?
Options for the "reports to" select, active contacts first.
-
#reference_number ⇒ String
CRM reference number for the contact.
-
#shipping_amount_last_30_days ⇒ Numeric
Total shipping amount charged to the contact's customer over the last 30 days.
-
#shipping_amount_last_month ⇒ Numeric
Total shipping amount charged to the contact's customer last month.
-
#state ⇒ String
Human-readable activity state of the contact.
-
#supplier_contact? ⇒ Boolean
Whether this contact belongs to a supplier.
-
#timezone ⇒ ActiveSupport::TimeZone?
Time zone for the contact, falling back to the customer's.
-
#to_label ⇒ String
Label used in pickers and dropdowns.
-
#to_s ⇒ String
Display name combining the contact's name with its customer's.
Instance Method Details
#all_addresses ⇒ Array<Address>
All distinct addresses across the contact and its customer.
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).
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 |
#colleagues ⇒ ActiveRecord::Relation<Contact>
Other contacts belonging to the same customer.
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 |
#country ⇒ Country?
Country of the contact, falling back to 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_address ⇒ Address?
First address for the contact, falling back to the customer's.
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.
147 148 149 |
# File 'app/concerns/models/contact_display.rb', line 147 def guest? false end |
#last_invoice_shipping_cost ⇒ Numeric
Shipping cost on the contact's customer's most recent invoice.
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 |
#locale ⇒ String?
Locale for the contact, inherited from the customer when unset.
33 34 35 |
# File 'app/concerns/models/contact_display.rb', line 33 def locale customer&.locale || super end |
#main_address ⇒ Address?
Primary address for the contact, falling back to the customer's.
84 85 86 |
# File 'app/concerns/models/contact_display.rb', line 84 def main_address addresses.first || customer&.main_address end |
#open_activities_counter ⇒ Integer
Number of open, visible-by-default activities for the contact.
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_to ⇒ Array<Array(String, Integer)>?
Options for the "reports to" select, active contacts first.
136 137 138 139 140 141 142 |
# File 'app/concerns/models/contact_display.rb', line 136 def 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_number ⇒ String
CRM reference number for the contact.
47 48 49 |
# File 'app/concerns/models/contact_display.rb', line 47 def reference_number "CT#{id}" end |
#shipping_amount_last_30_days ⇒ Numeric
Total shipping amount charged to the contact's customer over the last 30 days.
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_month ⇒ Numeric
Total shipping amount charged to the contact's customer last month.
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 |
#state ⇒ String
Human-readable activity state of the contact.
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.
154 155 156 |
# File 'app/concerns/models/contact_display.rb', line 154 def supplier_contact? supplier.present? end |
#timezone ⇒ ActiveSupport::TimeZone?
Time zone for the contact, falling back to the customer's.
70 71 72 |
# File 'app/concerns/models/contact_display.rb', line 70 def timezone ActiveSupport::TimeZone.find_tzinfo(timezone_name) || customer&.timezone end |
#to_label ⇒ String
Label used in pickers and dropdowns.
54 55 56 |
# File 'app/concerns/models/contact_display.rb', line 54 def to_label to_s end |
#to_s ⇒ String
Display name combining the contact's name with its customer's.
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 |