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

Constants included from Schedulable

Schedulable::SIMPLE_FORM_OPTIONS

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 Schedulable

config

Methods included from Models::AfterCommittable

#after_commit

Methods included from Models::EventPublishable

#publish_event

Instance Attribute Details

#contact_idObject (readonly)



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

validates :spiff_id, :contact_id, :enrollment_date, :start_date, :end_date, presence: true

#end_dateObject (readonly)



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

validates :spiff_id, :contact_id, :enrollment_date, :start_date, :end_date, presence: true

#enrollment_dateObject (readonly)



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

validates :spiff_id, :contact_id, :enrollment_date, :start_date, :end_date, presence: true

#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

#spiff_idObject (readonly)



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

validates :spiff_id, :contact_id, :enrollment_date, :start_date, :end_date, presence: true

#start_dateObject (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_spiffvoid

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_ordersActiveRecord::Relation<Order>

All in-window orders attached to the enrollment, ordered by
reference number — used by Liquid templates and reports.

Returns:

  • (ActiveRecord::Relation<Order>)


145
146
147
# File 'app/models/spiff_enrollment.rb', line 145

def active_orders
  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_spiffvoid

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.

Returns:

  • (Boolean)


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_addressAddress

Returns:

See Also:



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

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

#month_endTime

Last day of the previous calendar month.

Returns:

  • (Time)


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

def month_end
  1.month.ago.at_end_of_month
end

#month_nameString

Full English name of the previous month — for the email subject.

Returns:

  • (String)


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

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

#month_startTime

First day of the previous calendar month (used as the lower
bound on the monthly SPIFF update email).

Returns:

  • (Time)


112
113
114
# File 'app/models/spiff_enrollment.rb', line 112

def month_start
  1.month.ago.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_enrollmentsvoid, false

Validation: when the parent SPIFF caps max_enrollments per
contact, refuse to create another beyond that cap.

Returns:

  • (void, false)


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

#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_emailCommunication

Trigger the SPIFF_END communication template at end-of-
enrollment.

Returns:



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_emailCommunication

Trigger the SPIFF_UPDATE communication template for the
enrollee — typically queued from a monthly cron.

Returns:



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_emailvoid

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_datevoid

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

#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) ⇒ ActiveRecord::Relation<Order>

Orders placed under this enrollment that shipped between the
given dates and still count for SPIFF (active_spiffs scope).

Parameters:

  • from_date (Date)
  • to_date (Date)

Returns:

  • (ActiveRecord::Relation<Order>)


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_emailActiveRecord::Relation<Order>

Orders that should appear in the monthly update email
(previous calendar month, active SPIFF state).

Returns:

  • (ActiveRecord::Relation<Order>)


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_sString

Returns:

  • (String)


84
85
86
# File 'app/models/spiff_enrollment.rb', line 84

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