Class: ProductFilter::LineExtractor
- Inherits:
-
Object
- Object
- ProductFilter::LineExtractor
- Includes:
- Memery
- Defined in:
- app/services/product_filter/line_extractor.rb
Overview
Service object: line extractor.
Instance Attribute Summary collapse
-
#line_items ⇒ Object
readonly
Qualifying items for the coupon to be effective, but does not necessarely mean those are the lines getting the discount.
-
#logger ⇒ Object
readonly
Qualifying items for the coupon to be effective, but does not necessarely mean those are the lines getting the discount.
-
#options ⇒ Object
readonly
Qualifying items for the coupon to be effective, but does not necessarely mean those are the lines getting the discount.
-
#product_filters ⇒ Object
readonly
Qualifying items for the coupon to be effective, but does not necessarely mean those are the lines getting the discount.
-
#tax_class ⇒ Object
readonly
Qualifying items for the coupon to be effective, but does not necessarely mean those are the lines getting the discount.
Class Method Summary collapse
Instance Method Summary collapse
-
#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.
-
#discountable_line_items ⇒ Object
Gets the line items which will have the discount applied to them.
- #discounted_price_filtering(line_items) ⇒ Object
- #get_base_amount_discounted ⇒ Object
- #get_base_amount_msrp ⇒ Object
- #get_base_linear_ft ⇒ Object
- #get_base_quantities ⇒ Object
- #get_base_sq_ft ⇒ Object
-
#initialize(line_items, product_filters, tax_class, options = {}) ⇒ LineExtractor
constructor
A new instance of LineExtractor.
-
#preselect_line_items ⇒ Object
Preselect line items based on product filter.
- #qty_min_repeat ⇒ Object
- #tax_class_filtering(line_items) ⇒ Object
Constructor Details
#initialize(line_items, product_filters, tax_class, options = {}) ⇒ LineExtractor
Returns a new instance of LineExtractor.
24 25 26 27 28 29 30 |
# File 'app/services/product_filter/line_extractor.rb', line 24 def initialize(line_items, product_filters, tax_class, = {}) @line_items = line_items @product_filters = product_filters @tax_class = tax_class.to_s.downcase @options = @logger = [:logger] || Rails.logger end |
Instance Attribute Details
#line_items ⇒ Object (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
15 16 17 |
# File 'app/services/product_filter/line_extractor.rb', line 15 def line_items @line_items end |
#logger ⇒ Object (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
15 16 17 |
# File 'app/services/product_filter/line_extractor.rb', line 15 def logger @logger end |
#options ⇒ Object (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
15 16 17 |
# File 'app/services/product_filter/line_extractor.rb', line 15 def @options end |
#product_filters ⇒ Object (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
15 16 17 |
# File 'app/services/product_filter/line_extractor.rb', line 15 def product_filters @product_filters end |
#tax_class ⇒ Object (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
15 16 17 |
# File 'app/services/product_filter/line_extractor.rb', line 15 def tax_class @tax_class end |
Class Method Details
.retrieve_line_tax_class(line) ⇒ Object
32 33 34 |
# File 'app/services/product_filter/line_extractor.rb', line 32 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
76 77 78 79 80 81 82 83 |
# File 'app/services/product_filter/line_extractor.rb', line 76 def destination_filtering(line_items) if @options[:state_codes].present? && !@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_items ⇒ Object
Gets the line items which will have the discount applied to them
86 87 88 89 90 91 92 |
# File 'app/services/product_filter/line_extractor.rb', line 86 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
58 59 60 |
# File 'app/services/product_filter/line_extractor.rb', line 58 def discounted_price_filtering(line_items) line_items.reject { |li| li.discounted_price.zero? } end |
#get_base_amount_discounted ⇒ Object
95 96 97 98 99 |
# File 'app/services/product_filter/line_extractor.rb', line 95 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_msrp ⇒ Object
101 102 103 104 105 |
# File 'app/services/product_filter/line_extractor.rb', line 101 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_ft ⇒ Object
115 116 117 |
# File 'app/services/product_filter/line_extractor.rb', line 115 def get_base_linear_ft discountable_line_items.sum(&:linear_ft_total) end |
#get_base_quantities ⇒ Object
107 108 109 |
# File 'app/services/product_filter/line_extractor.rb', line 107 def get_base_quantities discountable_line_items.sum(&:quantity) end |
#get_base_sq_ft ⇒ Object
111 112 113 |
# File 'app/services/product_filter/line_extractor.rb', line 111 def get_base_sq_ft discountable_line_items.sum(&:sq_ft_total) end |
#preselect_line_items ⇒ Object
Preselect line items based on product filter
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'app/services/product_filter/line_extractor.rb', line 37 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_repeat ⇒ Object
119 120 121 122 123 |
# File 'app/services/product_filter/line_extractor.rb', line 119 def qty_min_repeat lq = ProductFilter::LineQualifier.new(line_items, product_filters) lq. lq.qty_min_repeat end |
#tax_class_filtering(line_items) ⇒ Object
62 63 64 65 66 67 68 69 70 71 |
# File 'app/services/product_filter/line_extractor.rb', line 62 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') && !@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.reject(&:is_international?) else filtered_line_items end end |