Class: LiabilityInsurance

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

Overview

== Schema Information

Table name: liability_insurances
Database name: primary

id :bigint not null, primary key
attachment_name :string
attachment_uid :string
expiration_date :datetime
expiration_reminder_sent :boolean default(FALSE)
expiration_reminder_sent_on :datetime
expired_alert_sent :boolean default(FALSE)
expired_alert_sent_on :datetime
state :string
valid_from_date :datetime
created_at :datetime not null
updated_at :datetime not null
customer_id :integer

Constant Summary

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

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

#attachmentObject (readonly)



34
# File 'app/models/liability_insurance.rb', line 34

validates :expiration_date, :valid_from_date, :attachment, presence: true

#expiration_dateObject (readonly)



34
# File 'app/models/liability_insurance.rb', line 34

validates :expiration_date, :valid_from_date, :attachment, presence: true

#valid_from_dateObject (readonly)



34
# File 'app/models/liability_insurance.rb', line 34

validates :expiration_date, :valid_from_date, :attachment, presence: true

Class Method Details

.activeActiveRecord::Relation<LiabilityInsurance>

A relation of LiabilityInsurances that are active. Active Record Scope

Returns:

See Also:



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

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

.effective_nowActiveRecord::Relation<LiabilityInsurance>

A relation of LiabilityInsurances that are effective now. Active Record Scope

Returns:

See Also:



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

scope :effective_now, -> { active.effective_on(Time.current) }

.effective_onActiveRecord::Relation<LiabilityInsurance>

A relation of LiabilityInsurances that are effective on. Active Record Scope

Returns:

See Also:



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

scope :effective_on, ->(date) { where("valid_from_date <= ? and expiration_date >= ?", date || Time.current, date || Time.current) }

.requestedActiveRecord::Relation<LiabilityInsurance>

A relation of LiabilityInsurances that are requested. Active Record Scope

Returns:

See Also:



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

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

Instance Method Details

#customerCustomer

Returns:

See Also:



28
# File 'app/models/liability_insurance.rb', line 28

belongs_to :customer, optional: true

#has_valid_date_rangeObject



86
87
88
89
90
91
# File 'app/models/liability_insurance.rb', line 86

def has_valid_date_range
  # Check dates are in order
  return unless valid_from_date && expiration_date

  errors.add(:base, "date range is invalid") if valid_from_date > expiration_date
end

#is_valid?Boolean

Returns:

  • (Boolean)


78
79
80
# File 'app/models/liability_insurance.rb', line 78

def is_valid?
  Date.current.between?(valid_from_date, expiration_date)
end


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

def liability_insurance_link
  "#{WEB_URL}/my_account/liability_insurances"
end

#notify_new_uploadObject



134
135
136
# File 'app/models/liability_insurance.rb', line 134

def notify_new_upload
  TrainingMailer.new_liability_insurance_card(self).deliver_later
end

#only_one_activeObject



93
94
95
# File 'app/models/liability_insurance.rb', line 93

def only_one_active
  errors.add(:base, "there is currently another insurance card active and valid") if state == 'active' && is_valid?
end

#partyParty

Returns:

See Also:



27
# File 'app/models/liability_insurance.rb', line 27

belongs_to :party, foreign_key: 'customer_id', optional: true

#periodObject



82
83
84
# File 'app/models/liability_insurance.rb', line 82

def period
  valid_from_date..expiration_date
end

#possible_eventsObject



97
98
99
# File 'app/models/liability_insurance.rb', line 97

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

#possible_events_for_selectObject



101
102
103
# File 'app/models/liability_insurance.rb', line 101

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

#process_expired_certificationObject



105
106
107
108
109
110
111
112
113
114
115
116
# File 'app/models/liability_insurance.rb', line 105

def process_expired_certification
  # This comes from an after_transition. If the customer has another liability insurance active at the moment, everything is fine and we
  # don't need to expire any certifications, otherwise, we have to expire any active certification because they wouldn't be protected
  # under an active insurance.
  return if customer.liability_insurances.active.present?

  send_expired_notice
  TrainingMailer.expired_liability_insurance_card(self).deliver_later
  # customer.certifications.active.each do |cert|
  #   cert.expire!
  # end
end

#send_expiration_coming_alertObject



126
127
128
129
130
131
132
# File 'app/models/liability_insurance.rb', line 126

def send_expiration_coming_alert
  CommunicationBuilder.new(resource: self,
                                  recipient_party: customer,
                                  use_best_email: true,
                                  template_system_code: 'LIAB_INSUR_EXPIRES10').create
  update(expiration_reminder_sent: true, expiration_reminder_sent_on: Time.current)
end

#send_expired_noticeObject



118
119
120
121
122
123
124
# File 'app/models/liability_insurance.rb', line 118

def send_expired_notice
  CommunicationBuilder.new(resource: self,
                                  recipient_party: customer,
                                  use_best_email: true,
                                  template_system_code: 'LIAB_INSUR_EXPIRED').create
  update(expired_alert_sent: true, expired_alert_sent_on: Time.current)
end