Class: AgreementParticipant

Inherits:
ApplicationRecord show all
Includes:
Models::Auditable, Models::LiquidMethods
Defined in:
app/models/agreement_participant.rb

Overview

== Schema Information

Table name: agreement_participants
Database name: primary

id :bigint not null, primary key
preferred_language :string default("en")
reference :string
sign_page_url :string
signed :boolean default(FALSE)
signed_date :datetime
signer_reference :string
created_at :datetime not null
updated_at :datetime not null
agreement_id :integer
email_id :integer
party_id :integer
phone_id :integer

Constant Summary collapse

ROLES =

Roles.

{ 'electrician' => ProfileConstants::ELECTRICIAN,
'installer' => ProfileConstants::INSTALLER,
'iso' => ProfileConstants::UNKNOWN_TRADE,
'store' => ProfileConstants::DEALER,
'homeowner' => ProfileConstants::HOMEOWNER,
'other' => ProfileConstants::UNKNOWN_TRADE }.freeze

Constants included from Models::Auditable

Models::Auditable::ALWAYS_IGNORED

Constants included from Schedulable

Schedulable::SIMPLE_FORM_OPTIONS

Instance Attribute Summary collapse

Belongs to collapse

Methods included from Models::Auditable

#creator, #updater

Has many collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Models::Auditable

#all_skipped_columns, #audit_reference_data, #should_not_save_version, #stamp_record

Methods inherited from ApplicationRecord

ransackable_associations, ransackable_attributes, ransackable_scopes, ransortable_attributes, #to_relation

Methods included from Schedulable

config

Methods included from Models::AfterCommittable

#after_commit

Methods included from Models::EventPublishable

#publish_event

Instance Attribute Details

#contact_nameObject

Returns the value of attribute contact_name.



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

def contact_name
  @contact_name
end

#party_nameObject

Returns the value of attribute party_name.



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

def party_name
  @party_name
end

Class Method Details

.find_participants(_agreement, params = {}) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'app/models/agreement_participant.rb', line 54

def self.find_participants(_agreement, params = {})
  results_array = []
  results = []
  if (search_term = params[:q].presence)
    per_page = (params[:per_page] || 10).to_i
    page = (params[:page] || 1).to_i
    if (cn_term = search_term.scan(Customer::REFERENCE_NUMBER_PATTERN).join.upcase.presence)
      results += Customer.where(id: cn_term).to_a
    elsif !search_term.match?(/[a-zA-Z]/) && (phone = PhoneNumber.parse(search_term)) && phone.valid?
      results = Party.customer_or_contact.joins(:contact_points).merge(ContactPoint.dialable.where(detail: phone.to_s)).limit(per_page).offset((page - 1) * per_page).to_a
    elsif (email = search_term.scan(ContactPoint::EMAIL_REGEXP).join.presence&.downcase)
      results = Party.customer_or_contact.joins(:contact_points).merge(ContactPoint.emails.where(detail: email)).limit(per_page).offset((page - 1) * per_page).to_a
    else # wild card lookup
      results += Party.customer_or_contact.lookup(search_term).limit(per_page).offset((page - 1) * per_page).to_a
    end
    # If we don't have a search term but we have a party_id we can load up suggested participants
  end
  if results.blank? && (party_id = params[:participant_id]).present?
    # Basically all contacts of the customer of that party
    party = Party.customer_or_contact.find(party_id)
    if party.customer
      results = party.customer.contacts.active.order(:full_name)
      results = results.where.not(id: params[:existing_participant_ids]) if params[:existing_participant_ids].present?
      results = results.to_a
    end
  end

  if results.present?
    results_array += results.uniq.map do |party|
      {
        text: party.to_s,
        id: party.id
      }
    end
  end
  results_array = results_array.sort_by { |r| r[:text] }

  total_entries = results.size
  { results: results_array, total: total_entries }
end

Instance Method Details

#agreementAgreement

Returns:

See Also:

Validations:



36
# File 'app/models/agreement_participant.rb', line 36

belongs_to :agreement, inverse_of: :agreement_participants, optional: true

#contact_pointsActiveRecord::Relation<ContactPoint>

Returns:

See Also:



40
# File 'app/models/agreement_participant.rb', line 40

has_many :contact_points, through: :party

#emailContactPoint



37
# File 'app/models/agreement_participant.rb', line 37

belongs_to :email, class_name: 'ContactPoint', inverse_of: :email_agreement_participants, optional: true

#partyParty

Returns:

See Also:

Validations:



35
# File 'app/models/agreement_participant.rb', line 35

belongs_to :party, inverse_of: :agreement_participants, optional: true

#phoneContactPoint



38
# File 'app/models/agreement_participant.rb', line 38

belongs_to :phone, class_name: 'ContactPoint', inverse_of: :phone_agreement_participants, optional: true

#save_partyObject



138
139
140
141
# File 'app/models/agreement_participant.rb', line 138

def save_party
  party&.save
  true
end

#set_partyObject



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'app/models/agreement_participant.rb', line 95

def set_party
  # Store contact points from form
  contact_points = party.contact_points.to_a if party&.new_record?
  contact_points ||= []

  customer_name = party_name.presence || contact_name.presence
  return if customer_name.blank? || role.blank? || party_id.present?

  c = Customer.new(catalog_id: default_catalog_id,
                   profile_id: ROLES[role],
                   source_id: default_source_id)
  if c.is_homeowner?
    pnp = PersonNameParser.new(customer_name)
    pnp.to_party(c)
  else
    c.full_name = customer_name
  end
  con = nil
  if c.valid?
    if contact_name.present? && contact_name != customer_name
      pnp = PersonNameParser.new(contact_name)
      con = Contact.new(customer: c)
      pnp.to_party(con)
      if con.valid?
        # Contact is the party
        self.party = con
      else
        con.errors.full_messages.each { |m| errors.add :contact_name, m }
      end
    else
      # Customer is the party
      self.party = c
    end
  else
    c.errors.full_messages.each { |m| errors.add :party_name, m }
  end

  # Carry over previously set contact points over to this new party
  return if contact_points.blank?

  contact_points.each { |cp| party.contact_points << cp unless party.contact_points.find { |ecp| ecp == cp } }
end