Class: Workarea::Pricing::Discount::BuySomeGetSome

Inherits:
Workarea::Pricing::Discount show all
Includes:
Conditions::OrderTotal, Conditions::PromoCodes, Conditions::UserTags
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

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

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

included

Methods included from ApplicationDocument

#releasable?

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

#embedded_children

Instance Attribute Details

#apply_quantityInteger

Returns the quantity discounted.

Returns:

  • (Integer)

    the quantity discounted



21
# File 'app/models/workarea/pricing/discount/buy_some_get_some.rb', line 21

field :apply_quantity, type: Integer

#category_idsArray

Returns for items in these categories.

Returns:

  • (Array)

    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_applicationsInteger

Returns the maximum number of applications an item can get.

Returns:

  • (Integer)

    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_offFloat

Returns the percent off, between 1 and 100.

Returns:

  • (Float)

    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_idsArray

Returns for items in these product_ids.

Returns:

  • (Array)

    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_quantityInteger

Returns the buy quantity.

Returns:

  • (Integer)

    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_nameObject



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.

Parameters:



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

#percentFloat

The float amount version of #percent_off for use in price calculation.

Returns:



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.

Parameters:

Returns:

  • (Boolean)


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_quantityInteger

The total minimum quantity an item would need to qualify for receiving this discount.

Returns:

  • (Integer)


96
97
98
# File 'app/models/workarea/pricing/discount/buy_some_get_some.rb', line 96

def total_quantity
  purchase_quantity + apply_quantity
end