Class: SpiffReward
- Inherits:
-
ApplicationRecord
- Object
- ActiveRecord::Base
- ApplicationRecord
- SpiffReward
- 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 Models::Schedulable
Models::Schedulable::SIMPLE_FORM_OPTIONS
Belongs to collapse
Methods included from Models::Auditable
Has many collapse
- #product_filters ⇒ ActiveRecord::Relation<ProductFilter>
- #spiff_reward_thresholds ⇒ ActiveRecord::Relation<SpiffRewardThreshold>
Instance Method Summary collapse
- #eligible_amount(itemizable) ⇒ BigDecimal
- #eligible_reward(itemizable) ⇒ BigDecimal
- #get_line_items(itemizable, _tax_class_filter = nil) ⇒ Array<LineItem>
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 Models::Schedulable
Methods included from Models::AfterCommittable
Methods included from Models::EventPublishable
Instance Method Details
#eligible_amount(itemizable) ⇒ BigDecimal
63 64 65 |
# File 'app/models/spiff_reward.rb', line 63 def eligible_amount(itemizable) get_line_items(itemizable).sum(&:total) end |
#eligible_reward(itemizable) ⇒ BigDecimal
69 70 71 72 73 74 75 76 |
# File 'app/models/spiff_reward.rb', line 69 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) ⇒ Array<LineItem>
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 55 56 57 58 59 |
# File 'app/models/spiff_reward.rb', line 28 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_filters ⇒ ActiveRecord::Relation<ProductFilter>
18 |
# File 'app/models/spiff_reward.rb', line 18 has_many :product_filters, as: :resource, dependent: :destroy |
#spiff_reward_thresholds ⇒ ActiveRecord::Relation<SpiffRewardThreshold>
20 |
# File 'app/models/spiff_reward.rb', line 20 has_many :spiff_reward_thresholds, dependent: :destroy |