Class: SpiffEnrollment
- Inherits:
-
ApplicationRecord
- Object
- ActiveRecord::Base
- ApplicationRecord
- SpiffEnrollment
- 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
Constants included from Models::Schedulable
Models::Schedulable::SIMPLE_FORM_OPTIONS
Instance Attribute Summary collapse
- #contact_id ⇒ Integer readonly
- #end_date ⇒ Date readonly
- #enrollment_date ⇒ Date readonly
-
#make_active ⇒ Object
Returns the value of attribute make_active.
- #notification_email ⇒ String(255) readonly
- #spiff_id ⇒ Integer readonly
- #start_date ⇒ Date readonly
Belongs to collapse
Methods included from Models::Auditable
Has many collapse
Delegated Instance Attributes collapse
-
#name ⇒ Object
Alias for Spiff#name.
Instance Method Summary collapse
-
#activate_spiff ⇒ void
When the form's "make active" checkbox is set, point the contact's
active_spiff_enrollmentat this enrollment so all subsequent orders attach to it. -
#active_orders ⇒ ActiveRecord::Relation<Order>
All in-window orders attached to the enrollment, ordered by reference number — used by Liquid templates and reports.
-
#deactivate_spiff ⇒ void
If the contact's active enrollment is this one and we just set an
unenrollment_date, clear the active pointer. -
#is_active? ⇒ Boolean
Whether the enrollment is currently earning rewards: not un-enrolled, today is between start/end, and the parent Spiff is still in its run window.
-
#month_end ⇒ Time
Last day of the previous calendar month.
-
#month_name ⇒ String
Full English name of the previous month — for the email subject.
-
#month_start ⇒ Time
First day of the previous calendar month (used as the lower bound on the monthly SPIFF update email).
-
#not_exceeded_maximum_enrollments ⇒ void, false
Validation: when the parent SPIFF caps
max_enrollmentsper contact, refuse to create another beyond that cap. -
#send_closing_email ⇒ Communication
Trigger the
SPIFF_ENDcommunication template at end-of- enrollment. -
#send_monthly_update_email ⇒ Communication
Trigger the
SPIFF_UPDATEcommunication template for the enrollee — typically queued from a monthly cron. -
#send_welcome_email ⇒ void
Trigger the SPIFF welcome email through CommunicationBuilder — fired by
after_create. -
#set_start_and_end_date ⇒ void
Stamp
start_date(today, but not earlier than the SPIFF's ownbegin_date) andend_date(start + enrollment period, falling back to the SPIFF's expiry). -
#spiff_orders_between_dates(from_date, to_date) ⇒ ActiveRecord::Relation<Order>
Orders placed under this enrollment that shipped between the given dates and still count for SPIFF (
active_spiffsscope). -
#spiff_orders_for_update_email ⇒ ActiveRecord::Relation<Order>
Orders that should appear in the monthly update email (previous calendar month, active SPIFF state).
- #to_s ⇒ String
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::Schedulable
Methods included from Models::AfterCommittable
Methods included from Models::EventPublishable
Instance Attribute Details
#contact_id ⇒ Integer (readonly)
50 |
# File 'app/models/spiff_enrollment.rb', line 50 validates :contact_id, presence: true |
#end_date ⇒ Date (readonly)
56 |
# File 'app/models/spiff_enrollment.rb', line 56 validates :end_date, presence: true |
#enrollment_date ⇒ Date (readonly)
52 |
# File 'app/models/spiff_enrollment.rb', line 52 validates :enrollment_date, presence: true |
#make_active ⇒ Object
Returns the value of attribute make_active.
62 63 64 |
# File 'app/models/spiff_enrollment.rb', line 62 def make_active @make_active end |
#notification_email ⇒ String(255) (readonly)
58 |
# File 'app/models/spiff_enrollment.rb', line 58 validates :notification_email, presence: true, on: :create |
#spiff_id ⇒ Integer (readonly)
48 |
# File 'app/models/spiff_enrollment.rb', line 48 validates :spiff_id, presence: true |
#start_date ⇒ Date (readonly)
54 |
# File 'app/models/spiff_enrollment.rb', line 54 validates :start_date, presence: true |
Instance Method Details
#activate_spiff ⇒ void
This method returns an undefined value.
When the form's "make active" checkbox is set, point the
contact's active_spiff_enrollment at this enrollment so all
subsequent orders attach to it.
77 78 79 |
# File 'app/models/spiff_enrollment.rb', line 77 def activate_spiff contact.update!(active_spiff_enrollment: self) if make_active == "1" end |
#active_orders ⇒ ActiveRecord::Relation<Order>
All in-window orders attached to the enrollment, ordered by
reference number — used by Liquid templates and reports.
160 161 162 |
# File 'app/models/spiff_enrollment.rb', line 160 def active_orders orders.active_spiffs.order(:reference_number) end |
#communications ⇒ ActiveRecord::Relation<Communication>
45 |
# File 'app/models/spiff_enrollment.rb', line 45 has_many :communications, -> { order(:id).reverse_order }, as: :resource, dependent: :nullify |
#contact ⇒ Contact?
36 |
# File 'app/models/spiff_enrollment.rb', line 36 belongs_to :contact, optional: true, inverse_of: :spiff_enrollments |
#deactivate_spiff ⇒ void
This method returns an undefined value.
If the contact's active enrollment is this one and we just
set an unenrollment_date, clear the active pointer.
84 85 86 87 88 |
# File 'app/models/spiff_enrollment.rb', line 84 def deactivate_spiff return if unenrollment_date.blank? contact.update!(active_spiff_enrollment: nil) if contact.active_spiff_enrollment_id.present? end |
#is_active? ⇒ Boolean
Whether the enrollment is currently earning rewards: not
un-enrolled, today is between start/end, and the parent
Spiff is still in its run window.
94 95 96 |
# File 'app/models/spiff_enrollment.rb', line 94 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_address ⇒ Address?
40 |
# File 'app/models/spiff_enrollment.rb', line 40 belongs_to :mailing_address, class_name: "Address", optional: true |
#month_end ⇒ Time
Last day of the previous calendar month.
133 134 135 |
# File 'app/models/spiff_enrollment.rb', line 133 def month_end 1.month.ago.at_end_of_month end |
#month_name ⇒ String
Full English name of the previous month — for the email subject.
139 140 141 |
# File 'app/models/spiff_enrollment.rb', line 139 def month_name month_start.strftime("%B") end |
#month_start ⇒ Time
First day of the previous calendar month (used as the lower
bound on the monthly SPIFF update email).
127 128 129 |
# File 'app/models/spiff_enrollment.rb', line 127 def month_start 1.month.ago.at_beginning_of_month end |
#name ⇒ Object
Alias for Spiff#name
69 |
# File 'app/models/spiff_enrollment.rb', line 69 delegate :name, to: :spiff |
#not_exceeded_maximum_enrollments ⇒ void, false
Validation: when the parent SPIFF caps max_enrollments per
contact, refuse to create another beyond that cap.
183 184 185 186 187 188 |
# File 'app/models/spiff_enrollment.rb', line 183 def not_exceeded_maximum_enrollments return unless spiff.max_enrollments && (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." false end |
#orders ⇒ ActiveRecord::Relation<Order>
43 |
# File 'app/models/spiff_enrollment.rb', line 43 has_many :orders |
#payable_to ⇒ Party?
38 |
# File 'app/models/spiff_enrollment.rb', line 38 belongs_to :payable_to, class_name: "Party", optional: true |
#send_closing_email ⇒ Communication
Trigger the SPIFF_END communication template at end-of-
enrollment.
167 168 169 |
# File 'app/models/spiff_enrollment.rb', line 167 def send_closing_email CommunicationBuilder.new(resource: self, template_system_code: 'SPIFF_END').create end |
#send_monthly_update_email ⇒ Communication
Trigger the SPIFF_UPDATE communication template for the
enrollee — typically queued from a monthly cron.
153 154 155 |
# File 'app/models/spiff_enrollment.rb', line 153 def send_monthly_update_email CommunicationBuilder.new(resource: self, template_system_code: 'SPIFF_UPDATE').create end |
#send_welcome_email ⇒ void
This method returns an undefined value.
Trigger the SPIFF welcome email through CommunicationBuilder
— fired by after_create. Logs and skips when no destination
address is set.
107 108 109 110 111 112 113 |
# File 'app/models/spiff_enrollment.rb', line 107 def send_welcome_email if notification_email.present? CommunicationBuilder.new(resource: self).create else Rails.logger.error "Spiff Enrollment #{id} cannot be sent a welcome email, no emails present." end end |
#set_start_and_end_date ⇒ void
This method returns an undefined value.
Stamp start_date (today, but not earlier than the SPIFF's
own begin_date) and end_date (start + enrollment period,
falling back to the SPIFF's expiry).
175 176 177 178 |
# File 'app/models/spiff_enrollment.rb', line 175 def set_start_and_end_date self.start_date = [Date.current, spiff.begin_date].max self.end_date = spiff.enrollment_period.present? ? (start_date + spiff.enrollment_period) : spiff.expire_date end |
#spiff ⇒ Spiff?
34 |
# File 'app/models/spiff_enrollment.rb', line 34 belongs_to :spiff, optional: true |
#spiff_orders_between_dates(from_date, to_date) ⇒ ActiveRecord::Relation<Order>
Orders placed under this enrollment that shipped between the
given dates and still count for SPIFF (active_spiffs scope).
120 121 122 |
# File 'app/models/spiff_enrollment.rb', line 120 def spiff_orders_between_dates(from_date, to_date) orders.active_spiffs.where("shipped_date between ? and ?", from_date, to_date).order(:reference_number) end |
#spiff_orders_for_update_email ⇒ ActiveRecord::Relation<Order>
Orders that should appear in the monthly update email
(previous calendar month, active SPIFF state).
146 147 148 |
# File 'app/models/spiff_enrollment.rb', line 146 def spiff_orders_for_update_email spiff_orders_between_dates(month_start, month_end) end |
#to_s ⇒ String
99 100 101 |
# File 'app/models/spiff_enrollment.rb', line 99 def to_s "#{spiff} #{contact.full_name}" end |