Class: Workarea::Pricing::Discount::ApplicationGroup
- Inherits:
-
Object
- Object
- Workarea::Pricing::Discount::ApplicationGroup
- Defined in:
- app/models/workarea/pricing/discount/application_group.rb
Instance Attribute Summary collapse
-
#discounts ⇒ Object
readonly
Returns the value of attribute discounts.
-
#order ⇒ Object
readonly
Returns the value of attribute order.
-
#shippings ⇒ Object
readonly
Returns the value of attribute shippings.
Class Method Summary collapse
Instance Method Summary collapse
- #apply ⇒ Object
-
#initialize(discounts, order, shippings) ⇒ ApplicationGroup
constructor
A new instance of ApplicationGroup.
- #value ⇒ Object
Constructor Details
#initialize(discounts, order, shippings) ⇒ ApplicationGroup
Returns a new instance of ApplicationGroup.
82 83 84 85 86 |
# File 'app/models/workarea/pricing/discount/application_group.rb', line 82 def initialize(discounts, order, shippings) @discounts = discounts.sort @order = order @shippings = shippings end |
Instance Attribute Details
#discounts ⇒ Object (readonly)
Returns the value of attribute discounts.
5 6 7 |
# File 'app/models/workarea/pricing/discount/application_group.rb', line 5 def discounts @discounts end |
#order ⇒ Object (readonly)
Returns the value of attribute order.
5 6 7 |
# File 'app/models/workarea/pricing/discount/application_group.rb', line 5 def order @order end |
#shippings ⇒ Object (readonly)
Returns the value of attribute shippings.
5 6 7 |
# File 'app/models/workarea/pricing/discount/application_group.rb', line 5 def shippings @shippings end |
Class Method Details
.calculate(discounts, order, shippings) ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'app/models/workarea/pricing/discount/application_group.rb', line 8 def calculate(discounts, order, shippings) expiration = Workarea.config.cache_expirations.discount_application_groups groups = Rails.cache.fetch(current_cache_key, expires_in: expiration, race_condition_ttl: 10) do # Ensure an undirected graph of compatibility on discounts discounts.each do |discount| discount.compatible_discount_ids.each do |id| compatible_discount = discounts.detect { |d| d.id.to_s == id } next unless compatible_discount.present? # compatible_discounts should be the open neighborhood of the # discount i.e. it shouldn't include itself unless compatible_discount == discount discount.compatible_discounts << compatible_discount compatible_discount.compatible_discounts << discount end end end results = [] build_groups(remaining: discounts.to_set, results: results) results end groups.map { |set| new(set.to_a, order, shippings) } end |
Instance Method Details
#apply ⇒ Object
88 89 90 91 92 93 94 95 |
# File 'app/models/workarea/pricing/discount/application_group.rb', line 88 def apply discounts.each do |discount| discount_order = Discount::Order.new(order, shippings, discount) next unless discount.qualifies?(discount_order) discount.apply(discount_order) end end |
#value ⇒ Object
97 98 99 100 101 102 103 104 105 |
# File 'app/models/workarea/pricing/discount/application_group.rb', line 97 def value @value ||= begin apply result = calculate_applied_value remove_discounts result end.to_m end |