Class: Coupon::Calculator::PromoMatrix

Inherits:
Base
  • Object
show all
Defined in:
app/services/coupon/calculator/promo_matrix.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 = {}) ⇒ PromoMatrix

Returns a new instance of PromoMatrix.



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

def initialize(line_item_extractor, options = {})
  super(line_item_extractor, options)
end

Instance Method Details

#calculate(discount) ⇒ Object



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

def calculate(discount)
  discount.amount = 0.0
  discount.line_discounts.each{|ld| ld.amount = 0.0 }
  @line_item_extractor.discountable_line_items.each do |line_item|
    logger.info "Calculating discount on line item #{line_item.id} for discount #{discount.id}"
    new_unit_amount = line_item.catalog_item&.sale_price
    # Here we set the allocated unit amount to the difference between the current discounted price and the target sale price
    # But in case there is no sale price set for this item, we allocate nothing, which will result in the line discount being cleared
    allocated_unit_amount = new_unit_amount ? -[line_item.discounted_price - new_unit_amount,0].max : 0.0
    discount.notes ||= +""
    discount.notes << "Unit price set to $#{new_unit_amount} for item id #{line_item.item_id}. "
    allocate_to_line(line_item, discount, allocated_unit_amount)
  end
end