Class: SpiffEnrollment
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
Models::Auditable::ALWAYS_IGNORED
Instance Attribute Summary collapse
#creator, #updater
Delegated Instance Attributes
collapse
Instance Method Summary
collapse
#all_skipped_columns, #audit_reference_data, #should_not_save_version, #stamp_record
ransackable_associations, ransackable_attributes, ransackable_scopes, ransortable_attributes, #to_relation
#publish_event
Instance Attribute Details
#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
43
|
# File 'app/models/spiff_enrollment.rb', line 43
validates :notification_email, presence: true, on: :create
|
Instance Method Details
#activate_spiff ⇒ Object
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_orders ⇒ Object
110
111
112
|
# File 'app/models/spiff_enrollment.rb', line 110
def active_orders
self.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
|
35
|
# File 'app/models/spiff_enrollment.rb', line 35
belongs_to :contact, optional: true, inverse_of: :spiff_enrollments
|
#deactivate_spiff ⇒ Object
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
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_address ⇒ Address
37
|
# File 'app/models/spiff_enrollment.rb', line 37
belongs_to :mailing_address, :class_name => "Address", optional: true
|
#month_end ⇒ Object
94
95
96
|
# File 'app/models/spiff_enrollment.rb', line 94
def month_end
(Time.current - 1.month).at_end_of_month
end
|
#month_name ⇒ Object
98
99
100
|
# File 'app/models/spiff_enrollment.rb', line 98
def month_name
month_start.strftime("%B")
end
|
#month_start ⇒ Object
90
91
92
|
# File 'app/models/spiff_enrollment.rb', line 90
def month_start
(Time.current - 1.month).at_beginning_of_month
end
|
#name ⇒ Object
54
|
# File 'app/models/spiff_enrollment.rb', line 54
delegate :name, :to => :spiff
|
#not_exceeded_maximum_enrollments ⇒ Object
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
|
#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 ⇒ Object
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_email ⇒ Object
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_email ⇒ Object
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_date ⇒ Object
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
|
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_email ⇒ Object
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_s ⇒ Object
74
75
76
|
# File 'app/models/spiff_enrollment.rb', line 74
def to_s
"#{self.spiff} #{self.contact.full_name}"
end
|