Class: Coupon::Calculator::ForceDiscountUnitPrice

Inherits:
Base
  • Object
show all
Defined in:
app/services/coupon/calculator/force_discount_unit_price.rb

Overview

Force Discount Unit Price sets it to a specific amount, regardless
of trade discount

Instance Method Summary collapse

Constructor Details

#initialize(line_item_extractor, options = {}) ⇒ ForceDiscountUnitPrice

Returns a new instance of ForceDiscountUnitPrice.



5
6
7
8
# File 'app/services/coupon/calculator/force_discount_unit_price.rb', line 5

def initialize(line_item_extractor, options = {})
  raise ":unit_price option must be specified" unless options[:unit_price].present?
  super(line_item_extractor, options)
end

Instance Method Details

#calculate(discount) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'app/services/coupon/calculator/force_discount_unit_price.rb', line 10

def calculate(discount)
  @line_item_extractor.discountable_line_items.each do |line_item|
    # e.g you want to make an item a certain price, you find how much you need to discount it
    # if discounted price is 300 and you want the item to be 100 then you do 300 - 100 = -200 which is what you need to apply
    allocated_amount = @options[:unit_price] - line_item.discounted_price
    discount.notes ||= ""
    if allocated_amount < 0 
      allocate_to_line(line_item, discount, allocated_amount)
      discount.notes += "Discounted price target is $#{@options[:unit_price]}. Coupon will be $#{allocated_amount}"
    else
      discount.notes += "Discounted price target is $#{@options[:unit_price]}.  But line is already discounted beyond this amount"
    end
  end
end