Class: Spiff

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

Overview

== Schema Information

Table name: spiffs
Database name: primary

id :integer not null, primary key
begin_date :date
buying_group_ids :integer is an Array
catalog_ids :integer is an Array
enrollment_period :integer
expire_date :date
max_enrollments :integer
name :string(255)
survey_url :string(255)
created_at :datetime
updated_at :datetime

Constant Summary

Constants included from Models::Auditable

Models::Auditable::ALWAYS_IGNORED

Instance Attribute Summary collapse

Has many collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Models::Auditable

#all_skipped_columns, #audit_reference_data, #creator, #should_not_save_version, #stamp_record, #updater

Methods inherited from ApplicationRecord

ransackable_associations, ransackable_attributes, ransackable_scopes, ransortable_attributes, #to_relation

Methods included from Models::EventPublishable

#publish_event

Instance Attribute Details

#begin_dateObject (readonly)



22
# File 'app/models/spiff.rb', line 22

validates :name, :begin_date, :expire_date, presence: true

#expire_dateObject (readonly)



22
# File 'app/models/spiff.rb', line 22

validates :name, :begin_date, :expire_date, presence: true

#nameObject (readonly)



22
# File 'app/models/spiff.rb', line 22

validates :name, :begin_date, :expire_date, presence: true

Class Method Details

.activeActiveRecord::Relation<Spiff>

A relation of Spiffs that are active. Active Record Scope

Returns:

  • (ActiveRecord::Relation<Spiff>)

See Also:



30
# File 'app/models/spiff.rb', line 30

scope :active, -> { where("spiffs.begin_date <= '#{Date.current}' and spiffs.expire_date >= '#{Date.current}'") }

.active_or_recentActiveRecord::Relation<Spiff>

A relation of Spiffs that are active or recent. Active Record Scope

Returns:

  • (ActiveRecord::Relation<Spiff>)

See Also:



29
# File 'app/models/spiff.rb', line 29

scope :active_or_recent, -> { where("spiffs.expire_date >= '#{Date.current - 1.month}'") }

.active_spiff_select(buying_group_id) ⇒ Object



68
69
70
# File 'app/models/spiff.rb', line 68

def self.active_spiff_select(buying_group_id)
  Spiff.active.where("buying_group_ids like '%#{buying_group_id}%'").map { |n| [n.name, n.id] }
end

.enrollment_period_optionsObject



55
56
57
# File 'app/models/spiff.rb', line 55

def self.enrollment_period_options
  [['30 days', 30], ['60 days', 60], ['90 days', 90]]
end

.for_buying_group_idActiveRecord::Relation<Spiff>

A relation of Spiffs that are for buying group id. Active Record Scope

Returns:

  • (ActiveRecord::Relation<Spiff>)

See Also:



38
# File 'app/models/spiff.rb', line 38

scope :for_buying_group_id, ->(buying_group_id) { where.any(buying_group_ids: buying_group_id).or(where('cardinality(buying_group_ids) = 0')) }

.for_catalog_idActiveRecord::Relation<Spiff>

A relation of Spiffs that are for catalog id. Active Record Scope

Returns:

  • (ActiveRecord::Relation<Spiff>)

See Also:



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

scope :for_catalog_id, ->(catalog_id) { where.any(catalog_ids: catalog_id).or(where('cardinality(catalog_ids) = 0')) }

.in_useActiveRecord::Relation<Spiff>

A relation of Spiffs that are in use. Active Record Scope

Returns:

  • (ActiveRecord::Relation<Spiff>)

See Also:



32
# File 'app/models/spiff.rb', line 32

scope :in_use, -> { where('id in (select distinct(p.spiff_id) from parties p where p.spiff_id is not null)') }

.not_buying_group_specificActiveRecord::Relation<Spiff>

A relation of Spiffs that are not buying group specific. Active Record Scope

Returns:

  • (ActiveRecord::Relation<Spiff>)

See Also:



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

scope :not_buying_group_specific, -> { where(buying_group_ids: []) }

.not_catalog_specificActiveRecord::Relation<Spiff>

A relation of Spiffs that are not catalog specific. Active Record Scope

Returns:

  • (ActiveRecord::Relation<Spiff>)

See Also:



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

scope :not_catalog_specific, -> { where(catalog_ids: []) }

.not_expiredActiveRecord::Relation<Spiff>

A relation of Spiffs that are not expired. Active Record Scope

Returns:

  • (ActiveRecord::Relation<Spiff>)

See Also:



31
# File 'app/models/spiff.rb', line 31

scope :not_expired, -> { where("spiffs.expire_date >= '#{Date.current}'") }

.recently_endedActiveRecord::Relation<Spiff>

A relation of Spiffs that are recently ended. Active Record Scope

Returns:

  • (ActiveRecord::Relation<Spiff>)

See Also:



33
34
35
# File 'app/models/spiff.rb', line 33

scope :recently_ended, -> {
  where("spiffs.expire_date <= '#{Date.current}' and spiffs.expire_date >= '#{(Date.current - 1.month).at_beginning_of_month}'")
}

.select_optionsObject



64
65
66
# File 'app/models/spiff.rb', line 64

def self.select_options
  Spiff.all.order(:name).map { |n| ["#{n.name} (#{n.begin_date.to_fs(:crm_date_only)} - #{n.expire_date.to_fs(:crm_date_only)})", n.id] }
end

.select_options_for_contact(contact, include_spiff_id) ⇒ Object



72
73
74
75
76
# File 'app/models/spiff.rb', line 72

def self.select_options_for_contact(contact, include_spiff_id)
  scope = valid_for_contact(contact).to_a
  scope << Spiff.find(include_spiff_id) if include_spiff_id
  scope.uniq.sort.map { |s| [s.name_with_period, s.id] }
end

.valid_for_contact(contact) ⇒ Object



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

def self.valid_for_contact(contact)
  customer = contact.customer
  valid_for_customer(customer)
end

.valid_for_customer(customer) ⇒ Object



49
50
51
52
53
# File 'app/models/spiff.rb', line 49

def self.valid_for_customer(customer)
  spiffs = not_expired.for_catalog_id(customer.catalog_id)
  spiffs = spiffs.for_buying_group_id(customer.buying_group_id) if customer.buying_group_id
  spiffs
end

Instance Method Details

#contactsActiveRecord::Relation<Contact>

Returns:

  • (ActiveRecord::Relation<Contact>)

See Also:



24
# File 'app/models/spiff.rb', line 24

has_many :contacts

#eligible_reward(itemizable) ⇒ Object



104
105
106
107
108
109
110
# File 'app/models/spiff.rb', line 104

def eligible_reward(itemizable)
  reward = BigDecimal('0')
  spiff_rewards.each do |r|
    reward += r.eligible_reward(itemizable)
  end
  reward
end

#eligible_rewards(itemizable) ⇒ Object



112
113
114
115
116
117
118
# File 'app/models/spiff.rb', line 112

def eligible_rewards(itemizable)
  rewards = {}
  spiff_rewards.each do |r|
    rewards[r.name] = r.eligible_reward(itemizable) if r.eligible_reward(itemizable) > 0
  end
  rewards
end

#expired?Boolean

Returns:

  • (Boolean)


89
90
91
# File 'app/models/spiff.rb', line 89

def expired?
  expire_date && expire_date <= Date.current
end

#get_bg_short_nameObject



128
129
130
131
132
133
134
# File 'app/models/spiff.rb', line 128

def get_bg_short_name
  return nil unless buying_group_ids.present?

  bg = BuyingGroup.find(buying_group_ids.first)
  y = Object.new.extend(ActionView::Helpers::TextHelper)
  y.truncate(bg.name.delete(' '), length: 7, omission: '')
end

#invoice_number(order) ⇒ Object



120
121
122
# File 'app/models/spiff.rb', line 120

def invoice_number(order)
  ['SPIFF', get_bg_short_name, begin_date.strftime('%m%y'), order.reference_number].compact.join('-')
end

#is_active?Boolean

Returns:

  • (Boolean)


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

def is_active?
  expire_date >= Date.current
end

#name_with_periodObject



97
98
99
100
101
102
# File 'app/models/spiff.rb', line 97

def name_with_period
  res = name
  res << " (#{enrollment_period} day period)" if enrollment_period.present?
  res << ' (expired)' if expired?
  res
end

#ordersActiveRecord::Relation<Order>

Returns:

  • (ActiveRecord::Relation<Order>)

See Also:



27
# File 'app/models/spiff.rb', line 27

has_many :orders, through: :spiff_enrollments

#spiff_enrollmentsActiveRecord::Relation<SpiffEnrollment>

Returns:

See Also:



26
# File 'app/models/spiff.rb', line 26

has_many :spiff_enrollments

#spiff_rewardsActiveRecord::Relation<SpiffReward>

Returns:

See Also:



25
# File 'app/models/spiff.rb', line 25

has_many :spiff_rewards, dependent: :destroy

#to_sObject



93
94
95
# File 'app/models/spiff.rb', line 93

def to_s
  "#{name} [#{id}]"
end

#total_revenue(spiff_state = %w[awaiting_payment paid])) ⇒ Object



78
79
80
81
# File 'app/models/spiff.rb', line 78

def total_revenue(spiff_state = %w[awaiting_payment paid])
  spiff_enrollment_ids = spiff_enrollments.collect { |e| e.id }
  Order.where(spiff_enrollment_id: spiff_enrollment_ids, spiff_state: spiff_state).sum(:total)
end

#total_rewards(spiff_state = %w[awaiting_payment paid])) ⇒ Object



83
84
85
86
87
# File 'app/models/spiff.rb', line 83

def total_rewards(spiff_state = %w[awaiting_payment paid])
  spiff_enrollment_ids = spiff_enrollments.collect { |e| e.id }
  orders = Order.where(spiff_enrollment_id: spiff_enrollment_ids, spiff_state: spiff_state)
  orders.to_a.sum { |o| o.spiff_enrollment.spiff.eligible_reward(o) }
end