Class: Certification

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

Overview

== Schema Information

Table name: certifications
Database name: primary

id :bigint not null, primary key
attachment_name :string
attachment_uid :string
certified_by :integer
certified_on :datetime
expiration_reminder_10_days_sent :boolean default(FALSE), not null
expiration_reminder_10_days_sent_on :datetime
expiration_reminder_30_days_sent :boolean default(FALSE), not null
expiration_reminder_30_days_sent_on :datetime
expired_reminder_sent :boolean default(FALSE), not null
expired_reminder_sent_on :datetime
expires_at :datetime
reference :string
state :string
title :string
welcome_meeting :boolean default(FALSE), not null
welcome_meeting_date :datetime
workshop_attended :boolean
workshop_attended_date :datetime
created_at :datetime not null
updated_at :datetime not null
course_enrollment_id :integer
party_id :integer

Indexes

idx_party_id (party_id)

Constant Summary

Constants included from Models::Auditable

Models::Auditable::ALWAYS_IGNORED

Belongs to collapse

Methods included from Models::Auditable

#creator, #updater

Has one 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 Models::EventPublishable

#publish_event

Class Method Details

.activeActiveRecord::Relation<Certification>

A relation of Certifications that are active. Active Record Scope

Returns:

See Also:



49
# File 'app/models/certification.rb', line 49

scope :active, -> {where(state: 'active')}

.expiredActiveRecord::Relation<Certification>

A relation of Certifications that are expired. Active Record Scope

Returns:

See Also:



50
# File 'app/models/certification.rb', line 50

scope :expired, -> {where(state: 'expired')}

.requestedActiveRecord::Relation<Certification>

A relation of Certifications that are requested. Active Record Scope

Returns:

See Also:



51
# File 'app/models/certification.rb', line 51

scope :requested, -> {where(state: 'requested')}

.titleObject



192
193
194
# File 'app/models/certification.rb', line 192

def self.title
  title ||= course_enrollment.course&.name
end

Instance Method Details

#agreementAgreement

Returns:

See Also:



45
# File 'app/models/certification.rb', line 45

has_one :agreement, as: :resource

#agreement_ok?Boolean

Returns:

  • (Boolean)


124
125
126
# File 'app/models/certification.rb', line 124

def agreement_ok?
  agreement.present? && agreement.signed?
end

#can_be_activated?Boolean

Returns:

  • (Boolean)


99
100
101
102
103
104
105
106
107
108
109
110
# File 'app/models/certification.rb', line 99

def can_be_activated?
  #There needs to be at least 1 liability insurance active at this moment
  liability = liability_ok?
  #An online welcome meeting needs to happen before being activated
  meeting = welcome_meeting_ok?
  #Pro needs to attend at least one workshop
  workshop = workshop_attended_ok?
  #The agreement needs to be signed and uploaded
  agreement = agreement_ok?

  return liability && meeting && workshop && agreement
end


196
197
198
# File 'app/models/certification.rb', line 196

def certificate_link
  url = "#{WEB_URL}/my_account/certifications/#{self.id}"
end

#contactParty

Returns:

See Also:



43
# File 'app/models/certification.rb', line 43

belongs_to :contact, class_name: 'Party', foreign_key: 'party_id', optional: true, inverse_of: :certifications

#course_enrollmentCourseEnrollment



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

belongs_to :course_enrollment, optional: true

#create_agreementObject



142
143
144
145
146
147
148
149
150
# File 'app/models/certification.rb', line 142

def create_agreement
  agreement = self.build_agreement(sign_method: 'esignatures', template: 'installer_agreement')
  agreement.agreement_participants.new(
    party_id: self.party_id
  )
  if agreement.save
    agreement.send_to_esignatures
  end
end

#create_welcome_meeting_activityObject



216
217
218
219
220
221
222
# File 'app/models/certification.rb', line 216

def create_welcome_meeting_activity
  Activity.create(lock_target_datetime: true,
                  activity_type_id: ActivityType.find_by(task_type: 'INSTALL_CERTIFICATION_PM')&.id,
                  party: self.party,
                  resource: self,
                  notes: "Welcome meeting for new certification request. Please arrange meeting.")
end

#create_workshop_activityObject



208
209
210
211
212
213
214
# File 'app/models/certification.rb', line 208

def create_workshop_activity
  Activity.create(lock_target_datetime: true,
                  activity_type_id: ActivityType.find_by(task_type: 'INSTALL_CERTIFICATION_PWS')&.id,
                  party: self.party,
                  resource: self,
                  notes: "New certification requires workshop to attend. Please schedule and verify this customer attends.")
end

#customerParty

Returns:

See Also:



42
# File 'app/models/certification.rb', line 42

belongs_to :customer, class_name: 'Party', foreign_key: 'party_id', optional: true

#liability_ok?Boolean

Returns:

  • (Boolean)


112
113
114
# File 'app/models/certification.rb', line 112

def liability_ok?
  party.customer.liability_insurances.active.any? && customer.liability_insurances.effective_on(Time.current).present?
end

#partyParty

Returns:

See Also:



41
# File 'app/models/certification.rb', line 41

belongs_to :party, optional: true

#possible_eventsObject



200
201
202
# File 'app/models/certification.rb', line 200

def possible_events
  state_transitions.map(&:event).sort
end

#possible_events_for_selectObject



204
205
206
# File 'app/models/certification.rb', line 204

def possible_events_for_select
  possible_events.map { |evt| [evt.to_s.titleize, evt] }
end

#send_admin_alertObject



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

def send_admin_alert
  InternalMailer.new_certification_request(self).deliver_later!
end

#send_congratulations_emailObject



152
153
154
155
156
157
158
# File 'app/models/certification.rb', line 152

def send_congratulations_email
  customer = self.party
  com = CommunicationBuilder.new(resource: self,
                                  recipient_party: customer,
                                  use_best_email: true,
                                  template_system_code: 'NEW_INSTALLER').create
end

#send_expiration_reminderObject



178
179
180
181
182
183
184
185
186
# File 'app/models/certification.rb', line 178

def send_expiration_reminder
  InternalMailer.certification_expired(self).deliver_later!
  customer = self.party
  com = CommunicationBuilder.new(resource: self,
                                  recipient_party: customer,
                                  use_best_email: true,
                                  template_system_code: 'CERT_EXPIRED').create
  self.update(expired_reminder_sent: true, expired_reminder_sent_on: Time.current)
end

#send_expiration_reminder_10Object



169
170
171
172
173
174
175
176
# File 'app/models/certification.rb', line 169

def send_expiration_reminder_10
  customer = self.party
  com = CommunicationBuilder.new(resource: self,
                                  recipient_party: customer,
                                  use_best_email: true,
                                  template_system_code: 'CERT_EXPIRES_10').create
  self.update(expiration_reminder_10_days_sent: true, expiration_reminder_10_days_sent_on: Time.current)
end

#send_expiration_reminder_30Object



160
161
162
163
164
165
166
167
# File 'app/models/certification.rb', line 160

def send_expiration_reminder_30
  customer = self.party
  com = CommunicationBuilder.new(resource: self,
                                  recipient_party: customer,
                                  use_best_email: true,
                                  template_system_code: 'CERT_EXPIRES_30').create
  self.update(expiration_reminder_30_days_sent: true, expiration_reminder_30_days_sent_on: Time.current)
end

#set_partyObject



136
137
138
139
140
# File 'app/models/certification.rb', line 136

def set_party
  if course_enrollment.party.present? && self.party_id.nil?
    self.party_id = course_enrollment.party.id
  end
end

#set_reference_numberObject



132
133
134
# File 'app/models/certification.rb', line 132

def set_reference_number
  self.update(reference: "CER#{self.id.to_s.rjust(4, '0')}")
end

#set_titleObject



128
129
130
# File 'app/models/certification.rb', line 128

def set_title
  self.title = course_enrollment&.course&.name unless title.present?
end

#welcome_meeting_ok?Boolean

Returns:

  • (Boolean)


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

def welcome_meeting_ok?
  welcome_meeting.to_b
end

#workshop_attended_ok?Boolean

Returns:

  • (Boolean)


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

def workshop_attended_ok?
  workshop_attended.to_b
end