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 Schedulable
Schedulable::SIMPLE_FORM_OPTIONS
Instance Attribute Summary collapse
- #contact_id ⇒ Object readonly
- #end_date ⇒ Object readonly
- #enrollment_date ⇒ Object readonly
-
#make_active ⇒ Object
Returns the value of attribute make_active.
- #notification_email ⇒ Object readonly
- #spiff_id ⇒ Object readonly
- #start_date ⇒ Object 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 Schedulable
Methods included from Models::AfterCommittable
Methods included from Models::EventPublishable
Instance Attribute Details
#contact_id ⇒ Object (readonly)
42 |
# File 'app/models/spiff_enrollment.rb', line 42 validates :spiff_id, :contact_id, :enrollment_date, :start_date, :end_date, presence: true |
#end_date ⇒ Object (readonly)
42 |
# File 'app/models/spiff_enrollment.rb', line 42 validates :spiff_id, :contact_id, :enrollment_date, :start_date, :end_date, presence: true |
#enrollment_date ⇒ Object (readonly)
42 |
# File 'app/models/spiff_enrollment.rb', line 42 validates :spiff_id, :contact_id, :enrollment_date, :start_date, :end_date, presence: true |
#make_active ⇒ Object
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_email ⇒ Object (readonly)
43 |
# File 'app/models/spiff_enrollment.rb', line 43 validates :notification_email, presence: true, on: :create |
#spiff_id ⇒ Object (readonly)
42 |
# File 'app/models/spiff_enrollment.rb', line 42 validates :spiff_id, :contact_id, :enrollment_date, :start_date, :end_date, presence: true |
#start_date ⇒ Object (readonly)
42 |
# File 'app/models/spiff_enrollment.rb', line 42 validates :spiff_id, :contact_id, :enrollment_date, :start_date, :end_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.
62 63 64 |
# File 'app/models/spiff_enrollment.rb', line 62 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.
145 146 147 |
# File 'app/models/spiff_enrollment.rb', line 145 def active_orders orders.active_spiffs.order(:reference_number) end |
#communications ⇒ ActiveRecord::Relation<Communication>
40 |
# File 'app/models/spiff_enrollment.rb', line 40 has_many :communications, -> { order(:id).reverse_order }, as: :resource, dependent: :nullify |
#contact ⇒ Contact
35 |
# File 'app/models/spiff_enrollment.rb', line 35 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.
69 70 71 72 73 |
# File 'app/models/spiff_enrollment.rb', line 69 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.
79 80 81 |
# File 'app/models/spiff_enrollment.rb', line 79 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
37 |
# File 'app/models/spiff_enrollment.rb', line 37 belongs_to :mailing_address, class_name: "Address", optional: true |
#month_end ⇒ Time
Last day of the previous calendar month.
118 119 120 |
# File 'app/models/spiff_enrollment.rb', line 118 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.
124 125 126 |
# File 'app/models/spiff_enrollment.rb', line 124 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).
112 113 114 |
# File 'app/models/spiff_enrollment.rb', line 112 def month_start 1.month.ago.at_beginning_of_month end |
#name ⇒ Object
Alias for Spiff#name
54 |
# File 'app/models/spiff_enrollment.rb', line 54 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.
168 169 170 171 172 173 |
# File 'app/models/spiff_enrollment.rb', line 168 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>
39 |
# File 'app/models/spiff_enrollment.rb', line 39 has_many :orders |
#payable_to ⇒ Party
36 |
# File 'app/models/spiff_enrollment.rb', line 36 belongs_to :payable_to, class_name: "Party", optional: true |
#send_closing_email ⇒ Communication
Trigger the SPIFF_END communication template at end-of-
enrollment.
152 153 154 |
# File 'app/models/spiff_enrollment.rb', line 152 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.
138 139 140 |
# File 'app/models/spiff_enrollment.rb', line 138 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.
92 93 94 95 96 97 98 |
# File 'app/models/spiff_enrollment.rb', line 92 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).
160 161 162 163 |
# File 'app/models/spiff_enrollment.rb', line 160 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).
105 106 107 |
# File 'app/models/spiff_enrollment.rb', line 105 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).
131 132 133 |
# File 'app/models/spiff_enrollment.rb', line 131 def spiff_orders_for_update_email spiff_orders_between_dates(month_start, month_end) end |
#to_s ⇒ String
84 85 86 |
# File 'app/models/spiff_enrollment.rb', line 84 def to_s "#{spiff} #{contact.full_name}" end |