Class: Workarea::Pricing::Discount::BuySomeGetSome
- Inherits:
-
Workarea::Pricing::Discount
- Object
- Workarea::Pricing::Discount
- Workarea::Pricing::Discount::BuySomeGetSome
- Defined in:
- app/models/workarea/pricing/discount/buy_some_get_some.rb
Overview
This class allows discounts of the form:
-
Buy 2, get 1 free
-
Buy 3, get 1 50% off
Defined Under Namespace
Classes: ItemApplication, OrderItemsByProduct, ProductApplication
Constant Summary
Constants included from Conditions::OrderTotal
Conditions::OrderTotal::OPERATORS
Instance Attribute Summary collapse
-
#apply_quantity ⇒ Integer
The quantity discounted.
-
#category_ids ⇒ Array
For items in these categories.
-
#max_applications ⇒ Integer
The maximum number of applications an item can get.
-
#percent_off ⇒ Float
The percent off, between 1 and 100.
-
#product_ids ⇒ Array
For items in these product_ids.
-
#purchase_quantity ⇒ Integer
The buy quantity.
Attributes inherited from Workarea::Pricing::Discount
#allow_sale_items, #auto_deactivated, #auto_deactivated_at, #compatible_discount_ids, #excluded_category_ids, #excluded_product_ids, #name, #price_level, #redemptions, #single_use
Class Method Summary collapse
Instance Method Summary collapse
-
#apply(order) ⇒ Object
Create the item price adjustments for items that qualify on the passed order.
-
#percent ⇒ Float
The float amount version of #percent_off for use in price calculation.
-
#qualifies?(order) ⇒ Boolean
Includes checking the necessary quantity and either a product or category match on an item.
-
#total_quantity ⇒ Integer
The total minimum quantity an item would need to qualify for receiving this discount.
Methods included from Conditions::UserTags
#user_tag?, #user_tags_qualify?
Methods included from Conditions::PromoCodes
#generated_codes, #promo_code?, #promo_codes_qualify?, #valid?
Methods included from Conditions::OrderTotal
#order_total?, #order_total_qualifies?
Methods inherited from Workarea::Pricing::Discount
#<=>, add_qualifier, auto_deactivate, #auto_deactivate!, #auto_deactivated?, #auto_deactivates_at, #can_be_used_by?, #compatible_discounts, #compatible_with?, #excludes_category_id?, #excludes_product_id?, #has_been_redeemed?, #last_redemption, #log_redemption, qualifier_methods, #remove_from
Methods included from Commentable
#add_subscription, #remove_subscription
Methods included from Releasable
#changesets_with_children, #destroy, #in_release, #release_changes, #release_originals, #save_changeset, #skip_changeset, #without_release
Methods included from Segmentable
#active?, #active_segment_ids_with_children, #segmented?, #segments
Methods included from Release::Activation
#activate_with?, #create_activation_changeset, #save_activate_with, #was_active?
Methods included from Mongoid::Document::Taggable
Methods included from ApplicationDocument
Methods included from Sidekiq::Callbacks
add_worker, assert_valid_config!, async, caching_classes?, disable, enable, inline, #run_callbacks, workers, workers_list
Methods included from Mongoid::Document
Instance Attribute Details
#apply_quantity ⇒ Integer
Returns the quantity discounted.
21 |
# File 'app/models/workarea/pricing/discount/buy_some_get_some.rb', line 21 field :apply_quantity, type: Integer |
#category_ids ⇒ Array
Returns for items in these categories.
42 |
# File 'app/models/workarea/pricing/discount/buy_some_get_some.rb', line 42 field :category_ids, type: Array, default: [] |
#max_applications ⇒ Integer
Returns the maximum number of applications an item can get.
31 |
# File 'app/models/workarea/pricing/discount/buy_some_get_some.rb', line 31 field :max_applications, type: Integer |
#percent_off ⇒ Float
Returns the percent off, between 1 and 100.
26 |
# File 'app/models/workarea/pricing/discount/buy_some_get_some.rb', line 26 field :percent_off, type: Float |
#product_ids ⇒ Array
Returns for items in these product_ids.
36 |
# File 'app/models/workarea/pricing/discount/buy_some_get_some.rb', line 36 field :product_ids, type: Array, default: [] |
#purchase_quantity ⇒ Integer
Returns the buy quantity.
16 |
# File 'app/models/workarea/pricing/discount/buy_some_get_some.rb', line 16 field :purchase_quantity, type: Integer |
Class Method Details
.model_name ⇒ Object
51 52 53 |
# File 'app/models/workarea/pricing/discount/buy_some_get_some.rb', line 51 def self.model_name Discount.model_name end |
Instance Method Details
#apply(order) ⇒ Object
Create the item price adjustments for items that qualify on the passed order.
76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'app/models/workarea/pricing/discount/buy_some_get_some.rb', line 76 def apply(order) OrderItemsByProduct.new(order).each do |product| next unless product_qualifies?(product) ProductApplication.new(self, product).items.each do |item, qty| application = ItemApplication.new(self, item, qty) next if application.value <= 0 item.adjust_pricing( adjustment_data(application.value, application.apply_quantity) ) end end end |
#percent ⇒ Float
The float amount version of #percent_off for use in price calculation.
105 106 107 |
# File 'app/models/workarea/pricing/discount/buy_some_get_some.rb', line 105 def percent 1 - (percent_off / 100) end |
#qualifies?(order) ⇒ Boolean
Includes checking the necessary quantity and either a product or category match on an item.
67 68 69 |
# File 'app/models/workarea/pricing/discount/buy_some_get_some.rb', line 67 def qualifies?(order) super && OrderItemsByProduct.new(order).any? { |p| product_qualifies?(p) } end |
#total_quantity ⇒ Integer
The total minimum quantity an item would need to qualify for receiving this discount.
96 97 98 |
# File 'app/models/workarea/pricing/discount/buy_some_get_some.rb', line 96 def total_quantity purchase_quantity + apply_quantity end |