Class: SpiffReward

Inherits:
ApplicationRecord show all
Includes:
Models::Auditable
Defined in:
app/models/spiff_reward.rb

Overview

== Schema Information

Table name: spiff_rewards
Database name: primary

id :integer not null, primary key
name :string(255)
created_at :datetime
updated_at :datetime
spiff_id :integer

Constant Summary

Constants included from Models::Auditable

Models::Auditable::ALWAYS_IGNORED

Constants included from Schedulable

Schedulable::SIMPLE_FORM_OPTIONS

Belongs to collapse

Methods included from Models::Auditable

#creator, #updater

Has many 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 Method Details

#eligible_amount(itemizable) ⇒ Object



56
57
58
# File 'app/models/spiff_reward.rb', line 56

def eligible_amount(itemizable)
  get_line_items(itemizable).sum(&:total)
end

#eligible_reward(itemizable) ⇒ Object



60
61
62
63
64
65
66
67
# File 'app/models/spiff_reward.rb', line 60

def eligible_reward(itemizable)
  amount = eligible_amount(itemizable)
  reward = BigDecimal("0")
  spiff_reward_thresholds.each do |t|
    reward = t.reward_amount if amount.positive? && (amount >= t.min_amount) && (reward < t.reward_amount)
  end
  reward
end

#get_line_items(itemizable, _tax_class_filter = nil) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'app/models/spiff_reward.rb', line 23

def get_line_items(itemizable, _tax_class_filter = nil)
  line_items = []
  min_quantities_met = true
  grouped_line_items = itemizable.line_items.group_by(&:item)

  # Pre-compute candidate item IDs once for targeted filter queries
  candidate_item_ids = grouped_line_items.keys.map(&:id)

  product_filters.where('min_qty > 0').find_each do |pf|
    qty = 0
    # Query only candidate items instead of ALL applicable items (~0.5-2ms vs ~1-5ms)
    matching_ids = pf.matching_item_ids(candidate_item_ids)
    # check min quantities met
    grouped_line_items.each do |item, lines|
      lines.each { |l| qty += l.quantity } if matching_ids.include?(item.id)
    end
    min_quantities_met = false if qty < pf.min_qty
  end

  if min_quantities_met == false
    []
  else
    product_filters.each do |pf|
      matching_ids = pf.matching_item_ids(candidate_item_ids)
      grouped_line_items.each do |item, lines|
        line_items += lines if matching_ids.include?(item.id)
      end
    end

    line_items
  end
end

#product_filtersActiveRecord::Relation<ProductFilter>

Returns:

See Also:



17
# File 'app/models/spiff_reward.rb', line 17

has_many :product_filters, as: :resource

#spiffSpiff

Returns:

See Also:



16
# File 'app/models/spiff_reward.rb', line 16

belongs_to :spiff, optional: true

#spiff_reward_thresholdsActiveRecord::Relation<SpiffRewardThreshold>

Returns:

See Also:



18
# File 'app/models/spiff_reward.rb', line 18

has_many :spiff_reward_thresholds