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
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) ⇒ Object
- #eligible_reward(itemizable) ⇒ Object
- #get_line_items(itemizable, tax_class_filter = nil) ⇒ Object
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::EventPublishable
Instance Method Details
#eligible_amount(itemizable) ⇒ Object
58 59 60 |
# File 'app/models/spiff_reward.rb', line 58 def eligible_amount(itemizable) get_line_items(itemizable).sum {|li| li.total} end |
#eligible_reward(itemizable) ⇒ Object
62 63 64 65 66 67 68 69 70 71 |
# File 'app/models/spiff_reward.rb', line 62 def eligible_reward(itemizable) amount = self.eligible_amount(itemizable) reward = BigDecimal("0") self.spiff_reward_thresholds.each do |t| if amount.positive? and amount >= t.min_amount and reward < t.reward_amount reward = t.reward_amount end end return 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 55 56 |
# 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').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| if matching_ids.include?(item.id) lines.each { |l| qty += l.quantity } end 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>
17 |
# File 'app/models/spiff_reward.rb', line 17 has_many :product_filters, :as => :resource |
#spiff_reward_thresholds ⇒ ActiveRecord::Relation<SpiffRewardThreshold>
18 |
# File 'app/models/spiff_reward.rb', line 18 has_many :spiff_reward_thresholds |