Class: SolidusPromotions::Calculators::PercentWithCap
- Inherits:
-
Spree::Calculator
- Object
- Spree::Calculator
- SolidusPromotions::Calculators::PercentWithCap
- Includes:
- PromotionCalculator
- Defined in:
- app/models/solidus_promotions/calculators/percent_with_cap.rb
Overview
A calculator that applies a percentage-based discount with a maximum cap.
This calculator computes a discount as a percentage of the line item’s discountable amount, but limits the total discount to a maximum amount distributed across all applicable line items. The actual discount applied is the lesser of the percentage discount and the proportional share of the maximum cap.
Instance Method Summary collapse
-
#compute_line_item(line_item) ⇒ BigDecimal
Computes the discount for a line item, capped at a maximum amount.
Methods included from PromotionCalculator
Instance Method Details
#compute_line_item(line_item) ⇒ BigDecimal
Computes the discount for a line item, capped at a maximum amount.
Calculates both a percentage-based discount and a distributed maximum discount, then returns whichever is smaller. This ensures the discount never exceeds the line item’s proportional share of the maximum cap, even if the percentage would result in a larger discount.
49 50 51 52 53 54 55 56 57 |
# File 'app/models/solidus_promotions/calculators/percent_with_cap.rb', line 49 def compute_line_item(line_item) percent_discount = round_to_currency(line_item.discountable_amount * preferred_percent / 100, line_item.order.currency) max_discount = DistributedAmount.new( calculable:, preferred_amount: preferred_max_amount ).compute_line_item(line_item) [percent_discount, max_discount].min end |