Class: Workarea::Pricing::Discount::Order
- Inherits:
-
Object
- Object
- Workarea::Pricing::Discount::Order
- Defined in:
- app/models/workarea/pricing/discount/order.rb
Overview
This class is a wrapper for Order, which includes logic for discount eligibility (e.g. some SKUs can’t be discounted).
Used in discount calculation, wraps the order and is passed to each discount for qualification and application.
Instance Method Summary collapse
-
#add_item(item) ⇒ Workarea::Order::Item
Add an item to the order (without persisting).
-
#allow_sale_items? ⇒ Boolean
Whether to allow sale items in the evaluation of this discount/order combination.
-
#include_item?(item) ⇒ Boolean
Whether or not an item should be included in discount qualification and value calculations.
-
#initialize(order, shippings = [], discount = nil) ⇒ Order
constructor
A new instance of Order.
-
#items ⇒ Array<Workarea::Order::Item>
Only return items that are discountable.
- #method_missing(sym, *args, &block) ⇒ Object
-
#quantity ⇒ Integer
The total quantity of only discountable items.
-
#remove_item(item) ⇒ Workarea::Order::Item
Remove an item to the order (without persisting).
- #respond_to_missing?(method_name, include_private = false) ⇒ Boolean
-
#shippings ⇒ Array<Shipping>
The shipping associated with the order for the purpose of this pricing request.
-
#subtotal_price ⇒ Money
The subtotal, not including items that cannot be discounted.
Constructor Details
#initialize(order, shippings = [], discount = nil) ⇒ Order
Returns a new instance of Order.
11 12 13 14 15 |
# File 'app/models/workarea/pricing/discount/order.rb', line 11 def initialize(order, shippings = [], discount = nil) @order = order @shippings = Array(shippings) @discount = discount end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(sym, *args, &block) ⇒ Object
98 99 100 |
# File 'app/models/workarea/pricing/discount/order.rb', line 98 def method_missing(sym, *args, &block) @order.send(sym, *args, &block) if @order.respond_to?(sym) end |
Instance Method Details
#add_item(item) ⇒ Workarea::Order::Item
Add an item to the order (without persisting). Used in FreeGift discounts.
66 67 68 |
# File 'app/models/workarea/pricing/discount/order.rb', line 66 def add_item(item) @order.items << item end |
#allow_sale_items? ⇒ Boolean
Whether to allow sale items in the evaluation of this discount/order combination.
22 23 24 |
# File 'app/models/workarea/pricing/discount/order.rb', line 22 def allow_sale_items? @discount.blank? || @discount.allow_sale_items? end |
#include_item?(item) ⇒ Boolean
Whether or not an item should be included in discount qualification and value calculations.
85 86 87 88 89 90 |
# File 'app/models/workarea/pricing/discount/order.rb', line 85 def include_item?(item) item.discountable? && (allow_sale_items? || !item.on_sale?) && !@discount&.excludes_product_id?(item.product_id) && item.category_ids.none? { |cid| @discount&.excludes_category_id?(cid) } end |
#items ⇒ Array<Workarea::Order::Item>
Only return items that are discountable.
39 40 41 |
# File 'app/models/workarea/pricing/discount/order.rb', line 39 def items @order.items.select(&method(:include_item?)) end |
#quantity ⇒ Integer
The total quantity of only discountable items.
57 58 59 |
# File 'app/models/workarea/pricing/discount/order.rb', line 57 def quantity @quantity ||= items.sum(&:quantity) || 0 end |
#remove_item(item) ⇒ Workarea::Order::Item
Remove an item to the order (without persisting). Used in FreeGift discounts.
75 76 77 |
# File 'app/models/workarea/pricing/discount/order.rb', line 75 def remove_item(item) @order.items = @order.items.reject { |i| i.id == item.id } end |
#respond_to_missing?(method_name, include_private = false) ⇒ Boolean
93 94 95 |
# File 'app/models/workarea/pricing/discount/order.rb', line 93 def respond_to_missing?(method_name, include_private = false) super || @order.respond_to?(method_name) end |
#shippings ⇒ Array<Shipping>
The shipping associated with the order for the purpose of this pricing request.
31 32 33 |
# File 'app/models/workarea/pricing/discount/order.rb', line 31 def shippings @shippings end |
#subtotal_price ⇒ Money
The subtotal, not including items that cannot be discounted.
47 48 49 50 51 |
# File 'app/models/workarea/pricing/discount/order.rb', line 47 def subtotal_price items.reduce(0.to_m) do |memo, item| memo + item.price_adjustments.sum end end |