Class: SpiffEnrollment

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

Overview

== Schema Information

Table name: spiff_enrollments
Database name: primary

id :integer not null, primary key
end_date :date
enrollment_date :date
notification_email :string(255)
start_date :date
unenrollment_date :date
created_at :datetime
updated_at :datetime
contact_id :integer
creator_id :integer
mailing_address_id :integer
payable_to_id :integer
spiff_id :integer
updater_id :integer

Indexes

spiff_enrollments_contact_id_idx (contact_id)

Foreign Keys

fk_rails_... (contact_id => parties.id)

Constant Summary

Constants included from Models::Auditable

Models::Auditable::ALWAYS_IGNORED

Instance Attribute Summary collapse

Belongs to collapse

Methods included from Models::Auditable

#creator, #updater

Has many collapse

Delegated Instance Attributes 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

Instance Attribute Details

#make_activeObject

Returns the value of attribute make_active.



47
48
49
# File 'app/models/spiff_enrollment.rb', line 47

def make_active
  @make_active
end

#notification_emailObject (readonly)



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

validates :notification_email, presence: true, on: :create

Instance Method Details

#activate_spiffObject



58
59
60
61
62
# File 'app/models/spiff_enrollment.rb', line 58

def activate_spiff
  if self.make_active == "1"
    self.contact.update!(:active_spiff_enrollment => self)
  end
end

#active_ordersObject



110
111
112
# File 'app/models/spiff_enrollment.rb', line 110

def active_orders
  self.orders.active_spiffs.order("reference_number")
end

#communicationsActiveRecord::Relation<Communication>

Returns:

See Also:



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

has_many :communications, -> { order(:id).reverse_order }, :as => :resource, :dependent => :nullify

#contactContact

Returns:

See Also:



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

belongs_to :contact, optional: true, inverse_of: :spiff_enrollments

#deactivate_spiffObject



64
65
66
67
68
# File 'app/models/spiff_enrollment.rb', line 64

def deactivate_spiff
  if self.unenrollment_date.present?
    self.contact.update!(:active_spiff_enrollment => nil) if self.contact.active_spiff_enrollment_id.present?
  end
end

#is_active?Boolean

Returns:

  • (Boolean)


70
71
72
# File 'app/models/spiff_enrollment.rb', line 70

def is_active?
  unenrollment_date.nil? and start_date and start_date <= Date.current and end_date and end_date >= Date.current and spiff.begin_date <= Date.current and spiff.expire_date >= Date.current
end

#mailing_addressAddress

Returns:

See Also:



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

belongs_to :mailing_address, :class_name => "Address", optional: true

#month_endObject



94
95
96
# File 'app/models/spiff_enrollment.rb', line 94

def month_end
  (Time.current - 1.month).at_end_of_month
end

#month_nameObject



98
99
100
# File 'app/models/spiff_enrollment.rb', line 98

def month_name
  month_start.strftime("%B")
end

#month_startObject



90
91
92
# File 'app/models/spiff_enrollment.rb', line 90

def month_start
  (Time.current - 1.month).at_beginning_of_month
end

#nameObject

Alias for Spiff#name

Returns:

  • (Object)

    Spiff#name

See Also:



54
# File 'app/models/spiff_enrollment.rb', line 54

delegate :name, :to => :spiff

#not_exceeded_maximum_enrollmentsObject



123
124
125
126
127
128
# File 'app/models/spiff_enrollment.rb', line 123

def not_exceeded_maximum_enrollments
  if spiff.max_enrollments and contact.spiff_enrollments.where(:spiff_id => spiff_id).count >= spiff.max_enrollments
    errors.add :base, "Contact already has #{spiff.max_enrollments} enrollments on this spiff, no more allowed."
    return false
  end
end

#ordersActiveRecord::Relation<Order>

Returns:

  • (ActiveRecord::Relation<Order>)

See Also:



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

has_many :orders

#payable_toParty

Returns:

See Also:



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

belongs_to :payable_to, :class_name => "Party", optional: true

#send_closing_emailObject



114
115
116
# File 'app/models/spiff_enrollment.rb', line 114

def send_closing_email
  com = CommunicationBuilder.new(resource: self, template_system_code: 'SPIFF_END').create
end

#send_monthly_update_emailObject



106
107
108
# File 'app/models/spiff_enrollment.rb', line 106

def send_monthly_update_email
  com = CommunicationBuilder.new(resource: self, template_system_code: 'SPIFF_UPDATE').create
end

#send_welcome_emailObject



78
79
80
81
82
83
84
# File 'app/models/spiff_enrollment.rb', line 78

def send_welcome_email
  if self.notification_email.present?
    comm = CommunicationBuilder.new(resource: self).create
  else
    Rails.logger.error "Spiff Enrollment #{self.id} cannot be sent a welcome email, no emails present."
  end
end

#set_start_and_end_dateObject



118
119
120
121
# File 'app/models/spiff_enrollment.rb', line 118

def set_start_and_end_date
  self.start_date = [Date.current, spiff.begin_date].max
  self.end_date = spiff.enrollment_period.present? ? (self.start_date + spiff.enrollment_period) : spiff.expire_date
end

#spiffSpiff

Returns:

See Also:



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

belongs_to :spiff, optional: true

#spiff_orders_between_dates(from_date, to_date) ⇒ Object



86
87
88
# File 'app/models/spiff_enrollment.rb', line 86

def spiff_orders_between_dates(from_date, to_date)
  self.orders.active_spiffs.where("shipped_date between ? and ?", from_date, to_date).order("reference_number")
end

#spiff_orders_for_update_emailObject



102
103
104
# File 'app/models/spiff_enrollment.rb', line 102

def spiff_orders_for_update_email
  spiff_orders_between_dates(month_start, month_end)
end

#to_sObject



74
75
76
# File 'app/models/spiff_enrollment.rb', line 74

def to_s
  "#{self.spiff} #{self.contact.full_name}"
end