Class: SolidusPromotions::Calculators::FlatRate
- Inherits:
-
Spree::Calculator
- Object
- Spree::Calculator
- SolidusPromotions::Calculators::FlatRate
- Includes:
- PromotionCalculator
- Defined in:
- app/models/solidus_promotions/calculators/flat_rate.rb
Overview
A calculator that applies a flat rate discount amount.
This calculator returns a fixed discount amount if the item’s order currency matches the preferred currency, otherwise it returns zero.
Instance Method Summary collapse
-
#compute_item(item) ⇒ BigDecimal
(also: #compute_line_item, #compute_shipment, #compute_shipping_rate)
Computes the discount amount for an item.
- #compute_price(price, options = {}) ⇒ Object
Methods included from PromotionCalculator
Instance Method Details
#compute_item(item) ⇒ BigDecimal Also known as: compute_line_item, compute_shipment, compute_shipping_rate
Computes the discount amount for an item.
Returns the preferred amount if the item’s order currency matches the preferred currency, otherwise returns 0.
35 36 37 38 39 40 41 42 |
# File 'app/models/solidus_promotions/calculators/flat_rate.rb', line 35 def compute_item(item) currency = item.order.currency if item && preferred_currency.casecmp(currency).zero? compute_for_amount(item.discountable_amount) else Spree::ZERO end end |
#compute_price(price, options = {}) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'app/models/solidus_promotions/calculators/flat_rate.rb', line 47 def compute_price(price, = {}) order = [:order] quantity = [:quantity] return preferred_amount unless order return Spree::ZERO if order.currency != preferred_currency line_item_with_variant = order.line_items.detect { _1.variant == price.variant } desired_extra_amount = quantity * price.discountable_amount current_discounted_amount = line_item_with_variant ? line_item_with_variant.discountable_amount : Spree::ZERO round_to_currency( (compute_for_amount(current_discounted_amount + desired_extra_amount.to_f) - compute_for_amount(current_discounted_amount)) / quantity, preferred_currency ) end |