Class: ProductFilter::LineExtractor

Inherits:
Object
  • Object
show all
Includes:
Memery
Defined in:
app/services/product_filter/line_extractor.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(line_items, product_filters, tax_class, options = {}) ⇒ LineExtractor

Returns a new instance of LineExtractor.



16
17
18
19
20
21
22
# File 'app/services/product_filter/line_extractor.rb', line 16

def initialize(line_items, product_filters, tax_class, options = {})
  @line_items = line_items
  @product_filters = product_filters
  @tax_class = tax_class.to_s.downcase
  @options = options
  @logger = options[:logger] || Rails.logger
end

Instance Attribute Details

#line_itemsObject (readonly)

Qualifying items for the coupon to be effective, but does not necessarely mean those are the lines getting the discount. All product filter must be met to be a qualifying.
Required:
line_items to filter on, array of LineItem
product_filters, array of ProductFilter
tax_class: can be one of g, shp, svc, symbol or string
Other options are:

allow_non_domestic_shipping : for shp tax class, non domestic shipping (different origin and destination country) is not allowed, unless this is true
state_codes: destination state code filter for a line



14
15
16
# File 'app/services/product_filter/line_extractor.rb', line 14

def line_items
  @line_items
end

#loggerObject (readonly)

Qualifying items for the coupon to be effective, but does not necessarely mean those are the lines getting the discount. All product filter must be met to be a qualifying.
Required:
line_items to filter on, array of LineItem
product_filters, array of ProductFilter
tax_class: can be one of g, shp, svc, symbol or string
Other options are:

allow_non_domestic_shipping : for shp tax class, non domestic shipping (different origin and destination country) is not allowed, unless this is true
state_codes: destination state code filter for a line



14
15
16
# File 'app/services/product_filter/line_extractor.rb', line 14

def logger
  @logger
end

#optionsObject (readonly)

Qualifying items for the coupon to be effective, but does not necessarely mean those are the lines getting the discount. All product filter must be met to be a qualifying.
Required:
line_items to filter on, array of LineItem
product_filters, array of ProductFilter
tax_class: can be one of g, shp, svc, symbol or string
Other options are:

allow_non_domestic_shipping : for shp tax class, non domestic shipping (different origin and destination country) is not allowed, unless this is true
state_codes: destination state code filter for a line



14
15
16
# File 'app/services/product_filter/line_extractor.rb', line 14

def options
  @options
end

#product_filtersObject (readonly)

Qualifying items for the coupon to be effective, but does not necessarely mean those are the lines getting the discount. All product filter must be met to be a qualifying.
Required:
line_items to filter on, array of LineItem
product_filters, array of ProductFilter
tax_class: can be one of g, shp, svc, symbol or string
Other options are:

allow_non_domestic_shipping : for shp tax class, non domestic shipping (different origin and destination country) is not allowed, unless this is true
state_codes: destination state code filter for a line



14
15
16
# File 'app/services/product_filter/line_extractor.rb', line 14

def product_filters
  @product_filters
end

#tax_classObject (readonly)

Qualifying items for the coupon to be effective, but does not necessarely mean those are the lines getting the discount. All product filter must be met to be a qualifying.
Required:
line_items to filter on, array of LineItem
product_filters, array of ProductFilter
tax_class: can be one of g, shp, svc, symbol or string
Other options are:

allow_non_domestic_shipping : for shp tax class, non domestic shipping (different origin and destination country) is not allowed, unless this is true
state_codes: destination state code filter for a line



14
15
16
# File 'app/services/product_filter/line_extractor.rb', line 14

def tax_class
  @tax_class
end

Class Method Details

.retrieve_line_tax_class(line) ⇒ Object



24
25
26
# File 'app/services/product_filter/line_extractor.rb', line 24

def self.retrieve_line_tax_class(line)
  (line.tax_class || line.item.tax_class).to_s.downcase
end

Instance Method Details

#destination_filtering(line_items) ⇒ Object

def skip_edi_line_filtering(line_items)
line_items = line_items.reject { |li| li.edi_reference.present? && li&.resource.is_a?(Order) }
end



69
70
71
72
73
74
75
76
# File 'app/services/product_filter/line_extractor.rb', line 69

def destination_filtering(line_items)
  if @options[:state_codes].present? and !@options[:state_codes].all?(&:blank?)
    # only applied to lines shipping to specific states
    line_items.select { |li| @options[:state_codes].include?(li&.resource&.shipping_address&.state_code) }
  else
    line_items
  end
end

#discountable_line_itemsObject

Gets the line items which will have the discount applied to them



79
80
81
82
83
84
85
# File 'app/services/product_filter/line_extractor.rb', line 79

def discountable_line_items
  line_items = preselect_line_items
  line_items = discounted_price_filtering(line_items)
  line_items = tax_class_filtering(line_items)
  # line_items = skip_edi_line_filtering(line_items)
  destination_filtering(line_items)
end

#discounted_price_filtering(line_items) ⇒ Object



50
51
52
# File 'app/services/product_filter/line_extractor.rb', line 50

def discounted_price_filtering(line_items)
  line_items.reject { |li| li.discounted_price.zero? }
end

#get_base_amount_discountedObject



88
89
90
91
92
# File 'app/services/product_filter/line_extractor.rb', line 88

def get_base_amount_discounted
  # Use in-memory discounted_price (not database discounted_total) during calculation
  # discounted_total relies on persisted line_discounts which are stale during calculation
  discountable_line_items.sum { |li| li.discounted_price * li.quantity }
end

#get_base_amount_msrpObject



94
95
96
97
98
# File 'app/services/product_filter/line_extractor.rb', line 94

def get_base_amount_msrp
  # For manual adjustments: cap against original MSRP, not already-discounted price
  # This ensures manual coupons like F3-A override previous discounts instead of stacking
  discountable_line_items.sum { |li| li.price * li.quantity }
end

#get_base_linear_ftObject



108
109
110
# File 'app/services/product_filter/line_extractor.rb', line 108

def get_base_linear_ft
  discountable_line_items.sum(&:linear_ft_total)
end

#get_base_quantitiesObject



100
101
102
# File 'app/services/product_filter/line_extractor.rb', line 100

def get_base_quantities
  discountable_line_items.sum(&:quantity)
end

#get_base_sq_ftObject



104
105
106
# File 'app/services/product_filter/line_extractor.rb', line 104

def get_base_sq_ft
  discountable_line_items.sum(&:sq_ft_total)
end

#preselect_line_itemsObject

Preselect line items based on product filter



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'app/services/product_filter/line_extractor.rb', line 29

def preselect_line_items
  line_items = []
  pf_apply_lines = @product_filters.select(&:apply_discount)
  if pf_apply_lines.empty?
    line_items = @line_items
  else
    # Pre-compute: group line items by item and extract candidate IDs once
    lines_by_item = @line_items.group_by(&:item)
    candidate_item_ids = lines_by_item.keys.map(&:id)

    pf_apply_lines.each do |pf|
      # Query only candidate items instead of ALL applicable items (~0.5-2ms vs ~1-5ms)
      matching_ids = pf.matching_item_ids(candidate_item_ids)
      lines_by_item.each do |item, lines|
        line_items = (line_items | lines) if matching_ids.include?(item.id)
      end
    end
  end
  line_items
end

#qty_min_repeatObject



112
113
114
115
116
# File 'app/services/product_filter/line_extractor.rb', line 112

def qty_min_repeat
  lq = ProductFilter::LineQualifier.new(line_items, product_filters)
  lq.qualifying_line_items?
  lq.qty_min_repeat
end

#tax_class_filtering(line_items) ⇒ Object



54
55
56
57
58
59
60
61
62
63
# File 'app/services/product_filter/line_extractor.rb', line 54

def tax_class_filtering(line_items)
  filtered_line_items = line_items.select { |li| self.class.retrieve_line_tax_class(li) == @tax_class }

  if @tax_class == 'shp' and !@options[:allow_non_domestic_shipping]
    # non domestic shipping is not allowed, so only select those with the same origin and destination country
    filtered_line_items.select { |li| !li.is_international? }
  else
    filtered_line_items
  end
end