Class: SolidusPromotions::Calculators::DistributedAmount
- Inherits:
-
Spree::Calculator
- Object
- Spree::Calculator
- SolidusPromotions::Calculators::DistributedAmount
- Includes:
- PromotionCalculator
- Defined in:
- app/models/solidus_promotions/calculators/distributed_amount.rb
Overview
A calculator that distributes a fixed discount amount across line items based on their value.
This calculator takes a preferred total discount amount and distributes it proportionally across applicable line items based on their prices. More expensive line items receive a greater share of the discount.
Instance Method Summary collapse
-
#compute_line_item(line_item) ⇒ BigDecimal
Computes the weighted discount amount for a specific line item.
Methods included from PromotionCalculator
Instance Method Details
#compute_line_item(line_item) ⇒ BigDecimal
Computes the weighted discount amount for a specific line item.
The discount is calculated by distributing the preferred amount across all applicable line items, weighted by their prices. Returns 0 if:
-
The line item is nil
-
The currency doesn’t match the preferred currency
-
The line item is not in the list of applicable line items
43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'app/models/solidus_promotions/calculators/distributed_amount.rb', line 43 def compute_line_item(line_item) return Spree::ZERO unless line_item return Spree::ZERO unless preferred_currency.casecmp(line_item.currency).zero? distributable_line_items = calculable.applicable_line_items(line_item.order) return Spree::ZERO unless line_item.in?(distributable_line_items) DistributedAmountsHandler.new( distributable_line_items, preferred_amount ).amount(line_item) end |