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

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

#publish_event

Class Method Details

.activeActiveRecord::Relation<LiabilityInsurance>

A relation of LiabilityInsurances that are active. Active Record Scope

Returns:

See Also:



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

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

.effective_nowActiveRecord::Relation<LiabilityInsurance>

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

Returns:

See Also:



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

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:



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

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

.requestedActiveRecord::Relation<LiabilityInsurance>

A relation of LiabilityInsurances that are requested. Active Record Scope

Returns:

See Also:



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

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

Instance Method Details

#customerCustomer

Returns:

See Also:



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

belongs_to :customer, optional: true

#has_valid_date_rangeObject



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

def has_valid_date_range
  #Check dates are in order
  if self.valid_from_date && self.expiration_date
    if self.valid_from_date > self.expiration_date
      errors.add(:base, "date range is invalid")
    end
  end
end

#is_valid?Boolean

Returns:

  • (Boolean)


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

def is_valid?
  valid_from_date <= Date.current && expiration_date >= Date.current
end


141
142
143
# File 'app/models/liability_insurance.rb', line 141

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

#notify_new_uploadObject



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

def notify_new_upload
  InternalMailer.new_liability_insurance_card(self).deliver_later
end

#only_one_activeObject



94
95
96
97
98
# File 'app/models/liability_insurance.rb', line 94

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

#partyParty

Returns:

See Also:



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

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

#periodObject



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

def period
  valid_from_date..expiration_date
end

#possible_eventsObject



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

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

#possible_events_for_selectObject



104
105
106
# File 'app/models/liability_insurance.rb', line 104

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

#process_expired_certificationObject



108
109
110
111
112
113
114
115
116
117
118
119
# File 'app/models/liability_insurance.rb', line 108

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.
  unless customer.liability_insurances.active.present?
    self.send_expired_notice
    InternalMailer.expired_liability_insurance_card(self).deliver_later
    # customer.certifications.active.each do |cert|
    #   cert.expire!
    # end
  end
end

#send_expiration_coming_alertObject



129
130
131
132
133
134
135
# File 'app/models/liability_insurance.rb', line 129

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

#send_expired_noticeObject



121
122
123
124
125
126
127
# File 'app/models/liability_insurance.rb', line 121

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