Created
December 10, 2021 04:16
-
-
Save mrdougwright/d9b19c84bc5a102b048bd97648a89835 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def bucket_items_applies?(discount) | |
@application_count = 0 | |
discount.bucket.bucket_items.all.each do |bucket_item| | |
@cloned_items = items.map(&:clone) | |
matching_line_item_product_index = find_line_item_product_index(bucket_item) | |
return false if matching_line_item_product_index.blank? | |
allocate_discount(matching_line_item_product_index: matching_line_item_product_index, discount: discount, bucket_item: bucket_item) | |
line_token = reduce_line_quantities(bucket_item) | |
@applicable_lines << line_token | |
end | |
true | |
end | |
def find_line_item_product_index(bucket_item) | |
product_present? = bucket_item.product.present? | |
bucket_item_type_id = product_present? ? bucket_item.product.id : bucket_item.line.id | |
line_item_product_index.select { |_, product| (product_present? product.id : product.line.id) == bucket_item_type_id } | |
end | |
def reduce_line_quantities(bucket_item) | |
line_token = bucket_item.product.present? ? bucket_item.product.line.to_gid.to_s : bucket_item.line.to_gid.to_s | |
@line_quantities_index[line_token] -= bucket_item.minimum_quantity | |
line_token | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment